blob: b28f4cdd5a87ec0e282b03dbd1c30da8324d91e6 [file] [log] [blame]
Narayan Kamath973b4662014-03-31 13:41:26 +01001/*
2 * Copyright (C) 2014 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
17package com.android.internal.os;
18
Jeff Sharkeyace874b2017-09-07 15:27:33 -060019import android.os.IVold;
Narayan Kamathfbb32f62015-06-12 15:34:35 +010020import android.os.Trace;
Elliott Hughes860c5912014-04-28 19:19:13 -070021import android.system.ErrnoException;
22import android.system.Os;
Narayan Kamath973b4662014-03-31 13:41:26 +010023
Jeff Sharkeyace874b2017-09-07 15:27:33 -060024import dalvik.system.ZygoteHooks;
Tobias Sargeantb9679dc2016-01-19 16:34:54 +000025
Narayan Kamath973b4662014-03-31 13:41:26 +010026/** @hide */
27public final class Zygote {
28 /*
Nicolas Geoffray81edac42017-09-07 14:13:29 +010029 * Bit values for "runtimeFlags" argument. The definitions are duplicated
Narayan Kamath973b4662014-03-31 13:41:26 +010030 * in the native code.
31 */
32
33 /** enable debugging over JDWP */
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000034 public static final int DEBUG_ENABLE_JDWP = 1;
Narayan Kamath973b4662014-03-31 13:41:26 +010035 /** enable JNI checks */
36 public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1;
37 /** enable Java programming language "assert" statements */
38 public static final int DEBUG_ENABLE_ASSERT = 1 << 2;
Mathieu Chartier7a490282015-03-17 09:51:36 -070039 /** disable the AOT compiler and JIT */
Narayan Kamath973b4662014-03-31 13:41:26 +010040 public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3;
41 /** Enable logging of third-party JNI activity. */
42 public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4;
David Srbecky065075e2015-05-28 17:16:09 +010043 /** Force generation of native debugging information. */
Nicolas Geoffray9abbf452015-11-05 11:29:42 +000044 public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5;
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000045 /** Always use JIT-ed code. */
46 public static final int DEBUG_ALWAYS_JIT = 1 << 6;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000047 /** Make the code native debuggable by turning off some optimizations. */
Tamas Berghammerdf6cb282016-01-29 12:07:00 +000048 public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7;
Nicolas Geoffray347b1df2016-12-20 14:05:05 +000049 /** Make the code Java debuggable by turning off some optimizations. */
50 public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8;
Mathieu Chartier7a490282015-03-17 09:51:36 -070051
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010052 /** Turn off the verifier. */
53 public static final int DISABLE_VERIFIER = 1 << 9;
54 /** Only use oat files located in /system. Otherwise use dex/jar/apk . */
55 public static final int ONLY_USE_SYSTEM_OAT_FILES = 1 << 10;
David Srbecky156ed922018-01-30 14:37:37 +000056 /** Force generation of native debugging information for backtraces. */
Mathew Inwood16073b82018-03-23 10:05:01 +000057 public static final int DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11;
58 /**
59 * Hidden API access restrictions. This is a mask for bits representing the API enforcement
60 * policy, defined by {@code @ApplicationInfo.HiddenApiEnforcementPolicy}.
61 */
62 public static final int API_ENFORCEMENT_POLICY_MASK = (1 << 12) | (1 << 13);
63 /**
64 * Bit shift for use with {@link #API_ENFORCEMENT_POLICY_MASK}.
65 *
66 * (flags & API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT gives
67 * @ApplicationInfo.ApiEnforcementPolicy values.
68 */
69 public static final int API_ENFORCEMENT_POLICY_SHIFT =
70 Integer.numberOfTrailingZeros(API_ENFORCEMENT_POLICY_MASK);
Calin Juravle8eb891b2018-05-03 19:51:18 -070071 /**
72 * Enable system server ART profiling.
73 */
74 public static final int PROFILE_SYSTEM_SERVER = 1 << 14;
Nicolas Geoffray1f88ad62017-09-13 14:21:00 +010075
Narayan Kamath973b4662014-03-31 13:41:26 +010076 /** No external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060077 public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE;
Jeff Sharkey9527b222015-06-24 15:24:48 -070078 /** Default external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060079 public static final int MOUNT_EXTERNAL_DEFAULT = IVold.REMOUNT_MODE_DEFAULT;
Jeff Sharkey9527b222015-06-24 15:24:48 -070080 /** Read-only external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060081 public static final int MOUNT_EXTERNAL_READ = IVold.REMOUNT_MODE_READ;
Jeff Sharkey9527b222015-06-24 15:24:48 -070082 /** Read-write external storage should be mounted. */
Jeff Sharkeyace874b2017-09-07 15:27:33 -060083 public static final int MOUNT_EXTERNAL_WRITE = IVold.REMOUNT_MODE_WRITE;
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080084 /**
Sudheer Shanka0b6da532019-01-09 12:06:51 -080085 * Mount mode for apps that are already installed on the device before the isolated_storage
86 * feature is enabled.
87 */
88 public static final int MOUNT_EXTERNAL_LEGACY = IVold.REMOUNT_MODE_LEGACY;
89 /**
Sudheer Shanka3a0df3b2018-12-12 12:43:43 -080090 * Mount mode for package installers which should give them access to
91 * all obb dirs in addition to their package sandboxes
92 */
93 public static final int MOUNT_EXTERNAL_INSTALLER = IVold.REMOUNT_MODE_INSTALLER;
Sudheer Shanka98cb3f02018-08-17 16:10:29 -070094 /** Read-write external storage should be mounted instead of package sandbox */
95 public static final int MOUNT_EXTERNAL_FULL = IVold.REMOUNT_MODE_FULL;
Narayan Kamath973b4662014-03-31 13:41:26 +010096
97 private static final ZygoteHooks VM_HOOKS = new ZygoteHooks();
98
Robert Sesekd0a190df2018-02-12 18:46:01 -050099 /**
100 * An extraArg passed when a zygote process is forking a child-zygote, specifying a name
101 * in the abstract socket namespace. This socket name is what the new child zygote
102 * should listen for connections on.
103 */
104 public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket=";
105
Martijn Coenen7e6fa672018-11-05 11:45:26 +0100106 /**
107 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
108 * requested ABI for the child Zygote.
109 */
110 public static final String CHILD_ZYGOTE_ABI_LIST_ARG = "--abi-list=";
111
Martijn Coenen86f08a52019-01-03 16:23:01 +0100112 /**
113 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
114 * start of the UID range the children of the Zygote may setuid()/setgid() to. This
115 * will be enforced with a seccomp filter.
116 */
117 public static final String CHILD_ZYGOTE_UID_RANGE_START = "--uid-range-start=";
118
119 /**
120 * An extraArg passed when a zygote process is forking a child-zygote, specifying the
121 * end of the UID range the children of the Zygote may setuid()/setgid() to. This
122 * will be enforced with a seccomp filter.
123 */
124 public static final String CHILD_ZYGOTE_UID_RANGE_END = "--uid-range-end=";
125
Narayan Kamath973b4662014-03-31 13:41:26 +0100126 private Zygote() {}
127
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800128 /** Called for some security initialization before any fork. */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800129 static native void nativeSecurityInit();
Victor Hsiehc8176ef2018-01-08 12:43:00 -0800130
Narayan Kamath973b4662014-03-31 13:41:26 +0100131 /**
132 * Forks a new VM instance. The current VM must have been started
133 * with the -Xzygote flag. <b>NOTE: new instance keeps all
134 * root capabilities. The new process is expected to call capset()</b>.
135 *
136 * @param uid the UNIX uid that the new process should setuid() to after
137 * fork()ing and and before spawning any threads.
138 * @param gid the UNIX gid that the new process should setgid() to after
139 * fork()ing and and before spawning any threads.
140 * @param gids null-ok; a list of UNIX gids that the new process should
141 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100142 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100143 * @param rlimits null-ok an array of rlimit tuples, with the second
144 * dimension having a length of 3 and representing
145 * (resource, rlim_cur, rlim_max). These are set via the posix
146 * setrlimit(2) call.
147 * @param seInfo null-ok a string specifying SELinux information for
148 * the new process.
149 * @param niceName null-ok a string specifying the process name.
150 * @param fdsToClose an array of ints, holding one or more POSIX
151 * file descriptor numbers that are to be closed by the child
152 * (and replaced by /dev/null) after forking. An integer value
153 * of -1 in any entry in the array means "ignore this one".
Andreas Gampe8dfa1782017-01-05 12:45:58 -0800154 * @param fdsToIgnore null-ok an array of ints, either null or holding
155 * one or more POSIX file descriptor numbers that are to be ignored
156 * in the file descriptor table check.
Robert Sesekd0a190df2018-02-12 18:46:01 -0500157 * @param startChildZygote if true, the new child process will itself be a
158 * new zygote process.
Andreas Gampeaec67dc2014-09-02 21:23:06 -0700159 * @param instructionSet null-ok the instruction set to use.
jgu212eacd062014-09-10 06:55:07 -0400160 * @param appDataDir null-ok the data directory of the app.
Narayan Kamath973b4662014-03-31 13:41:26 +0100161 *
162 * @return 0 if this is the child, pid of the child
163 * if this is the parent, or -1 on error.
164 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100165 public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags,
Sudheer Shanka3f0645b2018-09-18 13:07:59 -0700166 int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose,
167 int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir,
168 String packageName, String[] packagesForUid, String[] visibleVolIds) {
Narayan Kamath973b4662014-03-31 13:41:26 +0100169 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800170 // Resets nice priority for zygote process.
171 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100172 int pid = nativeForkAndSpecialize(
Chris Wailesaa1c9622019-01-10 16:55:32 -0800173 uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose,
174 fdsToIgnore, startChildZygote, instructionSet, appDataDir, packageName,
175 packagesForUid, visibleVolIds);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100176 // Enable tracing as soon as possible for the child process.
177 if (pid == 0) {
Andreas Gampe8f4eab22017-09-13 18:16:13 -0700178 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100179
180 // Note that this event ends at the end of handleChildProc,
181 Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork");
182 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100183 VM_HOOKS.postForkCommon();
184 return pid;
185 }
186
Chris Wailesaa1c9622019-01-10 16:55:32 -0800187 private static native int nativeForkAndSpecialize(int uid, int gid, int[] gids,
188 int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName,
189 int[] fdsToClose, int[] fdsToIgnore, boolean startChildZygote, String instructionSet,
190 String appDataDir, String packageName, String[] packagesForUid, String[] visibleVolIds);
191
192 private static native void nativeSpecializeBlastula(int uid, int gid, int[] gids,
193 int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName,
194 boolean startChildZygote, String instructionSet, String appDataDir, String packageName,
195 String[] packagesForUid, String[] visibleVolIds);
Narayan Kamath973b4662014-03-31 13:41:26 +0100196
197 /**
Christopher Ferris2980de42017-06-20 16:13:40 -0700198 * Called to do any initialization before starting an application.
199 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800200 static native void nativePreApplicationInit();
Christopher Ferris2980de42017-06-20 16:13:40 -0700201
202 /**
Narayan Kamath973b4662014-03-31 13:41:26 +0100203 * Special method to start the system server process. In addition to the
204 * common actions performed in forkAndSpecialize, the pid of the child
205 * process is recorded such that the death of the child process will cause
206 * zygote to exit.
207 *
208 * @param uid the UNIX uid that the new process should setuid() to after
209 * fork()ing and and before spawning any threads.
210 * @param gid the UNIX gid that the new process should setgid() to after
211 * fork()ing and and before spawning any threads.
212 * @param gids null-ok; a list of UNIX gids that the new process should
213 * setgroups() to after fork and before spawning any threads.
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100214 * @param runtimeFlags bit flags that enable ART features.
Narayan Kamath973b4662014-03-31 13:41:26 +0100215 * @param rlimits null-ok an array of rlimit tuples, with the second
216 * dimension having a length of 3 and representing
217 * (resource, rlim_cur, rlim_max). These are set via the posix
218 * setrlimit(2) call.
219 * @param permittedCapabilities argument for setcap()
220 * @param effectiveCapabilities argument for setcap()
221 *
222 * @return 0 if this is the child, pid of the child
223 * if this is the parent, or -1 on error.
224 */
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100225 public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100226 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) {
227 VM_HOOKS.preFork();
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800228 // Resets nice priority for zygote process.
229 resetNicePriority();
Narayan Kamath973b4662014-03-31 13:41:26 +0100230 int pid = nativeForkSystemServer(
Chris Wailesaa1c9622019-01-10 16:55:32 -0800231 uid, gid, gids, runtimeFlags, rlimits,
232 permittedCapabilities, effectiveCapabilities);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100233 // Enable tracing as soon as we enter the system_server.
234 if (pid == 0) {
Andreas Gampe8f4eab22017-09-13 18:16:13 -0700235 Trace.setTracingEnabled(true, runtimeFlags);
Narayan Kamathfbb32f62015-06-12 15:34:35 +0100236 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100237 VM_HOOKS.postForkCommon();
238 return pid;
239 }
240
Chris Wailesaa1c9622019-01-10 16:55:32 -0800241 private static native int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags,
Narayan Kamath973b4662014-03-31 13:41:26 +0100242 int[][] rlimits, long permittedCapabilities, long effectiveCapabilities);
243
doheon1.lee885b7422016-01-20 13:07:27 +0900244 /**
Robert Sesek54e387d2016-12-02 17:27:50 -0500245 * Lets children of the zygote inherit open file descriptors to this path.
246 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800247 protected static native void nativeAllowFileAcrossFork(String path);
Robert Sesek54e387d2016-12-02 17:27:50 -0500248
249 /**
Martijn Coenen86f08a52019-01-03 16:23:01 +0100250 * Installs a seccomp filter that limits setresuid()/setresgid() to the passed-in range
251 * @param uidGidMin The smallest allowed uid/gid
252 * @param uidGidMax The largest allowed uid/gid
253 */
254 native protected static void nativeInstallSeccompUidGidFilter(int uidGidMin, int uidGidMax);
255
256 /**
doheon1.lee885b7422016-01-20 13:07:27 +0900257 * Zygote unmount storage space on initializing.
258 * This method is called once.
259 */
Chris Wailesaa1c9622019-01-10 16:55:32 -0800260 protected static native void nativeUnmountStorageOnInit();
261
262 protected static native void nativeGetSocketFDs(boolean isPrimary);
263
264 private static native int nativeGetBlastulaPoolCount();
265
266 private static native int nativeGetBlastulaPoolEventFD();
267
268 private static native int nativeForkBlastula(int readPipeFD,
269 int writePipeFD,
270 int[] sessionSocketRawFDs);
271
272 private static native int[] nativeGetBlastulaPipeFDs();
273
274 private static native boolean nativeRemoveBlastulaTableEntry(int blastulaPID);
275
doheon1.lee885b7422016-01-20 13:07:27 +0900276
Orion Hodson46724e72018-10-19 13:05:33 +0100277 private static void callPostForkSystemServerHooks() {
278 // SystemServer specific post fork hooks run before child post fork hooks.
279 VM_HOOKS.postForkSystemServer();
280 }
281
Nicolas Geoffray81edac42017-09-07 14:13:29 +0100282 private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer,
Robert Sesekd0a190df2018-02-12 18:46:01 -0500283 boolean isZygote, String instructionSet) {
284 VM_HOOKS.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet);
Narayan Kamath973b4662014-03-31 13:41:26 +0100285 }
286
Narayan Kamathb49996d2017-02-06 20:24:08 +0000287 /**
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800288 * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY
289 * or nice value 0). This updates both the priority value in java.lang.Thread and
290 * the nice value (setpriority).
Narayan Kamathb49996d2017-02-06 20:24:08 +0000291 */
Hiroshi Yamauchi1e3db872017-03-02 13:39:07 -0800292 static void resetNicePriority() {
293 Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
294 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100295
296 /**
297 * Executes "/system/bin/sh -c &lt;command&gt;" using the exec() system call.
298 * This method throws a runtime exception if exec() failed, otherwise, this
299 * method never returns.
300 *
301 * @param command The shell command to execute.
302 */
303 public static void execShell(String command) {
304 String[] args = { "/system/bin/sh", "-c", command };
305 try {
Elliott Hughes860c5912014-04-28 19:19:13 -0700306 Os.execv(args[0], args);
Narayan Kamath973b4662014-03-31 13:41:26 +0100307 } catch (ErrnoException e) {
308 throw new RuntimeException(e);
309 }
310 }
311
312 /**
313 * Appends quotes shell arguments to the specified string builder.
314 * The arguments are quoted using single-quotes, escaped if necessary,
315 * prefixed with a space, and appended to the command.
316 *
317 * @param command A string builder for the shell command being constructed.
318 * @param args An array of argument strings to be quoted and appended to the command.
319 * @see #execShell(String)
320 */
321 public static void appendQuotedShellArgs(StringBuilder command, String[] args) {
322 for (String arg : args) {
323 command.append(" '").append(arg.replace("'", "'\\''")).append("'");
324 }
325 }
Narayan Kamath973b4662014-03-31 13:41:26 +0100326}