com_android_internal_os_Zygote.cpp revision 4878292b1b57edb69820e487c6a62a1095be4855
1/*
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
17#define LOG_TAG "Zygote"
18
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
23#include <list>
24#include <sstream>
25#include <string>
26
27#include <fcntl.h>
28#include <grp.h>
29#include <inttypes.h>
30#include <malloc.h>
31#include <mntent.h>
32#include <paths.h>
33#include <signal.h>
34#include <stdlib.h>
35#include <sys/capability.h>
36#include <sys/cdefs.h>
37#include <sys/personality.h>
38#include <sys/prctl.h>
39#include <sys/resource.h>
40#include <sys/stat.h>
41#include <sys/time.h>
42#include <sys/types.h>
43#include <sys/utsname.h>
44#include <sys/wait.h>
45#include <unistd.h>
46
47#include "android-base/logging.h"
48#include <android-base/file.h>
49#include <android-base/stringprintf.h>
50#include <cutils/fs.h>
51#include <cutils/multiuser.h>
52#include <cutils/sched_policy.h>
53#include <private/android_filesystem_config.h>
54#include <utils/String8.h>
55#include <selinux/android.h>
56#include <seccomp_policy.h>
57#include <processgroup/processgroup.h>
58
59#include "core_jni_helpers.h"
60#include <nativehelper/JNIHelp.h>
61#include <nativehelper/ScopedLocalRef.h>
62#include <nativehelper/ScopedPrimitiveArray.h>
63#include <nativehelper/ScopedUtfChars.h>
64#include "fd_utils.h"
65
66#include "nativebridge/native_bridge.h"
67
68namespace {
69
70using android::String8;
71using android::base::StringPrintf;
72using android::base::WriteStringToFile;
73
74static pid_t gSystemServerPid = 0;
75
76static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
77static jclass gZygoteClass;
78static jmethodID gCallPostForkChildHooks;
79
80static bool g_is_security_enforced = true;
81
82// Must match values in com.android.internal.os.Zygote.
83enum MountExternalKind {
84  MOUNT_EXTERNAL_NONE = 0,
85  MOUNT_EXTERNAL_DEFAULT = 1,
86  MOUNT_EXTERNAL_READ = 2,
87  MOUNT_EXTERNAL_WRITE = 3,
88};
89
90static 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());
94}
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
101  // 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
109  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)) {
118      ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
119    } else if (WIFSIGNALED(status)) {
120      ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
121      if (WCOREDUMP(status)) {
122        ALOGI("Process %d dumped core.", pid);
123      }
124    }
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) {
130      ALOGE("Exit zygote because system server (%d) has terminated", pid);
131      kill(getpid(), SIGKILL);
132    }
133  }
134
135  // 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));
139  }
140
141  errno = saved_errno;
142}
143
144// 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
147// have them be harvested immediately.
148//
149// 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//
159// This ends up being called repeatedly before each fork(), but there's
160// no real harm in that.
161static void SetSignalHandlers() {
162  struct sigaction sig_chld = {};
163  sig_chld.sa_handler = SigChldHandler;
164
165  if (sigaction(SIGCHLD, &sig_chld, NULL) < 0) {
166    ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
167  }
168
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  }
174}
175
176// Sets the SIGCHLD handler back to default behavior in zygote children.
177static void UnsetChldSignalHandler() {
178  struct sigaction sa;
179  memset(&sa, 0, sizeof(sa));
180  sa.sa_handler = SIG_DFL;
181
182  if (sigaction(SIGCHLD, &sa, NULL) < 0) {
183    ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
184  }
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) {
196    RuntimeAbort(env, __LINE__, "Getting gids int array failed");
197  }
198  int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
199  if (rc == -1) {
200    std::ostringstream oss;
201    oss << "setgroups failed: " << strerror(errno) << ", gids.size=" << gids.size();
202    RuntimeAbort(env, __LINE__, oss.str().c_str());
203  }
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) {
222      RuntimeAbort(env, __LINE__, "rlimits array must have a second dimension of size 3");
223    }
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) {
230      ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur,
231            rlim.rlim_max);
232      RuntimeAbort(env, __LINE__, "setrlimit failed");
233    }
234  }
235}
236
237// The debug malloc library needs to know whether it's the zygote or a child.
238extern "C" int gMallocLeakZygoteChild;
239
240static 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
248static 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
262static void EnableKeepCapabilities(JNIEnv* env) {
263  int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
264  if (rc == -1) {
265    RuntimeAbort(env, __LINE__, "prctl(PR_SET_KEEPCAPS) failed");
266  }
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 {
277        ALOGE("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno));
278        RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed");
279      }
280    }
281  }
282}
283
284static 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) {
307  __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;
318  capdata[0].inheritable = inheritable;
319  capdata[1].inheritable = inheritable >> 32;
320
321  if (capset(&capheader, &capdata[0]) == -1) {
322    ALOGE("capset(perm=%" PRIx64 ", eff=%" PRIx64 ", inh=%" PRIx64 ") failed: %s", permitted,
323          effective, inheritable, strerror(errno));
324    RuntimeAbort(env, __LINE__, "capset failed");
325  }
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");
332    RuntimeAbort(env, __LINE__, "set_sched_policy(0, SP_DEFAULT) failed");
333  }
334}
335
336static 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
364// Create a private mount namespace and bind mount appropriate emulated
365// storage for the given user.
366static 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
370    String8 storageSource;
371    if (mount_mode == MOUNT_EXTERNAL_DEFAULT) {
372        storageSource = "/mnt/runtime/default";
373    } else if (mount_mode == MOUNT_EXTERNAL_READ) {
374        storageSource = "/mnt/runtime/read";
375    } else if (mount_mode == MOUNT_EXTERNAL_WRITE) {
376        storageSource = "/mnt/runtime/write";
377    } else if (!force_mount_namespace) {
378        // Sane default of no storage visible
379        return true;
380    }
381
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
388    // Handle force_mount_namespace with MOUNT_EXTERNAL_NONE.
389    if (mount_mode == MOUNT_EXTERNAL_NONE) {
390        return true;
391    }
392
393    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
399    // Mount user-specific symlink helper into place
400    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
411    return true;
412}
413
414static 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}
433
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);
444  ScopedIntArrayRO ar(env, fdsToClose);
445  if (ar.get() == NULL) {
446      RuntimeAbort(env, __LINE__, "Bad fd array");
447  }
448  jsize i;
449  int devnull;
450  for (i = 0; i < count; i++) {
451    devnull = open("/dev/null", O_RDWR);
452    if (devnull < 0) {
453      ALOGE("Failed to open /dev/null: %s", strerror(errno));
454      RuntimeAbort(env, __LINE__, "Failed to open /dev/null");
455      continue;
456    }
457    ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno));
458    if (dup2(devnull, ar[i]) < 0) {
459      ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno));
460      RuntimeAbort(env, __LINE__, "Failed dup2()");
461    }
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) {
489    ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
490  }
491}
492
493// The list of open zygote file descriptors.
494static FileDescriptorTable* gOpenFdTable = NULL;
495
496static void FillFileDescriptorVector(JNIEnv* env,
497                                     jintArray java_fds,
498                                     std::vector<int>* fds) {
499  CHECK(fds != nullptr);
500  if (java_fds != nullptr) {
501    ScopedIntArrayRO ar(env, java_fds);
502    if (ar.get() == nullptr) {
503      RuntimeAbort(env, __LINE__, "Bad fd array");
504    }
505    fds->reserve(ar.size());
506    for (size_t i = 0; i < ar.size(); ++i) {
507      fds->push_back(ar[i]);
508    }
509  }
510}
511
512// Utility routine to fork zygote and specialize the child process.
513static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
514                                     jint runtime_flags, jobjectArray javaRlimits,
515                                     jlong permittedCapabilities, jlong effectiveCapabilities,
516                                     jint mount_external,
517                                     jstring java_se_info, jstring java_se_name,
518                                     bool is_system_server, jintArray fdsToClose,
519                                     jintArray fdsToIgnore,
520                                     jstring instructionSet, jstring dataDir) {
521  SetSignalHandlers();
522
523  sigset_t sigchld;
524  sigemptyset(&sigchld);
525  sigaddset(&sigchld, SIGCHLD);
526
527  // Temporarily block SIGCHLD during forks. The SIGCHLD handler might
528  // log, which would result in the logging FDs we close being reopened.
529  // This would cause failures because the FDs are not whitelisted.
530  //
531  // Note that the zygote process is single threaded at this point.
532  if (sigprocmask(SIG_BLOCK, &sigchld, nullptr) == -1) {
533    ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
534    RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_BLOCK, { SIGCHLD }) failed.");
535  }
536
537  // Close any logging related FDs before we start evaluating the list of
538  // file descriptors.
539  __android_log_close();
540
541  // If this is the first fork for this zygote, create the open FD table.
542  // If it isn't, we just need to check whether the list of open files has
543  // changed (and it shouldn't in the normal case).
544  std::vector<int> fds_to_ignore;
545  FillFileDescriptorVector(env, fdsToIgnore, &fds_to_ignore);
546  if (gOpenFdTable == NULL) {
547    gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore);
548    if (gOpenFdTable == NULL) {
549      RuntimeAbort(env, __LINE__, "Unable to construct file descriptor table.");
550    }
551  } else if (!gOpenFdTable->Restat(fds_to_ignore)) {
552    RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table.");
553  }
554
555  pid_t pid = fork();
556
557  if (pid == 0) {
558    PreApplicationInit();
559
560    // Clean up any descriptors which must be closed immediately
561    DetachDescriptors(env, fdsToClose);
562
563    // Re-open all remaining open file descriptors so that they aren't shared
564    // with the zygote across a fork.
565    if (!gOpenFdTable->ReopenOrDetach()) {
566      RuntimeAbort(env, __LINE__, "Unable to reopen whitelisted descriptors.");
567    }
568
569    if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
570      ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
571      RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
572    }
573
574    // Must be called when the new process still has CAP_SYS_ADMIN.  The other alternative is to
575    // call prctl(PR_SET_NO_NEW_PRIVS, 1) afterward, but that breaks SELinux domain transition (see
576    // b/71859146).
577    SetUpSeccompFilter(uid);
578
579    // Keep capabilities across UID change, unless we're staying root.
580    if (uid != 0) {
581      EnableKeepCapabilities(env);
582    }
583
584    SetInheritable(env, permittedCapabilities);
585    DropCapabilitiesBoundingSet(env);
586
587    bool use_native_bridge = !is_system_server && (instructionSet != NULL)
588        && android::NativeBridgeAvailable();
589    if (use_native_bridge) {
590      ScopedUtfChars isa_string(env, instructionSet);
591      use_native_bridge = android::NeedsNativeBridge(isa_string.c_str());
592    }
593    if (use_native_bridge && dataDir == NULL) {
594      // dataDir should never be null if we need to use a native bridge.
595      // In general, dataDir will never be null for normal applications. It can only happen in
596      // special cases (for isolated processes which are not associated with any app). These are
597      // launched by the framework and should not be emulated anyway.
598      use_native_bridge = false;
599      ALOGW("Native bridge will not be used because dataDir == NULL.");
600    }
601
602    if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) {
603      ALOGW("Failed to mount emulated storage: %s", strerror(errno));
604      if (errno == ENOTCONN || errno == EROFS) {
605        // When device is actively encrypting, we get ENOTCONN here
606        // since FUSE was mounted before the framework restarted.
607        // When encrypted device is booting, we get EROFS since
608        // FUSE hasn't been created yet by init.
609        // In either case, continue without external storage.
610      } else {
611        RuntimeAbort(env, __LINE__, "Cannot continue without emulated storage");
612      }
613    }
614
615    if (!is_system_server) {
616        int rc = createProcessGroup(uid, getpid());
617        if (rc != 0) {
618            if (rc == -EROFS) {
619                ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?");
620            } else {
621                ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
622            }
623        }
624    }
625
626    SetGids(env, javaGids);
627
628    SetRLimits(env, javaRlimits);
629
630    if (use_native_bridge) {
631      ScopedUtfChars isa_string(env, instructionSet);
632      ScopedUtfChars data_dir(env, dataDir);
633      android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str());
634    }
635
636    int rc = setresgid(gid, gid, gid);
637    if (rc == -1) {
638      ALOGE("setresgid(%d) failed: %s", gid, strerror(errno));
639      RuntimeAbort(env, __LINE__, "setresgid failed");
640    }
641
642    rc = setresuid(uid, uid, uid);
643    if (rc == -1) {
644      ALOGE("setresuid(%d) failed: %s", uid, strerror(errno));
645      RuntimeAbort(env, __LINE__, "setresuid failed");
646    }
647
648    if (NeedsNoRandomizeWorkaround()) {
649        // Work around ARM kernel ASLR lossage (http://b/5817320).
650        int old_personality = personality(0xffffffff);
651        int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
652        if (new_personality == -1) {
653            ALOGW("personality(%d) failed: %s", new_personality, strerror(errno));
654        }
655    }
656
657    SetCapabilities(env, permittedCapabilities, effectiveCapabilities, permittedCapabilities);
658
659    SetSchedulerPolicy(env);
660
661    const char* se_info_c_str = NULL;
662    ScopedUtfChars* se_info = NULL;
663    if (java_se_info != NULL) {
664        se_info = new ScopedUtfChars(env, java_se_info);
665        se_info_c_str = se_info->c_str();
666        if (se_info_c_str == NULL) {
667          RuntimeAbort(env, __LINE__, "se_info_c_str == NULL");
668        }
669    }
670    const char* se_name_c_str = NULL;
671    ScopedUtfChars* se_name = NULL;
672    if (java_se_name != NULL) {
673        se_name = new ScopedUtfChars(env, java_se_name);
674        se_name_c_str = se_name->c_str();
675        if (se_name_c_str == NULL) {
676          RuntimeAbort(env, __LINE__, "se_name_c_str == NULL");
677        }
678    }
679    rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
680    if (rc == -1) {
681      ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
682            is_system_server, se_info_c_str, se_name_c_str);
683      RuntimeAbort(env, __LINE__, "selinux_android_setcontext failed");
684    }
685
686    // Make it easier to debug audit logs by setting the main thread's name to the
687    // nice name rather than "app_process".
688    if (se_info_c_str == NULL && is_system_server) {
689      se_name_c_str = "system_server";
690    }
691    if (se_info_c_str != NULL) {
692      SetThreadName(se_name_c_str);
693    }
694
695    delete se_info;
696    delete se_name;
697
698    // Unset the SIGCHLD handler, but keep ignoring SIGHUP (rationale in SetSignalHandlers).
699    UnsetChldSignalHandler();
700
701    env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags,
702                              is_system_server, instructionSet);
703    if (env->ExceptionCheck()) {
704      RuntimeAbort(env, __LINE__, "Error calling post fork hooks.");
705    }
706  } else if (pid > 0) {
707    // the parent process
708
709    // We blocked SIGCHLD prior to a fork, we unblock it here.
710    if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) {
711      ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno));
712      RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed.");
713    }
714  }
715  return pid;
716}
717
718static uint64_t GetEffectiveCapabilityMask(JNIEnv* env) {
719    __user_cap_header_struct capheader;
720    memset(&capheader, 0, sizeof(capheader));
721    capheader.version = _LINUX_CAPABILITY_VERSION_3;
722    capheader.pid = 0;
723
724    __user_cap_data_struct capdata[2];
725    if (capget(&capheader, &capdata[0]) == -1) {
726        ALOGE("capget failed: %s", strerror(errno));
727        RuntimeAbort(env, __LINE__, "capget failed");
728    }
729
730    return capdata[0].effective |
731           (static_cast<uint64_t>(capdata[1].effective) << 32);
732}
733}  // anonymous namespace
734
735namespace android {
736
737static void com_android_internal_os_Zygote_nativeSecurityInit(JNIEnv*, jclass) {
738  // security_getenforce is not allowed on app process. Initialize and cache the value before
739  // zygote forks.
740  g_is_security_enforced = security_getenforce();
741}
742
743static void com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv*, jclass) {
744  PreApplicationInit();
745}
746
747static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
748        JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
749        jint runtime_flags, jobjectArray rlimits,
750        jint mount_external, jstring se_info, jstring se_name,
751        jintArray fdsToClose,
752        jintArray fdsToIgnore,
753        jstring instructionSet, jstring appDataDir) {
754    jlong capabilities = 0;
755
756    // Grant CAP_WAKE_ALARM to the Bluetooth process.
757    // Additionally, allow bluetooth to open packet sockets so it can start the DHCP client.
758    // Grant CAP_SYS_NICE to allow Bluetooth to set RT priority for
759    // audio-related threads.
760    // TODO: consider making such functionality an RPC to netd.
761    if (multiuser_get_app_id(uid) == AID_BLUETOOTH) {
762      capabilities |= (1LL << CAP_WAKE_ALARM);
763      capabilities |= (1LL << CAP_NET_RAW);
764      capabilities |= (1LL << CAP_NET_BIND_SERVICE);
765      capabilities |= (1LL << CAP_SYS_NICE);
766    }
767
768    // Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock"
769    bool gid_wakelock_found = false;
770    if (gid == AID_WAKELOCK) {
771      gid_wakelock_found = true;
772    } else if (gids != NULL) {
773      jsize gids_num = env->GetArrayLength(gids);
774      ScopedIntArrayRO ar(env, gids);
775      if (ar.get() == NULL) {
776        RuntimeAbort(env, __LINE__, "Bad gids array");
777      }
778      for (int i = 0; i < gids_num; i++) {
779        if (ar[i] == AID_WAKELOCK) {
780          gid_wakelock_found = true;
781          break;
782        }
783      }
784    }
785    if (gid_wakelock_found) {
786      capabilities |= (1LL << CAP_BLOCK_SUSPEND);
787    }
788
789    // Containers run without some capabilities, so drop any caps that are not
790    // available.
791    capabilities &= GetEffectiveCapabilityMask(env);
792
793    return ForkAndSpecializeCommon(env, uid, gid, gids, runtime_flags,
794            rlimits, capabilities, capabilities, mount_external, se_info,
795            se_name, false, fdsToClose, fdsToIgnore, instructionSet, appDataDir);
796}
797
798static jint com_android_internal_os_Zygote_nativeForkSystemServer(
799        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
800        jint runtime_flags, jobjectArray rlimits, jlong permittedCapabilities,
801        jlong effectiveCapabilities) {
802  pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
803                                      runtime_flags, rlimits,
804                                      permittedCapabilities, effectiveCapabilities,
805                                      MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL,
806                                      NULL, NULL, NULL);
807  if (pid > 0) {
808      // The zygote process checks whether the child process has died or not.
809      ALOGI("System server process %d has been created", pid);
810      gSystemServerPid = pid;
811      // There is a slight window that the system server process has crashed
812      // but it went unnoticed because we haven't published its pid yet. So
813      // we recheck here just to make sure that all is well.
814      int status;
815      if (waitpid(pid, &status, WNOHANG) == pid) {
816          ALOGE("System server process %d has died. Restarting Zygote!", pid);
817          RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
818      }
819
820      // Assign system_server to the correct memory cgroup.
821      // Not all devices mount /dev/memcg so check for the file first
822      // to avoid unnecessarily printing errors and denials in the logs.
823      if (!access("/dev/memcg/system/tasks", F_OK) &&
824                !WriteStringToFile(StringPrintf("%d", pid), "/dev/memcg/system/tasks")) {
825        ALOGE("couldn't write %d to /dev/memcg/system/tasks", pid);
826      }
827  }
828  return pid;
829}
830
831static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork(
832        JNIEnv* env, jclass, jstring path) {
833    ScopedUtfChars path_native(env, path);
834    const char* path_cstr = path_native.c_str();
835    if (!path_cstr) {
836        RuntimeAbort(env, __LINE__, "path_cstr == NULL");
837    }
838    FileDescriptorWhitelist::Get()->Allow(path_cstr);
839}
840
841static void com_android_internal_os_Zygote_nativeUnmountStorageOnInit(JNIEnv* env, jclass) {
842    // Zygote process unmount root storage space initially before every child processes are forked.
843    // Every forked child processes (include SystemServer) only mount their own root storage space
844    // and no need unmount storage operation in MountEmulatedStorage method.
845    // Zygote process does not utilize root storage spaces and unshares its mount namespace below.
846
847    // See storage config details at http://source.android.com/tech/storage/
848    // Create private mount namespace shared by all children
849    if (unshare(CLONE_NEWNS) == -1) {
850        RuntimeAbort(env, __LINE__, "Failed to unshare()");
851        return;
852    }
853
854    // Mark rootfs as being a slave so that changes from default
855    // namespace only flow into our children.
856    if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
857        RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE");
858        return;
859    }
860
861    // Create a staging tmpfs that is shared by our children; they will
862    // bind mount storage into their respective private namespaces, which
863    // are isolated from each other.
864    const char* target_base = getenv("EMULATED_STORAGE_TARGET");
865    if (target_base != nullptr) {
866#define STRINGIFY_UID(x) __STRING(x)
867        if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
868                  "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) {
869            ALOGE("Failed to mount tmpfs to %s", target_base);
870            RuntimeAbort(env, __LINE__, "Failed to mount tmpfs");
871            return;
872        }
873#undef STRINGIFY_UID
874    }
875
876    UnmountTree("/storage");
877}
878
879static const JNINativeMethod gMethods[] = {
880    { "nativeSecurityInit", "()V",
881      (void *) com_android_internal_os_Zygote_nativeSecurityInit },
882    { "nativeForkAndSpecialize",
883      "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;)I",
884      (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
885    { "nativeForkSystemServer", "(II[II[[IJJ)I",
886      (void *) com_android_internal_os_Zygote_nativeForkSystemServer },
887    { "nativeAllowFileAcrossFork", "(Ljava/lang/String;)V",
888      (void *) com_android_internal_os_Zygote_nativeAllowFileAcrossFork },
889    { "nativeUnmountStorageOnInit", "()V",
890      (void *) com_android_internal_os_Zygote_nativeUnmountStorageOnInit },
891    { "nativePreApplicationInit", "()V",
892      (void *) com_android_internal_os_Zygote_nativePreApplicationInit }
893};
894
895int register_com_android_internal_os_Zygote(JNIEnv* env) {
896  gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
897  gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
898                                                   "(IZLjava/lang/String;)V");
899
900  return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
901}
902}  // namespace android
903