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