1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef nsWindowsWMain_cpp
6 #define nsWindowsWMain_cpp
8 // This file is a .cpp file meant to be included in nsBrowserApp.cpp and other
9 // similar bootstrap code. It converts wide-character windows wmain into UTF-8
10 // narrow-character strings.
13 # error This file only makes sense on Windows.
16 #include "mozilla/Char16.h"
17 #include "nsUTF8Utils.h"
18 #include "nsWindowsHelpers.h"
21 #include <versionhelpers.h>
25 /* MingW currently does not implement a wide version of the
26 startup routines. Workaround is to implement something like
27 it ourselves. See bug 411826 */
29 # include <shellapi.h>
31 int wmain(int argc
, WCHAR
** argv
);
33 int main(int argc
, char** argv
) {
34 LPWSTR commandLine
= GetCommandLineW();
36 LPWSTR
* argvw
= CommandLineToArgvW(commandLine
, &argcw
);
37 if (!argvw
) return 127;
39 int result
= wmain(argcw
, argvw
);
43 #endif /* __MINGW32__ */
45 #define main NS_internal_main
47 #ifndef XRE_WANT_ENVIRON
48 int main(int argc
, char** argv
);
50 int main(int argc
, char** argv
, char** envp
);
53 // SanitizeEnvironmentVariables()
55 // Mitigate CVE-2007-6753 (binary planting via unexpanded environment variable
56 // references) by forcibly expanding all environment variable references in
59 // CVE-2007-6753 is documented to have affected all active mainline Windows
60 // versions at the time of its announcement (i.e., up to Windows 7). Microsoft
61 // has never formally stated that later versions of Windows are free of this
62 // issue; out of an abundance of caution, we continue to mitigate it on Windows
64 static void SanitizeEnvironmentVariables() {
65 DWORD bufferSize
= GetEnvironmentVariableW(L
"PATH", nullptr, 0);
67 wchar_t* originalPath
= new wchar_t[bufferSize
];
69 GetEnvironmentVariableW(L
"PATH", originalPath
, bufferSize
)) {
70 bufferSize
= ExpandEnvironmentStringsW(originalPath
, nullptr, 0);
71 // The maximum length of a Windows environment variable and the maximum
72 // length of a string returned by ExpandEnvironmentStringsW are both
73 // documented to be 32,767 characters. Under some versions of Windows,
74 // however, both may be longer, with delayed but deleterious consequences.
76 // We therefore cap the size manually, in case the user has a sufficiently
77 // problematic %PATH%. (See bug 1753910.) This is unlikely to occur unless
78 // there is some form of recursive referencing involved, in which case
79 // expansion is no mitigation anyway.
80 if (bufferSize
&& bufferSize
< 32768) {
81 wchar_t* newPath
= new wchar_t[bufferSize
];
82 if (ExpandEnvironmentStringsW(originalPath
, newPath
, bufferSize
)) {
83 SetEnvironmentVariableW(L
"PATH", newPath
);
88 delete[] originalPath
;
92 static char* AllocConvertUTF16toUTF8(char16ptr_t arg
) {
93 // be generous... UTF16 units can expand up to 3 UTF8 units
94 size_t len
= wcslen(arg
);
95 // ConvertUTF16toUTF8 requires +1. Let's do that here, too, lacking
96 // knowledge of Windows internals.
97 size_t dstLen
= len
* 3 + 1;
98 char* s
= new char[dstLen
+ 1]; // Another +1 for zero terminator
99 if (!s
) return nullptr;
102 ::WideCharToMultiByte(CP_UTF8
, 0, arg
, len
, s
, dstLen
, nullptr, nullptr);
107 static void FreeAllocStrings(int argc
, char** argv
) {
116 int wmain(int argc
, WCHAR
** argv
) {
117 // In Windows 7 32-bit, user32.dll must be mapped to the same virtual
118 // address in all processes. Otherwise, win32k's user-mode callback causes
119 // crash. Since we delayload user32.dll, if our code or injected code
120 // reserves the user32 address before user32.dll is loaded, it is loaded
121 // to a new address and we crash. To mitigate this problem, we explicitly
122 // load user32.dll as early as possible. See bug 1730033 for details.
123 if (!IsWindows8OrGreater()) {
125 ::GetNativeSystemInfo(&sysInfo
);
126 if (sysInfo
.wProcessorArchitecture
== PROCESSOR_ARCHITECTURE_INTEL
) {
127 LoadLibrarySystem32(L
"user32.dll");
131 SanitizeEnvironmentVariables();
132 SetDllDirectoryW(L
"");
134 // Only run this code if LauncherProcessWin.h was included beforehand, thus
135 // signalling that the hosting process should support launcher mode.
136 #if defined(mozilla_LauncherProcessWin_h)
137 mozilla::Maybe
<int> launcherResult
=
138 mozilla::LauncherMain(argc
, argv
, sAppData
);
139 if (launcherResult
) {
140 return launcherResult
.value();
142 #endif // defined(mozilla_LauncherProcessWin_h)
144 char** argvConverted
= new char*[argc
+ 1];
145 if (!argvConverted
) return 127;
147 for (int i
= 0; i
< argc
; ++i
) {
148 argvConverted
[i
] = AllocConvertUTF16toUTF8(argv
[i
]);
149 if (!argvConverted
[i
]) {
153 argvConverted
[argc
] = nullptr;
155 // need to save argvConverted copy for later deletion.
156 char** deleteUs
= new char*[argc
+ 1];
158 FreeAllocStrings(argc
, argvConverted
);
161 for (int i
= 0; i
< argc
; i
++) deleteUs
[i
] = argvConverted
[i
];
162 #ifndef XRE_WANT_ENVIRON
163 int result
= main(argc
, argvConverted
);
165 // Force creation of the multibyte _environ variable.
167 int result
= main(argc
, argvConverted
, _environ
);
170 delete[] argvConverted
;
171 FreeAllocStrings(argc
, deleteUs
);
176 #endif // nsWindowsWMain_cpp