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