1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/ipc/IOThreadChild.h"
#include "ContentProcess.h"
#include "base/shared_memory.h"
#include "mozilla/Preferences.h"
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
# include <stdlib.h>
# include "mozilla/Sandbox.h"
#endif
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
# include "mozilla/WindowsProcessMitigations.h"
#endif
#if (defined(XP_WIN) || defined(XP_MACOSX)) && defined(MOZ_SANDBOX)
# include "mozilla/SandboxSettings.h"
# include "nsAppDirectoryServiceDefs.h"
# include "nsDirectoryService.h"
# include "nsDirectoryServiceDefs.h"
#endif
#include "nsAppRunner.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/ProcessUtils.h"
#include "mozilla/GeckoArgs.h"
#include "nsAppStartupNotifier.h"
using mozilla::ipc::IOThreadChild;
namespace mozilla::dom {
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
static void SetTmpEnvironmentVariable(nsIFile* aValue) {
// Save the TMP environment variable so that is is picked up by GetTempPath().
// Note that we specifically write to the TMP variable, as that is the first
// variable that is checked by GetTempPath() to determine its output.
nsAutoString fullTmpPath;
nsresult rv = aValue->GetPath(fullTmpPath);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
Unused << NS_WARN_IF(!SetEnvironmentVariableW(L"TMP", fullTmpPath.get()));
// We also set TEMP in case there is naughty third-party code that is
// referencing the environment variable directly.
Unused << NS_WARN_IF(!SetEnvironmentVariableW(L"TEMP", fullTmpPath.get()));
}
#endif
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
static void SetUpSandboxEnvironment() {
MOZ_ASSERT(
nsDirectoryService::gService,
"SetUpSandboxEnvironment relies on nsDirectoryService being initialized");
// On Windows, a sandbox-writable temp directory is used whenever the sandbox
// is enabled, except when win32k is locked down when we no longer require a
// temp directory.
if (!IsContentSandboxEnabled() || IsWin32kLockedDown()) {
return;
}
nsCOMPtr<nsIFile> sandboxedContentTemp;
nsresult rv = nsDirectoryService::gService->Get(
NS_APP_CONTENT_PROCESS_TEMP_DIR, NS_GET_IID(nsIFile),
getter_AddRefs(sandboxedContentTemp));
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
// Change the gecko defined temp directory to our sandbox-writable one.
// Undefine returns a failure if the property is not already set.
Unused << nsDirectoryService::gService->Undefine(NS_OS_TEMP_DIR);
rv = nsDirectoryService::gService->Set(NS_OS_TEMP_DIR, sandboxedContentTemp);
if (NS_WARN_IF(NS_FAILED(rv))) {
return;
}
SetTmpEnvironmentVariable(sandboxedContentTemp);
}
#endif
static nsresult GetGREDir(nsIFile** aResult) {
nsCOMPtr<nsIFile> current;
nsresult rv = XRE_GetBinaryPath(getter_AddRefs(current));
NS_ENSURE_SUCCESS(rv, rv);
#ifdef OS_MACOSX
// Walk out of [subprocess].app/Contents/MacOS to the real GRE dir
const int depth = 4;
#else
const int depth = 1;
#endif
for (int i = 0; i < depth; ++i) {
nsCOMPtr<nsIFile> parent;
rv = current->GetParent(getter_AddRefs(parent));
NS_ENSURE_SUCCESS(rv, rv);
current = parent;
NS_ENSURE_TRUE(current, NS_ERROR_UNEXPECTED);
}
#ifdef OS_MACOSX
rv = current->SetNativeLeafName("Resources"_ns);
NS_ENSURE_SUCCESS(rv, rv);
#endif
current.forget(aResult);
return NS_OK;
}
ContentProcess::ContentProcess(ProcessId aParentPid,
const nsID& aMessageChannelId)
: ProcessChild(aParentPid, aMessageChannelId) {
NS_LogInit();
}
ContentProcess::~ContentProcess() { NS_LogTerm(); }
bool ContentProcess::Init(int aArgc, char* aArgv[]) {
Maybe<uint64_t> childID = geckoargs::sChildID.Get(aArgc, aArgv);
Maybe<bool> isForBrowser = Nothing();
Maybe<const char*> parentBuildID =
geckoargs::sParentBuildID.Get(aArgc, aArgv);
Maybe<uint64_t> jsInitHandle;
Maybe<uint64_t> jsInitLen = geckoargs::sJsInitLen.Get(aArgc, aArgv);
nsCOMPtr<nsIFile> appDirArg;
Maybe<const char*> appDir = geckoargs::sAppDir.Get(aArgc, aArgv);
if (appDir.isSome()) {
bool flag;
nsresult rv = XRE_GetFileFromPath(*appDir, getter_AddRefs(appDirArg));
if (NS_FAILED(rv) || NS_FAILED(appDirArg->Exists(&flag)) || !flag) {
NS_WARNING("Invalid application directory passed to content process.");
appDirArg = nullptr;
}
}
Maybe<bool> safeMode = geckoargs::sSafeMode.Get(aArgc, aArgv);
if (safeMode.isSome()) {
gSafeMode = *safeMode;
}
Maybe<bool> isForBrowerParam = geckoargs::sIsForBrowser.Get(aArgc, aArgv);
Maybe<bool> notForBrowserParam = geckoargs::sNotForBrowser.Get(aArgc, aArgv);
if (isForBrowerParam.isSome()) {
isForBrowser = Some(true);
}
if (notForBrowserParam.isSome()) {
isForBrowser = Some(false);
}
// command line: [-jsInitHandle handle] -jsInitLen length
#ifdef XP_WIN
jsInitHandle = geckoargs::sJsInitHandle.Get(aArgc, aArgv);
#endif
#if defined(XP_MACOSX) && defined(MOZ_SANDBOX)
nsCOMPtr<nsIFile> profileDir;
bool flag;
Maybe<const char*> profile = geckoargs::sProfile.Get(aArgc, aArgv);
// xpcshell self-test on macOS will hit this, so check isSome() otherwise
// Maybe<> assertions will MOZ_CRASH() us.
if (profile.isSome()) {
nsresult rv = XRE_GetFileFromPath(*profile, getter_AddRefs(profileDir));
if (NS_FAILED(rv) || NS_FAILED(profileDir->Exists(&flag)) || !flag) {
NS_WARNING("Invalid profile directory passed to content process.");
profileDir = nullptr;
}
} else {
NS_WARNING("No profile directory passed to content process.");
}
#endif /* XP_MACOSX && MOZ_SANDBOX */
// Did we find all the mandatory flags?
if (childID.isNothing() || isForBrowser.isNothing() ||
parentBuildID.isNothing()) {
return false;
}
if (!ProcessChild::InitPrefs(aArgc, aArgv)) {
return false;
}
if (!::mozilla::ipc::ImportSharedJSInit(jsInitHandle.valueOr(0),
jsInitLen.valueOr(0))) {
return false;
}
mContent.Init(TakeInitialEndpoint(), *parentBuildID, *childID, *isForBrowser);
nsCOMPtr<nsIFile> greDir;
nsresult rv = GetGREDir(getter_AddRefs(greDir));
if (NS_FAILED(rv)) {
return false;
}
nsCOMPtr<nsIFile> xpcomAppDir = appDirArg ? appDirArg : greDir;
rv = mDirProvider.Initialize(xpcomAppDir, greDir, nullptr);
if (NS_FAILED(rv)) {
return false;
}
rv = NS_InitXPCOM(nullptr, xpcomAppDir, &mDirProvider);
if (NS_FAILED(rv)) {
return false;
}
nsAppStartupNotifier::NotifyObservers(APPSTARTUP_CATEGORY);
#if (defined(XP_MACOSX)) && defined(MOZ_SANDBOX)
mContent.SetProfileDir(profileDir);
# if defined(DEBUG)
if (IsContentSandboxEnabled()) {
AssertMacSandboxEnabled();
}
# endif /* DEBUG */
#endif /* XP_MACOSX && MOZ_SANDBOX */
#if defined(XP_WIN) && defined(MOZ_SANDBOX)
SetUpSandboxEnvironment();
#endif
// Do this as early as possible to get the parent process to initialize the
// background thread since we'll likely need database information very soon.
mozilla::ipc::BackgroundChild::Startup();
mozilla::ipc::BackgroundChild::InitContentStarter(&mContent);
return true;
}
// Note: CleanUp() never gets called in non-debug builds because we exit early
// in ContentChild::ActorDestroy().
void ContentProcess::CleanUp() {
mDirProvider.DoShutdown();
NS_ShutdownXPCOM(nullptr);
}
} // namespace mozilla::dom
|