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