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