Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 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 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 17 | #include "InstalldNativeService.h" |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 20 | #include <fts.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 21 | #include <inttypes.h> |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 22 | #include <linux/fsverity.h> |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 25 | #include <string.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 26 | #include <sys/capability.h> |
| 27 | #include <sys/file.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
| 29 | #include <sys/mman.h> |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 30 | #include <sys/mount.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 31 | #include <sys/resource.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 32 | #include <sys/stat.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 33 | #include <sys/statvfs.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 34 | #include <sys/types.h> |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 35 | #include <sys/wait.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 36 | #include <sys/xattr.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 37 | #include <unistd.h> |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | #include <filesystem> |
| 40 | #include <fstream> |
| 41 | #include <functional> |
| 42 | #include <regex> |
Alex Buynytskyy | 02dca6b | 2023-11-28 19:06:34 +0000 | [diff] [blame] | 43 | #include <thread> |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 44 | #include <unordered_set> |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 45 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 46 | #include <android-base/file.h> |
Andreas Gampe | afa58d1 | 2016-06-03 16:09:32 -0700 | [diff] [blame] | 47 | #include <android-base/logging.h> |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 48 | #include <android-base/parseint.h> |
Jeff Sharkey | f29a3bc | 2018-01-08 10:40:19 -0700 | [diff] [blame] | 49 | #include <android-base/properties.h> |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 50 | #include <android-base/scopeguard.h> |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 51 | #include <android-base/stringprintf.h> |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 52 | #include <android-base/strings.h> |
Roland Levillain | 64b59cc | 2016-04-05 16:41:12 +0100 | [diff] [blame] | 53 | #include <android-base/unique_fd.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 54 | #include <cutils/ashmem.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 55 | #include <cutils/fs.h> |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 56 | #include <cutils/misc.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 57 | #include <cutils/properties.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 58 | #include <cutils/sched_policy.h> |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 59 | #include <linux/quota.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 60 | #include <log/log.h> // TODO: Move everything to base/logging. |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 61 | #include <logwrap/logwrap.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 62 | #include <private/android_filesystem_config.h> |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 63 | #include <private/android_projectid_config.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 64 | #include <selinux/android.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 65 | #include <system/thread_defs.h> |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 66 | #include <utils/Trace.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 68 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 69 | #include "globals.h" |
| 70 | #include "installd_deps.h" |
| 71 | #include "otapreopt_utils.h" |
| 72 | #include "utils.h" |
Eric Holk | 0ebbe0f | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 73 | #include "view_compiler.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 74 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 75 | #include "CacheTracker.h" |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 76 | #include "CrateManager.h" |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 77 | #include "MatchExtensionGen.h" |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 78 | #include "QuotaUtils.h" |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 79 | #include "SysTrace.h" |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 80 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 81 | #ifndef LOG_TAG |
| 82 | #define LOG_TAG "installd" |
| 83 | #endif |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 84 | |
Alex Buynytskyy | 779447d | 2021-11-22 17:06:53 -0800 | [diff] [blame] | 85 | #define GRANULAR_LOCKS |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 86 | |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 87 | using android::base::ParseUint; |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 88 | using android::base::Split; |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 89 | using android::base::StringPrintf; |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 90 | using android::base::unique_fd; |
| 91 | using android::os::ParcelFileDescriptor; |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 92 | using std::endl; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 93 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 94 | namespace android { |
| 95 | namespace installd { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 96 | |
Nikita Ioffe | 8e4d346 | 2019-01-21 17:56:03 +0000 | [diff] [blame] | 97 | // An uuid used in unit tests. |
| 98 | static constexpr const char* kTestUuid = "TEST"; |
| 99 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 100 | static constexpr const mode_t kRollbackFolderMode = 0700; |
| 101 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 102 | static constexpr const char* kCpPath = "/system/bin/cp"; |
| 103 | static constexpr const char* kXattrDefault = "user.default"; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 104 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 105 | static constexpr const char* kDataMirrorCePath = "/data_mirror/data_ce"; |
| 106 | static constexpr const char* kDataMirrorDePath = "/data_mirror/data_de"; |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 107 | static constexpr const char* kMiscMirrorCePath = "/data_mirror/misc_ce"; |
| 108 | static constexpr const char* kMiscMirrorDePath = "/data_mirror/misc_de"; |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 109 | |
Janis Danisevskis | 40bd3ef | 2016-06-07 10:31:27 -0700 | [diff] [blame] | 110 | static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M |
Janis Danisevskis | eecb2d2 | 2016-01-12 14:45:55 +0000 | [diff] [blame] | 111 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 112 | static constexpr const char* PKG_LIB_POSTFIX = "/lib"; |
| 113 | static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; |
| 114 | static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; |
| 115 | |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 116 | static constexpr const char* kFuseProp = "persist.sys.fuse"; |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 117 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Property to control if app data isolation is enabled. |
| 120 | */ |
| 121 | static constexpr const char* kAppDataIsolationEnabledProperty = "persist.zygote.app_data_isolation"; |
Zim | 1edb92a | 2020-01-09 08:48:00 +0000 | [diff] [blame] | 122 | static constexpr const char* kMntSdcardfs = "/mnt/runtime/default/"; |
| 123 | static constexpr const char* kMntFuse = "/mnt/pass_through/0/"; |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 124 | |
| 125 | static std::atomic<bool> sAppDataIsolationEnabled(false); |
| 126 | |
Shikha Malhotra | dcf469b | 2022-03-23 11:53:58 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Flag to control if project ids are supported for internal storage |
| 129 | */ |
| 130 | static std::atomic<bool> sUsingProjectIdsFlag(false); |
| 131 | static std::once_flag flag; |
| 132 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 133 | namespace { |
| 134 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 135 | static binder::Status ok() { |
| 136 | return binder::Status::ok(); |
| 137 | } |
| 138 | |
| 139 | static binder::Status exception(uint32_t code, const std::string& msg) { |
Jeff Sharkey | 8e9bf35 | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 140 | LOG(ERROR) << msg << " (" << code << ")"; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 141 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 142 | } |
| 143 | |
| 144 | static binder::Status error() { |
| 145 | return binder::Status::fromServiceSpecificError(errno); |
| 146 | } |
| 147 | |
| 148 | static binder::Status error(const std::string& msg) { |
| 149 | PLOG(ERROR) << msg; |
| 150 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 151 | } |
| 152 | |
| 153 | static binder::Status error(uint32_t code, const std::string& msg) { |
| 154 | LOG(ERROR) << msg << " (" << code << ")"; |
| 155 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); |
| 156 | } |
| 157 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 158 | binder::Status checkUid(uid_t expectedUid) { |
| 159 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 160 | if (uid == expectedUid || uid == AID_ROOT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 161 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 162 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 163 | return exception(binder::Status::EX_SECURITY, |
| 164 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
| 165 | } |
| 166 | } |
| 167 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 168 | binder::Status checkArgumentUuid(const std::optional<std::string>& uuid) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 169 | if (!uuid || is_valid_filename(*uuid)) { |
| 170 | return ok(); |
| 171 | } else { |
| 172 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 173 | StringPrintf("UUID %s is malformed", uuid->c_str())); |
| 174 | } |
| 175 | } |
| 176 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 177 | binder::Status checkArgumentUuidTestOrNull(const std::optional<std::string>& uuid) { |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 178 | if (!uuid || strcmp(uuid->c_str(), kTestUuid) == 0) { |
| 179 | return ok(); |
| 180 | } else { |
| 181 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 182 | StringPrintf("UUID must be null or \"%s\", got: %s", kTestUuid, uuid->c_str())); |
| 183 | } |
| 184 | } |
| 185 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 186 | binder::Status checkArgumentPackageName(const std::string& packageName) { |
Greg Kaiser | 6d758fc | 2019-03-26 08:55:32 -0700 | [diff] [blame] | 187 | if (is_valid_package_name(packageName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 188 | return ok(); |
| 189 | } else { |
| 190 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 191 | StringPrintf("Package name %s is malformed", packageName.c_str())); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 195 | binder::Status checkArgumentPath(const std::string& path) { |
| 196 | if (path.empty()) { |
| 197 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path"); |
| 198 | } |
| 199 | if (path[0] != '/') { |
| 200 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 201 | StringPrintf("Path %s is relative", path.c_str())); |
| 202 | } |
| 203 | if ((path + '/').find("/../") != std::string::npos) { |
| 204 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 205 | StringPrintf("Path %s is shady", path.c_str())); |
| 206 | } |
| 207 | for (const char& c : path) { |
| 208 | if (c == '\0' || c == '\n') { |
| 209 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 210 | StringPrintf("Path %s is malformed", path.c_str())); |
| 211 | } |
| 212 | } |
| 213 | return ok(); |
| 214 | } |
| 215 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 216 | binder::Status checkArgumentPath(const std::optional<std::string>& path) { |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 217 | if (path) { |
| 218 | return checkArgumentPath(*path); |
| 219 | } else { |
| 220 | return ok(); |
| 221 | } |
| 222 | } |
| 223 | |
Alex Buynytskyy | d04631f | 2022-08-12 15:14:41 -0700 | [diff] [blame] | 224 | binder::Status checkArgumentFileName(const std::string& path) { |
| 225 | if (path.empty()) { |
| 226 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing name"); |
| 227 | } |
| 228 | for (const char& c : path) { |
| 229 | if (c == '\0' || c == '\n' || c == '/') { |
| 230 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 231 | StringPrintf("Name %s is malformed", path.c_str())); |
| 232 | } |
| 233 | } |
| 234 | return ok(); |
| 235 | } |
| 236 | |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 237 | binder::Status checkArgumentAppId(int32_t appId) { |
| 238 | if (FIRST_APPLICATION_UID <= appId && appId <= LAST_APPLICATION_UID) { |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 239 | return ok(); |
| 240 | } |
| 241 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 242 | StringPrintf("appId %d is outside of the range", appId)); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 245 | #define ENFORCE_UID(uid) { \ |
| 246 | binder::Status status = checkUid((uid)); \ |
| 247 | if (!status.isOk()) { \ |
| 248 | return status; \ |
| 249 | } \ |
| 250 | } |
| 251 | |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 252 | // we could have tighter checks, but this is only to avoid hard errors. Negative values are defined |
| 253 | // in UserHandle.java and carry specific meanings that may not be handled by certain APIs here. |
Steven Moreland | 3e543b9 | 2023-09-07 20:08:15 +0000 | [diff] [blame] | 254 | #define ENFORCE_VALID_USER(userId) \ |
| 255 | { \ |
| 256 | if (static_cast<uid_t>(userId) >= std::numeric_limits<uid_t>::max() / AID_USER_OFFSET) { \ |
| 257 | return error("userId invalid: " + std::to_string(userId)); \ |
| 258 | } \ |
| 259 | } |
| 260 | |
| 261 | #define ENFORCE_VALID_USER_OR_NULL(userId) \ |
| 262 | { \ |
| 263 | if (static_cast<uid_t>(userId) != USER_NULL) { \ |
| 264 | ENFORCE_VALID_USER(userId); \ |
| 265 | } \ |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 268 | #define CHECK_ARGUMENT_UUID(uuid) { \ |
| 269 | binder::Status status = checkArgumentUuid((uuid)); \ |
| 270 | if (!status.isOk()) { \ |
| 271 | return status; \ |
| 272 | } \ |
| 273 | } |
| 274 | |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 275 | #define CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(uuid) { \ |
| 276 | auto status = checkArgumentUuidTestOrNull(uuid); \ |
| 277 | if (!status.isOk()) { \ |
| 278 | return status; \ |
| 279 | } \ |
| 280 | } \ |
| 281 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 282 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ |
| 283 | binder::Status status = \ |
| 284 | checkArgumentPackageName((packageName)); \ |
| 285 | if (!status.isOk()) { \ |
| 286 | return status; \ |
| 287 | } \ |
| 288 | } |
| 289 | |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 290 | #define CHECK_ARGUMENT_PATH(path) { \ |
| 291 | binder::Status status = checkArgumentPath((path)); \ |
| 292 | if (!status.isOk()) { \ |
| 293 | return status; \ |
| 294 | } \ |
| 295 | } |
| 296 | |
Alex Buynytskyy | d04631f | 2022-08-12 15:14:41 -0700 | [diff] [blame] | 297 | #define CHECK_ARGUMENT_FILE_NAME(path) \ |
| 298 | { \ |
| 299 | binder::Status status = checkArgumentFileName((path)); \ |
| 300 | if (!status.isOk()) { \ |
| 301 | return status; \ |
| 302 | } \ |
| 303 | } |
| 304 | |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 305 | #define CHECK_ARGUMENT_APP_ID(appId) \ |
| 306 | { \ |
| 307 | binder::Status status = checkArgumentAppId((appId)); \ |
| 308 | if (!status.isOk()) { \ |
| 309 | return status; \ |
| 310 | } \ |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 313 | #ifdef GRANULAR_LOCKS |
| 314 | |
| 315 | /** |
| 316 | * This class obtains in constructor and keeps the local strong pointer to the RefLock. |
| 317 | * On destruction, it checks if there are any other strong pointers, and remove the map entry if |
| 318 | * this was the last one. |
| 319 | */ |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 320 | template <class Key, class Mutex> |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 321 | struct LocalLockHolder { |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 322 | using WeakPointer = std::weak_ptr<Mutex>; |
| 323 | using StrongPointer = std::shared_ptr<Mutex>; |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 324 | using Map = std::unordered_map<Key, WeakPointer>; |
| 325 | using MapLock = std::recursive_mutex; |
| 326 | |
| 327 | LocalLockHolder(Key key, Map& map, MapLock& mapLock) |
| 328 | : mKey(std::move(key)), mMap(map), mMapLock(mapLock) { |
| 329 | std::lock_guard lock(mMapLock); |
| 330 | auto& weakPtr = mMap[mKey]; |
| 331 | |
| 332 | // Check if the RefLock is still alive. |
| 333 | mRefLock = weakPtr.lock(); |
| 334 | if (!mRefLock) { |
| 335 | // Create a new lock. |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 336 | mRefLock = std::make_shared<Mutex>(); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 337 | weakPtr = mRefLock; |
| 338 | } |
| 339 | } |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 340 | LocalLockHolder(LocalLockHolder&& other) noexcept |
| 341 | : mKey(std::move(other.mKey)), |
| 342 | mMap(other.mMap), |
| 343 | mMapLock(other.mMapLock), |
| 344 | mRefLock(std::move(other.mRefLock)) { |
| 345 | other.mRefLock.reset(); |
| 346 | } |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 347 | ~LocalLockHolder() { |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 348 | if (!mRefLock) { |
| 349 | return; |
| 350 | } |
| 351 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 352 | std::lock_guard lock(mMapLock); |
| 353 | // Clear the strong pointer. |
| 354 | mRefLock.reset(); |
| 355 | auto found = mMap.find(mKey); |
| 356 | if (found == mMap.end()) { |
| 357 | return; |
| 358 | } |
| 359 | const auto& weakPtr = found->second; |
| 360 | // If this was the last pointer then it's ok to remove the map entry. |
| 361 | if (weakPtr.expired()) { |
| 362 | mMap.erase(found); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void lock() { mRefLock->lock(); } |
| 367 | void unlock() { mRefLock->unlock(); } |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 368 | void lock_shared() { mRefLock->lock_shared(); } |
| 369 | void unlock_shared() { mRefLock->unlock_shared(); } |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 370 | |
| 371 | private: |
| 372 | Key mKey; |
| 373 | Map& mMap; |
| 374 | MapLock& mMapLock; |
| 375 | StrongPointer mRefLock; |
| 376 | }; |
| 377 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 378 | using UserLock = LocalLockHolder<userid_t, std::shared_mutex>; |
| 379 | using UserWriteLockGuard = std::unique_lock<UserLock>; |
| 380 | using UserReadLockGuard = std::shared_lock<UserLock>; |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 381 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 382 | using PackageLock = LocalLockHolder<std::string, std::recursive_mutex>; |
| 383 | using PackageLockGuard = std::lock_guard<PackageLock>; |
| 384 | |
| 385 | #define LOCK_USER() \ |
| 386 | UserLock localUserLock(userId, mUserIdLock, mLock); \ |
| 387 | UserWriteLockGuard userLock(localUserLock) |
| 388 | |
| 389 | #define LOCK_USER_READ() \ |
| 390 | UserLock localUserLock(userId, mUserIdLock, mLock); \ |
| 391 | UserReadLockGuard userLock(localUserLock) |
| 392 | |
| 393 | #define LOCK_PACKAGE() \ |
| 394 | PackageLock localPackageLock(packageName, mPackageNameLock, mLock); \ |
| 395 | PackageLockGuard packageLock(localPackageLock) |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 396 | |
| 397 | #define LOCK_PACKAGE_USER() \ |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 398 | LOCK_USER_READ(); \ |
| 399 | LOCK_PACKAGE() |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 400 | |
| 401 | #else |
| 402 | |
| 403 | #define LOCK_USER() std::lock_guard lock(mLock) |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 404 | #define LOCK_PACKAGE() std::lock_guard lock(mLock) |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 405 | #define LOCK_PACKAGE_USER() \ |
| 406 | (void)userId; \ |
| 407 | std::lock_guard lock(mLock) |
| 408 | |
| 409 | #endif // GRANULAR_LOCKS |
| 410 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 411 | } // namespace |
| 412 | |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 413 | binder::Status InstalldNativeService::FsveritySetupAuthToken::authenticate( |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 414 | const ParcelFileDescriptor& authFd, int32_t uid) { |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 415 | int open_flags = fcntl(authFd.get(), F_GETFL); |
| 416 | if (open_flags < 0) { |
| 417 | return exception(binder::Status::EX_SERVICE_SPECIFIC, "fcntl failed"); |
| 418 | } |
| 419 | if ((open_flags & O_ACCMODE) != O_WRONLY && (open_flags & O_ACCMODE) != O_RDWR) { |
| 420 | return exception(binder::Status::EX_SECURITY, "Received FD with unexpected open flag"); |
| 421 | } |
| 422 | if (fstat(authFd.get(), &this->mStatFromAuthFd) < 0) { |
| 423 | return exception(binder::Status::EX_SERVICE_SPECIFIC, "fstat failed"); |
| 424 | } |
| 425 | if (!S_ISREG(this->mStatFromAuthFd.st_mode)) { |
| 426 | return exception(binder::Status::EX_SECURITY, "Not a regular file"); |
| 427 | } |
| 428 | // Don't accept a file owned by a different app. |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 429 | if (this->mStatFromAuthFd.st_uid != (uid_t)uid) { |
| 430 | return exception(binder::Status::EX_SERVICE_SPECIFIC, "File not owned by uid"); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 431 | } |
| 432 | return ok(); |
| 433 | } |
| 434 | |
| 435 | bool InstalldNativeService::FsveritySetupAuthToken::isSameStat(const struct stat& st) const { |
| 436 | return memcmp(&st, &mStatFromAuthFd, sizeof(st)) == 0; |
| 437 | } |
| 438 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 439 | status_t InstalldNativeService::start() { |
| 440 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 441 | status_t ret = BinderService<InstalldNativeService>::publish(); |
| 442 | if (ret != android::OK) { |
| 443 | return ret; |
| 444 | } |
| 445 | sp<ProcessState> ps(ProcessState::self()); |
| 446 | ps->startThreadPool(); |
| 447 | ps->giveThreadPoolName(); |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 448 | sAppDataIsolationEnabled = android::base::GetBoolProperty( |
Ricky Wai | 30f947d | 2019-12-19 17:24:48 +0000 | [diff] [blame] | 449 | kAppDataIsolationEnabledProperty, true); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 450 | return android::OK; |
| 451 | } |
| 452 | |
Alex Buynytskyy | 17b12dc | 2022-09-12 20:57:24 -0700 | [diff] [blame] | 453 | status_t InstalldNativeService::dump(int fd, const Vector<String16>& /* args */) { |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 454 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 455 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 456 | dprintf(fd, "Storage mounts:\n"); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 457 | for (const auto& n : mStorageMounts) { |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 458 | dprintf(fd, " %s = %s\n", n.first.c_str(), n.second.c_str()); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 459 | } |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 460 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 461 | |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 462 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 463 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 464 | dprintf(fd, "Per-UID cache quotas:\n"); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 465 | for (const auto& n : mCacheQuotas) { |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 466 | dprintf(fd, " %d = %" PRId64 "\n", n.first, n.second); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 467 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Keun young Park | 144a989 | 2021-10-13 16:43:58 -0700 | [diff] [blame] | 470 | dprintf(fd, "is_dexopt_blocked:%d\n", android::installd::is_dexopt_blocked()); |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 471 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 472 | return NO_ERROR; |
| 473 | } |
| 474 | |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 475 | constexpr const char kXattrRestoreconInProgress[] = "user.restorecon_in_progress"; |
| 476 | |
| 477 | static std::string lgetfilecon(const std::string& path) { |
| 478 | char* context; |
| 479 | if (::lgetfilecon(path.c_str(), &context) < 0) { |
| 480 | PLOG(ERROR) << "Failed to lgetfilecon for " << path; |
| 481 | return {}; |
| 482 | } |
| 483 | std::string result{context}; |
| 484 | free(context); |
| 485 | return result; |
| 486 | } |
| 487 | |
| 488 | static bool getRestoreconInProgress(const std::string& path) { |
| 489 | bool inProgress = false; |
| 490 | if (getxattr(path.c_str(), kXattrRestoreconInProgress, &inProgress, sizeof(inProgress)) != |
| 491 | sizeof(inProgress)) { |
| 492 | if (errno != ENODATA) { |
| 493 | PLOG(ERROR) << "Failed to check in-progress restorecon for " << path; |
| 494 | } |
| 495 | return false; |
| 496 | } |
| 497 | return inProgress; |
| 498 | } |
| 499 | |
| 500 | struct RestoreconInProgress { |
| 501 | explicit RestoreconInProgress(const std::string& path) : mPath(path) { |
| 502 | bool inProgress = true; |
| 503 | if (setxattr(mPath.c_str(), kXattrRestoreconInProgress, &inProgress, sizeof(inProgress), |
| 504 | 0) != 0) { |
| 505 | PLOG(ERROR) << "Failed to set in-progress restorecon for " << path; |
| 506 | } |
| 507 | } |
| 508 | ~RestoreconInProgress() { |
| 509 | if (removexattr(mPath.c_str(), kXattrRestoreconInProgress) < 0) { |
| 510 | PLOG(ERROR) << "Failed to clear in-progress restorecon for " << mPath; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | const std::string& mPath; |
| 516 | }; |
| 517 | |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 518 | /** |
| 519 | * Perform restorecon of the given path, but only perform recursive restorecon |
| 520 | * if the label of that top-level file actually changed. This can save us |
| 521 | * significant time by avoiding no-op traversals of large filesystem trees. |
| 522 | */ |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 523 | static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, |
| 524 | bool existing) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 525 | ScopedTrace tracer("restorecon-lazy"); |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 526 | if (!existing) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 527 | ScopedTrace tracer("new-path"); |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 528 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, |
| 529 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { |
| 530 | PLOG(ERROR) << "Failed recursive restorecon for " << path; |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 531 | return -1; |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 532 | } |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 533 | return 0; |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 534 | } |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 535 | |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 536 | // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 537 | |
| 538 | // Check to see if there was an interrupted operation. |
| 539 | bool inProgress = getRestoreconInProgress(path); |
| 540 | std::string before, after; |
| 541 | if (!inProgress) { |
| 542 | if (before = lgetfilecon(path); before.empty()) { |
| 543 | PLOG(ERROR) << "Failed before getfilecon for " << path; |
| 544 | return -1; |
| 545 | } |
| 546 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { |
| 547 | PLOG(ERROR) << "Failed top-level restorecon for " << path; |
| 548 | return -1; |
| 549 | } |
| 550 | if (after = lgetfilecon(path); after.empty()) { |
| 551 | PLOG(ERROR) << "Failed after getfilecon for " << path; |
| 552 | return -1; |
| 553 | } |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | // If the initial top-level restorecon above changed the label, then go |
| 557 | // back and restorecon everything recursively |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 558 | if (inProgress || before != after) { |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 559 | if (existing) { |
| 560 | LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " |
Alex Buynytskyy | 02dca6b | 2023-11-28 19:06:34 +0000 | [diff] [blame] | 561 | << path << "; running recursive restorecon"; |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 562 | } |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 563 | |
Alex Buynytskyy | 02dca6b | 2023-11-28 19:06:34 +0000 | [diff] [blame] | 564 | auto restorecon = [path, seInfo, uid]() { |
| 565 | ScopedTrace tracer("label-change"); |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 566 | |
Alex Buynytskyy | 02dca6b | 2023-11-28 19:06:34 +0000 | [diff] [blame] | 567 | // Temporary mark the folder as "in-progress" to resume in case of reboot/other failure. |
| 568 | RestoreconInProgress fence(path); |
| 569 | |
| 570 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, |
| 571 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { |
| 572 | PLOG(ERROR) << "Failed recursive restorecon for " << path; |
| 573 | return -1; |
| 574 | } |
| 575 | return 0; |
| 576 | }; |
| 577 | if (inProgress) { |
| 578 | // The previous restorecon was interrupted. It's either crashed (unlikely), or the phone |
| 579 | // was rebooted. Possibly because it took too much time. This time let's move it to a |
| 580 | // separate thread - so it won't block the rest of the OS. |
| 581 | std::thread(restorecon).detach(); |
| 582 | } else { |
| 583 | if (int result = restorecon(); result) { |
| 584 | return result; |
| 585 | } |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 589 | return 0; |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 590 | } |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 591 | static bool internal_storage_has_project_id() { |
| 592 | // The following path is populated in setFirstBoot, so if this file is present |
Shikha Malhotra | dcf469b | 2022-03-23 11:53:58 +0000 | [diff] [blame] | 593 | // then project ids can be used. Using call once to cache the result of this check |
| 594 | // to avoid having to check the file presence again and again. |
| 595 | std::call_once(flag, []() { |
| 596 | auto using_project_ids = |
| 597 | StringPrintf("%smisc/installd/using_project_ids", android_data_dir.c_str()); |
| 598 | sUsingProjectIdsFlag = access(using_project_ids.c_str(), F_OK) == 0; |
| 599 | }); |
Shikha Malhotra | 8f55b3d | 2022-04-04 14:11:43 +0000 | [diff] [blame] | 600 | // return sUsingProjectIdsFlag; |
| 601 | return false; |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid, gid_t gid, |
| 605 | long project_id) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 606 | { |
| 607 | ScopedTrace tracer("prepare-dir"); |
| 608 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { |
| 609 | PLOG(ERROR) << "Failed to prepare " << path; |
| 610 | return -1; |
| 611 | } |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 612 | } |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 613 | if (internal_storage_has_project_id()) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 614 | ScopedTrace tracer("set-quota"); |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 615 | return set_quota_project_id(path, project_id, true); |
| 616 | } |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 620 | static int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 621 | uid_t uid, gid_t gid, long project_id) { |
| 622 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 623 | int ret; |
| 624 | { |
| 625 | ScopedTrace tracer("prepare-cache-dir"); |
| 626 | ret = prepare_app_cache_dir(parent, name, target_mode, uid, gid); |
| 627 | } |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 628 | if (ret == 0 && internal_storage_has_project_id()) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 629 | ScopedTrace tracer("set-quota-cache-dir"); |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 630 | return set_quota_project_id(path, project_id, true); |
| 631 | } |
| 632 | return ret; |
| 633 | } |
| 634 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 635 | static bool prepare_app_profile_dir(const std::string& packageName, int32_t appId, int32_t userId) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 636 | ScopedTrace tracer("prepare-app-profile"); |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 637 | int32_t uid = multiuser_get_uid(userId, appId); |
| 638 | int shared_app_gid = multiuser_get_shared_gid(userId, appId); |
| 639 | if (shared_app_gid == -1) { |
| 640 | // TODO(calin): this should no longer be possible but do not continue if we don't get |
| 641 | // a valid shared gid. |
| 642 | PLOG(WARNING) << "Invalid shared_app_gid for " << packageName; |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | const std::string profile_dir = |
| 647 | create_primary_current_profile_package_dir_path(userId, packageName); |
| 648 | // read-write-execute only for the app user. |
| 649 | if (fs_prepare_dir_strict(profile_dir.c_str(), 0700, uid, uid) != 0) { |
| 650 | PLOG(ERROR) << "Failed to prepare " << profile_dir; |
| 651 | return false; |
| 652 | } |
Alan Stokes | 928d2c1 | 2020-12-09 10:11:24 +0000 | [diff] [blame] | 653 | if (selinux_android_restorecon(profile_dir.c_str(), 0)) { |
| 654 | PLOG(ERROR) << "Failed to restorecon " << profile_dir; |
| 655 | return false; |
| 656 | } |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 657 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 658 | const std::string ref_profile_path = |
| 659 | create_primary_reference_profile_package_dir_path(packageName); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 660 | |
| 661 | // Prepare the reference profile directory. Note that we use the non strict version of |
| 662 | // fs_prepare_dir. This will fix the permission and the ownership to the correct values. |
| 663 | // This is particularly important given that in O there were some fixes for how the |
| 664 | // shared_app_gid is computed. |
| 665 | // |
| 666 | // Note that by the time we get here we know that we are using a correct uid (otherwise |
| 667 | // prepare_app_dir and the above fs_prepare_file_strict which check the uid). So we |
| 668 | // are sure that the gid being used belongs to the owning app and not someone else. |
| 669 | // |
| 670 | // dex2oat/profman runs under the shared app gid and it needs to read/write reference profiles. |
| 671 | if (fs_prepare_dir(ref_profile_path.c_str(), 0770, AID_SYSTEM, shared_app_gid) != 0) { |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 672 | PLOG(ERROR) << "Failed to prepare " << ref_profile_path; |
| 673 | return false; |
| 674 | } |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 675 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 676 | return true; |
| 677 | } |
| 678 | |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 679 | static bool chown_app_dir(const std::string& path, uid_t uid, uid_t previousUid, gid_t cacheGid) { |
| 680 | FTS* fts; |
| 681 | char *argv[] = { (char*) path.c_str(), nullptr }; |
| 682 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
| 683 | return false; |
| 684 | } |
| 685 | for (FTSENT* p; (p = fts_read(fts)) != nullptr;) { |
| 686 | if (p->fts_info == FTS_D && p->fts_level == 1 |
| 687 | && (strcmp(p->fts_name, "cache") == 0 |
| 688 | || strcmp(p->fts_name, "code_cache") == 0)) { |
| 689 | // Mark cache dirs |
| 690 | p->fts_number = 1; |
| 691 | } else { |
| 692 | // Inherit parent's number |
| 693 | p->fts_number = p->fts_parent->fts_number; |
| 694 | } |
| 695 | |
| 696 | switch (p->fts_info) { |
| 697 | case FTS_D: |
| 698 | case FTS_F: |
| 699 | case FTS_SL: |
| 700 | case FTS_SLNONE: |
| 701 | if (p->fts_statp->st_uid == previousUid) { |
| 702 | if (lchown(p->fts_path, uid, p->fts_number ? cacheGid : uid) != 0) { |
| 703 | PLOG(WARNING) << "Failed to lchown " << p->fts_path; |
| 704 | } |
| 705 | } else { |
| 706 | LOG(WARNING) << "Ignoring " << p->fts_path << " with unexpected UID " |
| 707 | << p->fts_statp->st_uid << " instead of " << previousUid; |
| 708 | } |
| 709 | break; |
| 710 | } |
| 711 | } |
| 712 | fts_close(fts); |
| 713 | return true; |
| 714 | } |
| 715 | |
| 716 | static void chown_app_profile_dir(const std::string &packageName, int32_t appId, int32_t userId) { |
| 717 | uid_t uid = multiuser_get_uid(userId, appId); |
| 718 | gid_t sharedGid = multiuser_get_shared_gid(userId, appId); |
| 719 | |
| 720 | const std::string profile_dir = |
| 721 | create_primary_current_profile_package_dir_path(userId, packageName); |
| 722 | char *argv[] = { (char*) profile_dir.c_str(), nullptr }; |
| 723 | if (FTS* fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr)) { |
| 724 | for (FTSENT* p; (p = fts_read(fts)) != nullptr;) { |
| 725 | switch (p->fts_info) { |
| 726 | case FTS_D: |
| 727 | case FTS_F: |
| 728 | case FTS_SL: |
| 729 | case FTS_SLNONE: |
| 730 | if (lchown(p->fts_path, uid, uid) != 0) { |
| 731 | PLOG(WARNING) << "Failed to lchown " << p->fts_path; |
| 732 | } |
| 733 | break; |
| 734 | } |
| 735 | } |
| 736 | fts_close(fts); |
| 737 | } |
| 738 | |
| 739 | const std::string ref_profile_path = |
| 740 | create_primary_reference_profile_package_dir_path(packageName); |
| 741 | argv[0] = (char *) ref_profile_path.c_str(); |
| 742 | if (FTS* fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr)) { |
| 743 | for (FTSENT* p; (p = fts_read(fts)) != nullptr;) { |
| 744 | if (p->fts_info == FTS_D && p->fts_level == 0) { |
| 745 | if (chown(p->fts_path, AID_SYSTEM, sharedGid) != 0) { |
| 746 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 747 | } |
| 748 | continue; |
| 749 | } |
| 750 | switch (p->fts_info) { |
| 751 | case FTS_D: |
| 752 | case FTS_F: |
| 753 | case FTS_SL: |
| 754 | case FTS_SLNONE: |
| 755 | if (lchown(p->fts_path, sharedGid, sharedGid) != 0) { |
| 756 | PLOG(WARNING) << "Failed to lchown " << p->fts_path; |
| 757 | } |
| 758 | break; |
| 759 | } |
| 760 | } |
| 761 | fts_close(fts); |
| 762 | } |
| 763 | } |
| 764 | |
Mohammad Samiul Islam | a73327a | 2022-03-09 09:59:32 +0000 | [diff] [blame] | 765 | static binder::Status createAppDataDirs(const std::string& path, int32_t uid, int32_t gid, |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 766 | int32_t previousUid, int32_t cacheGid, |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 767 | const std::string& seInfo, mode_t targetMode, |
| 768 | long projectIdApp, long projectIdCache) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 769 | ScopedTrace tracer("create-dirs"); |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 770 | struct stat st{}; |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 771 | bool parent_dir_exists = (stat(path.c_str(), &st) == 0); |
| 772 | |
| 773 | auto cache_path = StringPrintf("%s/%s", path.c_str(), "cache"); |
| 774 | auto code_cache_path = StringPrintf("%s/%s", path.c_str(), "code_cache"); |
| 775 | bool cache_exists = (access(cache_path.c_str(), F_OK) == 0); |
| 776 | bool code_cache_exists = (access(code_cache_path.c_str(), F_OK) == 0); |
| 777 | |
| 778 | if (parent_dir_exists) { |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 779 | if (previousUid > 0 && previousUid != uid) { |
| 780 | if (!chown_app_dir(path, uid, previousUid, cacheGid)) { |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 781 | return error("Failed to chown " + path); |
| 782 | } |
| 783 | } |
| 784 | } |
| 785 | |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 786 | // Prepare only the parent app directory |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 787 | if (prepare_app_dir(path, targetMode, uid, gid, projectIdApp) || |
| 788 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid, projectIdCache) || |
| 789 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid, projectIdCache)) { |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 790 | return error("Failed to prepare " + path); |
| 791 | } |
| 792 | |
| 793 | // Consider restorecon over contents if label changed |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 794 | if (restorecon_app_data_lazy(path, seInfo, uid, parent_dir_exists)) { |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 795 | return error("Failed to restorecon " + path); |
| 796 | } |
| 797 | |
Jigar Thakkar | f42c8a7 | 2021-10-28 17:24:33 +0000 | [diff] [blame] | 798 | // If the parent dir exists, the restorecon would already have been done |
| 799 | // as a part of the recursive restorecon above |
| 800 | if (parent_dir_exists && !cache_exists |
| 801 | && restorecon_app_data_lazy(cache_path, seInfo, uid, false)) { |
| 802 | return error("Failed to restorecon " + cache_path); |
| 803 | } |
| 804 | |
| 805 | // If the parent dir exists, the restorecon would already have been done |
| 806 | // as a part of the recursive restorecon above |
| 807 | if (parent_dir_exists && !code_cache_exists |
| 808 | && restorecon_app_data_lazy(code_cache_path, seInfo, uid, false)) { |
| 809 | return error("Failed to restorecon " + code_cache_path); |
| 810 | } |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 811 | return ok(); |
| 812 | } |
| 813 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 814 | binder::Status InstalldNativeService::createAppDataLocked( |
| 815 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 816 | int32_t flags, int32_t appId, int32_t previousAppId, const std::string& seInfo, |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 817 | int32_t targetSdkVersion, int64_t* ceDataInode, int64_t* deDataInode) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 818 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 819 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 820 | CHECK_ARGUMENT_UUID(uuid); |
| 821 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 822 | |
| 823 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 824 | const char* pkgname = packageName.c_str(); |
| 825 | |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 826 | // Assume invalid inode unless filled in below |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 827 | if (ceDataInode != nullptr) *ceDataInode = -1; |
| 828 | if (deDataInode != nullptr) *deDataInode = -1; |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 829 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 830 | int32_t uid = multiuser_get_uid(userId, appId); |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 831 | |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 832 | // If previousAppId > 0, an app is changing its app ID |
| 833 | int32_t previousUid = |
| 834 | previousAppId > 0 ? (int32_t)multiuser_get_uid(userId, previousAppId) : -1; |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 835 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 836 | int32_t cacheGid = multiuser_get_cache_gid(userId, appId); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 837 | mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; |
| 838 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 839 | // If UID doesn't have a specific cache GID, use UID value |
| 840 | if (cacheGid == -1) { |
| 841 | cacheGid = uid; |
| 842 | } |
| 843 | |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 844 | long projectIdApp = get_project_id(uid, PROJECT_ID_APP_START); |
| 845 | long projectIdCache = get_project_id(uid, PROJECT_ID_APP_CACHE_START); |
| 846 | |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 847 | if (flags & FLAG_STORAGE_CE) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 848 | ScopedTrace tracer("ce"); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 849 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 850 | |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 851 | auto status = createAppDataDirs(path, uid, uid, previousUid, cacheGid, seInfo, targetMode, |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 852 | projectIdApp, projectIdCache); |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 853 | if (!status.isOk()) { |
| 854 | return status; |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 855 | } |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 856 | |
| 857 | // Remember inode numbers of cache directories so that we can clear |
| 858 | // contents while CE storage is locked |
| 859 | if (write_path_inode(path, "cache", kXattrInodeCache) || |
| 860 | write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 861 | return error("Failed to write_path_inode for " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 862 | } |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 863 | |
| 864 | // And return the CE inode of the top-level data directory so we can |
| 865 | // clear contents while CE storage is locked |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 866 | if (ceDataInode != nullptr) { |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 867 | ino_t result; |
| 868 | if (get_path_inode(path, &result) != 0) { |
| 869 | return error("Failed to get_path_inode for " + path); |
| 870 | } |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 871 | *ceDataInode = static_cast<uint64_t>(result); |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 872 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 873 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 874 | if (flags & FLAG_STORAGE_DE) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 875 | ScopedTrace tracer("de"); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 876 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 877 | |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 878 | auto status = createAppDataDirs(path, uid, uid, previousUid, cacheGid, seInfo, targetMode, |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 879 | projectIdApp, projectIdCache); |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 880 | if (!status.isOk()) { |
| 881 | return status; |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 882 | } |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 883 | if (previousUid > 0 && previousUid != uid) { |
John Wu | 3b38337 | 2022-02-09 02:21:39 -0800 | [diff] [blame] | 884 | chown_app_profile_dir(packageName, appId, userId); |
| 885 | } |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 886 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 887 | if (!prepare_app_profile_dir(packageName, appId, userId)) { |
| 888 | return error("Failed to prepare profiles for " + packageName); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 889 | } |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 890 | |
| 891 | if (deDataInode != nullptr) { |
| 892 | ino_t result; |
| 893 | if (get_path_inode(path, &result) != 0) { |
| 894 | return error("Failed to get_path_inode for " + path); |
| 895 | } |
| 896 | *deDataInode = static_cast<uint64_t>(result); |
| 897 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 898 | } |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 899 | |
Samiul Islam | 7d5b1da | 2022-02-21 15:23:36 +0000 | [diff] [blame] | 900 | if (flags & FLAG_STORAGE_SDK) { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 901 | ScopedTrace tracer("sdk"); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 902 | // Safe to ignore status since we can retry creating this by calling reconcileSdkData |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 903 | auto ignore = createSdkSandboxDataPackageDirectory(uuid, packageName, userId, appId, flags); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 904 | if (!ignore.isOk()) { |
| 905 | PLOG(WARNING) << "Failed to create sdk data package directory for " << packageName; |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 906 | } |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 907 | } else { |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 908 | ScopedTrace tracer("destroy-sdk"); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 909 | // Package does not need sdk storage. Remove it. |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 910 | destroySdkSandboxDataPackageDirectory(uuid, packageName, userId, flags); |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | return ok(); |
| 914 | } |
| 915 | |
| 916 | /** |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 917 | * Responsible for creating /data/misc_{ce|de}/user/0/sdksandbox/<package-name> directory and other |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 918 | * app level sub directories, such as ./shared |
| 919 | */ |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 920 | binder::Status InstalldNativeService::createSdkSandboxDataPackageDirectory( |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 921 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 922 | int32_t appId, int32_t flags) { |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 923 | ENFORCE_VALID_USER(userId); |
| 924 | |
Nikita Ioffe | 4ee18ae | 2022-02-21 19:02:05 +0000 | [diff] [blame] | 925 | int32_t sdkSandboxUid = multiuser_get_sdk_sandbox_uid(userId, appId); |
| 926 | if (sdkSandboxUid == -1) { |
| 927 | // There no valid sdk sandbox process for this app. Skip creation of data directory |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 928 | return ok(); |
| 929 | } |
| 930 | |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 931 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 932 | |
| 933 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 934 | for (int currentFlag : storageFlags) { |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 935 | if ((flags & currentFlag) == 0) { |
| 936 | continue; |
| 937 | } |
| 938 | bool isCeData = (currentFlag == FLAG_STORAGE_CE); |
| 939 | |
Nikita Ioffe | 4ee18ae | 2022-02-21 19:02:05 +0000 | [diff] [blame] | 940 | // /data/misc_{ce,de}/<user-id>/sdksandbox directory gets created by vold |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 941 | // during user creation |
| 942 | |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 943 | // Prepare the package directory |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 944 | auto packagePath = create_data_misc_sdk_sandbox_package_path(uuid_, isCeData, userId, |
| 945 | packageName.c_str()); |
| 946 | #if SDK_DEBUG |
| 947 | LOG(DEBUG) << "Creating app-level sdk data directory: " << packagePath; |
| 948 | #endif |
| 949 | |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 950 | if (prepare_app_dir(packagePath, 0751, AID_SYSTEM, AID_SYSTEM, 0)) { |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 951 | return error("Failed to prepare " + packagePath); |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 952 | } |
Samiul Islam | c40dff5 | 2022-01-14 16:24:48 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 955 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 958 | binder::Status InstalldNativeService::createAppData( |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 959 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 960 | int32_t flags, int32_t appId, int32_t previousAppId, const std::string& seInfo, |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 961 | int32_t targetSdkVersion, int64_t* ceDataInode, int64_t* deDataInode) { |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 962 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 963 | ENFORCE_VALID_USER(userId); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 964 | CHECK_ARGUMENT_UUID(uuid); |
| 965 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 966 | LOCK_PACKAGE_USER(); |
| 967 | return createAppDataLocked(uuid, packageName, userId, flags, appId, previousAppId, seInfo, |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 968 | targetSdkVersion, ceDataInode, deDataInode); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | binder::Status InstalldNativeService::createAppData( |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 972 | const android::os::CreateAppDataArgs& args, |
| 973 | android::os::CreateAppDataResult* _aidl_return) { |
| 974 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 975 | ENFORCE_VALID_USER(args.userId); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 976 | // Locking is performed depeer in the callstack. |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 977 | |
| 978 | int64_t ceDataInode = -1; |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 979 | int64_t deDataInode = -1; |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 980 | auto status = createAppData(args.uuid, args.packageName, args.userId, args.flags, args.appId, |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 981 | args.previousAppId, args.seInfo, args.targetSdkVersion, |
| 982 | &ceDataInode, &deDataInode); |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 983 | _aidl_return->ceDataInode = ceDataInode; |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 984 | _aidl_return->deDataInode = deDataInode; |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 985 | _aidl_return->exceptionCode = status.exceptionCode(); |
| 986 | _aidl_return->exceptionMessage = status.exceptionMessage(); |
| 987 | return ok(); |
| 988 | } |
| 989 | |
| 990 | binder::Status InstalldNativeService::createAppDataBatched( |
| 991 | const std::vector<android::os::CreateAppDataArgs>& args, |
| 992 | std::vector<android::os::CreateAppDataResult>* _aidl_return) { |
| 993 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 994 | for (const auto& arg : args) { |
| 995 | ENFORCE_VALID_USER(arg.userId); |
| 996 | } |
| 997 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 998 | // Locking is performed depeer in the callstack. |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 999 | |
| 1000 | std::vector<android::os::CreateAppDataResult> results; |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 1001 | for (const auto &arg : args) { |
Jeff Sharkey | 8d3848b | 2020-08-13 14:16:46 -0600 | [diff] [blame] | 1002 | android::os::CreateAppDataResult result; |
| 1003 | createAppData(arg, &result); |
| 1004 | results.push_back(result); |
| 1005 | } |
| 1006 | *_aidl_return = results; |
| 1007 | return ok(); |
| 1008 | } |
| 1009 | |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1010 | binder::Status InstalldNativeService::reconcileSdkData( |
| 1011 | const android::os::ReconcileSdkDataArgs& args) { |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1012 | ENFORCE_VALID_USER(args.userId); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1013 | // Locking is performed depeer in the callstack. |
| 1014 | |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1015 | return reconcileSdkData(args.uuid, args.packageName, args.subDirNames, args.userId, args.appId, |
| 1016 | args.previousAppId, args.seInfo, args.flags); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Reconciles per-sdk directory under app-level sdk data directory. |
| 1021 | |
| 1022 | * E.g. `/data/misc_ce/0/sdksandbox/<package-name>/<sdkPackageName>-<randomSuffix> |
| 1023 | * |
| 1024 | * - If the sdk data package directory is missing, we create it first. |
| 1025 | * - If sdkPackageNames is empty, we delete sdk package directory since it's not needed anymore. |
| 1026 | * - If a sdk level directory we need to prepare already exist, we skip creating it again. This |
| 1027 | * is to avoid having same per-sdk directory with different suffix. |
| 1028 | * - If a sdk level directory exist which is absent from sdkPackageNames, we remove it. |
| 1029 | */ |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1030 | binder::Status InstalldNativeService::reconcileSdkData(const std::optional<std::string>& uuid, |
| 1031 | const std::string& packageName, |
| 1032 | const std::vector<std::string>& subDirNames, |
| 1033 | int userId, int appId, int previousAppId, |
| 1034 | const std::string& seInfo, int flags) { |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 1035 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1036 | ENFORCE_VALID_USER(userId); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1037 | CHECK_ARGUMENT_UUID(uuid); |
| 1038 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1039 | LOCK_PACKAGE_USER(); |
| 1040 | |
| 1041 | #if SDK_DEBUG |
| 1042 | LOG(DEBUG) << "Creating per sdk data directory for: " << packageName; |
| 1043 | #endif |
| 1044 | |
| 1045 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1046 | |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1047 | // Prepare the sdk package directory in case it's missing |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1048 | const auto status = |
| 1049 | createSdkSandboxDataPackageDirectory(uuid, packageName, userId, appId, flags); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1050 | if (!status.isOk()) { |
| 1051 | return status; |
| 1052 | } |
| 1053 | |
| 1054 | auto res = ok(); |
| 1055 | // We have to create sdk data for CE and DE storage |
| 1056 | const int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 1057 | for (int currentFlag : storageFlags) { |
| 1058 | if ((flags & currentFlag) == 0) { |
| 1059 | continue; |
| 1060 | } |
| 1061 | const bool isCeData = (currentFlag == FLAG_STORAGE_CE); |
| 1062 | |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1063 | const auto packagePath = create_data_misc_sdk_sandbox_package_path(uuid_, isCeData, userId, |
| 1064 | packageName.c_str()); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1065 | |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1066 | // Remove existing sub-directories not referred in subDirNames |
| 1067 | const std::unordered_set<std::string> expectedSubDirNames(subDirNames.begin(), |
| 1068 | subDirNames.end()); |
| 1069 | const auto subDirHandler = [&packagePath, &expectedSubDirNames, |
| 1070 | &res](const std::string& subDirName) { |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1071 | // Remove the per-sdk directory if it is not referred in |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1072 | // expectedSubDirNames |
| 1073 | if (expectedSubDirNames.find(subDirName) == expectedSubDirNames.end()) { |
| 1074 | auto path = packagePath + "/" + subDirName; |
| 1075 | if (delete_dir_contents_and_dir(path) != 0) { |
| 1076 | res = error("Failed to delete " + path); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1077 | return; |
| 1078 | } |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1079 | } |
| 1080 | }; |
| 1081 | const int ec = foreach_subdir(packagePath, subDirHandler); |
| 1082 | if (ec != 0) { |
| 1083 | res = error("Failed to process subdirs for " + packagePath); |
| 1084 | continue; |
| 1085 | } |
| 1086 | |
Mohammad Samiul Islam | 5288b05 | 2022-03-09 17:04:38 +0000 | [diff] [blame] | 1087 | // Now create the subDirNames |
| 1088 | for (const auto& subDirName : subDirNames) { |
| 1089 | const std::string path = |
| 1090 | create_data_misc_sdk_sandbox_sdk_path(uuid_, isCeData, userId, |
| 1091 | packageName.c_str(), subDirName.c_str()); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1092 | |
| 1093 | // Create the directory along with cache and code_cache |
| 1094 | const int32_t cacheGid = multiuser_get_cache_gid(userId, appId); |
| 1095 | if (cacheGid == -1) { |
| 1096 | return exception(binder::Status::EX_ILLEGAL_STATE, |
| 1097 | StringPrintf("cacheGid cannot be -1 for sdk data")); |
| 1098 | } |
| 1099 | const int32_t sandboxUid = multiuser_get_sdk_sandbox_uid(userId, appId); |
| 1100 | int32_t previousSandboxUid = multiuser_get_sdk_sandbox_uid(userId, previousAppId); |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 1101 | int32_t appUid = multiuser_get_uid(userId, appId); |
| 1102 | long projectIdApp = get_project_id(appUid, PROJECT_ID_APP_START); |
| 1103 | long projectIdCache = get_project_id(appUid, PROJECT_ID_APP_CACHE_START); |
| 1104 | auto status = |
John Wu | 841a802 | 2022-04-05 21:17:54 +0000 | [diff] [blame] | 1105 | createAppDataDirs(path, sandboxUid, AID_NOBODY, previousSandboxUid, cacheGid, |
Samiul Islam | 8621e55 | 2022-03-30 16:26:45 +0100 | [diff] [blame] | 1106 | seInfo, 0700 | S_ISGID, projectIdApp, projectIdCache); |
Samiul Islam | 92f5cf0 | 2022-02-03 12:45:51 +0000 | [diff] [blame] | 1107 | if (!status.isOk()) { |
| 1108 | res = status; |
| 1109 | continue; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | return res; |
| 1115 | } |
| 1116 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1117 | binder::Status InstalldNativeService::migrateAppData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1118 | const std::string& packageName, int32_t userId, int32_t flags) { |
| 1119 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1120 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1121 | CHECK_ARGUMENT_UUID(uuid); |
| 1122 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1123 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1124 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1125 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1126 | const char* pkgname = packageName.c_str(); |
| 1127 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1128 | // This method only exists to upgrade system apps that have requested |
| 1129 | // forceDeviceEncrypted, so their default storage always lives in a |
| 1130 | // consistent location. This only works on non-FBE devices, since we |
| 1131 | // never want to risk exposing data on a device with real CE/DE storage. |
| 1132 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1133 | auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 1134 | auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1135 | |
| 1136 | // If neither directory is marked as default, assume CE is default |
| 1137 | if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 |
| 1138 | && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 1139 | if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1140 | return error("Failed to mark default storage " + ce_path); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | // Migrate default data location if needed |
| 1145 | auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; |
| 1146 | auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; |
| 1147 | |
| 1148 | if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 1149 | LOG(WARNING) << "Requested default storage " << target |
| 1150 | << " is not active; migrating from " << source; |
| 1151 | if (delete_dir_contents_and_dir(target) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1152 | return error("Failed to delete " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1153 | } |
| 1154 | if (rename(source.c_str(), target.c_str()) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1155 | return error("Failed to rename " + source + " to " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1156 | } |
| 1157 | } |
| 1158 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1159 | return ok(); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 1162 | |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1163 | binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName, |
| 1164 | const std::string& profileName) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1165 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1166 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | df2fb61 | 2022-08-12 15:42:01 -0700 | [diff] [blame] | 1167 | CHECK_ARGUMENT_FILE_NAME(profileName); |
| 1168 | if (!base::EndsWith(profileName, ".prof")) { |
| 1169 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 1170 | StringPrintf("Profile name %s does not end with .prof", |
| 1171 | profileName.c_str())); |
| 1172 | } |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1173 | LOCK_PACKAGE(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1174 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1175 | binder::Status res = ok(); |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1176 | if (!clear_primary_reference_profile(packageName, profileName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1177 | res = error("Failed to clear reference profile for " + packageName); |
| 1178 | } |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 1179 | if (!clear_primary_current_profiles(packageName, profileName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1180 | res = error("Failed to clear current profiles for " + packageName); |
| 1181 | } |
| 1182 | return res; |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1185 | binder::Status InstalldNativeService::clearAppData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1186 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 1187 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1188 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1189 | CHECK_ARGUMENT_UUID(uuid); |
| 1190 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1191 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1192 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1193 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1194 | const char* pkgname = packageName.c_str(); |
| 1195 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1196 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1197 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1198 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1199 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 1200 | path = read_path_inode(path, "cache", kXattrInodeCache); |
| 1201 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 1202 | path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 1203 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1204 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1205 | if (delete_dir_contents(path) != 0) { |
| 1206 | res = error("Failed to delete contents of " + path); |
Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 1207 | } else if ((flags & (FLAG_CLEAR_CACHE_ONLY | FLAG_CLEAR_CODE_CACHE_ONLY)) == 0) { |
| 1208 | remove_path_xattr(path, kXattrInodeCache); |
| 1209 | remove_path_xattr(path, kXattrInodeCodeCache); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1210 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1213 | if (flags & FLAG_STORAGE_DE) { |
John Wu | 84e8f24 | 2021-10-21 11:50:41 -0700 | [diff] [blame] | 1214 | std::string suffix; |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1215 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 1216 | suffix = CACHE_DIR_POSTFIX; |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1217 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 1218 | suffix = CODE_CACHE_DIR_POSTFIX; |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 1219 | } |
| 1220 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1221 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1222 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1223 | if (delete_dir_contents(path) != 0) { |
| 1224 | res = error("Failed to delete contents of " + path); |
| 1225 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1226 | } |
| 1227 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1228 | if (flags & FLAG_STORAGE_EXTERNAL) { |
| 1229 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 1230 | for (const auto& n : mStorageMounts) { |
| 1231 | auto extPath = n.second; |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
| 1234 | std::regex re("^\\/mnt\\/pass_through\\/[0-9]+\\/emulated"); |
| 1235 | if (std::regex_match(extPath, re)) { |
| 1236 | extPath += "/" + std::to_string(userId); |
| 1237 | } |
| 1238 | } else { |
| 1239 | if (n.first.compare(0, 14, "/mnt/media_rw/") != 0) { |
| 1240 | extPath += StringPrintf("/%d", userId); |
| 1241 | } else if (userId != 0) { |
| 1242 | // TODO: support devices mounted under secondary users |
| 1243 | continue; |
| 1244 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1245 | } |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 1246 | |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1247 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 1248 | // Clear only cached data from shared storage |
| 1249 | auto path = StringPrintf("%s/Android/data/%s/cache", extPath.c_str(), pkgname); |
| 1250 | if (delete_dir_contents(path, true) != 0) { |
| 1251 | res = error("Failed to delete contents of " + path); |
| 1252 | } |
| 1253 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 1254 | // No code cache on shared storage |
| 1255 | } else { |
| 1256 | // Clear everything on shared storage |
| 1257 | auto path = StringPrintf("%s/Android/data/%s", extPath.c_str(), pkgname); |
| 1258 | if (delete_dir_contents(path, true) != 0) { |
| 1259 | res = error("Failed to delete contents of " + path); |
| 1260 | } |
| 1261 | path = StringPrintf("%s/Android/media/%s", extPath.c_str(), pkgname); |
| 1262 | if (delete_dir_contents(path, true) != 0) { |
| 1263 | res = error("Failed to delete contents of " + path); |
| 1264 | } |
Martijn Coenen | 24a9307 | 2020-01-08 10:10:43 +0100 | [diff] [blame] | 1265 | // Note that we explicitly don't delete OBBs - those are only removed on |
| 1266 | // app uninstall. |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1267 | } |
| 1268 | } |
| 1269 | } |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 1270 | auto status = clearSdkSandboxDataPackageDirectory(uuid, packageName, userId, flags); |
| 1271 | if (!status.isOk()) { |
| 1272 | res = status; |
| 1273 | } |
| 1274 | return res; |
| 1275 | } |
| 1276 | |
| 1277 | binder::Status InstalldNativeService::clearSdkSandboxDataPackageDirectory( |
| 1278 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 1279 | int32_t flags) { |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1280 | ENFORCE_VALID_USER(userId); |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 1281 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1282 | const char* pkgname = packageName.c_str(); |
| 1283 | |
| 1284 | binder::Status res = ok(); |
| 1285 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 1286 | for (int i = 0; i < 2; i++) { |
| 1287 | int currentFlag = storageFlags[i]; |
| 1288 | if ((flags & currentFlag) == 0) { |
| 1289 | continue; |
| 1290 | } |
| 1291 | bool isCeData = (currentFlag == FLAG_STORAGE_CE); |
| 1292 | std::string suffix; |
| 1293 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 1294 | suffix = CACHE_DIR_POSTFIX; |
| 1295 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 1296 | suffix = CODE_CACHE_DIR_POSTFIX; |
| 1297 | } |
| 1298 | |
| 1299 | auto appPath = create_data_misc_sdk_sandbox_package_path(uuid_, isCeData, userId, pkgname); |
| 1300 | if (access(appPath.c_str(), F_OK) != 0) continue; |
| 1301 | const auto subDirHandler = [&appPath, &res, &suffix](const std::string& filename) { |
| 1302 | auto filepath = appPath + "/" + filename + suffix; |
| 1303 | if (delete_dir_contents(filepath, true) != 0) { |
| 1304 | res = error("Failed to clear contents of " + filepath); |
| 1305 | } |
| 1306 | }; |
| 1307 | const int ec = foreach_subdir(appPath, subDirHandler); |
| 1308 | if (ec != 0) { |
| 1309 | res = error("Failed to process subdirs for " + appPath); |
| 1310 | } |
| 1311 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1312 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 1315 | static int destroy_app_reference_profile(const std::string& pkgname) { |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 1316 | return delete_dir_contents_and_dir( |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1317 | create_primary_reference_profile_package_dir_path(pkgname), |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 1318 | /*ignore_if_missing*/ true); |
| 1319 | } |
| 1320 | |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 1321 | static int destroy_app_current_profiles(const std::string& pkgname, userid_t userid) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 1322 | return delete_dir_contents_and_dir( |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1323 | create_primary_current_profile_package_dir_path(userid, pkgname), |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 1324 | /*ignore_if_missing*/ true); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1327 | binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { |
| 1328 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1329 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1330 | LOCK_PACKAGE(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1331 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1332 | binder::Status res = ok(); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 1333 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); |
| 1334 | for (auto user : users) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 1335 | if (destroy_app_current_profiles(packageName, user) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1336 | res = error("Failed to destroy current profiles for " + packageName); |
| 1337 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 1338 | } |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 1339 | if (destroy_app_reference_profile(packageName) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1340 | res = error("Failed to destroy reference profile for " + packageName); |
| 1341 | } |
| 1342 | return res; |
Calin Juravle | caa6b80 | 2016-03-22 14:15:52 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Jiakai Zhang | ee00e3b | 2022-03-24 15:03:04 +0000 | [diff] [blame] | 1345 | binder::Status InstalldNativeService::deleteReferenceProfile(const std::string& packageName, |
| 1346 | const std::string& profileName) { |
| 1347 | ENFORCE_UID(AID_SYSTEM); |
| 1348 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1349 | LOCK_PACKAGE(); |
| 1350 | |
| 1351 | // This function only supports primary dex'es. |
| 1352 | std::string path = |
| 1353 | create_reference_profile_path(packageName, profileName, /*is_secondary_dex=*/false); |
| 1354 | if (unlink(path.c_str()) != 0) { |
| 1355 | if (errno == ENOENT) { |
| 1356 | return ok(); |
| 1357 | } else { |
| 1358 | return error("Failed to delete profile " + profileName + " for " + packageName); |
| 1359 | } |
| 1360 | } |
| 1361 | return ok(); |
| 1362 | } |
| 1363 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1364 | binder::Status InstalldNativeService::destroyAppData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1365 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 1366 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1367 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1368 | CHECK_ARGUMENT_UUID(uuid); |
| 1369 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1370 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1371 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1372 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1373 | const char* pkgname = packageName.c_str(); |
| 1374 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1375 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1376 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1377 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 1378 | if (rename_delete_dir_contents_and_dir(path) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1379 | res = error("Failed to delete " + path); |
| 1380 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1381 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1382 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1383 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 1384 | if (rename_delete_dir_contents_and_dir(path) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1385 | res = error("Failed to delete " + path); |
| 1386 | } |
Alan Stokes | d983cc7 | 2020-02-14 14:36:58 +0000 | [diff] [blame] | 1387 | if ((flags & FLAG_CLEAR_APP_DATA_KEEP_ART_PROFILES) == 0) { |
| 1388 | destroy_app_current_profiles(packageName, userId); |
| 1389 | // TODO(calin): If the package is still installed by other users it's probably |
| 1390 | // beneficial to keep the reference profile around. |
| 1391 | // Verify if it's ok to do that. |
| 1392 | destroy_app_reference_profile(packageName); |
| 1393 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1394 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1395 | if (flags & FLAG_STORAGE_EXTERNAL) { |
| 1396 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 1397 | for (const auto& n : mStorageMounts) { |
| 1398 | auto extPath = n.second; |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
| 1401 | std::regex re("^\\/mnt\\/pass_through\\/[0-9]+\\/emulated"); |
| 1402 | if (std::regex_match(extPath, re)) { |
| 1403 | extPath += "/" + std::to_string(userId); |
| 1404 | } |
| 1405 | } else { |
| 1406 | if (n.first.compare(0, 14, "/mnt/media_rw/") != 0) { |
| 1407 | extPath += StringPrintf("/%d", userId); |
| 1408 | } else if (userId != 0) { |
| 1409 | // TODO: support devices mounted under secondary users |
| 1410 | continue; |
| 1411 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1412 | } |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 1413 | |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1414 | auto path = StringPrintf("%s/Android/data/%s", extPath.c_str(), pkgname); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 1415 | if (delete_dir_contents_and_dir(path, true) != 0) { |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1416 | res = error("Failed to delete contents of " + path); |
| 1417 | } |
| 1418 | path = StringPrintf("%s/Android/media/%s", extPath.c_str(), pkgname); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 1419 | if (delete_dir_contents_and_dir(path, true) != 0) { |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1420 | res = error("Failed to delete contents of " + path); |
| 1421 | } |
| 1422 | path = StringPrintf("%s/Android/obb/%s", extPath.c_str(), pkgname); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 1423 | if (delete_dir_contents_and_dir(path, true) != 0) { |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 1424 | res = error("Failed to delete contents of " + path); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 1428 | auto status = destroySdkSandboxDataPackageDirectory(uuid, packageName, userId, flags); |
| 1429 | if (!status.isOk()) { |
| 1430 | res = status; |
| 1431 | } |
| 1432 | return res; |
| 1433 | } |
| 1434 | |
| 1435 | binder::Status InstalldNativeService::destroySdkSandboxDataPackageDirectory( |
| 1436 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 1437 | int32_t flags) { |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1438 | ENFORCE_VALID_USER(userId); |
| 1439 | |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 1440 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1441 | const char* pkgname = packageName.c_str(); |
| 1442 | |
| 1443 | binder::Status res = ok(); |
| 1444 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 1445 | for (int i = 0; i < 2; i++) { |
| 1446 | int currentFlag = storageFlags[i]; |
| 1447 | if ((flags & currentFlag) == 0) { |
| 1448 | continue; |
| 1449 | } |
| 1450 | bool isCeData = (currentFlag == FLAG_STORAGE_CE); |
| 1451 | auto appPath = create_data_misc_sdk_sandbox_package_path(uuid_, isCeData, userId, pkgname); |
| 1452 | if (rename_delete_dir_contents_and_dir(appPath) != 0) { |
| 1453 | res = error("Failed to delete " + appPath); |
| 1454 | } |
| 1455 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1456 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1457 | } |
| 1458 | |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1459 | static gid_t get_cache_gid(uid_t uid) { |
| 1460 | int32_t gid = multiuser_get_cache_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid)); |
| 1461 | return (gid != -1) ? gid : uid; |
| 1462 | } |
| 1463 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1464 | binder::Status InstalldNativeService::fixupAppData(const std::optional<std::string>& uuid, |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1465 | int32_t flags) { |
| 1466 | ENFORCE_UID(AID_SYSTEM); |
| 1467 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1468 | |
| 1469 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1470 | for (auto userId : get_known_users(uuid_)) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1471 | LOCK_USER(); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 1472 | atrace_pm_begin("fixup user"); |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1473 | FTS* fts; |
| 1474 | FTSENT* p; |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1475 | auto ce_path = create_data_user_ce_path(uuid_, userId); |
| 1476 | auto de_path = create_data_user_de_path(uuid_, userId); |
Jeff Sharkey | d9ff77f | 2017-04-14 18:54:49 -0600 | [diff] [blame] | 1477 | char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1478 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1479 | return error("Failed to fts_open"); |
| 1480 | } |
| 1481 | while ((p = fts_read(fts)) != nullptr) { |
| 1482 | if (p->fts_info == FTS_D && p->fts_level == 1) { |
| 1483 | // Track down inodes of cache directories |
| 1484 | uint64_t raw = 0; |
| 1485 | ino_t inode_cache = 0; |
| 1486 | ino_t inode_code_cache = 0; |
| 1487 | if (getxattr(p->fts_path, kXattrInodeCache, &raw, sizeof(raw)) == sizeof(raw)) { |
| 1488 | inode_cache = raw; |
| 1489 | } |
| 1490 | if (getxattr(p->fts_path, kXattrInodeCodeCache, &raw, sizeof(raw)) == sizeof(raw)) { |
| 1491 | inode_code_cache = raw; |
| 1492 | } |
| 1493 | |
| 1494 | // Figure out expected GID of each child |
| 1495 | FTSENT* child = fts_children(fts, 0); |
| 1496 | while (child != nullptr) { |
| 1497 | if ((child->fts_statp->st_ino == inode_cache) |
| 1498 | || (child->fts_statp->st_ino == inode_code_cache) |
| 1499 | || !strcmp(child->fts_name, "cache") |
| 1500 | || !strcmp(child->fts_name, "code_cache")) { |
| 1501 | child->fts_number = get_cache_gid(p->fts_statp->st_uid); |
| 1502 | } else { |
| 1503 | child->fts_number = p->fts_statp->st_uid; |
| 1504 | } |
| 1505 | child = child->fts_link; |
| 1506 | } |
| 1507 | } else if (p->fts_level >= 2) { |
| 1508 | if (p->fts_level > 2) { |
| 1509 | // Inherit GID from parent once we're deeper into tree |
| 1510 | p->fts_number = p->fts_parent->fts_number; |
| 1511 | } |
| 1512 | |
| 1513 | uid_t uid = p->fts_parent->fts_statp->st_uid; |
| 1514 | gid_t cache_gid = get_cache_gid(uid); |
| 1515 | gid_t expected = p->fts_number; |
| 1516 | gid_t actual = p->fts_statp->st_gid; |
| 1517 | if (actual == expected) { |
| 1518 | #if FIXUP_DEBUG |
| 1519 | LOG(DEBUG) << "Ignoring " << p->fts_path << " with expected GID " << expected; |
| 1520 | #endif |
| 1521 | if (!(flags & FLAG_FORCE)) { |
| 1522 | fts_set(fts, p, FTS_SKIP); |
| 1523 | } |
| 1524 | } else if ((actual == uid) || (actual == cache_gid)) { |
| 1525 | // Only consider fixing up when current GID belongs to app |
| 1526 | if (p->fts_info != FTS_D) { |
| 1527 | LOG(INFO) << "Fixing " << p->fts_path << " with unexpected GID " << actual |
| 1528 | << " instead of " << expected; |
| 1529 | } |
| 1530 | switch (p->fts_info) { |
| 1531 | case FTS_DP: |
| 1532 | // If we're moving towards cache GID, we need to set S_ISGID |
| 1533 | if (expected == cache_gid) { |
| 1534 | if (chmod(p->fts_path, 02771) != 0) { |
| 1535 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 1536 | } |
| 1537 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 1538 | [[fallthrough]]; // also set GID |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1539 | case FTS_F: |
| 1540 | if (chown(p->fts_path, -1, expected) != 0) { |
| 1541 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1542 | } |
| 1543 | break; |
| 1544 | case FTS_SL: |
| 1545 | case FTS_SLNONE: |
| 1546 | if (lchown(p->fts_path, -1, expected) != 0) { |
| 1547 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 1548 | } |
| 1549 | break; |
| 1550 | } |
| 1551 | } else { |
| 1552 | // Ignore all other GID transitions, since they're kinda shady |
| 1553 | LOG(WARNING) << "Ignoring " << p->fts_path << " with unexpected GID " << actual |
| 1554 | << " instead of " << expected; |
Jeff Sharkey | 6b63b91 | 2017-09-21 15:41:37 -0600 | [diff] [blame] | 1555 | if (!(flags & FLAG_FORCE)) { |
| 1556 | fts_set(fts, p, FTS_SKIP); |
| 1557 | } |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | fts_close(fts); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 1562 | atrace_pm_end(); |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1563 | } |
| 1564 | return ok(); |
| 1565 | } |
| 1566 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1567 | static int32_t copy_directory_recursive(const char* from, const char* to) { |
Dario Freni | 918beeb | 2021-12-22 01:00:01 +0000 | [diff] [blame] | 1568 | char* argv[] = |
| 1569 | {(char*)kCpPath, |
| 1570 | (char*)"-F", /* delete any existing destination file first (--remove-destination) */ |
| 1571 | (char*)"--preserve=mode,ownership,timestamps,xattr", /* preserve properties */ |
| 1572 | (char*)"-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 1573 | (char*)"-P", /* Do not follow symlinks [default] */ |
| 1574 | (char*)"-d", /* don't dereference symlinks */ |
| 1575 | (char*)from, |
| 1576 | (char*)to}; |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1577 | |
| 1578 | LOG(DEBUG) << "Copying " << from << " to " << to; |
Tom Cherry | e79cde5 | 2019-09-25 16:51:56 -0700 | [diff] [blame] | 1579 | return logwrap_fork_execvp(ARRAY_SIZE(argv), argv, nullptr, false, LOG_ALOG, false, nullptr); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1582 | binder::Status InstalldNativeService::snapshotAppData(const std::optional<std::string>& volumeUuid, |
| 1583 | const std::string& packageName, |
| 1584 | int32_t userId, int32_t snapshotId, |
| 1585 | int32_t storageFlags, int64_t* _aidl_return) { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1586 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1587 | ENFORCE_VALID_USER(userId); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1588 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1589 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1590 | LOCK_PACKAGE_USER(); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1591 | |
| 1592 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1593 | const char* package_name = packageName.c_str(); |
| 1594 | |
| 1595 | binder::Status res = ok(); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1596 | // Default result to 0, it will be populated with inode of ce data snapshot |
| 1597 | // if FLAG_STORAGE_CE has been passed. |
| 1598 | if (_aidl_return != nullptr) *_aidl_return = 0; |
| 1599 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1600 | bool clear_ce_on_exit = false; |
| 1601 | bool clear_de_on_exit = false; |
| 1602 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1603 | auto deleter = [&clear_ce_on_exit, &clear_de_on_exit, &volume_uuid, &userId, &package_name, |
| 1604 | &snapshotId] { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1605 | if (clear_de_on_exit) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1606 | auto to = create_data_misc_de_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1607 | package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1608 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
| 1609 | LOG(WARNING) << "Failed to delete app data snapshot: " << to; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | if (clear_ce_on_exit) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1614 | auto to = create_data_misc_ce_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1615 | package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1616 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
| 1617 | LOG(WARNING) << "Failed to delete app data snapshot: " << to; |
| 1618 | } |
| 1619 | } |
| 1620 | }; |
| 1621 | |
| 1622 | auto scope_guard = android::base::make_scope_guard(deleter); |
| 1623 | |
JW Wang | 1d643c7 | 2020-10-06 13:42:41 +0800 | [diff] [blame] | 1624 | if (storageFlags & FLAG_STORAGE_DE) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1625 | auto from = create_data_user_de_package_path(volume_uuid, userId, package_name); |
| 1626 | auto to = create_data_misc_de_rollback_path(volume_uuid, userId, snapshotId); |
| 1627 | auto rollback_package_path = |
| 1628 | create_data_misc_de_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1629 | package_name); |
JW Wang | 1d643c7 | 2020-10-06 13:42:41 +0800 | [diff] [blame] | 1630 | |
| 1631 | int rc = create_dir_if_needed(to.c_str(), kRollbackFolderMode); |
| 1632 | if (rc != 0) { |
| 1633 | return error(rc, "Failed to create folder " + to); |
| 1634 | } |
| 1635 | |
| 1636 | rc = delete_dir_contents(rollback_package_path, true /* ignore_if_missing */); |
| 1637 | if (rc != 0) { |
| 1638 | return error(rc, "Failed clearing existing snapshot " + rollback_package_path); |
| 1639 | } |
| 1640 | |
| 1641 | // Check if we have data to copy. |
| 1642 | if (access(from.c_str(), F_OK) == 0) { |
| 1643 | rc = copy_directory_recursive(from.c_str(), to.c_str()); |
| 1644 | } |
| 1645 | if (rc != 0) { |
| 1646 | res = error(rc, "Failed copying " + from + " to " + to); |
| 1647 | clear_de_on_exit = true; |
| 1648 | return res; |
| 1649 | } |
| 1650 | } |
| 1651 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1652 | // The app may not have any data at all, in which case it's OK to skip here. |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1653 | auto from_ce = create_data_user_ce_package_path(volume_uuid, userId, package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1654 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 1655 | LOG(INFO) << "Missing source " << from_ce; |
| 1656 | return ok(); |
| 1657 | } |
| 1658 | |
Nikita Ioffe | 900b3c2 | 2019-01-23 14:32:48 +0000 | [diff] [blame] | 1659 | // ce_data_inode is not needed when FLAG_CLEAR_CACHE_ONLY is set. |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1660 | binder::Status clear_cache_result = |
| 1661 | clearAppData(volumeUuid, packageName, userId, storageFlags | FLAG_CLEAR_CACHE_ONLY, 0); |
Nikita Ioffe | 900b3c2 | 2019-01-23 14:32:48 +0000 | [diff] [blame] | 1662 | if (!clear_cache_result.isOk()) { |
| 1663 | // It should be fine to continue snapshot if we for some reason failed |
| 1664 | // to clear cache. |
| 1665 | LOG(WARNING) << "Failed to clear cache of app " << packageName; |
| 1666 | } |
| 1667 | |
| 1668 | // ce_data_inode is not needed when FLAG_CLEAR_CODE_CACHE_ONLY is set. |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1669 | binder::Status clear_code_cache_result = |
| 1670 | clearAppData(volumeUuid, packageName, userId, storageFlags | FLAG_CLEAR_CODE_CACHE_ONLY, |
| 1671 | 0); |
Nikita Ioffe | 900b3c2 | 2019-01-23 14:32:48 +0000 | [diff] [blame] | 1672 | if (!clear_code_cache_result.isOk()) { |
| 1673 | // It should be fine to continue snapshot if we for some reason failed |
| 1674 | // to clear code_cache. |
| 1675 | LOG(WARNING) << "Failed to clear code_cache of app " << packageName; |
| 1676 | } |
| 1677 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1678 | if (storageFlags & FLAG_STORAGE_CE) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1679 | auto from = create_data_user_ce_package_path(volume_uuid, userId, package_name); |
| 1680 | auto to = create_data_misc_ce_rollback_path(volume_uuid, userId, snapshotId); |
| 1681 | auto rollback_package_path = |
| 1682 | create_data_misc_ce_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1683 | package_name); |
Nikita Ioffe | 835ae77 | 2019-01-23 16:32:13 +0000 | [diff] [blame] | 1684 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1685 | int rc = create_dir_if_needed(to.c_str(), kRollbackFolderMode); |
| 1686 | if (rc != 0) { |
| 1687 | return error(rc, "Failed to create folder " + to); |
Nikita Ioffe | 835ae77 | 2019-01-23 16:32:13 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1690 | rc = delete_dir_contents(rollback_package_path, true /* ignore_if_missing */); |
| 1691 | if (rc != 0) { |
| 1692 | return error(rc, "Failed clearing existing snapshot " + rollback_package_path); |
| 1693 | } |
| 1694 | |
| 1695 | rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1696 | if (rc != 0) { |
| 1697 | res = error(rc, "Failed copying " + from + " to " + to); |
| 1698 | clear_ce_on_exit = true; |
| 1699 | return res; |
| 1700 | } |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1701 | if (_aidl_return != nullptr) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1702 | auto ce_snapshot_path = |
| 1703 | create_data_misc_ce_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1704 | package_name); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1705 | rc = get_path_inode(ce_snapshot_path, reinterpret_cast<ino_t*>(_aidl_return)); |
| 1706 | if (rc != 0) { |
| 1707 | res = error(rc, "Failed to get_path_inode for " + ce_snapshot_path); |
| 1708 | clear_ce_on_exit = true; |
| 1709 | return res; |
| 1710 | } |
| 1711 | } |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | return res; |
| 1715 | } |
| 1716 | |
| 1717 | binder::Status InstalldNativeService::restoreAppDataSnapshot( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1718 | const std::optional<std::string>& volumeUuid, const std::string& packageName, |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1719 | const int32_t appId, const std::string& seInfo, const int32_t userId, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1720 | const int32_t snapshotId, int32_t storageFlags) { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1721 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1722 | ENFORCE_VALID_USER(userId); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1723 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1724 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1725 | LOCK_PACKAGE_USER(); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1726 | |
| 1727 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1728 | const char* package_name = packageName.c_str(); |
| 1729 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1730 | auto from_ce = create_data_misc_ce_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1731 | package_name); |
| 1732 | auto from_de = create_data_misc_de_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1733 | package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1734 | |
| 1735 | const bool needs_ce_rollback = (storageFlags & FLAG_STORAGE_CE) && |
| 1736 | (access(from_ce.c_str(), F_OK) == 0); |
| 1737 | const bool needs_de_rollback = (storageFlags & FLAG_STORAGE_DE) && |
| 1738 | (access(from_de.c_str(), F_OK) == 0); |
| 1739 | |
| 1740 | if (!needs_ce_rollback && !needs_de_rollback) { |
| 1741 | return ok(); |
| 1742 | } |
| 1743 | |
| 1744 | // We know we're going to rollback one of the CE or DE data, so we clear |
| 1745 | // application data first. Note that it's possible that we're asked to |
| 1746 | // restore both CE & DE data but that one of the restores fail. Leaving the |
| 1747 | // app with no data in those cases is arguably better than leaving the app |
| 1748 | // with mismatched / stale data. |
| 1749 | LOG(INFO) << "Clearing app data for " << packageName << " to restore snapshot."; |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1750 | // It's fine to pass 0 as ceDataInode here, because restoreAppDataSnapshot |
| 1751 | // can only be called when user unlocks the phone, meaning that CE user data |
| 1752 | // is decrypted. |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1753 | binder::Status res = |
| 1754 | clearAppData(volumeUuid, packageName, userId, storageFlags, 0 /* ceDataInode */); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1755 | if (!res.isOk()) { |
| 1756 | return res; |
| 1757 | } |
| 1758 | |
| 1759 | if (needs_ce_rollback) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1760 | auto to_ce = create_data_user_ce_path(volume_uuid, userId); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1761 | int rc = copy_directory_recursive(from_ce.c_str(), to_ce.c_str()); |
| 1762 | if (rc != 0) { |
| 1763 | res = error(rc, "Failed copying " + from_ce + " to " + to_ce); |
| 1764 | return res; |
| 1765 | } |
JW Wang | fda19ec | 2020-05-18 16:54:08 +0800 | [diff] [blame] | 1766 | delete_dir_contents_and_dir(from_ce, true /* ignore_if_missing */); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | if (needs_de_rollback) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1770 | auto to_de = create_data_user_de_path(volume_uuid, userId); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1771 | int rc = copy_directory_recursive(from_de.c_str(), to_de.c_str()); |
| 1772 | if (rc != 0) { |
Nikita Ioffe | fd2ccd4 | 2019-01-25 11:44:36 +0000 | [diff] [blame] | 1773 | if (needs_ce_rollback) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1774 | auto ce_data = create_data_user_ce_package_path(volume_uuid, userId, package_name); |
Nikita Ioffe | fd2ccd4 | 2019-01-25 11:44:36 +0000 | [diff] [blame] | 1775 | LOG(WARNING) << "de_data rollback failed. Erasing rolled back ce_data " << ce_data; |
| 1776 | if (delete_dir_contents(ce_data.c_str(), 1, nullptr) != 0) { |
| 1777 | LOG(WARNING) << "Failed to delete rolled back ce_data " << ce_data; |
| 1778 | } |
| 1779 | } |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1780 | res = error(rc, "Failed copying " + from_de + " to " + to_de); |
| 1781 | return res; |
| 1782 | } |
JW Wang | fda19ec | 2020-05-18 16:54:08 +0800 | [diff] [blame] | 1783 | delete_dir_contents_and_dir(from_de, true /* ignore_if_missing */); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | // Finally, restore the SELinux label on the app data. |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1787 | return restoreconAppData(volumeUuid, packageName, userId, storageFlags, appId, seInfo); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1790 | binder::Status InstalldNativeService::destroyAppDataSnapshot( |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1791 | const std::optional<std::string>& volumeUuid, const std::string& packageName, |
| 1792 | const int32_t userId, const int64_t ceSnapshotInode, const int32_t snapshotId, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1793 | int32_t storageFlags) { |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1794 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1795 | ENFORCE_VALID_USER(userId); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1796 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
| 1797 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1798 | LOCK_PACKAGE_USER(); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1799 | |
| 1800 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1801 | const char* package_name = packageName.c_str(); |
| 1802 | |
| 1803 | if (storageFlags & FLAG_STORAGE_DE) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1804 | auto de_snapshot_path = create_data_misc_de_rollback_package_path(volume_uuid, userId, |
| 1805 | snapshotId, package_name); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1806 | |
| 1807 | int res = delete_dir_contents_and_dir(de_snapshot_path, true /* ignore_if_missing */); |
| 1808 | if (res != 0) { |
| 1809 | return error(res, "Failed clearing snapshot " + de_snapshot_path); |
| 1810 | } |
| 1811 | } |
| 1812 | |
| 1813 | if (storageFlags & FLAG_STORAGE_CE) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1814 | auto ce_snapshot_path = |
| 1815 | create_data_misc_ce_rollback_package_path(volume_uuid, userId, snapshotId, |
| 1816 | package_name, ceSnapshotInode); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1817 | int res = delete_dir_contents_and_dir(ce_snapshot_path, true /* ignore_if_missing */); |
| 1818 | if (res != 0) { |
| 1819 | return error(res, "Failed clearing snapshot " + ce_snapshot_path); |
| 1820 | } |
| 1821 | } |
| 1822 | return ok(); |
| 1823 | } |
| 1824 | |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1825 | binder::Status InstalldNativeService::destroyCeSnapshotsNotSpecified( |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1826 | const std::optional<std::string>& volumeUuid, const int32_t userId, |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1827 | const std::vector<int32_t>& retainSnapshotIds) { |
| 1828 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 1829 | ENFORCE_VALID_USER(userId); |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1830 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1831 | LOCK_USER(); |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1832 | |
| 1833 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1834 | |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1835 | auto base_path = create_data_misc_ce_rollback_base_path(volume_uuid, userId); |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1836 | |
| 1837 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(base_path.c_str()), closedir); |
| 1838 | if (!dir) { |
| 1839 | return error(-1, "Failed to open rollback base dir " + base_path); |
| 1840 | } |
| 1841 | |
| 1842 | struct dirent* ent; |
| 1843 | while ((ent = readdir(dir.get()))) { |
| 1844 | if (ent->d_type != DT_DIR) { |
| 1845 | continue; |
| 1846 | } |
| 1847 | |
| 1848 | uint snapshot_id; |
| 1849 | bool parse_ok = ParseUint(ent->d_name, &snapshot_id); |
| 1850 | if (parse_ok && |
| 1851 | std::find(retainSnapshotIds.begin(), retainSnapshotIds.end(), |
| 1852 | snapshot_id) == retainSnapshotIds.end()) { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1853 | auto rollback_path = |
| 1854 | create_data_misc_ce_rollback_path(volume_uuid, userId, snapshot_id); |
Oli Lan | c778957 | 2020-03-16 18:18:41 +0000 | [diff] [blame] | 1855 | int res = delete_dir_contents_and_dir(rollback_path, true /* ignore_if_missing */); |
| 1856 | if (res != 0) { |
| 1857 | return error(res, "Failed clearing snapshot " + rollback_path); |
| 1858 | } |
| 1859 | } |
| 1860 | } |
| 1861 | return ok(); |
| 1862 | } |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1863 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 1864 | binder::Status InstalldNativeService::moveCompleteApp(const std::optional<std::string>& fromUuid, |
| 1865 | const std::optional<std::string>& toUuid, const std::string& packageName, |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1866 | int32_t appId, const std::string& seInfo, |
Songchun Fan | aed3337 | 2020-02-05 12:02:22 -0800 | [diff] [blame] | 1867 | int32_t targetSdkVersion, const std::string& fromCodePath) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1868 | ENFORCE_UID(AID_SYSTEM); |
| 1869 | CHECK_ARGUMENT_UUID(fromUuid); |
| 1870 | CHECK_ARGUMENT_UUID(toUuid); |
| 1871 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1872 | LOCK_PACKAGE(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1873 | |
| 1874 | const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; |
| 1875 | const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; |
| 1876 | const char* package_name = packageName.c_str(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1877 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1878 | binder::Status res = ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1879 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 1880 | |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1881 | auto to_app_package_path_parent = create_data_app_path(to_uuid); |
| 1882 | auto to_app_package_path = StringPrintf("%s/%s", to_app_package_path_parent.c_str(), |
| 1883 | android::base::Basename(fromCodePath).c_str()); |
| 1884 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1885 | // Copy app |
| 1886 | { |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1887 | int rc = copy_directory_recursive(fromCodePath.c_str(), to_app_package_path_parent.c_str()); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1888 | if (rc != 0) { |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1889 | res = error(rc, "Failed copying " + fromCodePath + " to " + to_app_package_path); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1890 | goto fail; |
| 1891 | } |
| 1892 | |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1893 | if (selinux_android_restorecon(to_app_package_path.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
| 1894 | res = error("Failed to restorecon " + to_app_package_path); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1895 | goto fail; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | // Copy private data for all known users |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1900 | for (auto userId : users) { |
| 1901 | LOCK_USER(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1902 | |
| 1903 | // Data source may not exist for all users; that's okay |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1904 | auto from_ce = create_data_user_ce_package_path(from_uuid, userId, package_name); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1905 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 1906 | LOG(INFO) << "Missing source " << from_ce; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1907 | continue; |
| 1908 | } |
| 1909 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 1910 | if (!createAppDataLocked(toUuid, packageName, userId, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
Songchun Fan | cb567f9 | 2023-09-11 17:04:47 +0000 | [diff] [blame] | 1911 | appId, /* previousAppId */ -1, seInfo, targetSdkVersion, nullptr, |
| 1912 | nullptr) |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1913 | .isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1914 | res = error("Failed to create package target"); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1915 | goto fail; |
| 1916 | } |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1917 | { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1918 | auto from = create_data_user_de_package_path(from_uuid, userId, package_name); |
| 1919 | auto to = create_data_user_de_path(to_uuid, userId); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1920 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1921 | int rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1922 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1923 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1924 | goto fail; |
| 1925 | } |
| 1926 | } |
| 1927 | { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1928 | auto from = create_data_user_ce_package_path(from_uuid, userId, package_name); |
| 1929 | auto to = create_data_user_ce_path(to_uuid, userId); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1930 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1931 | int rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1932 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1933 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1934 | goto fail; |
| 1935 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 1938 | if (!restoreconAppDataLocked(toUuid, packageName, userId, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
| 1939 | appId, seInfo) |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1940 | .isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1941 | res = error("Failed to restorecon"); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1942 | goto fail; |
| 1943 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 1946 | // Copy sdk data for all known users |
| 1947 | for (auto userId : users) { |
| 1948 | LOCK_USER(); |
| 1949 | |
| 1950 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 1951 | for (int currentFlag : storageFlags) { |
| 1952 | const bool isCeData = currentFlag == FLAG_STORAGE_CE; |
| 1953 | |
| 1954 | const auto from = create_data_misc_sdk_sandbox_package_path(from_uuid, isCeData, userId, |
| 1955 | package_name); |
| 1956 | if (access(from.c_str(), F_OK) != 0) { |
| 1957 | LOG(INFO) << "Missing source " << from; |
| 1958 | continue; |
| 1959 | } |
| 1960 | const auto to = create_data_misc_sdk_sandbox_path(to_uuid, isCeData, userId); |
| 1961 | |
| 1962 | const int rc = copy_directory_recursive(from.c_str(), to.c_str()); |
| 1963 | if (rc != 0) { |
| 1964 | res = error(rc, "Failed copying " + from + " to " + to); |
| 1965 | goto fail; |
| 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | if (!restoreconSdkDataLocked(toUuid, packageName, userId, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
| 1970 | appId, seInfo) |
| 1971 | .isOk()) { |
| 1972 | res = error("Failed to restorecon"); |
| 1973 | goto fail; |
| 1974 | } |
| 1975 | } |
Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 1976 | // We let the framework scan the new location and persist that before |
| 1977 | // deleting the data in the old location; this ordering ensures that |
| 1978 | // we can recover from things like battery pulls. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1979 | return ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1980 | |
| 1981 | fail: |
| 1982 | // Nuke everything we might have already copied |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1983 | { |
Songchun Fan | dc2083f | 2020-02-18 17:45:46 -0800 | [diff] [blame] | 1984 | if (delete_dir_contents(to_app_package_path.c_str(), 1, nullptr) != 0) { |
| 1985 | LOG(WARNING) << "Failed to rollback " << to_app_package_path; |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1986 | } |
| 1987 | } |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1988 | for (auto userId : users) { |
| 1989 | LOCK_USER(); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1990 | { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1991 | auto to = create_data_user_de_package_path(to_uuid, userId, package_name); |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1992 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1993 | LOG(WARNING) << "Failed to rollback " << to; |
| 1994 | } |
| 1995 | } |
| 1996 | { |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 1997 | auto to = create_data_user_ce_package_path(to_uuid, userId, package_name); |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1998 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1999 | LOG(WARNING) << "Failed to rollback " << to; |
| 2000 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 2001 | } |
| 2002 | } |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 2003 | for (auto userId : users) { |
| 2004 | LOCK_USER(); |
| 2005 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 2006 | for (int currentFlag : storageFlags) { |
| 2007 | const bool isCeData = currentFlag == FLAG_STORAGE_CE; |
| 2008 | const auto to = create_data_misc_sdk_sandbox_package_path(to_uuid, isCeData, userId, |
| 2009 | package_name); |
| 2010 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
| 2011 | LOG(WARNING) << "Failed to rollback " << to; |
| 2012 | } |
| 2013 | } |
| 2014 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2015 | return res; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2018 | binder::Status InstalldNativeService::createUserData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2019 | int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { |
| 2020 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 2021 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2022 | CHECK_ARGUMENT_UUID(uuid); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 2023 | LOCK_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2024 | |
Alex Buynytskyy | a203d71 | 2023-06-08 12:56:15 -0700 | [diff] [blame] | 2025 | ScopedTrace tracer("create-user-data"); |
| 2026 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2027 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 2028 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2029 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2030 | if (ensure_config_user_dirs(userId) != 0) { |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 2031 | return error(StringPrintf("Failed to ensure dirs for %d", userId)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2032 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2033 | } |
| 2034 | } |
Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 2035 | |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 2036 | return ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2039 | binder::Status InstalldNativeService::destroyUserData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2040 | int32_t userId, int32_t flags) { |
| 2041 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 2042 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2043 | CHECK_ARGUMENT_UUID(uuid); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 2044 | LOCK_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2045 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2046 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2047 | binder::Status res = ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2048 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2049 | auto path = create_data_user_de_path(uuid_, userId); |
Eric Biggers | c3d9dce | 2022-04-28 04:42:45 +0000 | [diff] [blame] | 2050 | // Contents only, as vold is responsible for the user_de dir itself. |
| 2051 | if (delete_dir_contents(path, true) != 0) { |
| 2052 | res = error("Failed to delete contents of " + path); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2053 | } |
Rishabh Singh | 8d78437 | 2022-03-09 15:54:46 +0000 | [diff] [blame] | 2054 | auto sdk_sandbox_de_path = |
| 2055 | create_data_misc_sdk_sandbox_path(uuid_, /*isCeData=*/false, userId); |
| 2056 | if (delete_dir_contents_and_dir(sdk_sandbox_de_path, true) != 0) { |
| 2057 | res = error("Failed to delete " + sdk_sandbox_de_path); |
| 2058 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2059 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2060 | path = create_data_misc_legacy_path(userId); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 2061 | if (delete_dir_contents_and_dir(path, true) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2062 | res = error("Failed to delete " + path); |
| 2063 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 2064 | path = create_primary_cur_profile_dir_path(userId); |
Alex Buynytskyy | 5cb2526 | 2022-02-15 08:59:36 -0800 | [diff] [blame] | 2065 | if (delete_dir_contents_and_dir(path, true) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2066 | res = error("Failed to delete " + path); |
| 2067 | } |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 2068 | } |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 2069 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 2070 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2071 | auto path = create_data_user_ce_path(uuid_, userId); |
Eric Biggers | c3d9dce | 2022-04-28 04:42:45 +0000 | [diff] [blame] | 2072 | // Contents only, as vold is responsible for the user_ce dir itself. |
| 2073 | if (delete_dir_contents(path, true) != 0) { |
| 2074 | res = error("Failed to delete contents of " + path); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2075 | } |
Rishabh Singh | 8d78437 | 2022-03-09 15:54:46 +0000 | [diff] [blame] | 2076 | auto sdk_sandbox_ce_path = |
| 2077 | create_data_misc_sdk_sandbox_path(uuid_, /*isCeData=*/true, userId); |
| 2078 | if (delete_dir_contents_and_dir(sdk_sandbox_ce_path, true) != 0) { |
| 2079 | res = error("Failed to delete " + sdk_sandbox_ce_path); |
| 2080 | } |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2081 | path = findDataMediaPath(uuid, userId); |
Eric Biggers | c3d9dce | 2022-04-28 04:42:45 +0000 | [diff] [blame] | 2082 | // Contents only, as vold is responsible for the media dir itself. |
| 2083 | if (delete_dir_contents(path, true) != 0) { |
| 2084 | res = error("Failed to delete contents of " + path); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2085 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 2086 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2087 | return res; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2090 | binder::Status InstalldNativeService::freeCache(const std::optional<std::string>& uuid, |
Neharika Jali | 0e54231 | 2021-10-25 06:41:24 +0000 | [diff] [blame] | 2091 | int64_t targetFreeBytes, int32_t flags) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2092 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2093 | CHECK_ARGUMENT_UUID(uuid); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 2094 | #ifndef GRANULAR_LOCKS |
| 2095 | std::lock_guard lock(mLock); |
| 2096 | #endif // !GRANULAR_LOCKS |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2097 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2098 | auto uuidString = uuid.value_or(""); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2099 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2100 | auto data_path = create_data_path(uuid_); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2101 | auto noop = (flags & FLAG_FREE_CACHE_NOOP); |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2102 | auto defy_target = (flags & FLAG_FREE_CACHE_DEFY_TARGET_FREE_BYTES); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 2103 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2104 | int64_t free = data_disk_free(data_path); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2105 | if (free < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2106 | return error("Failed to determine free space for " + data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2107 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2108 | |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 2109 | int64_t needed = targetFreeBytes - free; |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2110 | if (!defy_target) { |
| 2111 | LOG(DEBUG) << "Device " << data_path << " has " << free << " free; requested " |
| 2112 | << targetFreeBytes << "; needed " << needed; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 2113 | |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2114 | if (free >= targetFreeBytes) { |
| 2115 | return ok(); |
| 2116 | } |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 2117 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2118 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2119 | if (flags & FLAG_FREE_CACHE_V2) { |
| 2120 | // This new cache strategy fairly removes files from UIDs by deleting |
| 2121 | // files from the UIDs which are most over their allocated quota |
| 2122 | |
| 2123 | // 1. Create trackers for every known UID |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2124 | atrace_pm_begin("create"); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 2125 | const auto users = get_known_users(uuid_); |
| 2126 | #ifdef GRANULAR_LOCKS |
| 2127 | std::vector<UserLock> userLocks; |
| 2128 | userLocks.reserve(users.size()); |
| 2129 | std::vector<UserWriteLockGuard> lockGuards; |
| 2130 | lockGuards.reserve(users.size()); |
| 2131 | #endif // GRANULAR_LOCKS |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2132 | std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers; |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 2133 | for (auto userId : users) { |
| 2134 | #ifdef GRANULAR_LOCKS |
| 2135 | userLocks.emplace_back(userId, mUserIdLock, mLock); |
| 2136 | lockGuards.emplace_back(userLocks.back()); |
| 2137 | #endif // GRANULAR_LOCKS |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2138 | FTS *fts; |
| 2139 | FTSENT *p; |
Samiul Islam | a219809 | 2022-05-07 23:22:45 +0100 | [diff] [blame] | 2140 | |
| 2141 | // Create a list of data paths whose children have cache directories |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 2142 | auto ce_path = create_data_user_ce_path(uuid_, userId); |
| 2143 | auto de_path = create_data_user_de_path(uuid_, userId); |
| 2144 | auto media_path = findDataMediaPath(uuid, userId) + "/Android/data/"; |
Samiul Islam | a219809 | 2022-05-07 23:22:45 +0100 | [diff] [blame] | 2145 | auto ce_sdk_path = create_data_misc_sdk_sandbox_path(uuid_, /*isCeData=*/true, userId); |
| 2146 | auto de_sdk_path = create_data_misc_sdk_sandbox_path(uuid_, /*isCeData=*/false, userId); |
| 2147 | |
| 2148 | std::vector<std::string> dataPaths = {ce_path, de_path, media_path}; |
| 2149 | foreach_subdir(ce_sdk_path, [&ce_sdk_path, &dataPaths](const std::string subDir) { |
| 2150 | const auto fullpath = ce_sdk_path + "/" + subDir; |
| 2151 | dataPaths.push_back(fullpath); |
| 2152 | }); |
| 2153 | foreach_subdir(de_sdk_path, [&de_sdk_path, &dataPaths](const std::string subDir) { |
| 2154 | const auto fullpath = de_sdk_path + "/" + subDir; |
| 2155 | dataPaths.push_back((char*)fullpath.c_str()); |
| 2156 | }); |
| 2157 | |
| 2158 | char* argv[dataPaths.size() + 1]; |
| 2159 | for (unsigned int i = 0; i < dataPaths.size(); i++) { |
| 2160 | argv[i] = (char*)dataPaths[i].c_str(); |
| 2161 | } |
| 2162 | argv[dataPaths.size()] = nullptr; |
| 2163 | |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2164 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2165 | return error("Failed to fts_open"); |
| 2166 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2167 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2168 | if (p->fts_info == FTS_D && p->fts_level == 1) { |
| 2169 | uid_t uid = p->fts_statp->st_uid; |
Samiul Islam | a219809 | 2022-05-07 23:22:45 +0100 | [diff] [blame] | 2170 | |
| 2171 | // If uid belongs to sdk sandbox, then the cache should be attributed to the |
| 2172 | // original client app. |
| 2173 | const auto client_uid = multiuser_convert_sdk_sandbox_to_app_uid(uid); |
| 2174 | const bool isSandboxUid = (client_uid != (uid_t)-1); |
| 2175 | if (isSandboxUid) uid = client_uid; |
| 2176 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2177 | if (multiuser_get_app_id(uid) == AID_MEDIA_RW) { |
| 2178 | uid = (multiuser_get_app_id(p->fts_statp->st_gid) - AID_EXT_GID_START) |
| 2179 | + AID_APP_START; |
| 2180 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2181 | auto search = trackers.find(uid); |
| 2182 | if (search != trackers.end()) { |
| 2183 | search->second->addDataPath(p->fts_path); |
| 2184 | } else { |
| 2185 | auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker( |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2186 | multiuser_get_user_id(uid), multiuser_get_app_id(uid), uuidString)); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2187 | tracker->addDataPath(p->fts_path); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 2188 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2189 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 2190 | tracker->cacheQuota = mCacheQuotas[uid]; |
| 2191 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2192 | if (tracker->cacheQuota == 0) { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 2193 | #if MEASURE_DEBUG |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2194 | LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB"; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 2195 | #endif |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2196 | tracker->cacheQuota = 67108864; |
| 2197 | } |
| 2198 | trackers[uid] = tracker; |
| 2199 | } |
| 2200 | fts_set(fts, p, FTS_SKIP); |
| 2201 | } |
| 2202 | } |
| 2203 | fts_close(fts); |
| 2204 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2205 | atrace_pm_end(); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2206 | |
| 2207 | // 2. Populate tracker stats and insert into priority queue |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2208 | atrace_pm_begin("populate"); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2209 | auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) { |
| 2210 | return (left->getCacheRatio() < right->getCacheRatio()); |
| 2211 | }; |
| 2212 | std::priority_queue<std::shared_ptr<CacheTracker>, |
| 2213 | std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp); |
| 2214 | for (const auto& it : trackers) { |
| 2215 | it.second->loadStats(); |
| 2216 | queue.push(it.second); |
| 2217 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2218 | atrace_pm_end(); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2219 | |
| 2220 | // 3. Bounce across the queue, freeing items from whichever tracker is |
| 2221 | // the most over their assigned quota |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2222 | atrace_pm_begin("bounce"); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2223 | std::shared_ptr<CacheTracker> active; |
| 2224 | while (active || !queue.empty()) { |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 2225 | // Only look at apps under quota when explicitly requested |
| 2226 | if (active && (active->getCacheRatio() < 10000) |
| 2227 | && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) { |
| 2228 | LOG(DEBUG) << "Active ratio " << active->getCacheRatio() |
| 2229 | << " isn't over quota, and defy not requested"; |
| 2230 | break; |
| 2231 | } |
| 2232 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2233 | // Find the best tracker to work with; this might involve swapping |
| 2234 | // if the active tracker is no longer the most over quota |
| 2235 | bool nextBetter = active && !queue.empty() |
| 2236 | && active->getCacheRatio() < queue.top()->getCacheRatio(); |
| 2237 | if (!active || nextBetter) { |
| 2238 | if (active) { |
| 2239 | // Current tracker still has items, so we'll consider it |
| 2240 | // again later once it bubbles up to surface |
| 2241 | queue.push(active); |
| 2242 | } |
| 2243 | active = queue.top(); queue.pop(); |
| 2244 | active->ensureItems(); |
| 2245 | continue; |
| 2246 | } |
| 2247 | |
| 2248 | // If no items remain, go find another tracker |
| 2249 | if (active->items.empty()) { |
| 2250 | active = nullptr; |
| 2251 | continue; |
| 2252 | } else { |
| 2253 | auto item = active->items.back(); |
| 2254 | active->items.pop_back(); |
| 2255 | |
| 2256 | LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString(); |
| 2257 | if (!noop) { |
| 2258 | item->purge(); |
| 2259 | } |
| 2260 | active->cacheUsed -= item->size; |
| 2261 | needed -= item->size; |
| 2262 | } |
| 2263 | |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2264 | if (!defy_target) { |
| 2265 | // Verify that we're actually done before bailing, since sneaky |
| 2266 | // apps might be using hardlinks |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2267 | if (needed <= 0) { |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2268 | free = data_disk_free(data_path); |
| 2269 | needed = targetFreeBytes - free; |
| 2270 | if (needed <= 0) { |
| 2271 | break; |
| 2272 | } else { |
| 2273 | LOG(WARNING) << "Expected to be done but still need " << needed; |
| 2274 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2275 | } |
| 2276 | } |
| 2277 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2278 | atrace_pm_end(); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2279 | |
| 2280 | } else { |
Jeff Sharkey | 1cb4aaf | 2017-03-27 16:41:06 -0600 | [diff] [blame] | 2281 | return error("Legacy cache logic no longer supported"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2284 | if (!defy_target) { |
| 2285 | free = data_disk_free(data_path); |
| 2286 | if (free >= targetFreeBytes) { |
| 2287 | return ok(); |
| 2288 | } else { |
| 2289 | return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, |
| 2290 | targetFreeBytes, data_path.c_str(), free)); |
| 2291 | } |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2292 | } else { |
Neharika Jali | 1d667a2 | 2021-10-25 19:36:18 +0000 | [diff] [blame] | 2293 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2294 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2295 | } |
| 2296 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2297 | binder::Status InstalldNativeService::rmdex(const std::string& codePath, |
| 2298 | const std::string& instructionSet) { |
| 2299 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2300 | CHECK_ARGUMENT_PATH(codePath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2301 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2302 | char dex_path[PKG_PATH_MAX]; |
| 2303 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2304 | const char* path = codePath.c_str(); |
| 2305 | const char* instruction_set = instructionSet.c_str(); |
| 2306 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 2307 | if (validate_apk_path(path) && validate_system_app_path(path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2308 | return error("Invalid path " + codePath); |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2311 | if (!create_cache_path(dex_path, path, instruction_set)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2312 | return error("Failed to create cache path for " + codePath); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2313 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2314 | |
| 2315 | ALOGV("unlink %s\n", dex_path); |
| 2316 | if (unlink(dex_path) < 0) { |
Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 2317 | // It's ok if we don't have a dalvik cache path. Report error only when the path exists |
| 2318 | // but could not be unlinked. |
| 2319 | if (errno != ENOENT) { |
| 2320 | return error(StringPrintf("Failed to unlink %s", dex_path)); |
| 2321 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2322 | } |
Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 2323 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2326 | struct stats { |
| 2327 | int64_t codeSize; |
| 2328 | int64_t dataSize; |
| 2329 | int64_t cacheSize; |
| 2330 | }; |
| 2331 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2332 | #if MEASURE_DEBUG |
| 2333 | static std::string toString(std::vector<int64_t> values) { |
| 2334 | std::stringstream res; |
| 2335 | res << "["; |
| 2336 | for (size_t i = 0; i < values.size(); i++) { |
| 2337 | res << values[i]; |
| 2338 | if (i < values.size() - 1) { |
| 2339 | res << ","; |
| 2340 | } |
| 2341 | } |
| 2342 | res << "]"; |
| 2343 | return res.str(); |
| 2344 | } |
| 2345 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2346 | |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2347 | // On devices without sdcardfs, if internal and external are on |
| 2348 | // the same volume, a uid such as u0_a123 is used for both |
| 2349 | // internal and external storage; therefore, subtract that |
| 2350 | // amount from internal to make sure we don't count it double. |
| 2351 | // This needs to happen for data, cache and OBB |
| 2352 | static void deductDoubleSpaceIfNeeded(stats* stats, int64_t doubleSpaceToBeDeleted, uid_t uid, |
| 2353 | const std::string& uuid) { |
| 2354 | if (!supports_sdcardfs()) { |
| 2355 | stats->dataSize -= doubleSpaceToBeDeleted; |
| 2356 | long obbProjectId = get_project_id(uid, PROJECT_ID_EXT_OBB_START); |
| 2357 | int64_t appObbSize = GetOccupiedSpaceForProjectId(uuid, obbProjectId); |
| 2358 | stats->dataSize -= appObbSize; |
| 2359 | } |
| 2360 | } |
| 2361 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2362 | static void collectQuotaStats(const std::string& uuid, int32_t userId, |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2363 | int32_t appId, struct stats* stats, struct stats* extStats) { |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2364 | int64_t space, doubleSpaceToBeDeleted = 0; |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 2365 | uid_t uid = multiuser_get_uid(userId, appId); |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2366 | static const bool supportsProjectId = internal_storage_has_project_id(); |
| 2367 | |
| 2368 | if (extStats != nullptr) { |
| 2369 | space = get_occupied_app_space_external(uuid, userId, appId); |
| 2370 | |
| 2371 | if (space != -1) { |
| 2372 | extStats->dataSize += space; |
| 2373 | doubleSpaceToBeDeleted += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2374 | } |
| 2375 | |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2376 | space = get_occupied_app_cache_space_external(uuid, userId, appId); |
| 2377 | if (space != -1) { |
| 2378 | extStats->dataSize += space; // cache counts for "data" |
| 2379 | extStats->cacheSize += space; |
| 2380 | doubleSpaceToBeDeleted += space; |
| 2381 | } |
| 2382 | } |
| 2383 | |
| 2384 | if (stats != nullptr) { |
| 2385 | if (!supportsProjectId) { |
| 2386 | if ((space = GetOccupiedSpaceForUid(uuid, uid)) != -1) { |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2387 | stats->dataSize += space; |
| 2388 | } |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2389 | deductDoubleSpaceIfNeeded(stats, doubleSpaceToBeDeleted, uid, uuid); |
| 2390 | int sdkSandboxUid = multiuser_get_sdk_sandbox_uid(userId, appId); |
| 2391 | if (sdkSandboxUid != -1) { |
| 2392 | if ((space = GetOccupiedSpaceForUid(uuid, sdkSandboxUid)) != -1) { |
| 2393 | stats->dataSize += space; |
| 2394 | } |
| 2395 | } |
| 2396 | int cacheGid = multiuser_get_cache_gid(userId, appId); |
| 2397 | if (cacheGid != -1) { |
| 2398 | if ((space = GetOccupiedSpaceForGid(uuid, cacheGid)) != -1) { |
| 2399 | stats->cacheSize += space; |
| 2400 | } |
| 2401 | } |
| 2402 | } else { |
| 2403 | long projectId = get_project_id(uid, PROJECT_ID_APP_START); |
| 2404 | if ((space = GetOccupiedSpaceForProjectId(uuid, projectId)) != -1) { |
| 2405 | stats->dataSize += space; |
| 2406 | } |
| 2407 | projectId = get_project_id(uid, PROJECT_ID_APP_CACHE_START); |
| 2408 | if ((space = GetOccupiedSpaceForProjectId(uuid, projectId)) != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2409 | stats->cacheSize += space; |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2410 | stats->dataSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2411 | } |
| 2412 | } |
| 2413 | |
| 2414 | int sharedGid = multiuser_get_shared_gid(0, appId); |
| 2415 | if (sharedGid != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2416 | if ((space = GetOccupiedSpaceForGid(uuid, sharedGid)) != -1) { |
| 2417 | stats->codeSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2418 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2419 | } |
| 2420 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2421 | } |
| 2422 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2423 | static void collectManualStats(const std::string& path, struct stats* stats) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2424 | DIR *d; |
| 2425 | int dfd; |
| 2426 | struct dirent *de; |
| 2427 | struct stat s; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2428 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2429 | d = opendir(path.c_str()); |
| 2430 | if (d == nullptr) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2431 | if (errno != ENOENT) { |
| 2432 | PLOG(WARNING) << "Failed to open " << path; |
| 2433 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2434 | return; |
| 2435 | } |
| 2436 | dfd = dirfd(d); |
| 2437 | while ((de = readdir(d))) { |
| 2438 | const char *name = de->d_name; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2439 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2440 | int64_t size = 0; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2441 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2442 | size = s.st_blocks * 512; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2443 | } |
| 2444 | |
| 2445 | if (de->d_type == DT_DIR) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2446 | if (!strcmp(name, ".")) { |
| 2447 | // Don't recurse, but still count node size |
| 2448 | } else if (!strcmp(name, "..")) { |
| 2449 | // Don't recurse or count node size |
| 2450 | continue; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2451 | } else { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2452 | // Measure all children nodes |
| 2453 | size = 0; |
| 2454 | calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2455 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2456 | |
| 2457 | if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { |
| 2458 | stats->cacheSize += size; |
| 2459 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2460 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2461 | |
| 2462 | // Legacy symlink isn't owned by app |
| 2463 | if (de->d_type == DT_LNK && !strcmp(name, "lib")) { |
| 2464 | continue; |
| 2465 | } |
| 2466 | |
| 2467 | // Everything found inside is considered data |
| 2468 | stats->dataSize += size; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2469 | } |
| 2470 | closedir(d); |
| 2471 | } |
| 2472 | |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2473 | void collectManualStatsForSubDirectories(const std::string& path, struct stats* stats) { |
| 2474 | const auto subDirHandler = [&path, &stats](const std::string& subDir) { |
| 2475 | auto fullpath = path + "/" + subDir; |
| 2476 | collectManualStats(fullpath, stats); |
| 2477 | }; |
| 2478 | foreach_subdir(path, subDirHandler); |
| 2479 | } |
| 2480 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2481 | static void collectManualStatsForUser(const std::string& path, struct stats* stats, |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2482 | bool exclude_apps = false, |
| 2483 | bool is_sdk_sandbox_storage = false) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2484 | DIR *d; |
| 2485 | int dfd; |
| 2486 | struct dirent *de; |
| 2487 | struct stat s; |
| 2488 | |
| 2489 | d = opendir(path.c_str()); |
| 2490 | if (d == nullptr) { |
| 2491 | if (errno != ENOENT) { |
| 2492 | PLOG(WARNING) << "Failed to open " << path; |
| 2493 | } |
| 2494 | return; |
| 2495 | } |
| 2496 | dfd = dirfd(d); |
| 2497 | while ((de = readdir(d))) { |
| 2498 | if (de->d_type == DT_DIR) { |
| 2499 | const char *name = de->d_name; |
| 2500 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { |
| 2501 | continue; |
| 2502 | } |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2503 | int32_t user_uid = multiuser_get_app_id(s.st_uid); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2504 | if (!strcmp(name, ".") || !strcmp(name, "..")) { |
| 2505 | continue; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2506 | } else if (exclude_apps && (user_uid >= AID_APP_START && user_uid <= AID_APP_END)) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2507 | continue; |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2508 | } else if (is_sdk_sandbox_storage) { |
| 2509 | // In case of sdk sandbox storage (e.g. /data/misc_ce/0/sdksandbox/<package-name>), |
| 2510 | // collect individual stats of each subdirectory (shared, storage of each sdk etc.) |
| 2511 | collectManualStatsForSubDirectories(StringPrintf("%s/%s", path.c_str(), name), |
| 2512 | stats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2513 | } else { |
| 2514 | collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); |
| 2515 | } |
| 2516 | } |
| 2517 | } |
| 2518 | closedir(d); |
| 2519 | } |
| 2520 | |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2521 | static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) { |
| 2522 | FTS *fts; |
| 2523 | FTSENT *p; |
| 2524 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2525 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2526 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 2527 | return; |
| 2528 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2529 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 2530 | p->fts_number = p->fts_parent->fts_number; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2531 | switch (p->fts_info) { |
| 2532 | case FTS_D: |
hyejin.kim | ae3e9ae | 2023-06-20 19:42:57 +0900 | [diff] [blame] | 2533 | if (p->fts_level == 3 |
| 2534 | && !strcmp(p->fts_parent->fts_name, "obb") |
| 2535 | && !strcmp(p->fts_parent->fts_parent->fts_name, "Android")) { |
| 2536 | p->fts_number = 1; |
| 2537 | } else if (p->fts_level == 4 |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2538 | && !strcmp(p->fts_name, "cache") |
| 2539 | && !strcmp(p->fts_parent->fts_parent->fts_name, "data") |
| 2540 | && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) { |
hyejin.kim | ae3e9ae | 2023-06-20 19:42:57 +0900 | [diff] [blame] | 2541 | p->fts_number = 2; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2542 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 2543 | [[fallthrough]]; // to count the directory |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2544 | case FTS_DEFAULT: |
| 2545 | case FTS_F: |
| 2546 | case FTS_SL: |
| 2547 | case FTS_SLNONE: |
| 2548 | int64_t size = (p->fts_statp->st_blocks * 512); |
| 2549 | if (p->fts_number == 1) { |
hyejin.kim | ae3e9ae | 2023-06-20 19:42:57 +0900 | [diff] [blame] | 2550 | stats->codeSize += size; |
| 2551 | } else { |
| 2552 | if (p->fts_number == 2) { |
| 2553 | stats->cacheSize += size; |
| 2554 | } |
| 2555 | stats->dataSize += size; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2556 | } |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2557 | break; |
| 2558 | } |
| 2559 | } |
| 2560 | fts_close(fts); |
| 2561 | } |
Shikha Malhotra | aae9644 | 2021-11-25 10:52:50 +0000 | [diff] [blame] | 2562 | static bool ownsExternalStorage(int32_t appId) { |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 2563 | // if project id calculation is supported then, there is no need to |
| 2564 | // calculate in a different way and project_id based calculation can work |
| 2565 | if (internal_storage_has_project_id()) { |
| 2566 | return false; |
| 2567 | } |
| 2568 | |
Shikha Malhotra | aae9644 | 2021-11-25 10:52:50 +0000 | [diff] [blame] | 2569 | // Fetch external storage owner appid and check if it is the same as the |
| 2570 | // current appId whose size is calculated |
| 2571 | struct stat s; |
| 2572 | auto _picDir = StringPrintf("%s/Pictures", create_data_media_path(nullptr, 0).c_str()); |
| 2573 | // check if the stat are present |
| 2574 | if (stat(_picDir.c_str(), &s) == 0) { |
| 2575 | // fetch the appId from the uid of the media app |
| 2576 | return ((int32_t)multiuser_get_app_id(s.st_uid) == appId); |
| 2577 | } |
| 2578 | return false; |
| 2579 | } |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2580 | binder::Status InstalldNativeService::getAppSize(const std::optional<std::string>& uuid, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2581 | const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, |
| 2582 | int32_t appId, const std::vector<int64_t>& ceDataInodes, |
| 2583 | const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2584 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2585 | CHECK_ARGUMENT_UUID(uuid); |
Alex Buynytskyy | a6823ef | 2022-02-28 14:18:19 -0800 | [diff] [blame] | 2586 | if (packageNames.size() != ceDataInodes.size()) { |
| 2587 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 2588 | "packageNames/ceDataInodes size mismatch."); |
| 2589 | } |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 2590 | for (const auto& packageName : packageNames) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2591 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 2592 | } |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2593 | for (const auto& codePath : codePaths) { |
| 2594 | CHECK_ARGUMENT_PATH(codePath); |
| 2595 | } |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 2596 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 2597 | // read-only measurements without mutation. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2598 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2599 | // When modifying this logic, always verify using tests: |
| 2600 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize |
| 2601 | |
| 2602 | #if MEASURE_DEBUG |
| 2603 | LOG(INFO) << "Measuring user " << userId << " app " << appId; |
| 2604 | #endif |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2605 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2606 | // Here's a summary of the common storage locations across the platform, |
| 2607 | // and how they're each tagged: |
| 2608 | // |
| 2609 | // /data/app/com.example UID system |
| 2610 | // /data/app/com.example/oat UID system |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2611 | // /data/user/0/com.example UID u0_a10 GID u0_a10 |
| 2612 | // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache |
| 2613 | // /data/media/0/foo.txt UID u0_media_rw |
| 2614 | // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image |
| 2615 | // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext |
| 2616 | // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache |
| 2617 | // /data/media/obb/com.example UID system |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2618 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2619 | struct stats stats; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2620 | struct stats extStats; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2621 | memset(&stats, 0, sizeof(stats)); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2622 | memset(&extStats, 0, sizeof(extStats)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2623 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2624 | auto uuidString = uuid.value_or(""); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2625 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2626 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2627 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2628 | flags &= ~FLAG_USE_QUOTA; |
| 2629 | } |
| 2630 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2631 | atrace_pm_begin("obb"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 2632 | for (const auto& packageName : packageNames) { |
Jeff Sharkey | 6d775c5 | 2019-03-22 13:46:20 -0600 | [diff] [blame] | 2633 | auto obbCodePath = create_data_media_package_path(uuid_, userId, |
| 2634 | "obb", packageName.c_str()); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2635 | calculate_tree_size(obbCodePath, &extStats.codeSize); |
| 2636 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2637 | atrace_pm_end(); |
Shikha Malhotra | aae9644 | 2021-11-25 10:52:50 +0000 | [diff] [blame] | 2638 | // Calculating the app size of the external storage owning app in a manual way, since |
| 2639 | // calculating it through quota apis also includes external media storage in the app storage |
| 2640 | // numbers |
| 2641 | if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START && !ownsExternalStorage(appId)) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2642 | atrace_pm_begin("code"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 2643 | for (const auto& codePath : codePaths) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2644 | calculate_tree_size(codePath, &stats.codeSize, -1, |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2645 | multiuser_get_shared_gid(0, appId)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2646 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2647 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2648 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2649 | atrace_pm_begin("quota"); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2650 | collectQuotaStats(uuidString, userId, appId, &stats, &extStats); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2651 | atrace_pm_end(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2652 | } else { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2653 | atrace_pm_begin("code"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 2654 | for (const auto& codePath : codePaths) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2655 | calculate_tree_size(codePath, &stats.codeSize); |
| 2656 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2657 | atrace_pm_end(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2658 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2659 | for (size_t i = 0; i < packageNames.size(); i++) { |
| 2660 | const char* pkgname = packageNames[i].c_str(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2661 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2662 | atrace_pm_begin("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2663 | auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); |
| 2664 | collectManualStats(cePath, &stats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2665 | auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 2666 | collectManualStats(dePath, &stats); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2667 | atrace_pm_end(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2668 | |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2669 | // In case of sdk sandbox storage (e.g. /data/misc_ce/0/sdksandbox/<package-name>), |
| 2670 | // collect individual stats of each subdirectory (shared, storage of each sdk etc.) |
| 2671 | if (appId >= AID_APP_START && appId <= AID_APP_END) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2672 | atrace_pm_begin("sdksandbox"); |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2673 | auto sdkSandboxCePath = |
| 2674 | create_data_misc_sdk_sandbox_package_path(uuid_, true, userId, pkgname); |
| 2675 | collectManualStatsForSubDirectories(sdkSandboxCePath, &stats); |
| 2676 | auto sdkSandboxDePath = |
| 2677 | create_data_misc_sdk_sandbox_package_path(uuid_, false, userId, pkgname); |
| 2678 | collectManualStatsForSubDirectories(sdkSandboxDePath, &stats); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2679 | atrace_pm_end(); |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2682 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2683 | atrace_pm_begin("profiles"); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2684 | calculate_tree_size( |
| 2685 | create_primary_current_profile_package_dir_path(userId, pkgname), |
| 2686 | &stats.dataSize); |
| 2687 | calculate_tree_size( |
| 2688 | create_primary_reference_profile_package_dir_path(pkgname), |
| 2689 | &stats.codeSize); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2690 | atrace_pm_end(); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2691 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2692 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2693 | atrace_pm_begin("external"); |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 2694 | auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2695 | collectManualStats(extPath, &extStats); |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 2696 | auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2697 | calculate_tree_size(mediaPath, &extStats.dataSize); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2698 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2701 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2702 | atrace_pm_begin("dalvik"); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2703 | int32_t sharedGid = multiuser_get_shared_gid(0, appId); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2704 | if (sharedGid != -1) { |
| 2705 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 2706 | sharedGid, -1); |
| 2707 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2708 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2709 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2710 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 2711 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2712 | std::vector<int64_t> ret; |
| 2713 | ret.push_back(stats.codeSize); |
| 2714 | ret.push_back(stats.dataSize); |
| 2715 | ret.push_back(stats.cacheSize); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2716 | ret.push_back(extStats.codeSize); |
| 2717 | ret.push_back(extStats.dataSize); |
| 2718 | ret.push_back(extStats.cacheSize); |
| 2719 | #if MEASURE_DEBUG |
| 2720 | LOG(DEBUG) << "Final result " << toString(ret); |
| 2721 | #endif |
| 2722 | *_aidl_return = ret; |
| 2723 | return ok(); |
| 2724 | } |
| 2725 | |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 2726 | struct external_sizes { |
| 2727 | int64_t audioSize; |
| 2728 | int64_t videoSize; |
| 2729 | int64_t imageSize; |
| 2730 | int64_t totalSize; // excludes OBBs (Android/obb), but includes app data + cache |
| 2731 | int64_t obbSize; |
| 2732 | }; |
| 2733 | |
| 2734 | #define PER_USER_RANGE 100000 |
| 2735 | |
| 2736 | static long getProjectIdForUser(int userId, long projectId) { |
| 2737 | return userId * PER_USER_RANGE + projectId; |
| 2738 | } |
| 2739 | |
| 2740 | static external_sizes getExternalSizesForUserWithQuota(const std::string& uuid, int32_t userId, const std::vector<int32_t>& appIds) { |
| 2741 | struct external_sizes sizes = {}; |
| 2742 | int64_t space; |
| 2743 | |
| 2744 | if (supports_sdcardfs()) { |
| 2745 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); |
| 2746 | if ((space = GetOccupiedSpaceForUid(uuid, uid)) != -1) { |
| 2747 | sizes.totalSize = space; |
| 2748 | } |
| 2749 | |
| 2750 | gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); |
| 2751 | if ((space = GetOccupiedSpaceForGid(uuid, audioGid)) != -1) { |
| 2752 | sizes.audioSize = space; |
| 2753 | } |
| 2754 | |
| 2755 | gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); |
| 2756 | if ((space = GetOccupiedSpaceForGid(uuid, videoGid)) != -1) { |
| 2757 | sizes.videoSize = space; |
| 2758 | } |
| 2759 | |
| 2760 | gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); |
| 2761 | if ((space = GetOccupiedSpaceForGid(uuid, imageGid)) != -1) { |
| 2762 | sizes.imageSize = space; |
| 2763 | } |
| 2764 | |
| 2765 | if ((space = GetOccupiedSpaceForGid(uuid, AID_MEDIA_OBB)) != -1) { |
| 2766 | sizes.obbSize = space; |
| 2767 | } |
| 2768 | } else { |
| 2769 | int64_t totalSize = 0; |
| 2770 | long defaultProjectId = getProjectIdForUser(userId, PROJECT_ID_EXT_DEFAULT); |
| 2771 | if ((space = GetOccupiedSpaceForProjectId(uuid, defaultProjectId)) != -1) { |
| 2772 | // This is all files that are not audio/video/images, excluding |
| 2773 | // OBBs and app-private data |
| 2774 | totalSize += space; |
| 2775 | } |
| 2776 | |
| 2777 | long audioProjectId = getProjectIdForUser(userId, PROJECT_ID_EXT_MEDIA_AUDIO); |
| 2778 | if ((space = GetOccupiedSpaceForProjectId(uuid, audioProjectId)) != -1) { |
| 2779 | sizes.audioSize = space; |
| 2780 | totalSize += space; |
| 2781 | } |
| 2782 | |
| 2783 | long videoProjectId = getProjectIdForUser(userId, PROJECT_ID_EXT_MEDIA_VIDEO); |
| 2784 | if ((space = GetOccupiedSpaceForProjectId(uuid, videoProjectId)) != -1) { |
| 2785 | sizes.videoSize = space; |
| 2786 | totalSize += space; |
| 2787 | } |
| 2788 | |
| 2789 | long imageProjectId = getProjectIdForUser(userId, PROJECT_ID_EXT_MEDIA_IMAGE); |
| 2790 | if ((space = GetOccupiedSpaceForProjectId(uuid, imageProjectId)) != -1) { |
| 2791 | sizes.imageSize = space; |
| 2792 | totalSize += space; |
| 2793 | } |
| 2794 | |
| 2795 | int64_t totalAppDataSize = 0; |
| 2796 | int64_t totalAppCacheSize = 0; |
| 2797 | int64_t totalAppObbSize = 0; |
| 2798 | for (auto appId : appIds) { |
| 2799 | if (appId >= AID_APP_START) { |
| 2800 | // App data |
| 2801 | uid_t uid = multiuser_get_uid(userId, appId); |
| 2802 | long projectId = uid - AID_APP_START + PROJECT_ID_EXT_DATA_START; |
| 2803 | totalAppDataSize += GetOccupiedSpaceForProjectId(uuid, projectId); |
| 2804 | |
| 2805 | // App cache |
| 2806 | long cacheProjectId = uid - AID_APP_START + PROJECT_ID_EXT_CACHE_START; |
| 2807 | totalAppCacheSize += GetOccupiedSpaceForProjectId(uuid, cacheProjectId); |
| 2808 | |
| 2809 | // App OBBs |
| 2810 | long obbProjectId = uid - AID_APP_START + PROJECT_ID_EXT_OBB_START; |
| 2811 | totalAppObbSize += GetOccupiedSpaceForProjectId(uuid, obbProjectId); |
| 2812 | } |
| 2813 | } |
| 2814 | // Total size should include app data + cache |
| 2815 | totalSize += totalAppDataSize; |
| 2816 | totalSize += totalAppCacheSize; |
| 2817 | sizes.totalSize = totalSize; |
| 2818 | |
| 2819 | // Only OBB is separate |
| 2820 | sizes.obbSize = totalAppObbSize; |
| 2821 | } |
| 2822 | |
| 2823 | return sizes; |
| 2824 | } |
| 2825 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2826 | binder::Status InstalldNativeService::getUserSize(const std::optional<std::string>& uuid, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2827 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, |
| 2828 | std::vector<int64_t>* _aidl_return) { |
| 2829 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 2830 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2831 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 2832 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 2833 | // read-only measurements without mutation. |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2834 | |
| 2835 | // When modifying this logic, always verify using tests: |
| 2836 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize |
| 2837 | |
| 2838 | #if MEASURE_DEBUG |
| 2839 | LOG(INFO) << "Measuring user " << userId; |
| 2840 | #endif |
| 2841 | |
| 2842 | struct stats stats; |
| 2843 | struct stats extStats; |
| 2844 | memset(&stats, 0, sizeof(stats)); |
| 2845 | memset(&extStats, 0, sizeof(extStats)); |
| 2846 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2847 | auto uuidString = uuid.value_or(""); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2848 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2849 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2850 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2851 | flags &= ~FLAG_USE_QUOTA; |
| 2852 | } |
| 2853 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2854 | if (flags & FLAG_USE_QUOTA) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2855 | atrace_pm_begin("code"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2856 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2857 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2858 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2859 | atrace_pm_begin("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2860 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 2861 | collectManualStatsForUser(cePath, &stats, true); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2862 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 2863 | collectManualStatsForUser(dePath, &stats, true); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2864 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2865 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2866 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2867 | atrace_pm_begin("profile"); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2868 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); |
| 2869 | calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); |
| 2870 | auto refProfilePath = create_primary_ref_profile_dir_path(); |
| 2871 | calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2872 | atrace_pm_end(); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2873 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2874 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2875 | atrace_pm_begin("external"); |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 2876 | auto sizes = getExternalSizesForUserWithQuota(uuidString, userId, appIds); |
| 2877 | extStats.dataSize += sizes.totalSize; |
| 2878 | extStats.codeSize += sizes.obbSize; |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2879 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2880 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2881 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2882 | atrace_pm_begin("dalvik"); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2883 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 2884 | -1, -1, true); |
| 2885 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize, |
| 2886 | -1, -1, true); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2887 | atrace_pm_end(); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2888 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2889 | atrace_pm_begin("quota"); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2890 | int64_t dataSize = extStats.dataSize; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2891 | for (auto appId : appIds) { |
| 2892 | if (appId >= AID_APP_START) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2893 | collectQuotaStats(uuidString, userId, appId, &stats, &extStats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2894 | #if MEASURE_DEBUG |
| 2895 | // Sleep to make sure we don't lose logs |
| 2896 | usleep(1); |
| 2897 | #endif |
| 2898 | } |
| 2899 | } |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2900 | extStats.dataSize = dataSize; |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2901 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2902 | } else { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2903 | atrace_pm_begin("code"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2904 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2905 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2906 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2907 | atrace_pm_begin("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2908 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 2909 | collectManualStatsForUser(cePath, &stats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2910 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 2911 | collectManualStatsForUser(dePath, &stats); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2912 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2913 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2914 | atrace_pm_begin("sdksandbox"); |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2915 | auto sdkSandboxCePath = create_data_misc_sdk_sandbox_path(uuid_, true, userId); |
| 2916 | collectManualStatsForUser(sdkSandboxCePath, &stats, false, true); |
| 2917 | auto sdkSandboxDePath = create_data_misc_sdk_sandbox_path(uuid_, false, userId); |
| 2918 | collectManualStatsForUser(sdkSandboxDePath, &stats, false, true); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2919 | atrace_pm_end(); |
Sanjana Sunil | 08c0ff3 | 2022-03-08 16:16:18 +0000 | [diff] [blame] | 2920 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2921 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2922 | atrace_pm_begin("profile"); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2923 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); |
| 2924 | calculate_tree_size(userProfilePath, &stats.dataSize); |
| 2925 | auto refProfilePath = create_primary_ref_profile_dir_path(); |
| 2926 | calculate_tree_size(refProfilePath, &stats.codeSize); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2927 | atrace_pm_end(); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2928 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2929 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2930 | atrace_pm_begin("external"); |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2931 | auto dataMediaPath = create_data_media_path(uuid_, userId); |
| 2932 | collectManualExternalStatsForUser(dataMediaPath, &extStats); |
hyejin.kim | ae3e9ae | 2023-06-20 19:42:57 +0900 | [diff] [blame] | 2933 | |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 2934 | #if MEASURE_DEBUG |
| 2935 | LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache " |
hyejin.kim | ae3e9ae | 2023-06-20 19:42:57 +0900 | [diff] [blame] | 2936 | << extStats.cacheSize << " code " << extStats.codeSize; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2937 | #endif |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2938 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2939 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2940 | if (!uuid) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2941 | atrace_pm_begin("dalvik"); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2942 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); |
| 2943 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2944 | atrace_pm_end(); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 2945 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2946 | } |
| 2947 | |
| 2948 | std::vector<int64_t> ret; |
| 2949 | ret.push_back(stats.codeSize); |
| 2950 | ret.push_back(stats.dataSize); |
| 2951 | ret.push_back(stats.cacheSize); |
| 2952 | ret.push_back(extStats.codeSize); |
| 2953 | ret.push_back(extStats.dataSize); |
| 2954 | ret.push_back(extStats.cacheSize); |
| 2955 | #if MEASURE_DEBUG |
| 2956 | LOG(DEBUG) << "Final result " << toString(ret); |
| 2957 | #endif |
| 2958 | *_aidl_return = ret; |
| 2959 | return ok(); |
| 2960 | } |
| 2961 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2962 | binder::Status InstalldNativeService::getExternalSize(const std::optional<std::string>& uuid, |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2963 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, |
| 2964 | std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2965 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 2966 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2967 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 2968 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 2969 | // read-only measurements without mutation. |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2970 | |
| 2971 | // When modifying this logic, always verify using tests: |
| 2972 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize |
| 2973 | |
| 2974 | #if MEASURE_DEBUG |
| 2975 | LOG(INFO) << "Measuring external " << userId; |
| 2976 | #endif |
| 2977 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 2978 | auto uuidString = uuid.value_or(""); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2979 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2980 | |
| 2981 | int64_t totalSize = 0; |
| 2982 | int64_t audioSize = 0; |
| 2983 | int64_t videoSize = 0; |
| 2984 | int64_t imageSize = 0; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2985 | int64_t appSize = 0; |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 2986 | int64_t obbSize = 0; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2987 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2988 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2989 | flags &= ~FLAG_USE_QUOTA; |
| 2990 | } |
| 2991 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2992 | if (flags & FLAG_USE_QUOTA) { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 2993 | atrace_pm_begin("quota"); |
Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 2994 | auto sizes = getExternalSizesForUserWithQuota(uuidString, userId, appIds); |
| 2995 | totalSize = sizes.totalSize; |
| 2996 | audioSize = sizes.audioSize; |
| 2997 | videoSize = sizes.videoSize; |
| 2998 | imageSize = sizes.imageSize; |
| 2999 | obbSize = sizes.obbSize; |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3000 | atrace_pm_end(); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 3001 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3002 | atrace_pm_begin("apps"); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 3003 | struct stats extStats; |
| 3004 | memset(&extStats, 0, sizeof(extStats)); |
| 3005 | for (auto appId : appIds) { |
| 3006 | if (appId >= AID_APP_START) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 3007 | collectQuotaStats(uuidString, userId, appId, nullptr, &extStats); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 3008 | } |
| 3009 | } |
Jeff Sharkey | 4ca8ff9 | 2017-06-07 15:59:03 -0600 | [diff] [blame] | 3010 | appSize = extStats.dataSize; |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3011 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3012 | } else { |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3013 | atrace_pm_begin("manual"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3014 | FTS *fts; |
| 3015 | FTSENT *p; |
| 3016 | auto path = create_data_media_path(uuid_, userId); |
| 3017 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 3018 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3019 | return error("Failed to fts_open " + path); |
| 3020 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 3021 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3022 | char* ext; |
| 3023 | int64_t size = (p->fts_statp->st_blocks * 512); |
| 3024 | switch (p->fts_info) { |
| 3025 | case FTS_F: |
| 3026 | // Only categorize files not belonging to apps |
Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 3027 | if (p->fts_parent->fts_number == 0) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3028 | ext = strrchr(p->fts_name, '.'); |
| 3029 | if (ext != nullptr) { |
| 3030 | switch (MatchExtension(++ext)) { |
| 3031 | case AID_MEDIA_AUDIO: audioSize += size; break; |
| 3032 | case AID_MEDIA_VIDEO: videoSize += size; break; |
| 3033 | case AID_MEDIA_IMAGE: imageSize += size; break; |
| 3034 | } |
| 3035 | } |
| 3036 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 3037 | [[fallthrough]]; // always count against total |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3038 | case FTS_D: |
Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 3039 | // Ignore data belonging to specific apps |
| 3040 | p->fts_number = p->fts_parent->fts_number; |
| 3041 | if (p->fts_level == 1 && !strcmp(p->fts_name, "Android")) { |
| 3042 | p->fts_number = 1; |
| 3043 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 3044 | [[fallthrough]]; // always count against total |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3045 | case FTS_DEFAULT: |
| 3046 | case FTS_SL: |
| 3047 | case FTS_SLNONE: |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 3048 | if (p->fts_parent->fts_number == 1) { |
| 3049 | appSize += size; |
| 3050 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3051 | totalSize += size; |
| 3052 | break; |
| 3053 | } |
| 3054 | } |
| 3055 | fts_close(fts); |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3056 | atrace_pm_end(); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 3057 | |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3058 | atrace_pm_begin("obb"); |
Jeff Sharkey | 6d775c5 | 2019-03-22 13:46:20 -0600 | [diff] [blame] | 3059 | auto obbPath = StringPrintf("%s/Android/obb", |
| 3060 | create_data_media_path(uuid_, userId).c_str()); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 3061 | calculate_tree_size(obbPath, &obbSize); |
Chuwei Xu | 5d64ddf | 2019-12-04 15:32:18 +0800 | [diff] [blame] | 3062 | if (!(flags & FLAG_USE_QUOTA)) { |
| 3063 | totalSize -= obbSize; |
| 3064 | } |
Zim | 23457d0 | 2022-08-17 12:57:41 +0100 | [diff] [blame] | 3065 | atrace_pm_end(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | std::vector<int64_t> ret; |
| 3069 | ret.push_back(totalSize); |
| 3070 | ret.push_back(audioSize); |
| 3071 | ret.push_back(videoSize); |
| 3072 | ret.push_back(imageSize); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 3073 | ret.push_back(appSize); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 3074 | ret.push_back(obbSize); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 3075 | #if MEASURE_DEBUG |
| 3076 | LOG(DEBUG) << "Final result " << toString(ret); |
| 3077 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 3078 | *_aidl_return = ret; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3079 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3082 | binder::Status InstalldNativeService::getAppCrates( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3083 | const std::optional<std::string>& uuid, |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3084 | const std::vector<std::string>& packageNames, int32_t userId, |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3085 | std::optional<std::vector<std::optional<CrateMetadata>>>* _aidl_return) { |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3086 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3087 | ENFORCE_VALID_USER(userId); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3088 | CHECK_ARGUMENT_UUID(uuid); |
| 3089 | for (const auto& packageName : packageNames) { |
| 3090 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 3091 | } |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3092 | #ifdef ENABLE_STORAGE_CRATES |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3093 | LOCK_PACKAGE_USER(); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3094 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3095 | auto retVector = std::vector<std::optional<CrateMetadata>>(); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3096 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 3097 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3098 | std::function<void(CratedFolder, CrateMetadata&&)> onCreateCrate = |
| 3099 | [&](CratedFolder cratedFolder, CrateMetadata&& crateMetadata) -> void { |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3100 | if (cratedFolder == nullptr) { |
| 3101 | return; |
| 3102 | } |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3103 | retVector.push_back(std::move(crateMetadata)); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3104 | }; |
| 3105 | |
| 3106 | for (const auto& packageName : packageNames) { |
| 3107 | #if CRATE_DEBUG |
| 3108 | LOG(DEBUG) << "packageName = " << packageName; |
| 3109 | #endif |
| 3110 | auto crateManager = std::make_unique<CrateManager>(uuid_, userId, packageName); |
| 3111 | crateManager->traverseAllCrates(onCreateCrate); |
| 3112 | } |
| 3113 | |
| 3114 | #if CRATE_DEBUG |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3115 | LOG(WARNING) << "retVector.size() =" << retVector.size(); |
| 3116 | for (auto& item : retVector) { |
Jooyung Han | d520e80 | 2020-03-03 02:56:34 +0900 | [diff] [blame] | 3117 | CrateManager::dump(*item); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3118 | } |
| 3119 | #endif |
| 3120 | |
| 3121 | *_aidl_return = std::move(retVector); |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3122 | #else // ENABLE_STORAGE_CRATES |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3123 | _aidl_return->reset(); |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3124 | |
| 3125 | /* prevent compile warning fail */ |
| 3126 | if (userId < 0) { |
| 3127 | return error(); |
| 3128 | } |
| 3129 | #endif // ENABLE_STORAGE_CRATES |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3130 | return ok(); |
| 3131 | } |
| 3132 | |
| 3133 | binder::Status InstalldNativeService::getUserCrates( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3134 | const std::optional<std::string>& uuid, int32_t userId, |
| 3135 | std::optional<std::vector<std::optional<CrateMetadata>>>* _aidl_return) { |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3136 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3137 | ENFORCE_VALID_USER(userId); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3138 | CHECK_ARGUMENT_UUID(uuid); |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3139 | #ifdef ENABLE_STORAGE_CRATES |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3140 | LOCK_USER(); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3141 | |
| 3142 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3143 | auto retVector = std::vector<std::optional<CrateMetadata>>(); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3144 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3145 | std::function<void(CratedFolder, CrateMetadata&&)> onCreateCrate = |
| 3146 | [&](CratedFolder cratedFolder, CrateMetadata&& crateMetadata) -> void { |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3147 | if (cratedFolder == nullptr) { |
| 3148 | return; |
| 3149 | } |
Jooyung Han | d520e80 | 2020-03-03 02:56:34 +0900 | [diff] [blame] | 3150 | retVector.push_back(std::move(crateMetadata)); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3151 | }; |
| 3152 | |
| 3153 | std::function<void(FTSENT*)> onHandingPackage = [&](FTSENT* packageDir) -> void { |
| 3154 | auto crateManager = std::make_unique<CrateManager>(uuid_, userId, packageDir->fts_name); |
| 3155 | crateManager->traverseAllCrates(onCreateCrate); |
| 3156 | }; |
| 3157 | CrateManager::traverseAllPackagesForUser(uuid, userId, onHandingPackage); |
| 3158 | |
| 3159 | #if CRATE_DEBUG |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3160 | LOG(DEBUG) << "retVector.size() =" << retVector.size(); |
| 3161 | for (auto& item : retVector) { |
Jooyung Han | d520e80 | 2020-03-03 02:56:34 +0900 | [diff] [blame] | 3162 | CrateManager::dump(*item); |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3163 | } |
| 3164 | #endif |
| 3165 | |
| 3166 | *_aidl_return = std::move(retVector); |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3167 | #else // ENABLE_STORAGE_CRATES |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3168 | _aidl_return->reset(); |
Felka Chang | 4df87f6 | 2020-02-11 19:08:43 +0800 | [diff] [blame] | 3169 | |
| 3170 | /* prevent compile warning fail */ |
| 3171 | if (userId < 0) { |
| 3172 | return error(); |
| 3173 | } |
| 3174 | #endif // ENABLE_STORAGE_CRATES |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 3175 | return ok(); |
| 3176 | } |
| 3177 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3178 | binder::Status InstalldNativeService::setAppQuota(const std::optional<std::string>& uuid, |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 3179 | int32_t userId, int32_t appId, int64_t cacheQuota) { |
| 3180 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3181 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 3182 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3183 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 3184 | |
| 3185 | int32_t uid = multiuser_get_uid(userId, appId); |
| 3186 | mCacheQuotas[uid] = cacheQuota; |
| 3187 | |
| 3188 | return ok(); |
| 3189 | } |
| 3190 | |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 3191 | // Dumps the contents of a profile file, using pkgname's dex files for pretty |
| 3192 | // printing the result. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3193 | binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, |
Martin Stjernholm | fd00040 | 2022-04-11 18:04:59 +0100 | [diff] [blame] | 3194 | const std::string& profileName, |
| 3195 | const std::string& codePath, |
| 3196 | bool dumpClassesAndMethods, bool* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3197 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3198 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3199 | CHECK_ARGUMENT_PATH(codePath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3200 | LOCK_PACKAGE(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3201 | |
Martin Stjernholm | fd00040 | 2022-04-11 18:04:59 +0100 | [diff] [blame] | 3202 | *_aidl_return = dump_profiles(uid, packageName, profileName, codePath, dumpClassesAndMethods); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3203 | return ok(); |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 3204 | } |
| 3205 | |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 3206 | // Copy the contents of a system profile over the data profile. |
| 3207 | binder::Status InstalldNativeService::copySystemProfile(const std::string& systemProfile, |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 3208 | int32_t packageUid, const std::string& packageName, const std::string& profileName, |
| 3209 | bool* _aidl_return) { |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 3210 | ENFORCE_UID(AID_SYSTEM); |
Alex Buynytskyy | d04631f | 2022-08-12 15:14:41 -0700 | [diff] [blame] | 3211 | CHECK_ARGUMENT_PATH(systemProfile); |
| 3212 | if (!base::EndsWith(systemProfile, ".prof")) { |
| 3213 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 3214 | StringPrintf("System profile path %s does not end with .prof", |
| 3215 | systemProfile.c_str())); |
| 3216 | } |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 3217 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | d04631f | 2022-08-12 15:14:41 -0700 | [diff] [blame] | 3218 | CHECK_ARGUMENT_FILE_NAME(profileName); |
| 3219 | if (!base::EndsWith(profileName, ".prof")) { |
| 3220 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 3221 | StringPrintf("Profile name %s does not end with .prof", |
| 3222 | profileName.c_str())); |
| 3223 | } |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3224 | LOCK_PACKAGE(); |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 3225 | *_aidl_return = copy_system_profile(systemProfile, packageUid, packageName, profileName); |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 3226 | return ok(); |
| 3227 | } |
| 3228 | |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 3229 | // TODO: Consider returning error codes. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3230 | binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, |
Calin Juravle | 3b75828 | 2021-06-08 08:04:52 -0700 | [diff] [blame] | 3231 | const std::string& profileName, int* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3232 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3233 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3234 | LOCK_PACKAGE(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3235 | |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 3236 | *_aidl_return = analyze_primary_profiles(uid, packageName, profileName); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3237 | return ok(); |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
Calin Juravle | c41dac2 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 3240 | binder::Status InstalldNativeService::createProfileSnapshot(int32_t appId, |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 3241 | const std::string& packageName, const std::string& profileName, |
| 3242 | const std::string& classpath, bool* _aidl_return) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3243 | ENFORCE_UID(AID_SYSTEM); |
| 3244 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3245 | LOCK_PACKAGE(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3246 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 3247 | *_aidl_return = create_profile_snapshot(appId, packageName, profileName, classpath); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3248 | return ok(); |
| 3249 | } |
| 3250 | |
| 3251 | binder::Status InstalldNativeService::destroyProfileSnapshot(const std::string& packageName, |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 3252 | const std::string& profileName) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3253 | ENFORCE_UID(AID_SYSTEM); |
| 3254 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3255 | LOCK_PACKAGE(); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3256 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 3257 | std::string snapshot = create_snapshot_profile_path(packageName, profileName); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3258 | if ((unlink(snapshot.c_str()) != 0) && (errno != ENOENT)) { |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 3259 | return error("Failed to destroy profile snapshot for " + packageName + ":" + profileName); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 3260 | } |
| 3261 | return ok(); |
| 3262 | } |
| 3263 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3264 | static const char* getCStr(const std::optional<std::string>& data, |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 3265 | const char* default_value = nullptr) { |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3266 | return data ? data->c_str() : default_value; |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 3267 | } |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3268 | binder::Status InstalldNativeService::dexopt( |
| 3269 | const std::string& apkPath, int32_t uid, const std::string& packageName, |
| 3270 | const std::string& instructionSet, int32_t dexoptNeeded, |
| 3271 | const std::optional<std::string>& outputPath, int32_t dexFlags, |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3272 | const std::string& compilerFilter, const std::optional<std::string>& uuid, |
| 3273 | const std::optional<std::string>& classLoaderContext, |
| 3274 | const std::optional<std::string>& seInfo, bool downgrade, int32_t targetSdkVersion, |
| 3275 | const std::optional<std::string>& profileName, |
| 3276 | const std::optional<std::string>& dexMetadataPath, |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3277 | const std::optional<std::string>& compilationReason, bool* aidl_return) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3278 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3279 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3280 | CHECK_ARGUMENT_PATH(apkPath); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3281 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3282 | CHECK_ARGUMENT_PATH(outputPath); |
| 3283 | CHECK_ARGUMENT_PATH(dexMetadataPath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3284 | const auto userId = multiuser_get_user_id(uid); |
| 3285 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3286 | |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 3287 | const char* oat_dir = getCStr(outputPath); |
| 3288 | const char* instruction_set = instructionSet.c_str(); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3289 | if (oat_dir != nullptr && !createOatDir(packageName, oat_dir, instruction_set).isOk()) { |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 3290 | // Can't create oat dir - let dexopt use cache dir. |
| 3291 | oat_dir = nullptr; |
| 3292 | } |
| 3293 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3294 | const char* apk_path = apkPath.c_str(); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3295 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3296 | const char* compiler_filter = compilerFilter.c_str(); |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 3297 | const char* volume_uuid = getCStr(uuid); |
| 3298 | const char* class_loader_context = getCStr(classLoaderContext); |
| 3299 | const char* se_info = getCStr(seInfo); |
| 3300 | const char* profile_name = getCStr(profileName); |
| 3301 | const char* dm_path = getCStr(dexMetadataPath); |
Calin Juravle | dcccd83 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 3302 | const char* compilation_reason = getCStr(compilationReason); |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 3303 | std::string error_msg; |
Keun young Park | 65578d6 | 2021-06-28 17:52:05 -0700 | [diff] [blame] | 3304 | bool completed = false; // not necessary but for compiler |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3305 | int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, |
Calin Juravle | 1d66761 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 3306 | oat_dir, dexFlags, compiler_filter, volume_uuid, class_loader_context, se_info, |
Keun young Park | 65578d6 | 2021-06-28 17:52:05 -0700 | [diff] [blame] | 3307 | downgrade, targetSdkVersion, profile_name, dm_path, compilation_reason, &error_msg, |
| 3308 | &completed); |
| 3309 | *aidl_return = completed; |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 3310 | return res ? error(res, error_msg) : ok(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 3311 | } |
| 3312 | |
Keun young Park | 65578d6 | 2021-06-28 17:52:05 -0700 | [diff] [blame] | 3313 | binder::Status InstalldNativeService::controlDexOptBlocking(bool block) { |
| 3314 | android::installd::control_dexopt_blocking(block); |
| 3315 | return ok(); |
| 3316 | } |
| 3317 | |
Eric Holk | 0ebbe0f | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 3318 | binder::Status InstalldNativeService::compileLayouts(const std::string& apkPath, |
| 3319 | const std::string& packageName, |
| 3320 | const std ::string& outDexFile, int uid, |
| 3321 | bool* _aidl_return) { |
| 3322 | const char* apk_path = apkPath.c_str(); |
| 3323 | const char* package_name = packageName.c_str(); |
| 3324 | const char* out_dex_file = outDexFile.c_str(); |
| 3325 | *_aidl_return = android::installd::view_compiler(apk_path, package_name, out_dex_file, uid); |
| 3326 | return *_aidl_return ? ok() : error("viewcompiler failed"); |
| 3327 | } |
| 3328 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3329 | binder::Status InstalldNativeService::linkNativeLibraryDirectory( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3330 | const std::optional<std::string>& uuid, const std::string& packageName, |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3331 | const std::string& nativeLibPath32, int32_t userId) { |
| 3332 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3333 | CHECK_ARGUMENT_UUID(uuid); |
| 3334 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3335 | CHECK_ARGUMENT_PATH(nativeLibPath32); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3336 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3337 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3338 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 3339 | const char* pkgname = packageName.c_str(); |
| 3340 | const char* asecLibDir = nativeLibPath32.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3341 | struct stat s, libStat; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3342 | binder::Status res = ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3343 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3344 | auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 3345 | auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 3346 | |
| 3347 | const char* pkgdir = _pkgdir.c_str(); |
| 3348 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3349 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3350 | if (stat(pkgdir, &s) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3351 | return error("Failed to stat " + _pkgdir); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3352 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3353 | |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 3354 | char *con = nullptr; |
Alex Buynytskyy | c668d56 | 2023-11-10 22:53:46 +0000 | [diff] [blame] | 3355 | if (::lgetfilecon(pkgdir, &con) < 0) { |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 3356 | return error("Failed to lgetfilecon " + _pkgdir); |
| 3357 | } |
| 3358 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3359 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 3360 | res = error("Failed to chown " + _pkgdir); |
| 3361 | goto out; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | if (chmod(pkgdir, 0700) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3365 | res = error("Failed to chmod " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3366 | goto out; |
| 3367 | } |
| 3368 | |
| 3369 | if (lstat(libsymlink, &libStat) < 0) { |
| 3370 | if (errno != ENOENT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3371 | res = error("Failed to stat " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3372 | goto out; |
| 3373 | } |
| 3374 | } else { |
| 3375 | if (S_ISDIR(libStat.st_mode)) { |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 3376 | if (delete_dir_contents(libsymlink, 1, nullptr) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3377 | res = error("Failed to delete " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3378 | goto out; |
| 3379 | } |
| 3380 | } else if (S_ISLNK(libStat.st_mode)) { |
| 3381 | if (unlink(libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3382 | res = error("Failed to unlink " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3383 | goto out; |
| 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | if (symlink(asecLibDir, libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3389 | res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3390 | goto out; |
| 3391 | } |
| 3392 | |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 3393 | if (lsetfilecon(libsymlink, con) < 0) { |
| 3394 | res = error("Failed to lsetfilecon " + _libsymlink); |
| 3395 | goto out; |
| 3396 | } |
| 3397 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3398 | out: |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 3399 | free(con); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3400 | if (chmod(pkgdir, s.st_mode) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3401 | auto msg = "Failed to cleanup chmod " + _pkgdir; |
| 3402 | if (res.isOk()) { |
| 3403 | res = error(msg); |
| 3404 | } else { |
| 3405 | PLOG(ERROR) << msg; |
| 3406 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3410 | auto msg = "Failed to cleanup chown " + _pkgdir; |
| 3411 | if (res.isOk()) { |
| 3412 | res = error(msg); |
| 3413 | } else { |
| 3414 | PLOG(ERROR) << msg; |
| 3415 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3416 | } |
| 3417 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3418 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 3419 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 3420 | |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3421 | binder::Status InstalldNativeService::restoreconAppData(const std::optional<std::string>& uuid, |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 3422 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, |
| 3423 | const std::string& seInfo) { |
| 3424 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3425 | ENFORCE_VALID_USER(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3426 | CHECK_ARGUMENT_UUID(uuid); |
| 3427 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3428 | LOCK_PACKAGE_USER(); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 3429 | return restoreconAppDataLocked(uuid, packageName, userId, flags, appId, seInfo); |
| 3430 | } |
| 3431 | |
| 3432 | binder::Status InstalldNativeService::restoreconAppDataLocked( |
| 3433 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 3434 | int32_t flags, int32_t appId, const std::string& seInfo) { |
| 3435 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3436 | ENFORCE_VALID_USER(userId); |
Alex Buynytskyy | fcd5bf3 | 2021-11-23 11:31:34 -0800 | [diff] [blame] | 3437 | CHECK_ARGUMENT_UUID(uuid); |
| 3438 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3439 | |
| 3440 | binder::Status res = ok(); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 3441 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 3442 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 3443 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 3444 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 3445 | const char* pkgName = packageName.c_str(); |
| 3446 | const char* seinfo = seInfo.c_str(); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 3447 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 3448 | uid_t uid = multiuser_get_uid(userId, appId); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 3449 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 3450 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 3451 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3452 | res = error("restorecon failed for " + path); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 3453 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 3454 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 3455 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 3456 | auto path = create_data_user_de_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 3457 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3458 | res = error("restorecon failed for " + path); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 3459 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 3460 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3461 | return res; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 3462 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 3463 | |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 3464 | binder::Status InstalldNativeService::restoreconSdkDataLocked( |
| 3465 | const std::optional<std::string>& uuid, const std::string& packageName, int32_t userId, |
| 3466 | int32_t flags, int32_t appId, const std::string& seInfo) { |
| 3467 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3468 | ENFORCE_VALID_USER(userId); |
Mohammad Samiul Islam | e7634ba | 2022-03-07 20:27:30 +0000 | [diff] [blame] | 3469 | CHECK_ARGUMENT_UUID(uuid); |
| 3470 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 3471 | |
| 3472 | binder::Status res = ok(); |
| 3473 | |
| 3474 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
| 3475 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; |
| 3476 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 3477 | const char* pkgName = packageName.c_str(); |
| 3478 | const char* seinfo = seInfo.c_str(); |
| 3479 | |
| 3480 | uid_t uid = multiuser_get_sdk_sandbox_uid(userId, appId); |
| 3481 | constexpr int storageFlags[2] = {FLAG_STORAGE_CE, FLAG_STORAGE_DE}; |
| 3482 | for (int currentFlag : storageFlags) { |
| 3483 | if ((flags & currentFlag) == 0) { |
| 3484 | continue; |
| 3485 | } |
| 3486 | const bool isCeData = (currentFlag == FLAG_STORAGE_CE); |
| 3487 | const auto packagePath = |
| 3488 | create_data_misc_sdk_sandbox_package_path(uuid_, isCeData, userId, pkgName); |
| 3489 | if (access(packagePath.c_str(), F_OK) != 0) { |
| 3490 | LOG(INFO) << "Missing source " << packagePath; |
| 3491 | continue; |
| 3492 | } |
| 3493 | const auto subDirHandler = [&packagePath, &seinfo, &uid, &seflags, |
| 3494 | &res](const std::string& subDir) { |
| 3495 | const auto& fullpath = packagePath + "/" + subDir; |
| 3496 | if (selinux_android_restorecon_pkgdir(fullpath.c_str(), seinfo, uid, seflags) < 0) { |
| 3497 | res = error("restorecon failed for " + fullpath); |
| 3498 | } |
| 3499 | }; |
| 3500 | const auto ec = foreach_subdir(packagePath, subDirHandler); |
| 3501 | if (ec != 0) { |
| 3502 | res = error("Failed to restorecon for subdirs of " + packagePath); |
| 3503 | } |
| 3504 | } |
| 3505 | return res; |
| 3506 | } |
| 3507 | |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3508 | binder::Status InstalldNativeService::createOatDir(const std::string& packageName, |
| 3509 | const std::string& oatDir, |
| 3510 | const std::string& instructionSet) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3511 | ENFORCE_UID(AID_SYSTEM); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3512 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3513 | CHECK_ARGUMENT_PATH(oatDir); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3514 | LOCK_PACKAGE(); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 3515 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3516 | const char* oat_dir = oatDir.c_str(); |
| 3517 | const char* instruction_set = instructionSet.c_str(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3518 | char oat_instr_dir[PKG_PATH_MAX]; |
| 3519 | |
| 3520 | if (validate_apk_path(oat_dir)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3521 | return error("Invalid path " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3522 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 3523 | if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3524 | return error("Failed to prepare " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3525 | } |
| 3526 | if (selinux_android_restorecon(oat_dir, 0)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3527 | return error("Failed to restorecon " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3528 | } |
| 3529 | snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 3530 | if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3531 | return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3532 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3533 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3534 | } |
| 3535 | |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3536 | binder::Status InstalldNativeService::rmPackageDir(const std::string& packageName, |
| 3537 | const std::string& packageDir) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3538 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3539 | CHECK_ARGUMENT_PATH(packageDir); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3540 | LOCK_PACKAGE(); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 3541 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3542 | if (validate_apk_path(packageDir.c_str())) { |
| 3543 | return error("Invalid path " + packageDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3544 | } |
Oleksandr Peletskyi | d2be632 | 2019-01-24 17:59:45 +0100 | [diff] [blame] | 3545 | if (rm_package_dir(packageDir) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3546 | return error("Failed to delete " + packageDir); |
| 3547 | } |
| 3548 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 3549 | } |
| 3550 | |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3551 | binder::Status InstalldNativeService::linkFile(const std::string& packageName, |
| 3552 | const std::string& relativePath, |
| 3553 | const std::string& fromBase, |
| 3554 | const std::string& toBase) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3555 | ENFORCE_UID(AID_SYSTEM); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3556 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3557 | CHECK_ARGUMENT_PATH(fromBase); |
| 3558 | CHECK_ARGUMENT_PATH(toBase); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3559 | LOCK_PACKAGE(); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 3560 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3561 | const char* relative_path = relativePath.c_str(); |
| 3562 | const char* from_base = fromBase.c_str(); |
| 3563 | const char* to_base = toBase.c_str(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 3564 | char from_path[PKG_PATH_MAX]; |
| 3565 | char to_path[PKG_PATH_MAX]; |
| 3566 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); |
| 3567 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); |
| 3568 | |
| 3569 | if (validate_apk_path_subdirs(from_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3570 | return error(StringPrintf("Invalid from path %s", from_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | if (validate_apk_path_subdirs(to_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3574 | return error(StringPrintf("Invalid to path %s", to_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 3575 | } |
| 3576 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3577 | if (link(from_path, to_path) < 0) { |
| 3578 | return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 3579 | } |
| 3580 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3581 | return ok(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 3582 | } |
| 3583 | |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3584 | binder::Status InstalldNativeService::moveAb(const std::string& packageName, |
| 3585 | const std::string& apkPath, |
| 3586 | const std::string& instructionSet, |
| 3587 | const std::string& outputPath) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3588 | ENFORCE_UID(AID_SYSTEM); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3589 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3590 | CHECK_ARGUMENT_PATH(apkPath); |
| 3591 | CHECK_ARGUMENT_PATH(outputPath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3592 | LOCK_PACKAGE(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 3593 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3594 | const char* apk_path = apkPath.c_str(); |
| 3595 | const char* instruction_set = instructionSet.c_str(); |
| 3596 | const char* oat_dir = outputPath.c_str(); |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 3597 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 3598 | bool success = move_ab(apk_path, instruction_set, oat_dir); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 3599 | return success ? ok() : error(); |
Andreas Gampe | e65b4fa | 2016-03-01 11:46:45 -0800 | [diff] [blame] | 3600 | } |
| 3601 | |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3602 | binder::Status InstalldNativeService::deleteOdex(const std::string& packageName, |
| 3603 | const std::string& apkPath, |
| 3604 | const std::string& instructionSet, |
| 3605 | const std::optional<std::string>& outputPath, |
| 3606 | int64_t* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3607 | ENFORCE_UID(AID_SYSTEM); |
Alex Buynytskyy | 44036e2 | 2021-11-19 08:58:23 -0800 | [diff] [blame] | 3608 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3609 | CHECK_ARGUMENT_PATH(apkPath); |
| 3610 | CHECK_ARGUMENT_PATH(outputPath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3611 | LOCK_PACKAGE(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 3612 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3613 | const char* apk_path = apkPath.c_str(); |
| 3614 | const char* instruction_set = instructionSet.c_str(); |
Andreas Gampe | c523409 | 2017-05-31 16:39:58 -0700 | [diff] [blame] | 3615 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 3616 | |
Calin Juravle | ea214ad | 2021-06-11 09:17:20 -0700 | [diff] [blame] | 3617 | *_aidl_return = delete_odex(apk_path, instruction_set, oat_dir); |
| 3618 | return *_aidl_return == -1 ? error() : ok(); |
Andreas Gampe | 3964da0 | 2016-09-09 17:07:04 -0700 | [diff] [blame] | 3619 | } |
| 3620 | |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 3621 | binder::Status InstalldNativeService::reconcileSecondaryDexFile( |
| 3622 | const std::string& dexPath, const std::string& packageName, int32_t uid, |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3623 | const std::vector<std::string>& isas, const std::optional<std::string>& volumeUuid, |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 3624 | int32_t storage_flag, bool* _aidl_return) { |
| 3625 | ENFORCE_UID(AID_SYSTEM); |
| 3626 | CHECK_ARGUMENT_UUID(volumeUuid); |
| 3627 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3628 | CHECK_ARGUMENT_PATH(dexPath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3629 | const auto userId = multiuser_get_user_id(uid); |
| 3630 | LOCK_PACKAGE_USER(); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3631 | |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 3632 | bool result = android::installd::reconcile_secondary_dex_file( |
| 3633 | dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return); |
| 3634 | return result ? ok() : error(); |
| 3635 | } |
| 3636 | |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 3637 | binder::Status InstalldNativeService::hashSecondaryDexFile( |
| 3638 | const std::string& dexPath, const std::string& packageName, int32_t uid, |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3639 | const std::optional<std::string>& volumeUuid, int32_t storageFlag, |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 3640 | std::vector<uint8_t>* _aidl_return) { |
| 3641 | ENFORCE_UID(AID_SYSTEM); |
| 3642 | CHECK_ARGUMENT_UUID(volumeUuid); |
| 3643 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3644 | CHECK_ARGUMENT_PATH(dexPath); |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 3645 | |
| 3646 | // mLock is not taken here since we will never modify the file system. |
| 3647 | // If a file is modified just as we are reading it this may result in an |
| 3648 | // anomalous hash, but that's ok. |
| 3649 | bool result = android::installd::hash_secondary_dex_file( |
| 3650 | dexPath, packageName, uid, volumeUuid, storageFlag, _aidl_return); |
| 3651 | return result ? ok() : error(); |
| 3652 | } |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 3653 | /** |
| 3654 | * Returns true if ioctl feature (F2FS_IOC_FS{GET,SET}XATTR) is supported as |
| 3655 | * these were introduced in Linux 4.14, so kernel versions before that will fail |
| 3656 | * while setting project id attributes. Only when these features are enabled, |
| 3657 | * storage calculation using project_id is enabled |
| 3658 | */ |
| 3659 | bool check_if_ioctl_feature_is_supported() { |
| 3660 | bool result = false; |
| 3661 | auto temp_path = StringPrintf("%smisc/installd/ioctl_check", android_data_dir.c_str()); |
| 3662 | if (access(temp_path.c_str(), F_OK) != 0) { |
| 3663 | int fd = open(temp_path.c_str(), O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0644); |
Shikha Malhotra | c18afa8 | 2022-04-01 22:13:39 +0000 | [diff] [blame] | 3664 | result = set_quota_project_id(temp_path, 0, false) == 0; |
Shikha Malhotra | 976ae00 | 2022-02-26 15:39:53 +0000 | [diff] [blame] | 3665 | close(fd); |
| 3666 | // delete the temp file |
| 3667 | remove(temp_path.c_str()); |
| 3668 | } |
| 3669 | return result; |
| 3670 | } |
| 3671 | |
| 3672 | binder::Status InstalldNativeService::setFirstBoot() { |
| 3673 | ENFORCE_UID(AID_SYSTEM); |
| 3674 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 3675 | std::string uuid; |
| 3676 | if (GetOccupiedSpaceForProjectId(uuid, 0) != -1 && check_if_ioctl_feature_is_supported()) { |
| 3677 | auto first_boot_path = |
| 3678 | StringPrintf("%smisc/installd/using_project_ids", android_data_dir.c_str()); |
| 3679 | if (access(first_boot_path.c_str(), F_OK) != 0) { |
| 3680 | close(open(first_boot_path.c_str(), O_CREAT | O_TRUNC | O_RDWR | O_CLOEXEC, 0644)); |
| 3681 | } |
| 3682 | } |
| 3683 | return ok(); |
| 3684 | } |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 3685 | |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 3686 | binder::Status InstalldNativeService::invalidateMounts() { |
| 3687 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3688 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 3689 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3690 | mStorageMounts.clear(); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 3691 | |
| 3692 | #if !BYPASS_QUOTA |
| 3693 | if (!InvalidateQuotaMounts()) { |
| 3694 | return error("Failed to read mounts"); |
| 3695 | } |
| 3696 | #endif |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 3697 | |
| 3698 | std::ifstream in("/proc/mounts"); |
| 3699 | if (!in.is_open()) { |
| 3700 | return error("Failed to read mounts"); |
| 3701 | } |
| 3702 | |
| 3703 | std::string source; |
| 3704 | std::string target; |
| 3705 | std::string ignored; |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 3706 | while (!in.eof()) { |
| 3707 | std::getline(in, source, ' '); |
| 3708 | std::getline(in, target, ' '); |
| 3709 | std::getline(in, ignored); |
| 3710 | |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 3711 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
Zim | 1edb92a | 2020-01-09 08:48:00 +0000 | [diff] [blame] | 3712 | if (target.find(kMntFuse) == 0) { |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 3713 | LOG(DEBUG) << "Found storage mount " << source << " at " << target; |
| 3714 | mStorageMounts[source] = target; |
| 3715 | } |
| 3716 | } else { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3717 | #if !BYPASS_SDCARDFS |
Zim | 1edb92a | 2020-01-09 08:48:00 +0000 | [diff] [blame] | 3718 | if (target.find(kMntSdcardfs) == 0) { |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 3719 | LOG(DEBUG) << "Found storage mount " << source << " at " << target; |
| 3720 | mStorageMounts[source] = target; |
| 3721 | } |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3722 | #endif |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 3723 | } |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 3724 | } |
| 3725 | return ok(); |
| 3726 | } |
| 3727 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3728 | // Mount volume's CE and DE storage to mirror |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3729 | binder::Status InstalldNativeService::tryMountDataMirror( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3730 | const std::optional<std::string>& uuid) { |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3731 | ENFORCE_UID(AID_SYSTEM); |
| 3732 | CHECK_ARGUMENT_UUID(uuid); |
| 3733 | if (!sAppDataIsolationEnabled) { |
| 3734 | return ok(); |
| 3735 | } |
| 3736 | if (!uuid) { |
| 3737 | return error("Should not happen, mounting uuid == null"); |
| 3738 | } |
| 3739 | |
| 3740 | const char* uuid_ = uuid->c_str(); |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3741 | |
Alex Buynytskyy | 40c45ba | 2021-11-18 15:16:27 -0800 | [diff] [blame] | 3742 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 3743 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3744 | std::string mirrorVolCePath(StringPrintf("%s/%s", kDataMirrorCePath, uuid_)); |
Eric Biggers | 5818c65 | 2023-06-14 00:53:20 +0000 | [diff] [blame] | 3745 | if (fs_prepare_dir(mirrorVolCePath.c_str(), 0511, AID_SYSTEM, AID_SYSTEM) != 0) { |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3746 | return error("Failed to create CE data mirror"); |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3747 | } |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3748 | |
| 3749 | std::string mirrorVolDePath(StringPrintf("%s/%s", kDataMirrorDePath, uuid_)); |
Eric Biggers | 5818c65 | 2023-06-14 00:53:20 +0000 | [diff] [blame] | 3750 | if (fs_prepare_dir(mirrorVolDePath.c_str(), 0511, AID_SYSTEM, AID_SYSTEM) != 0) { |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3751 | return error("Failed to create DE data mirror"); |
| 3752 | } |
| 3753 | |
| 3754 | std::string mirrorVolMiscCePath(StringPrintf("%s/%s", kMiscMirrorCePath, uuid_)); |
Eric Biggers | 5818c65 | 2023-06-14 00:53:20 +0000 | [diff] [blame] | 3755 | if (fs_prepare_dir(mirrorVolMiscCePath.c_str(), 0511, AID_SYSTEM, AID_SYSTEM) != 0) { |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3756 | return error("Failed to create CE misc mirror"); |
| 3757 | } |
| 3758 | |
| 3759 | std::string mirrorVolMiscDePath(StringPrintf("%s/%s", kMiscMirrorDePath, uuid_)); |
Eric Biggers | 5818c65 | 2023-06-14 00:53:20 +0000 | [diff] [blame] | 3760 | if (fs_prepare_dir(mirrorVolMiscDePath.c_str(), 0511, AID_SYSTEM, AID_SYSTEM) != 0) { |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3761 | return error("Failed to create DE misc mirror"); |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | auto cePath = StringPrintf("%s/user", create_data_path(uuid_).c_str()); |
| 3765 | auto dePath = StringPrintf("%s/user_de", create_data_path(uuid_).c_str()); |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3766 | auto miscCePath = StringPrintf("%s/misc_ce", create_data_path(uuid_).c_str()); |
| 3767 | auto miscDePath = StringPrintf("%s/misc_de", create_data_path(uuid_).c_str()); |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3768 | |
| 3769 | if (access(cePath.c_str(), F_OK) != 0) { |
| 3770 | return error("Cannot access CE path: " + cePath); |
| 3771 | } |
| 3772 | if (access(dePath.c_str(), F_OK) != 0) { |
| 3773 | return error("Cannot access DE path: " + dePath); |
| 3774 | } |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3775 | if (access(miscCePath.c_str(), F_OK) != 0) { |
| 3776 | return error("Cannot access misc CE path: " + cePath); |
| 3777 | } |
| 3778 | if (access(miscDePath.c_str(), F_OK) != 0) { |
| 3779 | return error("Cannot access misc DE path: " + dePath); |
| 3780 | } |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3781 | |
| 3782 | struct stat ceStat, mirrorCeStat; |
| 3783 | if (stat(cePath.c_str(), &ceStat) != 0) { |
| 3784 | return error("Failed to stat " + cePath); |
| 3785 | } |
| 3786 | if (stat(mirrorVolCePath.c_str(), &mirrorCeStat) != 0) { |
| 3787 | return error("Failed to stat " + mirrorVolCePath); |
| 3788 | } |
| 3789 | |
Eric Biggers | ab9ea46 | 2022-05-26 00:28:09 +0000 | [diff] [blame] | 3790 | if (mirrorCeStat.st_ino == ceStat.st_ino && mirrorCeStat.st_dev == ceStat.st_dev) { |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3791 | // As it's being called by prepareUserStorage, it can be called multiple times. |
| 3792 | // Hence, we if we mount it already, we should skip it. |
Eric Biggers | ab9ea46 | 2022-05-26 00:28:09 +0000 | [diff] [blame] | 3793 | LOG(INFO) << "CE dir is mounted already: " + cePath; |
Ricky Wai | 2a4983f | 2020-02-07 14:25:45 +0000 | [diff] [blame] | 3794 | return ok(); |
| 3795 | } |
| 3796 | |
| 3797 | // Mount CE mirror |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3798 | if (TEMP_FAILURE_RETRY(mount(cePath.c_str(), mirrorVolCePath.c_str(), NULL, |
| 3799 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, nullptr)) == -1) { |
| 3800 | return error("Failed to mount " + mirrorVolCePath); |
| 3801 | } |
| 3802 | |
| 3803 | // Mount DE mirror |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3804 | if (TEMP_FAILURE_RETRY(mount(dePath.c_str(), mirrorVolDePath.c_str(), NULL, |
| 3805 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, nullptr)) == -1) { |
| 3806 | return error("Failed to mount " + mirrorVolDePath); |
| 3807 | } |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Mount misc CE mirror |
| 3810 | if (TEMP_FAILURE_RETRY(mount(miscCePath.c_str(), mirrorVolMiscCePath.c_str(), NULL, |
| 3811 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, |
| 3812 | nullptr)) == -1) { |
| 3813 | return error("Failed to mount " + mirrorVolMiscCePath); |
| 3814 | } |
| 3815 | |
| 3816 | // Mount misc DE mirror |
| 3817 | if (TEMP_FAILURE_RETRY(mount(miscDePath.c_str(), mirrorVolMiscDePath.c_str(), NULL, |
| 3818 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, |
| 3819 | nullptr)) == -1) { |
| 3820 | return error("Failed to mount " + mirrorVolMiscDePath); |
| 3821 | } |
| 3822 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3823 | return ok(); |
| 3824 | } |
| 3825 | |
| 3826 | // Unmount volume's CE and DE storage from mirror |
| 3827 | binder::Status InstalldNativeService::onPrivateVolumeRemoved( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3828 | const std::optional<std::string>& uuid) { |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3829 | ENFORCE_UID(AID_SYSTEM); |
| 3830 | CHECK_ARGUMENT_UUID(uuid); |
| 3831 | if (!sAppDataIsolationEnabled) { |
| 3832 | return ok(); |
| 3833 | } |
| 3834 | if (!uuid) { |
| 3835 | // It happens when private volume failed to mount. |
| 3836 | LOG(INFO) << "Ignore unmount uuid=null"; |
| 3837 | return ok(); |
| 3838 | } |
| 3839 | const char* uuid_ = uuid->c_str(); |
| 3840 | |
| 3841 | binder::Status res = ok(); |
| 3842 | |
| 3843 | std::string mirrorCeVolPath(StringPrintf("%s/%s", kDataMirrorCePath, uuid_)); |
| 3844 | std::string mirrorDeVolPath(StringPrintf("%s/%s", kDataMirrorDePath, uuid_)); |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3845 | std::string mirrorMiscCeVolPath(StringPrintf("%s/%s", kMiscMirrorCePath, uuid_)); |
| 3846 | std::string mirrorMiscDeVolPath(StringPrintf("%s/%s", kMiscMirrorDePath, uuid_)); |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3847 | |
Alex Buynytskyy | 40c45ba | 2021-11-18 15:16:27 -0800 | [diff] [blame] | 3848 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 3849 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3850 | // Unmount CE storage |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3851 | if (TEMP_FAILURE_RETRY(umount(mirrorCeVolPath.c_str())) != 0) { |
| 3852 | if (errno != ENOENT) { |
| 3853 | res = error(StringPrintf("Failed to umount %s %s", mirrorCeVolPath.c_str(), |
| 3854 | strerror(errno))); |
| 3855 | } |
| 3856 | } |
| 3857 | if (delete_dir_contents_and_dir(mirrorCeVolPath, true) != 0) { |
| 3858 | res = error("Failed to delete " + mirrorCeVolPath); |
| 3859 | } |
| 3860 | |
| 3861 | // Unmount DE storage |
| 3862 | if (TEMP_FAILURE_RETRY(umount(mirrorDeVolPath.c_str())) != 0) { |
| 3863 | if (errno != ENOENT) { |
| 3864 | res = error(StringPrintf("Failed to umount %s %s", mirrorDeVolPath.c_str(), |
| 3865 | strerror(errno))); |
| 3866 | } |
| 3867 | } |
| 3868 | if (delete_dir_contents_and_dir(mirrorDeVolPath, true) != 0) { |
| 3869 | res = error("Failed to delete " + mirrorDeVolPath); |
| 3870 | } |
Sanjana Sunil | 691163d | 2022-03-28 19:15:54 +0000 | [diff] [blame] | 3871 | |
| 3872 | // Unmount misc CE storage |
| 3873 | if (TEMP_FAILURE_RETRY(umount(mirrorMiscCeVolPath.c_str())) != 0) { |
| 3874 | if (errno != ENOENT) { |
| 3875 | res = error(StringPrintf("Failed to umount %s %s", mirrorMiscCeVolPath.c_str(), |
| 3876 | strerror(errno))); |
| 3877 | } |
| 3878 | } |
| 3879 | if (delete_dir_contents_and_dir(mirrorMiscCeVolPath, true) != 0) { |
| 3880 | res = error("Failed to delete " + mirrorMiscCeVolPath); |
| 3881 | } |
| 3882 | |
| 3883 | // Unmount misc DE storage |
| 3884 | if (TEMP_FAILURE_RETRY(umount(mirrorMiscDeVolPath.c_str())) != 0) { |
| 3885 | if (errno != ENOENT) { |
| 3886 | res = error(StringPrintf("Failed to umount %s %s", mirrorMiscDeVolPath.c_str(), |
| 3887 | strerror(errno))); |
| 3888 | } |
| 3889 | } |
| 3890 | if (delete_dir_contents_and_dir(mirrorMiscDeVolPath, true) != 0) { |
| 3891 | res = error("Failed to delete " + mirrorMiscDeVolPath); |
| 3892 | } |
| 3893 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 3894 | return res; |
| 3895 | } |
| 3896 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3897 | std::string InstalldNativeService::findDataMediaPath( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3898 | const std::optional<std::string>& uuid, userid_t userid) { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 3899 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 3900 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 3901 | auto path = StringPrintf("%s/media", create_data_path(uuid_).c_str()); |
| 3902 | auto resolved = mStorageMounts[path]; |
| 3903 | if (resolved.empty()) { |
| 3904 | LOG(WARNING) << "Failed to find storage mount for " << path; |
| 3905 | resolved = path; |
| 3906 | } |
| 3907 | return StringPrintf("%s/%u", resolved.c_str(), userid); |
| 3908 | } |
| 3909 | |
Jeff Sharkey | 325b8c9 | 2017-02-21 10:00:53 -0700 | [diff] [blame] | 3910 | binder::Status InstalldNativeService::isQuotaSupported( |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3911 | const std::optional<std::string>& uuid, bool* _aidl_return) { |
| 3912 | *_aidl_return = IsQuotaSupported(uuid.value_or("")); |
Jeff Sharkey | 325b8c9 | 2017-02-21 10:00:53 -0700 | [diff] [blame] | 3913 | return ok(); |
| 3914 | } |
| 3915 | |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 3916 | binder::Status InstalldNativeService::prepareAppProfile(const std::string& packageName, |
| 3917 | int32_t userId, int32_t appId, const std::string& profileName, const std::string& codePath, |
Jooyung Han | 149be4a | 2020-01-23 12:45:10 +0900 | [diff] [blame] | 3918 | const std::optional<std::string>& dexMetadata, bool* _aidl_return) { |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 3919 | ENFORCE_UID(AID_SYSTEM); |
Steven Moreland | 3e543b9 | 2023-09-07 20:08:15 +0000 | [diff] [blame] | 3920 | ENFORCE_VALID_USER_OR_NULL(userId); |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 3921 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 3922 | CHECK_ARGUMENT_PATH(codePath); |
Alex Buynytskyy | 4fef886 | 2021-11-21 16:09:33 -0800 | [diff] [blame] | 3923 | LOCK_PACKAGE_USER(); |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 3924 | |
| 3925 | *_aidl_return = prepare_app_profile(packageName, userId, appId, profileName, codePath, |
| 3926 | dexMetadata); |
| 3927 | return ok(); |
| 3928 | } |
| 3929 | |
Narayan Kamath | 28ab93b | 2019-05-15 18:29:44 +0100 | [diff] [blame] | 3930 | binder::Status InstalldNativeService::migrateLegacyObbData() { |
| 3931 | ENFORCE_UID(AID_SYSTEM); |
| 3932 | // NOTE: The lint warning doesn't apply to the use of system(3) with |
| 3933 | // absolute parse and no command line arguments. |
Cole Faust | f6cd58a | 2022-09-01 17:34:36 -0700 | [diff] [blame] | 3934 | if (system("/system/bin/migrate_legacy_obb_data") != 0) { // NOLINT(cert-env33-c) |
Narayan Kamath | 28ab93b | 2019-05-15 18:29:44 +0100 | [diff] [blame] | 3935 | LOG(ERROR) << "Unable to migrate legacy obb data"; |
| 3936 | } |
| 3937 | |
| 3938 | return ok(); |
| 3939 | } |
| 3940 | |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 3941 | binder::Status InstalldNativeService::cleanupInvalidPackageDirs( |
| 3942 | const std::optional<std::string>& uuid, int32_t userId, int32_t flags) { |
Steven Moreland | 3694536 | 2023-07-05 20:29:10 +0000 | [diff] [blame] | 3943 | ENFORCE_VALID_USER(userId); |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 3944 | const char* uuid_cstr = uuid ? uuid->c_str() : nullptr; |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 3945 | |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 3946 | if (flags & FLAG_STORAGE_CE) { |
| 3947 | auto ce_path = create_data_user_ce_path(uuid_cstr, userId); |
| 3948 | cleanup_invalid_package_dirs_under_path(ce_path); |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 3949 | auto sdksandbox_ce_path = |
| 3950 | create_data_misc_sdk_sandbox_path(uuid_cstr, /*isCeData=*/true, userId); |
| 3951 | cleanup_invalid_package_dirs_under_path(sdksandbox_ce_path); |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 3952 | } |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 3953 | |
| 3954 | if (flags & FLAG_STORAGE_DE) { |
| 3955 | auto de_path = create_data_user_de_path(uuid_cstr, userId); |
| 3956 | cleanup_invalid_package_dirs_under_path(de_path); |
Rishabh Singh | e84b0b1 | 2022-02-02 20:56:13 +0000 | [diff] [blame] | 3957 | auto sdksandbox_de_path = |
| 3958 | create_data_misc_sdk_sandbox_path(uuid_cstr, /*isCeData=*/false, userId); |
| 3959 | cleanup_invalid_package_dirs_under_path(sdksandbox_de_path); |
Alex Buynytskyy | 58a7309 | 2022-02-14 13:15:53 -0800 | [diff] [blame] | 3960 | } |
| 3961 | |
Alex Buynytskyy | 038a19b | 2022-02-09 19:51:52 -0800 | [diff] [blame] | 3962 | return ok(); |
| 3963 | } |
| 3964 | |
Jiakai Zhang | f515a1b | 2022-02-23 18:33:26 +0000 | [diff] [blame] | 3965 | binder::Status InstalldNativeService::getOdexVisibility( |
| 3966 | const std::string& packageName, const std::string& apkPath, |
| 3967 | const std::string& instructionSet, const std::optional<std::string>& outputPath, |
| 3968 | int32_t* _aidl_return) { |
| 3969 | ENFORCE_UID(AID_SYSTEM); |
| 3970 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 3971 | CHECK_ARGUMENT_PATH(apkPath); |
| 3972 | CHECK_ARGUMENT_PATH(outputPath); |
| 3973 | LOCK_PACKAGE(); |
| 3974 | |
| 3975 | const char* apk_path = apkPath.c_str(); |
| 3976 | const char* instruction_set = instructionSet.c_str(); |
| 3977 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; |
| 3978 | |
| 3979 | *_aidl_return = get_odex_visibility(apk_path, instruction_set, oat_dir); |
| 3980 | return *_aidl_return == -1 ? error() : ok(); |
| 3981 | } |
| 3982 | |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 3983 | // Creates an auth token to be used in enableFsverity. This token is really to store a proof that |
| 3984 | // the caller can write to a file, represented by the authFd. Effectively, system_server as the |
| 3985 | // attacker-in-the-middle cannot enable fs-verity on arbitrary app files. If the FD is not writable, |
| 3986 | // return null. |
| 3987 | // |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 3988 | // app process uid is passed for additional ownership check, such that one app can not be |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 3989 | // authenticated for another app's file. These parameters are assumed trusted for this purpose of |
| 3990 | // consistency check. |
| 3991 | // |
| 3992 | // Notably, creating the token allows us to manage the writable FD easily during enableFsverity. |
| 3993 | // Since enabling fs-verity to a file requires no outstanding writable FD, passing the authFd to the |
| 3994 | // server allows the server to hold the only reference (as long as the client app doesn't). |
| 3995 | binder::Status InstalldNativeService::createFsveritySetupAuthToken( |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 3996 | const ParcelFileDescriptor& authFd, int32_t uid, |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 3997 | sp<IFsveritySetupAuthToken>* _aidl_return) { |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 3998 | CHECK_ARGUMENT_APP_ID(multiuser_get_app_id(uid)); |
| 3999 | ENFORCE_VALID_USER(multiuser_get_user_id(uid)); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4000 | |
| 4001 | auto token = sp<FsveritySetupAuthToken>::make(); |
Victor Hsieh | c72e4d7 | 2024-01-09 15:46:00 -0800 | [diff] [blame] | 4002 | binder::Status status = token->authenticate(authFd, uid); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4003 | if (!status.isOk()) { |
| 4004 | return status; |
| 4005 | } |
| 4006 | *_aidl_return = token; |
| 4007 | return ok(); |
| 4008 | } |
| 4009 | |
| 4010 | // Enables fs-verity for filePath, which must be an absolute path and the same inode as in the auth |
| 4011 | // token previously returned from createFsveritySetupAuthToken, and owned by the app uid. As |
| 4012 | // installd is more privileged than its client / system server, we attempt to limit what a |
| 4013 | // (compromised) client can do. |
| 4014 | // |
| 4015 | // The reason for this app request to go through installd is to avoid exposing a risky area (PKCS#7 |
| 4016 | // signature verification) in the kernel to the app as an attack surface (it can't be system server |
| 4017 | // because it can't override DAC and manipulate app files). Note that we should be able to drop |
| 4018 | // these hops and simply the app calls the ioctl, once all upgrading devices run with a kernel |
| 4019 | // without fs-verity built-in signature (https://r.android.com/2650402). |
| 4020 | binder::Status InstalldNativeService::enableFsverity(const sp<IFsveritySetupAuthToken>& authToken, |
| 4021 | const std::string& filePath, |
| 4022 | const std::string& packageName, |
| 4023 | int32_t* _aidl_return) { |
| 4024 | ENFORCE_UID(AID_SYSTEM); |
| 4025 | CHECK_ARGUMENT_PATH(filePath); |
| 4026 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 4027 | LOCK_PACKAGE(); |
| 4028 | if (authToken == nullptr) { |
| 4029 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Received a null auth token"); |
| 4030 | } |
| 4031 | |
Victor Hsieh | c4cff3d | 2024-01-17 09:02:47 -0800 | [diff] [blame^] | 4032 | // Authenticate to check the targeting file is the same inode as the authFd. With O_PATH, we |
| 4033 | // prevent a malicious client from blocking installd by providing a path to FIFO. After the |
| 4034 | // authentication, the actual open is safe. |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4035 | sp<IBinder> authTokenBinder = IInterface::asBinder(authToken)->localBinder(); |
| 4036 | if (authTokenBinder == nullptr) { |
| 4037 | return exception(binder::Status::EX_SECURITY, "Received a non-local auth token"); |
| 4038 | } |
Victor Hsieh | c4cff3d | 2024-01-17 09:02:47 -0800 | [diff] [blame^] | 4039 | unique_fd pathFd(open(filePath.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW | O_PATH)); |
| 4040 | // Returns a constant errno to avoid one app probing file existence of the others, before the |
| 4041 | // authentication is done. |
| 4042 | const int kFixedErrno = EPERM; |
| 4043 | if (pathFd.get() < 0) { |
| 4044 | PLOG(DEBUG) << "Failed to open the path"; |
| 4045 | *_aidl_return = kFixedErrno; |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4046 | return ok(); |
| 4047 | } |
Victor Hsieh | c4cff3d | 2024-01-17 09:02:47 -0800 | [diff] [blame^] | 4048 | std::string procFdPath(StringPrintf("/proc/self/fd/%d", pathFd.get())); |
| 4049 | struct stat stFromPath; |
| 4050 | if (stat(procFdPath.c_str(), &stFromPath) < 0) { |
| 4051 | PLOG(DEBUG) << "Failed to stat proc fd " << pathFd.get() << " -> " << filePath; |
| 4052 | *_aidl_return = kFixedErrno; |
| 4053 | return ok(); |
| 4054 | } |
| 4055 | auto authTokenInstance = sp<FsveritySetupAuthToken>::cast(authTokenBinder); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4056 | if (!authTokenInstance->isSameStat(stFromPath)) { |
| 4057 | LOG(DEBUG) << "FD authentication failed"; |
Victor Hsieh | c4cff3d | 2024-01-17 09:02:47 -0800 | [diff] [blame^] | 4058 | *_aidl_return = kFixedErrno; |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4059 | return ok(); |
| 4060 | } |
| 4061 | |
Victor Hsieh | c4cff3d | 2024-01-17 09:02:47 -0800 | [diff] [blame^] | 4062 | unique_fd rfd(open(procFdPath.c_str(), O_RDONLY | O_CLOEXEC)); |
Victor Hsieh | c7ab564 | 2023-07-28 15:07:55 -0700 | [diff] [blame] | 4063 | fsverity_enable_arg arg = {}; |
| 4064 | arg.version = 1; |
| 4065 | arg.hash_algorithm = FS_VERITY_HASH_ALG_SHA256; |
| 4066 | arg.block_size = 4096; |
| 4067 | if (ioctl(rfd.get(), FS_IOC_ENABLE_VERITY, &arg) < 0) { |
| 4068 | *_aidl_return = errno; |
| 4069 | } else { |
| 4070 | *_aidl_return = 0; |
| 4071 | } |
| 4072 | return ok(); |
| 4073 | } |
| 4074 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 4075 | } // namespace installd |
| 4076 | } // namespace android |