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