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