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> |
| 24 | #include <string> |
| 25 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 26 | #include <fcntl.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 27 | #include <grp.h> |
| 28 | #include <inttypes.h> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 29 | #include <mntent.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 30 | #include <paths.h> |
| 31 | #include <signal.h> |
| 32 | #include <stdlib.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 33 | #include <sys/capability.h> |
| 34 | #include <sys/personality.h> |
| 35 | #include <sys/prctl.h> |
| 36 | #include <sys/resource.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <sys/utsname.h> |
| 40 | #include <sys/wait.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 42 | |
| 43 | #include <cutils/fs.h> |
| 44 | #include <cutils/multiuser.h> |
| 45 | #include <cutils/sched_policy.h> |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 47 | #include <utils/String8.h> |
| 48 | #include <selinux/android.h> |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 49 | #include <processgroup/processgroup.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 50 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 51 | #include "core_jni_helpers.h" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 52 | #include "JNIHelp.h" |
| 53 | #include "ScopedLocalRef.h" |
| 54 | #include "ScopedPrimitiveArray.h" |
| 55 | #include "ScopedUtfChars.h" |
| 56 | |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 57 | #include "nativebridge/native_bridge.h" |
| 58 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 59 | namespace { |
| 60 | |
| 61 | using android::String8; |
| 62 | |
| 63 | static pid_t gSystemServerPid = 0; |
| 64 | |
| 65 | static const char kZygoteClassName[] = "com/android/internal/os/Zygote"; |
| 66 | static jclass gZygoteClass; |
| 67 | static jmethodID gCallPostForkChildHooks; |
| 68 | |
| 69 | // Must match values in com.android.internal.os.Zygote. |
| 70 | enum MountExternalKind { |
| 71 | MOUNT_EXTERNAL_NONE = 0, |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 72 | MOUNT_EXTERNAL_DEFAULT = 1, |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 73 | MOUNT_EXTERNAL_READ = 2, |
| 74 | MOUNT_EXTERNAL_WRITE = 3, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static void RuntimeAbort(JNIEnv* env) { |
| 78 | env->FatalError("RuntimeAbort"); |
| 79 | } |
| 80 | |
| 81 | // This signal handler is for zygote mode, since the zygote must reap its children |
| 82 | static void SigChldHandler(int /*signal_number*/) { |
| 83 | pid_t pid; |
| 84 | int status; |
| 85 | |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame^] | 86 | // It's necessary to save and restore the errno during this function. |
| 87 | // Since errno is stored per thread, changing it here modifies the errno |
| 88 | // on the thread on which this signal handler executes. If a signal occurs |
| 89 | // between a call and an errno check, it's possible to get the errno set |
| 90 | // here. |
| 91 | // See b/23572286 for extra information. |
| 92 | int saved_errno = errno; |
| 93 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 94 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 95 | // Log process-death status that we care about. In general it is |
| 96 | // not safe to call LOG(...) from a signal handler because of |
| 97 | // possible reentrancy. However, we know a priori that the |
| 98 | // current implementation of LOG() is safe to call from a SIGCHLD |
| 99 | // handler in the zygote process. If the LOG() implementation |
| 100 | // changes its locking strategy or its use of syscalls within the |
| 101 | // lazy-init critical section, its use here may become unsafe. |
| 102 | if (WIFEXITED(status)) { |
| 103 | if (WEXITSTATUS(status)) { |
| 104 | ALOGI("Process %d exited cleanly (%d)", pid, WEXITSTATUS(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 105 | } |
| 106 | } else if (WIFSIGNALED(status)) { |
| 107 | if (WTERMSIG(status) != SIGKILL) { |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 108 | ALOGI("Process %d exited due to signal (%d)", pid, WTERMSIG(status)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 109 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 110 | if (WCOREDUMP(status)) { |
| 111 | ALOGI("Process %d dumped core.", pid); |
| 112 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // If the just-crashed process is the system_server, bring down zygote |
| 116 | // so that it is restarted by init and system server will be restarted |
| 117 | // from there. |
| 118 | if (pid == gSystemServerPid) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 119 | ALOGE("Exit zygote because system server (%d) has terminated", pid); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 120 | kill(getpid(), SIGKILL); |
| 121 | } |
| 122 | } |
| 123 | |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 124 | // Note that we shouldn't consider ECHILD an error because |
| 125 | // the secondary zygote might have no children left to wait for. |
| 126 | if (pid < 0 && errno != ECHILD) { |
| 127 | ALOGW("Zygote SIGCHLD error in waitpid: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 128 | } |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame^] | 129 | |
| 130 | errno = saved_errno; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Configures the SIGCHLD handler for the zygote process. This is configured |
| 134 | // very late, because earlier in the runtime we may fork() and exec() |
| 135 | // other processes, and we want to waitpid() for those rather than |
| 136 | // have them be harvested immediately. |
| 137 | // |
| 138 | // This ends up being called repeatedly before each fork(), but there's |
| 139 | // no real harm in that. |
| 140 | static void SetSigChldHandler() { |
| 141 | struct sigaction sa; |
| 142 | memset(&sa, 0, sizeof(sa)); |
| 143 | sa.sa_handler = SigChldHandler; |
| 144 | |
| 145 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 146 | if (err < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 147 | ALOGW("Error setting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
| 151 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
| 152 | static void UnsetSigChldHandler() { |
| 153 | struct sigaction sa; |
| 154 | memset(&sa, 0, sizeof(sa)); |
| 155 | sa.sa_handler = SIG_DFL; |
| 156 | |
| 157 | int err = sigaction(SIGCHLD, &sa, NULL); |
| 158 | if (err < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 159 | ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // Calls POSIX setgroups() using the int[] object as an argument. |
| 164 | // A NULL argument is tolerated. |
| 165 | static void SetGids(JNIEnv* env, jintArray javaGids) { |
| 166 | if (javaGids == NULL) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | ScopedIntArrayRO gids(env, javaGids); |
| 171 | if (gids.get() == NULL) { |
| 172 | RuntimeAbort(env); |
| 173 | } |
| 174 | int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); |
| 175 | if (rc == -1) { |
| 176 | ALOGE("setgroups failed"); |
| 177 | RuntimeAbort(env); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Sets the resource limits via setrlimit(2) for the values in the |
| 182 | // two-dimensional array of integers that's passed in. The second dimension |
| 183 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is |
| 184 | // treated as an empty array. |
| 185 | static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { |
| 186 | if (javaRlimits == NULL) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | rlimit rlim; |
| 191 | memset(&rlim, 0, sizeof(rlim)); |
| 192 | |
| 193 | for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { |
| 194 | ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); |
| 195 | ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); |
| 196 | if (javaRlimit.size() != 3) { |
| 197 | ALOGE("rlimits array must have a second dimension of size 3"); |
| 198 | RuntimeAbort(env); |
| 199 | } |
| 200 | |
| 201 | rlim.rlim_cur = javaRlimit[1]; |
| 202 | rlim.rlim_max = javaRlimit[2]; |
| 203 | |
| 204 | int rc = setrlimit(javaRlimit[0], &rlim); |
| 205 | if (rc == -1) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 206 | ALOGE("setrlimit(%d, {%ld, %ld}) failed", javaRlimit[0], rlim.rlim_cur, |
| 207 | rlim.rlim_max); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 208 | RuntimeAbort(env); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 213 | // The debug malloc library needs to know whether it's the zygote or a child. |
| 214 | extern "C" int gMallocLeakZygoteChild; |
| 215 | |
| 216 | static void EnableKeepCapabilities(JNIEnv* env) { |
| 217 | int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 218 | if (rc == -1) { |
| 219 | ALOGE("prctl(PR_SET_KEEPCAPS) failed"); |
| 220 | RuntimeAbort(env); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | static void DropCapabilitiesBoundingSet(JNIEnv* env) { |
| 225 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
| 226 | int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 227 | if (rc == -1) { |
| 228 | if (errno == EINVAL) { |
| 229 | ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " |
| 230 | "your kernel is compiled with file capabilities support"); |
| 231 | } else { |
| 232 | ALOGE("prctl(PR_CAPBSET_DROP) failed"); |
| 233 | RuntimeAbort(env); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | static void SetCapabilities(JNIEnv* env, int64_t permitted, int64_t effective) { |
| 240 | __user_cap_header_struct capheader; |
| 241 | memset(&capheader, 0, sizeof(capheader)); |
| 242 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 243 | capheader.pid = 0; |
| 244 | |
| 245 | __user_cap_data_struct capdata[2]; |
| 246 | memset(&capdata, 0, sizeof(capdata)); |
| 247 | capdata[0].effective = effective; |
| 248 | capdata[1].effective = effective >> 32; |
| 249 | capdata[0].permitted = permitted; |
| 250 | capdata[1].permitted = permitted >> 32; |
| 251 | |
| 252 | if (capset(&capheader, &capdata[0]) == -1) { |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 253 | ALOGE("capset(%" PRId64 ", %" PRId64 ") failed", permitted, effective); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 254 | RuntimeAbort(env); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void SetSchedulerPolicy(JNIEnv* env) { |
| 259 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 260 | if (errno != 0) { |
| 261 | ALOGE("set_sched_policy(0, SP_DEFAULT) failed"); |
| 262 | RuntimeAbort(env); |
| 263 | } |
| 264 | } |
| 265 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 266 | static int UnmountTree(const char* path) { |
| 267 | size_t path_len = strlen(path); |
| 268 | |
| 269 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 270 | if (fp == NULL) { |
| 271 | ALOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 272 | return -errno; |
| 273 | } |
| 274 | |
| 275 | // Some volumes can be stacked on each other, so force unmount in |
| 276 | // reverse order to give us the best chance of success. |
| 277 | std::list<std::string> toUnmount; |
| 278 | mntent* mentry; |
| 279 | while ((mentry = getmntent(fp)) != NULL) { |
| 280 | if (strncmp(mentry->mnt_dir, path, path_len) == 0) { |
| 281 | toUnmount.push_front(std::string(mentry->mnt_dir)); |
| 282 | } |
| 283 | } |
| 284 | endmntent(fp); |
| 285 | |
| 286 | for (auto path : toUnmount) { |
| 287 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 288 | ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno)); |
| 289 | } |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 294 | // Create a private mount namespace and bind mount appropriate emulated |
| 295 | // storage for the given user. |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 296 | static bool MountEmulatedStorage(uid_t uid, jint mount_mode, |
| 297 | bool force_mount_namespace) { |
| 298 | // See storage config details at http://source.android.com/tech/storage/ |
| 299 | |
| 300 | // Create a second private mount namespace for our process |
| 301 | if (unshare(CLONE_NEWNS) == -1) { |
| 302 | ALOGW("Failed to unshare(): %s", strerror(errno)); |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | // Unmount storage provided by root namespace and mount requested view |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 307 | UnmountTree("/storage"); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 308 | |
| 309 | String8 storageSource; |
| 310 | if (mount_mode == MOUNT_EXTERNAL_DEFAULT) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 311 | storageSource = "/mnt/runtime/default"; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 312 | } else if (mount_mode == MOUNT_EXTERNAL_READ) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 313 | storageSource = "/mnt/runtime/read"; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 314 | } else if (mount_mode == MOUNT_EXTERNAL_WRITE) { |
Jeff Sharkey | 928e1ec | 2015-08-06 11:40:21 -0700 | [diff] [blame] | 315 | storageSource = "/mnt/runtime/write"; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 316 | } else { |
| 317 | // Sane default of no storage visible |
| 318 | return true; |
| 319 | } |
| 320 | if (TEMP_FAILURE_RETRY(mount(storageSource.string(), "/storage", |
| 321 | NULL, MS_BIND | MS_REC | MS_SLAVE, NULL)) == -1) { |
| 322 | ALOGW("Failed to mount %s to /storage: %s", storageSource.string(), strerror(errno)); |
| 323 | return false; |
| 324 | } |
| 325 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 326 | // Mount user-specific symlink helper into place |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 327 | userid_t user_id = multiuser_get_user_id(uid); |
| 328 | const String8 userSource(String8::format("/mnt/user/%d", user_id)); |
| 329 | if (fs_prepare_dir(userSource.string(), 0751, 0, 0) == -1) { |
| 330 | return false; |
| 331 | } |
| 332 | if (TEMP_FAILURE_RETRY(mount(userSource.string(), "/storage/self", |
| 333 | NULL, MS_BIND, NULL)) == -1) { |
| 334 | ALOGW("Failed to mount %s to /storage/self: %s", userSource.string(), strerror(errno)); |
| 335 | return false; |
| 336 | } |
| 337 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 338 | return true; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 341 | static 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 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 360 | |
| 361 | // Utility to close down the Zygote socket file descriptors while |
| 362 | // the child is still running as root with Zygote's privileges. Each |
| 363 | // descriptor (if any) is closed via dup2(), replacing it with a valid |
| 364 | // (open) descriptor to /dev/null. |
| 365 | |
| 366 | static void DetachDescriptors(JNIEnv* env, jintArray fdsToClose) { |
| 367 | if (!fdsToClose) { |
| 368 | return; |
| 369 | } |
| 370 | jsize count = env->GetArrayLength(fdsToClose); |
| 371 | jint *ar = env->GetIntArrayElements(fdsToClose, 0); |
| 372 | if (!ar) { |
| 373 | ALOGE("Bad fd array"); |
| 374 | RuntimeAbort(env); |
| 375 | } |
| 376 | jsize i; |
| 377 | int devnull; |
| 378 | for (i = 0; i < count; i++) { |
| 379 | devnull = open("/dev/null", O_RDWR); |
| 380 | if (devnull < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 381 | ALOGE("Failed to open /dev/null: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 382 | RuntimeAbort(env); |
| 383 | continue; |
| 384 | } |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 385 | ALOGV("Switching descriptor %d to /dev/null: %s", ar[i], strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 386 | if (dup2(devnull, ar[i]) < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 387 | ALOGE("Failed dup2() on descriptor %d: %s", ar[i], strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 388 | RuntimeAbort(env); |
| 389 | } |
| 390 | close(devnull); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void SetThreadName(const char* thread_name) { |
| 395 | bool hasAt = false; |
| 396 | bool hasDot = false; |
| 397 | const char* s = thread_name; |
| 398 | while (*s) { |
| 399 | if (*s == '.') { |
| 400 | hasDot = true; |
| 401 | } else if (*s == '@') { |
| 402 | hasAt = true; |
| 403 | } |
| 404 | s++; |
| 405 | } |
| 406 | const int len = s - thread_name; |
| 407 | if (len < 15 || hasAt || !hasDot) { |
| 408 | s = thread_name; |
| 409 | } else { |
| 410 | s = thread_name + len - 15; |
| 411 | } |
| 412 | // pthread_setname_np fails rather than truncating long strings. |
| 413 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
| 414 | strlcpy(buf, s, sizeof(buf)-1); |
| 415 | errno = pthread_setname_np(pthread_self(), buf); |
| 416 | if (errno != 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 417 | 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] | 418 | } |
| 419 | } |
| 420 | |
Tim Murray | 6d43a861 | 2015-08-05 14:31:05 -0700 | [diff] [blame] | 421 | #ifdef ENABLE_SCHED_BOOST |
| 422 | static void SetForkLoad(bool boost) { |
| 423 | // set scheduler knob to boost forked processes |
| 424 | pid_t currentPid = getpid(); |
| 425 | // fits at most "/proc/XXXXXXX/sched_init_task_load\0" |
| 426 | char schedPath[35]; |
| 427 | snprintf(schedPath, sizeof(schedPath), "/proc/%u/sched_init_task_load", currentPid); |
| 428 | int schedBoostFile = open(schedPath, O_WRONLY); |
| 429 | if (schedBoostFile < 0) { |
| 430 | ALOGW("Unable to set zygote scheduler boost"); |
| 431 | return; |
| 432 | } |
| 433 | if (boost) { |
| 434 | write(schedBoostFile, "100\0", 4); |
| 435 | } else { |
| 436 | write(schedBoostFile, "0\0", 2); |
| 437 | } |
| 438 | close(schedBoostFile); |
| 439 | } |
| 440 | #endif |
| 441 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 442 | // Utility routine to fork zygote and specialize the child process. |
| 443 | static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, |
| 444 | jint debug_flags, jobjectArray javaRlimits, |
| 445 | jlong permittedCapabilities, jlong effectiveCapabilities, |
| 446 | jint mount_external, |
| 447 | jstring java_se_info, jstring java_se_name, |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 448 | bool is_system_server, jintArray fdsToClose, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 449 | jstring instructionSet, jstring dataDir) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 450 | SetSigChldHandler(); |
| 451 | |
Tim Murray | 6d43a861 | 2015-08-05 14:31:05 -0700 | [diff] [blame] | 452 | #ifdef ENABLE_SCHED_BOOST |
| 453 | SetForkLoad(true); |
| 454 | #endif |
| 455 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 456 | pid_t pid = fork(); |
| 457 | |
| 458 | if (pid == 0) { |
| 459 | // The child process. |
| 460 | gMallocLeakZygoteChild = 1; |
| 461 | |
| 462 | // Clean up any descriptors which must be closed immediately |
| 463 | DetachDescriptors(env, fdsToClose); |
| 464 | |
| 465 | // Keep capabilities across UID change, unless we're staying root. |
| 466 | if (uid != 0) { |
| 467 | EnableKeepCapabilities(env); |
| 468 | } |
| 469 | |
| 470 | DropCapabilitiesBoundingSet(env); |
| 471 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 472 | bool use_native_bridge = !is_system_server && (instructionSet != NULL) |
| 473 | && android::NativeBridgeAvailable(); |
| 474 | if (use_native_bridge) { |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 475 | ScopedUtfChars isa_string(env, instructionSet); |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 476 | use_native_bridge = android::NeedsNativeBridge(isa_string.c_str()); |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 477 | } |
Calin Juravle | 6a4d236 | 2014-10-28 12:16:21 +0000 | [diff] [blame] | 478 | if (use_native_bridge && dataDir == NULL) { |
| 479 | // dataDir should never be null if we need to use a native bridge. |
| 480 | // In general, dataDir will never be null for normal applications. It can only happen in |
| 481 | // special cases (for isolated processes which are not associated with any app). These are |
| 482 | // launched by the framework and should not be emulated anyway. |
| 483 | use_native_bridge = false; |
| 484 | ALOGW("Native bridge will not be used because dataDir == NULL."); |
| 485 | } |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 486 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 487 | if (!MountEmulatedStorage(uid, mount_external, use_native_bridge)) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 488 | ALOGW("Failed to mount emulated storage: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 489 | if (errno == ENOTCONN || errno == EROFS) { |
| 490 | // When device is actively encrypting, we get ENOTCONN here |
| 491 | // since FUSE was mounted before the framework restarted. |
| 492 | // When encrypted device is booting, we get EROFS since |
| 493 | // FUSE hasn't been created yet by init. |
| 494 | // In either case, continue without external storage. |
| 495 | } else { |
| 496 | ALOGE("Cannot continue without emulated storage"); |
| 497 | RuntimeAbort(env); |
| 498 | } |
| 499 | } |
| 500 | |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 501 | if (!is_system_server) { |
| 502 | int rc = createProcessGroup(uid, getpid()); |
| 503 | if (rc != 0) { |
Colin Cross | 3089bed | 2014-07-14 15:07:04 -0700 | [diff] [blame] | 504 | if (rc == -EROFS) { |
| 505 | ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?"); |
| 506 | } else { |
| 507 | ALOGE("createProcessGroup(%d, %d) failed: %s", uid, pid, strerror(-rc)); |
| 508 | } |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 512 | SetGids(env, javaGids); |
| 513 | |
| 514 | SetRLimits(env, javaRlimits); |
| 515 | |
Calin Juravle | 79ec4c1 | 2014-10-24 16:16:49 +0100 | [diff] [blame] | 516 | if (use_native_bridge) { |
| 517 | ScopedUtfChars isa_string(env, instructionSet); |
| 518 | ScopedUtfChars data_dir(env, dataDir); |
| 519 | android::PreInitializeNativeBridge(data_dir.c_str(), isa_string.c_str()); |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 520 | } |
| 521 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 522 | int rc = setresgid(gid, gid, gid); |
| 523 | if (rc == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 524 | ALOGE("setresgid(%d) failed: %s", gid, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 525 | RuntimeAbort(env); |
| 526 | } |
| 527 | |
| 528 | rc = setresuid(uid, uid, uid); |
| 529 | if (rc == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 530 | ALOGE("setresuid(%d) failed: %s", uid, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 531 | RuntimeAbort(env); |
| 532 | } |
| 533 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 534 | if (NeedsNoRandomizeWorkaround()) { |
| 535 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 536 | int old_personality = personality(0xffffffff); |
| 537 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 538 | if (new_personality == -1) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 539 | ALOGW("personality(%d) failed: %s", new_personality, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 540 | } |
| 541 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 542 | |
| 543 | SetCapabilities(env, permittedCapabilities, effectiveCapabilities); |
| 544 | |
| 545 | SetSchedulerPolicy(env); |
| 546 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 547 | const char* se_info_c_str = NULL; |
| 548 | ScopedUtfChars* se_info = NULL; |
| 549 | if (java_se_info != NULL) { |
| 550 | se_info = new ScopedUtfChars(env, java_se_info); |
| 551 | se_info_c_str = se_info->c_str(); |
| 552 | if (se_info_c_str == NULL) { |
| 553 | ALOGE("se_info_c_str == NULL"); |
| 554 | RuntimeAbort(env); |
| 555 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 556 | } |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 557 | const char* se_name_c_str = NULL; |
| 558 | ScopedUtfChars* se_name = NULL; |
| 559 | if (java_se_name != NULL) { |
| 560 | se_name = new ScopedUtfChars(env, java_se_name); |
| 561 | se_name_c_str = se_name->c_str(); |
| 562 | if (se_name_c_str == NULL) { |
| 563 | ALOGE("se_name_c_str == NULL"); |
| 564 | RuntimeAbort(env); |
| 565 | } |
| 566 | } |
| 567 | rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str); |
| 568 | if (rc == -1) { |
| 569 | ALOGE("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid, |
| 570 | is_system_server, se_info_c_str, se_name_c_str); |
| 571 | RuntimeAbort(env); |
| 572 | } |
| 573 | |
| 574 | // Make it easier to debug audit logs by setting the main thread's name to the |
| 575 | // nice name rather than "app_process". |
| 576 | if (se_info_c_str == NULL && is_system_server) { |
| 577 | se_name_c_str = "system_server"; |
| 578 | } |
| 579 | if (se_info_c_str != NULL) { |
| 580 | SetThreadName(se_name_c_str); |
| 581 | } |
| 582 | |
| 583 | delete se_info; |
| 584 | delete se_name; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 585 | |
| 586 | UnsetSigChldHandler(); |
| 587 | |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 588 | env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, |
| 589 | is_system_server ? NULL : instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 590 | if (env->ExceptionCheck()) { |
| 591 | ALOGE("Error calling post fork hooks."); |
| 592 | RuntimeAbort(env); |
| 593 | } |
| 594 | } else if (pid > 0) { |
| 595 | // the parent process |
Tim Murray | 6d43a861 | 2015-08-05 14:31:05 -0700 | [diff] [blame] | 596 | |
| 597 | #ifdef ENABLE_SCHED_BOOST |
| 598 | // unset scheduler knob |
| 599 | SetForkLoad(false); |
| 600 | #endif |
| 601 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 602 | } |
| 603 | return pid; |
| 604 | } |
| 605 | } // anonymous namespace |
| 606 | |
| 607 | namespace android { |
| 608 | |
| 609 | static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( |
| 610 | JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
| 611 | jint debug_flags, jobjectArray rlimits, |
| 612 | jint mount_external, jstring se_info, jstring se_name, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 613 | jintArray fdsToClose, jstring instructionSet, jstring appDataDir) { |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 614 | // Grant CAP_WAKE_ALARM to the Bluetooth process. |
| 615 | jlong capabilities = 0; |
| 616 | if (uid == AID_BLUETOOTH) { |
| 617 | capabilities |= (1LL << CAP_WAKE_ALARM); |
| 618 | } |
| 619 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 620 | return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 621 | rlimits, capabilities, capabilities, mount_external, se_info, |
Andreas Gampe | a103ebe | 2014-09-24 15:37:53 -0700 | [diff] [blame] | 622 | se_name, false, fdsToClose, instructionSet, appDataDir); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | static jint com_android_internal_os_Zygote_nativeForkSystemServer( |
| 626 | JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
| 627 | jint debug_flags, jobjectArray rlimits, jlong permittedCapabilities, |
| 628 | jlong effectiveCapabilities) { |
| 629 | pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, |
| 630 | debug_flags, rlimits, |
| 631 | permittedCapabilities, effectiveCapabilities, |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 632 | MOUNT_EXTERNAL_DEFAULT, NULL, NULL, true, NULL, |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 633 | NULL, NULL); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 634 | if (pid > 0) { |
| 635 | // The zygote process checks whether the child process has died or not. |
| 636 | ALOGI("System server process %d has been created", pid); |
| 637 | gSystemServerPid = pid; |
| 638 | // There is a slight window that the system server process has crashed |
| 639 | // but it went unnoticed because we haven't published its pid yet. So |
| 640 | // we recheck here just to make sure that all is well. |
| 641 | int status; |
| 642 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 643 | ALOGE("System server process %d has died. Restarting Zygote!", pid); |
| 644 | RuntimeAbort(env); |
| 645 | } |
| 646 | } |
| 647 | return pid; |
| 648 | } |
| 649 | |
| 650 | static JNINativeMethod gMethods[] = { |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 651 | { "nativeForkAndSpecialize", |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 652 | "(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I", |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 653 | (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, |
| 654 | { "nativeForkSystemServer", "(II[II[[IJJ)I", |
| 655 | (void *) com_android_internal_os_Zygote_nativeForkSystemServer } |
| 656 | }; |
| 657 | |
| 658 | int register_com_android_internal_os_Zygote(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 659 | gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName)); |
| 660 | gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks", |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 661 | "(ILjava/lang/String;)V"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 662 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 663 | return RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 664 | } |
| 665 | } // namespace android |
| 666 | |