blob: d05be2ca787ba185cb2d73c7d75e23eeb293ff99 [file] [log] [blame]
Narayan Kamath973b4662014-03-31 13:41:26 +01001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Colin Cross18cd9f52014-06-13 12:58:55 -070017#define LOG_TAG "Zygote"
Narayan Kamath973b4662014-03-31 13:41:26 +010018
19// sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc
20#include <sys/mount.h>
21#include <linux/fs.h>
22
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -070023#include <list>
Andreas Gampeb053cce2015-11-17 16:38:59 -080024#include <sstream>
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -070025#include <string>
26
Colin Cross18cd9f52014-06-13 12:58:55 -070027#include <fcntl.h>
Dan Albert46d84442014-11-18 16:07:51 -080028#include <grp.h>
29#include <inttypes.h>
Christopher Ferrisab16dd12017-05-15 16:50:29 -070030#include <malloc.h>
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -070031#include <mntent.h>
Narayan Kamath973b4662014-03-31 13:41:26 +010032#include <paths.h>
33#include <signal.h>
34#include <stdlib.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070035#include <sys/capability.h>
Robert Seseke4f8d692016-09-13 19:13:01 -040036#include <sys/cdefs.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070037#include <sys/personality.h>
38#include <sys/prctl.h>
39#include <sys/resource.h>
40#include <sys/stat.h>
Vitalii Tomkiv5cbce852016-05-18 17:43:02 -070041#include <sys/time.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070042#include <sys/types.h>
43#include <sys/utsname.h>
44#include <sys/wait.h>
Dan Albert46d84442014-11-18 16:07:51 -080045#include <unistd.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070046
Andreas Gampe8dfa1782017-01-05 12:45:58 -080047#include "android-base/logging.h"
Carmen Jacksondd401252017-02-23 15:21:10 -080048#include <android-base/file.h>
49#include <android-base/stringprintf.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070050#include <cutils/fs.h>
51#include <cutils/multiuser.h>
52#include <cutils/sched_policy.h>
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -070053#include <private/android_filesystem_config.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070054#include <utils/String8.h>
55#include <selinux/android.h>
Victor Hsiehc8176ef2018-01-08 12:43:00 -080056#include <seccomp_policy.h>
Colin Cross0161bbc2014-06-03 13:26:58 -070057#include <processgroup/processgroup.h>
Colin Cross18cd9f52014-06-13 12:58:55 -070058
Andreas Gampeed6b9df2014-11-20 22:02:20 -080059#include "core_jni_helpers.h"
Steven Moreland2279b252017-07-19 09:50:45 -070060#include <nativehelper/JNIHelp.h>
61#include <nativehelper/ScopedLocalRef.h>
62#include <nativehelper/ScopedPrimitiveArray.h>
63#include <nativehelper/ScopedUtfChars.h>
Robert Sesek8225b7c2016-12-16 14:02:31 -050064#include "fd_utils.h"
Narayan Kamath973b4662014-03-31 13:41:26 +010065
jgu212eacd062014-09-10 06:55:07 -040066#include "nativebridge/native_bridge.h"
67
Narayan Kamath973b4662014-03-31 13:41:26 +010068namespace {
69
70using android::String8;
Carmen Jacksondd401252017-02-23 15:21:10 -080071using android::base::StringPrintf;
72using android::base::WriteStringToFile;
Narayan Kamath973b4662014-03-31 13:41:26 +010073
74static pid_t gSystemServerPid = 0;
75
76static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
77static jclass gZygoteClass;
78static jmethodID gCallPostForkChildHooks;
79
Victor Hsiehc8176ef2018-01-08 12:43:00 -080080static bool g_is_security_enforced = true;
81
Narayan Kamath973b4662014-03-31 13:41:26 +010082// Must match values in com.android.internal.os.Zygote.
83enum MountExternalKind {
84 MOUNT_EXTERNAL_NONE = 0,
Jeff Sharkey48877892015-03-18 11:27:19 -070085 MOUNT_EXTERNAL_DEFAULT = 1,
Jeff Sharkey9527b222015-06-24 15:24:48 -070086 MOUNT_EXTERNAL_READ = 2,
87 MOUNT_EXTERNAL_WRITE = 3,
Narayan Kamath973b4662014-03-31 13:41:26 +010088};
89
Andreas Gampeb053cce2015-11-17 16:38:59 -080090static void RuntimeAbort(JNIEnv* env, int line, const char* msg) {
91 std::ostringstream oss;
92 oss << __FILE__ << ":" << line << ": " << msg;
93 env->FatalError(oss.str().c_str());
Narayan Kamath973b4662014-03-31 13:41:26 +010094}
95
96// This signal handler is for zygote mode, since the zygote must reap its children
97static void SigChldHandler(int /*signal_number*/) {
98 pid_t pid;
99 int status;
100
Christopher Ferrisa8a79542015-08-31 15:40:01 -0700101 // It's necessary to save and restore the errno during this function.
102 // Since errno is stored per thread, changing it here modifies the errno
103 // on the thread on which this signal handler executes. If a signal occurs
104 // between a call and an errno check, it's possible to get the errno set
105 // here.
106 // See b/23572286 for extra information.
107 int saved_errno = errno;
108
Narayan Kamath973b4662014-03-31 13:41:26 +0100109 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
110 // Log process-death status that we care about. In general it is
111 // not safe to call LOG(...) from a signal handler because of
112 // possible reentrancy. However, we know a priori that the
113 // current implementation of LOG() is safe to call from a SIGCHLD
114 // handler in the zygote process. If the LOG() implementation
115 // changes its locking strategy or its use of syscalls within the
116 // lazy-init critical section, its use here may become unsafe.
117 if (WIFEXITED(status)) {
Josh Gao6d747ca2017-08-02 12:54:05 -0700118 ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
Narayan Kamath973b4662014-03-31 13:41:26 +0100119 } else if (WIFSIGNALED(status)) {
Josh Gao6d747ca2017-08-02 12:54:05 -0700120 ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
Narayan Kamath973b4662014-03-31 13:41:26 +0100121 if (WCOREDUMP(status)) {
122 ALOGI("Process %d dumped core.", pid);
123 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100124 }
125
126 // If the just-crashed process is the system_server, bring down zygote
127 // so that it is restarted by init and system server will be restarted
128 // from there.
129 if (pid == gSystemServerPid) {
Dan Albert46d84442014-11-18 16:07:51 -0800130 ALOGE("Exit zygote because system server (%d) has terminated", pid);
Narayan Kamath973b4662014-03-31 13:41:26 +0100131 kill(getpid(), SIGKILL);
132 }
133 }
134
Narayan Kamath160992d2014-04-14 14:46:07 +0100135 // Note that we shouldn't consider ECHILD an error because
136 // the secondary zygote might have no children left to wait for.
137 if (pid < 0 && errno != ECHILD) {
138 ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100139 }
Christopher Ferrisa8a79542015-08-31 15:40:01 -0700140
141 errno = saved_errno;
Narayan Kamath973b4662014-03-31 13:41:26 +0100142}
143
yuanhao435e84b2018-01-15 15:37:02 +0800144// Configures the SIGCHLD/SIGHUP handlers for the zygote process. This is
145// configured very late, because earlier in the runtime we may fork() and
146// exec() other processes, and we want to waitpid() for those rather than
Narayan Kamath973b4662014-03-31 13:41:26 +0100147// have them be harvested immediately.
148//
yuanhao435e84b2018-01-15 15:37:02 +0800149// Ignore SIGHUP because all processes forked by the zygote are in the same
150// process group as the zygote and we don't want to be notified if we become
151// an orphaned group and have one or more stopped processes. This is not a
152// theoretical concern :
153// - we can become an orphaned group if one of our direct descendants forks
154// and is subsequently killed before its children.
155// - crash_dump routinely STOPs the process it's tracing.
156//
157// See issues b/71965619 and b/25567761 for further details.
158//
Narayan Kamath973b4662014-03-31 13:41:26 +0100159// This ends up being called repeatedly before each fork(), but there's
160// no real harm in that.
yuanhao435e84b2018-01-15 15:37:02 +0800161static void SetSignalHandlers() {
162 struct sigaction sig_chld = {};
163 sig_chld.sa_handler = SigChldHandler;
Narayan Kamath973b4662014-03-31 13:41:26 +0100164
yuanhao435e84b2018-01-15 15:37:02 +0800165 if (sigaction(SIGCHLD, &sig_chld, NULL) < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700166 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100167 }
yuanhao435e84b2018-01-15 15:37:02 +0800168
169 struct sigaction sig_hup = {};
170 sig_hup.sa_handler = SIG_IGN;
171 if (sigaction(SIGHUP, &sig_hup, NULL) < 0) {
172 ALOGW("Error setting SIGHUP handler: %s", strerror(errno));
173 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100174}
175
176// Sets the SIGCHLD handler back to default behavior in zygote children.
yuanhao435e84b2018-01-15 15:37:02 +0800177static void UnsetChldSignalHandler() {
Narayan Kamath973b4662014-03-31 13:41:26 +0100178 struct sigaction sa;
179 memset(&sa, 0, sizeof(sa));
180 sa.sa_handler = SIG_DFL;
181
yuanhao435e84b2018-01-15 15:37:02 +0800182 if (sigaction(SIGCHLD, &sa, NULL) < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700183 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100184 }
185}
186
187// Calls POSIX setgroups() using the int[] object as an argument.
188// A NULL argument is tolerated.
189static void SetGids(JNIEnv* env, jintArray javaGids) {
190 if (javaGids == NULL) {
191 return;
192 }
193
194 ScopedIntArrayRO gids(env, javaGids);
195 if (gids.get() == NULL) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800196 RuntimeAbort(env, __LINE__, "Getting gids int array failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100197 }
198 int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
199 if (rc == -1) {
Narayan Kamath593aab72016-08-09 17:23:31 +0100200 std::ostringstream oss;
201 oss << "setgroups failed: " << strerror(errno) << ", gids.size=" << gids.size();
202 RuntimeAbort(env, __LINE__, oss.str().c_str());
Narayan Kamath973b4662014-03-31 13:41:26 +0100203 }
204}
205
206// Sets the resource limits via setrlimit(2) for the values in the
207// two-dimensional array of integers that's passed in. The second dimension
208// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
209// treated as an empty array.
210static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
211 if (javaRlimits == NULL) {
212 return;
213 }
214
215 rlimit rlim;
216 memset(&rlim, 0, sizeof(rlim));
217
218 for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
219 ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
220 ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
221 if (javaRlimit.size() != 3) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800222 RuntimeAbort(env, __LINE__, "rlimits array must have a second dimension of size 3");
Narayan Kamath973b4662014-03-31 13:41:26 +0100223 }
224
225 rlim.rlim_cur = javaRlimit[1];
226 rlim.rlim_max = javaRlimit[2];
227
228 int rc = setrlimit(javaRlimit[0], &rlim);
229 if (rc == -1) {
Dan Albert46d84442014-11-18 16:07:51 -0800230 ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
231 rlim.rlim_max);
Andreas Gampeb053cce2015-11-17 16:38:59 -0800232 RuntimeAbort(env, __LINE__, "setrlimit failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100233 }
234 }
235}
236
Narayan Kamath973b4662014-03-31 13:41:26 +0100237// The debug malloc library needs to know whether it's the zygote or a child.
238extern "C" int gMallocLeakZygoteChild;
239
Christopher Ferris76de39e2017-06-20 16:13:40 -0700240static void PreApplicationInit() {
241 // The child process sets this to indicate it's not the zygote.
242 gMallocLeakZygoteChild = 1;
243
244 // Set the jemalloc decay time to 1.
245 mallopt(M_DECAY_TIME, 1);
246}
247
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800248static void SetUpSeccompFilter(uid_t uid) {
249 if (!g_is_security_enforced) {
250 ALOGI("seccomp disabled by setenforce 0");
251 return;
252 }
253
254 // Apply system or app filter based on uid.
255 if (getuid() >= AID_APP_START) {
256 set_app_seccomp_filter();
257 } else {
258 set_system_seccomp_filter();
259 }
260}
261
Narayan Kamath973b4662014-03-31 13:41:26 +0100262static void EnableKeepCapabilities(JNIEnv* env) {
263 int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
264 if (rc == -1) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800265 RuntimeAbort(env, __LINE__, "prctl(PR_SET_KEEPCAPS) failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100266 }
267}
268
269static void DropCapabilitiesBoundingSet(JNIEnv* env) {
270 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
271 int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
272 if (rc == -1) {
273 if (errno == EINVAL) {
274 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
275 "your kernel is compiled with file capabilities support");
276 } else {
Josh Gao45dab782017-02-01 14:56:09 -0800277 ALOGE("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800278 RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100279 }
280 }
281 }
282}
283
Josh Gao45dab782017-02-01 14:56:09 -0800284static void SetInheritable(JNIEnv* env, uint64_t inheritable) {
285 __user_cap_header_struct capheader;
286 memset(&capheader, 0, sizeof(capheader));
287 capheader.version = _LINUX_CAPABILITY_VERSION_3;
288 capheader.pid = 0;
289
290 __user_cap_data_struct capdata[2];
291 if (capget(&capheader, &capdata[0]) == -1) {
292 ALOGE("capget failed: %s", strerror(errno));
293 RuntimeAbort(env, __LINE__, "capget failed");
294 }
295
296 capdata[0].inheritable = inheritable;
297 capdata[1].inheritable = inheritable >> 32;
298
299 if (capset(&capheader, &capdata[0]) == -1) {
300 ALOGE("capset(inh=%" PRIx64 ") failed: %s", inheritable, strerror(errno));
301 RuntimeAbort(env, __LINE__, "capset failed");
302 }
303}
304
305static void SetCapabilities(JNIEnv* env, uint64_t permitted, uint64_t effective,
306 uint64_t inheritable) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100307 __user_cap_header_struct capheader;
308 memset(&capheader, 0, sizeof(capheader));
309 capheader.version = _LINUX_CAPABILITY_VERSION_3;
310 capheader.pid = 0;
311
312 __user_cap_data_struct capdata[2];
313 memset(&capdata, 0, sizeof(capdata));
314 capdata[0].effective = effective;
315 capdata[1].effective = effective >> 32;
316 capdata[0].permitted = permitted;
317 capdata[1].permitted = permitted >> 32;
Josh Gao45dab782017-02-01 14:56:09 -0800318 capdata[0].inheritable = inheritable;
319 capdata[1].inheritable = inheritable >> 32;
Narayan Kamath973b4662014-03-31 13:41:26 +0100320
321 if (capset(&capheader, &capdata[0]) == -1) {
Josh Gao45dab782017-02-01 14:56:09 -0800322 ALOGE("capset(perm=%" PRIx64 ", eff=%" PRIx64 ", inh=%" PRIx64 ") failed: %s", permitted,
323 effective, inheritable, strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800324 RuntimeAbort(env, __LINE__, "capset failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100325 }
326}
327
328static void SetSchedulerPolicy(JNIEnv* env) {
329 errno = -set_sched_policy(0, SP_DEFAULT);
330 if (errno != 0) {
331 ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
Andreas Gampeb053cce2015-11-17 16:38:59 -0800332 RuntimeAbort(env, __LINE__, "set_sched_policy(0, SP_DEFAULT) failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100333 }
334}
335
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -0700336static int UnmountTree(const char* path) {
337 size_t path_len = strlen(path);
338
339 FILE* fp = setmntent("/proc/mounts", "r");
340 if (fp == NULL) {
341 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
342 return -errno;
343 }
344
345 // Some volumes can be stacked on each other, so force unmount in
346 // reverse order to give us the best chance of success.
347 std::list<std::string> toUnmount;
348 mntent* mentry;
349 while ((mentry = getmntent(fp)) != NULL) {
350 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
351 toUnmount.push_front(std::string(mentry->mnt_dir));
352 }
353 }
354 endmntent(fp);
355
356 for (auto path : toUnmount) {
357 if (umount2(path.c_str(), MNT_DETACH)) {
358 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
359 }
360 }
361 return 0;
362}
363
Narayan Kamath973b4662014-03-31 13:41:26 +0100364// Create a private mount namespace and bind mount appropriate emulated
365// storage for the given user.
Jeff Sharkey9527b222015-06-24 15:24:48 -0700366static bool MountEmulatedStorage(uid_t uid, jint mount_mode,
367 bool force_mount_namespace) {
368 // See storage config details at http://source.android.com/tech/storage/
369
Jeff Sharkey9527b222015-06-24 15:24:48 -0700370 String8 storageSource;
371 if (mount_mode == MOUNT_EXTERNAL_DEFAULT) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700372 storageSource = "/mnt/runtime/default";
Jeff Sharkey9527b222015-06-24 15:24:48 -0700373 } else if (mount_mode == MOUNT_EXTERNAL_READ) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700374 storageSource = "/mnt/runtime/read";
Jeff Sharkey9527b222015-06-24 15:24:48 -0700375 } else if (mount_mode == MOUNT_EXTERNAL_WRITE) {
Jeff Sharkey928e1ec2015-08-06 11:40:21 -0700376 storageSource = "/mnt/runtime/write";
Robert Sesek06af1c02016-11-10 21:50:04 -0500377 } else if (!force_mount_namespace) {
Jeff Sharkey9527b222015-06-24 15:24:48 -0700378 // Sane default of no storage visible
379 return true;
380 }
Robert Sesek8a3a6ff2016-10-31 11:25:10 -0400381
382 // Create a second private mount namespace for our process
383 if (unshare(CLONE_NEWNS) == -1) {
384 ALOGW("Failed to unshare(): %s", strerror(errno));
385 return false;
386 }
387
Robert Sesek06f39302017-03-20 17:30:05 -0400388 // Handle force_mount_namespace with MOUNT_EXTERNAL_NONE.
389 if (mount_mode == MOUNT_EXTERNAL_NONE) {
390 return true;
391 }
392
Jeff Sharkey9527b222015-06-24 15:24:48 -0700393 if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage",
394 NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) {
395 ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno));
396 return false;
397 }
398
Jeff Sharkeyfaf3f692015-06-30 15:56:33 -0700399 // Mount user-specific symlink helper into place
Jeff Sharkey9527b222015-06-24 15:24:48 -0700400 userid_t user_id = multiuser_get_user_id(uid);
401 const String8 userSource(String8::format("/mnt/user/%d", user_id));
402 if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) {
403 return false;
404 }
405 if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self",
406 NULL, MS_BIND, NULL)) == -1) {
407 ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno));
408 return false;
409 }
410
Narayan Kamath973b4662014-03-31 13:41:26 +0100411 return true;
Narayan Kamath973b4662014-03-31 13:41:26 +0100412}
413
Narayan Kamath973b4662014-03-31 13:41:26 +0100414static bool NeedsNoRandomizeWorkaround() {
415#if !defined(__arm__)
416 return false;
417#else
418 int major;
419 int minor;
420 struct utsname uts;
421 if (uname(&uts) == -1) {
422 return false;
423 }
424
425 if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
426 return false;
427 }
428
429 // Kernels before 3.4.* need the workaround.
430 return (major < 3) || ((major == 3) && (minor < 4));
431#endif
432}
Narayan Kamath973b4662014-03-31 13:41:26 +0100433
434// Utility to close down the Zygote socket file descriptors while
435// the child is still running as root with Zygote's privileges. Each
436// descriptor (if any) is closed via dup2(), replacing it with a valid
437// (open) descriptor to /dev/null.
438
439static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
440 if (!fdsToClose) {
441 return;
442 }
443 jsize count = env->GetArrayLength(fdsToClose);
Mykola Kondratenko1ca062f2015-07-31 17:22:26 +0200444 ScopedIntArrayRO ar(env, fdsToClose);
445 if (ar.get() == NULL) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800446 RuntimeAbort(env, __LINE__, "Bad fd array");
Narayan Kamath973b4662014-03-31 13:41:26 +0100447 }
448 jsize i;
449 int devnull;
450 for (i = 0; i < count; i++) {
451 devnull = open("/dev/null", O_RDWR);
452 if (devnull < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700453 ALOGE("Failed to open /dev/null: %s", strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800454 RuntimeAbort(env, __LINE__, "Failed to open /dev/null");
Narayan Kamath973b4662014-03-31 13:41:26 +0100455 continue;
456 }
Elliott Hughes960e8312014-09-30 08:49:01 -0700457 ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100458 if (dup2(devnull, ar[i]) < 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700459 ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800460 RuntimeAbort(env, __LINE__, "Failed dup2()");
Narayan Kamath973b4662014-03-31 13:41:26 +0100461 }
462 close(devnull);
463 }
464}
465
466void SetThreadName(const char* thread_name) {
467 bool hasAt = false;
468 bool hasDot = false;
469 const char* s = thread_name;
470 while (*s) {
471 if (*s == '.') {
472 hasDot = true;
473 } else if (*s == '@') {
474 hasAt = true;
475 }
476 s++;
477 }
478 const int len = s - thread_name;
479 if (len < 15 || hasAt || !hasDot) {
480 s = thread_name;
481 } else {
482 s = thread_name + len - 15;
483 }
484 // pthread_setname_np fails rather than truncating long strings.
485 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
486 strlcpy(buf, s, sizeof(buf)-1);
487 errno = pthread_setname_np(pthread_self(), buf);
488 if (errno != 0) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700489 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100490 }
Andreas Gampe041483a2018-03-05 13:00:42 -0800491 // Update base::logging default tag.
492 android::base::SetDefaultTag(buf);
Narayan Kamath973b4662014-03-31 13:41:26 +0100493}
494
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100495// The list of open zygote file descriptors.
496static FileDescriptorTable* gOpenFdTable = NULL;
497
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800498static void FillFileDescriptorVector(JNIEnv* env,
499 jintArray java_fds,
500 std::vector<int>* fds) {
501 CHECK(fds != nullptr);
502 if (java_fds != nullptr) {
503 ScopedIntArrayRO ar(env, java_fds);
504 if (ar.get() == nullptr) {
505 RuntimeAbort(env, __LINE__, "Bad fd array");
506 }
507 fds->reserve(ar.size());
508 for (size_t i = 0; i < ar.size(); ++i) {
509 fds->push_back(ar[i]);
510 }
511 }
512}
513
Narayan Kamath973b4662014-03-31 13:41:26 +0100514// Utility routine to fork zygote and specialize the child process.
515static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100516 jint runtime_flags, jobjectArray javaRlimits,
Narayan Kamath973b4662014-03-31 13:41:26 +0100517 jlong permittedCapabilities, jlong effectiveCapabilities,
518 jint mount_external,
519 jstring java_se_info, jstring java_se_name,
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700520 bool is_system_server, jintArray fdsToClose,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500521 jintArray fdsToIgnore, bool is_child_zygote,
jgu212eacd062014-09-10 06:55:07 -0400522 jstring instructionSet, jstring dataDir) {
yuanhao435e84b2018-01-15 15:37:02 +0800523 SetSignalHandlers();
Narayan Kamath973b4662014-03-31 13:41:26 +0100524
Narayan Kamathdfcc79e2016-11-07 16:22:48 +0000525 sigset_t sigchld;
526 sigemptyset(&sigchld);
527 sigaddset(&sigchld, SIGCHLD);
528
529 // Temporarily block SIGCHLD during forks. The SIGCHLD handler might
530 // log, which would result in the logging FDs we close being reopened.
531 // This would cause failures because the FDs are not whitelisted.
532 //
533 // Note that the zygote process is single threaded at this point.
534 if (sigprocmask(SIG_BLOCK, &sigchld, nullptr) == -1) {
535 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
536 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_BLOCK, { SIGCHLD }) failed.");
537 }
538
Narayan Kamath3764a262016-08-30 15:36:19 +0100539 // Close any logging related FDs before we start evaluating the list of
540 // file descriptors.
541 __android_log_close();
542
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100543 // If this is the first fork for this zygote, create the open FD table.
544 // If it isn't, we just need to check whether the list of open files has
545 // changed (and it shouldn't in the normal case).
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800546 std::vector<int> fds_to_ignore;
547 FillFileDescriptorVector(env, fdsToIgnore, &fds_to_ignore);
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100548 if (gOpenFdTable == NULL) {
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800549 gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore);
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100550 if (gOpenFdTable == NULL) {
551 RuntimeAbort(env, __LINE__, "Unable to construct file descriptor table.");
552 }
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800553 } else if (!gOpenFdTable->Restat(fds_to_ignore)) {
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100554 RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table.");
555 }
556
Narayan Kamath973b4662014-03-31 13:41:26 +0100557 pid_t pid = fork();
558
559 if (pid == 0) {
Christopher Ferris76de39e2017-06-20 16:13:40 -0700560 PreApplicationInit();
Christopher Ferrisab16dd12017-05-15 16:50:29 -0700561
Narayan Kamath973b4662014-03-31 13:41:26 +0100562 // Clean up any descriptors which must be closed immediately
563 DetachDescriptors(env, fdsToClose);
564
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100565 // Re-open all remaining open file descriptors so that they aren't shared
566 // with the zygote across a fork.
Narayan Kamath3764a262016-08-30 15:36:19 +0100567 if (!gOpenFdTable->ReopenOrDetach()) {
Narayan Kamathc5f27a72016-08-19 13:45:24 +0100568 RuntimeAbort(env, __LINE__, "Unable to reopen whitelisted descriptors.");
569 }
570
Narayan Kamathdfcc79e2016-11-07 16:22:48 +0000571 if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
572 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
573 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
574 }
575
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800576 // Must be called when the new process still has CAP_SYS_ADMIN. The other alternative is to
577 // call prctl(PR_SET_NO_NEW_PRIVS, 1) afterward, but that breaks SELinux domain transition (see
578 // b/71859146).
579 SetUpSeccompFilter(uid);
580
Narayan Kamath973b4662014-03-31 13:41:26 +0100581 // Keep capabilities across UID change, unless we're staying root.
582 if (uid != 0) {
583 EnableKeepCapabilities(env);
584 }
585
Josh Gao45dab782017-02-01 14:56:09 -0800586 SetInheritable(env, permittedCapabilities);
Narayan Kamath973b4662014-03-31 13:41:26 +0100587 DropCapabilitiesBoundingSet(env);
588
Calin Juravle79ec4c12014-10-24 16:16:49 +0100589 bool use_native_bridge = !is_system_server && (instructionSet != NULL)
590 && android::NativeBridgeAvailable();
591 if (use_native_bridge) {
jgu212eacd062014-09-10 06:55:07 -0400592 ScopedUtfChars isa_string(env, instructionSet);
Calin Juravle79ec4c12014-10-24 16:16:49 +0100593 use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400594 }
Calin Juravle6a4d2362014-10-28 12:16:21 +0000595 if (use_native_bridge && dataDir == NULL) {
596 // dataDir should never be null if we need to use a native bridge.
597 // In general, dataDir will never be null for normal applications. It can only happen in
598 // special cases (for isolated processes which are not associated with any app). These are
599 // launched by the framework and should not be emulated anyway.
600 use_native_bridge = false;
601 ALOGW("Native bridge will not be used because dataDir == NULL.");
602 }
jgu212eacd062014-09-10 06:55:07 -0400603
Calin Juravle79ec4c12014-10-24 16:16:49 +0100604 if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700605 ALOGW("Failed to mount emulated storage: %s", strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100606 if (errno == ENOTCONN || errno == EROFS) {
607 // When device is actively encrypting, we get ENOTCONN here
608 // since FUSE was mounted before the framework restarted.
609 // When encrypted device is booting, we get EROFS since
610 // FUSE hasn't been created yet by init.
611 // In either case, continue without external storage.
612 } else {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800613 RuntimeAbort(env, __LINE__, "Cannot continue without emulated storage");
Narayan Kamath973b4662014-03-31 13:41:26 +0100614 }
615 }
616
Colin Cross0161bbc2014-06-03 13:26:58 -0700617 if (!is_system_server) {
618 int rc = createProcessGroup(uid, getpid());
619 if (rc != 0) {
Colin Cross3089bed2014-07-14 15:07:04 -0700620 if (rc == -EROFS) {
621 ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
622 } else {
623 ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
624 }
Colin Cross0161bbc2014-06-03 13:26:58 -0700625 }
626 }
627
Narayan Kamath973b4662014-03-31 13:41:26 +0100628 SetGids(env, javaGids);
629
630 SetRLimits(env, javaRlimits);
631
Calin Juravle79ec4c12014-10-24 16:16:49 +0100632 if (use_native_bridge) {
633 ScopedUtfChars isa_string(env, instructionSet);
634 ScopedUtfChars data_dir(env, dataDir);
635 android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
jgu212eacd062014-09-10 06:55:07 -0400636 }
637
Narayan Kamath973b4662014-03-31 13:41:26 +0100638 int rc = setresgid(gid, gid, gid);
639 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700640 ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800641 RuntimeAbort(env, __LINE__, "setresgid failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100642 }
643
644 rc = setresuid(uid, uid, uid);
645 if (rc == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700646 ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
Andreas Gampeb053cce2015-11-17 16:38:59 -0800647 RuntimeAbort(env, __LINE__, "setresuid failed");
Narayan Kamath973b4662014-03-31 13:41:26 +0100648 }
649
Narayan Kamath973b4662014-03-31 13:41:26 +0100650 if (NeedsNoRandomizeWorkaround()) {
651 // Work around ARM kernel ASLR lossage (http://b/5817320).
652 int old_personality = personality(0xffffffff);
653 int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
654 if (new_personality == -1) {
Elliott Hughes960e8312014-09-30 08:49:01 -0700655 ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
Narayan Kamath973b4662014-03-31 13:41:26 +0100656 }
657 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100658
Josh Gao45dab782017-02-01 14:56:09 -0800659 SetCapabilities(env, permittedCapabilities, effectiveCapabilities, permittedCapabilities);
Narayan Kamath973b4662014-03-31 13:41:26 +0100660
661 SetSchedulerPolicy(env);
662
Colin Cross18cd9f52014-06-13 12:58:55 -0700663 const char* se_info_c_str = NULL;
664 ScopedUtfChars* se_info = NULL;
665 if (java_se_info != NULL) {
666 se_info = new ScopedUtfChars(env, java_se_info);
667 se_info_c_str = se_info->c_str();
668 if (se_info_c_str == NULL) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800669 RuntimeAbort(env, __LINE__, "se_info_c_str == NULL");
Colin Cross18cd9f52014-06-13 12:58:55 -0700670 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100671 }
Colin Cross18cd9f52014-06-13 12:58:55 -0700672 const char* se_name_c_str = NULL;
673 ScopedUtfChars* se_name = NULL;
674 if (java_se_name != NULL) {
675 se_name = new ScopedUtfChars(env, java_se_name);
676 se_name_c_str = se_name->c_str();
677 if (se_name_c_str == NULL) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800678 RuntimeAbort(env, __LINE__, "se_name_c_str == NULL");
Colin Cross18cd9f52014-06-13 12:58:55 -0700679 }
680 }
681 rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
682 if (rc == -1) {
683 ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
684 is_system_server, se_info_c_str, se_name_c_str);
Andreas Gampeb053cce2015-11-17 16:38:59 -0800685 RuntimeAbort(env, __LINE__, "selinux_android_setcontext failed");
Colin Cross18cd9f52014-06-13 12:58:55 -0700686 }
687
688 // Make it easier to debug audit logs by setting the main thread's name to the
689 // nice name rather than "app_process".
Andreas Gampe041483a2018-03-05 13:00:42 -0800690 if (se_name_c_str == NULL && is_system_server) {
Colin Cross18cd9f52014-06-13 12:58:55 -0700691 se_name_c_str = "system_server";
692 }
Andreas Gampe041483a2018-03-05 13:00:42 -0800693 if (se_name_c_str != NULL) {
Colin Cross18cd9f52014-06-13 12:58:55 -0700694 SetThreadName(se_name_c_str);
695 }
696
697 delete se_info;
698 delete se_name;
Narayan Kamath973b4662014-03-31 13:41:26 +0100699
yuanhao435e84b2018-01-15 15:37:02 +0800700 // Unset the SIGCHLD handler, but keep ignoring SIGHUP (rationale in SetSignalHandlers).
701 UnsetChldSignalHandler();
Narayan Kamath973b4662014-03-31 13:41:26 +0100702
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100703 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500704 is_system_server, is_child_zygote, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100705 if (env->ExceptionCheck()) {
Andreas Gampeb053cce2015-11-17 16:38:59 -0800706 RuntimeAbort(env, __LINE__, "Error calling post fork hooks.");
Narayan Kamath973b4662014-03-31 13:41:26 +0100707 }
708 } else if (pid > 0) {
709 // the parent process
Tim Murray6d43a8612015-08-05 14:31:05 -0700710
Narayan Kamathdfcc79e2016-11-07 16:22:48 +0000711 // We blocked SIGCHLD prior to a fork, we unblock it here.
712 if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
713 ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
714 RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
715 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100716 }
717 return pid;
718}
Luis Hector Chavez72042c92017-07-12 10:03:30 -0700719
720static uint64_t GetEffectiveCapabilityMask(JNIEnv* env) {
721 __user_cap_header_struct capheader;
722 memset(&capheader, 0, sizeof(capheader));
723 capheader.version = _LINUX_CAPABILITY_VERSION_3;
724 capheader.pid = 0;
725
726 __user_cap_data_struct capdata[2];
727 if (capget(&capheader, &capdata[0]) == -1) {
728 ALOGE("capget failed: %s", strerror(errno));
729 RuntimeAbort(env, __LINE__, "capget failed");
730 }
731
732 return capdata[0].effective |
733 (static_cast<uint64_t>(capdata[1].effective) << 32);
734}
Narayan Kamath973b4662014-03-31 13:41:26 +0100735} // anonymous namespace
736
737namespace android {
738
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800739static void com_android_internal_os_Zygote_nativeSecurityInit(JNIEnv*, jclass) {
740 // security_getenforce is not allowed on app process. Initialize and cache the value before
741 // zygote forks.
742 g_is_security_enforced = security_getenforce();
743}
744
Christopher Ferris76de39e2017-06-20 16:13:40 -0700745static void com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv*, jclass) {
746 PreApplicationInit();
747}
748
Narayan Kamath973b4662014-03-31 13:41:26 +0100749static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
750 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100751 jint runtime_flags, jobjectArray rlimits,
Narayan Kamath973b4662014-03-31 13:41:26 +0100752 jint mount_external, jstring se_info, jstring se_name,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500753 jintArray fdsToClose, jintArray fdsToIgnore, jboolean is_child_zygote,
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800754 jstring instructionSet, jstring appDataDir) {
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700755 jlong capabilities = 0;
Pavlin Radoslavovfbd59042015-11-23 17:13:25 -0800756
757 // Grant CAP_WAKE_ALARM to the Bluetooth process.
Erik Kline0f6ae2e2016-02-22 15:42:07 +0900758 // Additionally, allow bluetooth to open packet sockets so it can start the DHCP client.
Philip Cuadra0fabba32017-04-26 13:49:37 -0700759 // Grant CAP_SYS_NICE to allow Bluetooth to set RT priority for
760 // audio-related threads.
Erik Kline0f6ae2e2016-02-22 15:42:07 +0900761 // TODO: consider making such functionality an RPC to netd.
Pavlin Radoslavov2956bee2016-01-27 16:22:15 -0800762 if (multiuser_get_app_id(uid) == AID_BLUETOOTH) {
Pavlin Radoslavovfbd59042015-11-23 17:13:25 -0800763 capabilities |= (1LL << CAP_WAKE_ALARM);
Erik Kline0f6ae2e2016-02-22 15:42:07 +0900764 capabilities |= (1LL << CAP_NET_RAW);
765 capabilities |= (1LL << CAP_NET_BIND_SERVICE);
Philip Cuadra0fabba32017-04-26 13:49:37 -0700766 capabilities |= (1LL << CAP_SYS_NICE);
Pavlin Radoslavovfbd59042015-11-23 17:13:25 -0800767 }
Sharvil Nanavatibabe8152015-08-31 23:25:06 -0700768
Pavlin Radoslavovfbd59042015-11-23 17:13:25 -0800769 // Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock"
770 bool gid_wakelock_found = false;
771 if (gid == AID_WAKELOCK) {
772 gid_wakelock_found = true;
773 } else if (gids != NULL) {
774 jsize gids_num = env->GetArrayLength(gids);
775 ScopedIntArrayRO ar(env, gids);
776 if (ar.get() == NULL) {
777 RuntimeAbort(env, __LINE__, "Bad gids array");
778 }
779 for (int i = 0; i < gids_num; i++) {
780 if (ar[i] == AID_WAKELOCK) {
781 gid_wakelock_found = true;
782 break;
Sharvil Nanavatibabe8152015-08-31 23:25:06 -0700783 }
Pavlin Radoslavovfbd59042015-11-23 17:13:25 -0800784 }
785 }
786 if (gid_wakelock_found) {
787 capabilities |= (1LL << CAP_BLOCK_SUSPEND);
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700788 }
789
Robert Sesekd0a190df2018-02-12 18:46:01 -0500790 // If forking a child zygote process, that zygote will need to be able to change
791 // the UID and GID of processes it forks, as well as drop those capabilities.
792 if (is_child_zygote) {
793 capabilities |= (1LL << CAP_SETUID);
794 capabilities |= (1LL << CAP_SETGID);
795 capabilities |= (1LL << CAP_SETPCAP);
796 }
797
Luis Hector Chavez72042c92017-07-12 10:03:30 -0700798 // Containers run without some capabilities, so drop any caps that are not
799 // available.
800 capabilities &= GetEffectiveCapabilityMask(env);
801
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100802 return ForkAndSpecializeCommon(env, uid, gid, gids, runtime_flags,
Sharvil Nanavati4990e4f2014-06-29 17:06:52 -0700803 rlimits, capabilities, capabilities, mount_external, se_info,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500804 se_name, false, fdsToClose, fdsToIgnore, is_child_zygote == JNI_TRUE,
805 instructionSet, appDataDir);
Narayan Kamath973b4662014-03-31 13:41:26 +0100806}
807
808static jint com_android_internal_os_Zygote_nativeForkSystemServer(
809 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100810 jint runtime_flags, jobjectArray rlimits, jlong permittedCapabilities,
Narayan Kamath973b4662014-03-31 13:41:26 +0100811 jlong effectiveCapabilities) {
812 pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100813 runtime_flags, rlimits,
Narayan Kamath973b4662014-03-31 13:41:26 +0100814 permittedCapabilities, effectiveCapabilities,
Jeff Sharkey9527b222015-06-24 15:24:48 -0700815 MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500816 NULL, false, NULL, NULL);
Narayan Kamath973b4662014-03-31 13:41:26 +0100817 if (pid > 0) {
818 // The zygote process checks whether the child process has died or not.
819 ALOGI("System server process %d has been created", pid);
820 gSystemServerPid = pid;
821 // There is a slight window that the system server process has crashed
822 // but it went unnoticed because we haven't published its pid yet. So
823 // we recheck here just to make sure that all is well.
824 int status;
825 if (waitpid(pid, &status, WNOHANG) == pid) {
826 ALOGE("System server process %d has died. Restarting Zygote!", pid);
Andreas Gampeb053cce2015-11-17 16:38:59 -0800827 RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
Narayan Kamath973b4662014-03-31 13:41:26 +0100828 }
Carmen Jacksondd401252017-02-23 15:21:10 -0800829
830 // Assign system_server to the correct memory cgroup.
Jeff Vander Stoep6bdc3a22017-11-22 23:09:23 -0800831 // Not all devices mount /dev/memcg so check for the file first
832 // to avoid unnecessarily printing errors and denials in the logs.
833 if (!access("/dev/memcg/system/tasks", F_OK) &&
834 !WriteStringToFile(StringPrintf("%d", pid), "/dev/memcg/system/tasks")) {
Carmen Jacksondd401252017-02-23 15:21:10 -0800835 ALOGE("couldn't write %d to /dev/memcg/system/tasks", pid);
836 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100837 }
838 return pid;
839}
840
Robert Sesek54e387d2016-12-02 17:27:50 -0500841static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork(
842 JNIEnv* env, jclass, jstring path) {
843 ScopedUtfChars path_native(env, path);
844 const char* path_cstr = path_native.c_str();
845 if (!path_cstr) {
846 RuntimeAbort(env, __LINE__, "path_cstr == NULL");
847 }
848 FileDescriptorWhitelist::Get()->Allow(path_cstr);
849}
850
doheon1.lee885b7422016-01-20 13:07:27 +0900851static void com_android_internal_os_Zygote_nativeUnmountStorageOnInit(JNIEnv* env, jclass) {
852 // Zygote process unmount root storage space initially before every child processes are forked.
853 // Every forked child processes (include SystemServer) only mount their own root storage space
Robert Seseke4f8d692016-09-13 19:13:01 -0400854 // and no need unmount storage operation in MountEmulatedStorage method.
855 // Zygote process does not utilize root storage spaces and unshares its mount namespace below.
856
857 // See storage config details at http://source.android.com/tech/storage/
858 // Create private mount namespace shared by all children
859 if (unshare(CLONE_NEWNS) == -1) {
860 RuntimeAbort(env, __LINE__, "Failed to unshare()");
861 return;
862 }
863
864 // Mark rootfs as being a slave so that changes from default
865 // namespace only flow into our children.
866 if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
867 RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE");
868 return;
869 }
870
871 // Create a staging tmpfs that is shared by our children; they will
872 // bind mount storage into their respective private namespaces, which
873 // are isolated from each other.
874 const char* target_base = getenv("EMULATED_STORAGE_TARGET");
875 if (target_base != nullptr) {
876#define STRINGIFY_UID(x) __STRING(x)
877 if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
878 "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) {
879 ALOGE("Failed to mount tmpfs to %s", target_base);
880 RuntimeAbort(env, __LINE__, "Failed to mount tmpfs");
881 return;
882 }
883#undef STRINGIFY_UID
884 }
doheon1.lee885b7422016-01-20 13:07:27 +0900885
886 UnmountTree("/storage");
doheon1.lee885b7422016-01-20 13:07:27 +0900887}
888
Daniel Micay76f6a862015-09-19 17:31:01 -0400889static const JNINativeMethod gMethods[] = {
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800890 { "nativeSecurityInit", "()V",
891 (void *) com_android_internal_os_Zygote_nativeSecurityInit },
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700892 { "nativeForkAndSpecialize",
Robert Sesekd0a190df2018-02-12 18:46:01 -0500893 "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)I",
Narayan Kamath973b4662014-03-31 13:41:26 +0100894 (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
895 { "nativeForkSystemServer", "(II[II[[IJJ)I",
doheon1.lee885b7422016-01-20 13:07:27 +0900896 (void *) com_android_internal_os_Zygote_nativeForkSystemServer },
Robert Sesek54e387d2016-12-02 17:27:50 -0500897 { "nativeAllowFileAcrossFork", "(Ljava/lang/String;)V",
898 (void *) com_android_internal_os_Zygote_nativeAllowFileAcrossFork },
doheon1.lee885b7422016-01-20 13:07:27 +0900899 { "nativeUnmountStorageOnInit", "()V",
Christopher Ferris76de39e2017-06-20 16:13:40 -0700900 (void *) com_android_internal_os_Zygote_nativeUnmountStorageOnInit },
901 { "nativePreApplicationInit", "()V",
902 (void *) com_android_internal_os_Zygote_nativePreApplicationInit }
Narayan Kamath973b4662014-03-31 13:41:26 +0100903};
904
905int register_com_android_internal_os_Zygote(JNIEnv* env) {
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800906 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
907 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
Robert Sesekd0a190df2018-02-12 18:46:01 -0500908 "(IZZLjava/lang/String;)V");
Narayan Kamath973b4662014-03-31 13:41:26 +0100909
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800910 return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
Narayan Kamath973b4662014-03-31 13:41:26 +0100911}
912} // namespace android