Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [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 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 17 | // sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc |
| 18 | #include <sys/mount.h> |
| 19 | #include <linux/fs.h> |
| 20 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 21 | #include <grp.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 22 | #include <paths.h> |
Elliott Hughes | ada2da9 | 2012-01-17 17:58:58 -0800 | [diff] [blame] | 23 | #include <signal.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 24 | #include <stdlib.h> |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 29 | #include "cutils/fs.h" |
| 30 | #include "cutils/multiuser.h" |
Elliott Hughes | b6636b8 | 2012-04-24 10:41:16 -0700 | [diff] [blame] | 31 | #include "cutils/sched_policy.h" |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 32 | #include "debugger.h" |
| 33 | #include "jni_internal.h" |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 34 | #include "JNIHelp.h" |
| 35 | #include "ScopedLocalRef.h" |
| 36 | #include "ScopedPrimitiveArray.h" |
| 37 | #include "ScopedUtfChars.h" |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 38 | #include "thread.h" |
| 39 | |
Elliott Hughes | b4807ec | 2012-01-17 18:33:04 -0800 | [diff] [blame] | 40 | #if defined(HAVE_PRCTL) |
| 41 | #include <sys/prctl.h> |
| 42 | #endif |
| 43 | |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 44 | #include <selinux/android.h> |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 45 | |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 46 | #if defined(__linux__) |
| 47 | #include <sys/personality.h> |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 48 | #include <sys/utsname.h> |
Nick Kralevich | 2b97c27 | 2013-03-01 11:14:38 -0800 | [diff] [blame] | 49 | #include <sys/capability.h> |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 50 | #endif |
| 51 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 52 | namespace art { |
| 53 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 54 | static pid_t gSystemServerPid = 0; |
| 55 | |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 56 | // Must match values in dalvik.system.Zygote. |
| 57 | enum MountExternalKind { |
| 58 | MOUNT_EXTERNAL_NONE = 0, |
| 59 | MOUNT_EXTERNAL_SINGLEUSER = 1, |
| 60 | MOUNT_EXTERNAL_MULTIUSER = 2, |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 61 | MOUNT_EXTERNAL_MULTIUSER_ALL = 3, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 64 | // This signal handler is for zygote mode, since the zygote must reap its children |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 65 | static void SigChldHandler(int /*signal_number*/) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 66 | pid_t pid; |
| 67 | int status; |
| 68 | |
| 69 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 70 | // Log process-death status that we care about. In general it is |
| 71 | // not safe to call LOG(...) from a signal handler because of |
| 72 | // possible reentrancy. However, we know a priori that the |
| 73 | // current implementation of LOG() is safe to call from a SIGCHLD |
| 74 | // handler in the zygote process. If the LOG() implementation |
| 75 | // changes its locking strategy or its use of syscalls within the |
| 76 | // lazy-init critical section, its use here may become unsafe. |
| 77 | if (WIFEXITED(status)) { |
| 78 | if (WEXITSTATUS(status)) { |
| 79 | LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")"; |
| 80 | } else if (false) { |
| 81 | LOG(INFO) << "Process " << pid << " exited cleanly (" << WEXITSTATUS(status) << ")"; |
| 82 | } |
| 83 | } else if (WIFSIGNALED(status)) { |
| 84 | if (WTERMSIG(status) != SIGKILL) { |
| 85 | LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")"; |
| 86 | } else if (false) { |
| 87 | LOG(INFO) << "Process " << pid << " terminated by signal (" << WTERMSIG(status) << ")"; |
| 88 | } |
| 89 | #ifdef WCOREDUMP |
| 90 | if (WCOREDUMP(status)) { |
| 91 | LOG(INFO) << "Process " << pid << " dumped core"; |
| 92 | } |
| 93 | #endif /* ifdef WCOREDUMP */ |
| 94 | } |
| 95 | |
| 96 | // If the just-crashed process is the system_server, bring down zygote |
| 97 | // so that it is restarted by init and system server will be restarted |
| 98 | // from there. |
| 99 | if (pid == gSystemServerPid) { |
Brian Carlstrom | 6a4be3a | 2011-10-20 16:34:03 -0700 | [diff] [blame] | 100 | LOG(ERROR) << "Exit zygote because system server (" << pid << ") has terminated"; |
| 101 | kill(getpid(), SIGKILL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | if (pid < 0) { |
| 106 | PLOG(WARNING) << "Zygote SIGCHLD error in waitpid"; |
| 107 | } |
| 108 | } |
| 109 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 110 | // Configures the SIGCHLD handler for the zygote process. This is configured |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 111 | // very late, because earlier in the runtime we may fork() and exec() |
| 112 | // other processes, and we want to waitpid() for those rather than |
| 113 | // have them be harvested immediately. |
| 114 | // |
| 115 | // This ends up being called repeatedly before each fork(), but there's |
| 116 | // no real harm in that. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 117 | static void SetSigChldHandler() { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 118 | struct sigaction sa; |
| 119 | memset(&sa, 0, sizeof(sa)); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 120 | sa.sa_handler = SigChldHandler; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 121 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 122 | int err = sigaction(SIGCHLD, &sa, NULL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 123 | if (err < 0) { |
| 124 | PLOG(WARNING) << "Error setting SIGCHLD handler"; |
| 125 | } |
| 126 | } |
| 127 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 128 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
| 129 | static void UnsetSigChldHandler() { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 130 | struct sigaction sa; |
| 131 | memset(&sa, 0, sizeof(sa)); |
| 132 | sa.sa_handler = SIG_DFL; |
| 133 | |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 134 | int err = sigaction(SIGCHLD, &sa, NULL); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 135 | if (err < 0) { |
| 136 | PLOG(WARNING) << "Error unsetting SIGCHLD handler"; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Calls POSIX setgroups() using the int[] object as an argument. |
| 141 | // A NULL argument is tolerated. |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 142 | static void SetGids(JNIEnv* env, jintArray javaGids) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 143 | if (javaGids == NULL) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 144 | return; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | COMPILE_ASSERT(sizeof(gid_t) == sizeof(jint), sizeof_gid_and_jint_are_differerent); |
| 148 | ScopedIntArrayRO gids(env, javaGids); |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 149 | CHECK(gids.get() != NULL); |
| 150 | int rc = setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])); |
| 151 | if (rc == -1) { |
| 152 | PLOG(FATAL) << "setgroups failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 153 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Sets the resource limits via setrlimit(2) for the values in the |
| 157 | // two-dimensional array of integers that's passed in. The second dimension |
| 158 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). NULL is |
| 159 | // treated as an empty array. |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 160 | static void SetRLimits(JNIEnv* env, jobjectArray javaRlimits) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 161 | if (javaRlimits == NULL) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 162 | return; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 165 | rlimit rlim; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 166 | memset(&rlim, 0, sizeof(rlim)); |
| 167 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 168 | for (int i = 0; i < env->GetArrayLength(javaRlimits); ++i) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 169 | ScopedLocalRef<jobject> javaRlimitObject(env, env->GetObjectArrayElement(javaRlimits, i)); |
| 170 | ScopedIntArrayRO javaRlimit(env, reinterpret_cast<jintArray>(javaRlimitObject.get())); |
| 171 | if (javaRlimit.size() != 3) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 172 | LOG(FATAL) << "rlimits array must have a second dimension of size 3"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | rlim.rlim_cur = javaRlimit[1]; |
| 176 | rlim.rlim_max = javaRlimit[2]; |
| 177 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 178 | int rc = setrlimit(javaRlimit[0], &rlim); |
| 179 | if (rc == -1) { |
| 180 | PLOG(FATAL) << "setrlimit(" << javaRlimit[0] << ", " |
| 181 | << "{" << rlim.rlim_cur << ", " << rlim.rlim_max << "}) failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 186 | #if defined(HAVE_ANDROID_OS) |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 187 | |
| 188 | // The debug malloc library needs to know whether it's the zygote or a child. |
| 189 | extern "C" int gMallocLeakZygoteChild; |
| 190 | |
| 191 | static void EnableDebugger() { |
| 192 | // To let a non-privileged gdbserver attach to this |
| 193 | // process, we must set our dumpable flag. |
| 194 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 195 | PLOG(ERROR) << "prctl(PR_SET_DUMPABLE) failed for pid " << getpid(); |
| 196 | } |
| 197 | // We don't want core dumps, though, so set the core dump size to 0. |
| 198 | rlimit rl; |
| 199 | rl.rlim_cur = 0; |
| 200 | rl.rlim_max = RLIM_INFINITY; |
| 201 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 202 | PLOG(ERROR) << "setrlimit(RLIMIT_CORE) failed for pid " << getpid(); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | static void EnableKeepCapabilities() { |
| 207 | int rc = prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0); |
| 208 | if (rc == -1) { |
| 209 | PLOG(FATAL) << "prctl(PR_SET_KEEPCAPS) failed"; |
| 210 | } |
| 211 | } |
| 212 | |
Nick Kralevich | a0664be | 2013-02-13 14:39:17 -0800 | [diff] [blame] | 213 | static void DropCapabilitiesBoundingSet() { |
| 214 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) { |
| 215 | if (i == CAP_NET_RAW) { |
| 216 | // Don't break /system/bin/ping |
| 217 | continue; |
| 218 | } |
| 219 | int rc = prctl(PR_CAPBSET_DROP, i, 0, 0, 0); |
| 220 | if (rc == -1) { |
| 221 | if (errno == EINVAL) { |
| 222 | PLOG(ERROR) << "prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " |
| 223 | << "your kernel is compiled with file capabilities support"; |
| 224 | } else { |
| 225 | PLOG(FATAL) << "prctl(PR_CAPBSET_DROP) failed"; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 231 | static void SetCapabilities(int64_t permitted, int64_t effective) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 232 | __user_cap_header_struct capheader; |
| 233 | __user_cap_data_struct capdata; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 234 | |
| 235 | memset(&capheader, 0, sizeof(capheader)); |
| 236 | memset(&capdata, 0, sizeof(capdata)); |
| 237 | |
| 238 | capheader.version = _LINUX_CAPABILITY_VERSION; |
| 239 | capheader.pid = 0; |
| 240 | |
| 241 | capdata.effective = effective; |
| 242 | capdata.permitted = permitted; |
| 243 | |
| 244 | if (capset(&capheader, &capdata) != 0) { |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 245 | PLOG(FATAL) << "capset(" << permitted << ", " << effective << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 246 | } |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 247 | } |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 248 | |
| 249 | static void SetSchedulerPolicy() { |
| 250 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 251 | if (errno != 0) { |
| 252 | PLOG(FATAL) << "set_sched_policy(0, SP_DEFAULT) failed"; |
| 253 | } |
| 254 | } |
| 255 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 256 | #else |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 257 | |
| 258 | static int gMallocLeakZygoteChild = 0; |
| 259 | |
| 260 | static void EnableDebugger() {} |
| 261 | static void EnableKeepCapabilities() {} |
Brian Carlstrom | 9b7085a | 2013-07-18 15:15:21 -0700 | [diff] [blame] | 262 | static void DropCapabilitiesBoundingSet() {} |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 263 | static void SetCapabilities(int64_t, int64_t) {} |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 264 | static void SetSchedulerPolicy() {} |
| 265 | |
Elliott Hughes | 76e3694 | 2012-03-16 13:44:56 -0700 | [diff] [blame] | 266 | #endif |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 267 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 268 | static void EnableDebugFeatures(uint32_t debug_flags) { |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 269 | // Must match values in dalvik.system.Zygote. |
| 270 | enum { |
| 271 | DEBUG_ENABLE_DEBUGGER = 1, |
| 272 | DEBUG_ENABLE_CHECKJNI = 1 << 1, |
| 273 | DEBUG_ENABLE_ASSERT = 1 << 2, |
| 274 | DEBUG_ENABLE_SAFEMODE = 1 << 3, |
| 275 | DEBUG_ENABLE_JNI_LOGGING = 1 << 4, |
| 276 | }; |
| 277 | |
| 278 | if ((debug_flags & DEBUG_ENABLE_CHECKJNI) != 0) { |
| 279 | Runtime* runtime = Runtime::Current(); |
| 280 | JavaVMExt* vm = runtime->GetJavaVM(); |
| 281 | if (!vm->check_jni) { |
| 282 | LOG(DEBUG) << "Late-enabling -Xcheck:jni"; |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 283 | vm->SetCheckJniEnabled(true); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 284 | // There's only one thread running at this point, so only one JNIEnv to fix up. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 285 | Thread::Current()->GetJniEnv()->SetCheckJniEnabled(true); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 286 | } else { |
| 287 | LOG(DEBUG) << "Not late-enabling -Xcheck:jni (already on)"; |
| 288 | } |
| 289 | debug_flags &= ~DEBUG_ENABLE_CHECKJNI; |
| 290 | } |
| 291 | |
| 292 | if ((debug_flags & DEBUG_ENABLE_JNI_LOGGING) != 0) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 293 | gLogVerbosity.third_party_jni = true; |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 294 | debug_flags &= ~DEBUG_ENABLE_JNI_LOGGING; |
| 295 | } |
| 296 | |
| 297 | Dbg::SetJdwpAllowed((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 298 | if ((debug_flags & DEBUG_ENABLE_DEBUGGER) != 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 299 | EnableDebugger(); |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 300 | } |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 301 | debug_flags &= ~DEBUG_ENABLE_DEBUGGER; |
| 302 | |
| 303 | // These two are for backwards compatibility with Dalvik. |
| 304 | debug_flags &= ~DEBUG_ENABLE_ASSERT; |
| 305 | debug_flags &= ~DEBUG_ENABLE_SAFEMODE; |
| 306 | |
| 307 | if (debug_flags != 0) { |
| 308 | LOG(ERROR) << StringPrintf("Unknown bits set in debug_flags: %#x", debug_flags); |
| 309 | } |
| 310 | } |
| 311 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 312 | // Create a private mount namespace and bind mount appropriate emulated |
| 313 | // storage for the given user. |
| 314 | static bool MountEmulatedStorage(uid_t uid, jint mount_mode) { |
| 315 | if (mount_mode == MOUNT_EXTERNAL_NONE) { |
| 316 | return true; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 319 | // See storage config details at http://source.android.com/tech/storage/ |
| 320 | userid_t user_id = multiuser_get_user_id(uid); |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 321 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 322 | // Create a second private mount namespace for our process |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 323 | if (unshare(CLONE_NEWNS) == -1) { |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 324 | PLOG(WARNING) << "Failed to unshare()"; |
| 325 | return false; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 328 | // Create bind mounts to expose external storage |
| 329 | if (mount_mode == MOUNT_EXTERNAL_MULTIUSER || mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) { |
| 330 | // These paths must already be created by init.rc |
| 331 | const char* source = getenv("EMULATED_STORAGE_SOURCE"); |
| 332 | const char* target = getenv("EMULATED_STORAGE_TARGET"); |
| 333 | const char* legacy = getenv("EXTERNAL_STORAGE"); |
| 334 | if (source == NULL || target == NULL || legacy == NULL) { |
| 335 | LOG(WARNING) << "Storage environment undefined; unable to provide external storage"; |
| 336 | return false; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 337 | } |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 338 | |
| 339 | // Prepare source paths |
| 340 | |
| 341 | // /mnt/shell/emulated/0 |
| 342 | std::string source_user(StringPrintf("%s/%d", source, user_id)); |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 343 | // /storage/emulated/0 |
| 344 | std::string target_user(StringPrintf("%s/%d", target, user_id)); |
| 345 | |
| 346 | if (fs_prepare_dir(source_user.c_str(), 0000, 0, 0) == -1 |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 347 | || fs_prepare_dir(target_user.c_str(), 0000, 0, 0) == -1) { |
| 348 | return false; |
| 349 | } |
| 350 | |
| 351 | if (mount_mode == MOUNT_EXTERNAL_MULTIUSER_ALL) { |
| 352 | // Mount entire external storage tree for all users |
| 353 | if (mount(source, target, NULL, MS_BIND, NULL) == -1) { |
| 354 | PLOG(WARNING) << "Failed to mount " << source << " to " << target; |
| 355 | return false; |
| 356 | } |
| 357 | } else { |
| 358 | // Only mount user-specific external storage |
| 359 | if (mount(source_user.c_str(), target_user.c_str(), NULL, MS_BIND, NULL) == -1) { |
| 360 | PLOG(WARNING) << "Failed to mount " << source_user << " to " << target_user; |
| 361 | return false; |
| 362 | } |
| 363 | } |
| 364 | |
Jeff Sharkey | b8c46fc | 2013-09-10 18:28:10 -0700 | [diff] [blame^] | 365 | if (fs_prepare_dir(legacy, 0000, 0, 0) == -1) { |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 366 | return false; |
| 367 | } |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 368 | |
| 369 | // Finally, mount user-specific path into place for legacy users |
| 370 | if (mount(target_user.c_str(), legacy, NULL, MS_BIND | MS_REC, NULL) == -1) { |
| 371 | PLOG(WARNING) << "Failed to mount " << target_user << " to " << legacy; |
| 372 | return false; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 373 | } |
| 374 | } else { |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 375 | LOG(WARNING) << "Mount mode " << mount_mode << " unsupported"; |
| 376 | return false; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 377 | } |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 378 | |
| 379 | return true; |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 382 | #if defined(__linux__) |
| 383 | static bool NeedsNoRandomizeWorkaround() { |
Nick Kralevich | 50e1744 | 2012-10-19 13:02:37 -0700 | [diff] [blame] | 384 | #if !defined(__arm__) |
| 385 | return false; |
| 386 | #else |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 387 | int major; |
| 388 | int minor; |
| 389 | struct utsname uts; |
| 390 | if (uname(&uts) == -1) { |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | // Kernels before 3.4.* need the workaround. |
| 399 | return (major < 3) || ((major == 3) && (minor < 4)); |
Nick Kralevich | 50e1744 | 2012-10-19 13:02:37 -0700 | [diff] [blame] | 400 | #endif |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 401 | } |
| 402 | #endif |
| 403 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 404 | // Utility routine to fork zygote and specialize the child process. |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 405 | static pid_t ForkAndSpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray javaGids, |
| 406 | jint debug_flags, jobjectArray javaRlimits, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 407 | jlong permittedCapabilities, jlong effectiveCapabilities, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 408 | jint mount_external, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 409 | jstring java_se_info, jstring java_se_name, bool is_system_server) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 410 | Runtime* runtime = Runtime::Current(); |
| 411 | CHECK(runtime->IsZygote()) << "runtime instance not started with -Xzygote"; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 412 | if (!runtime->PreZygoteFork()) { |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 413 | LOG(FATAL) << "pre-fork heap failed"; |
| 414 | } |
| 415 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 416 | SetSigChldHandler(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 417 | |
| 418 | // Grab thread before fork potentially makes Thread::pthread_key_self_ unusable. |
| 419 | Thread* self = Thread::Current(); |
| 420 | |
| 421 | // dvmDumpLoaderStats("zygote"); // TODO: ? |
| 422 | pid_t pid = fork(); |
| 423 | |
| 424 | if (pid == 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 425 | // The child process. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 426 | gMallocLeakZygoteChild = 1; |
| 427 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 428 | // Keep capabilities across UID change, unless we're staying root. |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 429 | if (uid != 0) { |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 430 | EnableKeepCapabilities(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Nick Kralevich | a0664be | 2013-02-13 14:39:17 -0800 | [diff] [blame] | 433 | DropCapabilitiesBoundingSet(); |
| 434 | |
Brian Carlstrom | bd86bcc | 2013-03-10 20:26:16 -0700 | [diff] [blame] | 435 | if (!MountEmulatedStorage(uid, mount_external)) { |
| 436 | PLOG(WARNING) << "Failed to mount emulated storage"; |
| 437 | if (errno == ENOTCONN || errno == EROFS) { |
| 438 | // When device is actively encrypting, we get ENOTCONN here |
| 439 | // since FUSE was mounted before the framework restarted. |
| 440 | // When encrypted device is booting, we get EROFS since |
| 441 | // FUSE hasn't been created yet by init. |
| 442 | // In either case, continue without external storage. |
| 443 | } else { |
| 444 | LOG(FATAL) << "Cannot continue without emulated storage"; |
| 445 | } |
| 446 | } |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 447 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 448 | SetGids(env, javaGids); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 449 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 450 | SetRLimits(env, javaRlimits); |
| 451 | |
Nick Kralevich | 7e2364c | 2013-02-20 14:46:54 -0800 | [diff] [blame] | 452 | int rc = setresgid(gid, gid, gid); |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 453 | if (rc == -1) { |
Nick Kralevich | 7e2364c | 2013-02-20 14:46:54 -0800 | [diff] [blame] | 454 | PLOG(FATAL) << "setresgid(" << gid << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Nick Kralevich | 7e2364c | 2013-02-20 14:46:54 -0800 | [diff] [blame] | 457 | rc = setresuid(uid, uid, uid); |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 458 | if (rc == -1) { |
Nick Kralevich | 7e2364c | 2013-02-20 14:46:54 -0800 | [diff] [blame] | 459 | PLOG(FATAL) << "setresuid(" << uid << ") failed"; |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 462 | #if defined(__linux__) |
Nick Kralevich | b788278 | 2012-09-27 12:29:02 -0700 | [diff] [blame] | 463 | if (NeedsNoRandomizeWorkaround()) { |
| 464 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 465 | int old_personality = personality(0xffffffff); |
| 466 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 467 | if (new_personality == -1) { |
| 468 | PLOG(WARNING) << "personality(" << new_personality << ") failed"; |
| 469 | } |
Elliott Hughes | 51e5386 | 2012-05-02 17:17:28 -0700 | [diff] [blame] | 470 | } |
Brian Carlstrom | 154cef6 | 2012-05-02 22:34:03 -0700 | [diff] [blame] | 471 | #endif |
Elliott Hughes | 51e5386 | 2012-05-02 17:17:28 -0700 | [diff] [blame] | 472 | |
Elliott Hughes | 3d30d9b | 2011-12-07 17:35:48 -0800 | [diff] [blame] | 473 | SetCapabilities(permittedCapabilities, effectiveCapabilities); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 474 | |
Elliott Hughes | 2abeb9b | 2012-05-11 23:42:02 -0700 | [diff] [blame] | 475 | SetSchedulerPolicy(); |
Elliott Hughes | b6636b8 | 2012-04-24 10:41:16 -0700 | [diff] [blame] | 476 | |
Kenny Root | dc1cd10 | 2012-10-17 10:35:32 -0700 | [diff] [blame] | 477 | #if defined(HAVE_ANDROID_OS) |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 478 | { // NOLINT(whitespace/braces) |
Brian Carlstrom | 1a0b475 | 2012-10-17 16:23:18 -0700 | [diff] [blame] | 479 | const char* se_info_c_str = NULL; |
| 480 | UniquePtr<ScopedUtfChars> se_info; |
| 481 | if (java_se_info != NULL) { |
| 482 | se_info.reset(new ScopedUtfChars(env, java_se_info)); |
| 483 | se_info_c_str = se_info->c_str(); |
| 484 | CHECK(se_info_c_str != NULL); |
| 485 | } |
| 486 | const char* se_name_c_str = NULL; |
| 487 | UniquePtr<ScopedUtfChars> se_name; |
| 488 | if (java_se_name != NULL) { |
| 489 | se_name.reset(new ScopedUtfChars(env, java_se_name)); |
| 490 | se_name_c_str = se_name->c_str(); |
| 491 | CHECK(se_name_c_str != NULL); |
| 492 | } |
| 493 | rc = selinux_android_setcontext(uid, is_system_server, se_info_c_str, se_name_c_str); |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 494 | if (rc == -1) { |
| 495 | PLOG(FATAL) << "selinux_android_setcontext(" << uid << ", " |
| 496 | << (is_system_server ? "true" : "false") << ", " |
Brian Carlstrom | 1a0b475 | 2012-10-17 16:23:18 -0700 | [diff] [blame] | 497 | << "\"" << se_info_c_str << "\", \"" << se_name_c_str << "\") failed"; |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | #else |
| 501 | UNUSED(is_system_server); |
| 502 | UNUSED(java_se_info); |
| 503 | UNUSED(java_se_name); |
| 504 | #endif |
| 505 | |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 506 | // Our system thread ID, etc, has changed so reset Thread state. |
| 507 | self->InitAfterFork(); |
| 508 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 509 | EnableDebugFeatures(debug_flags); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 510 | |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 511 | UnsetSigChldHandler(); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 512 | runtime->DidForkFromZygote(); |
| 513 | } else if (pid > 0) { |
| 514 | // the parent process |
| 515 | } |
| 516 | return pid; |
| 517 | } |
| 518 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 519 | static jint Zygote_nativeForkAndSpecialize(JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 520 | jint debug_flags, jobjectArray rlimits, jint mount_external, |
Elliott Hughes | c151f90 | 2012-06-21 20:33:21 -0700 | [diff] [blame] | 521 | jstring se_info, jstring se_name) { |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 522 | return ForkAndSpecializeCommon(env, uid, gid, gids, debug_flags, rlimits, 0, 0, mount_external, se_info, se_name, false); |
Brian Carlstrom | a9f1978 | 2011-10-13 00:14:47 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 525 | static jint Zygote_nativeForkSystemServer(JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
| 526 | jint debug_flags, jobjectArray rlimits, |
| 527 | jlong permittedCapabilities, jlong effectiveCapabilities) { |
Elliott Hughes | 4ffd313 | 2011-10-24 12:06:42 -0700 | [diff] [blame] | 528 | pid_t pid = ForkAndSpecializeCommon(env, uid, gid, gids, |
| 529 | debug_flags, rlimits, |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 530 | permittedCapabilities, effectiveCapabilities, |
| 531 | MOUNT_EXTERNAL_NONE, NULL, NULL, true); |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 532 | if (pid > 0) { |
| 533 | // The zygote process checks whether the child process has died or not. |
| 534 | LOG(INFO) << "System server process " << pid << " has been created"; |
| 535 | gSystemServerPid = pid; |
| 536 | // There is a slight window that the system server process has crashed |
| 537 | // but it went unnoticed because we haven't published its pid yet. So |
| 538 | // we recheck here just to make sure that all is well. |
| 539 | int status; |
| 540 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 541 | LOG(FATAL) << "System server process " << pid << " has died. Restarting Zygote!"; |
| 542 | } |
| 543 | } |
| 544 | return pid; |
| 545 | } |
| 546 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 547 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 178cdcc | 2012-08-16 16:47:31 -0700 | [diff] [blame] | 548 | NATIVE_METHOD(Zygote, nativeForkAndSpecialize, "(II[II[[IILjava/lang/String;Ljava/lang/String;)I"), |
Brian Carlstrom | caabb1b | 2011-10-11 18:09:13 -0700 | [diff] [blame] | 549 | NATIVE_METHOD(Zygote, nativeForkSystemServer, "(II[II[[IJJ)I"), |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 550 | }; |
| 551 | |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 552 | void register_dalvik_system_Zygote(JNIEnv* env) { |
Elliott Hughes | 7756d54 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 553 | REGISTER_NATIVE_METHODS("dalvik/system/Zygote"); |
Elliott Hughes | 01158d7 | 2011-09-19 19:47:10 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | } // namespace art |