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