Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 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 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 17 | #define LOG_TAG "Zygote" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 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 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 23 | #include <list> |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 24 | #include <sstream> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 25 | #include <string> |
| 26 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 27 | #include <fcntl.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 28 | #include <grp.h> |
| 29 | #include <inttypes.h> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 30 | #include <mntent.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 31 | #include <paths.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdlib.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 34 | #include <sys/capability.h> |
Robert Sesek | e4f8d69 | 2016-09-13 19:13:01 -0400 | [diff] [blame] | 35 | #include <sys/cdefs.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 36 | #include <sys/personality.h> |
| 37 | #include <sys/prctl.h> |
| 38 | #include <sys/resource.h> |
| 39 | #include <sys/stat.h> |
Vitalii Tomkiv | 5cbce85 | 2016-05-18 17:43:02 -0700 | [diff] [blame] | 40 | #include <sys/time.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | #include <sys/utsname.h> |
| 43 | #include <sys/wait.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 44 | #include <unistd.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 45 | |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 46 | #include "android-base/logging.h" |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 47 | #include <cutils/fs.h> |
| 48 | #include <cutils/multiuser.h> |
| 49 | #include <cutils/sched_policy.h> |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 50 | #include <private/android_filesystem_config.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 51 | #include <utils/String8.h> |
| 52 | #include <selinux/android.h> |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 53 | #include <processgroup/processgroup.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 54 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 55 | #include "core_jni_helpers.h" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 56 | #include "JNIHelp.h" |
| 57 | #include "ScopedLocalRef.h" |
| 58 | #include "ScopedPrimitiveArray.h" |
| 59 | #include "ScopedUtfChars.h" |
Robert Sesek | 8225b7c | 2016-12-16 14:02:31 -0500 | [diff] [blame] | 60 | #include "fd_utils.h" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 61 | |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 62 | #include "nativebridge/native_bridge.h" |
| 63 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 64 | namespace { |
| 65 | |
| 66 | using android::String8; |
| 67 | |
| 68 | static pid_t gSystemServerPid = 0; |
| 69 | |
| 70 | static const char kZygoteClassName[] = "com/android/internal/os/Zygote"; |
| 71 | static jclass gZygoteClass; |
| 72 | static jmethodID gCallPostForkChildHooks; |
| 73 | |
| 74 | // Must match values in com.android.internal.os.Zygote. |
| 75 | enum MountExternalKind { |
| 76 | MOUNT_EXTERNAL_NONE = 0, |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 77 | MOUNT_EXTERNAL_DEFAULT = 1, |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 78 | MOUNT_EXTERNAL_READ = 2, |
| 79 | MOUNT_EXTERNAL_WRITE = 3, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 82 | static void RuntimeAbort(JNIEnv* env, int line, const char* msg) { |
| 83 | std::ostringstream oss; |
| 84 | oss << __FILE__ << ":" << line << ": " << msg; |
| 85 | env->FatalError(oss.str().c_str()); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // This signal handler is for zygote mode, since the zygote must reap its children |
| 89 | static void SigChldHandler(int /*signal_number*/) { |
| 90 | pid_t pid; |
| 91 | int status; |
| 92 | |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame] | 93 | // It's necessary to save and restore the errno during this function. |
| 94 | // Since errno is stored per thread, changing it here modifies the errno |
| 95 | // on the thread on which this signal handler executes. If a signal occurs |
| 96 | // between a call and an errno check, it's possible to get the errno set |
| 97 | // here. |
| 98 | // See b/23572286 for extra information. |
| 99 | int saved_errno = errno; |
| 100 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 101 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 102 | // Log process-death status that we care about. In general it is |
| 103 | // not safe to call LOG(...) from a signal handler because of |
| 104 | // possible reentrancy. However, we know a priori that the |
| 105 | // current implementation of LOG() is safe to call from a SIGCHLD |
| 106 | // handler in the zygote process. If the LOG() implementation |
| 107 | // changes its locking strategy or its use of syscalls within the |
| 108 | // lazy-init critical section, its use here may become unsafe. |
| 109 | if (WIFEXITED(status)) { |
| 110 | if (WEXITSTATUS(status)) { |
| 111 | ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 112 | } |
| 113 | } else if (WIFSIGNALED(status)) { |
| 114 | if (WTERMSIG(status) != SIGKILL) { |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 115 | ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 116 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 117 | if (WCOREDUMP(status)) { |
| 118 | ALOGI("Process %d dumped core.", pid); |
| 119 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // If the just-crashed process is the system_server, bring down zygote |
| 123 | // so that it is restarted by init and system server will be restarted |
| 124 | // from there. |
| 125 | if (pid == gSystemServerPid) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 126 | ALOGE("Exit zygote because system server (%d) has terminated", pid); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 127 | kill(getpid(), SIGKILL); |
| 128 | } |
| 129 | } |
| 130 | |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 131 | // Note that we shouldn't consider ECHILD an error because |
| 132 | // the secondary zygote might have no children left to wait for. |
| 133 | if (pid < 0 && errno != ECHILD) { |
| 134 | ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 135 | } |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame] | 136 | |
| 137 | errno = saved_errno; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Configures the SIGCHLD handler for the zygote process. This is configured |
| 141 | // very late, because earlier in the runtime we may fork() and exec() |
| 142 | // other processes, and we want to waitpid() for those rather than |
| 143 | // have them be harvested immediately. |
| 144 | // |
| 145 | // This ends up being called repeatedly before each fork(), but there's |
| 146 | // no real harm in that. |
| 147 | static void SetSigChldHandler() { |
| 148 | struct sigaction sa; |
| 149 | memset(&sa, 0, sizeof(sa)); |
| 150 | sa.sa_handler = SigChldHandler; |
| 151 | |
| 152 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 153 | if (err < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 154 | ALOGW("Error setting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Vitalii Tomkiv | 5cbce85 | 2016-05-18 17:43:02 -0700 | [diff] [blame] | 158 | // Resets nice priority for zygote process. Zygote priority can be set |
| 159 | // to high value during boot phase to speed it up. We want to ensure |
| 160 | // zygote is running at normal priority before childs are forked from it. |
| 161 | // |
| 162 | // This ends up being called repeatedly before each fork(), but there's |
| 163 | // no real harm in that. |
| 164 | static void ResetNicePriority(JNIEnv* env) { |
| 165 | errno = 0; |
| 166 | int prio = getpriority(PRIO_PROCESS, 0); |
| 167 | if (prio == -1 && errno != 0) { |
| 168 | ALOGW("getpriority failed: %s\n", strerror(errno)); |
| 169 | } |
| 170 | if (prio != 0 && setpriority(PRIO_PROCESS, 0, 0) != 0) { |
| 171 | ALOGE("setpriority(%d, 0, 0) failed: %s", PRIO_PROCESS, strerror(errno)); |
| 172 | RuntimeAbort(env, __LINE__, "setpriority failed"); |
| 173 | } |
| 174 | } |
| 175 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 176 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
| 177 | static void UnsetSigChldHandler() { |
| 178 | struct sigaction sa; |
| 179 | memset(&sa, 0, sizeof(sa)); |
| 180 | sa.sa_handler = SIG_DFL; |
| 181 | |
| 182 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 183 | if (err < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 184 | ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // Calls POSIX setgroups() using the int[] object as an argument. |
| 189 | // A NULL argument is tolerated. |
| 190 | static void SetGids(JNIEnv* env, jintArray javaGids) { |
| 191 | if (javaGids == NULL) { |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | ScopedIntArrayRO gids(env, javaGids); |
| 196 | if (gids.get() == NULL) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 197 | RuntimeAbort(env, __LINE__, "Getting gids int array failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 198 | } |
| 199 | int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); |
| 200 | if (rc == -1) { |
Narayan Kamath | 593aab7 | 2016-08-09 17:23:31 +0100 | [diff] [blame] | 201 | std::ostringstream oss; |
| 202 | oss << "setgroups failed: " << strerror(errno) << ", gids.size=" << gids.size(); |
| 203 | RuntimeAbort(env, __LINE__, oss.str().c_str()); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | // Sets the resource limits via setrlimit(2) for the values in the |
| 208 | // two-dimensional array of integers that's passed in. The second dimension |
| 209 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is |
| 210 | // treated as an empty array. |
| 211 | static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { |
| 212 | if (javaRlimits == NULL) { |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | rlimit rlim; |
| 217 | memset(&rlim, 0, sizeof(rlim)); |
| 218 | |
| 219 | for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { |
| 220 | ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); |
| 221 | ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); |
| 222 | if (javaRlimit.size() != 3) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 223 | RuntimeAbort(env, __LINE__, "rlimits array must have a second dimension of size 3"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | rlim.rlim_cur = javaRlimit[1]; |
| 227 | rlim.rlim_max = javaRlimit[2]; |
| 228 | |
| 229 | int rc = setrlimit(javaRlimit[0], &rlim); |
| 230 | if (rc == -1) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 231 | ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur, |
| 232 | rlim.rlim_max); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 233 | RuntimeAbort(env, __LINE__, "setrlimit failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 238 | // The debug malloc library needs to know whether it's the zygote or a child. |
| 239 | extern "C" int gMallocLeakZygoteChild; |
| 240 | |
| 241 | static void EnableKeepCapabilities(JNIEnv* env) { |
| 242 | int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 243 | if (rc == -1) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 244 | RuntimeAbort(env, __LINE__, "prctl(PR_SET_KEEPCAPS) failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | static void DropCapabilitiesBoundingSet(JNIEnv* env) { |
| 249 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
Josh Gao | 5997221 | 2017-01-25 11:45:58 -0800 | [diff] [blame] | 250 | // Keep CAP_SYS_PTRACE in our bounding set so crash_dump can gain it. |
| 251 | if (i == CAP_SYS_PTRACE) { |
| 252 | continue; |
| 253 | } |
| 254 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 255 | int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 256 | if (rc == -1) { |
| 257 | if (errno == EINVAL) { |
| 258 | ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " |
| 259 | "your kernel is compiled with file capabilities support"); |
| 260 | } else { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 261 | RuntimeAbort(env, __LINE__, "prctl(PR_CAPBSET_DROP) failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) { |
| 268 | __user_cap_header_struct capheader; |
| 269 | memset(&capheader, 0, sizeof(capheader)); |
| 270 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 271 | capheader.pid = 0; |
| 272 | |
| 273 | __user_cap_data_struct capdata[2]; |
| 274 | memset(&capdata, 0, sizeof(capdata)); |
| 275 | capdata[0].effective = effective; |
| 276 | capdata[1].effective = effective >> 32; |
| 277 | capdata[0].permitted = permitted; |
| 278 | capdata[1].permitted = permitted >> 32; |
| 279 | |
| 280 | if (capset(&capheader, &capdata[0]) == -1) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 281 | ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 282 | RuntimeAbort(env, __LINE__, "capset failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | static void SetSchedulerPolicy(JNIEnv* env) { |
| 287 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 288 | if (errno != 0) { |
| 289 | ALOGE("set_sched_policy(0, SP_DEFAULT) failed"); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 290 | RuntimeAbort(env, __LINE__, "set_sched_policy(0, SP_DEFAULT) failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 294 | static int UnmountTree(const char* path) { |
| 295 | size_t path_len = strlen(path); |
| 296 | |
| 297 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 298 | if (fp == NULL) { |
| 299 | ALOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 300 | return -errno; |
| 301 | } |
| 302 | |
| 303 | // Some volumes can be stacked on each other, so force unmount in |
| 304 | // reverse order to give us the best chance of success. |
| 305 | std::list<std::string> toUnmount; |
| 306 | mntent* mentry; |
| 307 | while ((mentry = getmntent(fp)) != NULL) { |
| 308 | if (strncmp(mentry->mnt_dir, path, path_len) == 0) { |
| 309 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 310 | } |
| 311 | } |
| 312 | endmntent(fp); |
| 313 | |
| 314 | for (auto path : toUnmount) { |
| 315 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 316 | ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno)); |
| 317 | } |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 322 | // Create a private mount namespace and bind mount appropriate emulated |
| 323 | // storage for the given user. |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 324 | static bool MountEmulatedStorage(uid_t uid, jint mount_mode, |
| 325 | bool force_mount_namespace) { |
| 326 | // See storage config details at http://source.android.com/tech/storage/ |
| 327 | |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 328 | String8 storageSource; |
| 329 | if (mount_mode == MOUNT_EXTERNAL_DEFAULT) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 330 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 331 | } else if (mount_mode == MOUNT_EXTERNAL_READ) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 332 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 333 | } else if (mount_mode == MOUNT_EXTERNAL_WRITE) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 334 | storageSource = "/mnt/runtime/write"; |
Robert Sesek | 06af1c0 | 2016-11-10 21:50:04 -0500 | [diff] [blame] | 335 | } else if (!force_mount_namespace) { |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 336 | // Sane default of no storage visible |
| 337 | return true; |
| 338 | } |
Robert Sesek | 8a3a6ff | 2016-10-31 11:25:10 -0400 | [diff] [blame] | 339 | |
| 340 | // Create a second private mount namespace for our process |
| 341 | if (unshare(CLONE_NEWNS) == -1) { |
| 342 | ALOGW("Failed to unshare(): %s", strerror(errno)); |
| 343 | return false; |
| 344 | } |
| 345 | |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 346 | if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage", |
| 347 | NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 348 | ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno)); |
| 349 | return false; |
| 350 | } |
| 351 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 352 | // Mount user-specific symlink helper into place |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 353 | userid_t user_id = multiuser_get_user_id(uid); |
| 354 | const String8 userSource(String8::format("/mnt/user/%d", user_id)); |
| 355 | if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) { |
| 356 | return false; |
| 357 | } |
| 358 | if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self", |
| 359 | NULL, MS_BIND, NULL)) == -1) { |
| 360 | ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno)); |
| 361 | return false; |
| 362 | } |
| 363 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 364 | return true; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 367 | static bool NeedsNoRandomizeWorkaround() { |
| 368 | #if !defined(__arm__) |
| 369 | return false; |
| 370 | #else |
| 371 | int major; |
| 372 | int minor; |
| 373 | struct utsname uts; |
| 374 | if (uname(&uts) == -1) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | // Kernels before 3.4.* need the workaround. |
| 383 | return (major < 3) || ((major == 3) && (minor < 4)); |
| 384 | #endif |
| 385 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 386 | |
| 387 | // Utility to close down the Zygote socket file descriptors while |
| 388 | // the child is still running as root with Zygote's privileges. Each |
| 389 | // descriptor (if any) is closed via dup2(), replacing it with a valid |
| 390 | // (open) descriptor to /dev/null. |
| 391 | |
| 392 | static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) { |
| 393 | if (!fdsToClose) { |
| 394 | return; |
| 395 | } |
| 396 | jsize count = env->GetArrayLength(fdsToClose); |
Mykola Kondratenko | 1ca062f | 2015-07-31 17:22:26 +0200 | [diff] [blame] | 397 | ScopedIntArrayRO ar(env, fdsToClose); |
| 398 | if (ar.get() == NULL) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 399 | RuntimeAbort(env, __LINE__, "Bad fd array"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 400 | } |
| 401 | jsize i; |
| 402 | int devnull; |
| 403 | for (i = 0; i < count; i++) { |
| 404 | devnull = open("/dev/null", O_RDWR); |
| 405 | if (devnull < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 406 | ALOGE("Failed to open /dev/null: %s", strerror(errno)); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 407 | RuntimeAbort(env, __LINE__, "Failed to open /dev/null"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 408 | continue; |
| 409 | } |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 410 | ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 411 | if (dup2(devnull, ar[i]) < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 412 | ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno)); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 413 | RuntimeAbort(env, __LINE__, "Failed dup2()"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 414 | } |
| 415 | close(devnull); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void SetThreadName(const char* thread_name) { |
| 420 | bool hasAt = false; |
| 421 | bool hasDot = false; |
| 422 | const char* s = thread_name; |
| 423 | while (*s) { |
| 424 | if (*s == '.') { |
| 425 | hasDot = true; |
| 426 | } else if (*s == '@') { |
| 427 | hasAt = true; |
| 428 | } |
| 429 | s++; |
| 430 | } |
| 431 | const int len = s - thread_name; |
| 432 | if (len < 15 || hasAt || !hasDot) { |
| 433 | s = thread_name; |
| 434 | } else { |
| 435 | s = thread_name + len - 15; |
| 436 | } |
| 437 | // pthread_setname_np fails rather than truncating long strings. |
| 438 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 439 | strlcpy(buf, s, sizeof(buf)-1); |
| 440 | errno = pthread_setname_np(pthread_self(), buf); |
| 441 | if (errno != 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 442 | ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 446 | // The list of open zygote file descriptors. |
| 447 | static FileDescriptorTable* gOpenFdTable = NULL; |
| 448 | |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 449 | static void FillFileDescriptorVector(JNIEnv* env, |
| 450 | jintArray java_fds, |
| 451 | std::vector<int>* fds) { |
| 452 | CHECK(fds != nullptr); |
| 453 | if (java_fds != nullptr) { |
| 454 | ScopedIntArrayRO ar(env, java_fds); |
| 455 | if (ar.get() == nullptr) { |
| 456 | RuntimeAbort(env, __LINE__, "Bad fd array"); |
| 457 | } |
| 458 | fds->reserve(ar.size()); |
| 459 | for (size_t i = 0; i < ar.size(); ++i) { |
| 460 | fds->push_back(ar[i]); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 465 | // Utility routine to fork zygote and specialize the child process. |
| 466 | static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, |
| 467 | jint debug_flags, jobjectArray javaRlimits, |
| 468 | jlong permittedCapabilities, jlong effectiveCapabilities, |
| 469 | jint mount_external, |
| 470 | jstring java_se_info, jstring java_se_name, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 471 | bool is_system_server, jintArray fdsToClose, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 472 | jintArray fdsToIgnore, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 473 | jstring instructionSet, jstring dataDir) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 474 | SetSigChldHandler(); |
| 475 | |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 476 | sigset_t sigchld; |
| 477 | sigemptyset(&sigchld); |
| 478 | sigaddset(&sigchld, SIGCHLD); |
| 479 | |
| 480 | // Temporarily block SIGCHLD during forks. The SIGCHLD handler might |
| 481 | // log, which would result in the logging FDs we close being reopened. |
| 482 | // This would cause failures because the FDs are not whitelisted. |
| 483 | // |
| 484 | // Note that the zygote process is single threaded at this point. |
| 485 | if (sigprocmask(SIG_BLOCK, &sigchld, nullptr) == -1) { |
| 486 | ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); |
| 487 | RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_BLOCK, { SIGCHLD }) failed."); |
| 488 | } |
| 489 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 490 | // Close any logging related FDs before we start evaluating the list of |
| 491 | // file descriptors. |
| 492 | __android_log_close(); |
| 493 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 494 | // If this is the first fork for this zygote, create the open FD table. |
| 495 | // If it isn't, we just need to check whether the list of open files has |
| 496 | // changed (and it shouldn't in the normal case). |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 497 | std::vector<int> fds_to_ignore; |
| 498 | FillFileDescriptorVector(env, fdsToIgnore, &fds_to_ignore); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 499 | if (gOpenFdTable == NULL) { |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 500 | gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 501 | if (gOpenFdTable == NULL) { |
| 502 | RuntimeAbort(env, __LINE__, "Unable to construct file descriptor table."); |
| 503 | } |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 504 | } else if (!gOpenFdTable->Restat(fds_to_ignore)) { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 505 | RuntimeAbort(env, __LINE__, "Unable to restat file descriptor table."); |
| 506 | } |
| 507 | |
Vitalii Tomkiv | 5cbce85 | 2016-05-18 17:43:02 -0700 | [diff] [blame] | 508 | ResetNicePriority(env); |
| 509 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 510 | pid_t pid = fork(); |
| 511 | |
| 512 | if (pid == 0) { |
| 513 | // The child process. |
| 514 | gMallocLeakZygoteChild = 1; |
| 515 | |
| 516 | // Clean up any descriptors which must be closed immediately |
| 517 | DetachDescriptors(env, fdsToClose); |
| 518 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 519 | // Re-open all remaining open file descriptors so that they aren't shared |
| 520 | // with the zygote across a fork. |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 521 | if (!gOpenFdTable->ReopenOrDetach()) { |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 522 | RuntimeAbort(env, __LINE__, "Unable to reopen whitelisted descriptors."); |
| 523 | } |
| 524 | |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 525 | if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) { |
| 526 | ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); |
| 527 | RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed."); |
| 528 | } |
| 529 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 530 | // Keep capabilities across UID change, unless we're staying root. |
| 531 | if (uid != 0) { |
| 532 | EnableKeepCapabilities(env); |
| 533 | } |
| 534 | |
| 535 | DropCapabilitiesBoundingSet(env); |
| 536 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 537 | bool use_native_bridge = !is_system_server && (instructionSet != NULL) |
| 538 | && android::NativeBridgeAvailable(); |
| 539 | if (use_native_bridge) { |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 540 | ScopedUtfChars isa_string(env, instructionSet); |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 541 | use_native_bridge = android::NeedsNativeBridge(isa_string.c_str()); |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 542 | } |
Calin Juravle | 6a4d236 | 2014-10-28 12:16:21 +0000 | [diff] [blame] | 543 | if (use_native_bridge && dataDir == NULL) { |
| 544 | // dataDir should never be null if we need to use a native bridge. |
| 545 | // In general, dataDir will never be null for normal applications. It can only happen in |
| 546 | // special cases (for isolated processes which are not associated with any app). These are |
| 547 | // launched by the framework and should not be emulated anyway. |
| 548 | use_native_bridge = false; |
| 549 | ALOGW("Native bridge will not be used because dataDir == NULL."); |
| 550 | } |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 551 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 552 | if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 553 | ALOGW("Failed to mount emulated storage: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 554 | if (errno == ENOTCONN || errno == EROFS) { |
| 555 | // When device is actively encrypting, we get ENOTCONN here |
| 556 | // since FUSE was mounted before the framework restarted. |
| 557 | // When encrypted device is booting, we get EROFS since |
| 558 | // FUSE hasn't been created yet by init. |
| 559 | // In either case, continue without external storage. |
| 560 | } else { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 561 | RuntimeAbort(env, __LINE__, "Cannot continue without emulated storage"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 565 | if (!is_system_server) { |
| 566 | int rc = createProcessGroup(uid, getpid()); |
| 567 | if (rc != 0) { |
Colin Cross | 3089bed | 2014-07-14 15:07:04 -0700 | [diff] [blame] | 568 | if (rc == -EROFS) { |
| 569 | ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?"); |
| 570 | } else { |
| 571 | ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc)); |
| 572 | } |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
| 575 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 576 | SetGids(env, javaGids); |
| 577 | |
| 578 | SetRLimits(env, javaRlimits); |
| 579 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 580 | if (use_native_bridge) { |
| 581 | ScopedUtfChars isa_string(env, instructionSet); |
| 582 | ScopedUtfChars data_dir(env, dataDir); |
| 583 | android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str()); |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 584 | } |
| 585 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 586 | int rc = setresgid(gid, gid, gid); |
| 587 | if (rc == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 588 | ALOGE("setresgid(%d) failed: %s", gid, strerror(errno)); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 589 | RuntimeAbort(env, __LINE__, "setresgid failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | rc = setresuid(uid, uid, uid); |
| 593 | if (rc == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 594 | ALOGE("setresuid(%d) failed: %s", uid, strerror(errno)); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 595 | RuntimeAbort(env, __LINE__, "setresuid failed"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 598 | if (NeedsNoRandomizeWorkaround()) { |
| 599 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 600 | int old_personality = personality(0xffffffff); |
| 601 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 602 | if (new_personality == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 603 | ALOGW("personality(%d) failed: %s", new_personality, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 604 | } |
| 605 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 606 | |
| 607 | SetCapabilities(env, permittedCapabilities, effectiveCapabilities); |
| 608 | |
| 609 | SetSchedulerPolicy(env); |
| 610 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 611 | const char* se_info_c_str = NULL; |
| 612 | ScopedUtfChars* se_info = NULL; |
| 613 | if (java_se_info != NULL) { |
| 614 | se_info = new ScopedUtfChars(env, java_se_info); |
| 615 | se_info_c_str = se_info->c_str(); |
| 616 | if (se_info_c_str == NULL) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 617 | RuntimeAbort(env, __LINE__, "se_info_c_str == NULL"); |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 618 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 619 | } |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 620 | const char* se_name_c_str = NULL; |
| 621 | ScopedUtfChars* se_name = NULL; |
| 622 | if (java_se_name != NULL) { |
| 623 | se_name = new ScopedUtfChars(env, java_se_name); |
| 624 | se_name_c_str = se_name->c_str(); |
| 625 | if (se_name_c_str == NULL) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 626 | RuntimeAbort(env, __LINE__, "se_name_c_str == NULL"); |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str); |
| 630 | if (rc == -1) { |
| 631 | ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid, |
| 632 | is_system_server, se_info_c_str, se_name_c_str); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 633 | RuntimeAbort(env, __LINE__, "selinux_android_setcontext failed"); |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // Make it easier to debug audit logs by setting the main thread's name to the |
| 637 | // nice name rather than "app_process". |
| 638 | if (se_info_c_str == NULL && is_system_server) { |
| 639 | se_name_c_str = "system_server"; |
| 640 | } |
| 641 | if (se_info_c_str != NULL) { |
| 642 | SetThreadName(se_name_c_str); |
| 643 | } |
| 644 | |
| 645 | delete se_info; |
| 646 | delete se_name; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 647 | |
| 648 | UnsetSigChldHandler(); |
| 649 | |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 650 | env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, |
Nicolas Geoffray | a877235 | 2015-12-11 15:01:04 +0000 | [diff] [blame] | 651 | is_system_server, instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 652 | if (env->ExceptionCheck()) { |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 653 | RuntimeAbort(env, __LINE__, "Error calling post fork hooks."); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 654 | } |
| 655 | } else if (pid > 0) { |
| 656 | // the parent process |
Tim Murray | 6d43a861 | 2015-08-05 14:31:05 -0700 | [diff] [blame] | 657 | |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 658 | // We blocked SIGCHLD prior to a fork, we unblock it here. |
| 659 | if (sigprocmask(SIG_UNBLOCK, &sigchld, nullptr) == -1) { |
| 660 | ALOGE("sigprocmask(SIG_SETMASK, { SIGCHLD }) failed: %s", strerror(errno)); |
| 661 | RuntimeAbort(env, __LINE__, "Call to sigprocmask(SIG_UNBLOCK, { SIGCHLD }) failed."); |
| 662 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 663 | } |
| 664 | return pid; |
| 665 | } |
| 666 | } // anonymous namespace |
| 667 | |
| 668 | namespace android { |
| 669 | |
| 670 | static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( |
| 671 | JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
| 672 | jint debug_flags, jobjectArray rlimits, |
| 673 | jint mount_external, jstring se_info, jstring se_name, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 674 | jintArray fdsToClose, |
| 675 | jintArray fdsToIgnore, |
| 676 | jstring instructionSet, jstring appDataDir) { |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 677 | jlong capabilities = 0; |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 678 | |
| 679 | // Grant CAP_WAKE_ALARM to the Bluetooth process. |
Erik Kline | 0f6ae2e | 2016-02-22 15:42:07 +0900 | [diff] [blame] | 680 | // Additionally, allow bluetooth to open packet sockets so it can start the DHCP client. |
| 681 | // TODO: consider making such functionality an RPC to netd. |
Pavlin Radoslavov | 2956bee | 2016-01-27 16:22:15 -0800 | [diff] [blame] | 682 | if (multiuser_get_app_id(uid) == AID_BLUETOOTH) { |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 683 | capabilities |= (1LL << CAP_WAKE_ALARM); |
Erik Kline | 0f6ae2e | 2016-02-22 15:42:07 +0900 | [diff] [blame] | 684 | capabilities |= (1LL << CAP_NET_RAW); |
| 685 | capabilities |= (1LL << CAP_NET_BIND_SERVICE); |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 686 | } |
Sharvil Nanavati | babe815 | 2015-08-31 23:25:06 -0700 | [diff] [blame] | 687 | |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 688 | // Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock" |
| 689 | bool gid_wakelock_found = false; |
| 690 | if (gid == AID_WAKELOCK) { |
| 691 | gid_wakelock_found = true; |
| 692 | } else if (gids != NULL) { |
| 693 | jsize gids_num = env->GetArrayLength(gids); |
| 694 | ScopedIntArrayRO ar(env, gids); |
| 695 | if (ar.get() == NULL) { |
| 696 | RuntimeAbort(env, __LINE__, "Bad gids array"); |
| 697 | } |
| 698 | for (int i = 0; i < gids_num; i++) { |
| 699 | if (ar[i] == AID_WAKELOCK) { |
| 700 | gid_wakelock_found = true; |
| 701 | break; |
Sharvil Nanavati | babe815 | 2015-08-31 23:25:06 -0700 | [diff] [blame] | 702 | } |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | if (gid_wakelock_found) { |
| 706 | capabilities |= (1LL << CAP_BLOCK_SUSPEND); |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 709 | return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 710 | rlimits, capabilities, capabilities, mount_external, se_info, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 711 | se_name, false, fdsToClose, fdsToIgnore, instructionSet, appDataDir); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | static jint com_android_internal_os_Zygote_nativeForkSystemServer( |
| 715 | JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
| 716 | jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities, |
| 717 | jlong effectiveCapabilities) { |
| 718 | pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, |
| 719 | debug_flags, rlimits, |
| 720 | permittedCapabilities, effectiveCapabilities, |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 721 | MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL, |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 722 | NULL, NULL, NULL); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 723 | if (pid > 0) { |
| 724 | // The zygote process checks whether the child process has died or not. |
| 725 | ALOGI("System server process %d has been created", pid); |
| 726 | gSystemServerPid = pid; |
| 727 | // There is a slight window that the system server process has crashed |
| 728 | // but it went unnoticed because we haven't published its pid yet. So |
| 729 | // we recheck here just to make sure that all is well. |
| 730 | int status; |
| 731 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 732 | ALOGE("System server process %d has died. Restarting Zygote!", pid); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 733 | RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | return pid; |
| 737 | } |
| 738 | |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 739 | static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork( |
| 740 | JNIEnv* env, jclass, jstring path) { |
| 741 | ScopedUtfChars path_native(env, path); |
| 742 | const char* path_cstr = path_native.c_str(); |
| 743 | if (!path_cstr) { |
| 744 | RuntimeAbort(env, __LINE__, "path_cstr == NULL"); |
| 745 | } |
| 746 | FileDescriptorWhitelist::Get()->Allow(path_cstr); |
| 747 | } |
| 748 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 749 | static void com_android_internal_os_Zygote_nativeUnmountStorageOnInit(JNIEnv* env, jclass) { |
| 750 | // Zygote process unmount root storage space initially before every child processes are forked. |
| 751 | // Every forked child processes (include SystemServer) only mount their own root storage space |
Robert Sesek | e4f8d69 | 2016-09-13 19:13:01 -0400 | [diff] [blame] | 752 | // and no need unmount storage operation in MountEmulatedStorage method. |
| 753 | // Zygote process does not utilize root storage spaces and unshares its mount namespace below. |
| 754 | |
| 755 | // See storage config details at http://source.android.com/tech/storage/ |
| 756 | // Create private mount namespace shared by all children |
| 757 | if (unshare(CLONE_NEWNS) == -1) { |
| 758 | RuntimeAbort(env, __LINE__, "Failed to unshare()"); |
| 759 | return; |
| 760 | } |
| 761 | |
| 762 | // Mark rootfs as being a slave so that changes from default |
| 763 | // namespace only flow into our children. |
| 764 | if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) { |
| 765 | RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE"); |
| 766 | return; |
| 767 | } |
| 768 | |
| 769 | // Create a staging tmpfs that is shared by our children; they will |
| 770 | // bind mount storage into their respective private namespaces, which |
| 771 | // are isolated from each other. |
| 772 | const char* target_base = getenv("EMULATED_STORAGE_TARGET"); |
| 773 | if (target_base != nullptr) { |
| 774 | #define STRINGIFY_UID(x) __STRING(x) |
| 775 | if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV, |
| 776 | "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) { |
| 777 | ALOGE("Failed to mount tmpfs to %s", target_base); |
| 778 | RuntimeAbort(env, __LINE__, "Failed to mount tmpfs"); |
| 779 | return; |
| 780 | } |
| 781 | #undef STRINGIFY_UID |
| 782 | } |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 783 | |
| 784 | UnmountTree("/storage"); |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 785 | } |
| 786 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 787 | static const JNINativeMethod gMethods[] = { |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 788 | { "nativeForkAndSpecialize", |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 789 | "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;)I", |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 790 | (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, |
| 791 | { "nativeForkSystemServer", "(II[II[[IJJ)I", |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 792 | (void *) com_android_internal_os_Zygote_nativeForkSystemServer }, |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 793 | { "nativeAllowFileAcrossFork", "(Ljava/lang/String;)V", |
| 794 | (void *) com_android_internal_os_Zygote_nativeAllowFileAcrossFork }, |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 795 | { "nativeUnmountStorageOnInit", "()V", |
| 796 | (void *) com_android_internal_os_Zygote_nativeUnmountStorageOnInit } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 797 | }; |
| 798 | |
| 799 | int register_com_android_internal_os_Zygote(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 800 | gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName)); |
| 801 | gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks", |
Nicolas Geoffray | a877235 | 2015-12-11 15:01:04 +0000 | [diff] [blame] | 802 | "(IZLjava/lang/String;)V"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 803 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 804 | return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 805 | } |
| 806 | } // namespace android |