Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.os; |
| 18 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 19 | import static com.android.internal.os.ZygoteConnectionConstants.MAX_ZYGOTE_ARGC; |
| 20 | |
| 21 | import android.net.Credentials; |
| 22 | import android.os.FactoryTest; |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 23 | import android.os.IVold; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 24 | import android.os.Process; |
| 25 | import android.os.SystemProperties; |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 26 | import android.os.Trace; |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 27 | import android.system.ErrnoException; |
| 28 | import android.system.Os; |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 29 | import android.util.Log; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 30 | |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 31 | import dalvik.system.ZygoteHooks; |
Tobias Sargeant | b9679dc | 2016-01-19 16:34:54 +0000 | [diff] [blame] | 32 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 33 | import java.io.BufferedReader; |
| 34 | import java.io.IOException; |
| 35 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 36 | /** @hide */ |
| 37 | public final class Zygote { |
| 38 | /* |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 39 | * Bit values for "runtimeFlags" argument. The definitions are duplicated |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 40 | * in the native code. |
| 41 | */ |
| 42 | |
| 43 | /** enable debugging over JDWP */ |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 44 | public static final int DEBUG_ENABLE_JDWP = 1; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 45 | /** enable JNI checks */ |
| 46 | public static final int DEBUG_ENABLE_CHECKJNI = 1 << 1; |
| 47 | /** enable Java programming language "assert" statements */ |
| 48 | public static final int DEBUG_ENABLE_ASSERT = 1 << 2; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 49 | /** disable the AOT compiler and JIT */ |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 50 | public static final int DEBUG_ENABLE_SAFEMODE = 1 << 3; |
| 51 | /** Enable logging of third-party JNI activity. */ |
| 52 | public static final int DEBUG_ENABLE_JNI_LOGGING = 1 << 4; |
David Srbecky | 065075e | 2015-05-28 17:16:09 +0100 | [diff] [blame] | 53 | /** Force generation of native debugging information. */ |
Nicolas Geoffray | 9abbf45 | 2015-11-05 11:29:42 +0000 | [diff] [blame] | 54 | public static final int DEBUG_GENERATE_DEBUG_INFO = 1 << 5; |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 55 | /** Always use JIT-ed code. */ |
| 56 | public static final int DEBUG_ALWAYS_JIT = 1 << 6; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 57 | /** Make the code native debuggable by turning off some optimizations. */ |
Tamas Berghammer | df6cb28 | 2016-01-29 12:07:00 +0000 | [diff] [blame] | 58 | public static final int DEBUG_NATIVE_DEBUGGABLE = 1 << 7; |
Nicolas Geoffray | 347b1df | 2016-12-20 14:05:05 +0000 | [diff] [blame] | 59 | /** Make the code Java debuggable by turning off some optimizations. */ |
| 60 | public static final int DEBUG_JAVA_DEBUGGABLE = 1 << 8; |
Mathieu Chartier | 7a49028 | 2015-03-17 09:51:36 -0700 | [diff] [blame] | 61 | |
Nicolas Geoffray | 1f88ad6 | 2017-09-13 14:21:00 +0100 | [diff] [blame] | 62 | /** Turn off the verifier. */ |
| 63 | public static final int DISABLE_VERIFIER = 1 << 9; |
| 64 | /** Only use oat files located in /system. Otherwise use dex/jar/apk . */ |
| 65 | public static final int ONLY_USE_SYSTEM_OAT_FILES = 1 << 10; |
David Srbecky | 156ed92 | 2018-01-30 14:37:37 +0000 | [diff] [blame] | 66 | /** Force generation of native debugging information for backtraces. */ |
Mathew Inwood | 16073b8 | 2018-03-23 10:05:01 +0000 | [diff] [blame] | 67 | public static final int DEBUG_GENERATE_MINI_DEBUG_INFO = 1 << 11; |
| 68 | /** |
| 69 | * Hidden API access restrictions. This is a mask for bits representing the API enforcement |
| 70 | * policy, defined by {@code @ApplicationInfo.HiddenApiEnforcementPolicy}. |
| 71 | */ |
| 72 | public static final int API_ENFORCEMENT_POLICY_MASK = (1 << 12) | (1 << 13); |
| 73 | /** |
| 74 | * Bit shift for use with {@link #API_ENFORCEMENT_POLICY_MASK}. |
| 75 | * |
| 76 | * (flags & API_ENFORCEMENT_POLICY_MASK) >> API_ENFORCEMENT_POLICY_SHIFT gives |
| 77 | * @ApplicationInfo.ApiEnforcementPolicy values. |
| 78 | */ |
| 79 | public static final int API_ENFORCEMENT_POLICY_SHIFT = |
| 80 | Integer.numberOfTrailingZeros(API_ENFORCEMENT_POLICY_MASK); |
Calin Juravle | 8eb891b | 2018-05-03 19:51:18 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Enable system server ART profiling. |
| 83 | */ |
| 84 | public static final int PROFILE_SYSTEM_SERVER = 1 << 14; |
Nicolas Geoffray | 1f88ad6 | 2017-09-13 14:21:00 +0100 | [diff] [blame] | 85 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 86 | /** No external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 87 | public static final int MOUNT_EXTERNAL_NONE = IVold.REMOUNT_MODE_NONE; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 88 | /** Default external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 89 | public static final int MOUNT_EXTERNAL_DEFAULT = IVold.REMOUNT_MODE_DEFAULT; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 90 | /** Read-only external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 91 | public static final int MOUNT_EXTERNAL_READ = IVold.REMOUNT_MODE_READ; |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 92 | /** Read-write external storage should be mounted. */ |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 93 | public static final int MOUNT_EXTERNAL_WRITE = IVold.REMOUNT_MODE_WRITE; |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 94 | /** |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 95 | * Mount mode for apps that are already installed on the device before the isolated_storage |
| 96 | * feature is enabled. |
| 97 | */ |
| 98 | public static final int MOUNT_EXTERNAL_LEGACY = IVold.REMOUNT_MODE_LEGACY; |
| 99 | /** |
Sudheer Shanka | 3a0df3b | 2018-12-12 12:43:43 -0800 | [diff] [blame] | 100 | * Mount mode for package installers which should give them access to |
| 101 | * all obb dirs in addition to their package sandboxes |
| 102 | */ |
| 103 | public static final int MOUNT_EXTERNAL_INSTALLER = IVold.REMOUNT_MODE_INSTALLER; |
Sudheer Shanka | 98cb3f0 | 2018-08-17 16:10:29 -0700 | [diff] [blame] | 104 | /** Read-write external storage should be mounted instead of package sandbox */ |
| 105 | public static final int MOUNT_EXTERNAL_FULL = IVold.REMOUNT_MODE_FULL; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 106 | |
| 107 | private static final ZygoteHooks VM_HOOKS = new ZygoteHooks(); |
| 108 | |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 109 | /** |
| 110 | * An extraArg passed when a zygote process is forking a child-zygote, specifying a name |
| 111 | * in the abstract socket namespace. This socket name is what the new child zygote |
| 112 | * should listen for connections on. |
| 113 | */ |
| 114 | public static final String CHILD_ZYGOTE_SOCKET_NAME_ARG = "--zygote-socket="; |
| 115 | |
Martijn Coenen | 7e6fa67 | 2018-11-05 11:45:26 +0100 | [diff] [blame] | 116 | /** |
| 117 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 118 | * requested ABI for the child Zygote. |
| 119 | */ |
| 120 | public static final String CHILD_ZYGOTE_ABI_LIST_ARG = "--abi-list="; |
| 121 | |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 122 | /** |
| 123 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 124 | * start of the UID range the children of the Zygote may setuid()/setgid() to. This |
| 125 | * will be enforced with a seccomp filter. |
| 126 | */ |
| 127 | public static final String CHILD_ZYGOTE_UID_RANGE_START = "--uid-range-start="; |
| 128 | |
| 129 | /** |
| 130 | * An extraArg passed when a zygote process is forking a child-zygote, specifying the |
| 131 | * end of the UID range the children of the Zygote may setuid()/setgid() to. This |
| 132 | * will be enforced with a seccomp filter. |
| 133 | */ |
| 134 | public static final String CHILD_ZYGOTE_UID_RANGE_END = "--uid-range-end="; |
| 135 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 136 | /** a prototype instance for a future List.toArray() */ |
| 137 | protected static final int[][] INT_ARRAY_2D = new int[0][0]; |
| 138 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 139 | private Zygote() {} |
| 140 | |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 141 | /** Called for some security initialization before any fork. */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 142 | static native void nativeSecurityInit(); |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 143 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 144 | /** |
| 145 | * Forks a new VM instance. The current VM must have been started |
| 146 | * with the -Xzygote flag. <b>NOTE: new instance keeps all |
| 147 | * root capabilities. The new process is expected to call capset()</b>. |
| 148 | * |
| 149 | * @param uid the UNIX uid that the new process should setuid() to after |
| 150 | * fork()ing and and before spawning any threads. |
| 151 | * @param gid the UNIX gid that the new process should setgid() to after |
| 152 | * fork()ing and and before spawning any threads. |
| 153 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 154 | * setgroups() to after fork and before spawning any threads. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 155 | * @param runtimeFlags bit flags that enable ART features. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 156 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 157 | * dimension having a length of 3 and representing |
| 158 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 159 | * setrlimit(2) call. |
| 160 | * @param seInfo null-ok a string specifying SELinux information for |
| 161 | * the new process. |
| 162 | * @param niceName null-ok a string specifying the process name. |
| 163 | * @param fdsToClose an array of ints, holding one or more POSIX |
| 164 | * file descriptor numbers that are to be closed by the child |
| 165 | * (and replaced by /dev/null) after forking. An integer value |
| 166 | * of -1 in any entry in the array means "ignore this one". |
Andreas Gampe | 8dfa178 | 2017-01-05 12:45:58 -0800 | [diff] [blame] | 167 | * @param fdsToIgnore null-ok an array of ints, either null or holding |
| 168 | * one or more POSIX file descriptor numbers that are to be ignored |
| 169 | * in the file descriptor table check. |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 170 | * @param startChildZygote if true, the new child process will itself be a |
| 171 | * new zygote process. |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 172 | * @param instructionSet null-ok the instruction set to use. |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 173 | * @param appDataDir null-ok the data directory of the app. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 174 | * |
| 175 | * @return 0 if this is the child, pid of the child |
| 176 | * if this is the parent, or -1 on error. |
| 177 | */ |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 178 | public static int forkAndSpecialize(int uid, int gid, int[] gids, int runtimeFlags, |
Sudheer Shanka | 3f0645b | 2018-09-18 13:07:59 -0700 | [diff] [blame] | 179 | int[][] rlimits, int mountExternal, String seInfo, String niceName, int[] fdsToClose, |
| 180 | int[] fdsToIgnore, boolean startChildZygote, String instructionSet, String appDataDir, |
| 181 | String packageName, String[] packagesForUid, String[] visibleVolIds) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 182 | VM_HOOKS.preFork(); |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 183 | // Resets nice priority for zygote process. |
| 184 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 185 | int pid = nativeForkAndSpecialize( |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 186 | uid, gid, gids, runtimeFlags, rlimits, mountExternal, seInfo, niceName, fdsToClose, |
| 187 | fdsToIgnore, startChildZygote, instructionSet, appDataDir, packageName, |
| 188 | packagesForUid, visibleVolIds); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 189 | // Enable tracing as soon as possible for the child process. |
| 190 | if (pid == 0) { |
Andreas Gampe | 8f4eab2 | 2017-09-13 18:16:13 -0700 | [diff] [blame] | 191 | Trace.setTracingEnabled(true, runtimeFlags); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 192 | |
| 193 | // Note that this event ends at the end of handleChildProc, |
| 194 | Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "PostFork"); |
| 195 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 196 | VM_HOOKS.postForkCommon(); |
| 197 | return pid; |
| 198 | } |
| 199 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 200 | private static native int nativeForkAndSpecialize(int uid, int gid, int[] gids, |
| 201 | int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName, |
| 202 | int[] fdsToClose, int[] fdsToIgnore, boolean startChildZygote, String instructionSet, |
| 203 | String appDataDir, String packageName, String[] packagesForUid, String[] visibleVolIds); |
| 204 | |
| 205 | private static native void nativeSpecializeBlastula(int uid, int gid, int[] gids, |
| 206 | int runtimeFlags, int[][] rlimits, int mountExternal, String seInfo, String niceName, |
| 207 | boolean startChildZygote, String instructionSet, String appDataDir, String packageName, |
| 208 | String[] packagesForUid, String[] visibleVolIds); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 209 | |
| 210 | /** |
Christopher Ferris | 2980de4 | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 211 | * Called to do any initialization before starting an application. |
| 212 | */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 213 | static native void nativePreApplicationInit(); |
Christopher Ferris | 2980de4 | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 216 | * Special method to start the system server process. In addition to the |
| 217 | * common actions performed in forkAndSpecialize, the pid of the child |
| 218 | * process is recorded such that the death of the child process will cause |
| 219 | * zygote to exit. |
| 220 | * |
| 221 | * @param uid the UNIX uid that the new process should setuid() to after |
| 222 | * fork()ing and and before spawning any threads. |
| 223 | * @param gid the UNIX gid that the new process should setgid() to after |
| 224 | * fork()ing and and before spawning any threads. |
| 225 | * @param gids null-ok; a list of UNIX gids that the new process should |
| 226 | * setgroups() to after fork and before spawning any threads. |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 227 | * @param runtimeFlags bit flags that enable ART features. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 228 | * @param rlimits null-ok an array of rlimit tuples, with the second |
| 229 | * dimension having a length of 3 and representing |
| 230 | * (resource, rlim_cur, rlim_max). These are set via the posix |
| 231 | * setrlimit(2) call. |
| 232 | * @param permittedCapabilities argument for setcap() |
| 233 | * @param effectiveCapabilities argument for setcap() |
| 234 | * |
| 235 | * @return 0 if this is the child, pid of the child |
| 236 | * if this is the parent, or -1 on error. |
| 237 | */ |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 238 | public static int forkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 239 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities) { |
| 240 | VM_HOOKS.preFork(); |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 241 | // Resets nice priority for zygote process. |
| 242 | resetNicePriority(); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 243 | int pid = nativeForkSystemServer( |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 244 | uid, gid, gids, runtimeFlags, rlimits, |
| 245 | permittedCapabilities, effectiveCapabilities); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 246 | // Enable tracing as soon as we enter the system_server. |
| 247 | if (pid == 0) { |
Andreas Gampe | 8f4eab2 | 2017-09-13 18:16:13 -0700 | [diff] [blame] | 248 | Trace.setTracingEnabled(true, runtimeFlags); |
Narayan Kamath | fbb32f6 | 2015-06-12 15:34:35 +0100 | [diff] [blame] | 249 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 250 | VM_HOOKS.postForkCommon(); |
| 251 | return pid; |
| 252 | } |
| 253 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 254 | private static native int nativeForkSystemServer(int uid, int gid, int[] gids, int runtimeFlags, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 255 | int[][] rlimits, long permittedCapabilities, long effectiveCapabilities); |
| 256 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 257 | /** |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 258 | * Lets children of the zygote inherit open file descriptors to this path. |
| 259 | */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 260 | protected static native void nativeAllowFileAcrossFork(String path); |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 261 | |
| 262 | /** |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 263 | * Installs a seccomp filter that limits setresuid()/setresgid() to the passed-in range |
| 264 | * @param uidGidMin The smallest allowed uid/gid |
| 265 | * @param uidGidMax The largest allowed uid/gid |
| 266 | */ |
| 267 | native protected static void nativeInstallSeccompUidGidFilter(int uidGidMin, int uidGidMax); |
| 268 | |
| 269 | /** |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 270 | * Zygote unmount storage space on initializing. |
| 271 | * This method is called once. |
| 272 | */ |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 273 | protected static native void nativeUnmountStorageOnInit(); |
| 274 | |
| 275 | protected static native void nativeGetSocketFDs(boolean isPrimary); |
| 276 | |
| 277 | private static native int nativeGetBlastulaPoolCount(); |
| 278 | |
| 279 | private static native int nativeGetBlastulaPoolEventFD(); |
| 280 | |
| 281 | private static native int nativeForkBlastula(int readPipeFD, |
| 282 | int writePipeFD, |
| 283 | int[] sessionSocketRawFDs); |
| 284 | |
| 285 | private static native int[] nativeGetBlastulaPipeFDs(); |
| 286 | |
| 287 | private static native boolean nativeRemoveBlastulaTableEntry(int blastulaPID); |
| 288 | |
Chris Wailes | 2be2626 | 2019-01-11 16:14:43 -0800 | [diff] [blame^] | 289 | /** |
| 290 | * uid 1000 (Process.SYSTEM_UID) may specify any uid > 1000 in normal |
| 291 | * operation. It may also specify any gid and setgroups() list it chooses. |
| 292 | * In factory test mode, it may specify any UID. |
| 293 | * |
| 294 | * @param args non-null; zygote spawner arguments |
| 295 | * @param peer non-null; peer credentials |
| 296 | * @throws ZygoteSecurityException |
| 297 | */ |
| 298 | protected static void applyUidSecurityPolicy(ZygoteArguments args, Credentials peer) |
| 299 | throws ZygoteSecurityException { |
| 300 | |
| 301 | if (peer.getUid() == Process.SYSTEM_UID) { |
| 302 | /* In normal operation, SYSTEM_UID can only specify a restricted |
| 303 | * set of UIDs. In factory test mode, SYSTEM_UID may specify any uid. |
| 304 | */ |
| 305 | boolean uidRestricted = FactoryTest.getMode() == FactoryTest.FACTORY_TEST_OFF; |
| 306 | |
| 307 | if (uidRestricted && args.mUidSpecified && (args.mUid < Process.SYSTEM_UID)) { |
| 308 | throw new ZygoteSecurityException( |
| 309 | "System UID may not launch process with UID < " |
| 310 | + Process.SYSTEM_UID); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // If not otherwise specified, uid and gid are inherited from peer |
| 315 | if (!args.mUidSpecified) { |
| 316 | args.mUid = peer.getUid(); |
| 317 | args.mUidSpecified = true; |
| 318 | } |
| 319 | if (!args.mGidSpecified) { |
| 320 | args.mGid = peer.getGid(); |
| 321 | args.mGidSpecified = true; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Applies debugger system properties to the zygote arguments. |
| 327 | * |
| 328 | * If "ro.debuggable" is "1", all apps are debuggable. Otherwise, |
| 329 | * the debugger state is specified via the "--enable-jdwp" flag |
| 330 | * in the spawn request. |
| 331 | * |
| 332 | * @param args non-null; zygote spawner args |
| 333 | */ |
| 334 | protected static void applyDebuggerSystemProperty(ZygoteArguments args) { |
| 335 | if (RoSystemProperties.DEBUGGABLE) { |
| 336 | args.mRuntimeFlags |= Zygote.DEBUG_ENABLE_JDWP; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Applies zygote security policy. |
| 342 | * Based on the credentials of the process issuing a zygote command: |
| 343 | * <ol> |
| 344 | * <li> uid 0 (root) may specify --invoke-with to launch Zygote with a |
| 345 | * wrapper command. |
| 346 | * <li> Any other uid may not specify any invoke-with argument. |
| 347 | * </ul> |
| 348 | * |
| 349 | * @param args non-null; zygote spawner arguments |
| 350 | * @param peer non-null; peer credentials |
| 351 | * @throws ZygoteSecurityException |
| 352 | */ |
| 353 | protected static void applyInvokeWithSecurityPolicy(ZygoteArguments args, Credentials peer) |
| 354 | throws ZygoteSecurityException { |
| 355 | int peerUid = peer.getUid(); |
| 356 | |
| 357 | if (args.mInvokeWith != null && peerUid != 0 |
| 358 | && (args.mRuntimeFlags & Zygote.DEBUG_ENABLE_JDWP) == 0) { |
| 359 | throw new ZygoteSecurityException("Peer is permitted to specify an" |
| 360 | + "explicit invoke-with wrapper command only for debuggable" |
| 361 | + "applications."); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Applies invoke-with system properties to the zygote arguments. |
| 367 | * |
| 368 | * @param args non-null; zygote args |
| 369 | */ |
| 370 | protected static void applyInvokeWithSystemProperty(ZygoteArguments args) { |
| 371 | if (args.mInvokeWith == null && args.mNiceName != null) { |
| 372 | String property = "wrap." + args.mNiceName; |
| 373 | args.mInvokeWith = SystemProperties.get(property); |
| 374 | if (args.mInvokeWith != null && args.mInvokeWith.length() == 0) { |
| 375 | args.mInvokeWith = null; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Reads an argument list from the provided socket |
| 382 | * @return Argument list or null if EOF is reached |
| 383 | * @throws IOException passed straight through |
| 384 | */ |
| 385 | static String[] readArgumentList(BufferedReader socketReader) throws IOException { |
| 386 | |
| 387 | /** |
| 388 | * See android.os.Process.zygoteSendArgsAndGetPid() |
| 389 | * Presently the wire format to the zygote process is: |
| 390 | * a) a count of arguments (argc, in essence) |
| 391 | * b) a number of newline-separated argument strings equal to count |
| 392 | * |
| 393 | * After the zygote process reads these it will write the pid of |
| 394 | * the child or -1 on failure. |
| 395 | */ |
| 396 | |
| 397 | int argc; |
| 398 | |
| 399 | try { |
| 400 | String argc_string = socketReader.readLine(); |
| 401 | |
| 402 | if (argc_string == null) { |
| 403 | // EOF reached. |
| 404 | return null; |
| 405 | } |
| 406 | argc = Integer.parseInt(argc_string); |
| 407 | |
| 408 | } catch (NumberFormatException ex) { |
| 409 | Log.e("Zygote", "Invalid Zygote wire format: non-int at argc"); |
| 410 | throw new IOException("Invalid wire format"); |
| 411 | } |
| 412 | |
| 413 | // See bug 1092107: large argc can be used for a DOS attack |
| 414 | if (argc > MAX_ZYGOTE_ARGC) { |
| 415 | throw new IOException("Max arg count exceeded"); |
| 416 | } |
| 417 | |
| 418 | String[] args = new String[argc]; |
| 419 | for (int arg_index = 0; arg_index < argc; arg_index++) { |
| 420 | args[arg_index] = socketReader.readLine(); |
| 421 | if (args[arg_index] == null) { |
| 422 | // We got an unexpected EOF. |
| 423 | throw new IOException("Truncated request"); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return args; |
| 428 | } |
| 429 | |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 430 | |
Orion Hodson | 46724e7 | 2018-10-19 13:05:33 +0100 | [diff] [blame] | 431 | private static void callPostForkSystemServerHooks() { |
| 432 | // SystemServer specific post fork hooks run before child post fork hooks. |
| 433 | VM_HOOKS.postForkSystemServer(); |
| 434 | } |
| 435 | |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 436 | private static void callPostForkChildHooks(int runtimeFlags, boolean isSystemServer, |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 437 | boolean isZygote, String instructionSet) { |
| 438 | VM_HOOKS.postForkChild(runtimeFlags, isSystemServer, isZygote, instructionSet); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 439 | } |
| 440 | |
Narayan Kamath | b49996d | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 441 | /** |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 442 | * Resets the calling thread priority to the default value (Thread.NORM_PRIORITY |
| 443 | * or nice value 0). This updates both the priority value in java.lang.Thread and |
| 444 | * the nice value (setpriority). |
Narayan Kamath | b49996d | 2017-02-06 20:24:08 +0000 | [diff] [blame] | 445 | */ |
Hiroshi Yamauchi | 1e3db87 | 2017-03-02 13:39:07 -0800 | [diff] [blame] | 446 | static void resetNicePriority() { |
| 447 | Thread.currentThread().setPriority(Thread.NORM_PRIORITY); |
| 448 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * Executes "/system/bin/sh -c <command>" using the exec() system call. |
| 452 | * This method throws a runtime exception if exec() failed, otherwise, this |
| 453 | * method never returns. |
| 454 | * |
| 455 | * @param command The shell command to execute. |
| 456 | */ |
| 457 | public static void execShell(String command) { |
| 458 | String[] args = { "/system/bin/sh", "-c", command }; |
| 459 | try { |
Elliott Hughes | 860c591 | 2014-04-28 19:19:13 -0700 | [diff] [blame] | 460 | Os.execv(args[0], args); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 461 | } catch (ErrnoException e) { |
| 462 | throw new RuntimeException(e); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Appends quotes shell arguments to the specified string builder. |
| 468 | * The arguments are quoted using single-quotes, escaped if necessary, |
| 469 | * prefixed with a space, and appended to the command. |
| 470 | * |
| 471 | * @param command A string builder for the shell command being constructed. |
| 472 | * @param args An array of argument strings to be quoted and appended to the command. |
| 473 | * @see #execShell(String) |
| 474 | */ |
| 475 | public static void appendQuotedShellArgs(StringBuilder command, String[] args) { |
| 476 | for (String arg : args) { |
| 477 | command.append(" '").append(arg.replace("'", "'\\''")).append("'"); |
| 478 | } |
| 479 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 480 | } |