Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Chris Wailes | eac7f4e | 2019-01-17 14:57:10 -0800 | [diff] [blame] | 17 | /* |
| 18 | * Disable optimization of this file if we are compiling with the address |
| 19 | * sanitizer. This is a mitigation for b/122921367 and can be removed once the |
| 20 | * bug is fixed. |
| 21 | */ |
| 22 | #if __has_feature(address_sanitizer) |
| 23 | #pragma clang optimize off |
| 24 | #endif |
| 25 | |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 26 | #define LOG_TAG "Zygote" |
Jeff Sharkey | 853e53e | 2019-03-18 14:35:08 -0600 | [diff] [blame] | 27 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 28 | |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 29 | #include <async_safe/log.h> |
| 30 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 31 | // sys/mount.h has to come before linux/fs.h due to redefinition of MS_RDONLY, MS_BIND, etc |
| 32 | #include <sys/mount.h> |
| 33 | #include <linux/fs.h> |
| 34 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 35 | #include <array> |
| 36 | #include <atomic> |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 37 | #include <functional> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 38 | #include <list> |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 39 | #include <optional> |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 40 | #include <sstream> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 41 | #include <string> |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 42 | #include <string_view> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 43 | |
Josh Gao | d795110 | 2018-06-26 16:05:12 -0700 | [diff] [blame] | 44 | #include <android/fdsan.h> |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 45 | #include <arpa/inet.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 46 | #include <fcntl.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 47 | #include <grp.h> |
| 48 | #include <inttypes.h> |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 49 | #include <link.h> |
Christopher Ferris | ab16dd1 | 2017-05-15 16:50:29 -0700 | [diff] [blame] | 50 | #include <malloc.h> |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 51 | #include <mntent.h> |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 52 | #include <paths.h> |
| 53 | #include <signal.h> |
| 54 | #include <stdlib.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 55 | #include <sys/capability.h> |
Robert Sesek | e4f8d69 | 2016-09-13 19:13:01 -0400 | [diff] [blame] | 56 | #include <sys/cdefs.h> |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 57 | #include <sys/eventfd.h> |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 58 | #include <sys/mman.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 59 | #include <sys/personality.h> |
| 60 | #include <sys/prctl.h> |
| 61 | #include <sys/resource.h> |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 62 | #include <sys/socket.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 63 | #include <sys/stat.h> |
Vitalii Tomkiv | 5cbce85 | 2016-05-18 17:43:02 -0700 | [diff] [blame] | 64 | #include <sys/time.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 65 | #include <sys/types.h> |
| 66 | #include <sys/utsname.h> |
| 67 | #include <sys/wait.h> |
Dan Albert | 46d8444 | 2014-11-18 16:07:51 -0800 | [diff] [blame] | 68 | #include <unistd.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 69 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 70 | #include <android-base/logging.h> |
Minchan Kim | 5fa8af2 | 2018-06-27 11:32:40 +0900 | [diff] [blame] | 71 | #include <android-base/properties.h> |
Carmen Jackson | dd40125 | 2017-02-23 15:21:10 -0800 | [diff] [blame] | 72 | #include <android-base/file.h> |
| 73 | #include <android-base/stringprintf.h> |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 74 | #include <android-base/strings.h> |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 75 | #include <android-base/unique_fd.h> |
Christopher Ferris | 15c0233 | 2019-09-11 19:08:52 -0700 | [diff] [blame] | 76 | #include <bionic/malloc.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 77 | #include <cutils/fs.h> |
| 78 | #include <cutils/multiuser.h> |
Sharvil Nanavati | 4990e4f | 2014-06-29 17:06:52 -0700 | [diff] [blame] | 79 | #include <private/android_filesystem_config.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 80 | #include <utils/String8.h> |
Jeff Sharkey | 853e53e | 2019-03-18 14:35:08 -0600 | [diff] [blame] | 81 | #include <utils/Trace.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 82 | #include <selinux/android.h> |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 83 | #include <seccomp_policy.h> |
Howard Ro | 2733041 | 2018-10-02 12:08:28 -0700 | [diff] [blame] | 84 | #include <stats_event_list.h> |
Colin Cross | 0161bbc | 2014-06-03 13:26:58 -0700 | [diff] [blame] | 85 | #include <processgroup/processgroup.h> |
Suren Baghdasaryan | e443326 | 2019-01-04 12:16:57 -0800 | [diff] [blame] | 86 | #include <processgroup/sched_policy.h> |
Colin Cross | 18cd9f5 | 2014-06-13 12:58:55 -0700 | [diff] [blame] | 87 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 88 | #include "core_jni_helpers.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 89 | #include <nativehelper/JNIHelp.h> |
| 90 | #include <nativehelper/ScopedLocalRef.h> |
| 91 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 92 | #include <nativehelper/ScopedUtfChars.h> |
Robert Sesek | 8225b7c | 2016-12-16 14:02:31 -0500 | [diff] [blame] | 93 | #include "fd_utils.h" |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 94 | |
jgu21 | 2eacd06 | 2014-09-10 06:55:07 -0400 | [diff] [blame] | 95 | #include "nativebridge/native_bridge.h" |
| 96 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 97 | namespace { |
| 98 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 99 | // TODO (chriswailes): Add a function to initialize native Zygote data. |
| 100 | // TODO (chriswailes): Fix mixed indentation style (2 and 4 spaces). |
| 101 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 102 | using namespace std::placeholders; |
| 103 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 104 | using android::String8; |
Sudheer Shanka | 663b104 | 2018-07-30 17:34:21 -0700 | [diff] [blame] | 105 | using android::base::StringAppendF; |
Carmen Jackson | dd40125 | 2017-02-23 15:21:10 -0800 | [diff] [blame] | 106 | using android::base::StringPrintf; |
| 107 | using android::base::WriteStringToFile; |
Minchan Kim | 5fa8af2 | 2018-06-27 11:32:40 +0900 | [diff] [blame] | 108 | using android::base::GetBoolProperty; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 109 | |
Andreas Gampe | d5758f6 | 2018-03-12 12:08:55 -0700 | [diff] [blame] | 110 | #define CREATE_ERROR(...) StringPrintf("%s:%d: ", __FILE__, __LINE__). \ |
| 111 | append(StringPrintf(__VA_ARGS__)) |
| 112 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 113 | // This type is duplicated in fd_utils.h |
| 114 | typedef const std::function<void(std::string)>& fail_fn_t; |
| 115 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 116 | static pid_t gSystemServerPid = 0; |
| 117 | |
Andreas Gampe | 76b4b2c | 2019-03-15 11:56:48 -0700 | [diff] [blame] | 118 | static constexpr const char* kZygoteClassName = "com/android/internal/os/Zygote"; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 119 | static jclass gZygoteClass; |
Orion Hodson | 46724e7 | 2018-10-19 13:05:33 +0100 | [diff] [blame] | 120 | static jmethodID gCallPostForkSystemServerHooks; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 121 | static jmethodID gCallPostForkChildHooks; |
| 122 | |
Andreas Gampe | 76b4b2c | 2019-03-15 11:56:48 -0700 | [diff] [blame] | 123 | static constexpr const char* kZygoteInitClassName = "com/android/internal/os/ZygoteInit"; |
| 124 | static jclass gZygoteInitClass; |
| 125 | static jmethodID gCreateSystemServerClassLoader; |
| 126 | |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 127 | static bool gIsSecurityEnforced = true; |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 128 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 129 | /** |
| 130 | * The maximum number of characters (not including a null terminator) that a |
| 131 | * process name may contain. |
| 132 | */ |
| 133 | static constexpr size_t MAX_NAME_LENGTH = 15; |
| 134 | |
| 135 | /** |
| 136 | * The prefix string for environmental variables storing socket FDs created by |
| 137 | * init. |
| 138 | */ |
| 139 | |
| 140 | static constexpr std::string_view ANDROID_SOCKET_PREFIX("ANDROID_SOCKET_"); |
| 141 | |
| 142 | /** |
| 143 | * The file descriptor for the Zygote socket opened by init. |
| 144 | */ |
| 145 | |
| 146 | static int gZygoteSocketFD = -1; |
| 147 | |
| 148 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 149 | * The file descriptor for the unspecialized app process (USAP) pool socket opened by init. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 150 | */ |
| 151 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 152 | static int gUsapPoolSocketFD = -1; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 155 | * The number of USAPs currently in this Zygote's pool. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 156 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 157 | static std::atomic_uint32_t gUsapPoolCount = 0; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 158 | |
| 159 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 160 | * Event file descriptor used to communicate reaped USAPs to the |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 161 | * ZygoteServer. |
| 162 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 163 | static int gUsapPoolEventFD = -1; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 166 | * The maximum value that the gUSAPPoolSizeMax variable may take. This value |
| 167 | * is a mirror of ZygoteServer.USAP_POOL_SIZE_MAX_LIMIT |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 168 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 169 | static constexpr int USAP_POOL_SIZE_MAX_LIMIT = 100; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 170 | |
| 171 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 172 | * A helper class containing accounting information for USAPs. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 173 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 174 | class UsapTableEntry { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 175 | public: |
| 176 | struct EntryStorage { |
| 177 | int32_t pid; |
| 178 | int32_t read_pipe_fd; |
| 179 | |
| 180 | bool operator!=(const EntryStorage& other) { |
| 181 | return pid != other.pid || read_pipe_fd != other.read_pipe_fd; |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | private: |
| 186 | static constexpr EntryStorage INVALID_ENTRY_VALUE = {-1, -1}; |
| 187 | |
| 188 | std::atomic<EntryStorage> mStorage; |
| 189 | static_assert(decltype(mStorage)::is_always_lock_free); |
| 190 | |
| 191 | public: |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 192 | constexpr UsapTableEntry() : mStorage(INVALID_ENTRY_VALUE) {} |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * If the provided PID matches the one stored in this entry, the entry will |
| 196 | * be invalidated and the associated file descriptor will be closed. If the |
| 197 | * PIDs don't match nothing will happen. |
| 198 | * |
| 199 | * @param pid The ID of the process who's entry we want to clear. |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 200 | * @return True if the entry was cleared by this call; false otherwise |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 201 | */ |
| 202 | bool ClearForPID(int32_t pid) { |
| 203 | EntryStorage storage = mStorage.load(); |
| 204 | |
| 205 | if (storage.pid == pid) { |
| 206 | /* |
| 207 | * There are three possible outcomes from this compare-and-exchange: |
| 208 | * 1) It succeeds, in which case we close the FD |
| 209 | * 2) It fails and the new value is INVALID_ENTRY_VALUE, in which case |
| 210 | * the entry has already been cleared. |
| 211 | * 3) It fails and the new value isn't INVALID_ENTRY_VALUE, in which |
| 212 | * case the entry has already been cleared and re-used. |
| 213 | * |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 214 | * In all three cases the goal of the caller has been met, but only in |
| 215 | * the first case do we need to decrement the pool count. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 216 | */ |
| 217 | if (mStorage.compare_exchange_strong(storage, INVALID_ENTRY_VALUE)) { |
| 218 | close(storage.read_pipe_fd); |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 219 | return true; |
| 220 | } else { |
| 221 | return false; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 224 | } else { |
| 225 | return false; |
| 226 | } |
| 227 | } |
| 228 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 229 | void Clear() { |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 230 | EntryStorage storage = mStorage.load(); |
| 231 | |
| 232 | if (storage != INVALID_ENTRY_VALUE) { |
| 233 | close(storage.read_pipe_fd); |
| 234 | mStorage.store(INVALID_ENTRY_VALUE); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | void Invalidate() { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 239 | mStorage.store(INVALID_ENTRY_VALUE); |
| 240 | } |
| 241 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 242 | /** |
| 243 | * @return A copy of the data stored in this entry. |
| 244 | */ |
| 245 | std::optional<EntryStorage> GetValues() { |
| 246 | EntryStorage storage = mStorage.load(); |
| 247 | |
| 248 | if (storage != INVALID_ENTRY_VALUE) { |
| 249 | return storage; |
| 250 | } else { |
| 251 | return std::nullopt; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Sets the entry to the given values if it is currently invalid. |
| 257 | * |
| 258 | * @param pid The process ID for the new entry. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 259 | * @param read_pipe_fd The read end of the USAP control pipe for this |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 260 | * process. |
| 261 | * @return True if the entry was set; false otherwise. |
| 262 | */ |
| 263 | bool SetIfInvalid(int32_t pid, int32_t read_pipe_fd) { |
| 264 | EntryStorage new_value_storage; |
| 265 | |
| 266 | new_value_storage.pid = pid; |
| 267 | new_value_storage.read_pipe_fd = read_pipe_fd; |
| 268 | |
| 269 | EntryStorage expected = INVALID_ENTRY_VALUE; |
| 270 | |
| 271 | return mStorage.compare_exchange_strong(expected, new_value_storage); |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 276 | * A table containing information about the USAPs currently in the pool. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 277 | * |
| 278 | * Multiple threads may be attempting to modify the table, either from the |
| 279 | * signal handler or from the ZygoteServer poll loop. Atomic loads/stores in |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 280 | * the USAPTableEntry class prevent data races during these concurrent |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 281 | * operations. |
| 282 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 283 | static std::array<UsapTableEntry, USAP_POOL_SIZE_MAX_LIMIT> gUsapTable; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 284 | |
| 285 | /** |
| 286 | * The list of open zygote file descriptors. |
| 287 | */ |
| 288 | static FileDescriptorTable* gOpenFdTable = nullptr; |
| 289 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 290 | // Must match values in com.android.internal.os.Zygote. |
| 291 | enum MountExternalKind { |
| 292 | MOUNT_EXTERNAL_NONE = 0, |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 293 | MOUNT_EXTERNAL_DEFAULT = 1, |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 294 | MOUNT_EXTERNAL_READ = 2, |
| 295 | MOUNT_EXTERNAL_WRITE = 3, |
Sudheer Shanka | 0b6da53 | 2019-01-09 12:06:51 -0800 | [diff] [blame] | 296 | MOUNT_EXTERNAL_LEGACY = 4, |
| 297 | MOUNT_EXTERNAL_INSTALLER = 5, |
| 298 | MOUNT_EXTERNAL_FULL = 6, |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 299 | }; |
| 300 | |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 301 | // Must match values in com.android.internal.os.Zygote. |
| 302 | enum RuntimeFlags : uint32_t { |
| 303 | DEBUG_ENABLE_JDWP = 1, |
Yabin Cui | 4d8546d | 2019-01-29 16:29:20 -0800 | [diff] [blame] | 304 | PROFILE_FROM_SHELL = 1 << 15, |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 305 | }; |
| 306 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 307 | // Forward declaration so we don't have to move the signal handler. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 308 | static bool RemoveUsapTableEntry(pid_t usap_pid); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 309 | |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 310 | static void RuntimeAbort(JNIEnv* env, int line, const char* msg) { |
| 311 | std::ostringstream oss; |
| 312 | oss << __FILE__ << ":" << line << ": " << msg; |
| 313 | env->FatalError(oss.str().c_str()); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // This signal handler is for zygote mode, since the zygote must reap its children |
| 317 | static void SigChldHandler(int /*signal_number*/) { |
| 318 | pid_t pid; |
| 319 | int status; |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 320 | int64_t usaps_removed = 0; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 321 | |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame] | 322 | // It's necessary to save and restore the errno during this function. |
| 323 | // Since errno is stored per thread, changing it here modifies the errno |
| 324 | // on the thread on which this signal handler executes. If a signal occurs |
| 325 | // between a call and an errno check, it's possible to get the errno set |
| 326 | // here. |
| 327 | // See b/23572286 for extra information. |
| 328 | int saved_errno = errno; |
| 329 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 330 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 331 | // Log process-death status that we care about. |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 332 | if (WIFEXITED(status)) { |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 333 | async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG, |
| 334 | "Process %d exited cleanly (%d)", pid, WEXITSTATUS(status)); |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 335 | |
| 336 | // Check to see if the PID is in the USAP pool and remove it if it is. |
| 337 | if (RemoveUsapTableEntry(pid)) { |
| 338 | ++usaps_removed; |
| 339 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 340 | } else if (WIFSIGNALED(status)) { |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 341 | async_safe_format_log(ANDROID_LOG_INFO, LOG_TAG, |
| 342 | "Process %d exited due to signal %d (%s)%s", pid, |
| 343 | WTERMSIG(status), strsignal(WTERMSIG(status)), |
| 344 | WCOREDUMP(status) ? "; core dumped" : ""); |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 345 | |
| 346 | // If the process exited due to a signal other than SIGTERM, check to see |
| 347 | // if the PID is in the USAP pool and remove it if it is. If the process |
| 348 | // was closed by the Zygote using SIGTERM then the USAP pool entry will |
| 349 | // have already been removed (see nativeEmptyUsapPool()). |
| 350 | if (WTERMSIG(status) != SIGTERM && RemoveUsapTableEntry(pid)) { |
| 351 | ++usaps_removed; |
| 352 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // If the just-crashed process is the system_server, bring down zygote |
| 356 | // so that it is restarted by init and system server will be restarted |
| 357 | // from there. |
| 358 | if (pid == gSystemServerPid) { |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 359 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
| 360 | "Exit zygote because system server (pid %d) has terminated", pid); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 361 | kill(getpid(), SIGKILL); |
| 362 | } |
| 363 | } |
| 364 | |
Narayan Kamath | 160992d | 2014-04-14 14:46:07 +0100 | [diff] [blame] | 365 | // Note that we shouldn't consider ECHILD an error because |
| 366 | // the secondary zygote might have no children left to wait for. |
| 367 | if (pid < 0 && errno != ECHILD) { |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 368 | async_safe_format_log(ANDROID_LOG_WARN, LOG_TAG, |
| 369 | "Zygote SIGCHLD error in waitpid: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 370 | } |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame] | 371 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 372 | if (usaps_removed > 0) { |
Chris Wailes | fe3a348 | 2019-03-13 14:38:04 -0700 | [diff] [blame] | 373 | if (TEMP_FAILURE_RETRY(write(gUsapPoolEventFD, &usaps_removed, sizeof(usaps_removed))) == -1) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 374 | // If this write fails something went terribly wrong. We will now kill |
| 375 | // the zygote and let the system bring it back up. |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 376 | async_safe_format_log(ANDROID_LOG_ERROR, LOG_TAG, |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 377 | "Zygote failed to write to USAP pool event FD: %s", |
wangmingming1 | 6d0dd1a | 2018-11-14 10:43:36 +0800 | [diff] [blame] | 378 | strerror(errno)); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 379 | kill(getpid(), SIGKILL); |
| 380 | } |
| 381 | } |
| 382 | |
Christopher Ferris | a8a7954 | 2015-08-31 15:40:01 -0700 | [diff] [blame] | 383 | errno = saved_errno; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 384 | } |
| 385 | |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 386 | // Configures the SIGCHLD/SIGHUP handlers for the zygote process. This is |
| 387 | // configured very late, because earlier in the runtime we may fork() and |
| 388 | // exec() other processes, and we want to waitpid() for those rather than |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 389 | // have them be harvested immediately. |
| 390 | // |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 391 | // Ignore SIGHUP because all processes forked by the zygote are in the same |
| 392 | // process group as the zygote and we don't want to be notified if we become |
| 393 | // an orphaned group and have one or more stopped processes. This is not a |
| 394 | // theoretical concern : |
| 395 | // - we can become an orphaned group if one of our direct descendants forks |
| 396 | // and is subsequently killed before its children. |
| 397 | // - crash_dump routinely STOPs the process it's tracing. |
| 398 | // |
| 399 | // See issues b/71965619 and b/25567761 for further details. |
| 400 | // |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 401 | // This ends up being called repeatedly before each fork(), but there's |
| 402 | // no real harm in that. |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 403 | static void SetSignalHandlers() { |
| 404 | struct sigaction sig_chld = {}; |
| 405 | sig_chld.sa_handler = SigChldHandler; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 406 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 407 | if (sigaction(SIGCHLD, &sig_chld, nullptr) < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 408 | ALOGW("Error setting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 409 | } |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 410 | |
| 411 | struct sigaction sig_hup = {}; |
| 412 | sig_hup.sa_handler = SIG_IGN; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 413 | if (sigaction(SIGHUP, &sig_hup, nullptr) < 0) { |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 414 | ALOGW("Error setting SIGHUP handler: %s", strerror(errno)); |
| 415 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Sets the SIGCHLD handler back to default behavior in zygote children. |
yuanhao | 435e84b | 2018-01-15 15:37:02 +0800 | [diff] [blame] | 419 | static void UnsetChldSignalHandler() { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 420 | struct sigaction sa; |
| 421 | memset(&sa, 0, sizeof(sa)); |
| 422 | sa.sa_handler = SIG_DFL; |
| 423 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 424 | if (sigaction(SIGCHLD, &sa, nullptr) < 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 425 | ALOGW("Error unsetting SIGCHLD handler: %s", strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
| 429 | // Calls POSIX setgroups() using the int[] object as an argument. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 430 | // A nullptr argument is tolerated. |
| 431 | static void SetGids(JNIEnv* env, jintArray managed_gids, fail_fn_t fail_fn) { |
| 432 | if (managed_gids == nullptr) { |
| 433 | return; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 436 | ScopedIntArrayRO gids(env, managed_gids); |
| 437 | if (gids.get() == nullptr) { |
| 438 | fail_fn(CREATE_ERROR("Getting gids int array failed")); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 439 | } |
Andreas Gampe | d5758f6 | 2018-03-12 12:08:55 -0700 | [diff] [blame] | 440 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 441 | if (setgroups(gids.size(), reinterpret_cast<const gid_t*>(&gids[0])) == -1) { |
| 442 | fail_fn(CREATE_ERROR("setgroups failed: %s, gids.size=%zu", strerror(errno), gids.size())); |
| 443 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | // Sets the resource limits via setrlimit(2) for the values in the |
| 447 | // two-dimensional array of integers that's passed in. The second dimension |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 448 | // contains a tuple of length 3: (resource, rlim_cur, rlim_max). nullptr is |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 449 | // treated as an empty array. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 450 | static void SetRLimits(JNIEnv* env, jobjectArray managed_rlimits, fail_fn_t fail_fn) { |
| 451 | if (managed_rlimits == nullptr) { |
| 452 | return; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | rlimit rlim; |
| 456 | memset(&rlim, 0, sizeof(rlim)); |
| 457 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 458 | for (int i = 0; i < env->GetArrayLength(managed_rlimits); ++i) { |
| 459 | ScopedLocalRef<jobject> |
| 460 | managed_rlimit_object(env, env->GetObjectArrayElement(managed_rlimits, i)); |
| 461 | ScopedIntArrayRO rlimit_handle(env, reinterpret_cast<jintArray>(managed_rlimit_object.get())); |
| 462 | |
| 463 | if (rlimit_handle.size() != 3) { |
| 464 | fail_fn(CREATE_ERROR("rlimits array must have a second dimension of size 3")); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 467 | rlim.rlim_cur = rlimit_handle[1]; |
| 468 | rlim.rlim_max = rlimit_handle[2]; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 469 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 470 | if (setrlimit(rlimit_handle[0], &rlim) == -1) { |
| 471 | fail_fn(CREATE_ERROR("setrlimit(%d, {%ld, %ld}) failed", |
| 472 | rlimit_handle[0], rlim.rlim_cur, rlim.rlim_max)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 477 | static void EnableDebugger() { |
| 478 | // To let a non-privileged gdbserver attach to this |
| 479 | // process, we must set our dumpable flag. |
| 480 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 481 | ALOGE("prctl(PR_SET_DUMPABLE) failed"); |
| 482 | } |
| 483 | |
| 484 | // A non-privileged native debugger should be able to attach to the debuggable app, even if Yama |
| 485 | // is enabled (see kernel/Documentation/security/Yama.txt). |
| 486 | if (prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0) == -1) { |
| 487 | // if Yama is off prctl(PR_SET_PTRACER) returns EINVAL - don't log in this |
| 488 | // case since it's expected behaviour. |
| 489 | if (errno != EINVAL) { |
| 490 | ALOGE("prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY) failed"); |
| 491 | } |
| 492 | } |
| 493 | |
Orion Hodson | 2b71ad0 | 2018-12-07 16:44:33 +0000 | [diff] [blame] | 494 | // Set the core dump size to zero unless wanted (see also coredump_setup in build/envsetup.sh). |
| 495 | if (!GetBoolProperty("persist.zygote.core_dump", false)) { |
| 496 | // Set the soft limit on core dump size to 0 without changing the hard limit. |
| 497 | rlimit rl; |
| 498 | if (getrlimit(RLIMIT_CORE, &rl) == -1) { |
| 499 | ALOGE("getrlimit(RLIMIT_CORE) failed"); |
| 500 | } else { |
| 501 | rl.rlim_cur = 0; |
| 502 | if (setrlimit(RLIMIT_CORE, &rl) == -1) { |
| 503 | ALOGE("setrlimit(RLIMIT_CORE) failed"); |
| 504 | } |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 509 | static void PreApplicationInit() { |
| 510 | // The child process sets this to indicate it's not the zygote. |
Christopher Ferris | c00c1cf | 2019-04-09 16:42:32 -0700 | [diff] [blame] | 511 | android_mallopt(M_SET_ZYGOTE_CHILD, nullptr, 0); |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 512 | |
| 513 | // Set the jemalloc decay time to 1. |
| 514 | mallopt(M_DECAY_TIME, 1); |
| 515 | } |
| 516 | |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 517 | static void SetUpSeccompFilter(uid_t uid, bool is_child_zygote) { |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 518 | if (!gIsSecurityEnforced) { |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 519 | ALOGI("seccomp disabled by setenforce 0"); |
| 520 | return; |
| 521 | } |
| 522 | |
| 523 | // Apply system or app filter based on uid. |
Victor Hsieh | fa046a1 | 2018-03-28 16:26:28 -0700 | [diff] [blame] | 524 | if (uid >= AID_APP_START) { |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 525 | if (is_child_zygote) { |
Martijn Coenen | 6ef1680 | 2019-01-18 16:40:01 +0100 | [diff] [blame] | 526 | set_app_zygote_seccomp_filter(); |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 527 | } else { |
| 528 | set_app_seccomp_filter(); |
| 529 | } |
Victor Hsieh | c8176ef | 2018-01-08 12:43:00 -0800 | [diff] [blame] | 530 | } else { |
| 531 | set_system_seccomp_filter(); |
| 532 | } |
| 533 | } |
| 534 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 535 | static void EnableKeepCapabilities(fail_fn_t fail_fn) { |
| 536 | if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) == -1) { |
| 537 | fail_fn(CREATE_ERROR("prctl(PR_SET_KEEPCAPS) failed: %s", strerror(errno))); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 538 | } |
| 539 | } |
| 540 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 541 | static void DropCapabilitiesBoundingSet(fail_fn_t fail_fn) { |
| 542 | for (int i = 0; prctl(PR_CAPBSET_READ, i, 0, 0, 0) >= 0; i++) {; |
| 543 | if (prctl(PR_CAPBSET_DROP, i, 0, 0, 0) == -1) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 544 | if (errno == EINVAL) { |
| 545 | ALOGE("prctl(PR_CAPBSET_DROP) failed with EINVAL. Please verify " |
| 546 | "your kernel is compiled with file capabilities support"); |
| 547 | } else { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 548 | fail_fn(CREATE_ERROR("prctl(PR_CAPBSET_DROP, %d) failed: %s", i, strerror(errno))); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 554 | static void SetInheritable(uint64_t inheritable, fail_fn_t fail_fn) { |
Josh Gao | 45dab78 | 2017-02-01 14:56:09 -0800 | [diff] [blame] | 555 | __user_cap_header_struct capheader; |
| 556 | memset(&capheader, 0, sizeof(capheader)); |
| 557 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 558 | capheader.pid = 0; |
| 559 | |
| 560 | __user_cap_data_struct capdata[2]; |
| 561 | if (capget(&capheader, &capdata[0]) == -1) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 562 | fail_fn(CREATE_ERROR("capget failed: %s", strerror(errno))); |
Josh Gao | 45dab78 | 2017-02-01 14:56:09 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | capdata[0].inheritable = inheritable; |
| 566 | capdata[1].inheritable = inheritable >> 32; |
| 567 | |
| 568 | if (capset(&capheader, &capdata[0]) == -1) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 569 | fail_fn(CREATE_ERROR("capset(inh=%" PRIx64 ") failed: %s", inheritable, strerror(errno))); |
Josh Gao | 45dab78 | 2017-02-01 14:56:09 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 573 | static void SetCapabilities(uint64_t permitted, uint64_t effective, uint64_t inheritable, |
| 574 | fail_fn_t fail_fn) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 575 | __user_cap_header_struct capheader; |
| 576 | memset(&capheader, 0, sizeof(capheader)); |
| 577 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 578 | capheader.pid = 0; |
| 579 | |
| 580 | __user_cap_data_struct capdata[2]; |
| 581 | memset(&capdata, 0, sizeof(capdata)); |
| 582 | capdata[0].effective = effective; |
| 583 | capdata[1].effective = effective >> 32; |
| 584 | capdata[0].permitted = permitted; |
| 585 | capdata[1].permitted = permitted >> 32; |
Josh Gao | 45dab78 | 2017-02-01 14:56:09 -0800 | [diff] [blame] | 586 | capdata[0].inheritable = inheritable; |
| 587 | capdata[1].inheritable = inheritable >> 32; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 588 | |
| 589 | if (capset(&capheader, &capdata[0]) == -1) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 590 | fail_fn(CREATE_ERROR("capset(perm=%" PRIx64 ", eff=%" PRIx64 ", inh=%" PRIx64 ") " |
| 591 | "failed: %s", permitted, effective, inheritable, strerror(errno))); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 595 | static void SetSchedulerPolicy(fail_fn_t fail_fn) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 596 | errno = -set_sched_policy(0, SP_DEFAULT); |
| 597 | if (errno != 0) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 598 | fail_fn(CREATE_ERROR("set_sched_policy(0, SP_DEFAULT) failed: %s", strerror(errno))); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 602 | static int UnmountTree(const char* path) { |
Jeff Sharkey | 853e53e | 2019-03-18 14:35:08 -0600 | [diff] [blame] | 603 | ATRACE_CALL(); |
| 604 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 605 | size_t path_len = strlen(path); |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 606 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 607 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 608 | if (fp == nullptr) { |
| 609 | ALOGE("Error opening /proc/mounts: %s", strerror(errno)); |
| 610 | return -errno; |
| 611 | } |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 612 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 613 | // Some volumes can be stacked on each other, so force unmount in |
| 614 | // reverse order to give us the best chance of success. |
| 615 | std::list<std::string> to_unmount; |
| 616 | mntent* mentry; |
| 617 | while ((mentry = getmntent(fp)) != nullptr) { |
| 618 | if (strncmp(mentry->mnt_dir, path, path_len) == 0) { |
| 619 | to_unmount.push_front(std::string(mentry->mnt_dir)); |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 620 | } |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 621 | } |
| 622 | endmntent(fp); |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 623 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 624 | for (const auto& path : to_unmount) { |
| 625 | if (umount2(path.c_str(), MNT_DETACH)) { |
| 626 | ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno)); |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 627 | } |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 628 | } |
| 629 | return 0; |
Jeff Sharkey | faf3f69 | 2015-06-30 15:56:33 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 632 | // Create a private mount namespace and bind mount appropriate emulated |
| 633 | // storage for the given user. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 634 | static void MountEmulatedStorage(uid_t uid, jint mount_mode, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 635 | bool force_mount_namespace, |
Sudheer Shanka | 03fd40b | 2019-02-06 12:39:14 -0800 | [diff] [blame] | 636 | fail_fn_t fail_fn) { |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 637 | // See storage config details at http://source.android.com/tech/storage/ |
Jeff Sharkey | 853e53e | 2019-03-18 14:35:08 -0600 | [diff] [blame] | 638 | ATRACE_CALL(); |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 639 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 640 | String8 storage_source; |
| 641 | if (mount_mode == MOUNT_EXTERNAL_DEFAULT) { |
| 642 | storage_source = "/mnt/runtime/default"; |
| 643 | } else if (mount_mode == MOUNT_EXTERNAL_READ) { |
| 644 | storage_source = "/mnt/runtime/read"; |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 645 | } else if (mount_mode == MOUNT_EXTERNAL_WRITE |
| 646 | || mount_mode == MOUNT_EXTERNAL_LEGACY |
| 647 | || mount_mode == MOUNT_EXTERNAL_INSTALLER) { |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 648 | storage_source = "/mnt/runtime/write"; |
Sudheer Shanka | 783c90e | 2019-04-12 13:55:20 -0700 | [diff] [blame] | 649 | } else if (mount_mode == MOUNT_EXTERNAL_FULL) { |
| 650 | storage_source = "/mnt/runtime/full"; |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 651 | } else if (mount_mode == MOUNT_EXTERNAL_NONE && !force_mount_namespace) { |
| 652 | // Sane default of no storage visible |
| 653 | return; |
| 654 | } |
Robert Sesek | 8a3a6ff | 2016-10-31 11:25:10 -0400 | [diff] [blame] | 655 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 656 | // Create a second private mount namespace for our process |
| 657 | if (unshare(CLONE_NEWNS) == -1) { |
| 658 | fail_fn(CREATE_ERROR("Failed to unshare(): %s", strerror(errno))); |
| 659 | } |
Robert Sesek | 8a3a6ff | 2016-10-31 11:25:10 -0400 | [diff] [blame] | 660 | |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 661 | // Handle force_mount_namespace with MOUNT_EXTERNAL_NONE. |
| 662 | if (mount_mode == MOUNT_EXTERNAL_NONE) { |
| 663 | return; |
| 664 | } |
Robert Sesek | 06f3930 | 2017-03-20 17:30:05 -0400 | [diff] [blame] | 665 | |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 666 | if (TEMP_FAILURE_RETRY(mount(storage_source.string(), "/storage", nullptr, |
| 667 | MS_BIND | MS_REC | MS_SLAVE, nullptr)) == -1) { |
| 668 | fail_fn(CREATE_ERROR("Failed to mount %s to /storage: %s", |
| 669 | storage_source.string(), |
| 670 | strerror(errno))); |
| 671 | } |
Jeff Sharkey | 9527b22 | 2015-06-24 15:24:48 -0700 | [diff] [blame] | 672 | |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 673 | // Mount user-specific symlink helper into place |
| 674 | userid_t user_id = multiuser_get_user_id(uid); |
| 675 | const String8 user_source(String8::format("/mnt/user/%d", user_id)); |
| 676 | if (fs_prepare_dir(user_source.string(), 0751, 0, 0) == -1) { |
| 677 | fail_fn(CREATE_ERROR("fs_prepare_dir failed on %s", |
| 678 | user_source.string())); |
| 679 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 680 | |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 681 | if (TEMP_FAILURE_RETRY(mount(user_source.string(), "/storage/self", |
| 682 | nullptr, MS_BIND, nullptr)) == -1) { |
| 683 | fail_fn(CREATE_ERROR("Failed to mount %s to /storage/self: %s", |
| 684 | user_source.string(), strerror(errno))); |
Sudheer Shanka | 74584a5 | 2019-02-22 13:04:41 -0800 | [diff] [blame] | 685 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 686 | } |
| 687 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 688 | static bool NeedsNoRandomizeWorkaround() { |
| 689 | #if !defined(__arm__) |
| 690 | return false; |
| 691 | #else |
| 692 | int major; |
| 693 | int minor; |
| 694 | struct utsname uts; |
| 695 | if (uname(&uts) == -1) { |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | if (sscanf(uts.release, "%d.%d", &major, &minor) != 2) { |
| 700 | return false; |
| 701 | } |
| 702 | |
| 703 | // Kernels before 3.4.* need the workaround. |
| 704 | return (major < 3) || ((major == 3) && (minor < 4)); |
| 705 | #endif |
| 706 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 707 | |
| 708 | // Utility to close down the Zygote socket file descriptors while |
| 709 | // the child is still running as root with Zygote's privileges. Each |
Nick Kralevich | 5d5bf1f | 2019-01-25 10:24:42 -0800 | [diff] [blame] | 710 | // descriptor (if any) is closed via dup3(), replacing it with a valid |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 711 | // (open) descriptor to /dev/null. |
| 712 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 713 | static void DetachDescriptors(JNIEnv* env, |
| 714 | const std::vector<int>& fds_to_close, |
| 715 | fail_fn_t fail_fn) { |
| 716 | |
| 717 | if (fds_to_close.size() > 0) { |
Nick Kralevich | 5d5bf1f | 2019-01-25 10:24:42 -0800 | [diff] [blame] | 718 | android::base::unique_fd devnull_fd(open("/dev/null", O_RDWR | O_CLOEXEC)); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 719 | if (devnull_fd == -1) { |
| 720 | fail_fn(std::string("Failed to open /dev/null: ").append(strerror(errno))); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 721 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 722 | |
| 723 | for (int fd : fds_to_close) { |
| 724 | ALOGV("Switching descriptor %d to /dev/null", fd); |
Nick Kralevich | 5d5bf1f | 2019-01-25 10:24:42 -0800 | [diff] [blame] | 725 | if (dup3(devnull_fd, fd, O_CLOEXEC) == -1) { |
| 726 | fail_fn(StringPrintf("Failed dup3() on descriptor %d: %s", fd, strerror(errno))); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 727 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 728 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 732 | void SetThreadName(const std::string& thread_name) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 733 | bool hasAt = false; |
| 734 | bool hasDot = false; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 735 | |
| 736 | for (const char str_el : thread_name) { |
| 737 | if (str_el == '.') { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 738 | hasDot = true; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 739 | } else if (str_el == '@') { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 740 | hasAt = true; |
| 741 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 742 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 743 | |
| 744 | const char* name_start_ptr = thread_name.c_str(); |
| 745 | if (thread_name.length() >= MAX_NAME_LENGTH && !hasAt && hasDot) { |
| 746 | name_start_ptr += thread_name.length() - MAX_NAME_LENGTH; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 747 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 748 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 749 | // pthread_setname_np fails rather than truncating long strings. |
| 750 | char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded into bionic |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 751 | strlcpy(buf, name_start_ptr, sizeof(buf) - 1); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 752 | errno = pthread_setname_np(pthread_self(), buf); |
| 753 | if (errno != 0) { |
Elliott Hughes | 960e831 | 2014-09-30 08:49:01 -0700 | [diff] [blame] | 754 | ALOGW("Unable to set the name of current thread to '%s': %s", buf, strerror(errno)); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 755 | } |
Andreas Gampe | 041483a | 2018-03-05 13:00:42 -0800 | [diff] [blame] | 756 | // Update base::logging default tag. |
| 757 | android::base::SetDefaultTag(buf); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 758 | } |
| 759 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 760 | /** |
| 761 | * A failure function used to report fatal errors to the managed runtime. This |
| 762 | * function is often curried with the process name information and then passed |
| 763 | * to called functions. |
| 764 | * |
| 765 | * @param env Managed runtime environment |
| 766 | * @param process_name A native representation of the process name |
| 767 | * @param managed_process_name A managed representation of the process name |
| 768 | * @param msg The error message to be reported |
| 769 | */ |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 770 | [[noreturn]] |
| 771 | static void ZygoteFailure(JNIEnv* env, |
| 772 | const char* process_name, |
| 773 | jstring managed_process_name, |
| 774 | const std::string& msg) { |
| 775 | std::unique_ptr<ScopedUtfChars> scoped_managed_process_name_ptr = nullptr; |
| 776 | if (managed_process_name != nullptr) { |
| 777 | scoped_managed_process_name_ptr.reset(new ScopedUtfChars(env, managed_process_name)); |
| 778 | if (scoped_managed_process_name_ptr->c_str() != nullptr) { |
| 779 | process_name = scoped_managed_process_name_ptr->c_str(); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 783 | const std::string& error_msg = |
| 784 | (process_name == nullptr) ? msg : StringPrintf("(%s) %s", process_name, msg.c_str()); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 785 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 786 | env->FatalError(error_msg.c_str()); |
| 787 | __builtin_unreachable(); |
| 788 | } |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 789 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 790 | /** |
| 791 | * A helper method for converting managed strings to native strings. A fatal |
| 792 | * error is generated if a problem is encountered in extracting a non-null |
| 793 | * string. |
| 794 | * |
| 795 | * @param env Managed runtime environment |
| 796 | * @param process_name A native representation of the process name |
| 797 | * @param managed_process_name A managed representation of the process name |
| 798 | * @param managed_string The managed string to extract |
| 799 | * |
| 800 | * @return An empty option if the managed string is null. A optional-wrapped |
| 801 | * string otherwise. |
| 802 | */ |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 803 | static std::optional<std::string> ExtractJString(JNIEnv* env, |
| 804 | const char* process_name, |
| 805 | jstring managed_process_name, |
| 806 | jstring managed_string) { |
| 807 | if (managed_string == nullptr) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 808 | return std::nullopt; |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 809 | } else { |
| 810 | ScopedUtfChars scoped_string_chars(env, managed_string); |
| 811 | |
| 812 | if (scoped_string_chars.c_str() != nullptr) { |
| 813 | return std::optional<std::string>(scoped_string_chars.c_str()); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 814 | } else { |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 815 | ZygoteFailure(env, process_name, managed_process_name, "Failed to extract JString."); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 816 | } |
| 817 | } |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 820 | /** |
| 821 | * A helper method for converting managed string arrays to native vectors. A |
| 822 | * fatal error is generated if a problem is encountered in extracting a non-null array. |
| 823 | * |
| 824 | * @param env Managed runtime environment |
| 825 | * @param process_name A native representation of the process name |
| 826 | * @param managed_process_name A managed representation of the process name |
| 827 | * @param managed_array The managed integer array to extract |
| 828 | * |
| 829 | * @return An empty option if the managed array is null. A optional-wrapped |
| 830 | * vector otherwise. |
| 831 | */ |
| 832 | static std::optional<std::vector<int>> ExtractJIntArray(JNIEnv* env, |
| 833 | const char* process_name, |
| 834 | jstring managed_process_name, |
| 835 | jintArray managed_array) { |
| 836 | if (managed_array == nullptr) { |
| 837 | return std::nullopt; |
| 838 | } else { |
| 839 | ScopedIntArrayRO managed_array_handle(env, managed_array); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 840 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 841 | if (managed_array_handle.get() != nullptr) { |
| 842 | std::vector<int> native_array; |
| 843 | native_array.reserve(managed_array_handle.size()); |
| 844 | |
| 845 | for (size_t array_index = 0; array_index < managed_array_handle.size(); ++array_index) { |
| 846 | native_array.push_back(managed_array_handle[array_index]); |
| 847 | } |
| 848 | |
| 849 | return std::move(native_array); |
| 850 | |
| 851 | } else { |
| 852 | ZygoteFailure(env, process_name, managed_process_name, "Failed to extract JIntArray."); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /** |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 858 | * A utility function for blocking signals. |
| 859 | * |
| 860 | * @param signum Signal number to block |
| 861 | * @param fail_fn Fatal error reporting function |
| 862 | * |
| 863 | * @see ZygoteFailure |
| 864 | */ |
| 865 | static void BlockSignal(int signum, fail_fn_t fail_fn) { |
| 866 | sigset_t sigs; |
| 867 | sigemptyset(&sigs); |
| 868 | sigaddset(&sigs, signum); |
| 869 | |
| 870 | if (sigprocmask(SIG_BLOCK, &sigs, nullptr) == -1) { |
| 871 | fail_fn(CREATE_ERROR("Failed to block signal %s: %s", strsignal(signum), strerror(errno))); |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | |
| 876 | /** |
| 877 | * A utility function for unblocking signals. |
| 878 | * |
| 879 | * @param signum Signal number to unblock |
| 880 | * @param fail_fn Fatal error reporting function |
| 881 | * |
| 882 | * @see ZygoteFailure |
| 883 | */ |
| 884 | static void UnblockSignal(int signum, fail_fn_t fail_fn) { |
| 885 | sigset_t sigs; |
| 886 | sigemptyset(&sigs); |
| 887 | sigaddset(&sigs, signum); |
| 888 | |
| 889 | if (sigprocmask(SIG_UNBLOCK, &sigs, nullptr) == -1) { |
| 890 | fail_fn(CREATE_ERROR("Failed to un-block signal %s: %s", strsignal(signum), strerror(errno))); |
| 891 | } |
| 892 | } |
| 893 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 894 | static void ClearUsapTable() { |
| 895 | for (UsapTableEntry& entry : gUsapTable) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 896 | entry.Clear(); |
| 897 | } |
| 898 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 899 | gUsapPoolCount = 0; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 900 | } |
| 901 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 902 | // Utility routine to fork a process from the zygote. |
| 903 | static pid_t ForkCommon(JNIEnv* env, bool is_system_server, |
| 904 | const std::vector<int>& fds_to_close, |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 905 | const std::vector<int>& fds_to_ignore) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 906 | SetSignalHandlers(); |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 907 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 908 | // Curry a failure function. |
| 909 | auto fail_fn = std::bind(ZygoteFailure, env, is_system_server ? "system_server" : "zygote", |
| 910 | nullptr, _1); |
Andreas Gampe | d5758f6 | 2018-03-12 12:08:55 -0700 | [diff] [blame] | 911 | |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 912 | // Temporarily block SIGCHLD during forks. The SIGCHLD handler might |
| 913 | // log, which would result in the logging FDs we close being reopened. |
| 914 | // This would cause failures because the FDs are not whitelisted. |
| 915 | // |
| 916 | // Note that the zygote process is single threaded at this point. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 917 | BlockSignal(SIGCHLD, fail_fn); |
Narayan Kamath | dfcc79e | 2016-11-07 16:22:48 +0000 | [diff] [blame] | 918 | |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 919 | // Close any logging related FDs before we start evaluating the list of |
| 920 | // file descriptors. |
| 921 | __android_log_close(); |
Howard Ro | 2733041 | 2018-10-02 12:08:28 -0700 | [diff] [blame] | 922 | stats_log_close(); |
Narayan Kamath | 3764a26 | 2016-08-30 15:36:19 +0100 | [diff] [blame] | 923 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 924 | // If this is the first fork for this zygote, create the open FD table. If |
| 925 | // it isn't, we just need to check whether the list of open files has changed |
| 926 | // (and it shouldn't in the normal case). |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 927 | if (gOpenFdTable == nullptr) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 928 | gOpenFdTable = FileDescriptorTable::Create(fds_to_ignore, fail_fn); |
| 929 | } else { |
| 930 | gOpenFdTable->Restat(fds_to_ignore, fail_fn); |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Josh Gao | d795110 | 2018-06-26 16:05:12 -0700 | [diff] [blame] | 933 | android_fdsan_error_level fdsan_error_level = android_fdsan_get_error_level(); |
| 934 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 935 | pid_t pid = fork(); |
| 936 | |
| 937 | if (pid == 0) { |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 938 | // The child process. |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 939 | PreApplicationInit(); |
Christopher Ferris | ab16dd1 | 2017-05-15 16:50:29 -0700 | [diff] [blame] | 940 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 941 | // Clean up any descriptors which must be closed immediately |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 942 | DetachDescriptors(env, fds_to_close, fail_fn); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 943 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 944 | // Invalidate the entries in the USAP table. |
| 945 | ClearUsapTable(); |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 946 | |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 947 | // Re-open all remaining open file descriptors so that they aren't shared |
| 948 | // with the zygote across a fork. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 949 | gOpenFdTable->ReopenOrDetach(fail_fn); |
Josh Gao | d795110 | 2018-06-26 16:05:12 -0700 | [diff] [blame] | 950 | |
| 951 | // Turn fdsan back on. |
| 952 | android_fdsan_set_error_level(fdsan_error_level); |
Martin Stjernholm | a9bd8c3 | 2019-02-23 02:35:07 +0000 | [diff] [blame] | 953 | } else { |
| 954 | ALOGD("Forked child process %d", pid); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 955 | } |
Narayan Kamath | c5f27a7 | 2016-08-19 13:45:24 +0100 | [diff] [blame] | 956 | |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 957 | // We blocked SIGCHLD prior to a fork, we unblock it here. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 958 | UnblockSignal(SIGCHLD, fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 959 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 960 | return pid; |
| 961 | } |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 962 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 963 | // Utility routine to specialize a zygote child process. |
| 964 | static void SpecializeCommon(JNIEnv* env, uid_t uid, gid_t gid, jintArray gids, |
| 965 | jint runtime_flags, jobjectArray rlimits, |
| 966 | jlong permitted_capabilities, jlong effective_capabilities, |
| 967 | jint mount_external, jstring managed_se_info, |
| 968 | jstring managed_nice_name, bool is_system_server, |
| 969 | bool is_child_zygote, jstring managed_instruction_set, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 970 | jstring managed_app_data_dir) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 971 | const char* process_name = is_system_server ? "system_server" : "zygote"; |
| 972 | auto fail_fn = std::bind(ZygoteFailure, env, process_name, managed_nice_name, _1); |
| 973 | auto extract_fn = std::bind(ExtractJString, env, process_name, managed_nice_name, _1); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 974 | |
| 975 | auto se_info = extract_fn(managed_se_info); |
| 976 | auto nice_name = extract_fn(managed_nice_name); |
| 977 | auto instruction_set = extract_fn(managed_instruction_set); |
| 978 | auto app_data_dir = extract_fn(managed_app_data_dir); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 979 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 980 | // Keep capabilities across UID change, unless we're staying root. |
| 981 | if (uid != 0) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 982 | EnableKeepCapabilities(fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 985 | SetInheritable(permitted_capabilities, fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 986 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 987 | DropCapabilitiesBoundingSet(fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 988 | |
| 989 | bool use_native_bridge = !is_system_server && |
| 990 | instruction_set.has_value() && |
| 991 | android::NativeBridgeAvailable() && |
| 992 | android::NeedsNativeBridge(instruction_set.value().c_str()); |
| 993 | |
| 994 | if (use_native_bridge && !app_data_dir.has_value()) { |
| 995 | // The app_data_dir variable should never be empty if we need to use a |
| 996 | // native bridge. In general, app_data_dir will never be empty for normal |
| 997 | // applications. It can only happen in special cases (for isolated |
| 998 | // processes which are not associated with any app). These are launched by |
| 999 | // the framework and should not be emulated anyway. |
| 1000 | use_native_bridge = false; |
| 1001 | ALOGW("Native bridge will not be used because managed_app_data_dir == nullptr."); |
| 1002 | } |
| 1003 | |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1004 | MountEmulatedStorage(uid, mount_external, use_native_bridge, fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1005 | |
| 1006 | // If this zygote isn't root, it won't be able to create a process group, |
| 1007 | // since the directory is owned by root. |
| 1008 | if (!is_system_server && getuid() == 0) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1009 | const int rc = createProcessGroup(uid, getpid()); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1010 | if (rc == -EROFS) { |
| 1011 | ALOGW("createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?"); |
| 1012 | } else if (rc != 0) { |
| 1013 | ALOGE("createProcessGroup(%d, %d) failed: %s", uid, /* pid= */ 0, strerror(-rc)); |
| 1014 | } |
| 1015 | } |
| 1016 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1017 | SetGids(env, gids, fail_fn); |
| 1018 | SetRLimits(env, rlimits, fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1019 | |
| 1020 | if (use_native_bridge) { |
| 1021 | // Due to the logic behind use_native_bridge we know that both app_data_dir |
| 1022 | // and instruction_set contain values. |
| 1023 | android::PreInitializeNativeBridge(app_data_dir.value().c_str(), |
| 1024 | instruction_set.value().c_str()); |
| 1025 | } |
| 1026 | |
| 1027 | if (setresgid(gid, gid, gid) == -1) { |
| 1028 | fail_fn(CREATE_ERROR("setresgid(%d) failed: %s", gid, strerror(errno))); |
| 1029 | } |
| 1030 | |
| 1031 | // Must be called when the new process still has CAP_SYS_ADMIN, in this case, |
| 1032 | // before changing uid from 0, which clears capabilities. The other |
| 1033 | // alternative is to call prctl(PR_SET_NO_NEW_PRIVS, 1) afterward, but that |
| 1034 | // breaks SELinux domain transition (see b/71859146). As the result, |
| 1035 | // privileged syscalls used below still need to be accessible in app process. |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1036 | SetUpSeccompFilter(uid, is_child_zygote); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1037 | |
| 1038 | if (setresuid(uid, uid, uid) == -1) { |
| 1039 | fail_fn(CREATE_ERROR("setresuid(%d) failed: %s", uid, strerror(errno))); |
| 1040 | } |
| 1041 | |
| 1042 | // The "dumpable" flag of a process, which controls core dump generation, is |
| 1043 | // overwritten by the value in /proc/sys/fs/suid_dumpable when the effective |
| 1044 | // user or group ID changes. See proc(5) for possible values. In most cases, |
| 1045 | // the value is 0, so core dumps are disabled for zygote children. However, |
| 1046 | // when running in a Chrome OS container, the value is already set to 2, |
| 1047 | // which allows the external crash reporter to collect all core dumps. Since |
| 1048 | // only system crashes are interested, core dump is disabled for app |
| 1049 | // processes. This also ensures compliance with CTS. |
| 1050 | int dumpable = prctl(PR_GET_DUMPABLE); |
| 1051 | if (dumpable == -1) { |
| 1052 | ALOGE("prctl(PR_GET_DUMPABLE) failed: %s", strerror(errno)); |
| 1053 | RuntimeAbort(env, __LINE__, "prctl(PR_GET_DUMPABLE) failed"); |
| 1054 | } |
| 1055 | |
| 1056 | if (dumpable == 2 && uid >= AID_APP) { |
| 1057 | if (prctl(PR_SET_DUMPABLE, 0, 0, 0, 0) == -1) { |
| 1058 | ALOGE("prctl(PR_SET_DUMPABLE, 0) failed: %s", strerror(errno)); |
| 1059 | RuntimeAbort(env, __LINE__, "prctl(PR_SET_DUMPABLE, 0) failed"); |
| 1060 | } |
| 1061 | } |
| 1062 | |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 1063 | // Set process properties to enable debugging if required. |
| 1064 | if ((runtime_flags & RuntimeFlags::DEBUG_ENABLE_JDWP) != 0) { |
| 1065 | EnableDebugger(); |
| 1066 | } |
Yabin Cui | 4d8546d | 2019-01-29 16:29:20 -0800 | [diff] [blame] | 1067 | if ((runtime_flags & RuntimeFlags::PROFILE_FROM_SHELL) != 0) { |
| 1068 | // simpleperf needs the process to be dumpable to profile it. |
| 1069 | if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) { |
| 1070 | ALOGE("prctl(PR_SET_DUMPABLE) failed: %s", strerror(errno)); |
| 1071 | RuntimeAbort(env, __LINE__, "prctl(PR_SET_DUMPABLE, 1) failed"); |
| 1072 | } |
| 1073 | } |
Orion Hodson | 8d005a6 | 2018-12-05 12:28:53 +0000 | [diff] [blame] | 1074 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1075 | if (NeedsNoRandomizeWorkaround()) { |
| 1076 | // Work around ARM kernel ASLR lossage (http://b/5817320). |
| 1077 | int old_personality = personality(0xffffffff); |
| 1078 | int new_personality = personality(old_personality | ADDR_NO_RANDOMIZE); |
| 1079 | if (new_personality == -1) { |
| 1080 | ALOGW("personality(%d) failed: %s", new_personality, strerror(errno)); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1084 | SetCapabilities(permitted_capabilities, effective_capabilities, permitted_capabilities, fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1085 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1086 | SetSchedulerPolicy(fail_fn); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1087 | |
Mathieu Chartier | 0bccbf7 | 2019-01-30 15:56:17 -0800 | [diff] [blame] | 1088 | __android_log_close(); |
| 1089 | stats_log_close(); |
| 1090 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1091 | const char* se_info_ptr = se_info.has_value() ? se_info.value().c_str() : nullptr; |
| 1092 | const char* nice_name_ptr = nice_name.has_value() ? nice_name.value().c_str() : nullptr; |
| 1093 | |
| 1094 | if (selinux_android_setcontext(uid, is_system_server, se_info_ptr, nice_name_ptr) == -1) { |
| 1095 | fail_fn(CREATE_ERROR("selinux_android_setcontext(%d, %d, \"%s\", \"%s\") failed", |
| 1096 | uid, is_system_server, se_info_ptr, nice_name_ptr)); |
| 1097 | } |
| 1098 | |
| 1099 | // Make it easier to debug audit logs by setting the main thread's name to the |
| 1100 | // nice name rather than "app_process". |
| 1101 | if (nice_name.has_value()) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1102 | SetThreadName(nice_name.value()); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1103 | } else if (is_system_server) { |
| 1104 | SetThreadName("system_server"); |
| 1105 | } |
| 1106 | |
| 1107 | // Unset the SIGCHLD handler, but keep ignoring SIGHUP (rationale in SetSignalHandlers). |
| 1108 | UnsetChldSignalHandler(); |
| 1109 | |
| 1110 | if (is_system_server) { |
Mathieu Chartier | 742981e | 2019-11-21 14:30:03 -0800 | [diff] [blame] | 1111 | env->CallStaticVoidMethod(gZygoteClass, gCallPostForkSystemServerHooks, runtime_flags); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1112 | if (env->ExceptionCheck()) { |
| 1113 | fail_fn("Error calling post fork system server hooks."); |
| 1114 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1115 | |
Andreas Gampe | 76b4b2c | 2019-03-15 11:56:48 -0700 | [diff] [blame] | 1116 | // Prefetch the classloader for the system server. This is done early to |
| 1117 | // allow a tie-down of the proper system server selinux domain. |
| 1118 | env->CallStaticVoidMethod(gZygoteInitClass, gCreateSystemServerClassLoader); |
| 1119 | if (env->ExceptionCheck()) { |
| 1120 | // Be robust here. The Java code will attempt to create the classloader |
| 1121 | // at a later point (but may not have rights to use AoT artifacts). |
| 1122 | env->ExceptionClear(); |
| 1123 | } |
| 1124 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1125 | // TODO(oth): Remove hardcoded label here (b/117874058). |
| 1126 | static const char* kSystemServerLabel = "u:r:system_server:s0"; |
| 1127 | if (selinux_android_setcon(kSystemServerLabel) != 0) { |
| 1128 | fail_fn(CREATE_ERROR("selinux_android_setcon(%s)", kSystemServerLabel)); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, runtime_flags, |
| 1133 | is_system_server, is_child_zygote, managed_instruction_set); |
| 1134 | |
| 1135 | if (env->ExceptionCheck()) { |
| 1136 | fail_fn("Error calling post fork hooks."); |
| 1137 | } |
| 1138 | } |
| 1139 | |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 1140 | static uint64_t GetEffectiveCapabilityMask(JNIEnv* env) { |
| 1141 | __user_cap_header_struct capheader; |
| 1142 | memset(&capheader, 0, sizeof(capheader)); |
| 1143 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 1144 | capheader.pid = 0; |
| 1145 | |
| 1146 | __user_cap_data_struct capdata[2]; |
| 1147 | if (capget(&capheader, &capdata[0]) == -1) { |
| 1148 | ALOGE("capget failed: %s", strerror(errno)); |
| 1149 | RuntimeAbort(env, __LINE__, "capget failed"); |
| 1150 | } |
| 1151 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1152 | return capdata[0].effective | (static_cast<uint64_t>(capdata[1].effective) << 32); |
| 1153 | } |
| 1154 | |
| 1155 | static jlong CalculateCapabilities(JNIEnv* env, jint uid, jint gid, jintArray gids, |
| 1156 | bool is_child_zygote) { |
| 1157 | jlong capabilities = 0; |
| 1158 | |
| 1159 | /* |
| 1160 | * Grant the following capabilities to the Bluetooth user: |
| 1161 | * - CAP_WAKE_ALARM |
Nitin Shivpure | 99cec9d | 2019-05-29 14:02:49 +0530 | [diff] [blame] | 1162 | * - CAP_NET_ADMIN |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1163 | * - CAP_NET_RAW |
| 1164 | * - CAP_NET_BIND_SERVICE (for DHCP client functionality) |
| 1165 | * - CAP_SYS_NICE (for setting RT priority for audio-related threads) |
| 1166 | */ |
| 1167 | |
| 1168 | if (multiuser_get_app_id(uid) == AID_BLUETOOTH) { |
| 1169 | capabilities |= (1LL << CAP_WAKE_ALARM); |
Nitin Shivpure | 99cec9d | 2019-05-29 14:02:49 +0530 | [diff] [blame] | 1170 | capabilities |= (1LL << CAP_NET_ADMIN); |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1171 | capabilities |= (1LL << CAP_NET_RAW); |
| 1172 | capabilities |= (1LL << CAP_NET_BIND_SERVICE); |
| 1173 | capabilities |= (1LL << CAP_SYS_NICE); |
| 1174 | } |
| 1175 | |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 1176 | if (multiuser_get_app_id(uid) == AID_NETWORK_STACK) { |
| 1177 | capabilities |= (1LL << CAP_NET_ADMIN); |
| 1178 | capabilities |= (1LL << CAP_NET_BROADCAST); |
| 1179 | capabilities |= (1LL << CAP_NET_BIND_SERVICE); |
| 1180 | capabilities |= (1LL << CAP_NET_RAW); |
| 1181 | } |
| 1182 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1183 | /* |
| 1184 | * Grant CAP_BLOCK_SUSPEND to processes that belong to GID "wakelock" |
| 1185 | */ |
| 1186 | |
| 1187 | bool gid_wakelock_found = false; |
| 1188 | if (gid == AID_WAKELOCK) { |
| 1189 | gid_wakelock_found = true; |
| 1190 | } else if (gids != nullptr) { |
| 1191 | jsize gids_num = env->GetArrayLength(gids); |
| 1192 | ScopedIntArrayRO native_gid_proxy(env, gids); |
| 1193 | |
| 1194 | if (native_gid_proxy.get() == nullptr) { |
| 1195 | RuntimeAbort(env, __LINE__, "Bad gids array"); |
| 1196 | } |
| 1197 | |
Chris Wailes | 31c52c9 | 2019-02-14 11:20:02 -0800 | [diff] [blame] | 1198 | for (int gids_index = 0; gids_index < gids_num; ++gids_index) { |
| 1199 | if (native_gid_proxy[gids_index] == AID_WAKELOCK) { |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1200 | gid_wakelock_found = true; |
| 1201 | break; |
| 1202 | } |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (gid_wakelock_found) { |
| 1207 | capabilities |= (1LL << CAP_BLOCK_SUSPEND); |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * Grant child Zygote processes the following capabilities: |
| 1212 | * - CAP_SETUID (change UID of child processes) |
| 1213 | * - CAP_SETGID (change GID of child processes) |
| 1214 | * - CAP_SETPCAP (change capabilities of child processes) |
| 1215 | */ |
| 1216 | |
| 1217 | if (is_child_zygote) { |
| 1218 | capabilities |= (1LL << CAP_SETUID); |
| 1219 | capabilities |= (1LL << CAP_SETGID); |
| 1220 | capabilities |= (1LL << CAP_SETPCAP); |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | * Containers run without some capabilities, so drop any caps that are not |
| 1225 | * available. |
| 1226 | */ |
| 1227 | |
| 1228 | return capabilities & GetEffectiveCapabilityMask(env); |
Luis Hector Chavez | 72042c9 | 2017-07-12 10:03:30 -0700 | [diff] [blame] | 1229 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1230 | |
| 1231 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1232 | * Adds the given information about a newly created unspecialized app |
| 1233 | * processes to the Zygote's USAP table. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1234 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1235 | * @param usap_pid Process ID of the newly created USAP |
| 1236 | * @param read_pipe_fd File descriptor for the read end of the USAP |
| 1237 | * reporting pipe. Used in the ZygoteServer poll loop to track USAP |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1238 | * specialization. |
| 1239 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1240 | static void AddUsapTableEntry(pid_t usap_pid, int read_pipe_fd) { |
| 1241 | static int sUsapTableInsertIndex = 0; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1242 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1243 | int search_index = sUsapTableInsertIndex; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1244 | |
| 1245 | do { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1246 | if (gUsapTable[search_index].SetIfInvalid(usap_pid, read_pipe_fd)) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1247 | // Start our next search right after where we finished this one. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1248 | sUsapTableInsertIndex = (search_index + 1) % gUsapTable.size(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1249 | |
| 1250 | return; |
| 1251 | } |
| 1252 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1253 | search_index = (search_index + 1) % gUsapTable.size(); |
| 1254 | } while (search_index != sUsapTableInsertIndex); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1255 | |
| 1256 | // Much like money in the banana stand, there should always be an entry |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1257 | // in the USAP table. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1258 | __builtin_unreachable(); |
| 1259 | } |
| 1260 | |
| 1261 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1262 | * Invalidates the entry in the USAPTable corresponding to the provided |
| 1263 | * process ID if it is present. If an entry was removed the USAP pool |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1264 | * count is decremented. |
| 1265 | * |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1266 | * @param usap_pid Process ID of the USAP entry to invalidate |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1267 | * @return True if an entry was invalidated; false otherwise |
| 1268 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1269 | static bool RemoveUsapTableEntry(pid_t usap_pid) { |
| 1270 | for (UsapTableEntry& entry : gUsapTable) { |
| 1271 | if (entry.ClearForPID(usap_pid)) { |
| 1272 | --gUsapPoolCount; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1273 | return true; |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1281 | * @return A vector of the read pipe FDs for each of the active USAPs. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1282 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1283 | std::vector<int> MakeUsapPipeReadFDVector() { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1284 | std::vector<int> fd_vec; |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1285 | fd_vec.reserve(gUsapTable.size()); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1286 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1287 | for (UsapTableEntry& entry : gUsapTable) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1288 | auto entry_values = entry.GetValues(); |
| 1289 | |
| 1290 | if (entry_values.has_value()) { |
| 1291 | fd_vec.push_back(entry_values.value().read_pipe_fd); |
| 1292 | } |
| 1293 | } |
| 1294 | |
| 1295 | return fd_vec; |
| 1296 | } |
| 1297 | |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 1298 | static void UnmountStorageOnInit(JNIEnv* env) { |
| 1299 | // Zygote process unmount root storage space initially before every child processes are forked. |
| 1300 | // Every forked child processes (include SystemServer) only mount their own root storage space |
| 1301 | // and no need unmount storage operation in MountEmulatedStorage method. |
| 1302 | // Zygote process does not utilize root storage spaces and unshares its mount namespace below. |
| 1303 | |
| 1304 | // See storage config details at http://source.android.com/tech/storage/ |
| 1305 | // Create private mount namespace shared by all children |
| 1306 | if (unshare(CLONE_NEWNS) == -1) { |
| 1307 | RuntimeAbort(env, __LINE__, "Failed to unshare()"); |
| 1308 | return; |
| 1309 | } |
| 1310 | |
| 1311 | // Mark rootfs as being a slave so that changes from default |
| 1312 | // namespace only flow into our children. |
| 1313 | if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) { |
| 1314 | RuntimeAbort(env, __LINE__, "Failed to mount() rootfs as MS_SLAVE"); |
| 1315 | return; |
| 1316 | } |
| 1317 | |
| 1318 | // Create a staging tmpfs that is shared by our children; they will |
| 1319 | // bind mount storage into their respective private namespaces, which |
| 1320 | // are isolated from each other. |
| 1321 | const char* target_base = getenv("EMULATED_STORAGE_TARGET"); |
| 1322 | if (target_base != nullptr) { |
| 1323 | #define STRINGIFY_UID(x) __STRING(x) |
| 1324 | if (mount("tmpfs", target_base, "tmpfs", MS_NOSUID | MS_NODEV, |
| 1325 | "uid=0,gid=" STRINGIFY_UID(AID_SDCARD_R) ",mode=0751") == -1) { |
| 1326 | ALOGE("Failed to mount tmpfs to %s", target_base); |
| 1327 | RuntimeAbort(env, __LINE__, "Failed to mount tmpfs"); |
| 1328 | return; |
| 1329 | } |
| 1330 | #undef STRINGIFY_UID |
| 1331 | } |
| 1332 | |
| 1333 | UnmountTree("/storage"); |
| 1334 | } |
| 1335 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1336 | } // anonymous namespace |
| 1337 | |
| 1338 | namespace android { |
| 1339 | |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 1340 | static void com_android_internal_os_Zygote_nativePreApplicationInit(JNIEnv*, jclass) { |
| 1341 | PreApplicationInit(); |
| 1342 | } |
| 1343 | |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1344 | static jint com_android_internal_os_Zygote_nativeForkAndSpecialize( |
| 1345 | JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
Nicolas Geoffray | 81edac4 | 2017-09-07 14:13:29 +0100 | [diff] [blame] | 1346 | jint runtime_flags, jobjectArray rlimits, |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1347 | jint mount_external, jstring se_info, jstring nice_name, |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1348 | jintArray managed_fds_to_close, jintArray managed_fds_to_ignore, jboolean is_child_zygote, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1349 | jstring instruction_set, jstring app_data_dir) { |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1350 | jlong capabilities = CalculateCapabilities(env, uid, gid, gids, is_child_zygote); |
Pavlin Radoslavov | fbd5904 | 2015-11-23 17:13:25 -0800 | [diff] [blame] | 1351 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1352 | if (UNLIKELY(managed_fds_to_close == nullptr)) { |
| 1353 | ZygoteFailure(env, "zygote", nice_name, "Zygote received a null fds_to_close vector."); |
| 1354 | } |
| 1355 | |
| 1356 | std::vector<int> fds_to_close = |
| 1357 | ExtractJIntArray(env, "zygote", nice_name, managed_fds_to_close).value(); |
| 1358 | std::vector<int> fds_to_ignore = |
| 1359 | ExtractJIntArray(env, "zygote", nice_name, managed_fds_to_ignore) |
| 1360 | .value_or(std::vector<int>()); |
| 1361 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1362 | std::vector<int> usap_pipes = MakeUsapPipeReadFDVector(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1363 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1364 | fds_to_close.insert(fds_to_close.end(), usap_pipes.begin(), usap_pipes.end()); |
| 1365 | fds_to_ignore.insert(fds_to_ignore.end(), usap_pipes.begin(), usap_pipes.end()); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1366 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1367 | fds_to_close.push_back(gUsapPoolSocketFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1368 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1369 | if (gUsapPoolEventFD != -1) { |
| 1370 | fds_to_close.push_back(gUsapPoolEventFD); |
| 1371 | fds_to_ignore.push_back(gUsapPoolEventFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1372 | } |
| 1373 | |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 1374 | pid_t pid = ForkCommon(env, false, fds_to_close, fds_to_ignore); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1375 | |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 1376 | if (pid == 0) { |
| 1377 | SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, |
| 1378 | capabilities, capabilities, |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1379 | mount_external, se_info, nice_name, false, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1380 | is_child_zygote == JNI_TRUE, instruction_set, app_data_dir); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 1381 | } |
| 1382 | return pid; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | static jint com_android_internal_os_Zygote_nativeForkSystemServer( |
| 1386 | JNIEnv* env, jclass, uid_t uid, gid_t gid, jintArray gids, |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1387 | jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, |
| 1388 | jlong effective_capabilities) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1389 | std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()), |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1390 | fds_to_ignore(fds_to_close); |
| 1391 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1392 | fds_to_close.push_back(gUsapPoolSocketFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1393 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1394 | if (gUsapPoolEventFD != -1) { |
| 1395 | fds_to_close.push_back(gUsapPoolEventFD); |
| 1396 | fds_to_ignore.push_back(gUsapPoolEventFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1399 | pid_t pid = ForkCommon(env, true, |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1400 | fds_to_close, |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 1401 | fds_to_ignore); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 1402 | if (pid == 0) { |
| 1403 | SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1404 | permitted_capabilities, effective_capabilities, |
| 1405 | MOUNT_EXTERNAL_DEFAULT, nullptr, nullptr, true, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1406 | false, nullptr, nullptr); |
David Sehr | de8d0bd | 2018-06-22 10:45:36 -0700 | [diff] [blame] | 1407 | } else if (pid > 0) { |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1408 | // The zygote process checks whether the child process has died or not. |
| 1409 | ALOGI("System server process %d has been created", pid); |
| 1410 | gSystemServerPid = pid; |
| 1411 | // There is a slight window that the system server process has crashed |
| 1412 | // but it went unnoticed because we haven't published its pid yet. So |
| 1413 | // we recheck here just to make sure that all is well. |
| 1414 | int status; |
| 1415 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 1416 | ALOGE("System server process %d has died. Restarting Zygote!", pid); |
Andreas Gampe | b053cce | 2015-11-17 16:38:59 -0800 | [diff] [blame] | 1417 | RuntimeAbort(env, __LINE__, "System server process has died. Restarting Zygote!"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1418 | } |
Carmen Jackson | dd40125 | 2017-02-23 15:21:10 -0800 | [diff] [blame] | 1419 | |
Suren Baghdasaryan | 3fc4af6 | 2018-12-14 10:32:22 -0800 | [diff] [blame] | 1420 | if (UsePerAppMemcg()) { |
Minchan Kim | 5fa8af2 | 2018-06-27 11:32:40 +0900 | [diff] [blame] | 1421 | // Assign system_server to the correct memory cgroup. |
Suren Baghdasaryan | 3fc4af6 | 2018-12-14 10:32:22 -0800 | [diff] [blame] | 1422 | // Not all devices mount memcg so check if it is mounted first |
Minchan Kim | 5fa8af2 | 2018-06-27 11:32:40 +0900 | [diff] [blame] | 1423 | // to avoid unnecessarily printing errors and denials in the logs. |
Suren Baghdasaryan | 3fc4af6 | 2018-12-14 10:32:22 -0800 | [diff] [blame] | 1424 | if (!SetTaskProfiles(pid, std::vector<std::string>{"SystemMemoryProcess"})) { |
| 1425 | ALOGE("couldn't add process %d into system memcg group", pid); |
Minchan Kim | 5fa8af2 | 2018-06-27 11:32:40 +0900 | [diff] [blame] | 1426 | } |
Carmen Jackson | dd40125 | 2017-02-23 15:21:10 -0800 | [diff] [blame] | 1427 | } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1428 | } |
| 1429 | return pid; |
| 1430 | } |
| 1431 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1432 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1433 | * A JNI function that forks an unspecialized app process from the Zygote while |
| 1434 | * ensuring proper file descriptor hygiene. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1435 | * |
| 1436 | * @param env Managed runtime environment |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1437 | * @param read_pipe_fd The read FD for the USAP reporting pipe. Manually closed by blastlas |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1438 | * in managed code. |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1439 | * @param write_pipe_fd The write FD for the USAP reporting pipe. Manually closed by the |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1440 | * zygote in managed code. |
| 1441 | * @param managed_session_socket_fds A list of anonymous session sockets that must be ignored by |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1442 | * the FD hygiene code and automatically "closed" in the new USAP. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1443 | * @return |
| 1444 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1445 | static jint com_android_internal_os_Zygote_nativeForkUsap(JNIEnv* env, |
| 1446 | jclass, |
| 1447 | jint read_pipe_fd, |
| 1448 | jint write_pipe_fd, |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 1449 | jintArray managed_session_socket_fds) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1450 | std::vector<int> fds_to_close(MakeUsapPipeReadFDVector()), |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1451 | fds_to_ignore(fds_to_close); |
| 1452 | |
| 1453 | std::vector<int> session_socket_fds = |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1454 | ExtractJIntArray(env, "USAP", nullptr, managed_session_socket_fds) |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1455 | .value_or(std::vector<int>()); |
| 1456 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1457 | // The USAP Pool Event FD is created during the initialization of the |
| 1458 | // USAP pool and should always be valid here. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1459 | |
| 1460 | fds_to_close.push_back(gZygoteSocketFD); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1461 | fds_to_close.push_back(gUsapPoolEventFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1462 | fds_to_close.insert(fds_to_close.end(), session_socket_fds.begin(), session_socket_fds.end()); |
| 1463 | |
| 1464 | fds_to_ignore.push_back(gZygoteSocketFD); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1465 | fds_to_ignore.push_back(gUsapPoolSocketFD); |
| 1466 | fds_to_ignore.push_back(gUsapPoolEventFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1467 | fds_to_ignore.push_back(read_pipe_fd); |
| 1468 | fds_to_ignore.push_back(write_pipe_fd); |
| 1469 | fds_to_ignore.insert(fds_to_ignore.end(), session_socket_fds.begin(), session_socket_fds.end()); |
| 1470 | |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 1471 | pid_t usap_pid = ForkCommon(env, /* is_system_server= */ false, fds_to_close, fds_to_ignore); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1472 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1473 | if (usap_pid != 0) { |
| 1474 | ++gUsapPoolCount; |
| 1475 | AddUsapTableEntry(usap_pid, read_pipe_fd); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1476 | } |
| 1477 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1478 | return usap_pid; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 1481 | static void com_android_internal_os_Zygote_nativeAllowFileAcrossFork( |
| 1482 | JNIEnv* env, jclass, jstring path) { |
| 1483 | ScopedUtfChars path_native(env, path); |
| 1484 | const char* path_cstr = path_native.c_str(); |
| 1485 | if (!path_cstr) { |
Chris Wailes | af594fc | 2018-11-02 11:00:07 -0700 | [diff] [blame] | 1486 | RuntimeAbort(env, __LINE__, "path_cstr == nullptr"); |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 1487 | } |
| 1488 | FileDescriptorWhitelist::Get()->Allow(path_cstr); |
| 1489 | } |
| 1490 | |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1491 | static void com_android_internal_os_Zygote_nativeInstallSeccompUidGidFilter( |
| 1492 | JNIEnv* env, jclass, jint uidGidMin, jint uidGidMax) { |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 1493 | if (!gIsSecurityEnforced) { |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1494 | ALOGI("seccomp disabled by setenforce 0"); |
| 1495 | return; |
| 1496 | } |
| 1497 | |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1498 | bool installed = install_setuidgid_seccomp_filter(uidGidMin, uidGidMax); |
| 1499 | if (!installed) { |
| 1500 | RuntimeAbort(env, __LINE__, "Could not install setuid/setgid seccomp filter."); |
| 1501 | } |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1502 | } |
| 1503 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1504 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1505 | * Called from an unspecialized app process to specialize the process for a |
| 1506 | * given application. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1507 | * |
| 1508 | * @param env Managed runtime environment |
| 1509 | * @param uid User ID of the new application |
| 1510 | * @param gid Group ID of the new application |
| 1511 | * @param gids Extra groups that the process belongs to |
| 1512 | * @param runtime_flags Flags for changing the behavior of the managed runtime |
| 1513 | * @param rlimits Resource limits |
| 1514 | * @param mount_external The mode (read/write/normal) that external storage will be mounted with |
| 1515 | * @param se_info SELinux policy information |
| 1516 | * @param nice_name New name for this process |
| 1517 | * @param is_child_zygote If the process is to become a WebViewZygote |
| 1518 | * @param instruction_set The instruction set expected/requested by the new application |
| 1519 | * @param app_data_dir Path to the application's data directory |
| 1520 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1521 | static void com_android_internal_os_Zygote_nativeSpecializeAppProcess( |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1522 | JNIEnv* env, jclass, jint uid, jint gid, jintArray gids, |
| 1523 | jint runtime_flags, jobjectArray rlimits, |
| 1524 | jint mount_external, jstring se_info, jstring nice_name, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1525 | jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1526 | jlong capabilities = CalculateCapabilities(env, uid, gid, gids, is_child_zygote); |
| 1527 | |
| 1528 | SpecializeCommon(env, uid, gid, gids, runtime_flags, rlimits, |
| 1529 | capabilities, capabilities, |
| 1530 | mount_external, se_info, nice_name, false, |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1531 | is_child_zygote == JNI_TRUE, instruction_set, app_data_dir); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | /** |
| 1535 | * A helper method for fetching socket file descriptors that were opened by init from the |
| 1536 | * environment. |
| 1537 | * |
| 1538 | * @param env Managed runtime environment |
| 1539 | * @param is_primary If this process is the primary or secondary Zygote; used to compute the name |
| 1540 | * of the environment variable storing the file descriptors. |
| 1541 | */ |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 1542 | static void com_android_internal_os_Zygote_nativeInitNativeState(JNIEnv* env, jclass, |
| 1543 | jboolean is_primary) { |
| 1544 | /* |
| 1545 | * Obtain file descriptors created by init from the environment. |
| 1546 | */ |
| 1547 | |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1548 | std::string android_socket_prefix(ANDROID_SOCKET_PREFIX); |
| 1549 | std::string env_var_name = android_socket_prefix + (is_primary ? "zygote" : "zygote_secondary"); |
| 1550 | char* env_var_val = getenv(env_var_name.c_str()); |
| 1551 | |
| 1552 | if (env_var_val != nullptr) { |
| 1553 | gZygoteSocketFD = atoi(env_var_val); |
| 1554 | ALOGV("Zygote:zygoteSocketFD = %d", gZygoteSocketFD); |
| 1555 | } else { |
| 1556 | ALOGE("Unable to fetch Zygote socket file descriptor"); |
| 1557 | } |
| 1558 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1559 | env_var_name = android_socket_prefix + (is_primary ? "usap_pool_primary" : "usap_pool_secondary"); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1560 | env_var_val = getenv(env_var_name.c_str()); |
| 1561 | |
| 1562 | if (env_var_val != nullptr) { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1563 | gUsapPoolSocketFD = atoi(env_var_val); |
| 1564 | ALOGV("Zygote:usapPoolSocketFD = %d", gUsapPoolSocketFD); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1565 | } else { |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1566 | ALOGE("Unable to fetch USAP pool socket file descriptor"); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1567 | } |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* |
| 1570 | * Security Initialization |
| 1571 | */ |
| 1572 | |
| 1573 | // security_getenforce is not allowed on app process. Initialize and cache |
| 1574 | // the value before zygote forks. |
| 1575 | gIsSecurityEnforced = security_getenforce(); |
| 1576 | |
| 1577 | selinux_android_seapp_context_init(); |
| 1578 | |
| 1579 | /* |
| 1580 | * Storage Initialization |
| 1581 | */ |
| 1582 | |
| 1583 | UnmountStorageOnInit(env); |
| 1584 | |
| 1585 | /* |
| 1586 | * Performance Initialization |
| 1587 | */ |
| 1588 | |
| 1589 | if (!SetTaskProfiles(0, {})) { |
| 1590 | ZygoteFailure(env, "zygote", nullptr, "Zygote SetTaskProfiles failed"); |
| 1591 | } |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * @param env Managed runtime environment |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1596 | * @return A managed array of raw file descriptors for the read ends of the USAP reporting |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1597 | * pipes. |
| 1598 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1599 | static jintArray com_android_internal_os_Zygote_nativeGetUsapPipeFDs(JNIEnv* env, jclass) { |
| 1600 | std::vector<int> usap_fds = MakeUsapPipeReadFDVector(); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1601 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1602 | jintArray managed_usap_fds = env->NewIntArray(usap_fds.size()); |
| 1603 | env->SetIntArrayRegion(managed_usap_fds, 0, usap_fds.size(), usap_fds.data()); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1604 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1605 | return managed_usap_fds; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1609 | * A JNI wrapper around RemoveUsapTableEntry. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1610 | * |
| 1611 | * @param env Managed runtime environment |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1612 | * @param usap_pid Process ID of the USAP entry to invalidate |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1613 | * @return True if an entry was invalidated; false otherwise. |
| 1614 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1615 | static jboolean com_android_internal_os_Zygote_nativeRemoveUsapTableEntry(JNIEnv* env, jclass, |
| 1616 | jint usap_pid) { |
| 1617 | return RemoveUsapTableEntry(usap_pid); |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1621 | * Creates the USAP pool event FD if it doesn't exist and returns it. This is used by the |
| 1622 | * ZygoteServer poll loop to know when to re-fill the USAP pool. |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1623 | * |
| 1624 | * @param env Managed runtime environment |
| 1625 | * @return A raw event file descriptor used to communicate (from the signal handler) when the |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1626 | * Zygote receives a SIGCHLD for a USAP |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1627 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1628 | static jint com_android_internal_os_Zygote_nativeGetUsapPoolEventFD(JNIEnv* env, jclass) { |
| 1629 | if (gUsapPoolEventFD == -1) { |
| 1630 | if ((gUsapPoolEventFD = eventfd(0, 0)) == -1) { |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1631 | ZygoteFailure(env, "zygote", nullptr, StringPrintf("Unable to create eventfd: %s", strerror(errno))); |
| 1632 | } |
| 1633 | } |
| 1634 | |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1635 | return gUsapPoolEventFD; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /** |
| 1639 | * @param env Managed runtime environment |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1640 | * @return The number of USAPs currently in the USAP pool |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1641 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1642 | static jint com_android_internal_os_Zygote_nativeGetUsapPoolCount(JNIEnv* env, jclass) { |
| 1643 | return gUsapPoolCount; |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 1646 | /** |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1647 | * Kills all processes currently in the USAP pool and closes their read pipe |
| 1648 | * FDs. |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 1649 | * |
| 1650 | * @param env Managed runtime environment |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 1651 | */ |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1652 | static void com_android_internal_os_Zygote_nativeEmptyUsapPool(JNIEnv* env, jclass) { |
| 1653 | for (auto& entry : gUsapTable) { |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 1654 | auto entry_storage = entry.GetValues(); |
| 1655 | |
| 1656 | if (entry_storage.has_value()) { |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 1657 | kill(entry_storage.value().pid, SIGTERM); |
| 1658 | |
| 1659 | // Clean up the USAP table entry here. This avoids a potential race |
| 1660 | // where a newly created USAP might not be able to find a valid table |
| 1661 | // entry if signal handler (which would normally do the cleanup) doesn't |
| 1662 | // run between now and when the new process is created. |
| 1663 | |
Chris Wailes | db132a3 | 2019-02-20 10:49:27 -0800 | [diff] [blame] | 1664 | close(entry_storage.value().read_pipe_fd); |
| 1665 | |
| 1666 | // Avoid a second atomic load by invalidating instead of clearing. |
| 1667 | entry.Invalidate(); |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1668 | --gUsapPoolCount; |
Chris Wailes | ae93714 | 2019-01-24 12:57:33 -0800 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | } |
| 1672 | |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 1673 | static int disable_execute_only(struct dl_phdr_info *info, size_t size, void *data) { |
| 1674 | // Search for any execute-only segments and mark them read+execute. |
| 1675 | for (int i = 0; i < info->dlpi_phnum; i++) { |
| 1676 | if ((info->dlpi_phdr[i].p_type == PT_LOAD) && (info->dlpi_phdr[i].p_flags == PF_X)) { |
| 1677 | mprotect(reinterpret_cast<void*>(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr), |
| 1678 | info->dlpi_phdr[i].p_memsz, PROT_READ | PROT_EXEC); |
| 1679 | } |
| 1680 | } |
| 1681 | // Return non-zero to exit dl_iterate_phdr. |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * @param env Managed runtime environment |
| 1687 | * @return True if disable was successful. |
| 1688 | */ |
| 1689 | static jboolean com_android_internal_os_Zygote_nativeDisableExecuteOnly(JNIEnv* env, jclass) { |
| 1690 | return dl_iterate_phdr(disable_execute_only, nullptr) == 0; |
| 1691 | } |
| 1692 | |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 1693 | static void com_android_internal_os_Zygote_nativeBlockSigTerm(JNIEnv* env, jclass) { |
| 1694 | auto fail_fn = std::bind(ZygoteFailure, env, "usap", nullptr, _1); |
| 1695 | BlockSignal(SIGTERM, fail_fn); |
| 1696 | } |
| 1697 | |
| 1698 | static void com_android_internal_os_Zygote_nativeUnblockSigTerm(JNIEnv* env, jclass) { |
| 1699 | auto fail_fn = std::bind(ZygoteFailure, env, "usap", nullptr, _1); |
| 1700 | UnblockSignal(SIGTERM, fail_fn); |
| 1701 | } |
| 1702 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 1703 | static const JNINativeMethod gMethods[] = { |
Andreas Gampe | aec67dc | 2014-09-02 21:23:06 -0700 | [diff] [blame] | 1704 | { "nativeForkAndSpecialize", |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1705 | "(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)I", |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1706 | (void *) com_android_internal_os_Zygote_nativeForkAndSpecialize }, |
| 1707 | { "nativeForkSystemServer", "(II[II[[IJJ)I", |
doheon1.lee | 885b742 | 2016-01-20 13:07:27 +0900 | [diff] [blame] | 1708 | (void *) com_android_internal_os_Zygote_nativeForkSystemServer }, |
Robert Sesek | 54e387d | 2016-12-02 17:27:50 -0500 | [diff] [blame] | 1709 | { "nativeAllowFileAcrossFork", "(Ljava/lang/String;)V", |
| 1710 | (void *) com_android_internal_os_Zygote_nativeAllowFileAcrossFork }, |
Christopher Ferris | 76de39e | 2017-06-20 16:13:40 -0700 | [diff] [blame] | 1711 | { "nativePreApplicationInit", "()V", |
Martijn Coenen | 86f08a5 | 2019-01-03 16:23:01 +0100 | [diff] [blame] | 1712 | (void *) com_android_internal_os_Zygote_nativePreApplicationInit }, |
| 1713 | { "nativeInstallSeccompUidGidFilter", "(II)V", |
Chris Wailes | aa1c962 | 2019-01-10 16:55:32 -0800 | [diff] [blame] | 1714 | (void *) com_android_internal_os_Zygote_nativeInstallSeccompUidGidFilter }, |
Mathew Inwood | 51c53b4 | 2019-05-24 14:06:10 +0100 | [diff] [blame] | 1715 | { "nativeForkUsap", "(II[I)I", |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1716 | (void *) com_android_internal_os_Zygote_nativeForkUsap }, |
| 1717 | { "nativeSpecializeAppProcess", |
Sudheer Shanka | 64501e5 | 2019-04-29 10:46:26 -0700 | [diff] [blame] | 1718 | "(II[II[[IILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;)V", |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1719 | (void *) com_android_internal_os_Zygote_nativeSpecializeAppProcess }, |
Chris Wailes | 6d482d54 | 2019-04-03 13:00:52 -0700 | [diff] [blame] | 1720 | { "nativeInitNativeState", "(Z)V", |
| 1721 | (void *) com_android_internal_os_Zygote_nativeInitNativeState }, |
Chris Wailes | 7e797b6 | 2019-02-22 18:29:22 -0800 | [diff] [blame] | 1722 | { "nativeGetUsapPipeFDs", "()[I", |
| 1723 | (void *) com_android_internal_os_Zygote_nativeGetUsapPipeFDs }, |
| 1724 | { "nativeRemoveUsapTableEntry", "(I)Z", |
| 1725 | (void *) com_android_internal_os_Zygote_nativeRemoveUsapTableEntry }, |
| 1726 | { "nativeGetUsapPoolEventFD", "()I", |
| 1727 | (void *) com_android_internal_os_Zygote_nativeGetUsapPoolEventFD }, |
| 1728 | { "nativeGetUsapPoolCount", "()I", |
| 1729 | (void *) com_android_internal_os_Zygote_nativeGetUsapPoolCount }, |
| 1730 | { "nativeEmptyUsapPool", "()V", |
Jeff Vander Stoep | 739c0b5 | 2019-03-25 20:27:52 -0700 | [diff] [blame] | 1731 | (void *) com_android_internal_os_Zygote_nativeEmptyUsapPool }, |
| 1732 | { "nativeDisableExecuteOnly", "()Z", |
Chris Wailes | f68c4e2 | 2019-06-05 16:07:50 -0700 | [diff] [blame] | 1733 | (void *) com_android_internal_os_Zygote_nativeDisableExecuteOnly }, |
| 1734 | { "nativeBlockSigTerm", "()V", |
| 1735 | (void* ) com_android_internal_os_Zygote_nativeBlockSigTerm }, |
| 1736 | { "nativeUnblockSigTerm", "()V", |
| 1737 | (void* ) com_android_internal_os_Zygote_nativeUnblockSigTerm } |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1738 | }; |
| 1739 | |
| 1740 | int register_com_android_internal_os_Zygote(JNIEnv* env) { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1741 | gZygoteClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteClassName)); |
Orion Hodson | 46724e7 | 2018-10-19 13:05:33 +0100 | [diff] [blame] | 1742 | gCallPostForkSystemServerHooks = GetStaticMethodIDOrDie(env, gZygoteClass, |
| 1743 | "callPostForkSystemServerHooks", |
Mathieu Chartier | 742981e | 2019-11-21 14:30:03 -0800 | [diff] [blame] | 1744 | "(I)V"); |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1745 | gCallPostForkChildHooks = GetStaticMethodIDOrDie(env, gZygoteClass, "callPostForkChildHooks", |
Robert Sesek | d0a190df | 2018-02-12 18:46:01 -0500 | [diff] [blame] | 1746 | "(IZZLjava/lang/String;)V"); |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1747 | |
Andreas Gampe | 76b4b2c | 2019-03-15 11:56:48 -0700 | [diff] [blame] | 1748 | gZygoteInitClass = MakeGlobalRefOrDie(env, FindClassOrDie(env, kZygoteInitClassName)); |
| 1749 | gCreateSystemServerClassLoader = GetStaticMethodIDOrDie(env, gZygoteInitClass, |
| 1750 | "createSystemServerClassLoader", |
| 1751 | "()V"); |
| 1752 | |
| 1753 | RegisterMethodsOrDie(env, "com/android/internal/os/Zygote", gMethods, NELEM(gMethods)); |
| 1754 | |
| 1755 | return JNI_OK; |
Narayan Kamath | 973b466 | 2014-03-31 13:41:26 +0100 | [diff] [blame] | 1756 | } |
| 1757 | } // namespace android |