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