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
17 #define LOG_TAG "Zygote"
18 #define ATRACE_TAG ATRACE_TAG_DALVIK
19
20 #include "com_android_internal_os_Zygote.h"
21
22 #include <async_safe/log.h>
23
24 // sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc
25 #include <sys/mount.h>
26 #include <linux/fs.h>
27 #include <sys/types.h>
28 #include <dirent.h>
29
30 #include <algorithm>
31 #include <array>
32 #include <atomic>
33 #include <functional>
34 #include <iterator>
35 #include <list>
36 #include <optional>
37 #include <sstream>
38 #include <string>
39 #include <string_view>
40 #include <unordered_set>
41
42 #include <android/fdsan.h>
43 #include <arpa/inet.h>
44 #include <fcntl.h>
45 #include <grp.h>
46 #include <inttypes.h>
47 #include <malloc.h>
48 #include <mntent.h>
49 #include <paths.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <sys/auxv.h>
54 #include <sys/capability.h>
55 #include <sys/cdefs.h>
56 #include <sys/eventfd.h>
57 #include <sys/personality.h>
58 #include <sys/prctl.h>
59 #include <sys/resource.h>
60 #include <sys/socket.h>
61 #include <sys/stat.h>
62 #include <sys/time.h>
63 #include <sys/types.h>
64 #include <sys/un.h>
65 #include <sys/wait.h>
66 #include <unistd.h>
67
68 #include <android-base/file.h>
69 #include <android-base/logging.h>
70 #include <android-base/properties.h>
71 #include <android-base/stringprintf.h>
72 #include <android-base/unique_fd.h>
73 #include <bionic/malloc.h>
74 #include <bionic/mte.h>
75 #include <cutils/fs.h>
76 #include <cutils/multiuser.h>
77 #include <cutils/sockets.h>
78 #include <private/android_filesystem_config.h>
79 #include <processgroup/processgroup.h>
80 #include <processgroup/sched_policy.h>
81 #include <seccomp_policy.h>
82 #include <selinux/android.h>
83 #include <stats_socket.h>
84 #include <utils/String8.h>
85 #include <utils/Trace.h>
86
87 #include <nativehelper/JNIHelp.h>
88 #include <nativehelper/ScopedLocalRef.h>
89 #include <nativehelper/ScopedPrimitiveArray.h>
90 #include <nativehelper/ScopedUtfChars.h>
91 #include "core_jni_helpers.h"
92 #include "fd_utils.h"
93 #include "filesystem_utils.h"
94
95 #include "nativebridge/native_bridge.h"
96
97 #if defined(__BIONIC__)
98 extern "C" void android_reset_stack_guards();
99 #endif
100
101 namespace {
102
103 // TODO (chriswailes): Add a function to initialize native Zygote data.
104 // TODO (chriswailes): Fix mixed indentation style (2 and 4 spaces).
105
106 using namespace std::placeholders;
107
108 using android::String8;
109 using android::base::ReadFileToString;
110 using android::base::StringAppendF;
111 using android::base::StringPrintf;
112 using android::base::WriteStringToFile;
113 using android::base::GetBoolProperty;
114
115 using android::zygote::ZygoteFailure;
116
117 using Action = android_mallopt_gwp_asan_options_t::Action;
118
119 // This type is duplicated in fd_utils.h
120 typedef const std::function<void(std::string)>& fail_fn_t;
121
122 static pid_t gSystemServerPid = 0;
123
124 static constexpr const char* kVoldAppDataIsolation = "persist.sys.vold_app_data_isolation_enabled";
125 static const char kZygoteClassName[] = "com/android/internal/os/Zygote";
126 static jclass gZygoteClass;
127 static jmethodID gCallPostForkSystemServerHooks;
128 static jmethodID gCallPostForkChildHooks;
129
130 static constexpr const char* kZygoteInitClassName = "com/android/internal/os/ZygoteInit";
131 static jclass gZygoteInitClass;
132 static jmethodID gGetOrCreateSystemServerClassLoader;
133 static jmethodID gPrefetchStandaloneSystemServerJars;
134
135 static bool gIsSecurityEnforced = true;
136
137 /**
138 * True if the app process is running in its mount namespace.
139 */
140 static bool gInAppMountNamespace = false;
141
142 /**
143 * The maximum number of characters (not including a null terminator) that a
144 * process name may contain.
145 */
146 static constexpr size_t MAX_NAME_LENGTH = 15;
147
148 /**
149 * The file descriptor for the Zygote socket opened by init.
150 */
151
152 static int gZygoteSocketFD = -1;
153
154 /**
155 * The file descriptor for the unspecialized app process (USAP) pool socket opened by init.
156 */
157
158 static int gUsapPoolSocketFD = -1;
159
160 /**
161 * The number of USAPs currently in this Zygote's pool.
162 */
163 static std::atomic_uint32_t gUsapPoolCount = 0;
164
165 /**
166 * Event file descriptor used to communicate reaped USAPs to the
167 * ZygoteServer.
168 */
169 static int gUsapPoolEventFD = -1;
170
171 /**
172 * The socket file descriptor used to send notifications to the
173 * system_server.
174 */
175 static int gSystemServerSocketFd = -1;
176
177 static constexpr int DEFAULT_DATA_DIR_PERMISSION = 0751;
178
179 static constexpr const uint64_t UPPER_HALF_WORD_MASK = 0xFFFF'FFFF'0000'0000;
180 static constexpr const uint64_t LOWER_HALF_WORD_MASK = 0x0000'0000'FFFF'FFFF;
181
182 static constexpr const char* kCurProfileDirPath = "/data/misc/profiles/cur";
183 static constexpr const char* kRefProfileDirPath = "/data/misc/profiles/ref";
184
185 /**
186 * The maximum value that the gUSAPPoolSizeMax variable may take. This value
187 * is a mirror of ZygoteServer.USAP_POOL_SIZE_MAX_LIMIT
188 */
189 static constexpr int USAP_POOL_SIZE_MAX_LIMIT = 100;
190
191 /** The numeric value for the maximum priority a process may possess. */
192 static constexpr int PROCESS_PRIORITY_MAX = -20;
193
194 /** The numeric value for the minimum priority a process may possess. */
195 static constexpr int PROCESS_PRIORITY_MIN = 19;
196
197 /** The numeric value for the normal priority a process should have. */
198 static constexpr int PROCESS_PRIORITY_DEFAULT = 0;
199
200 /** Exponential back off parameters for storage dir check. */
201 static constexpr unsigned int STORAGE_DIR_CHECK_RETRY_MULTIPLIER = 2;
202 static constexpr unsigned int STORAGE_DIR_CHECK_INIT_INTERVAL_US = 50;
203 static constexpr unsigned int STORAGE_DIR_CHECK_MAX_INTERVAL_US = 1000;
204 /**
205 * Lower bound time we allow storage dir check to sleep.
206 * If it exceeds 2s, PROC_START_TIMEOUT_MSG will kill the starting app anyway,
207 * so it's fine to assume max retries is 5 mins.
208 */
209 static constexpr int STORAGE_DIR_CHECK_TIMEOUT_US = 1000 * 1000 * 60 * 5;
210
211 static void WaitUntilDirReady(const std::string& target, fail_fn_t fail_fn);
212
213 /**
214 * A helper class containing accounting information for USAPs.
215 */
216 class UsapTableEntry {
217 public:
218 struct EntryStorage {
219 int32_t pid;
220 int32_t read_pipe_fd;
221
operator !=__anon59e4017d0110::UsapTableEntry::EntryStorage222 bool operator!=(const EntryStorage& other) {
223 return pid != other.pid || read_pipe_fd != other.read_pipe_fd;
224 }
225 };
226
227 private:
228 static constexpr EntryStorage INVALID_ENTRY_VALUE = {-1, -1};
229
230 std::atomic<EntryStorage> mStorage;
231 static_assert(decltype(mStorage)::is_always_lock_free); // Accessed from signal handler.
232
233 public:
UsapTableEntry()234 constexpr UsapTableEntry() : mStorage(INVALID_ENTRY_VALUE) {}
235
236 /**
237 * If the provided PID matches the one stored in this entry, the entry will
238 * be invalidated and the associated file descriptor will be closed. If the
239 * PIDs don't match nothing will happen.
240 *
241 * @param pid The ID of the process who's entry we want to clear.
242 * @return True if the entry was cleared by this call; false otherwise
243 */
ClearForPID(int32_t pid)244 bool ClearForPID(int32_t pid) {
245 EntryStorage storage = mStorage.load();
246
247 if (storage.pid == pid) {
248 /*
249 * There are three possible outcomes from this compare-and-exchange:
250 * 1) It succeeds, in which case we close the FD
251 * 2) It fails and the new value is INVALID_ENTRY_VALUE, in which case
252 * the entry has already been cleared.
253 * 3) It fails and the new value isn't INVALID_ENTRY_VALUE, in which
254 * case the entry has already been cleared and re-used.
255 *
256 * In all three cases the goal of the caller has been met, but only in
257 * the first case do we need to decrement the pool count.
258 */
259 if (mStorage.compare_exchange_strong(storage, INVALID_ENTRY_VALUE)) {
260 close(storage.read_pipe_fd);
261 return true;
262 } else {
263 return false;
264 }
265
266 } else {
267 return false;
268 }
269 }
270
Clear()271 void Clear() {
272 EntryStorage storage = mStorage.load();
273
274 if (storage != INVALID_ENTRY_VALUE) {
275 close(storage.read_pipe_fd);
276 mStorage.store(INVALID_ENTRY_VALUE);
277 }
278 }
279
Invalidate()280 void Invalidate() {
281 mStorage.store(INVALID_ENTRY_VALUE);
282 }
283
284 /**
285 * @return A copy of the data stored in this entry.
286 */
GetValues()287 std::optional<EntryStorage> GetValues() {
288 EntryStorage storage = mStorage.load();
289
290 if (storage != INVALID_ENTRY_VALUE) {
291 return storage;
292 } else {
293 return std::nullopt;
294 }
295 }
296
297 /**
298 * Sets the entry to the given values if it is currently invalid.
299 *
300 * @param pid The process ID for the new entry.
301 * @param read_pipe_fd The read end of the USAP control pipe for this
302 * process.
303 * @return True if the entry was set; false otherwise.
304 */
SetIfInvalid(int32_t pid,int32_t read_pipe_fd)305 bool SetIfInvalid(int32_t pid, int32_t read_pipe_fd) {
306 EntryStorage new_value_storage;
307
308 new_value_storage.pid = pid;
309 new_value_storage.read_pipe_fd = read_pipe_fd;
310
311 EntryStorage expected = INVALID_ENTRY_VALUE;
312
313 return mStorage.compare_exchange_strong(expected, new_value_storage);
314 }
315 };
316
317 /**
318 * A table containing information about the USAPs currently in the pool.
319 *
320 * Multiple threads may be attempting to modify the table, either from the
321 * signal handler or from the ZygoteServer poll loop. Atomic loads/stores in
322 * the USAPTableEntry class prevent data races during these concurrent
323 * operations.
324 */
325 static std::array<UsapTableEntry, USAP_POOL_SIZE_MAX_LIMIT> gUsapTable;
326
327 /**
328 * The list of open zygote file descriptors.
329 */
330 static FileDescriptorTable* gOpenFdTable = nullptr;
331
332 // Must match values in com.android.internal.os.Zygote.
333 // The values should be consistent with IVold.aidl
334 enum MountExternalKind {
335 MOUNT_EXTERNAL_NONE = 0,
336 MOUNT_EXTERNAL_DEFAULT = 1,
337 MOUNT_EXTERNAL_INSTALLER = 2,
338 MOUNT_EXTERNAL_PASS_THROUGH = 3,
339 MOUNT_EXTERNAL_ANDROID_WRITABLE = 4,
340 MOUNT_EXTERNAL_COUNT = 5
341 };
342
343 // Must match values in com.android.internal.os.Zygote.
344 enum RuntimeFlags : uint32_t {
345 DEBUG_ENABLE_JDWP = 1,
346 PROFILE_SYSTEM_SERVER = 1 << 14,
347 PROFILE_FROM_SHELL = 1 << 15,
348 MEMORY_TAG_LEVEL_MASK = (1 << 19) | (1 << 20),
349 MEMORY_TAG_LEVEL_TBI = 1 << 19,
350 MEMORY_TAG_LEVEL_ASYNC = 2 << 19,
351 MEMORY_TAG_LEVEL_SYNC = 3 << 19,
352 GWP_ASAN_LEVEL_MASK = (1 << 21) | (1 << 22),
353 GWP_ASAN_LEVEL_NEVER = 0 << 21,
354 GWP_ASAN_LEVEL_LOTTERY = 1 << 21,
355 GWP_ASAN_LEVEL_ALWAYS = 2 << 21,
356 GWP_ASAN_LEVEL_DEFAULT = 3 << 21,
357 NATIVE_HEAP_ZERO_INIT_ENABLED = 1 << 23,
358 PROFILEABLE = 1 << 24,
359 DEBUG_ENABLE_PTRACE = 1 << 25,
360 };
361
362 enum UnsolicitedZygoteMessageTypes : uint32_t {
363 UNSOLICITED_ZYGOTE_MESSAGE_TYPE_RESERVED = 0,
364 UNSOLICITED_ZYGOTE_MESSAGE_TYPE_SIGCHLD = 1,
365 };
366
367 struct UnsolicitedZygoteMessageSigChld {
368 struct {
369 UnsolicitedZygoteMessageTypes type;
370 } header;
371 struct {
372 pid_t pid;
373 uid_t uid;
374 int status;
375 } payload;
376 };
377
378 // Keep sync with services/core/java/com/android/server/am/ProcessList.java
379 static constexpr struct sockaddr_un kSystemServerSockAddr =
380 {.sun_family = AF_LOCAL, .sun_path = "/data/system/unsolzygotesocket"};
381
382 // Forward declaration so we don't have to move the signal handler.
383 static bool RemoveUsapTableEntry(pid_t usap_pid);
384
RuntimeAbort(JNIEnv * env,int line,const char * msg)385 static void RuntimeAbort(JNIEnv* env, int line, const char* msg) {
386 std::ostringstream oss;
387 oss << __FILE__ << ":" << line << ": " << msg;
388 env->FatalError(oss.str().c_str());
389 }
390
391 // Create the socket which is going to be used to send unsolicited message
392 // to system_server, the socket will be closed post forking a child process.
393 // It's expected to be called at each zygote's initialization.
initUnsolSocketToSystemServer()394 static void initUnsolSocketToSystemServer() {
395 gSystemServerSocketFd = socket(AF_LOCAL, SOCK_DGRAM | SOCK_NONBLOCK, 0);
396 if (gSystemServerSocketFd >= 0) {
397 ALOGV("Zygote:systemServerSocketFD = %d", gSystemServerSocketFd);
398 } else {
399 ALOGE("Unable to create socket file descriptor to connect to system_server");
400 }
401 }
402
sendSigChildStatus(const pid_t pid,const uid_t uid,const int status)403 static void sendSigChildStatus(const pid_t pid, const uid_t uid, const int status) {
404 int socketFd = gSystemServerSocketFd;
405 if (socketFd >= 0) {
406 // fill the message buffer
407 struct UnsolicitedZygoteMessageSigChld data =
408 {.header = {.type = UNSOLICITED_ZYGOTE_MESSAGE_TYPE_SIGCHLD},
409 .payload = {.pid = pid, .uid = uid, .status = status}};
410 if (TEMP_FAILURE_RETRY(
411 sendto(socketFd, &data, sizeof(data), 0,
412 reinterpret_cast<const struct sockaddr*>(&kSystemServerSockAddr),
413 sizeof(kSystemServerSockAddr))) == -1) {
414 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
415 "Zygote failed to write to system_server FD: %s",
416 strerror(errno));
417 }
418 }
419 }
420
421 // This signal handler is for zygote mode, since the zygote must reap its children
422 NO_STACK_PROTECTOR
SigChldHandler(int,siginfo_t * info,void *)423 static void SigChldHandler(int /*signal_number*/, siginfo_t* info, void* /*ucontext*/) {
424 pid_t pid;
425 int status;
426 int64_t usaps_removed = 0;
427
428 // It's necessary to save and restore the errno during this function.
429 // Since errno is stored per thread, changing it here modifies the errno
430 // on the thread on which this signal handler executes. If a signal occurs
431 // between a call and an errno check, it's possible to get the errno set
432 // here.
433 // See b/23572286 for extra information.
434 int saved_errno = errno;
435
436 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
437 // Notify system_server that we received a SIGCHLD
438 sendSigChildStatus(pid, info->si_uid, status);
439 // Log process-death status that we care about.
440 if (WIFEXITED(status)) {
441 async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG, "Process %d exited cleanly (%d)", pid,
442 WEXITSTATUS(status));
443
444 // Check to see if the PID is in the USAP pool and remove it if it is.
445 if (RemoveUsapTableEntry(pid)) {
446 ++usaps_removed;
447 }
448 } else if (WIFSIGNALED(status)) {
449 async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG,
450 "Process %d exited due to signal %d (%s)%s", pid,
451 WTERMSIG(status), strsignal(WTERMSIG(status)),
452 WCOREDUMP(status) ? "; core dumped" : "");
453
454 // If the process exited due to a signal other than SIGTERM, check to see
455 // if the PID is in the USAP pool and remove it if it is. If the process
456 // was closed by the Zygote using SIGTERM then the USAP pool entry will
457 // have already been removed (see nativeEmptyUsapPool()).
458 if (WTERMSIG(status) != SIGTERM && RemoveUsapTableEntry(pid)) {
459 ++usaps_removed;
460 }
461 }
462
463 // If the just-crashed process is the system_server, bring down zygote
464 // so that it is restarted by init and system server will be restarted
465 // from there.
466 if (pid == gSystemServerPid) {
467 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
468 "Exit zygote because system server (pid %d) has terminated", pid);
469 kill(getpid(), SIGKILL);
470 }
471 }
472
473 // Note that we shouldn't consider ECHILD an error because
474 // the secondary zygote might have no children left to wait for.
475 if (pid < 0 && errno != ECHILD) {
476 async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, "Zygote SIGCHLD error in waitpid: %s",
477 strerror(errno));
478 }
479
480 if (usaps_removed > 0) {
481 if (TEMP_FAILURE_RETRY(write(gUsapPoolEventFD, &usaps_removed, sizeof(usaps_removed))) ==
482 -1) {
483 // If this write fails something went terribly wrong. We will now kill
484 // the zygote and let the system bring it back up.
485 async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG,
486 "Zygote failed to write to USAP pool event FD: %s",
487 strerror(errno));
488 kill(getpid(), SIGKILL);
489 }
490 }
491
492 errno = saved_errno;
493 }
494
495 // Configures the SIGCHLD/SIGHUP handlers for the zygote process. This is
496 // configured very late, because earlier in the runtime we may fork() and
497 // exec() other processes, and we want to waitpid() for those rather than
498 // have them be harvested immediately.
499 //
500 // Ignore SIGHUP because all processes forked by the zygote are in the same
501 // process group as the zygote and we don't want to be notified if we become
502 // an orphaned group and have one or more stopped processes. This is not a
503 // theoretical concern :
504 // - we can become an orphaned group if one of our direct descendants forks
505 // and is subsequently killed before its children.
506 // - crash_dump routinely STOPs the process it's tracing.
507 //
508 // See issues b/71965619 and b/25567761 for further details.
509 //
510 // This ends up being called repeatedly before each fork(), but there's
511 // no real harm in that.
SetSignalHandlers()512 static void SetSignalHandlers() {
513 struct sigaction sig_chld = {.sa_flags = SA_SIGINFO, .sa_sigaction = SigChldHandler};
514
515 if (sigaction(SIGCHLD, &sig_chld, nullptr) < 0) {
516 ALOGW("Error setting SIGCHLD handler: %s", strerror(errno));
517 }
518
519 struct sigaction sig_hup = {};
520 sig_hup.sa_handler = SIG_IGN;
521 if (sigaction(SIGHUP, &sig_hup, nullptr) < 0) {
522 ALOGW("Error setting SIGHUP handler: %s", strerror(errno));
523 }
524 }
525
526 // Sets the SIGCHLD handler back to default behavior in zygote children.
UnsetChldSignalHandler()527 static void UnsetChldSignalHandler() {
528 struct sigaction sa;
529 memset(&sa, 0, sizeof(sa));
530 sa.sa_handler = SIG_DFL;
531
532 if (sigaction(SIGCHLD, &sa, nullptr) < 0) {
533 ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno));
534 }
535 }
536
537 // Calls POSIX setgroups() using the int[] object as an argument.
538 // A nullptr argument is tolerated.
SetGids(JNIEnv * env,jintArray managed_gids,jboolean is_child_zygote,fail_fn_t fail_fn)539 static void SetGids(JNIEnv* env, jintArray managed_gids, jboolean is_child_zygote,
540 fail_fn_t fail_fn) {
541 if (managed_gids == nullptr) {
542 if (is_child_zygote) {
543 // For child zygotes like webview and app zygote, we want to clear out
544 // any supplemental groups the parent zygote had.
545 if (setgroups(0, NULL) == -1) {
546 fail_fn(CREATE_ERROR("Failed to remove supplementary groups for child zygote"));
547 }
548 }
549 return;
550 }
551
552 ScopedIntArrayRO gids(env, managed_gids);
553 if (gids.get() == nullptr) {
554 fail_fn(CREATE_ERROR("Getting gids int array failed"));
555 }
556
557 if (setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])) == -1) {
558 fail_fn(CREATE_ERROR("setgroups failed: %s, gids.size=%zu", strerror(errno), gids.size()));
559 }
560 }
561
ensureInAppMountNamespace(fail_fn_t fail_fn)562 static void ensureInAppMountNamespace(fail_fn_t fail_fn) {
563 if (gInAppMountNamespace) {
564 // In app mount namespace already
565 return;
566 }
567 if (unshare(CLONE_NEWNS) == -1) {
568 fail_fn(CREATE_ERROR("Failed to unshare(): %s", strerror(errno)));
569 }
570 gInAppMountNamespace = true;
571 }
572
573 // Sets the resource limits via setrlimit(2) for the values in the
574 // two-dimensional array of integers that's passed in. The second dimension
575 // contains a tuple of length 3: (resource, rlim_cur, rlim_max). nullptr is
576 // treated as an empty array.
SetRLimits(JNIEnv * env,jobjectArray managed_rlimits,fail_fn_t fail_fn)577 static void SetRLimits(JNIEnv* env, jobjectArray managed_rlimits, fail_fn_t fail_fn) {
578 if (managed_rlimits == nullptr) {
579 return;
580 }
581
582 rlimit rlim;
583 memset(&rlim, 0, sizeof(rlim));
584
585 for (int i = 0; i < env->GetArrayLength(managed_rlimits); ++i) {
586 ScopedLocalRef<jobject>
587 managed_rlimit_object(env, env->GetObjectArrayElement(managed_rlimits, i));
588 ScopedIntArrayRO rlimit_handle(env, reinterpret_cast<jintArray>(managed_rlimit_object.get()));
589
590 if (rlimit_handle.size() != 3) {
591 fail_fn(CREATE_ERROR("rlimits array must have a second dimension of size 3"));
592 }
593
594 rlim.rlim_cur = rlimit_handle[1];
595 rlim.rlim_max = rlimit_handle[2];
596
597 if (setrlimit(rlimit_handle[0], &rlim) == -1) {
598 fail_fn(CREATE_ERROR("setrlimit(%d, {%ld, %ld}) failed",
599 rlimit_handle[0], rlim.rlim_cur, rlim.rlim_max));
600 }
601 }
602 }
603
EnableDebugger()604 static void EnableDebugger() {
605 // To let a non-privileged gdbserver attach to this
606 // process, we must set our dumpable flag.
607 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
608 ALOGE("prctl(PR_SET_DUMPABLE) failed");
609 }
610
611 // A non-privileged native debugger should be able to attach to the debuggable app, even if Yama
612 // is enabled (see kernel/Documentation/security/Yama.txt).
613 if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) {
614 // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this
615 // case since it's expected behaviour.
616 if (errno != EINVAL) {
617 ALOGE("prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed");
618 }
619 }
620
621 // Set the core dump size to zero unless wanted (see also coredump_setup in build/envsetup.sh).
622 if (!GetBoolProperty("persist.zygote.core_dump", false)) {
623 // Set the soft limit on core dump size to 0 without changing the hard limit.
624 rlimit rl;
625 if (getrlimit(RLIMIT_CORE, &rl) == -1) {
626 ALOGE("getrlimit(RLIMIT_CORE) failed");
627 } else {
628 rl.rlim_cur = 0;
629 if (setrlimit(RLIMIT_CORE, &rl) == -1) {
630 ALOGE("setrlimit(RLIMIT_CORE) failed");
631 }
632 }
633 }
634 }
635
PreApplicationInit()636 static void PreApplicationInit() {
637 // The child process sets this to indicate it's not the zygote.
638 android_mallopt(M_SET_ZYGOTE_CHILD, nullptr, 0);
639
640 // Set the jemalloc decay time to 1.
641 mallopt(M_DECAY_TIME, 1);
642 }
643
SetUpSeccompFilter(uid_t uid,bool is_child_zygote)644 static void SetUpSeccompFilter(uid_t uid, bool is_child_zygote) {
645 if (!gIsSecurityEnforced) {
646 ALOGI("seccomp disabled by setenforce 0");
647 return;
648 }
649
650 // Apply system or app filter based on uid.
651 if (uid >= AID_APP_START) {
652 if (is_child_zygote) {
653 set_app_zygote_seccomp_filter();
654 } else {
655 set_app_seccomp_filter();
656 }
657 } else {
658 set_system_seccomp_filter();
659 }
660 }
661
EnableKeepCapabilities(fail_fn_t fail_fn)662 static void EnableKeepCapabilities(fail_fn_t fail_fn) {
663 if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) {
664 fail_fn(CREATE_ERROR("prctl(PR_SET_KEEPCAPS) failed: %s", strerror(errno)));
665 }
666 }
667
DropCapabilitiesBoundingSet(fail_fn_t fail_fn)668 static void DropCapabilitiesBoundingSet(fail_fn_t fail_fn) {
669 for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {;
670 if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) {
671 if (errno == EINVAL) {
672 ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify "
673 "your kernel is compiled with file capabilities support");
674 } else {
675 fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno)));
676 }
677 }
678 }
679 }
680
SetInheritable(uint64_t inheritable,fail_fn_t fail_fn)681 static void SetInheritable(uint64_t inheritable, fail_fn_t fail_fn) {
682 __user_cap_header_struct capheader;
683 memset(&capheader, 0, sizeof(capheader));
684 capheader.version = _LINUX_CAPABILITY_VERSION_3;
685 capheader.pid = 0;
686
687 __user_cap_data_struct capdata[2];
688 if (capget(&capheader, &capdata[0]) == -1) {
689 fail_fn(CREATE_ERROR("capget failed: %s", strerror(errno)));
690 }
691
692 capdata[0].inheritable = inheritable;
693 capdata[1].inheritable = inheritable >> 32;
694
695 if (capset(&capheader, &capdata[0]) == -1) {
696 fail_fn(CREATE_ERROR("capset(inh=%" PRIx64 ") failed: %s", inheritable, strerror(errno)));
697 }
698 }
699
SetCapabilities(uint64_t permitted,uint64_t effective,uint64_t inheritable,fail_fn_t fail_fn)700 static void SetCapabilities(uint64_t permitted, uint64_t effective, uint64_t inheritable,
701 fail_fn_t fail_fn) {
702 __user_cap_header_struct capheader;
703 memset(&capheader, 0, sizeof(capheader));
704 capheader.version = _LINUX_CAPABILITY_VERSION_3;
705 capheader.pid = 0;
706
707 __user_cap_data_struct capdata[2];
708 memset(&capdata, 0, sizeof(capdata));
709 capdata[0].effective = effective;
710 capdata[1].effective = effective >> 32;
711 capdata[0].permitted = permitted;
712 capdata[1].permitted = permitted >> 32;
713 capdata[0].inheritable = inheritable;
714 capdata[1].inheritable = inheritable >> 32;
715
716 if (capset(&capheader, &capdata[0]) == -1) {
717 fail_fn(CREATE_ERROR("capset(perm=%" PRIx64 ", eff=%" PRIx64 ", inh=%" PRIx64 ") "
718 "failed: %s", permitted, effective, inheritable, strerror(errno)));
719 }
720 }
721
SetSchedulerPolicy(fail_fn_t fail_fn,bool is_top_app)722 static void SetSchedulerPolicy(fail_fn_t fail_fn, bool is_top_app) {
723 SchedPolicy policy = is_top_app ? SP_TOP_APP : SP_DEFAULT;
724
725 if (is_top_app && cpusets_enabled()) {
726 errno = -set_cpuset_policy(0, policy);
727 if (errno != 0) {
728 fail_fn(CREATE_ERROR("set_cpuset_policy(0, %d) failed: %s", policy, strerror(errno)));
729 }
730 }
731
732 errno = -set_sched_policy(0, policy);
733 if (errno != 0) {
734 fail_fn(CREATE_ERROR("set_sched_policy(0, %d) failed: %s", policy, strerror(errno)));
735 }
736
737 // We are going to lose the permission to set scheduler policy during the specialization, so make
738 // sure that we don't cache the fd of cgroup path that may cause sepolicy violation by writing
739 // value to the cached fd directly when creating new thread.
740 DropTaskProfilesResourceCaching();
741 }
742
UnmountTree(const char * path)743 static int UnmountTree(const char* path) {
744 ATRACE_CALL();
745
746 size_t path_len = strlen(path);
747
748 FILE* fp = setmntent("/proc/mounts", "r");
749 if (fp == nullptr) {
750 ALOGE("Error opening /proc/mounts: %s", strerror(errno));
751 return -errno;
752 }
753
754 // Some volumes can be stacked on each other, so force unmount in
755 // reverse order to give us the best chance of success.
756 std::list<std::string> to_unmount;
757 mntent* mentry;
758 while ((mentry = getmntent(fp)) != nullptr) {
759 if (strncmp(mentry->mnt_dir, path, path_len) == 0) {
760 to_unmount.push_front(std::string(mentry->mnt_dir));
761 }
762 }
763 endmntent(fp);
764
765 for (const auto& path : to_unmount) {
766 if (umount2(path.c_str(), MNT_DETACH)) {
767 ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
768 }
769 }
770 return 0;
771 }
772
PrepareDir(const std::string & dir,mode_t mode,uid_t uid,gid_t gid,fail_fn_t fail_fn)773 static void PrepareDir(const std::string& dir, mode_t mode, uid_t uid, gid_t gid,
774 fail_fn_t fail_fn) {
775 if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
776 fail_fn(CREATE_ERROR("fs_prepare_dir failed on %s: %s",
777 dir.c_str(), strerror(errno)));
778 }
779 }
780
PrepareDirIfNotPresent(const std::string & dir,mode_t mode,uid_t uid,gid_t gid,fail_fn_t fail_fn)781 static void PrepareDirIfNotPresent(const std::string& dir, mode_t mode, uid_t uid, gid_t gid,
782 fail_fn_t fail_fn) {
783 struct stat sb;
784 if (TEMP_FAILURE_RETRY(stat(dir.c_str(), &sb)) != -1) {
785 // Directory exists already
786 return;
787 }
788 PrepareDir(dir, mode, uid, gid, fail_fn);
789 }
790
BindMount(const std::string & source_dir,const std::string & target_dir)791 static bool BindMount(const std::string& source_dir, const std::string& target_dir) {
792 return !(TEMP_FAILURE_RETRY(mount(source_dir.c_str(), target_dir.c_str(), nullptr,
793 MS_BIND | MS_REC, nullptr)) == -1);
794 }
795
BindMount(const std::string & source_dir,const std::string & target_dir,fail_fn_t fail_fn)796 static void BindMount(const std::string& source_dir, const std::string& target_dir,
797 fail_fn_t fail_fn) {
798 if (!BindMount(source_dir, target_dir)) {
799 fail_fn(CREATE_ERROR("Failed to mount %s to %s: %s",
800 source_dir.c_str(), target_dir.c_str(), strerror(errno)));
801 }
802 }
803
MountAppDataTmpFs(const std::string & target_dir,fail_fn_t fail_fn)804 static void MountAppDataTmpFs(const std::string& target_dir,
805 fail_fn_t fail_fn) {
806 if (TEMP_FAILURE_RETRY(mount("tmpfs", target_dir.c_str(), "tmpfs",
807 MS_NOSUID | MS_NODEV | MS_NOEXEC, "uid=0,gid=0,mode=0751")) == -1) {
808 fail_fn(CREATE_ERROR("Failed to mount tmpfs to %s: %s",
809 target_dir.c_str(), strerror(errno)));
810 }
811 }
812
813 // Create a private mount namespace and bind mount appropriate emulated
814 // storage for the given user.
MountEmulatedStorage(uid_t uid,jint mount_mode,bool force_mount_namespace,fail_fn_t fail_fn)815 static void MountEmulatedStorage(uid_t uid, jint mount_mode,
816 bool force_mount_namespace,
817 fail_fn_t fail_fn) {
818 // See storage config details at http://source.android.com/tech/storage/
819 ATRACE_CALL();
820
821 if (mount_mode < 0 || mount_mode >= MOUNT_EXTERNAL_COUNT) {
822 fail_fn(CREATE_ERROR("Unknown mount_mode: %d", mount_mode));
823 }
824
825 if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace) {
826 // Valid default of no storage visible
827 return;
828 }
829
830 // Create a second private mount namespace for our process
831 ensureInAppMountNamespace(fail_fn);
832
833 // Handle force_mount_namespace with MOUNT_EXTERNAL_NONE.
834 if (mount_mode == MOUNT_EXTERNAL_NONE) {
835 return;
836 }
837
838 const userid_t user_id = multiuser_get_user_id(uid);
839 const std::string user_source = StringPrintf("/mnt/user/%d", user_id);
840 // Shell is neither AID_ROOT nor AID_EVERYBODY. Since it equally needs 'execute' access to
841 // /mnt/user/0 to 'adb shell ls /sdcard' for instance, we set the uid bit of /mnt/user/0 to
842 // AID_SHELL. This gives shell access along with apps running as group everybody (user 0 apps)
843 // These bits should be consistent with what is set in vold in
844 // Utils#MountUserFuse on FUSE volume mount
845 PrepareDir(user_source, 0710, user_id ? AID_ROOT : AID_SHELL,
846 multiuser_get_uid(user_id, AID_EVERYBODY), fail_fn);
847
848 bool isAppDataIsolationEnabled = GetBoolProperty(kVoldAppDataIsolation, false);
849
850 if (mount_mode == MOUNT_EXTERNAL_PASS_THROUGH) {
851 const std::string pass_through_source = StringPrintf("/mnt/pass_through/%d", user_id);
852 PrepareDir(pass_through_source, 0710, AID_ROOT, AID_MEDIA_RW, fail_fn);
853 BindMount(pass_through_source, "/storage", fail_fn);
854 } else if (mount_mode == MOUNT_EXTERNAL_INSTALLER) {
855 const std::string installer_source = StringPrintf("/mnt/installer/%d", user_id);
856 BindMount(installer_source, "/storage", fail_fn);
857 } else if (isAppDataIsolationEnabled && mount_mode == MOUNT_EXTERNAL_ANDROID_WRITABLE) {
858 const std::string writable_source = StringPrintf("/mnt/androidwritable/%d", user_id);
859 BindMount(writable_source, "/storage", fail_fn);
860 } else {
861 BindMount(user_source, "/storage", fail_fn);
862 }
863 }
864
865 // Utility to close down the Zygote socket file descriptors while
866 // the child is still running as root with Zygote's privileges. Each
867 // descriptor (if any) is closed via dup3(), replacing it with a valid
868 // (open) descriptor to /dev/null.
869
DetachDescriptors(JNIEnv * env,const std::vector<int> & fds_to_close,fail_fn_t fail_fn)870 static void DetachDescriptors(JNIEnv* env,
871 const std::vector<int>& fds_to_close,
872 fail_fn_t fail_fn) {
873
874 if (fds_to_close.size() > 0) {
875 android::base::unique_fd devnull_fd(open("/dev/null", O_RDWR | O_CLOEXEC));
876 if (devnull_fd == -1) {
877 fail_fn(std::string("Failed to open /dev/null: ").append(strerror(errno)));
878 }
879
880 for (int fd : fds_to_close) {
881 ALOGV("Switching descriptor %d to /dev/null", fd);
882 if (TEMP_FAILURE_RETRY(dup3(devnull_fd, fd, O_CLOEXEC)) == -1) {
883 fail_fn(StringPrintf("Failed dup3() on descriptor %d: %s", fd, strerror(errno)));
884 }
885 }
886 }
887 }
888
SetThreadName(const std::string & thread_name)889 void SetThreadName(const std::string& thread_name) {
890 bool hasAt = false;
891 bool hasDot = false;
892
893 for (const char str_el : thread_name) {
894 if (str_el == '.') {
895 hasDot = true;
896 } else if (str_el == '@') {
897 hasAt = true;
898 }
899 }
900
901 const char* name_start_ptr = thread_name.c_str();
902 if (thread_name.length() >= MAX_NAME_LENGTH && !hasAt && hasDot) {
903 name_start_ptr += thread_name.length() - MAX_NAME_LENGTH;
904 }
905
906 // pthread_setname_np fails rather than truncating long strings.
907 char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic
908 strlcpy(buf, name_start_ptr, sizeof(buf));
909 errno = pthread_setname_np(pthread_self(), buf);
910 if (errno != 0) {
911 ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno));
912 }
913 // Update base::logging default tag.
914 android::base::SetDefaultTag(buf);
915 }
916
917 /**
918 * A helper method for converting managed strings to native strings. A fatal
919 * error is generated if a problem is encountered in extracting a non-null
920 * string.
921 *
922 * @param env Managed runtime environment
923 * @param process_name A native representation of the process name
924 * @param managed_process_name A managed representation of the process name
925 * @param managed_string The managed string to extract
926 *
927 * @return An empty option if the managed string is null. A optional-wrapped
928 * string otherwise.
929 */
ExtractJString(JNIEnv * env,const char * process_name,jstring managed_process_name,jstring managed_string)930 static std::optional<std::string> ExtractJString(JNIEnv* env,
931 const char* process_name,
932 jstring managed_process_name,
933 jstring managed_string) {
934 if (managed_string == nullptr) {
935 return std::nullopt;
936 } else {
937 ScopedUtfChars scoped_string_chars(env, managed_string);
938
939 if (scoped_string_chars.c_str() != nullptr) {
940 return std::optional<std::string>(scoped_string_chars.c_str());
941 } else {
942 ZygoteFailure(env, process_name, managed_process_name, "Failed to extract JString.");
943 }
944 }
945 }
946
947 /**
948 * A helper method for converting managed string arrays to native vectors. A
949 * fatal error is generated if a problem is encountered in extracting a non-null array.
950 *
951 * @param env Managed runtime environment
952 * @param process_name A native representation of the process name
953 * @param managed_process_name A managed representation of the process name
954 * @param managed_array The managed integer array to extract
955 *
956 * @return An empty option if the managed array is null. A optional-wrapped
957 * vector otherwise.
958 */
ExtractJIntArray(JNIEnv * env,const char * process_name,jstring managed_process_name,jintArray managed_array)959 static std::optional<std::vector<int>> ExtractJIntArray(JNIEnv* env,
960 const char* process_name,
961 jstring managed_process_name,
962 jintArray managed_array) {
963 if (managed_array == nullptr) {
964 return std::nullopt;
965 } else {
966 ScopedIntArrayRO managed_array_handle(env, managed_array);
967
968 if (managed_array_handle.get() != nullptr) {
969 std::vector<int> native_array;
970 native_array.reserve(managed_array_handle.size());
971
972 for (size_t array_index = 0; array_index < managed_array_handle.size(); ++array_index) {
973 native_array.push_back(managed_array_handle[array_index]);
974 }
975
976 return std::move(native_array);
977
978 } else {
979 ZygoteFailure(env, process_name, managed_process_name, "Failed to extract JIntArray.");
980 }
981 }
982 }
983
984 /**
985 * A utility function for blocking signals.
986 *
987 * @param signum Signal number to block
988 * @param fail_fn Fatal error reporting function
989 *
990 * @see ZygoteFailure
991 */
BlockSignal(int signum,fail_fn_t fail_fn)992 static void BlockSignal(int signum, fail_fn_t fail_fn) {
993 sigset_t sigs;
994 sigemptyset(&sigs);
995 sigaddset(&sigs, signum);
996
997 if (sigprocmask(SIG_BLOCK, &sigs, nullptr) == -1) {
998 fail_fn(CREATE_ERROR("Failed to block signal %s: %s", strsignal(signum), strerror(errno)));
999 }
1000 }
1001
1002
1003 /**
1004 * A utility function for unblocking signals.
1005 *
1006 * @param signum Signal number to unblock
1007 * @param fail_fn Fatal error reporting function
1008 *
1009 * @see ZygoteFailure
1010 */
UnblockSignal(int signum,fail_fn_t fail_fn)1011 static void UnblockSignal(int signum, fail_fn_t fail_fn) {
1012 sigset_t sigs;
1013 sigemptyset(&sigs);
1014 sigaddset(&sigs, signum);
1015
1016 if (sigprocmask(SIG_UNBLOCK, &sigs, nullptr) == -1) {
1017 fail_fn(CREATE_ERROR("Failed to un-block signal %s: %s", strsignal(signum), strerror(errno)));
1018 }
1019 }
1020
ClearUsapTable()1021 static void ClearUsapTable() {
1022 for (UsapTableEntry& entry : gUsapTable) {
1023 entry.Clear();
1024 }
1025
1026 gUsapPoolCount = 0;
1027 }
1028
1029 // Create an app data directory over tmpfs overlayed CE / DE storage, and bind mount it
1030 // from the actual app data directory in data mirror.
createAndMountAppData(std::string_view package_name,std::string_view mirror_pkg_dir_name,std::string_view mirror_data_path,std::string_view actual_data_path,fail_fn_t fail_fn,bool call_fail_fn)1031 static bool createAndMountAppData(std::string_view package_name,
1032 std::string_view mirror_pkg_dir_name, std::string_view mirror_data_path,
1033 std::string_view actual_data_path, fail_fn_t fail_fn, bool call_fail_fn) {
1034
1035 char mirrorAppDataPath[PATH_MAX];
1036 char actualAppDataPath[PATH_MAX];
1037 snprintf(mirrorAppDataPath, PATH_MAX, "%s/%s", mirror_data_path.data(),
1038 mirror_pkg_dir_name.data());
1039 snprintf(actualAppDataPath, PATH_MAX, "%s/%s", actual_data_path.data(), package_name.data());
1040
1041 PrepareDir(actualAppDataPath, 0700, AID_ROOT, AID_ROOT, fail_fn);
1042
1043 // Bind mount from original app data directory in mirror.
1044 if (call_fail_fn) {
1045 BindMount(mirrorAppDataPath, actualAppDataPath, fail_fn);
1046 } else if(!BindMount(mirrorAppDataPath, actualAppDataPath)) {
1047 ALOGW("Failed to mount %s to %s: %s",
1048 mirrorAppDataPath, actualAppDataPath, strerror(errno));
1049 return false;
1050 }
1051 return true;
1052 }
1053
1054 // There is an app data directory over tmpfs overlaid CE / DE storage
1055 // bind mount it from the actual app data directory in data mirror.
mountAppData(std::string_view package_name,std::string_view mirror_pkg_dir_name,std::string_view mirror_data_path,std::string_view actual_data_path,fail_fn_t fail_fn)1056 static void mountAppData(std::string_view package_name,
1057 std::string_view mirror_pkg_dir_name, std::string_view mirror_data_path,
1058 std::string_view actual_data_path, fail_fn_t fail_fn) {
1059
1060 char mirrorAppDataPath[PATH_MAX];
1061 char actualAppDataPath[PATH_MAX];
1062 snprintf(mirrorAppDataPath, PATH_MAX, "%s/%s", mirror_data_path.data(),
1063 mirror_pkg_dir_name.data());
1064 snprintf(actualAppDataPath, PATH_MAX, "%s/%s", actual_data_path.data(), package_name.data());
1065
1066 // Bind mount from original app data directory in mirror.
1067 BindMount(mirrorAppDataPath, actualAppDataPath, fail_fn);
1068 }
1069
1070 // Get the directory name stored in /data/data. If device is unlocked it should be the same as
1071 // package name, otherwise it will be an encrypted name but with same inode number.
getAppDataDirName(std::string_view parent_path,std::string_view package_name,long long ce_data_inode,fail_fn_t fail_fn)1072 static std::string getAppDataDirName(std::string_view parent_path, std::string_view package_name,
1073 long long ce_data_inode, fail_fn_t fail_fn) {
1074 // Check if directory exists
1075 char tmpPath[PATH_MAX];
1076 snprintf(tmpPath, PATH_MAX, "%s/%s", parent_path.data(), package_name.data());
1077 struct stat s;
1078 int err = stat(tmpPath, &s);
1079 if (err == 0) {
1080 // Directory exists, so return the directory name
1081 return package_name.data();
1082 } else {
1083 if (errno != ENOENT) {
1084 fail_fn(CREATE_ERROR("Unexpected error in getAppDataDirName: %s", strerror(errno)));
1085 return nullptr;
1086 }
1087 {
1088 // Directory doesn't exist, try to search the name from inode
1089 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(parent_path.data()), closedir);
1090 if (dir == nullptr) {
1091 fail_fn(CREATE_ERROR("Failed to opendir %s", parent_path.data()));
1092 }
1093 struct dirent* ent;
1094 while ((ent = readdir(dir.get()))) {
1095 if (ent->d_ino == ce_data_inode) {
1096 return ent->d_name;
1097 }
1098 }
1099 }
1100
1101 // Fallback due to b/145989852, ce_data_inode stored in package manager may be corrupted
1102 // if ino_t is 32 bits.
1103 ino_t fixed_ce_data_inode = 0;
1104 if ((ce_data_inode & UPPER_HALF_WORD_MASK) == UPPER_HALF_WORD_MASK) {
1105 fixed_ce_data_inode = ce_data_inode & LOWER_HALF_WORD_MASK;
1106 } else if ((ce_data_inode & LOWER_HALF_WORD_MASK) == LOWER_HALF_WORD_MASK) {
1107 fixed_ce_data_inode = ((ce_data_inode >> 32) & LOWER_HALF_WORD_MASK);
1108 }
1109 if (fixed_ce_data_inode != 0) {
1110 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(parent_path.data()), closedir);
1111 if (dir == nullptr) {
1112 fail_fn(CREATE_ERROR("Failed to opendir %s", parent_path.data()));
1113 }
1114 struct dirent* ent;
1115 while ((ent = readdir(dir.get()))) {
1116 if (ent->d_ino == fixed_ce_data_inode) {
1117 long long d_ino = ent->d_ino;
1118 ALOGW("Fallback success inode %lld -> %lld", ce_data_inode, d_ino);
1119 return ent->d_name;
1120 }
1121 }
1122 }
1123 // Fallback done
1124 ALOGW("Unable to find %s:%lld in %s", package_name.data(), ce_data_inode, parent_path.data());
1125 return "";
1126 }
1127 }
1128
1129 // Isolate app's data directory, by mounting a tmpfs on CE DE storage,
1130 // and create and bind mount app data in related_packages.
isolateAppDataPerPackage(int userId,std::string_view package_name,std::string_view volume_uuid,long long ce_data_inode,std::string_view actualCePath,std::string_view actualDePath,fail_fn_t fail_fn)1131 static void isolateAppDataPerPackage(int userId, std::string_view package_name,
1132 std::string_view volume_uuid, long long ce_data_inode, std::string_view actualCePath,
1133 std::string_view actualDePath, fail_fn_t fail_fn) {
1134
1135 char mirrorCePath[PATH_MAX];
1136 char mirrorDePath[PATH_MAX];
1137 char mirrorCeParent[PATH_MAX];
1138 snprintf(mirrorCeParent, PATH_MAX, "/data_mirror/data_ce/%s", volume_uuid.data());
1139 snprintf(mirrorCePath, PATH_MAX, "%s/%d", mirrorCeParent, userId);
1140 snprintf(mirrorDePath, PATH_MAX, "/data_mirror/data_de/%s/%d", volume_uuid.data(), userId);
1141
1142 createAndMountAppData(package_name, package_name, mirrorDePath, actualDePath, fail_fn,
1143 true /*call_fail_fn*/);
1144
1145 std::string ce_data_path = getAppDataDirName(mirrorCePath, package_name, ce_data_inode, fail_fn);
1146 if (ce_data_path.empty()) {
1147 ALOGE("Ignoring missing CE app data dir for %s\n", package_name.data());
1148 return;
1149 }
1150 if (!createAndMountAppData(package_name, ce_data_path, mirrorCePath, actualCePath, fail_fn,
1151 false /*call_fail_fn*/)) {
1152 // CE might unlocks and the name is decrypted
1153 // get the name and mount again
1154 ce_data_path=getAppDataDirName(mirrorCePath, package_name, ce_data_inode, fail_fn);
1155 if (ce_data_path.empty()) {
1156 ALOGE("Ignoring missing CE app data dir for %s\n", package_name.data());
1157 return;
1158 }
1159 mountAppData(package_name, ce_data_path, mirrorCePath, actualCePath, fail_fn);
1160 }
1161 }
1162
1163 // Relabel directory
relabelDir(const char * path,const char * context,fail_fn_t fail_fn)1164 static void relabelDir(const char* path, const char* context, fail_fn_t fail_fn) {
1165 if (setfilecon(path, context) != 0) {
1166 fail_fn(CREATE_ERROR("Failed to setfilecon %s %s", path, strerror(errno)));
1167 }
1168 }
1169
1170 // Relabel the subdirectories and symlinks in the given directory, non-recursively.
relabelSubdirs(const char * path,const char * context,fail_fn_t fail_fn)1171 static void relabelSubdirs(const char* path, const char* context, fail_fn_t fail_fn) {
1172 DIR* dir = opendir(path);
1173 if (dir == nullptr) {
1174 fail_fn(CREATE_ERROR("Failed to opendir %s", path));
1175 }
1176 struct dirent* ent;
1177 while ((ent = readdir(dir))) {
1178 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
1179 auto filePath = StringPrintf("%s/%s", path, ent->d_name);
1180 if (ent->d_type == DT_DIR) {
1181 relabelDir(filePath.c_str(), context, fail_fn);
1182 } else if (ent->d_type == DT_LNK) {
1183 if (lsetfilecon(filePath.c_str(), context) != 0) {
1184 fail_fn(CREATE_ERROR("Failed to lsetfilecon %s %s", filePath.c_str(), strerror(errno)));
1185 }
1186 } else {
1187 fail_fn(CREATE_ERROR("Unexpected type: %d %s", ent->d_type, filePath.c_str()));
1188 }
1189 }
1190 closedir(dir);
1191 }
1192
1193 /**
1194 * Hide the CE and DE data directories of non-related apps.
1195 *
1196 * Without this, apps can detect if any app is installed by trying to "touch" the app's CE
1197 * or DE data directory, e.g. /data/data/com.whatsapp. This fails with EACCES if the app
1198 * is installed, or ENOENT if it's not. Traditional file permissions or SELinux can only
1199 * block accessing those directories but can't fix fingerprinting like this.
1200 *
1201 * Instead, we hide non-related apps' data directories from the filesystem entirely by
1202 * mounting tmpfs instances over their parent directories and bind-mounting in just the
1203 * needed app data directories. This is done in a private mount namespace.
1204 *
1205 * Steps:
1206 * (1) Collect a list of all related apps (apps with same uid and allowlisted apps) data info
1207 * (package name, data stored volume uuid, and inode number of its CE data directory)
1208 * (2) Mount tmpfs on /data/data and /data/user{,_de}, and on /mnt/expand/$volume/user{,_de}
1209 * for all adoptable storage volumes. This hides all app data directories.
1210 * (3) For each related app, create stubs for its data directories in the relevant tmpfs
1211 * instances, then bind mount in the actual directories from /data_mirror. This works
1212 * for both the CE and DE directories. DE storage is always unlocked, whereas the
1213 * app's CE directory can be found via inode number if CE storage is locked.
1214 *
1215 * Example assuming user 0, app "com.android.foo", no shared uid, and no adoptable storage:
1216 * (1) Info = ["com.android.foo", "null" (volume uuid "null"=default), "123456" (inode number)]
1217 * (2) Mount tmpfs on /data/data, /data/user, and /data/user_de.
1218 * (3) For DE storage, create a directory /data/user_de/0/com.android.foo and bind mount
1219 * /data_mirror/data_de/0/com.android.foo onto it.
1220 * (4) Do similar for CE storage. But if the device is in direct boot mode, then CE
1221 * storage will be locked, so the app's CE data directory won't exist at the usual
1222 * path /data_mirror/data_ce/0/com.android.foo. It will still exist in
1223 * /data_mirror/data_ce/0, but its filename will be an unpredictable no-key name. In
1224 * this case, we use the inode number to find the right directory instead. Note that
1225 * the bind-mounted app CE data directory will remain locked. It will be unlocked
1226 * automatically if/when the user's CE storage is unlocked, since adding an encryption
1227 * key takes effect on a whole filesystem instance including all its mounts.
1228 */
isolateAppData(JNIEnv * env,const std::vector<std::string> & merged_data_info_list,uid_t uid,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1229 static void isolateAppData(JNIEnv* env, const std::vector<std::string>& merged_data_info_list,
1230 uid_t uid, const char* process_name,
1231 jstring managed_nice_name, fail_fn_t fail_fn) {
1232
1233 const userid_t userId = multiuser_get_user_id(uid);
1234
1235 int size = merged_data_info_list.size();
1236
1237 // Mount tmpfs on all possible data directories, so app no longer see the original apps data.
1238 char internalCePath[PATH_MAX];
1239 char internalLegacyCePath[PATH_MAX];
1240 char internalDePath[PATH_MAX];
1241 char externalPrivateMountPath[PATH_MAX];
1242
1243 snprintf(internalCePath, PATH_MAX, "/data/user");
1244 snprintf(internalLegacyCePath, PATH_MAX, "/data/data");
1245 snprintf(internalDePath, PATH_MAX, "/data/user_de");
1246 snprintf(externalPrivateMountPath, PATH_MAX, "/mnt/expand");
1247
1248 // Get the "u:object_r:system_userdir_file:s0" security context. This can be
1249 // gotten from several different places; we use /data/user.
1250 char* dataUserdirContext = nullptr;
1251 if (getfilecon(internalCePath, &dataUserdirContext) < 0) {
1252 fail_fn(CREATE_ERROR("Unable to getfilecon on %s %s", internalCePath,
1253 strerror(errno)));
1254 }
1255 // Get the "u:object_r:system_data_file:s0" security context. This can be
1256 // gotten from several different places; we use /data/misc.
1257 char* dataFileContext = nullptr;
1258 if (getfilecon("/data/misc", &dataFileContext) < 0) {
1259 fail_fn(CREATE_ERROR("Unable to getfilecon on /data/misc %s", strerror(errno)));
1260 }
1261
1262 MountAppDataTmpFs(internalLegacyCePath, fail_fn);
1263 MountAppDataTmpFs(internalCePath, fail_fn);
1264 MountAppDataTmpFs(internalDePath, fail_fn);
1265
1266 // Mount tmpfs on all external vols DE and CE storage
1267 DIR* dir = opendir(externalPrivateMountPath);
1268 if (dir == nullptr) {
1269 fail_fn(CREATE_ERROR("Failed to opendir %s", externalPrivateMountPath));
1270 }
1271 struct dirent* ent;
1272 while ((ent = readdir(dir))) {
1273 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
1274 if (ent->d_type != DT_DIR) {
1275 fail_fn(CREATE_ERROR("Unexpected type: %d %s", ent->d_type, ent->d_name));
1276 }
1277 auto volPath = StringPrintf("%s/%s", externalPrivateMountPath, ent->d_name);
1278 auto cePath = StringPrintf("%s/user", volPath.c_str());
1279 auto dePath = StringPrintf("%s/user_de", volPath.c_str());
1280 // Wait until dir user is created.
1281 WaitUntilDirReady(cePath.c_str(), fail_fn);
1282 MountAppDataTmpFs(cePath.c_str(), fail_fn);
1283 // Wait until dir user_de is created.
1284 WaitUntilDirReady(dePath.c_str(), fail_fn);
1285 MountAppDataTmpFs(dePath.c_str(), fail_fn);
1286 }
1287 closedir(dir);
1288
1289 // No bind mounting of app data should occur in the case of a sandbox process since SDK sandboxes
1290 // should not be able to read app data. Tmpfs was mounted however since a sandbox should not have
1291 // access to app data.
1292 appid_t appId = multiuser_get_app_id(uid);
1293 bool isSdkSandboxProcess =
1294 (appId >= AID_SDK_SANDBOX_PROCESS_START && appId <= AID_SDK_SANDBOX_PROCESS_END);
1295 if (!isSdkSandboxProcess) {
1296 // Prepare default dirs for user 0 as user 0 always exists.
1297 int result = symlink("/data/data", "/data/user/0");
1298 if (result != 0) {
1299 fail_fn(CREATE_ERROR("Failed to create symlink /data/user/0 %s", strerror(errno)));
1300 }
1301 PrepareDirIfNotPresent("/data/user_de/0", DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1302 fail_fn);
1303
1304 for (int i = 0; i < size; i += 3) {
1305 std::string const& packageName = merged_data_info_list[i];
1306 std::string const& volUuid = merged_data_info_list[i + 1];
1307 std::string const& inode = merged_data_info_list[i + 2];
1308
1309 std::string::size_type sz;
1310 long long ceDataInode = std::stoll(inode, &sz);
1311
1312 std::string actualCePath, actualDePath;
1313 if (volUuid.compare("null") != 0) {
1314 // Volume that is stored in /mnt/expand
1315 char volPath[PATH_MAX];
1316 char volCePath[PATH_MAX];
1317 char volDePath[PATH_MAX];
1318 char volCeUserPath[PATH_MAX];
1319 char volDeUserPath[PATH_MAX];
1320
1321 snprintf(volPath, PATH_MAX, "/mnt/expand/%s", volUuid.c_str());
1322 snprintf(volCePath, PATH_MAX, "%s/user", volPath);
1323 snprintf(volDePath, PATH_MAX, "%s/user_de", volPath);
1324 snprintf(volCeUserPath, PATH_MAX, "%s/%d", volCePath, userId);
1325 snprintf(volDeUserPath, PATH_MAX, "%s/%d", volDePath, userId);
1326
1327 PrepareDirIfNotPresent(volPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1328 fail_fn);
1329 PrepareDirIfNotPresent(volCePath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1330 fail_fn);
1331 PrepareDirIfNotPresent(volDePath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1332 fail_fn);
1333 PrepareDirIfNotPresent(volCeUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1334 fail_fn);
1335 PrepareDirIfNotPresent(volDeUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT,
1336 fail_fn);
1337
1338 actualCePath = volCeUserPath;
1339 actualDePath = volDeUserPath;
1340 } else {
1341 // Internal volume that stored in /data
1342 char internalCeUserPath[PATH_MAX];
1343 char internalDeUserPath[PATH_MAX];
1344 snprintf(internalCeUserPath, PATH_MAX, "/data/user/%d", userId);
1345 snprintf(internalDeUserPath, PATH_MAX, "/data/user_de/%d", userId);
1346 // If it's not user 0, create /data/user/$USER.
1347 if (userId == 0) {
1348 actualCePath = internalLegacyCePath;
1349 } else {
1350 PrepareDirIfNotPresent(internalCeUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT,
1351 AID_ROOT, fail_fn);
1352 actualCePath = internalCeUserPath;
1353 }
1354 PrepareDirIfNotPresent(internalDeUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT,
1355 AID_ROOT, fail_fn);
1356 actualDePath = internalDeUserPath;
1357 }
1358 isolateAppDataPerPackage(userId, packageName, volUuid, ceDataInode, actualCePath,
1359 actualDePath, fail_fn);
1360 }
1361 }
1362
1363 // We set the label AFTER everything is done, as we are applying
1364 // the file operations on tmpfs. If we set the label when we mount
1365 // tmpfs, SELinux will not happy as we are changing system_data_files.
1366 // Relabel dir under /data/user, including /data/user/0
1367 relabelSubdirs(internalCePath, dataFileContext, fail_fn);
1368
1369 // Relabel /data/user
1370 relabelDir(internalCePath, dataUserdirContext, fail_fn);
1371
1372 // Relabel /data/data
1373 relabelDir(internalLegacyCePath, dataFileContext, fail_fn);
1374
1375 // Relabel subdirectories of /data/user_de
1376 relabelSubdirs(internalDePath, dataFileContext, fail_fn);
1377
1378 // Relabel /data/user_de
1379 relabelDir(internalDePath, dataUserdirContext, fail_fn);
1380
1381 // Relabel CE and DE dirs under /mnt/expand
1382 dir = opendir(externalPrivateMountPath);
1383 if (dir == nullptr) {
1384 fail_fn(CREATE_ERROR("Failed to opendir %s", externalPrivateMountPath));
1385 }
1386 while ((ent = readdir(dir))) {
1387 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
1388 auto volPath = StringPrintf("%s/%s", externalPrivateMountPath, ent->d_name);
1389 auto cePath = StringPrintf("%s/user", volPath.c_str());
1390 auto dePath = StringPrintf("%s/user_de", volPath.c_str());
1391
1392 relabelSubdirs(cePath.c_str(), dataFileContext, fail_fn);
1393 relabelDir(cePath.c_str(), dataUserdirContext, fail_fn);
1394 relabelSubdirs(dePath.c_str(), dataFileContext, fail_fn);
1395 relabelDir(dePath.c_str(), dataUserdirContext, fail_fn);
1396 }
1397 closedir(dir);
1398
1399 freecon(dataUserdirContext);
1400 freecon(dataFileContext);
1401 }
1402
1403 /**
1404 * Without sdk sandbox data isolation, the sandbox could detect if another app is installed on the
1405 * system by "touching" other data directories like /data/misc_ce/0/sdksandbox/com.whatsapp, similar
1406 * to apps without app data isolation (see {@link #isolateAppData()}).
1407 *
1408 * To prevent this, tmpfs is mounted onto misc_ce and misc_de directories on all possible volumes in
1409 * a separate mount namespace. The sandbox directory path is then created containing the name of the
1410 * client app package associated with the sdk sandbox. The contents for this (sdk level storage and
1411 * shared sdk storage) are bind mounted from the sandbox data mirror.
1412 */
isolateSdkSandboxData(JNIEnv * env,jobjectArray pkg_data_info_list,uid_t uid,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1413 static void isolateSdkSandboxData(JNIEnv* env, jobjectArray pkg_data_info_list, uid_t uid,
1414 const char* process_name, jstring managed_nice_name,
1415 fail_fn_t fail_fn) {
1416 const userid_t userId = multiuser_get_user_id(uid);
1417
1418 int size = (pkg_data_info_list != nullptr) ? env->GetArrayLength(pkg_data_info_list) : 0;
1419 // The sandbox should only have information of one associated client app (package, uuid, inode)
1420 if (size != 3) {
1421 fail_fn(CREATE_ERROR(
1422 "Unable to isolate sandbox data, incorrect associated app information"));
1423 }
1424
1425 auto extract_fn = [env, process_name, managed_nice_name,
1426 pkg_data_info_list](int info_list_idx) {
1427 jstring jstr = (jstring)(env->GetObjectArrayElement(pkg_data_info_list, info_list_idx));
1428 return ExtractJString(env, process_name, managed_nice_name, jstr).value();
1429 };
1430 std::string packageName = extract_fn(0);
1431 std::string volUuid = extract_fn(1);
1432
1433 char internalCePath[PATH_MAX];
1434 char internalDePath[PATH_MAX];
1435 char externalPrivateMountPath[PATH_MAX];
1436 snprintf(internalCePath, PATH_MAX, "/data/misc_ce");
1437 snprintf(internalDePath, PATH_MAX, "/data/misc_de");
1438 snprintf(externalPrivateMountPath, PATH_MAX, "/mnt/expand");
1439
1440 char ceUserPath[PATH_MAX];
1441 char deUserPath[PATH_MAX];
1442 if (volUuid != "null") {
1443 snprintf(ceUserPath, PATH_MAX, "%s/%s/misc_ce/%d", externalPrivateMountPath,
1444 volUuid.c_str(), userId);
1445 snprintf(deUserPath, PATH_MAX, "%s/%s/misc_de/%d", externalPrivateMountPath,
1446 volUuid.c_str(), userId);
1447 } else {
1448 snprintf(ceUserPath, PATH_MAX, "%s/%d", internalCePath, userId);
1449 snprintf(deUserPath, PATH_MAX, "%s/%d", internalDePath, userId);
1450 }
1451
1452 char ceSandboxPath[PATH_MAX];
1453 char deSandboxPath[PATH_MAX];
1454 snprintf(ceSandboxPath, PATH_MAX, "%s/sdksandbox", ceUserPath);
1455 snprintf(deSandboxPath, PATH_MAX, "%s/sdksandbox", deUserPath);
1456
1457 // If the client app using the sandbox has been installed when the device is locked and the
1458 // sandbox starts up when the device is locked, sandbox storage might not have been created.
1459 // In that case, mount tmpfs for data isolation, but don't bind mount.
1460 bool bindMountCeSandboxDataDirs = true;
1461 bool bindMountDeSandboxDataDirs = true;
1462 if (access(ceSandboxPath, F_OK) != 0) {
1463 bindMountCeSandboxDataDirs = false;
1464 }
1465 if (access(deSandboxPath, F_OK) != 0) {
1466 bindMountDeSandboxDataDirs = false;
1467 }
1468
1469 char* context = nullptr;
1470 char* userContext = nullptr;
1471 char* sandboxContext = nullptr;
1472 if (getfilecon(internalDePath, &context) < 0) {
1473 fail_fn(CREATE_ERROR("Unable to getfilecon on %s %s", internalDePath, strerror(errno)));
1474 }
1475 if (bindMountDeSandboxDataDirs) {
1476 if (getfilecon(deUserPath, &userContext) < 0) {
1477 fail_fn(CREATE_ERROR("Unable to getfilecon on %s %s", deUserPath, strerror(errno)));
1478 }
1479 if (getfilecon(deSandboxPath, &sandboxContext) < 0) {
1480 fail_fn(CREATE_ERROR("Unable to getfilecon on %s %s", deSandboxPath, strerror(errno)));
1481 }
1482 }
1483
1484 MountAppDataTmpFs(internalCePath, fail_fn);
1485 MountAppDataTmpFs(internalDePath, fail_fn);
1486
1487 // Mount tmpfs on all external volumes
1488 DIR* dir = opendir(externalPrivateMountPath);
1489 if (dir == nullptr) {
1490 fail_fn(CREATE_ERROR("Failed to opendir %s", externalPrivateMountPath));
1491 }
1492 struct dirent* ent;
1493 while ((ent = readdir(dir))) {
1494 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
1495 if (ent->d_type != DT_DIR) {
1496 fail_fn(CREATE_ERROR("Unexpected type: %d %s", ent->d_type, ent->d_name));
1497 }
1498 auto volPath = StringPrintf("%s/%s", externalPrivateMountPath, ent->d_name);
1499 auto externalCePath = StringPrintf("%s/misc_ce", volPath.c_str());
1500 auto externalDePath = StringPrintf("%s/misc_de", volPath.c_str());
1501
1502 WaitUntilDirReady(externalCePath.c_str(), fail_fn);
1503 MountAppDataTmpFs(externalCePath.c_str(), fail_fn);
1504 WaitUntilDirReady(externalDePath.c_str(), fail_fn);
1505 MountAppDataTmpFs(externalDePath.c_str(), fail_fn);
1506 }
1507 closedir(dir);
1508
1509 char mirrorCeSandboxPath[PATH_MAX];
1510 char mirrorDeSandboxPath[PATH_MAX];
1511 snprintf(mirrorCeSandboxPath, PATH_MAX, "/data_mirror/misc_ce/%s/%d/sdksandbox",
1512 volUuid.c_str(), userId);
1513 snprintf(mirrorDeSandboxPath, PATH_MAX, "/data_mirror/misc_de/%s/%d/sdksandbox",
1514 volUuid.c_str(), userId);
1515
1516 if (bindMountCeSandboxDataDirs) {
1517 PrepareDir(ceUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT, fail_fn);
1518 PrepareDir(ceSandboxPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT, fail_fn);
1519 // TODO(b/231322885): Use inode numbers to find the correct app path when the device locked.
1520 createAndMountAppData(packageName, packageName, mirrorCeSandboxPath, ceSandboxPath, fail_fn,
1521 true /*call_fail_fn*/);
1522
1523 relabelDir(ceSandboxPath, sandboxContext, fail_fn);
1524 relabelDir(ceUserPath, userContext, fail_fn);
1525 }
1526 if (bindMountDeSandboxDataDirs) {
1527 PrepareDir(deUserPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT, fail_fn);
1528 PrepareDir(deSandboxPath, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT, fail_fn);
1529 createAndMountAppData(packageName, packageName, mirrorDeSandboxPath, deSandboxPath, fail_fn,
1530 true /*call_fail_fn*/);
1531
1532 relabelDir(deSandboxPath, sandboxContext, fail_fn);
1533 relabelDir(deUserPath, userContext, fail_fn);
1534 }
1535
1536 // We set the label AFTER everything is done, as we are applying
1537 // the file operations on tmpfs. If we set the label when we mount
1538 // tmpfs, SELinux will not happy as we are changing system_data_files.
1539 relabelDir(internalCePath, context, fail_fn);
1540 relabelDir(internalDePath, context, fail_fn);
1541
1542 // Relabel CE and DE dirs under /mnt/expand
1543 dir = opendir(externalPrivateMountPath);
1544 if (dir == nullptr) {
1545 fail_fn(CREATE_ERROR("Failed to opendir %s", externalPrivateMountPath));
1546 }
1547 while ((ent = readdir(dir))) {
1548 if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue;
1549 auto volPath = StringPrintf("%s/%s", externalPrivateMountPath, ent->d_name);
1550 auto externalCePath = StringPrintf("%s/misc_ce", volPath.c_str());
1551 auto externalDePath = StringPrintf("%s/misc_de", volPath.c_str());
1552 relabelDir(externalCePath.c_str(), context, fail_fn);
1553 relabelDir(externalDePath.c_str(), context, fail_fn);
1554 }
1555 closedir(dir);
1556
1557 if (bindMountDeSandboxDataDirs) {
1558 freecon(sandboxContext);
1559 freecon(userContext);
1560 }
1561 freecon(context);
1562 }
1563
insertPackagesToMergedList(JNIEnv * env,std::vector<std::string> & merged_data_info_list,jobjectArray data_info_list,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1564 static void insertPackagesToMergedList(JNIEnv* env,
1565 std::vector<std::string>& merged_data_info_list,
1566 jobjectArray data_info_list, const char* process_name,
1567 jstring managed_nice_name, fail_fn_t fail_fn) {
1568
1569 auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1);
1570
1571 int size = (data_info_list != nullptr) ? env->GetArrayLength(data_info_list) : 0;
1572 // Size should be a multiple of 3, as it contains list of <package_name, volume_uuid, inode>
1573 if ((size % 3) != 0) {
1574 fail_fn(CREATE_ERROR("Wrong data_info_list size %d", size));
1575 }
1576
1577 for (int i = 0; i < size; i += 3) {
1578 jstring package_str = (jstring) (env->GetObjectArrayElement(data_info_list, i));
1579 std::string packageName = extract_fn(package_str).value();
1580 merged_data_info_list.push_back(packageName);
1581
1582 jstring vol_str = (jstring) (env->GetObjectArrayElement(data_info_list, i + 1));
1583 std::string volUuid = extract_fn(vol_str).value();
1584 merged_data_info_list.push_back(volUuid);
1585
1586 jstring inode_str = (jstring) (env->GetObjectArrayElement(data_info_list, i + 2));
1587 std::string inode = extract_fn(inode_str).value();
1588 merged_data_info_list.push_back(inode);
1589 }
1590 }
1591
isolateAppData(JNIEnv * env,jobjectArray pkg_data_info_list,jobjectArray allowlisted_data_info_list,uid_t uid,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1592 static void isolateAppData(JNIEnv* env, jobjectArray pkg_data_info_list,
1593 jobjectArray allowlisted_data_info_list, uid_t uid,
1594 const char* process_name, jstring managed_nice_name, fail_fn_t fail_fn) {
1595 std::vector<std::string> merged_data_info_list;
1596 insertPackagesToMergedList(env, merged_data_info_list, pkg_data_info_list, process_name,
1597 managed_nice_name, fail_fn);
1598 insertPackagesToMergedList(env, merged_data_info_list, allowlisted_data_info_list, process_name,
1599 managed_nice_name, fail_fn);
1600
1601 isolateAppData(env, merged_data_info_list, uid, process_name, managed_nice_name, fail_fn);
1602 }
1603
1604 /**
1605 * Like isolateAppData(), isolate jit profile directories, so apps don't see what
1606 * other apps are installed by reading content inside /data/misc/profiles/cur.
1607 *
1608 * The implementation is similar to isolateAppData(), it creates a tmpfs
1609 * on /data/misc/profiles/cur, and bind mounts related package profiles to it.
1610 */
isolateJitProfile(JNIEnv * env,jobjectArray pkg_data_info_list,uid_t uid,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1611 static void isolateJitProfile(JNIEnv* env, jobjectArray pkg_data_info_list,
1612 uid_t uid, const char* process_name, jstring managed_nice_name,
1613 fail_fn_t fail_fn) {
1614
1615 auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1);
1616 const userid_t user_id = multiuser_get_user_id(uid);
1617
1618 int size = (pkg_data_info_list != nullptr) ? env->GetArrayLength(pkg_data_info_list) : 0;
1619 // Size should be a multiple of 3, as it contains list of <package_name, volume_uuid, inode>
1620 if ((size % 3) != 0) {
1621 fail_fn(CREATE_ERROR("Wrong pkg_inode_list size %d", size));
1622 }
1623
1624 // Mount (namespace) tmpfs on profile directory, so apps no longer access
1625 // the original profile directory anymore.
1626 MountAppDataTmpFs(kCurProfileDirPath, fail_fn);
1627 MountAppDataTmpFs(kRefProfileDirPath, fail_fn);
1628
1629 // Sandbox processes do not have JIT profile, so no data needs to be bind mounted. However, it
1630 // should still not have access to JIT profile, so tmpfs is mounted.
1631 appid_t appId = multiuser_get_app_id(uid);
1632 if (appId >= AID_SDK_SANDBOX_PROCESS_START && appId <= AID_SDK_SANDBOX_PROCESS_END) {
1633 return;
1634 }
1635
1636 // Create profile directory for this user.
1637 std::string actualCurUserProfile = StringPrintf("%s/%d", kCurProfileDirPath, user_id);
1638 PrepareDir(actualCurUserProfile, DEFAULT_DATA_DIR_PERMISSION, AID_ROOT, AID_ROOT, fail_fn);
1639
1640 for (int i = 0; i < size; i += 3) {
1641 jstring package_str = (jstring) (env->GetObjectArrayElement(pkg_data_info_list, i));
1642 std::string packageName = extract_fn(package_str).value();
1643
1644 std::string actualCurPackageProfile = StringPrintf("%s/%s", actualCurUserProfile.c_str(),
1645 packageName.c_str());
1646 std::string mirrorCurPackageProfile = StringPrintf("/data_mirror/cur_profiles/%d/%s",
1647 user_id, packageName.c_str());
1648 std::string actualRefPackageProfile = StringPrintf("%s/%s", kRefProfileDirPath,
1649 packageName.c_str());
1650 std::string mirrorRefPackageProfile = StringPrintf("/data_mirror/ref_profiles/%s",
1651 packageName.c_str());
1652
1653 if (access(mirrorCurPackageProfile.c_str(), F_OK) != 0) {
1654 ALOGW("Can't access app profile directory: %s", mirrorCurPackageProfile.c_str());
1655 continue;
1656 }
1657 if (access(mirrorRefPackageProfile.c_str(), F_OK) != 0) {
1658 ALOGW("Can't access app profile directory: %s", mirrorRefPackageProfile.c_str());
1659 continue;
1660 }
1661
1662 PrepareDir(actualCurPackageProfile, DEFAULT_DATA_DIR_PERMISSION, uid, uid, fail_fn);
1663 BindMount(mirrorCurPackageProfile, actualCurPackageProfile, fail_fn);
1664 PrepareDir(actualRefPackageProfile, DEFAULT_DATA_DIR_PERMISSION, uid, uid, fail_fn);
1665 BindMount(mirrorRefPackageProfile, actualRefPackageProfile, fail_fn);
1666 }
1667 }
1668
WaitUntilDirReady(const std::string & target,fail_fn_t fail_fn)1669 static void WaitUntilDirReady(const std::string& target, fail_fn_t fail_fn) {
1670 unsigned int sleepIntervalUs = STORAGE_DIR_CHECK_INIT_INTERVAL_US;
1671
1672 // This is just an approximate value as it doesn't need to be very accurate.
1673 unsigned int sleepTotalUs = 0;
1674
1675 const char* dir_path = target.c_str();
1676 while (sleepTotalUs < STORAGE_DIR_CHECK_TIMEOUT_US) {
1677 if (access(dir_path, F_OK) == 0) {
1678 return;
1679 }
1680 // Failed, so we add exponential backoff and retry
1681 usleep(sleepIntervalUs);
1682 sleepTotalUs += sleepIntervalUs;
1683 sleepIntervalUs = std::min<unsigned int>(
1684 sleepIntervalUs * STORAGE_DIR_CHECK_RETRY_MULTIPLIER,
1685 STORAGE_DIR_CHECK_MAX_INTERVAL_US);
1686 }
1687 // Last chance and get the latest errno if it fails.
1688 if (access(dir_path, F_OK) == 0) {
1689 return;
1690 }
1691 fail_fn(CREATE_ERROR("Error dir is not ready %s: %s", dir_path, strerror(errno)));
1692 }
1693
BindMountStorageToLowerFs(const userid_t user_id,const uid_t uid,const char * dir_name,const char * package,fail_fn_t fail_fn)1694 static void BindMountStorageToLowerFs(const userid_t user_id, const uid_t uid,
1695 const char* dir_name, const char* package, fail_fn_t fail_fn) {
1696 bool hasSdcardFs = IsSdcardfsUsed();
1697 std::string source;
1698 if (hasSdcardFs) {
1699 source = StringPrintf("/mnt/runtime/default/emulated/%d/%s/%s", user_id, dir_name, package);
1700 } else {
1701 source = StringPrintf("/mnt/pass_through/%d/emulated/%d/%s/%s", user_id, user_id, dir_name,
1702 package);
1703 }
1704
1705 // Directory might be not ready, as prepareStorageDirs() is running asynchronously in ProcessList,
1706 // so wait until dir is created.
1707 WaitUntilDirReady(source, fail_fn);
1708 std::string target = StringPrintf("/storage/emulated/%d/%s/%s", user_id, dir_name, package);
1709
1710 // As the parent is mounted as tmpfs, we need to create the target dir here.
1711 PrepareDirIfNotPresent(target, 0700, uid, uid, fail_fn);
1712
1713 if (access(source.c_str(), F_OK) != 0) {
1714 fail_fn(CREATE_ERROR("Error accessing %s: %s", source.c_str(), strerror(errno)));
1715 }
1716 if (access(target.c_str(), F_OK) != 0) {
1717 fail_fn(CREATE_ERROR("Error accessing %s: %s", target.c_str(), strerror(errno)));
1718 }
1719 BindMount(source, target, fail_fn);
1720 }
1721
1722 // Mount tmpfs on Android/data and Android/obb, then bind mount all app visible package
1723 // directories in data and obb directories.
BindMountStorageDirs(JNIEnv * env,jobjectArray pkg_data_info_list,uid_t uid,const char * process_name,jstring managed_nice_name,fail_fn_t fail_fn)1724 static void BindMountStorageDirs(JNIEnv* env, jobjectArray pkg_data_info_list,
1725 uid_t uid, const char* process_name, jstring managed_nice_name, fail_fn_t fail_fn) {
1726
1727 auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1);
1728 const userid_t user_id = multiuser_get_user_id(uid);
1729
1730 // Fuse is ready, so we can start using fuse path.
1731 int size = (pkg_data_info_list != nullptr) ? env->GetArrayLength(pkg_data_info_list) : 0;
1732
1733 // Create tmpfs on Android/obb and Android/data so these 2 dirs won't enter fuse anymore.
1734 std::string androidObbDir = StringPrintf("/storage/emulated/%d/Android/obb", user_id);
1735 MountAppDataTmpFs(androidObbDir, fail_fn);
1736 std::string androidDataDir = StringPrintf("/storage/emulated/%d/Android/data", user_id);
1737 MountAppDataTmpFs(androidDataDir, fail_fn);
1738
1739 // Bind mount each package obb directory
1740 for (int i = 0; i < size; i += 3) {
1741 jstring package_str = (jstring) (env->GetObjectArrayElement(pkg_data_info_list, i));
1742 std::string packageName = extract_fn(package_str).value();
1743 BindMountStorageToLowerFs(user_id, uid, "Android/obb", packageName.c_str(), fail_fn);
1744 BindMountStorageToLowerFs(user_id, uid, "Android/data", packageName.c_str(), fail_fn);
1745 }
1746 }
1747
1748 // Utility routine to specialize a zygote child process.
SpecializeCommon(JNIEnv * env,uid_t uid,gid_t gid,jintArray gids,jint runtime_flags,jobjectArray rlimits,jlong permitted_capabilities,jlong effective_capabilities,jint mount_external,jstring managed_se_info,jstring managed_nice_name,bool is_system_server,bool is_child_zygote,jstring managed_instruction_set,jstring managed_app_data_dir,bool is_top_app,jobjectArray pkg_data_info_list,jobjectArray allowlisted_data_info_list,bool mount_data_dirs,bool mount_storage_dirs)1749 static void SpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray gids, jint runtime_flags,
1750 jobjectArray rlimits, jlong permitted_capabilities,
1751 jlong effective_capabilities, jint mount_external,
1752 jstring managed_se_info, jstring managed_nice_name,
1753 bool is_system_server, bool is_child_zygote,
1754 jstring managed_instruction_set, jstring managed_app_data_dir,
1755 bool is_top_app, jobjectArray pkg_data_info_list,
1756 jobjectArray allowlisted_data_info_list, bool mount_data_dirs,
1757 bool mount_storage_dirs) {
1758 const char* process_name = is_system_server ? "system_server" : "zygote";
1759 auto fail_fn = std::bind(ZygoteFailure, env, process_name, managed_nice_name, _1);
1760 auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1);
1761
1762 auto se_info = extract_fn(managed_se_info);
1763 auto nice_name = extract_fn(managed_nice_name);
1764 auto instruction_set = extract_fn(managed_instruction_set);
1765 auto app_data_dir = extract_fn(managed_app_data_dir);
1766
1767 // Keep capabilities across UID change, unless we're staying root.
1768 if (uid != 0) {
1769 EnableKeepCapabilities(fail_fn);
1770 }
1771
1772 SetInheritable(permitted_capabilities, fail_fn);
1773
1774 DropCapabilitiesBoundingSet(fail_fn);
1775
1776 bool need_pre_initialize_native_bridge = !is_system_server && instruction_set.has_value() &&
1777 android::NativeBridgeAvailable() &&
1778 // Native bridge may be already initialized if this
1779 // is an app forked from app-zygote.
1780 !android::NativeBridgeInitialized() &&
1781 android::NeedsNativeBridge(instruction_set.value().c_str());
1782
1783 MountEmulatedStorage(uid, mount_external, need_pre_initialize_native_bridge, fail_fn);
1784
1785 // Make sure app is running in its own mount namespace before isolating its data directories.
1786 ensureInAppMountNamespace(fail_fn);
1787
1788 // Isolate app data, jit profile and sandbox data directories by overlaying a tmpfs on those
1789 // dirs and bind mount all related packages separately.
1790 if (mount_data_dirs) {
1791 // Sdk sandbox data isolation does not need to occur for app processes since sepolicy
1792 // prevents access to sandbox data anyway.
1793 appid_t appId = multiuser_get_app_id(uid);
1794 if (appId >= AID_SDK_SANDBOX_PROCESS_START && appId <= AID_SDK_SANDBOX_PROCESS_END) {
1795 isolateSdkSandboxData(env, pkg_data_info_list, uid, process_name, managed_nice_name,
1796 fail_fn);
1797 }
1798 isolateAppData(env, pkg_data_info_list, allowlisted_data_info_list, uid, process_name,
1799 managed_nice_name, fail_fn);
1800 isolateJitProfile(env, pkg_data_info_list, uid, process_name, managed_nice_name, fail_fn);
1801 }
1802 // MOUNT_EXTERNAL_INSTALLER, MOUNT_EXTERNAL_PASS_THROUGH, MOUNT_EXTERNAL_ANDROID_WRITABLE apps
1803 // will have mount_storage_dirs == false here (set by ProcessList.needsStorageDataIsolation()),
1804 // and hence they won't bind mount storage dirs.
1805 if (mount_storage_dirs) {
1806 BindMountStorageDirs(env, pkg_data_info_list, uid, process_name, managed_nice_name,
1807 fail_fn);
1808 }
1809
1810 // If this zygote isn't root, it won't be able to create a process group,
1811 // since the directory is owned by root.
1812 if (!is_system_server && getuid() == 0) {
1813 const int rc = createProcessGroup(uid, getpid());
1814 if (rc != 0) {
1815 fail_fn(rc == -EROFS ? CREATE_ERROR("createProcessGroup failed, kernel missing "
1816 "CONFIG_CGROUP_CPUACCT?")
1817 : CREATE_ERROR("createProcessGroup(%d, %d) failed: %s", uid,
1818 /* pid= */ 0, strerror(-rc)));
1819 }
1820 }
1821
1822 SetGids(env, gids, is_child_zygote, fail_fn);
1823 SetRLimits(env, rlimits, fail_fn);
1824
1825 if (need_pre_initialize_native_bridge) {
1826 // Due to the logic behind need_pre_initialize_native_bridge we know that
1827 // instruction_set contains a value.
1828 android::PreInitializeNativeBridge(app_data_dir.has_value() ? app_data_dir.value().c_str()
1829 : nullptr,
1830 instruction_set.value().c_str());
1831 }
1832
1833 if (is_system_server && !(runtime_flags & RuntimeFlags::PROFILE_SYSTEM_SERVER)) {
1834 // Prefetch the classloader for the system server. This is done early to
1835 // allow a tie-down of the proper system server selinux domain.
1836 // We don't prefetch when the system server is being profiled to avoid
1837 // loading AOT code.
1838 env->CallStaticObjectMethod(gZygoteInitClass, gGetOrCreateSystemServerClassLoader);
1839 if (env->ExceptionCheck()) {
1840 // Be robust here. The Java code will attempt to create the classloader
1841 // at a later point (but may not have rights to use AoT artifacts).
1842 env->ExceptionClear();
1843 }
1844 // Also prefetch standalone system server jars. The reason for doing this here is the same
1845 // as above.
1846 env->CallStaticVoidMethod(gZygoteInitClass, gPrefetchStandaloneSystemServerJars);
1847 if (env->ExceptionCheck()) {
1848 env->ExceptionClear();
1849 }
1850 }
1851
1852 if (setresgid(gid, gid, gid) == -1) {
1853 fail_fn(CREATE_ERROR("setresgid(%d) failed: %s", gid, strerror(errno)));
1854 }
1855
1856 // Must be called when the new process still has CAP_SYS_ADMIN, in this case,
1857 // before changing uid from 0, which clears capabilities. The other
1858 // alternative is to call prctl(PR_SET_NO_NEW_PRIVS, 1) afterward, but that
1859 // breaks SELinux domain transition (see b/71859146). As the result,
1860 // privileged syscalls used below still need to be accessible in app process.
1861 SetUpSeccompFilter(uid, is_child_zygote);
1862
1863 // Must be called before losing the permission to set scheduler policy.
1864 SetSchedulerPolicy(fail_fn, is_top_app);
1865
1866 if (setresuid(uid, uid, uid) == -1) {
1867 fail_fn(CREATE_ERROR("setresuid(%d) failed: %s", uid, strerror(errno)));
1868 }
1869
1870 // The "dumpable" flag of a process, which controls core dump generation, is
1871 // overwritten by the value in /proc/sys/fs/suid_dumpable when the effective
1872 // user or group ID changes. See proc(5) for possible values. In most cases,
1873 // the value is 0, so core dumps are disabled for zygote children. However,
1874 // when running in a Chrome OS container, the value is already set to 2,
1875 // which allows the external crash reporter to collect all core dumps. Since
1876 // only system crashes are interested, core dump is disabled for app
1877 // processes. This also ensures compliance with CTS.
1878 int dumpable = prctl(PR_GET_DUMPABLE);
1879 if (dumpable == -1) {
1880 ALOGE("prctl(PR_GET_DUMPABLE) failed: %s", strerror(errno));
1881 RuntimeAbort(env, __LINE__, "prctl(PR_GET_DUMPABLE) failed");
1882 }
1883
1884 if (dumpable == 2 && uid >= AID_APP) {
1885 if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) {
1886 ALOGE("prctl(PR_SET_DUMPABLE, 0) failed: %s", strerror(errno));
1887 RuntimeAbort(env, __LINE__, "prctl(PR_SET_DUMPABLE, 0) failed");
1888 }
1889 }
1890
1891 // Set process properties to enable debugging if required.
1892 if ((runtime_flags & RuntimeFlags::DEBUG_ENABLE_PTRACE) != 0) {
1893 EnableDebugger();
1894 // Don't pass unknown flag to the ART runtime.
1895 runtime_flags &= ~RuntimeFlags::DEBUG_ENABLE_PTRACE;
1896 }
1897 if ((runtime_flags & RuntimeFlags::PROFILE_FROM_SHELL) != 0) {
1898 // simpleperf needs the process to be dumpable to profile it.
1899 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
1900 ALOGE("prctl(PR_SET_DUMPABLE) failed: %s", strerror(errno));
1901 RuntimeAbort(env, __LINE__, "prctl(PR_SET_DUMPABLE, 1) failed");
1902 }
1903 }
1904
1905 HeapTaggingLevel heap_tagging_level;
1906 switch (runtime_flags & RuntimeFlags::MEMORY_TAG_LEVEL_MASK) {
1907 case RuntimeFlags::MEMORY_TAG_LEVEL_TBI:
1908 heap_tagging_level = M_HEAP_TAGGING_LEVEL_TBI;
1909 break;
1910 case RuntimeFlags::MEMORY_TAG_LEVEL_ASYNC:
1911 heap_tagging_level = M_HEAP_TAGGING_LEVEL_ASYNC;
1912 break;
1913 case RuntimeFlags::MEMORY_TAG_LEVEL_SYNC:
1914 heap_tagging_level = M_HEAP_TAGGING_LEVEL_SYNC;
1915 break;
1916 default:
1917 heap_tagging_level = M_HEAP_TAGGING_LEVEL_NONE;
1918 break;
1919 }
1920 mallopt(M_BIONIC_SET_HEAP_TAGGING_LEVEL, heap_tagging_level);
1921
1922 // Now that we've used the flag, clear it so that we don't pass unknown flags to the ART
1923 // runtime.
1924 runtime_flags &= ~RuntimeFlags::MEMORY_TAG_LEVEL_MASK;
1925
1926 // Avoid heap zero initialization for applications without MTE. Zero init may
1927 // cause app compat problems, use more memory, or reduce performance. While it
1928 // would be nice to have them for apps, we will have to wait until they are
1929 // proven out, have more efficient hardware, and/or apply them only to new
1930 // applications.
1931 if (!(runtime_flags & RuntimeFlags::NATIVE_HEAP_ZERO_INIT_ENABLED)) {
1932 mallopt(M_BIONIC_ZERO_INIT, 0);
1933 }
1934
1935 // Now that we've used the flag, clear it so that we don't pass unknown flags to the ART
1936 // runtime.
1937 runtime_flags &= ~RuntimeFlags::NATIVE_HEAP_ZERO_INIT_ENABLED;
1938
1939 const char* nice_name_ptr = nice_name.has_value() ? nice_name.value().c_str() : nullptr;
1940 android_mallopt_gwp_asan_options_t gwp_asan_options;
1941 const char* kGwpAsanAppRecoverableSysprop =
1942 "persist.device_config.memory_safety_native.gwp_asan_recoverable_apps";
1943 // The system server doesn't have its nice name set by the time SpecializeCommon is called.
1944 gwp_asan_options.program_name = nice_name_ptr ?: process_name;
1945 switch (runtime_flags & RuntimeFlags::GWP_ASAN_LEVEL_MASK) {
1946 default:
1947 case RuntimeFlags::GWP_ASAN_LEVEL_DEFAULT:
1948 gwp_asan_options.desire = GetBoolProperty(kGwpAsanAppRecoverableSysprop, true)
1949 ? Action::TURN_ON_FOR_APP_SAMPLED_NON_CRASHING
1950 : Action::DONT_TURN_ON_UNLESS_OVERRIDDEN;
1951 android_mallopt(M_INITIALIZE_GWP_ASAN, &gwp_asan_options, sizeof(gwp_asan_options));
1952 break;
1953 case RuntimeFlags::GWP_ASAN_LEVEL_NEVER:
1954 gwp_asan_options.desire = Action::DONT_TURN_ON_UNLESS_OVERRIDDEN;
1955 android_mallopt(M_INITIALIZE_GWP_ASAN, &gwp_asan_options, sizeof(gwp_asan_options));
1956 break;
1957 case RuntimeFlags::GWP_ASAN_LEVEL_ALWAYS:
1958 gwp_asan_options.desire = Action::TURN_ON_FOR_APP;
1959 android_mallopt(M_INITIALIZE_GWP_ASAN, &gwp_asan_options, sizeof(gwp_asan_options));
1960 break;
1961 case RuntimeFlags::GWP_ASAN_LEVEL_LOTTERY:
1962 gwp_asan_options.desire = Action::TURN_ON_WITH_SAMPLING;
1963 android_mallopt(M_INITIALIZE_GWP_ASAN, &gwp_asan_options, sizeof(gwp_asan_options));
1964 break;
1965 }
1966 // Now that we've used the flag, clear it so that we don't pass unknown flags to the ART
1967 // runtime.
1968 runtime_flags &= ~RuntimeFlags::GWP_ASAN_LEVEL_MASK;
1969
1970 SetCapabilities(permitted_capabilities, effective_capabilities, permitted_capabilities,
1971 fail_fn);
1972
1973 __android_log_close();
1974 AStatsSocket_close();
1975
1976 const char* se_info_ptr = se_info.has_value() ? se_info.value().c_str() : nullptr;
1977
1978 if (selinux_android_setcontext(uid, is_system_server, se_info_ptr, nice_name_ptr) == -1) {
1979 fail_fn(CREATE_ERROR("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", uid,
1980 is_system_server, se_info_ptr, nice_name_ptr));
1981 }
1982
1983 // Make it easier to debug audit logs by setting the main thread's name to the
1984 // nice name rather than "app_process".
1985 if (nice_name.has_value()) {
1986 SetThreadName(nice_name.value());
1987 } else if (is_system_server) {
1988 SetThreadName("system_server");
1989 }
1990
1991 // Unset the SIGCHLD handler, but keep ignoring SIGHUP (rationale in SetSignalHandlers).
1992 UnsetChldSignalHandler();
1993
1994 if (is_system_server) {
1995 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkSystemServerHooks, runtime_flags);
1996 if (env->ExceptionCheck()) {
1997 fail_fn("Error calling post fork system server hooks.");
1998 }
1999
2000 // TODO(b/117874058): Remove hardcoded label here.
2001 static const char* kSystemServerLabel = "u:r:system_server:s0";
2002 if (selinux_android_setcon(kSystemServerLabel) != 0) {
2003 fail_fn(CREATE_ERROR("selinux_android_setcon(%s)", kSystemServerLabel));
2004 }
2005 }
2006
2007 if (is_child_zygote) {
2008 initUnsolSocketToSystemServer();
2009 }
2010
2011 env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags,
2012 is_system_server, is_child_zygote, managed_instruction_set);
2013
2014 // Reset the process priority to the default value.
2015 setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_DEFAULT);
2016
2017 if (env->ExceptionCheck()) {
2018 fail_fn("Error calling post fork hooks.");
2019 }
2020 }
2021
GetEffectiveCapabilityMask(JNIEnv * env)2022 static uint64_t GetEffectiveCapabilityMask(JNIEnv* env) {
2023 __user_cap_header_struct capheader;
2024 memset(&capheader, 0, sizeof(capheader));
2025 capheader.version = _LINUX_CAPABILITY_VERSION_3;
2026 capheader.pid = 0;
2027
2028 __user_cap_data_struct capdata[2];
2029 if (capget(&capheader, &capdata[0]) == -1) {
2030 ALOGE("capget failed: %s", strerror(errno));
2031 RuntimeAbort(env, __LINE__, "capget failed");
2032 }
2033
2034 return capdata[0].effective | (static_cast<uint64_t>(capdata[1].effective) << 32);
2035 }
2036
CalculateCapabilities(JNIEnv * env,jint uid,jint gid,jintArray gids,bool is_child_zygote)2037 static jlong CalculateCapabilities(JNIEnv* env, jint uid, jint gid, jintArray gids,
2038 bool is_child_zygote) {
2039 jlong capabilities = 0;
2040
2041 /*
2042 * Grant the following capabilities to the Bluetooth user:
2043 * - CAP_WAKE_ALARM
2044 * - CAP_NET_ADMIN
2045 * - CAP_NET_RAW
2046 * - CAP_NET_BIND_SERVICE (for DHCP client functionality)
2047 * - CAP_SYS_NICE (for setting RT priority for audio-related threads)
2048 */
2049
2050 if (multiuser_get_app_id(uid) == AID_BLUETOOTH) {
2051 capabilities |= (1LL << CAP_WAKE_ALARM);
2052 capabilities |= (1LL << CAP_NET_ADMIN);
2053 capabilities |= (1LL << CAP_NET_RAW);
2054 capabilities |= (1LL << CAP_NET_BIND_SERVICE);
2055 capabilities |= (1LL << CAP_SYS_NICE);
2056 }
2057
2058 if (multiuser_get_app_id(uid) == AID_NETWORK_STACK) {
2059 capabilities |= (1LL << CAP_NET_ADMIN);
2060 capabilities |= (1LL << CAP_NET_BROADCAST);
2061 capabilities |= (1LL << CAP_NET_BIND_SERVICE);
2062 capabilities |= (1LL << CAP_NET_RAW);
2063 }
2064
2065 /*
2066 * Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock"
2067 */
2068
2069 bool gid_wakelock_found = false;
2070 if (gid == AID_WAKELOCK) {
2071 gid_wakelock_found = true;
2072 } else if (gids != nullptr) {
2073 jsize gids_num = env->GetArrayLength(gids);
2074 ScopedIntArrayRO native_gid_proxy(env, gids);
2075
2076 if (native_gid_proxy.get() == nullptr) {
2077 RuntimeAbort(env, __LINE__, "Bad gids array");
2078 }
2079
2080 for (int gids_index = 0; gids_index < gids_num; ++gids_index) {
2081 if (native_gid_proxy[gids_index] == AID_WAKELOCK) {
2082 gid_wakelock_found = true;
2083 break;
2084 }
2085 }
2086 }
2087
2088 if (gid_wakelock_found) {
2089 capabilities |= (1LL << CAP_BLOCK_SUSPEND);
2090 }
2091
2092 /*
2093 * Grant child Zygote processes the following capabilities:
2094 * - CAP_SETUID (change UID of child processes)
2095 * - CAP_SETGID (change GID of child processes)
2096 * - CAP_SETPCAP (change capabilities of child processes)
2097 */
2098
2099 if (is_child_zygote) {
2100 capabilities |= (1LL << CAP_SETUID);
2101 capabilities |= (1LL << CAP_SETGID);
2102 capabilities |= (1LL << CAP_SETPCAP);
2103 }
2104
2105 /*
2106 * Containers run without some capabilities, so drop any caps that are not
2107 * available.
2108 */
2109
2110 return capabilities & GetEffectiveCapabilityMask(env);
2111 }
2112
2113 /**
2114 * Adds the given information about a newly created unspecialized app
2115 * processes to the Zygote's USAP table.
2116 *
2117 * @param usap_pid Process ID of the newly created USAP
2118 * @param read_pipe_fd File descriptor for the read end of the USAP
2119 * reporting pipe. Used in the ZygoteServer poll loop to track USAP
2120 * specialization.
2121 */
AddUsapTableEntry(pid_t usap_pid,int read_pipe_fd)2122 static void AddUsapTableEntry(pid_t usap_pid, int read_pipe_fd) {
2123 static int sUsapTableInsertIndex = 0;
2124
2125 int search_index = sUsapTableInsertIndex;
2126 do {
2127 if (gUsapTable[search_index].SetIfInvalid(usap_pid, read_pipe_fd)) {
2128 ++gUsapPoolCount;
2129
2130 // Start our next search right after where we finished this one.
2131 sUsapTableInsertIndex = (search_index + 1) % gUsapTable.size();
2132
2133 return;
2134 }
2135
2136 search_index = (search_index + 1) % gUsapTable.size();
2137 } while (search_index != sUsapTableInsertIndex);
2138
2139 // Much like money in the banana stand, there should always be an entry
2140 // in the USAP table.
2141 __builtin_unreachable();
2142 }
2143
2144 /**
2145 * Invalidates the entry in the USAPTable corresponding to the provided
2146 * process ID if it is present. If an entry was removed the USAP pool
2147 * count is decremented. May be called from signal handler.
2148 *
2149 * @param usap_pid Process ID of the USAP entry to invalidate
2150 * @return True if an entry was invalidated; false otherwise
2151 */
RemoveUsapTableEntry(pid_t usap_pid)2152 static bool RemoveUsapTableEntry(pid_t usap_pid) {
2153 for (UsapTableEntry& entry : gUsapTable) {
2154 if (entry.ClearForPID(usap_pid)) {
2155 --gUsapPoolCount;
2156 return true;
2157 }
2158 }
2159
2160 return false;
2161 }
2162
2163 /**
2164 * @return A vector of the read pipe FDs for each of the active USAPs.
2165 */
MakeUsapPipeReadFDVector()2166 std::vector<int> MakeUsapPipeReadFDVector() {
2167 std::vector<int> fd_vec;
2168 fd_vec.reserve(gUsapTable.size());
2169
2170 for (UsapTableEntry& entry : gUsapTable) {
2171 auto entry_values = entry.GetValues();
2172
2173 if (entry_values.has_value()) {
2174 fd_vec.push_back(entry_values.value().read_pipe_fd);
2175 }
2176 }
2177
2178 return fd_vec;
2179 }
2180
UnmountStorageOnInit(JNIEnv * env)2181 static void UnmountStorageOnInit(JNIEnv* env) {
2182 // Zygote process unmount root storage space initially before every child processes are forked.
2183 // Every forked child processes (include SystemServer) only mount their own root storage space
2184 // and no need unmount storage operation in MountEmulatedStorage method.
2185 // Zygote process does not utilize root storage spaces and unshares its mount namespace below.
2186
2187 // See storage config details at http://source.android.com/tech/storage/
2188 // Create private mount namespace shared by all children
2189 if (unshare(CLONE_NEWNS) == -1) {
2190 RuntimeAbort(env, __LINE__, "Failed to unshare()");
2191 return;
2192 }
2193
2194 // Mark rootfs as being MS_SLAVE so that changes from default
2195 // namespace only flow into our children.
2196 if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
2197 RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE");
2198 return;
2199 }
2200
2201 // Create a staging tmpfs that is shared by our children; they will
2202 // bind mount storage into their respective private namespaces, which
2203 // are isolated from each other.
2204 const char* target_base = getenv("EMULATED_STORAGE_TARGET");
2205 if (target_base != nullptr) {
2206 #define STRINGIFY_UID(x) __STRING(x)
2207 if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV,
2208 "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) {
2209 ALOGE("Failed to mount tmpfs to %s", target_base);
2210 RuntimeAbort(env, __LINE__, "Failed to mount tmpfs");
2211 return;
2212 }
2213 #undef STRINGIFY_UID
2214 }
2215
2216 UnmountTree("/storage");
2217 }
2218
2219 } // anonymous namespace
2220
2221 namespace android {
2222
2223 /**
2224 * A failure function used to report fatal errors to the managed runtime. This
2225 * function is often curried with the process name information and then passed
2226 * to called functions.
2227 *
2228 * @param env Managed runtime environment
2229 * @param process_name A native representation of the process name
2230 * @param managed_process_name A managed representation of the process name
2231 * @param msg The error message to be reported
2232 */
2233 [[noreturn]]
ZygoteFailure(JNIEnv * env,const char * process_name,jstring managed_process_name,const std::string & msg)2234 void zygote::ZygoteFailure(JNIEnv* env,
2235 const char* process_name,
2236 jstring managed_process_name,
2237 const std::string& msg) {
2238 std::unique_ptr<ScopedUtfChars> scoped_managed_process_name_ptr = nullptr;
2239 if (managed_process_name != nullptr) {
2240 scoped_managed_process_name_ptr.reset(new ScopedUtfChars(env, managed_process_name));
2241 if (scoped_managed_process_name_ptr->c_str() != nullptr) {
2242 process_name = scoped_managed_process_name_ptr->c_str();
2243 }
2244 }
2245
2246 const std::string& error_msg =
2247 (process_name == nullptr || process_name[0] == '\0') ?
2248 msg : StringPrintf("(%s) %s", process_name, msg.c_str());
2249
2250 env->FatalError(error_msg.c_str());
2251 __builtin_unreachable();
2252 }
2253
2254 static std::set<int>* gPreloadFds = nullptr;
2255 static bool gPreloadFdsExtracted = false;
2256
2257 // Utility routine to fork a process from the zygote.
2258 NO_STACK_PROTECTOR
ForkCommon(JNIEnv * env,bool is_system_server,const std::vector<int> & fds_to_close,const std::vector<int> & fds_to_ignore,bool is_priority_fork,bool purge)2259 pid_t zygote::ForkCommon(JNIEnv* env, bool is_system_server,
2260 const std::vector<int>& fds_to_close,
2261 const std::vector<int>& fds_to_ignore,
2262 bool is_priority_fork,
2263 bool purge) {
2264 SetSignalHandlers();
2265
2266 // Curry a failure function.
2267 auto fail_fn = std::bind(zygote::ZygoteFailure, env,
2268 is_system_server ? "system_server" : "zygote",
2269 nullptr, _1);
2270
2271 // Temporarily block SIGCHLD during forks. The SIGCHLD handler might
2272 // log, which would result in the logging FDs we close being reopened.
2273 // This would cause failures because the FDs are not allowlisted.
2274 //
2275 // Note that the zygote process is single threaded at this point.
2276 BlockSignal(SIGCHLD, fail_fn);
2277
2278 // Close any logging related FDs before we start evaluating the list of
2279 // file descriptors.
2280 __android_log_close();
2281 AStatsSocket_close();
2282
2283 // If this is the first fork for this zygote, create the open FD table,
2284 // verifying that files are of supported type and allowlisted. Otherwise (not
2285 // the first fork), check that the open files have not changed. Newly open
2286 // files are not expected, and will be disallowed in the future. Currently
2287 // they are allowed if they pass the same checks as in the
2288 // FileDescriptorTable::Create() above.
2289 if (gOpenFdTable == nullptr) {
2290 gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore, fail_fn);
2291 } else {
2292 gOpenFdTable->Restat(fds_to_ignore, fail_fn);
2293 }
2294
2295 android_fdsan_error_level fdsan_error_level = android_fdsan_get_error_level();
2296
2297 if (purge) {
2298 // Purge unused native memory in an attempt to reduce the amount of false
2299 // sharing with the child process. By reducing the size of the libc_malloc
2300 // region shared with the child process we reduce the number of pages that
2301 // transition to the private-dirty state when malloc adjusts the meta-data
2302 // on each of the pages it is managing after the fork.
2303 if (mallopt(M_PURGE_ALL, 0) != 1) {
2304 mallopt(M_PURGE, 0);
2305 }
2306 }
2307
2308 pid_t pid = fork();
2309
2310 if (pid == 0) {
2311 if (is_priority_fork) {
2312 setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_MAX);
2313 } else {
2314 setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_MIN);
2315 }
2316
2317 #if defined(__BIONIC__) && !defined(NO_RESET_STACK_PROTECTOR)
2318 // Reset the stack guard for the new process.
2319 android_reset_stack_guards();
2320 #endif
2321
2322 // The child process.
2323 PreApplicationInit();
2324
2325 // Clean up any descriptors which must be closed immediately
2326 DetachDescriptors(env, fds_to_close, fail_fn);
2327
2328 // Invalidate the entries in the USAP table.
2329 ClearUsapTable();
2330
2331 // Re-open all remaining open file descriptors so that they aren't shared
2332 // with the zygote across a fork.
2333 gOpenFdTable->ReopenOrDetach(fail_fn);
2334
2335 // Turn fdsan back on.
2336 android_fdsan_set_error_level(fdsan_error_level);
2337
2338 // Reset the fd to the unsolicited zygote socket
2339 gSystemServerSocketFd = -1;
2340 } else if (pid == -1) {
2341 ALOGE("Failed to fork child process: %s (%d)", strerror(errno), errno);
2342 } else {
2343 ALOGD("Forked child process %d", pid);
2344 }
2345
2346 // We blocked SIGCHLD prior to a fork, we unblock it here.
2347 UnblockSignal(SIGCHLD, fail_fn);
2348
2349 return pid;
2350 }
2351
com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv *,jclass)2352 static void com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv*, jclass) {
2353 PreApplicationInit();
2354 }
2355
2356 NO_STACK_PROTECTOR
com_android_internal_os_Zygote_nativeForkAndSpecialize(JNIEnv * env,jclass,jint uid,jint gid,jintArray gids,jint runtime_flags,jobjectArray rlimits,jint mount_external,jstring se_info,jstring nice_name,jintArray managed_fds_to_close,jintArray managed_fds_to_ignore,jboolean is_child_zygote,jstring instruction_set,jstring app_data_dir,jboolean is_top_app,jobjectArray pkg_data_info_list,jobjectArray allowlisted_data_info_list,jboolean mount_data_dirs,jboolean mount_storage_dirs)2357 static jint com_android_internal_os_Zygote_nativeForkAndSpecialize(
2358 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, jint runtime_flags,
2359 jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name,
2360 jintArray managed_fds_to_close, jintArray managed_fds_to_ignore, jboolean is_child_zygote,
2361 jstring instruction_set, jstring app_data_dir, jboolean is_top_app,
2362 jobjectArray pkg_data_info_list, jobjectArray allowlisted_data_info_list,
2363 jboolean mount_data_dirs, jboolean mount_storage_dirs) {
2364 jlong capabilities = CalculateCapabilities(env, uid, gid, gids, is_child_zygote);
2365
2366 if (UNLIKELY(managed_fds_to_close == nullptr)) {
2367 zygote::ZygoteFailure(env, "zygote", nice_name,
2368 "Zygote received a null fds_to_close vector.");
2369 }
2370
2371 std::vector<int> fds_to_close =
2372 ExtractJIntArray(env, "zygote", nice_name, managed_fds_to_close).value();
2373 std::vector<int> fds_to_ignore =
2374 ExtractJIntArray(env, "zygote", nice_name, managed_fds_to_ignore)
2375 .value_or(std::vector<int>());
2376
2377 std::vector<int> usap_pipes = MakeUsapPipeReadFDVector();
2378
2379 fds_to_close.insert(fds_to_close.end(), usap_pipes.begin(), usap_pipes.end());
2380 fds_to_ignore.insert(fds_to_ignore.end(), usap_pipes.begin(), usap_pipes.end());
2381
2382 fds_to_close.push_back(gUsapPoolSocketFD);
2383
2384 if (gUsapPoolEventFD != -1) {
2385 fds_to_close.push_back(gUsapPoolEventFD);
2386 fds_to_ignore.push_back(gUsapPoolEventFD);
2387 }
2388
2389 if (gSystemServerSocketFd != -1) {
2390 fds_to_close.push_back(gSystemServerSocketFd);
2391 fds_to_ignore.push_back(gSystemServerSocketFd);
2392 }
2393
2394 if (gPreloadFds && gPreloadFdsExtracted) {
2395 fds_to_ignore.insert(fds_to_ignore.end(), gPreloadFds->begin(), gPreloadFds->end());
2396 }
2397
2398 pid_t pid = zygote::ForkCommon(env, /* is_system_server= */ false, fds_to_close, fds_to_ignore,
2399 true);
2400
2401 if (pid == 0) {
2402 SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, capabilities, capabilities,
2403 mount_external, se_info, nice_name, false, is_child_zygote == JNI_TRUE,
2404 instruction_set, app_data_dir, is_top_app == JNI_TRUE, pkg_data_info_list,
2405 allowlisted_data_info_list, mount_data_dirs == JNI_TRUE,
2406 mount_storage_dirs == JNI_TRUE);
2407 }
2408 return pid;
2409 }
2410
2411 NO_STACK_PROTECTOR
com_android_internal_os_Zygote_nativeForkSystemServer(JNIEnv * env,jclass,uid_t uid,gid_t gid,jintArray gids,jint runtime_flags,jobjectArray rlimits,jlong permitted_capabilities,jlong effective_capabilities)2412 static jint com_android_internal_os_Zygote_nativeForkSystemServer(
2413 JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids,
2414 jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities,
2415 jlong effective_capabilities) {
2416 std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()),
2417 fds_to_ignore(fds_to_close);
2418
2419 fds_to_close.push_back(gUsapPoolSocketFD);
2420
2421 if (gUsapPoolEventFD != -1) {
2422 fds_to_close.push_back(gUsapPoolEventFD);
2423 fds_to_ignore.push_back(gUsapPoolEventFD);
2424 }
2425
2426 if (gSystemServerSocketFd != -1) {
2427 fds_to_close.push_back(gSystemServerSocketFd);
2428 fds_to_ignore.push_back(gSystemServerSocketFd);
2429 }
2430
2431 pid_t pid = zygote::ForkCommon(env, true,
2432 fds_to_close,
2433 fds_to_ignore,
2434 true);
2435 if (pid == 0) {
2436 // System server prcoess does not need data isolation so no need to
2437 // know pkg_data_info_list.
2438 SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, permitted_capabilities,
2439 effective_capabilities, MOUNT_EXTERNAL_DEFAULT, nullptr, nullptr, true,
2440 false, nullptr, nullptr, /* is_top_app= */ false,
2441 /* pkg_data_info_list */ nullptr,
2442 /* allowlisted_data_info_list */ nullptr, false, false);
2443 } else if (pid > 0) {
2444 // The zygote process checks whether the child process has died or not.
2445 ALOGI("System server process %d has been created", pid);
2446 gSystemServerPid = pid;
2447 // There is a slight window that the system server process has crashed
2448 // but it went unnoticed because we haven't published its pid yet. So
2449 // we recheck here just to make sure that all is well.
2450 int status;
2451 if (waitpid(pid, &status, WNOHANG) == pid) {
2452 ALOGE("System server process %d has died. Restarting Zygote!", pid);
2453 RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!");
2454 }
2455
2456 if (UsePerAppMemcg()) {
2457 // Assign system_server to the correct memory cgroup.
2458 // Not all devices mount memcg so check if it is mounted first
2459 // to avoid unnecessarily printing errors and denials in the logs.
2460 if (!SetTaskProfiles(pid, std::vector<std::string>{"SystemMemoryProcess"})) {
2461 ALOGE("couldn't add process %d into system memcg group", pid);
2462 }
2463 }
2464 }
2465 return pid;
2466 }
2467
2468 /**
2469 * A JNI function that forks an unspecialized app process from the Zygote while
2470 * ensuring proper file descriptor hygiene.
2471 *
2472 * @param env Managed runtime environment
2473 * @param read_pipe_fd The read FD for the USAP reporting pipe. Manually closed by the child
2474 * in managed code. -1 indicates none.
2475 * @param write_pipe_fd The write FD for the USAP reporting pipe. Manually closed by the
2476 * zygote in managed code. -1 indicates none.
2477 * @param managed_session_socket_fds A list of anonymous session sockets that must be ignored by
2478 * the FD hygiene code and automatically "closed" in the new USAP.
2479 * @param args_known Arguments for specialization are available; no need to read from a socket
2480 * @param is_priority_fork Controls the nice level assigned to the newly created process
2481 * @return child pid in the parent, 0 in the child
2482 */
2483 NO_STACK_PROTECTOR
com_android_internal_os_Zygote_nativeForkApp(JNIEnv * env,jclass,jint read_pipe_fd,jint write_pipe_fd,jintArray managed_session_socket_fds,jboolean args_known,jboolean is_priority_fork)2484 static jint com_android_internal_os_Zygote_nativeForkApp(JNIEnv* env,
2485 jclass,
2486 jint read_pipe_fd,
2487 jint write_pipe_fd,
2488 jintArray managed_session_socket_fds,
2489 jboolean args_known,
2490 jboolean is_priority_fork) {
2491 std::vector<int> session_socket_fds =
2492 ExtractJIntArray(env, "USAP", nullptr, managed_session_socket_fds)
2493 .value_or(std::vector<int>());
2494 return zygote::forkApp(env, read_pipe_fd, write_pipe_fd, session_socket_fds,
2495 args_known == JNI_TRUE, is_priority_fork == JNI_TRUE, true);
2496 }
2497
2498 NO_STACK_PROTECTOR
forkApp(JNIEnv * env,int read_pipe_fd,int write_pipe_fd,const std::vector<int> & session_socket_fds,bool args_known,bool is_priority_fork,bool purge)2499 int zygote::forkApp(JNIEnv* env,
2500 int read_pipe_fd,
2501 int write_pipe_fd,
2502 const std::vector<int>& session_socket_fds,
2503 bool args_known,
2504 bool is_priority_fork,
2505 bool purge) {
2506
2507 std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()),
2508 fds_to_ignore(fds_to_close);
2509
2510 fds_to_close.push_back(gZygoteSocketFD);
2511 if (gSystemServerSocketFd != -1) {
2512 fds_to_close.push_back(gSystemServerSocketFd);
2513 }
2514 if (args_known) {
2515 fds_to_close.push_back(gUsapPoolSocketFD);
2516 }
2517 fds_to_close.insert(fds_to_close.end(), session_socket_fds.begin(), session_socket_fds.end());
2518
2519 fds_to_ignore.push_back(gUsapPoolSocketFD);
2520 fds_to_ignore.push_back(gZygoteSocketFD);
2521 if (read_pipe_fd != -1) {
2522 fds_to_ignore.push_back(read_pipe_fd);
2523 }
2524 if (write_pipe_fd != -1) {
2525 fds_to_ignore.push_back(write_pipe_fd);
2526 }
2527 fds_to_ignore.insert(fds_to_ignore.end(), session_socket_fds.begin(), session_socket_fds.end());
2528
2529 if (gUsapPoolEventFD != -1) {
2530 fds_to_close.push_back(gUsapPoolEventFD);
2531 fds_to_ignore.push_back(gUsapPoolEventFD);
2532 }
2533 if (gSystemServerSocketFd != -1) {
2534 if (args_known) {
2535 fds_to_close.push_back(gSystemServerSocketFd);
2536 }
2537 fds_to_ignore.push_back(gSystemServerSocketFd);
2538 }
2539 if (gPreloadFds && gPreloadFdsExtracted) {
2540 fds_to_ignore.insert(fds_to_ignore.end(), gPreloadFds->begin(), gPreloadFds->end());
2541 }
2542
2543 return zygote::ForkCommon(env, /* is_system_server= */ false, fds_to_close,
2544 fds_to_ignore, is_priority_fork == JNI_TRUE, purge);
2545 }
2546
com_android_internal_os_Zygote_nativeAllowFileAcrossFork(JNIEnv * env,jclass,jstring path)2547 static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork(
2548 JNIEnv* env, jclass, jstring path) {
2549 ScopedUtfChars path_native(env, path);
2550 const char* path_cstr = path_native.c_str();
2551 if (!path_cstr) {
2552 RuntimeAbort(env, __LINE__, "path_cstr == nullptr");
2553 }
2554 FileDescriptorAllowlist::Get()->Allow(path_cstr);
2555 }
2556
com_android_internal_os_Zygote_nativeInstallSeccompUidGidFilter(JNIEnv * env,jclass,jint uidGidMin,jint uidGidMax)2557 static void com_android_internal_os_Zygote_nativeInstallSeccompUidGidFilter(
2558 JNIEnv* env, jclass, jint uidGidMin, jint uidGidMax) {
2559 if (!gIsSecurityEnforced) {
2560 ALOGI("seccomp disabled by setenforce 0");
2561 return;
2562 }
2563
2564 bool installed = install_setuidgid_seccomp_filter(uidGidMin, uidGidMax);
2565 if (!installed) {
2566 RuntimeAbort(env, __LINE__, "Could not install setuid/setgid seccomp filter.");
2567 }
2568 }
2569
2570 /**
2571 * Called from an unspecialized app process to specialize the process for a
2572 * given application.
2573 *
2574 * @param env Managed runtime environment
2575 * @param uid User ID of the new application
2576 * @param gid Group ID of the new application
2577 * @param gids Extra groups that the process belongs to
2578 * @param runtime_flags Flags for changing the behavior of the managed runtime
2579 * @param rlimits Resource limits
2580 * @param mount_external The mode (read/write/normal) that external storage will be mounted with
2581 * @param se_info SELinux policy information
2582 * @param nice_name New name for this process
2583 * @param is_child_zygote If the process is to become a WebViewZygote
2584 * @param instruction_set The instruction set expected/requested by the new application
2585 * @param app_data_dir Path to the application's data directory
2586 * @param is_top_app If the process is for top (high priority) application
2587 */
com_android_internal_os_Zygote_nativeSpecializeAppProcess(JNIEnv * env,jclass,jint uid,jint gid,jintArray gids,jint runtime_flags,jobjectArray rlimits,jint mount_external,jstring se_info,jstring nice_name,jboolean is_child_zygote,jstring instruction_set,jstring app_data_dir,jboolean is_top_app,jobjectArray pkg_data_info_list,jobjectArray allowlisted_data_info_list,jboolean mount_data_dirs,jboolean mount_storage_dirs)2588 static void com_android_internal_os_Zygote_nativeSpecializeAppProcess(
2589 JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, jint runtime_flags,
2590 jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name,
2591 jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir,
2592 jboolean is_top_app, jobjectArray pkg_data_info_list,
2593 jobjectArray allowlisted_data_info_list, jboolean mount_data_dirs,
2594 jboolean mount_storage_dirs) {
2595 jlong capabilities = CalculateCapabilities(env, uid, gid, gids, is_child_zygote);
2596
2597 SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, capabilities, capabilities,
2598 mount_external, se_info, nice_name, false, is_child_zygote == JNI_TRUE,
2599 instruction_set, app_data_dir, is_top_app == JNI_TRUE, pkg_data_info_list,
2600 allowlisted_data_info_list, mount_data_dirs == JNI_TRUE,
2601 mount_storage_dirs == JNI_TRUE);
2602 }
2603
2604 /**
2605 * A helper method for fetching socket file descriptors that were opened by init from the
2606 * environment.
2607 *
2608 * @param env Managed runtime environment
2609 * @param is_primary If this process is the primary or secondary Zygote; used to compute the name
2610 * of the environment variable storing the file descriptors.
2611 */
com_android_internal_os_Zygote_nativeInitNativeState(JNIEnv * env,jclass,jboolean is_primary)2612 static void com_android_internal_os_Zygote_nativeInitNativeState(JNIEnv* env, jclass,
2613 jboolean is_primary) {
2614 /*
2615 * Obtain file descriptors created by init from the environment.
2616 */
2617
2618 gZygoteSocketFD =
2619 android_get_control_socket(is_primary ? "zygote" : "zygote_secondary");
2620 if (gZygoteSocketFD >= 0) {
2621 ALOGV("Zygote:zygoteSocketFD = %d", gZygoteSocketFD);
2622 } else {
2623 ALOGE("Unable to fetch Zygote socket file descriptor");
2624 }
2625
2626 gUsapPoolSocketFD =
2627 android_get_control_socket(is_primary ? "usap_pool_primary" : "usap_pool_secondary");
2628 if (gUsapPoolSocketFD >= 0) {
2629 ALOGV("Zygote:usapPoolSocketFD = %d", gUsapPoolSocketFD);
2630 } else {
2631 ALOGE("Unable to fetch USAP pool socket file descriptor");
2632 }
2633
2634 initUnsolSocketToSystemServer();
2635
2636 /*
2637 * Security Initialization
2638 */
2639
2640 // security_getenforce is not allowed on app process. Initialize and cache
2641 // the value before zygote forks.
2642 gIsSecurityEnforced = security_getenforce();
2643
2644 selinux_android_seapp_context_init();
2645
2646 /*
2647 * Storage Initialization
2648 */
2649
2650 UnmountStorageOnInit(env);
2651
2652 /*
2653 * Performance Initialization
2654 */
2655
2656 if (!SetTaskProfiles(0, {})) {
2657 zygote::ZygoteFailure(env, "zygote", nullptr, "Zygote SetTaskProfiles failed");
2658 }
2659 }
2660
2661 /**
2662 * @param env Managed runtime environment
2663 * @return A managed array of raw file descriptors for the read ends of the USAP reporting
2664 * pipes.
2665 */
com_android_internal_os_Zygote_nativeGetUsapPipeFDs(JNIEnv * env,jclass)2666 static jintArray com_android_internal_os_Zygote_nativeGetUsapPipeFDs(JNIEnv* env, jclass) {
2667 std::vector<int> usap_fds = MakeUsapPipeReadFDVector();
2668
2669 jintArray managed_usap_fds = env->NewIntArray(usap_fds.size());
2670 env->SetIntArrayRegion(managed_usap_fds, 0, usap_fds.size(), usap_fds.data());
2671
2672 return managed_usap_fds;
2673 }
2674
2675 /*
2676 * Add the given pid and file descriptor to the Usap table. CriticalNative method.
2677 */
com_android_internal_os_Zygote_nativeAddUsapTableEntry(jint pid,jint read_pipe_fd)2678 static void com_android_internal_os_Zygote_nativeAddUsapTableEntry(jint pid, jint read_pipe_fd) {
2679 AddUsapTableEntry(pid, read_pipe_fd);
2680 }
2681
2682 /**
2683 * A JNI wrapper around RemoveUsapTableEntry. CriticalNative method.
2684 *
2685 * @param env Managed runtime environment
2686 * @param usap_pid Process ID of the USAP entry to invalidate
2687 * @return True if an entry was invalidated; false otherwise.
2688 */
com_android_internal_os_Zygote_nativeRemoveUsapTableEntry(jint usap_pid)2689 static jboolean com_android_internal_os_Zygote_nativeRemoveUsapTableEntry(jint usap_pid) {
2690 return RemoveUsapTableEntry(usap_pid);
2691 }
2692
2693 /**
2694 * Creates the USAP pool event FD if it doesn't exist and returns it. This is used by the
2695 * ZygoteServer poll loop to know when to re-fill the USAP pool.
2696 *
2697 * @param env Managed runtime environment
2698 * @return A raw event file descriptor used to communicate (from the signal handler) when the
2699 * Zygote receives a SIGCHLD for a USAP
2700 */
com_android_internal_os_Zygote_nativeGetUsapPoolEventFD(JNIEnv * env,jclass)2701 static jint com_android_internal_os_Zygote_nativeGetUsapPoolEventFD(JNIEnv* env, jclass) {
2702 if (gUsapPoolEventFD == -1) {
2703 if ((gUsapPoolEventFD = eventfd(0, 0)) == -1) {
2704 zygote::ZygoteFailure(env, "zygote", nullptr,
2705 StringPrintf("Unable to create eventfd: %s", strerror(errno)));
2706 }
2707 }
2708
2709 return gUsapPoolEventFD;
2710 }
2711
2712 /**
2713 * @param env Managed runtime environment
2714 * @return The number of USAPs currently in the USAP pool
2715 */
com_android_internal_os_Zygote_nativeGetUsapPoolCount(JNIEnv * env,jclass)2716 static jint com_android_internal_os_Zygote_nativeGetUsapPoolCount(JNIEnv* env, jclass) {
2717 return gUsapPoolCount;
2718 }
2719
2720 /**
2721 * Kills all processes currently in the USAP pool and closes their read pipe
2722 * FDs.
2723 *
2724 * @param env Managed runtime environment
2725 */
com_android_internal_os_Zygote_nativeEmptyUsapPool(JNIEnv * env,jclass)2726 static void com_android_internal_os_Zygote_nativeEmptyUsapPool(JNIEnv* env, jclass) {
2727 for (auto& entry : gUsapTable) {
2728 auto entry_storage = entry.GetValues();
2729
2730 if (entry_storage.has_value()) {
2731 kill(entry_storage.value().pid, SIGTERM);
2732
2733 // Clean up the USAP table entry here. This avoids a potential race
2734 // where a newly created USAP might not be able to find a valid table
2735 // entry if signal handler (which would normally do the cleanup) doesn't
2736 // run between now and when the new process is created.
2737
2738 close(entry_storage.value().read_pipe_fd);
2739
2740 // Avoid a second atomic load by invalidating instead of clearing.
2741 entry.Invalidate();
2742 --gUsapPoolCount;
2743 }
2744 }
2745 }
2746
com_android_internal_os_Zygote_nativeBlockSigTerm(JNIEnv * env,jclass)2747 static void com_android_internal_os_Zygote_nativeBlockSigTerm(JNIEnv* env, jclass) {
2748 auto fail_fn = std::bind(zygote::ZygoteFailure, env, "usap", nullptr, _1);
2749 BlockSignal(SIGTERM, fail_fn);
2750 }
2751
com_android_internal_os_Zygote_nativeUnblockSigTerm(JNIEnv * env,jclass)2752 static void com_android_internal_os_Zygote_nativeUnblockSigTerm(JNIEnv* env, jclass) {
2753 auto fail_fn = std::bind(zygote::ZygoteFailure, env, "usap", nullptr, _1);
2754 UnblockSignal(SIGTERM, fail_fn);
2755 }
2756
com_android_internal_os_Zygote_nativeBoostUsapPriority(JNIEnv * env,jclass)2757 static void com_android_internal_os_Zygote_nativeBoostUsapPriority(JNIEnv* env, jclass) {
2758 setpriority(PRIO_PROCESS, 0, PROCESS_PRIORITY_MAX);
2759 }
2760
com_android_internal_os_Zygote_nativeParseSigChld(JNIEnv * env,jclass,jbyteArray in,jint length,jintArray out)2761 static jint com_android_internal_os_Zygote_nativeParseSigChld(JNIEnv* env, jclass, jbyteArray in,
2762 jint length, jintArray out) {
2763 if (length != sizeof(struct UnsolicitedZygoteMessageSigChld)) {
2764 // Apparently it's not the message we are expecting.
2765 return -1;
2766 }
2767 if (in == nullptr || out == nullptr) {
2768 // Invalid parameter
2769 jniThrowException(env, "java/lang/IllegalArgumentException", nullptr);
2770 return -1;
2771 }
2772 ScopedByteArrayRO source(env, in);
2773 if (source.size() < length) {
2774 // Invalid parameter
2775 jniThrowException(env, "java/lang/IllegalArgumentException", nullptr);
2776 return -1;
2777 }
2778 const struct UnsolicitedZygoteMessageSigChld* msg =
2779 reinterpret_cast<const struct UnsolicitedZygoteMessageSigChld*>(source.get());
2780
2781 switch (msg->header.type) {
2782 case UNSOLICITED_ZYGOTE_MESSAGE_TYPE_SIGCHLD: {
2783 ScopedIntArrayRW buf(env, out);
2784 if (buf.size() != 3) {
2785 jniThrowException(env, "java/lang/IllegalArgumentException", nullptr);
2786 return UNSOLICITED_ZYGOTE_MESSAGE_TYPE_RESERVED;
2787 }
2788 buf[0] = msg->payload.pid;
2789 buf[1] = msg->payload.uid;
2790 buf[2] = msg->payload.status;
2791 return 3;
2792 }
2793 default:
2794 break;
2795 }
2796 return -1;
2797 }
2798
com_android_internal_os_Zygote_nativeSupportsMemoryTagging(JNIEnv * env,jclass)2799 static jboolean com_android_internal_os_Zygote_nativeSupportsMemoryTagging(JNIEnv* env, jclass) {
2800 #if defined(__aarch64__)
2801 return mte_supported();
2802 #else
2803 return false;
2804 #endif
2805 }
2806
com_android_internal_os_Zygote_nativeSupportsTaggedPointers(JNIEnv * env,jclass)2807 static jboolean com_android_internal_os_Zygote_nativeSupportsTaggedPointers(JNIEnv* env, jclass) {
2808 #ifdef __aarch64__
2809 int res = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
2810 return res >= 0 && res & PR_TAGGED_ADDR_ENABLE;
2811 #else
2812 return false;
2813 #endif
2814 }
2815
com_android_internal_os_Zygote_nativeCurrentTaggingLevel(JNIEnv * env,jclass)2816 static jint com_android_internal_os_Zygote_nativeCurrentTaggingLevel(JNIEnv* env, jclass) {
2817 #if defined(__aarch64__)
2818 int level = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0);
2819 if (level < 0) {
2820 ALOGE("Failed to get memory tag level: %s", strerror(errno));
2821 return 0;
2822 } else if (!(level & PR_TAGGED_ADDR_ENABLE)) {
2823 return 0;
2824 }
2825 // TBI is only possible on non-MTE hardware.
2826 if (!mte_supported()) {
2827 return MEMORY_TAG_LEVEL_TBI;
2828 }
2829
2830 switch (level & PR_MTE_TCF_MASK) {
2831 case PR_MTE_TCF_NONE:
2832 return 0;
2833 case PR_MTE_TCF_SYNC:
2834 return MEMORY_TAG_LEVEL_SYNC;
2835 case PR_MTE_TCF_ASYNC:
2836 case PR_MTE_TCF_ASYNC | PR_MTE_TCF_SYNC:
2837 return MEMORY_TAG_LEVEL_ASYNC;
2838 default:
2839 ALOGE("Unknown memory tagging level: %i", level);
2840 return 0;
2841 }
2842 #else // defined(__aarch64__)
2843 return 0;
2844 #endif // defined(__aarch64__)
2845 }
2846
com_android_internal_os_Zygote_nativeMarkOpenedFilesBeforePreload(JNIEnv * env,jclass)2847 static void com_android_internal_os_Zygote_nativeMarkOpenedFilesBeforePreload(JNIEnv* env, jclass) {
2848 // Ignore invocations when too early or too late.
2849 if (gPreloadFds) {
2850 return;
2851 }
2852
2853 // App Zygote Preload starts soon. Save FDs remaining open. After the
2854 // preload finishes newly open files will be determined.
2855 auto fail_fn = std::bind(zygote::ZygoteFailure, env, "zygote", nullptr, _1);
2856 gPreloadFds = GetOpenFds(fail_fn).release();
2857 }
2858
com_android_internal_os_Zygote_nativeAllowFilesOpenedByPreload(JNIEnv * env,jclass)2859 static void com_android_internal_os_Zygote_nativeAllowFilesOpenedByPreload(JNIEnv* env, jclass) {
2860 // Ignore invocations when too early or too late.
2861 if (!gPreloadFds || gPreloadFdsExtracted) {
2862 return;
2863 }
2864
2865 // Find the newly open FDs, if any.
2866 auto fail_fn = std::bind(zygote::ZygoteFailure, env, "zygote", nullptr, _1);
2867 std::unique_ptr<std::set<int>> current_fds = GetOpenFds(fail_fn);
2868 auto difference = std::make_unique<std::set<int>>();
2869 std::set_difference(current_fds->begin(), current_fds->end(), gPreloadFds->begin(),
2870 gPreloadFds->end(), std::inserter(*difference, difference->end()));
2871 delete gPreloadFds;
2872 gPreloadFds = difference.release();
2873 gPreloadFdsExtracted = true;
2874 }
2875
2876 static const JNINativeMethod gMethods[] = {
2877 {"nativeForkAndSpecialize",
2878 "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/"
2879 "String;Z[Ljava/lang/String;[Ljava/lang/String;ZZ)I",
2880 (void*)com_android_internal_os_Zygote_nativeForkAndSpecialize},
2881 {"nativeForkSystemServer", "(II[II[[IJJ)I",
2882 (void*)com_android_internal_os_Zygote_nativeForkSystemServer},
2883 {"nativeAllowFileAcrossFork", "(Ljava/lang/String;)V",
2884 (void*)com_android_internal_os_Zygote_nativeAllowFileAcrossFork},
2885 {"nativePreApplicationInit", "()V",
2886 (void*)com_android_internal_os_Zygote_nativePreApplicationInit},
2887 {"nativeInstallSeccompUidGidFilter", "(II)V",
2888 (void*)com_android_internal_os_Zygote_nativeInstallSeccompUidGidFilter},
2889 {"nativeForkApp", "(II[IZZ)I", (void*)com_android_internal_os_Zygote_nativeForkApp},
2890 // @CriticalNative
2891 {"nativeAddUsapTableEntry", "(II)V",
2892 (void*)com_android_internal_os_Zygote_nativeAddUsapTableEntry},
2893 {"nativeSpecializeAppProcess",
2894 "(II[II[[IILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/"
2895 "String;Z[Ljava/lang/String;[Ljava/lang/String;ZZ)V",
2896 (void*)com_android_internal_os_Zygote_nativeSpecializeAppProcess},
2897 {"nativeInitNativeState", "(Z)V",
2898 (void*)com_android_internal_os_Zygote_nativeInitNativeState},
2899 {"nativeGetUsapPipeFDs", "()[I",
2900 (void*)com_android_internal_os_Zygote_nativeGetUsapPipeFDs},
2901 // @CriticalNative
2902 {"nativeAddUsapTableEntry", "(II)V",
2903 (void*)com_android_internal_os_Zygote_nativeAddUsapTableEntry},
2904 // @CriticalNative
2905 {"nativeRemoveUsapTableEntry", "(I)Z",
2906 (void*)com_android_internal_os_Zygote_nativeRemoveUsapTableEntry},
2907 {"nativeGetUsapPoolEventFD", "()I",
2908 (void*)com_android_internal_os_Zygote_nativeGetUsapPoolEventFD},
2909 {"nativeGetUsapPoolCount", "()I",
2910 (void*)com_android_internal_os_Zygote_nativeGetUsapPoolCount},
2911 {"nativeEmptyUsapPool", "()V", (void*)com_android_internal_os_Zygote_nativeEmptyUsapPool},
2912 {"nativeBlockSigTerm", "()V", (void*)com_android_internal_os_Zygote_nativeBlockSigTerm},
2913 {"nativeUnblockSigTerm", "()V", (void*)com_android_internal_os_Zygote_nativeUnblockSigTerm},
2914 {"nativeBoostUsapPriority", "()V",
2915 (void*)com_android_internal_os_Zygote_nativeBoostUsapPriority},
2916 {"nativeParseSigChld", "([BI[I)I",
2917 (void*)com_android_internal_os_Zygote_nativeParseSigChld},
2918 {"nativeSupportsMemoryTagging", "()Z",
2919 (void*)com_android_internal_os_Zygote_nativeSupportsMemoryTagging},
2920 {"nativeSupportsTaggedPointers", "()Z",
2921 (void*)com_android_internal_os_Zygote_nativeSupportsTaggedPointers},
2922 {"nativeCurrentTaggingLevel", "()I",
2923 (void*)com_android_internal_os_Zygote_nativeCurrentTaggingLevel},
2924 {"nativeMarkOpenedFilesBeforePreload", "()V",
2925 (void*)com_android_internal_os_Zygote_nativeMarkOpenedFilesBeforePreload},
2926 {"nativeAllowFilesOpenedByPreload", "()V",
2927 (void*)com_android_internal_os_Zygote_nativeAllowFilesOpenedByPreload},
2928 };
2929
register_com_android_internal_os_Zygote(JNIEnv * env)2930 int register_com_android_internal_os_Zygote(JNIEnv* env) {
2931 gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName));
2932 gCallPostForkSystemServerHooks = GetStaticMethodIDOrDie(env, gZygoteClass,
2933 "callPostForkSystemServerHooks",
2934 "(I)V");
2935 gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks",
2936 "(IZZLjava/lang/String;)V");
2937
2938 gZygoteInitClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteInitClassName));
2939 gGetOrCreateSystemServerClassLoader =
2940 GetStaticMethodIDOrDie(env, gZygoteInitClass, "getOrCreateSystemServerClassLoader",
2941 "()Ljava/lang/ClassLoader;");
2942 gPrefetchStandaloneSystemServerJars =
2943 GetStaticMethodIDOrDie(env, gZygoteInitClass, "prefetchStandaloneSystemServerJars",
2944 "()V");
2945
2946 RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods));
2947
2948 return JNI_OK;
2949 }
2950 } // namespace android
2951