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