Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / toolkit / xre / nsWindowsDllBlocklist.cpp
blob07b76aea82dc5624b1a81ac4b3b45ba4ff365a5c
1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Corp
19 * Portions created by the Initial Developer are Copyright (C) 2009
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Vladimir Vukicevic <vladimir@pobox.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include <windows.h>
40 #include <winternl.h>
42 #include <stdio.h>
44 #include "nsAutoPtr.h"
46 #include "prlog.h"
48 #include "nsWindowsDllInterceptor.h"
50 #define IN_WINDOWS_DLL_BLOCKLIST
51 #include "nsWindowsDllBlocklist.h"
53 #ifndef STATUS_DLL_NOT_FOUND
54 #define STATUS_DLL_NOT_FOUND ((DWORD)0xC0000135L)
55 #endif
57 // define this for very verbose dll load debug spew
58 #undef DEBUG_very_verbose
60 // The signature for LdrLoadDll changed at some point, with the second arg
61 // becoming a PULONG instead of a ULONG. This should only matter on 64-bit
62 // systems, for which there was no support earlier -- on 32-bit systems,
63 // they should be the same size.
64 PR_STATIC_ASSERT(sizeof(PULONG) == sizeof(ULONG));
66 typedef NTSTATUS (NTAPI *LdrLoadDll_func) (PWCHAR filePath, PULONG flags, PUNICODE_STRING moduleFileName, PHANDLE handle);
68 static LdrLoadDll_func stub_LdrLoadDll = 0;
70 static NTSTATUS NTAPI
71 patched_LdrLoadDll (PWCHAR filePath, PULONG flags, PUNICODE_STRING moduleFileName, PHANDLE handle)
73 // We have UCS2 (UTF16?), we want ASCII, but we also just want the filename portion
74 #define DLLNAME_MAX 128
75 char dllName[DLLNAME_MAX+1];
77 int len = moduleFileName->Length / 2;
78 wchar_t *fname = moduleFileName->Buffer;
80 // The filename isn't guaranteed to be null terminated, but in practice
81 // it always will be; ensure that this is so, and bail if not.
82 // This is done instead of the more robust approach because of bug 527122,
83 // where lots of weird things were happening when we tried to make a copy.
84 if (moduleFileName->MaximumLength < moduleFileName->Length+2 ||
85 fname[len] != 0)
87 #ifdef DEBUG
88 printf_stderr("LdrLoadDll: non-null terminated string found!\n");
89 #endif
90 goto continue_loading;
93 wchar_t *dll_part = wcsrchr(fname, L'\\');
94 if (dll_part) {
95 dll_part = dll_part + 1;
96 len -= dll_part - fname;
97 } else {
98 dll_part = fname;
101 #ifdef DEBUG_very_verbose
102 printf_stderr("LdrLoadDll: dll_part '%S' %d\n", dll_part, len);
103 #endif
105 // if it's too long, then, we assume we won't want to block it,
106 // since DLLNAME_MAX should be at least long enough to hold the longest
107 // entry in our blocklist.
108 if (len > DLLNAME_MAX) {
109 #ifdef DEBUG
110 printf_stderr("LdrLoadDll: len too long! %d\n", len);
111 #endif
112 goto continue_loading;
115 // copy over to our char byte buffer, lowercasing ASCII as we go
116 for (int i = 0; i < len; i++) {
117 wchar_t c = dll_part[i];
119 if (c > 0x7f) {
120 // welp, it's not ascii; if we need to add non-ascii things to
121 // our blocklist, we'll have to remove this limitation.
122 goto continue_loading;
125 // ensure that dll name is all lowercase
126 if (c >= 'A' && c <= 'Z')
127 c += 'a' - 'A';
129 dllName[i] = (char) c;
132 dllName[len] = 0;
134 #ifdef DEBUG_very_verbose
135 printf_stderr("LdrLoadDll: dll name '%s'\n", dllName);
136 #endif
138 // then compare to everything on the blocklist
139 DllBlockInfo *info = &sWindowsDllBlocklist[0];
140 while (info->name) {
141 if (strcmp(info->name, dllName) == 0)
142 break;
144 info++;
147 if (info->name) {
148 bool load_ok = false;
150 #ifdef DEBUG_very_verbose
151 printf_stderr("LdrLoadDll: info->name: '%s'\n", info->name);
152 #endif
154 if (info->maxVersion != ALL_VERSIONS) {
155 // figure out the length of the string that we need
156 DWORD pathlen = SearchPathW(filePath, fname, L".dll", 0, NULL, NULL);
157 if (pathlen == 0) {
158 // uh, we couldn't find the DLL at all, so...
159 printf_stderr("LdrLoadDll: Blocking load of '%s' (SearchPathW didn't find it?)\n", dllName);
160 return STATUS_DLL_NOT_FOUND;
163 wchar_t *full_fname = (wchar_t*) malloc(sizeof(wchar_t)*(pathlen+1));
164 if (!full_fname) {
165 // couldn't allocate memory?
166 return STATUS_DLL_NOT_FOUND;
169 // now actually grab it
170 SearchPathW(filePath, fname, L".dll", pathlen+1, full_fname, NULL);
172 DWORD zero;
173 DWORD infoSize = GetFileVersionInfoSizeW(full_fname, &zero);
175 // If we failed to get the version information, we block.
177 if (infoSize != 0) {
178 nsAutoArrayPtr<unsigned char> infoData = new unsigned char[infoSize];
179 VS_FIXEDFILEINFO *vInfo;
180 UINT vInfoLen;
182 if (GetFileVersionInfoW(full_fname, 0, infoSize, infoData) &&
183 VerQueryValueW(infoData, L"\\", (LPVOID*) &vInfo, &vInfoLen))
185 unsigned long long fVersion =
186 ((unsigned long long)vInfo->dwFileVersionMS) << 32 |
187 ((unsigned long long)vInfo->dwFileVersionLS);
189 // finally do the version check, and if it's greater than our block
190 // version, keep loading
191 if (fVersion > info->maxVersion)
192 load_ok = true;
196 free(full_fname);
199 if (!load_ok) {
200 printf_stderr("LdrLoadDll: Blocking load of '%s' -- see http://www.mozilla.com/en-US/blocklist/\n", dllName);
201 return STATUS_DLL_NOT_FOUND;
205 continue_loading:
206 #ifdef DEBUG_very_verbose
207 printf_stderr("LdrLoadDll: continuing load... ('%S')\n", moduleFileName->Buffer);
208 #endif
210 NS_SetHasLoadedNewDLLs();
212 return stub_LdrLoadDll(filePath, flags, moduleFileName, handle);
215 WindowsDllInterceptor NtDllIntercept;
217 void
218 SetupDllBlocklist()
220 NtDllIntercept.Init("ntdll.dll");
222 bool ok = NtDllIntercept.AddHook("LdrLoadDll", patched_LdrLoadDll, (void**) &stub_LdrLoadDll);
224 #ifdef DEBUG
225 if (!ok)
226 printf_stderr ("LdrLoadDll hook failed, no dll blocklisting active\n");
227 #endif