com_android_internal_os_Zygote.cpp revision 0161bbc6e54744d5e0348af3d123858818f966e9
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 <grp.h>
24#include <fcntl.h>
25#include <paths.h>
26#include <signal.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <sys/capability.h>
30#include <sys/personality.h>
31#include <sys/prctl.h>
32#include <sys/resource.h>
33#include <sys/stat.h>
34#include <sys/types.h>
35#include <sys/utsname.h>
36#include <sys/wait.h>
37
38
39#include <cutils/fs.h>
40#include <cutils/multiuser.h>
41#include <cutils/sched_policy.h>
42#include <utils/String8.h>
43#include <selinux/android.h>
44#include <processgroup/processgroup.h>
45
46#include "android_runtime/AndroidRuntime.h"
47#include "JNIHelp.h"
48#include "ScopedLocalRef.h"
49#include "ScopedPrimitiveArray.h"
50#include "ScopedUtfChars.h"
51
52namespace {
53
54using android::String8;
55
56static pid_t gSystemServerPid = 0;
57
58static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
59static jclass gZygoteClass;
60static jmethodID gCallPostForkChildHooks;
61
62// Must match values in com.android.internal.os.Zygote.
63enum MountExternalKind {
64  MOUNT_EXTERNAL_NONE = 0,
65  MOUNT_EXTERNAL_SINGLEUSER = 1,
66  MOUNT_EXTERNAL_MULTIUSER = 2,
67  MOUNT_EXTERNAL_MULTIUSER_ALL = 3,
68};
69
70static void RuntimeAbort(JNIEnv* env) {
71  env->FatalError("RuntimeAbort");
72}
73
74// This signal handler is for zygote mode, since the zygote must reap its children
75static void SigChldHandler(int /*signal_number*/) {
76  pid_t pid;
77  int status;
78
79  while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
80     // Log process-death status that we care about.  In general it is
81     // not safe to call LOG(...) from a signal handler because of
82     // possible reentrancy.  However, we know a priori that the
83     // current implementation of LOG() is safe to call from a SIGCHLD
84     // handler in the zygote process.  If the LOG() implementation
85     // changes its locking strategy or its use of syscalls within the
86     // lazy-init critical section, its use here may become unsafe.
87    if (WIFEXITED(status)) {
88      if (WEXITSTATUS(status)) {
89        ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status));
90      }
91    } else if (WIFSIGNALED(status)) {
92      if (WTERMSIG(status) != SIGKILL) {
93        ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status));
94      }
95      if (WCOREDUMP(status)) {
96        ALOGI("Process %d dumped core.", pid);
97      }
98    }
99
100    // If the just-crashed process is the system_server, bring down zygote
101    // so that it is restarted by init and system server will be restarted
102    // from there.
103    if (pid == gSystemServerPid) {
104      ALOGE("Exit zygote because system server (%d) has terminated");
105      kill(getpid(), SIGKILL);
106    }
107  }
108
109  // Note that we shouldn't consider ECHILD an error because
110  // the secondary zygote might have no children left to wait for.
111  if (pid < 0 && errno != ECHILD) {
112    ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno));
113  }
114}
115
116// Configures the SIGCHLD handler for the zygote process. This is configured
117// very late, because earlier in the runtime we may fork() and exec()
118// other processes, and we want to waitpid() for those rather than
119// have them be harvested immediately.
120//
121// This ends up being called repeatedly before each fork(), but there's
122// no real harm in that.
123static void SetSigChldHandler() {
124  struct sigaction sa;
125  memset(&sa, 0, sizeof(sa));
126  sa.sa_handler = SigChldHandler;
127
128  int err = sigaction(SIGCHLD, &sa, NULL);
129  if (err < 0) {
130    ALOGW("Error setting SIGCHLD handler: %d", errno);
131  }
132}
133
134// Sets the SIGCHLD handler back to default behavior in zygote children.
135static void UnsetSigChldHandler() {
136  struct sigaction sa;
137  memset(&sa, 0, sizeof(sa));
138  sa.sa_handler = SIG_DFL;
139
140  int err = sigaction(SIGCHLD, &sa, NULL);
141  if (err < 0) {
142    ALOGW("Error unsetting SIGCHLD handler: %d", errno);
143  }
144}
145
146// Calls POSIX setgroups() using the int[] object as an argument.
147// A NULL argument is tolerated.
148static void SetGids(JNIEnv* env, jintArray javaGids) {
149  if (javaGids == NULL) {
150    return;
151  }
152
153  ScopedIntArrayRO gids(env, javaGids);
154  if (gids.get() == NULL) {
155      RuntimeAbort(env);
156  }
157  int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0]));
158  if (rc == -1) {
159    ALOGE("setgroups failed");
160    RuntimeAbort(env);
161  }
162}
163
164// Sets the resource limits via setrlimit(2) for the values in the
165// two-dimensional array of integers that's passed in. The second dimension
166// contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is
167// treated as an empty array.
168static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) {
169  if (javaRlimits == NULL) {
170    return;
171  }
172
173  rlimit rlim;
174  memset(&rlim, 0, sizeof(rlim));
175
176  for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) {
177    ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i));
178    ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get()));
179    if (javaRlimit.size() != 3) {
180      ALOGE("rlimits array must have a second dimension of size 3");
181      RuntimeAbort(env);
182    }
183
184    rlim.rlim_cur = javaRlimit[1];
185    rlim.rlim_max = javaRlimit[2];
186
187    int rc = setrlimit(javaRlimit[0], &rlim);
188    if (rc == -1) {
189      ALOGE("setrlimit(%d, {%d, %d}) failed", javaRlimit[0], rlim.rlim_cur, rlim.rlim_max);
190      RuntimeAbort(env);
191    }
192  }
193}
194
195// The debug malloc library needs to know whether it's the zygote or a child.
196extern "C" int gMallocLeakZygoteChild;
197
198static void EnableKeepCapabilities(JNIEnv* env) {
199  int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0);
200  if (rc == -1) {
201    ALOGE("prctl(PR_SET_KEEPCAPS) failed");
202    RuntimeAbort(env);
203  }
204}
205
206static void DropCapabilitiesBoundingSet(JNIEnv* env) {
207  for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {
208    int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0);
209    if (rc == -1) {
210      if (errno == EINVAL) {
211        ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
212              "your kernel is compiled with file capabilities support");
213      } else {
214        ALOGE("prctl(PR_CAPBSET_DROP) failed");
215        RuntimeAbort(env);
216      }
217    }
218  }
219}
220
221static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) {
222  __user_cap_header_struct capheader;
223  memset(&capheader, 0, sizeof(capheader));
224  capheader.version = _LINUX_CAPABILITY_VERSION_3;
225  capheader.pid = 0;
226
227  __user_cap_data_struct capdata[2];
228  memset(&capdata, 0, sizeof(capdata));
229  capdata[0].effective = effective;
230  capdata[1].effective = effective >> 32;
231  capdata[0].permitted = permitted;
232  capdata[1].permitted = permitted >> 32;
233
234  if (capset(&capheader, &capdata[0]) == -1) {
235    ALOGE("capset(%lld, %lld) failed", permitted, effective);
236    RuntimeAbort(env);
237  }
238}
239
240static void SetSchedulerPolicy(JNIEnv* env) {
241  errno = -set_sched_policy(0, SP_DEFAULT);
242  if (errno != 0) {
243    ALOGE("set_sched_policy(0, SP_DEFAULT) failed");
244    RuntimeAbort(env);
245  }
246}
247
248// Create a private mount namespace and bind mount appropriate emulated
249// storage for the given user.
250static bool MountEmulatedStorage(uid_t uid, jint mount_mode) {
251  if (mount_mode == MOUNT_EXTERNAL_NONE) {
252    return true;
253  }
254
255  // See storage config details at http://source.android.com/tech/storage/
256  userid_t user_id = multiuser_get_user_id(uid);
257
258  // Create a second private mount namespace for our process
259  if (unshare(CLONE_NEWNS) == -1) {
260      ALOGW("Failed to unshare(): %d", errno);
261      return false;
262  }
263
264  // Create bind mounts to expose external storage
265  if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
266    // These paths must already be created by init.rc
267    const char* source = getenv("EMULATED_STORAGE_SOURCE");
268    const char* target = getenv("EMULATED_STORAGE_TARGET");
269    const char* legacy = getenv("EXTERNAL_STORAGE");
270    if (source == NULL || target == NULL || legacy == NULL) {
271      ALOGW("Storage environment undefined; unable to provide external storage");
272      return false;
273    }
274
275    // Prepare source paths
276
277    // /mnt/shell/emulated/0
278    const String8 source_user(String8::format("%s/%d", source, user_id));
279    // /storage/emulated/0
280    const String8 target_user(String8::format("%s/%d", target, user_id));
281
282    if (fs_prepare_dir(source_user.string(), 0000, 0, 0) == -1
283        || fs_prepare_dir(target_user.string(), 0000, 0, 0) == -1) {
284      return false;
285    }
286
287    if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) {
288      // Mount entire external storage tree for all users
289      if (TEMP_FAILURE_RETRY(mount(source, target, NULL, MS_BIND, NULL)) == -1) {
290        ALOGW("Failed to mount %s to %s :%d", source, target, errno);
291        return false;
292      }
293    } else {
294      // Only mount user-specific external storage
295      if (TEMP_FAILURE_RETRY(
296              mount(source_user.string(), target_user.string(), NULL, MS_BIND, NULL)) == -1) {
297        ALOGW("Failed to mount %s to %s: %d", source_user.string(), target_user.string(), errno);
298        return false;
299      }
300    }
301
302    if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) {
303        return false;
304    }
305
306    // Finally, mount user-specific path into place for legacy users
307    if (TEMP_FAILURE_RETRY(
308            mount(target_user.string(), legacy, NULL, MS_BIND | MS_REC, NULL)) == -1) {
309      ALOGW("Failed to mount %s to %s: %d", target_user.string(), legacy, errno);
310      return false;
311    }
312  } else {
313    ALOGW("Mount mode %d unsupported", mount_mode);
314    return false;
315  }
316
317  return true;
318}
319
320static bool NeedsNoRandomizeWorkaround() {
321#if !defined(__arm__)
322    return false;
323#else
324    int major;
325    int minor;
326    struct utsname uts;
327    if (uname(&uts) == -1) {
328        return false;
329    }
330
331    if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) {
332        return false;
333    }
334
335    // Kernels before 3.4.* need the workaround.
336    return (major < 3) || ((major == 3) && (minor < 4));
337#endif
338}
339
340// Utility to close down the Zygote socket file descriptors while
341// the child is still running as root with Zygote's privileges.  Each
342// descriptor (if any) is closed via dup2(), replacing it with a valid
343// (open) descriptor to /dev/null.
344
345static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) {
346  if (!fdsToClose) {
347    return;
348  }
349  jsize count = env->GetArrayLength(fdsToClose);
350  jint *ar = env->GetIntArrayElements(fdsToClose, 0);
351  if (!ar) {
352      ALOGE("Bad fd array");
353      RuntimeAbort(env);
354  }
355  jsize i;
356  int devnull;
357  for (i = 0; i < count; i++) {
358    devnull = open("/dev/null", O_RDWR);
359    if (devnull < 0) {
360      ALOGE("Failed to open /dev/null");
361      RuntimeAbort(env);
362      continue;
363    }
364    ALOGV("Switching descriptor %d to /dev/null: %d", ar[i], errno);
365    if (dup2(devnull, ar[i]) < 0) {
366      ALOGE("Failed dup2() on descriptor %d", ar[i]);
367      RuntimeAbort(env);
368    }
369    close(devnull);
370  }
371}
372
373void SetThreadName(const char* thread_name) {
374  bool hasAt = false;
375  bool hasDot = false;
376  const char* s = thread_name;
377  while (*s) {
378    if (*s == '.') {
379      hasDot = true;
380    } else if (*s == '@') {
381      hasAt = true;
382    }
383    s++;
384  }
385  const int len = s - thread_name;
386  if (len < 15 || hasAt || !hasDot) {
387    s = thread_name;
388  } else {
389    s = thread_name + len - 15;
390  }
391  // pthread_setname_np fails rather than truncating long strings.
392  char buf[16];       // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
393  strlcpy(buf, s, sizeof(buf)-1);
394  errno = pthread_setname_np(pthread_self(), buf);
395  if (errno != 0) {
396    ALOGW("Unable to set the name of current thread to '%s'", buf);
397  }
398}
399
400// Utility routine to fork zygote and specialize the child process.
401static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids,
402                                     jint debug_flags, jobjectArray javaRlimits,
403                                     jlong permittedCapabilities, jlong effectiveCapabilities,
404                                     jint mount_external,
405                                     jstring java_se_info, jstring java_se_name,
406                                     bool is_system_server, jintArray fdsToClose) {
407  SetSigChldHandler();
408
409  pid_t pid = fork();
410
411  if (pid == 0) {
412    // The child process.
413    gMallocLeakZygoteChild = 1;
414
415    // Clean up any descriptors which must be closed immediately
416    DetachDescriptors(env, fdsToClose);
417
418    // Keep capabilities across UID change, unless we're staying root.
419    if (uid != 0) {
420      EnableKeepCapabilities(env);
421    }
422
423    DropCapabilitiesBoundingSet(env);
424
425    if (!MountEmulatedStorage(uid, mount_external)) {
426      ALOGW("Failed to mount emulated storage: %d", errno);
427      if (errno == ENOTCONN || errno == EROFS) {
428        // When device is actively encrypting, we get ENOTCONN here
429        // since FUSE was mounted before the framework restarted.
430        // When encrypted device is booting, we get EROFS since
431        // FUSE hasn't been created yet by init.
432        // In either case, continue without external storage.
433      } else {
434        ALOGE("Cannot continue without emulated storage");
435        RuntimeAbort(env);
436      }
437    }
438
439    if (!is_system_server) {
440        int rc = createProcessGroup(uid, getpid());
441        if (rc != 0) {
442            ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc));
443            RuntimeAbort(env);
444        }
445    }
446
447    SetGids(env, javaGids);
448
449    SetRLimits(env, javaRlimits);
450
451    int rc = setresgid(gid, gid, gid);
452    if (rc == -1) {
453      ALOGE("setresgid(%d) failed", gid);
454      RuntimeAbort(env);
455    }
456
457    rc = setresuid(uid, uid, uid);
458    if (rc == -1) {
459      ALOGE("setresuid(%d) failed", uid);
460      RuntimeAbort(env);
461    }
462
463    if (NeedsNoRandomizeWorkaround()) {
464        // Work around ARM kernel ASLR lossage (http://b/5817320).
465        int old_personality = personality(0xffffffff);
466        int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE);
467        if (new_personality == -1) {
468            ALOGW("personality(%d) failed", new_personality);
469        }
470    }
471
472    SetCapabilities(env, permittedCapabilities, effectiveCapabilities);
473
474    SetSchedulerPolicy(env);
475
476    const char* se_info_c_str = NULL;
477    ScopedUtfChars* se_info = NULL;
478    if (java_se_info != NULL) {
479        se_info = new ScopedUtfChars(env, java_se_info);
480        se_info_c_str = se_info->c_str();
481        if (se_info_c_str == NULL) {
482          ALOGE("se_info_c_str == NULL");
483          RuntimeAbort(env);
484        }
485    }
486    const char* se_name_c_str = NULL;
487    ScopedUtfChars* se_name = NULL;
488    if (java_se_name != NULL) {
489        se_name = new ScopedUtfChars(env, java_se_name);
490        se_name_c_str = se_name->c_str();
491        if (se_name_c_str == NULL) {
492          ALOGE("se_name_c_str == NULL");
493          RuntimeAbort(env);
494        }
495    }
496    rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str);
497    if (rc == -1) {
498      ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
499            is_system_server, se_info_c_str, se_name_c_str);
500      RuntimeAbort(env);
501    }
502
503    // Make it easier to debug audit logs by setting the main thread's name to the
504    // nice name rather than "app_process".
505    if (se_info_c_str == NULL && is_system_server) {
506      se_name_c_str = "system_server";
507    }
508    if (se_info_c_str != NULL) {
509      SetThreadName(se_name_c_str);
510    }
511
512    delete se_info;
513    delete se_name;
514
515    UnsetSigChldHandler();
516
517    env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags);
518    if (env->ExceptionCheck()) {
519      ALOGE("Error calling post fork hooks.");
520      RuntimeAbort(env);
521    }
522  } else if (pid > 0) {
523    // the parent process
524  }
525  return pid;
526}
527}  // anonymous namespace
528
529namespace android {
530
531static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
532        JNIEnv* env, jclass, jint uid, jint gid, jintArray gids,
533        jint debug_flags, jobjectArray rlimits,
534        jint mount_external, jstring se_info, jstring se_name,
535        jintArray fdsToClose) {
536    return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags,
537            rlimits, 0, 0, mount_external, se_info, se_name, false, fdsToClose);
538}
539
540static jint com_android_internal_os_Zygote_nativeForkSystemServer(
541        JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
542        jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities,
543        jlong effectiveCapabilities) {
544  pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids,
545                                      debug_flags, rlimits,
546                                      permittedCapabilities, effectiveCapabilities,
547                                      MOUNT_EXTERNAL_NONE, NULL, NULL, true, NULL);
548  if (pid > 0) {
549      // The zygote process checks whether the child process has died or not.
550      ALOGI("System server process %d has been created", pid);
551      gSystemServerPid = pid;
552      // There is a slight window that the system server process has crashed
553      // but it went unnoticed because we haven't published its pid yet. So
554      // we recheck here just to make sure that all is well.
555      int status;
556      if (waitpid(pid, &status, WNOHANG) == pid) {
557          ALOGE("System server process %d has died. Restarting Zygote!", pid);
558          RuntimeAbort(env);
559      }
560  }
561  return pid;
562}
563
564static JNINativeMethod gMethods[] = {
565    { "nativeForkAndSpecialize", "(II[II[[IILjava/lang/String;Ljava/lang/String;[I)I",
566      (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize },
567    { "nativeForkSystemServer", "(II[II[[IJJ)I",
568      (void *) com_android_internal_os_Zygote_nativeForkSystemServer }
569};
570
571int register_com_android_internal_os_Zygote(JNIEnv* env) {
572  gZygoteClass = (jclass) env->NewGlobalRef(env->FindClass(kZygoteClassName));
573  if (gZygoteClass == NULL) {
574    RuntimeAbort(env);
575  }
576  gCallPostForkChildHooks = env->GetStaticMethodID(gZygoteClass, "callPostForkChildHooks", "(I)V");
577
578  return AndroidRuntime::registerNativeMethods(env, "com/android/internal/os/Zygote",
579      gMethods, NELEM(gMethods));
580}
581}  // namespace android
582
583