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 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER |
| 20 | |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <errno.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 23 | #include <fstream> |
| 24 | #include <fts.h> |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 25 | #include <functional> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 27 | #include <regex> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 28 | #include <stdlib.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 29 | #include <string.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <sys/capability.h> |
| 31 | #include <sys/file.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/mman.h> |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 34 | #include <sys/mount.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 35 | #include <sys/resource.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 36 | #include <sys/stat.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 37 | #include <sys/statvfs.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 38 | #include <sys/types.h> |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 39 | #include <sys/wait.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 40 | #include <sys/xattr.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 41 | #include <unistd.h> |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 42 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 43 | #include <android-base/file.h> |
Andreas Gampe | afa58d1 | 2016-06-03 16:09:32 -0700 | [diff] [blame] | 44 | #include <android-base/logging.h> |
Jeff Sharkey | f29a3bc | 2018-01-08 10:40:19 -0700 | [diff] [blame] | 45 | #include <android-base/properties.h> |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 46 | #include <android-base/scopeguard.h> |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 47 | #include <android-base/stringprintf.h> |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 48 | #include <android-base/strings.h> |
Roland Levillain | 64b59cc | 2016-04-05 16:41:12 +0100 | [diff] [blame] | 49 | #include <android-base/unique_fd.h> |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 50 | #include <cutils/ashmem.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 51 | #include <cutils/fs.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 52 | #include <cutils/properties.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 53 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 54 | #include <log/log.h> // TODO: Move everything to base/logging. |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 55 | #include <logwrap/logwrap.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 56 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 57 | #include <selinux/android.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 58 | #include <system/thread_defs.h> |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 59 | #include <utils/Trace.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 60 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 61 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 62 | #include "globals.h" |
| 63 | #include "installd_deps.h" |
| 64 | #include "otapreopt_utils.h" |
| 65 | #include "utils.h" |
Eric Holk | 0ebbe0f | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 66 | #include "view_compiler.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 67 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 68 | #include "CacheTracker.h" |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 69 | #include "CrateManager.h" |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 70 | #include "MatchExtensionGen.h" |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 71 | #include "QuotaUtils.h" |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 72 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 73 | #ifndef LOG_TAG |
| 74 | #define LOG_TAG "installd" |
| 75 | #endif |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 76 | |
| 77 | using android::base::StringPrintf; |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 78 | using std::endl; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 79 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 80 | namespace android { |
| 81 | namespace installd { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 82 | |
Nikita Ioffe | 8e4d346 | 2019-01-21 17:56:03 +0000 | [diff] [blame] | 83 | // An uuid used in unit tests. |
| 84 | static constexpr const char* kTestUuid = "TEST"; |
| 85 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 86 | static constexpr const mode_t kRollbackFolderMode = 0700; |
| 87 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 88 | static constexpr const char* kCpPath = "/system/bin/cp"; |
| 89 | static constexpr const char* kXattrDefault = "user.default"; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 90 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 91 | static constexpr const char* kDataMirrorCePath = "/data_mirror/data_ce"; |
| 92 | static constexpr const char* kDataMirrorDePath = "/data_mirror/data_de"; |
| 93 | |
Janis Danisevskis | 40bd3ef | 2016-06-07 10:31:27 -0700 | [diff] [blame] | 94 | static constexpr const int MIN_RESTRICTED_HOME_SDK_VERSION = 24; // > M |
Janis Danisevskis | eecb2d2 | 2016-01-12 14:45:55 +0000 | [diff] [blame] | 95 | |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 96 | static constexpr const char* PKG_LIB_POSTFIX = "/lib"; |
| 97 | static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; |
| 98 | static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; |
| 99 | |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 100 | // fsverity assumes the page size is always 4096. If not, the feature can not be |
| 101 | // enabled. |
| 102 | static constexpr int kVerityPageSize = 4096; |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 103 | static constexpr size_t kSha256Size = 32; |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 104 | static constexpr const char* kPropApkVerityMode = "ro.apk_verity.mode"; |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 105 | static constexpr const char* kFuseProp = "persist.sys.fuse"; |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 106 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Property to control if app data isolation is enabled. |
| 109 | */ |
| 110 | static constexpr const char* kAppDataIsolationEnabledProperty = "persist.zygote.app_data_isolation"; |
| 111 | |
| 112 | static std::atomic<bool> sAppDataIsolationEnabled(false); |
| 113 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 114 | namespace { |
| 115 | |
| 116 | constexpr const char* kDump = "android.permission.DUMP"; |
| 117 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 118 | static binder::Status ok() { |
| 119 | return binder::Status::ok(); |
| 120 | } |
| 121 | |
| 122 | static binder::Status exception(uint32_t code, const std::string& msg) { |
Jeff Sharkey | 8e9bf35 | 2018-02-27 11:40:45 -0700 | [diff] [blame] | 123 | LOG(ERROR) << msg << " (" << code << ")"; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 124 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 125 | } |
| 126 | |
| 127 | static binder::Status error() { |
| 128 | return binder::Status::fromServiceSpecificError(errno); |
| 129 | } |
| 130 | |
| 131 | static binder::Status error(const std::string& msg) { |
| 132 | PLOG(ERROR) << msg; |
| 133 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 134 | } |
| 135 | |
| 136 | static binder::Status error(uint32_t code, const std::string& msg) { |
| 137 | LOG(ERROR) << msg << " (" << code << ")"; |
| 138 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); |
| 139 | } |
| 140 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 141 | binder::Status checkPermission(const char* permission) { |
| 142 | pid_t pid; |
| 143 | uid_t uid; |
| 144 | |
| 145 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), |
| 146 | reinterpret_cast<int32_t*>(&uid))) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 147 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 148 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 149 | return exception(binder::Status::EX_SECURITY, |
| 150 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | binder::Status checkUid(uid_t expectedUid) { |
| 155 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 156 | if (uid == expectedUid || uid == AID_ROOT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 157 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 158 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 159 | return exception(binder::Status::EX_SECURITY, |
| 160 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { |
| 165 | if (!uuid || is_valid_filename(*uuid)) { |
| 166 | return ok(); |
| 167 | } else { |
| 168 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 169 | StringPrintf("UUID %s is malformed", uuid->c_str())); |
| 170 | } |
| 171 | } |
| 172 | |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 173 | binder::Status checkArgumentUuidTestOrNull(const std::unique_ptr<std::string>& uuid) { |
| 174 | if (!uuid || strcmp(uuid->c_str(), kTestUuid) == 0) { |
| 175 | return ok(); |
| 176 | } else { |
| 177 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 178 | StringPrintf("UUID must be null or \"%s\", got: %s", kTestUuid, uuid->c_str())); |
| 179 | } |
| 180 | } |
| 181 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 182 | binder::Status checkArgumentPackageName(const std::string& packageName) { |
Greg Kaiser | 6d758fc | 2019-03-26 08:55:32 -0700 | [diff] [blame] | 183 | if (is_valid_package_name(packageName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 184 | return ok(); |
| 185 | } else { |
| 186 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 187 | StringPrintf("Package name %s is malformed", packageName.c_str())); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 191 | binder::Status checkArgumentPath(const std::string& path) { |
| 192 | if (path.empty()) { |
| 193 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, "Missing path"); |
| 194 | } |
| 195 | if (path[0] != '/') { |
| 196 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 197 | StringPrintf("Path %s is relative", path.c_str())); |
| 198 | } |
| 199 | if ((path + '/').find("/../") != std::string::npos) { |
| 200 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 201 | StringPrintf("Path %s is shady", path.c_str())); |
| 202 | } |
| 203 | for (const char& c : path) { |
| 204 | if (c == '\0' || c == '\n') { |
| 205 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 206 | StringPrintf("Path %s is malformed", path.c_str())); |
| 207 | } |
| 208 | } |
| 209 | return ok(); |
| 210 | } |
| 211 | |
| 212 | binder::Status checkArgumentPath(const std::unique_ptr<std::string>& path) { |
| 213 | if (path) { |
| 214 | return checkArgumentPath(*path); |
| 215 | } else { |
| 216 | return ok(); |
| 217 | } |
| 218 | } |
| 219 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 220 | #define ENFORCE_UID(uid) { \ |
| 221 | binder::Status status = checkUid((uid)); \ |
| 222 | if (!status.isOk()) { \ |
| 223 | return status; \ |
| 224 | } \ |
| 225 | } |
| 226 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 227 | #define CHECK_ARGUMENT_UUID(uuid) { \ |
| 228 | binder::Status status = checkArgumentUuid((uuid)); \ |
| 229 | if (!status.isOk()) { \ |
| 230 | return status; \ |
| 231 | } \ |
| 232 | } |
| 233 | |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 234 | #define CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(uuid) { \ |
| 235 | auto status = checkArgumentUuidTestOrNull(uuid); \ |
| 236 | if (!status.isOk()) { \ |
| 237 | return status; \ |
| 238 | } \ |
| 239 | } \ |
| 240 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 241 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ |
| 242 | binder::Status status = \ |
| 243 | checkArgumentPackageName((packageName)); \ |
| 244 | if (!status.isOk()) { \ |
| 245 | return status; \ |
| 246 | } \ |
| 247 | } |
| 248 | |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 249 | #define CHECK_ARGUMENT_PATH(path) { \ |
| 250 | binder::Status status = checkArgumentPath((path)); \ |
| 251 | if (!status.isOk()) { \ |
| 252 | return status; \ |
| 253 | } \ |
| 254 | } |
| 255 | |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 256 | #define ASSERT_PAGE_SIZE_4K() { \ |
| 257 | if (getpagesize() != kVerityPageSize) { \ |
Victor Hsieh | 7ebd513 | 2018-01-23 07:52:17 -0800 | [diff] [blame] | 258 | return error("FSVerity only supports 4K pages"); \ |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 259 | } \ |
| 260 | } |
| 261 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 262 | } // namespace |
| 263 | |
| 264 | status_t InstalldNativeService::start() { |
| 265 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 266 | status_t ret = BinderService<InstalldNativeService>::publish(); |
| 267 | if (ret != android::OK) { |
| 268 | return ret; |
| 269 | } |
| 270 | sp<ProcessState> ps(ProcessState::self()); |
| 271 | ps->startThreadPool(); |
| 272 | ps->giveThreadPoolName(); |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 273 | sAppDataIsolationEnabled = android::base::GetBoolProperty( |
| 274 | kAppDataIsolationEnabledProperty, false); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 275 | return android::OK; |
| 276 | } |
| 277 | |
| 278 | status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 279 | auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd)); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 280 | const binder::Status dump_permission = checkPermission(kDump); |
| 281 | if (!dump_permission.isOk()) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 282 | out << dump_permission.toString8() << endl; |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 283 | return PERMISSION_DENIED; |
| 284 | } |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 285 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 286 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 287 | out << "installd is happy!" << endl; |
| 288 | |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 289 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 290 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 291 | out << endl << "Storage mounts:" << endl; |
| 292 | for (const auto& n : mStorageMounts) { |
| 293 | out << " " << n.first << " = " << n.second << endl; |
| 294 | } |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 295 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 296 | |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 297 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 298 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 299 | out << endl << "Per-UID cache quotas:" << endl; |
| 300 | for (const auto& n : mCacheQuotas) { |
| 301 | out << " " << n.first << " = " << n.second << endl; |
| 302 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 305 | out << endl; |
| 306 | out.flush(); |
| 307 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 308 | return NO_ERROR; |
| 309 | } |
| 310 | |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 311 | /** |
| 312 | * Perform restorecon of the given path, but only perform recursive restorecon |
| 313 | * if the label of that top-level file actually changed. This can save us |
| 314 | * significant time by avoiding no-op traversals of large filesystem trees. |
| 315 | */ |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 316 | static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, |
| 317 | bool existing) { |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 318 | int res = 0; |
| 319 | char* before = nullptr; |
| 320 | char* after = nullptr; |
| 321 | |
| 322 | // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by |
| 323 | // libselinux. Not needed here. |
| 324 | |
Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 325 | if (lgetfilecon(path.c_str(), &before) < 0) { |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 326 | PLOG(ERROR) << "Failed before getfilecon for " << path; |
| 327 | goto fail; |
| 328 | } |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 329 | 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] | 330 | PLOG(ERROR) << "Failed top-level restorecon for " << path; |
| 331 | goto fail; |
| 332 | } |
Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 333 | if (lgetfilecon(path.c_str(), &after) < 0) { |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 334 | PLOG(ERROR) << "Failed after getfilecon for " << path; |
| 335 | goto fail; |
| 336 | } |
| 337 | |
| 338 | // If the initial top-level restorecon above changed the label, then go |
| 339 | // back and restorecon everything recursively |
| 340 | if (strcmp(before, after)) { |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 341 | if (existing) { |
| 342 | LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " |
| 343 | << path << "; running recursive restorecon"; |
| 344 | } |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 345 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 346 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { |
| 347 | PLOG(ERROR) << "Failed recursive restorecon for " << path; |
| 348 | goto fail; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | goto done; |
| 353 | fail: |
| 354 | res = -1; |
| 355 | done: |
| 356 | free(before); |
| 357 | free(after); |
| 358 | return res; |
| 359 | } |
| 360 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 361 | static int restorecon_app_data_lazy(const std::string& parent, const char* name, |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 362 | const std::string& seInfo, uid_t uid, bool existing) { |
| 363 | return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, |
| 364 | existing); |
Jeff Sharkey | 8567ebf | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 365 | } |
| 366 | |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 367 | static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 368 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { |
| 369 | PLOG(ERROR) << "Failed to prepare " << path; |
| 370 | return -1; |
| 371 | } |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 375 | static bool prepare_app_profile_dir(const std::string& packageName, int32_t appId, int32_t userId) { |
| 376 | if (!property_get_bool("dalvik.vm.usejitprofiles", false)) { |
| 377 | return true; |
| 378 | } |
| 379 | |
| 380 | int32_t uid = multiuser_get_uid(userId, appId); |
| 381 | int shared_app_gid = multiuser_get_shared_gid(userId, appId); |
| 382 | if (shared_app_gid == -1) { |
| 383 | // TODO(calin): this should no longer be possible but do not continue if we don't get |
| 384 | // a valid shared gid. |
| 385 | PLOG(WARNING) << "Invalid shared_app_gid for " << packageName; |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | const std::string profile_dir = |
| 390 | create_primary_current_profile_package_dir_path(userId, packageName); |
| 391 | // read-write-execute only for the app user. |
| 392 | if (fs_prepare_dir_strict(profile_dir.c_str(), 0700, uid, uid) != 0) { |
| 393 | PLOG(ERROR) << "Failed to prepare " << profile_dir; |
| 394 | return false; |
| 395 | } |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 396 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 397 | const std::string ref_profile_path = |
| 398 | create_primary_reference_profile_package_dir_path(packageName); |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 399 | |
| 400 | // Prepare the reference profile directory. Note that we use the non strict version of |
| 401 | // fs_prepare_dir. This will fix the permission and the ownership to the correct values. |
| 402 | // This is particularly important given that in O there were some fixes for how the |
| 403 | // shared_app_gid is computed. |
| 404 | // |
| 405 | // Note that by the time we get here we know that we are using a correct uid (otherwise |
| 406 | // prepare_app_dir and the above fs_prepare_file_strict which check the uid). So we |
| 407 | // are sure that the gid being used belongs to the owning app and not someone else. |
| 408 | // |
| 409 | // dex2oat/profman runs under the shared app gid and it needs to read/write reference profiles. |
| 410 | 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] | 411 | PLOG(ERROR) << "Failed to prepare " << ref_profile_path; |
| 412 | return false; |
| 413 | } |
Calin Juravle | 6f06eb6 | 2017-11-28 18:44:53 -0800 | [diff] [blame] | 414 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 415 | return true; |
| 416 | } |
| 417 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 418 | binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, |
| 419 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 420 | const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 421 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 422 | CHECK_ARGUMENT_UUID(uuid); |
| 423 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 424 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 425 | |
| 426 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 427 | const char* pkgname = packageName.c_str(); |
| 428 | |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 429 | // Assume invalid inode unless filled in below |
| 430 | if (_aidl_return != nullptr) *_aidl_return = -1; |
| 431 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 432 | int32_t uid = multiuser_get_uid(userId, appId); |
| 433 | int32_t cacheGid = multiuser_get_cache_gid(userId, appId); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 434 | mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; |
| 435 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame] | 436 | // If UID doesn't have a specific cache GID, use UID value |
| 437 | if (cacheGid == -1) { |
| 438 | cacheGid = uid; |
| 439 | } |
| 440 | |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 441 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 442 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 443 | bool existing = (access(path.c_str(), F_OK) == 0); |
| 444 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 445 | if (prepare_app_dir(path, targetMode, uid) || |
| 446 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || |
| 447 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 448 | return error("Failed to prepare " + path); |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 452 | if (restorecon_app_data_lazy(path, seInfo, uid, existing) || |
| 453 | restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || |
| 454 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 455 | return error("Failed to restorecon " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 456 | } |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 457 | |
| 458 | // Remember inode numbers of cache directories so that we can clear |
| 459 | // contents while CE storage is locked |
| 460 | if (write_path_inode(path, "cache", kXattrInodeCache) || |
| 461 | write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 462 | return error("Failed to write_path_inode for " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 463 | } |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 464 | |
| 465 | // And return the CE inode of the top-level data directory so we can |
| 466 | // clear contents while CE storage is locked |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 467 | if (_aidl_return != nullptr) { |
| 468 | ino_t result; |
| 469 | if (get_path_inode(path, &result) != 0) { |
| 470 | return error("Failed to get_path_inode for " + path); |
| 471 | } |
| 472 | *_aidl_return = static_cast<uint64_t>(result); |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 473 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 475 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 476 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 477 | bool existing = (access(path.c_str(), F_OK) == 0); |
| 478 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 479 | if (prepare_app_dir(path, targetMode, uid) || |
| 480 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || |
| 481 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 482 | return error("Failed to prepare " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 483 | } |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 484 | |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 485 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 7b6c840 | 2017-04-15 12:09:22 -0600 | [diff] [blame] | 486 | if (restorecon_app_data_lazy(path, seInfo, uid, existing) || |
| 487 | restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || |
| 488 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 489 | return error("Failed to restorecon " + path); |
Jeff Sharkey | 22f6fd5 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 490 | } |
| 491 | |
Calin Juravle | d2affb8 | 2017-11-28 17:41:43 -0800 | [diff] [blame] | 492 | if (!prepare_app_profile_dir(packageName, appId, userId)) { |
| 493 | return error("Failed to prepare profiles for " + packageName); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 494 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 495 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 496 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 499 | binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, |
| 500 | const std::string& packageName, int32_t userId, int32_t flags) { |
| 501 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 502 | CHECK_ARGUMENT_UUID(uuid); |
| 503 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 504 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 505 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 506 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 507 | const char* pkgname = packageName.c_str(); |
| 508 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 509 | // This method only exists to upgrade system apps that have requested |
| 510 | // forceDeviceEncrypted, so their default storage always lives in a |
| 511 | // consistent location. This only works on non-FBE devices, since we |
| 512 | // never want to risk exposing data on a device with real CE/DE storage. |
| 513 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 514 | auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 515 | auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 516 | |
| 517 | // If neither directory is marked as default, assume CE is default |
| 518 | if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 |
| 519 | && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 520 | if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 521 | return error("Failed to mark default storage " + ce_path); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
| 525 | // Migrate default data location if needed |
| 526 | auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; |
| 527 | auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; |
| 528 | |
| 529 | if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 530 | LOG(WARNING) << "Requested default storage " << target |
| 531 | << " is not active; migrating from " << source; |
| 532 | if (delete_dir_contents_and_dir(target) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 533 | return error("Failed to delete " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 534 | } |
| 535 | if (rename(source.c_str(), target.c_str()) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 536 | return error("Failed to rename " + source + " to " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 540 | return ok(); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 543 | |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 544 | binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName, |
| 545 | const std::string& profileName) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 546 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 547 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 548 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 549 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 550 | binder::Status res = ok(); |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 551 | if (!clear_primary_reference_profile(packageName, profileName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 552 | res = error("Failed to clear reference profile for " + packageName); |
| 553 | } |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 554 | if (!clear_primary_current_profiles(packageName, profileName)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 555 | res = error("Failed to clear current profiles for " + packageName); |
| 556 | } |
| 557 | return res; |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 560 | binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, |
| 561 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 562 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 563 | CHECK_ARGUMENT_UUID(uuid); |
| 564 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 565 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 566 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 567 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 568 | const char* pkgname = packageName.c_str(); |
| 569 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 570 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 571 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 572 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 573 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 574 | path = read_path_inode(path, "cache", kXattrInodeCache); |
| 575 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 576 | path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 577 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 578 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 579 | if (delete_dir_contents(path) != 0) { |
| 580 | res = error("Failed to delete contents of " + path); |
Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 581 | } else if ((flags & (FLAG_CLEAR_CACHE_ONLY | FLAG_CLEAR_CODE_CACHE_ONLY)) == 0) { |
| 582 | remove_path_xattr(path, kXattrInodeCache); |
| 583 | remove_path_xattr(path, kXattrInodeCodeCache); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 584 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 587 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 1f6a7f1 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 588 | std::string suffix = ""; |
| 589 | bool only_cache = false; |
| 590 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 591 | suffix = CACHE_DIR_POSTFIX; |
| 592 | only_cache = true; |
| 593 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 594 | suffix = CODE_CACHE_DIR_POSTFIX; |
| 595 | only_cache = true; |
| 596 | } |
| 597 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 598 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 599 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 600 | if (delete_dir_contents(path) != 0) { |
| 601 | res = error("Failed to delete contents of " + path); |
| 602 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 603 | } |
| 604 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 605 | if (flags & FLAG_STORAGE_EXTERNAL) { |
| 606 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 607 | for (const auto& n : mStorageMounts) { |
| 608 | auto extPath = n.second; |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 609 | |
| 610 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
| 611 | std::regex re("^\\/mnt\\/pass_through\\/[0-9]+\\/emulated"); |
| 612 | if (std::regex_match(extPath, re)) { |
| 613 | extPath += "/" + std::to_string(userId); |
| 614 | } |
| 615 | } else { |
| 616 | if (n.first.compare(0, 14, "/mnt/media_rw/") != 0) { |
| 617 | extPath += StringPrintf("/%d", userId); |
| 618 | } else if (userId != 0) { |
| 619 | // TODO: support devices mounted under secondary users |
| 620 | continue; |
| 621 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 622 | } |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 623 | |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 624 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 625 | // Clear only cached data from shared storage |
| 626 | auto path = StringPrintf("%s/Android/data/%s/cache", extPath.c_str(), pkgname); |
| 627 | if (delete_dir_contents(path, true) != 0) { |
| 628 | res = error("Failed to delete contents of " + path); |
| 629 | } |
| 630 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 631 | // No code cache on shared storage |
| 632 | } else { |
| 633 | // Clear everything on shared storage |
| 634 | auto path = StringPrintf("%s/Android/data/%s", extPath.c_str(), pkgname); |
| 635 | if (delete_dir_contents(path, true) != 0) { |
| 636 | res = error("Failed to delete contents of " + path); |
| 637 | } |
| 638 | path = StringPrintf("%s/Android/media/%s", extPath.c_str(), pkgname); |
| 639 | if (delete_dir_contents(path, true) != 0) { |
| 640 | res = error("Failed to delete contents of " + path); |
| 641 | } |
Martijn Coenen | 24a9307 | 2020-01-08 10:10:43 +0100 | [diff] [blame] | 642 | // Note that we explicitly don't delete OBBs - those are only removed on |
| 643 | // app uninstall. |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 647 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 650 | static int destroy_app_reference_profile(const std::string& pkgname) { |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 651 | return delete_dir_contents_and_dir( |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 652 | create_primary_reference_profile_package_dir_path(pkgname), |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 653 | /*ignore_if_missing*/ true); |
| 654 | } |
| 655 | |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 656 | 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] | 657 | return delete_dir_contents_and_dir( |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 658 | create_primary_current_profile_package_dir_path(userid, pkgname), |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 659 | /*ignore_if_missing*/ true); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 662 | binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { |
| 663 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 664 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 665 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 666 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 667 | binder::Status res = ok(); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 668 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); |
| 669 | for (auto user : users) { |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 670 | if (destroy_app_current_profiles(packageName, user) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 671 | res = error("Failed to destroy current profiles for " + packageName); |
| 672 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 673 | } |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 674 | if (destroy_app_reference_profile(packageName) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 675 | res = error("Failed to destroy reference profile for " + packageName); |
| 676 | } |
| 677 | return res; |
Calin Juravle | caa6b80 | 2016-03-22 14:15:52 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 680 | binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, |
| 681 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 682 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 683 | CHECK_ARGUMENT_UUID(uuid); |
| 684 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 685 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 686 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 687 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 688 | const char* pkgname = packageName.c_str(); |
| 689 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 690 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 691 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 692 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
| 693 | if (delete_dir_contents_and_dir(path) != 0) { |
| 694 | res = error("Failed to delete " + path); |
| 695 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 696 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 697 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 698 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 699 | if (delete_dir_contents_and_dir(path) != 0) { |
| 700 | res = error("Failed to delete " + path); |
| 701 | } |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 702 | destroy_app_current_profiles(packageName, userId); |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 703 | // TODO(calin): If the package is still installed by other users it's probably |
| 704 | // beneficial to keep the reference profile around. |
| 705 | // Verify if it's ok to do that. |
Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 706 | destroy_app_reference_profile(packageName); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 707 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 708 | if (flags & FLAG_STORAGE_EXTERNAL) { |
| 709 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 710 | for (const auto& n : mStorageMounts) { |
| 711 | auto extPath = n.second; |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 712 | |
| 713 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
| 714 | std::regex re("^\\/mnt\\/pass_through\\/[0-9]+\\/emulated"); |
| 715 | if (std::regex_match(extPath, re)) { |
| 716 | extPath += "/" + std::to_string(userId); |
| 717 | } |
| 718 | } else { |
| 719 | if (n.first.compare(0, 14, "/mnt/media_rw/") != 0) { |
| 720 | extPath += StringPrintf("/%d", userId); |
| 721 | } else if (userId != 0) { |
| 722 | // TODO: support devices mounted under secondary users |
| 723 | continue; |
| 724 | } |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 725 | } |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 726 | |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 727 | auto path = StringPrintf("%s/Android/data/%s", extPath.c_str(), pkgname); |
| 728 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 729 | res = error("Failed to delete contents of " + path); |
| 730 | } |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 731 | |
Jeff Sharkey | 6e17569 | 2019-05-14 14:55:33 -0600 | [diff] [blame] | 732 | path = StringPrintf("%s/Android/media/%s", extPath.c_str(), pkgname); |
| 733 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 734 | res = error("Failed to delete contents of " + path); |
| 735 | } |
| 736 | path = StringPrintf("%s/Android/obb/%s", extPath.c_str(), pkgname); |
| 737 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 738 | res = error("Failed to delete contents of " + path); |
| 739 | } |
| 740 | } |
| 741 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 742 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 745 | static gid_t get_cache_gid(uid_t uid) { |
| 746 | int32_t gid = multiuser_get_cache_gid(multiuser_get_user_id(uid), multiuser_get_app_id(uid)); |
| 747 | return (gid != -1) ? gid : uid; |
| 748 | } |
| 749 | |
| 750 | binder::Status InstalldNativeService::fixupAppData(const std::unique_ptr<std::string>& uuid, |
| 751 | int32_t flags) { |
| 752 | ENFORCE_UID(AID_SYSTEM); |
| 753 | CHECK_ARGUMENT_UUID(uuid); |
| 754 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 755 | |
| 756 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 757 | for (auto user : get_known_users(uuid_)) { |
| 758 | ATRACE_BEGIN("fixup user"); |
| 759 | FTS* fts; |
| 760 | FTSENT* p; |
Jeff Sharkey | d9ff77f | 2017-04-14 18:54:49 -0600 | [diff] [blame] | 761 | auto ce_path = create_data_user_ce_path(uuid_, user); |
| 762 | auto de_path = create_data_user_de_path(uuid_, user); |
| 763 | 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] | 764 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 765 | return error("Failed to fts_open"); |
| 766 | } |
| 767 | while ((p = fts_read(fts)) != nullptr) { |
| 768 | if (p->fts_info == FTS_D && p->fts_level == 1) { |
| 769 | // Track down inodes of cache directories |
| 770 | uint64_t raw = 0; |
| 771 | ino_t inode_cache = 0; |
| 772 | ino_t inode_code_cache = 0; |
| 773 | if (getxattr(p->fts_path, kXattrInodeCache, &raw, sizeof(raw)) == sizeof(raw)) { |
| 774 | inode_cache = raw; |
| 775 | } |
| 776 | if (getxattr(p->fts_path, kXattrInodeCodeCache, &raw, sizeof(raw)) == sizeof(raw)) { |
| 777 | inode_code_cache = raw; |
| 778 | } |
| 779 | |
| 780 | // Figure out expected GID of each child |
| 781 | FTSENT* child = fts_children(fts, 0); |
| 782 | while (child != nullptr) { |
| 783 | if ((child->fts_statp->st_ino == inode_cache) |
| 784 | || (child->fts_statp->st_ino == inode_code_cache) |
| 785 | || !strcmp(child->fts_name, "cache") |
| 786 | || !strcmp(child->fts_name, "code_cache")) { |
| 787 | child->fts_number = get_cache_gid(p->fts_statp->st_uid); |
| 788 | } else { |
| 789 | child->fts_number = p->fts_statp->st_uid; |
| 790 | } |
| 791 | child = child->fts_link; |
| 792 | } |
| 793 | } else if (p->fts_level >= 2) { |
| 794 | if (p->fts_level > 2) { |
| 795 | // Inherit GID from parent once we're deeper into tree |
| 796 | p->fts_number = p->fts_parent->fts_number; |
| 797 | } |
| 798 | |
| 799 | uid_t uid = p->fts_parent->fts_statp->st_uid; |
| 800 | gid_t cache_gid = get_cache_gid(uid); |
| 801 | gid_t expected = p->fts_number; |
| 802 | gid_t actual = p->fts_statp->st_gid; |
| 803 | if (actual == expected) { |
| 804 | #if FIXUP_DEBUG |
| 805 | LOG(DEBUG) << "Ignoring " << p->fts_path << " with expected GID " << expected; |
| 806 | #endif |
| 807 | if (!(flags & FLAG_FORCE)) { |
| 808 | fts_set(fts, p, FTS_SKIP); |
| 809 | } |
| 810 | } else if ((actual == uid) || (actual == cache_gid)) { |
| 811 | // Only consider fixing up when current GID belongs to app |
| 812 | if (p->fts_info != FTS_D) { |
| 813 | LOG(INFO) << "Fixing " << p->fts_path << " with unexpected GID " << actual |
| 814 | << " instead of " << expected; |
| 815 | } |
| 816 | switch (p->fts_info) { |
| 817 | case FTS_DP: |
| 818 | // If we're moving towards cache GID, we need to set S_ISGID |
| 819 | if (expected == cache_gid) { |
| 820 | if (chmod(p->fts_path, 02771) != 0) { |
| 821 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 822 | } |
| 823 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 824 | [[fallthrough]]; // also set GID |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 825 | case FTS_F: |
| 826 | if (chown(p->fts_path, -1, expected) != 0) { |
| 827 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 828 | } |
| 829 | break; |
| 830 | case FTS_SL: |
| 831 | case FTS_SLNONE: |
| 832 | if (lchown(p->fts_path, -1, expected) != 0) { |
| 833 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 834 | } |
| 835 | break; |
| 836 | } |
| 837 | } else { |
| 838 | // Ignore all other GID transitions, since they're kinda shady |
| 839 | LOG(WARNING) << "Ignoring " << p->fts_path << " with unexpected GID " << actual |
| 840 | << " instead of " << expected; |
Jeff Sharkey | 6b63b91 | 2017-09-21 15:41:37 -0600 | [diff] [blame] | 841 | if (!(flags & FLAG_FORCE)) { |
| 842 | fts_set(fts, p, FTS_SKIP); |
| 843 | } |
Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 844 | } |
| 845 | } |
| 846 | } |
| 847 | fts_close(fts); |
| 848 | ATRACE_END(); |
| 849 | } |
| 850 | return ok(); |
| 851 | } |
| 852 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 853 | static int32_t copy_directory_recursive(const char* from, const char* to) { |
| 854 | char *argv[] = { |
| 855 | (char*) kCpPath, |
| 856 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 857 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 858 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 859 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 860 | (char*) "-d", /* don't dereference symlinks */ |
| 861 | (char*) from, |
| 862 | (char*) to |
| 863 | }; |
| 864 | |
| 865 | LOG(DEBUG) << "Copying " << from << " to " << to; |
Tom Cherry | e79cde5 | 2019-09-25 16:51:56 -0700 | [diff] [blame] | 866 | 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] | 867 | } |
| 868 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 869 | binder::Status InstalldNativeService::snapshotAppData( |
| 870 | const std::unique_ptr<std::string>& volumeUuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 871 | const std::string& packageName, int32_t user, int32_t snapshotId, |
| 872 | int32_t storageFlags, int64_t* _aidl_return) { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 873 | ENFORCE_UID(AID_SYSTEM); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 874 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 875 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 876 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 877 | |
| 878 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 879 | const char* package_name = packageName.c_str(); |
| 880 | |
| 881 | binder::Status res = ok(); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 882 | // Default result to 0, it will be populated with inode of ce data snapshot |
| 883 | // if FLAG_STORAGE_CE has been passed. |
| 884 | if (_aidl_return != nullptr) *_aidl_return = 0; |
| 885 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 886 | bool clear_ce_on_exit = false; |
| 887 | bool clear_de_on_exit = false; |
| 888 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 889 | auto deleter = [&clear_ce_on_exit, &clear_de_on_exit, &volume_uuid, &user, &package_name, |
| 890 | &snapshotId] { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 891 | if (clear_de_on_exit) { |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 892 | auto to = create_data_misc_de_rollback_package_path(volume_uuid, user, snapshotId, |
| 893 | package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 894 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
| 895 | LOG(WARNING) << "Failed to delete app data snapshot: " << to; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (clear_ce_on_exit) { |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 900 | auto to = create_data_misc_ce_rollback_package_path(volume_uuid, user, snapshotId, |
| 901 | package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 902 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
| 903 | LOG(WARNING) << "Failed to delete app data snapshot: " << to; |
| 904 | } |
| 905 | } |
| 906 | }; |
| 907 | |
| 908 | auto scope_guard = android::base::make_scope_guard(deleter); |
| 909 | |
| 910 | // The app may not have any data at all, in which case it's OK to skip here. |
| 911 | auto from_ce = create_data_user_ce_package_path(volume_uuid, user, package_name); |
| 912 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 913 | LOG(INFO) << "Missing source " << from_ce; |
| 914 | return ok(); |
| 915 | } |
| 916 | |
Nikita Ioffe | 900b3c2 | 2019-01-23 14:32:48 +0000 | [diff] [blame] | 917 | // ce_data_inode is not needed when FLAG_CLEAR_CACHE_ONLY is set. |
| 918 | binder::Status clear_cache_result = clearAppData(volumeUuid, packageName, user, |
| 919 | storageFlags | FLAG_CLEAR_CACHE_ONLY, 0); |
| 920 | if (!clear_cache_result.isOk()) { |
| 921 | // It should be fine to continue snapshot if we for some reason failed |
| 922 | // to clear cache. |
| 923 | LOG(WARNING) << "Failed to clear cache of app " << packageName; |
| 924 | } |
| 925 | |
| 926 | // ce_data_inode is not needed when FLAG_CLEAR_CODE_CACHE_ONLY is set. |
| 927 | binder::Status clear_code_cache_result = clearAppData(volumeUuid, packageName, user, |
| 928 | storageFlags | FLAG_CLEAR_CODE_CACHE_ONLY, 0); |
| 929 | if (!clear_code_cache_result.isOk()) { |
| 930 | // It should be fine to continue snapshot if we for some reason failed |
| 931 | // to clear code_cache. |
| 932 | LOG(WARNING) << "Failed to clear code_cache of app " << packageName; |
| 933 | } |
| 934 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 935 | if (storageFlags & FLAG_STORAGE_DE) { |
| 936 | auto from = create_data_user_de_package_path(volume_uuid, user, package_name); |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 937 | auto to = create_data_misc_de_rollback_path(volume_uuid, user, snapshotId); |
| 938 | auto rollback_package_path = create_data_misc_de_rollback_package_path(volume_uuid, user, |
| 939 | snapshotId, package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 940 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 941 | int rc = create_dir_if_needed(to.c_str(), kRollbackFolderMode); |
| 942 | if (rc != 0) { |
| 943 | return error(rc, "Failed to create folder " + to); |
Nikita Ioffe | 835ae77 | 2019-01-23 16:32:13 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 946 | rc = delete_dir_contents(rollback_package_path, true /* ignore_if_missing */); |
| 947 | if (rc != 0) { |
| 948 | return error(rc, "Failed clearing existing snapshot " + rollback_package_path); |
| 949 | } |
| 950 | |
| 951 | rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 952 | if (rc != 0) { |
| 953 | res = error(rc, "Failed copying " + from + " to " + to); |
| 954 | clear_de_on_exit = true; |
| 955 | return res; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | if (storageFlags & FLAG_STORAGE_CE) { |
| 960 | auto from = create_data_user_ce_package_path(volume_uuid, user, package_name); |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 961 | auto to = create_data_misc_ce_rollback_path(volume_uuid, user, snapshotId); |
| 962 | auto rollback_package_path = create_data_misc_ce_rollback_package_path(volume_uuid, user, |
| 963 | snapshotId, package_name); |
Nikita Ioffe | 835ae77 | 2019-01-23 16:32:13 +0000 | [diff] [blame] | 964 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 965 | int rc = create_dir_if_needed(to.c_str(), kRollbackFolderMode); |
| 966 | if (rc != 0) { |
| 967 | return error(rc, "Failed to create folder " + to); |
Nikita Ioffe | 835ae77 | 2019-01-23 16:32:13 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 970 | rc = delete_dir_contents(rollback_package_path, true /* ignore_if_missing */); |
| 971 | if (rc != 0) { |
| 972 | return error(rc, "Failed clearing existing snapshot " + rollback_package_path); |
| 973 | } |
| 974 | |
| 975 | rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 976 | if (rc != 0) { |
| 977 | res = error(rc, "Failed copying " + from + " to " + to); |
| 978 | clear_ce_on_exit = true; |
| 979 | return res; |
| 980 | } |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 981 | if (_aidl_return != nullptr) { |
| 982 | auto ce_snapshot_path = create_data_misc_ce_rollback_package_path(volume_uuid, user, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 983 | snapshotId, package_name); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 984 | rc = get_path_inode(ce_snapshot_path, reinterpret_cast<ino_t*>(_aidl_return)); |
| 985 | if (rc != 0) { |
| 986 | res = error(rc, "Failed to get_path_inode for " + ce_snapshot_path); |
| 987 | clear_ce_on_exit = true; |
| 988 | return res; |
| 989 | } |
| 990 | } |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | return res; |
| 994 | } |
| 995 | |
| 996 | binder::Status InstalldNativeService::restoreAppDataSnapshot( |
| 997 | const std::unique_ptr<std::string>& volumeUuid, const std::string& packageName, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 998 | const int32_t appId, const std::string& seInfo, const int32_t user, |
| 999 | const int32_t snapshotId, int32_t storageFlags) { |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1000 | ENFORCE_UID(AID_SYSTEM); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1001 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1002 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1003 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 1004 | |
| 1005 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1006 | const char* package_name = packageName.c_str(); |
| 1007 | |
| 1008 | auto from_ce = create_data_misc_ce_rollback_package_path(volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1009 | user, snapshotId, package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1010 | auto from_de = create_data_misc_de_rollback_package_path(volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1011 | user, snapshotId, package_name); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1012 | |
| 1013 | const bool needs_ce_rollback = (storageFlags & FLAG_STORAGE_CE) && |
| 1014 | (access(from_ce.c_str(), F_OK) == 0); |
| 1015 | const bool needs_de_rollback = (storageFlags & FLAG_STORAGE_DE) && |
| 1016 | (access(from_de.c_str(), F_OK) == 0); |
| 1017 | |
| 1018 | if (!needs_ce_rollback && !needs_de_rollback) { |
| 1019 | return ok(); |
| 1020 | } |
| 1021 | |
| 1022 | // We know we're going to rollback one of the CE or DE data, so we clear |
| 1023 | // application data first. Note that it's possible that we're asked to |
| 1024 | // restore both CE & DE data but that one of the restores fail. Leaving the |
| 1025 | // app with no data in those cases is arguably better than leaving the app |
| 1026 | // with mismatched / stale data. |
| 1027 | LOG(INFO) << "Clearing app data for " << packageName << " to restore snapshot."; |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1028 | // It's fine to pass 0 as ceDataInode here, because restoreAppDataSnapshot |
| 1029 | // can only be called when user unlocks the phone, meaning that CE user data |
| 1030 | // is decrypted. |
| 1031 | binder::Status res = clearAppData(volumeUuid, packageName, user, storageFlags, |
| 1032 | 0 /* ceDataInode */); |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1033 | if (!res.isOk()) { |
| 1034 | return res; |
| 1035 | } |
| 1036 | |
| 1037 | if (needs_ce_rollback) { |
| 1038 | auto to_ce = create_data_user_ce_path(volume_uuid, user); |
| 1039 | int rc = copy_directory_recursive(from_ce.c_str(), to_ce.c_str()); |
| 1040 | if (rc != 0) { |
| 1041 | res = error(rc, "Failed copying " + from_ce + " to " + to_ce); |
| 1042 | return res; |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | if (needs_de_rollback) { |
| 1047 | auto to_de = create_data_user_de_path(volume_uuid, user); |
| 1048 | int rc = copy_directory_recursive(from_de.c_str(), to_de.c_str()); |
| 1049 | if (rc != 0) { |
Nikita Ioffe | fd2ccd4 | 2019-01-25 11:44:36 +0000 | [diff] [blame] | 1050 | if (needs_ce_rollback) { |
| 1051 | auto ce_data = create_data_user_ce_package_path(volume_uuid, user, package_name); |
| 1052 | LOG(WARNING) << "de_data rollback failed. Erasing rolled back ce_data " << ce_data; |
| 1053 | if (delete_dir_contents(ce_data.c_str(), 1, nullptr) != 0) { |
| 1054 | LOG(WARNING) << "Failed to delete rolled back ce_data " << ce_data; |
| 1055 | } |
| 1056 | } |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1057 | res = error(rc, "Failed copying " + from_de + " to " + to_de); |
| 1058 | return res; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | // Finally, restore the SELinux label on the app data. |
| 1063 | return restoreconAppData(volumeUuid, packageName, user, storageFlags, appId, seInfo); |
| 1064 | } |
| 1065 | |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1066 | binder::Status InstalldNativeService::destroyAppDataSnapshot( |
| 1067 | const std::unique_ptr<std::string> &volumeUuid, const std::string& packageName, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1068 | const int32_t user, const int64_t ceSnapshotInode, const int32_t snapshotId, |
| 1069 | int32_t storageFlags) { |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1070 | ENFORCE_UID(AID_SYSTEM); |
| 1071 | CHECK_ARGUMENT_UUID_IS_TEST_OR_NULL(volumeUuid); |
| 1072 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1073 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 1074 | |
| 1075 | const char* volume_uuid = volumeUuid ? volumeUuid->c_str() : nullptr; |
| 1076 | const char* package_name = packageName.c_str(); |
| 1077 | |
| 1078 | if (storageFlags & FLAG_STORAGE_DE) { |
| 1079 | auto de_snapshot_path = create_data_misc_de_rollback_package_path(volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1080 | user, snapshotId, package_name); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1081 | |
| 1082 | int res = delete_dir_contents_and_dir(de_snapshot_path, true /* ignore_if_missing */); |
| 1083 | if (res != 0) { |
| 1084 | return error(res, "Failed clearing snapshot " + de_snapshot_path); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | if (storageFlags & FLAG_STORAGE_CE) { |
| 1089 | auto ce_snapshot_path = create_data_misc_ce_rollback_package_path(volume_uuid, |
Nikita Ioffe | b68f0d1 | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 1090 | user, snapshotId, package_name, ceSnapshotInode); |
Nikita Ioffe | 77be2ed | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 1091 | int res = delete_dir_contents_and_dir(ce_snapshot_path, true /* ignore_if_missing */); |
| 1092 | if (res != 0) { |
| 1093 | return error(res, "Failed clearing snapshot " + ce_snapshot_path); |
| 1094 | } |
| 1095 | } |
| 1096 | return ok(); |
| 1097 | } |
| 1098 | |
| 1099 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1100 | binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, |
| 1101 | const std::unique_ptr<std::string>& toUuid, const std::string& packageName, |
| 1102 | const std::string& dataAppName, int32_t appId, const std::string& seInfo, |
| 1103 | int32_t targetSdkVersion) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1104 | ENFORCE_UID(AID_SYSTEM); |
| 1105 | CHECK_ARGUMENT_UUID(fromUuid); |
| 1106 | CHECK_ARGUMENT_UUID(toUuid); |
| 1107 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1108 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1109 | |
| 1110 | const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; |
| 1111 | const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; |
| 1112 | const char* package_name = packageName.c_str(); |
| 1113 | const char* data_app_name = dataAppName.c_str(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1114 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1115 | binder::Status res = ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1116 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 1117 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1118 | // Copy app |
| 1119 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1120 | auto from = create_data_app_package_path(from_uuid, data_app_name); |
| 1121 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
| 1122 | auto to_parent = create_data_app_path(to_uuid); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1123 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1124 | int rc = copy_directory_recursive(from.c_str(), to_parent.c_str()); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1125 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1126 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1127 | goto fail; |
| 1128 | } |
| 1129 | |
| 1130 | if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1131 | res = error("Failed to restorecon " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1132 | goto fail; |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | // Copy private data for all known users |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1137 | for (auto user : users) { |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1138 | |
| 1139 | // Data source may not exist for all users; that's okay |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1140 | auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 1141 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 1142 | LOG(INFO) << "Missing source " << from_ce; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1143 | continue; |
| 1144 | } |
| 1145 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 1146 | if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 1147 | seInfo, targetSdkVersion, nullptr).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1148 | res = error("Failed to create package target"); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1149 | goto fail; |
| 1150 | } |
| 1151 | |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1152 | { |
| 1153 | auto from = create_data_user_de_package_path(from_uuid, user, package_name); |
| 1154 | auto to = create_data_user_de_path(to_uuid, user); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1155 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1156 | int rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1157 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1158 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1159 | goto fail; |
| 1160 | } |
| 1161 | } |
| 1162 | { |
| 1163 | auto from = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 1164 | auto to = create_data_user_ce_path(to_uuid, user); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1165 | |
Narayan Kamath | e63ca7d | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 1166 | int rc = copy_directory_recursive(from.c_str(), to.c_str()); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1167 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1168 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1169 | goto fail; |
| 1170 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1173 | if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
| 1174 | appId, seInfo).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1175 | res = error("Failed to restorecon"); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1176 | goto fail; |
| 1177 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1178 | } |
| 1179 | |
Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 1180 | // We let the framework scan the new location and persist that before |
| 1181 | // deleting the data in the old location; this ordering ensures that |
| 1182 | // we can recover from things like battery pulls. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1183 | return ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1184 | |
| 1185 | fail: |
| 1186 | // Nuke everything we might have already copied |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1187 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1188 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1189 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 1190 | LOG(WARNING) << "Failed to rollback " << to; |
| 1191 | } |
| 1192 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1193 | for (auto user : users) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1194 | { |
| 1195 | auto to = create_data_user_de_package_path(to_uuid, user, package_name); |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1196 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1197 | LOG(WARNING) << "Failed to rollback " << to; |
| 1198 | } |
| 1199 | } |
| 1200 | { |
| 1201 | auto to = create_data_user_ce_package_path(to_uuid, user, package_name); |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1202 | if (delete_dir_contents(to.c_str(), 1, nullptr) != 0) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 1203 | LOG(WARNING) << "Failed to rollback " << to; |
| 1204 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1207 | return res; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 1208 | } |
| 1209 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1210 | binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, |
| 1211 | int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { |
| 1212 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1213 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1214 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1215 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1216 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 1217 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1218 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1219 | if (ensure_config_user_dirs(userId) != 0) { |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 1220 | return error(StringPrintf("Failed to ensure dirs for %d", userId)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1221 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1222 | } |
| 1223 | } |
Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 1224 | |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 1225 | return ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, |
| 1229 | int32_t userId, int32_t flags) { |
| 1230 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1231 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1232 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1233 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1234 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1235 | binder::Status res = ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1236 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1237 | auto path = create_data_user_de_path(uuid_, userId); |
| 1238 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 1239 | res = error("Failed to delete " + path); |
| 1240 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1241 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1242 | path = create_data_misc_legacy_path(userId); |
| 1243 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 1244 | res = error("Failed to delete " + path); |
| 1245 | } |
Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 1246 | path = create_primary_cur_profile_dir_path(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1247 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 1248 | res = error("Failed to delete " + path); |
| 1249 | } |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1250 | } |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1251 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 1252 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1253 | auto path = create_data_user_ce_path(uuid_, userId); |
| 1254 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 1255 | res = error("Failed to delete " + path); |
| 1256 | } |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 1257 | path = findDataMediaPath(uuid, userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1258 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 1259 | res = error("Failed to delete " + path); |
| 1260 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 1261 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1262 | return res; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1265 | binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1266 | int64_t targetFreeBytes, int64_t cacheReservedBytes, int32_t flags) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1267 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1268 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1269 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1270 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1271 | auto uuidString = uuid ? *uuid : ""; |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1272 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1273 | auto data_path = create_data_path(uuid_); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1274 | auto noop = (flags & FLAG_FREE_CACHE_NOOP); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1275 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1276 | int64_t free = data_disk_free(data_path); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1277 | if (free < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1278 | return error("Failed to determine free space for " + data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1279 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1280 | |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1281 | int64_t cleared = 0; |
| 1282 | int64_t needed = targetFreeBytes - free; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 1283 | LOG(DEBUG) << "Device " << data_path << " has " << free << " free; requested " |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1284 | << targetFreeBytes << "; needed " << needed; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 1285 | |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1286 | if (free >= targetFreeBytes) { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 1287 | return ok(); |
| 1288 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1289 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1290 | if (flags & FLAG_FREE_CACHE_V2) { |
| 1291 | // This new cache strategy fairly removes files from UIDs by deleting |
| 1292 | // files from the UIDs which are most over their allocated quota |
| 1293 | |
| 1294 | // 1. Create trackers for every known UID |
| 1295 | ATRACE_BEGIN("create"); |
| 1296 | std::unordered_map<uid_t, std::shared_ptr<CacheTracker>> trackers; |
| 1297 | for (auto user : get_known_users(uuid_)) { |
| 1298 | FTS *fts; |
| 1299 | FTSENT *p; |
Jeff Sharkey | d9ff77f | 2017-04-14 18:54:49 -0600 | [diff] [blame] | 1300 | auto ce_path = create_data_user_ce_path(uuid_, user); |
| 1301 | auto de_path = create_data_user_de_path(uuid_, user); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 1302 | auto media_path = findDataMediaPath(uuid, user) + "/Android/data/"; |
| 1303 | char *argv[] = { (char*) ce_path.c_str(), (char*) de_path.c_str(), |
| 1304 | (char*) media_path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1305 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1306 | return error("Failed to fts_open"); |
| 1307 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1308 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1309 | if (p->fts_info == FTS_D && p->fts_level == 1) { |
| 1310 | uid_t uid = p->fts_statp->st_uid; |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 1311 | if (multiuser_get_app_id(uid) == AID_MEDIA_RW) { |
| 1312 | uid = (multiuser_get_app_id(p->fts_statp->st_gid) - AID_EXT_GID_START) |
| 1313 | + AID_APP_START; |
| 1314 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1315 | auto search = trackers.find(uid); |
| 1316 | if (search != trackers.end()) { |
| 1317 | search->second->addDataPath(p->fts_path); |
| 1318 | } else { |
| 1319 | auto tracker = std::shared_ptr<CacheTracker>(new CacheTracker( |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1320 | multiuser_get_user_id(uid), multiuser_get_app_id(uid), uuidString)); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1321 | tracker->addDataPath(p->fts_path); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1322 | { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 1323 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1324 | tracker->cacheQuota = mCacheQuotas[uid]; |
| 1325 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1326 | if (tracker->cacheQuota == 0) { |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 1327 | #if MEASURE_DEBUG |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1328 | LOG(WARNING) << "UID " << uid << " has no cache quota; assuming 64MB"; |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 1329 | #endif |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1330 | tracker->cacheQuota = 67108864; |
| 1331 | } |
| 1332 | trackers[uid] = tracker; |
| 1333 | } |
| 1334 | fts_set(fts, p, FTS_SKIP); |
| 1335 | } |
| 1336 | } |
| 1337 | fts_close(fts); |
| 1338 | } |
| 1339 | ATRACE_END(); |
| 1340 | |
| 1341 | // 2. Populate tracker stats and insert into priority queue |
| 1342 | ATRACE_BEGIN("populate"); |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1343 | int64_t cacheTotal = 0; |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1344 | auto cmp = [](std::shared_ptr<CacheTracker> left, std::shared_ptr<CacheTracker> right) { |
| 1345 | return (left->getCacheRatio() < right->getCacheRatio()); |
| 1346 | }; |
| 1347 | std::priority_queue<std::shared_ptr<CacheTracker>, |
| 1348 | std::vector<std::shared_ptr<CacheTracker>>, decltype(cmp)> queue(cmp); |
| 1349 | for (const auto& it : trackers) { |
| 1350 | it.second->loadStats(); |
| 1351 | queue.push(it.second); |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1352 | cacheTotal += it.second->cacheUsed; |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1353 | } |
| 1354 | ATRACE_END(); |
| 1355 | |
| 1356 | // 3. Bounce across the queue, freeing items from whichever tracker is |
| 1357 | // the most over their assigned quota |
| 1358 | ATRACE_BEGIN("bounce"); |
| 1359 | std::shared_ptr<CacheTracker> active; |
| 1360 | while (active || !queue.empty()) { |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 1361 | // Only look at apps under quota when explicitly requested |
| 1362 | if (active && (active->getCacheRatio() < 10000) |
| 1363 | && !(flags & FLAG_FREE_CACHE_V2_DEFY_QUOTA)) { |
| 1364 | LOG(DEBUG) << "Active ratio " << active->getCacheRatio() |
| 1365 | << " isn't over quota, and defy not requested"; |
| 1366 | break; |
| 1367 | } |
| 1368 | |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1369 | // Only keep clearing when we haven't pushed into reserved area |
| 1370 | if (cacheReservedBytes > 0 && cleared >= (cacheTotal - cacheReservedBytes)) { |
| 1371 | LOG(DEBUG) << "Refusing to clear cached data in reserved space"; |
| 1372 | break; |
| 1373 | } |
| 1374 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1375 | // Find the best tracker to work with; this might involve swapping |
| 1376 | // if the active tracker is no longer the most over quota |
| 1377 | bool nextBetter = active && !queue.empty() |
| 1378 | && active->getCacheRatio() < queue.top()->getCacheRatio(); |
| 1379 | if (!active || nextBetter) { |
| 1380 | if (active) { |
| 1381 | // Current tracker still has items, so we'll consider it |
| 1382 | // again later once it bubbles up to surface |
| 1383 | queue.push(active); |
| 1384 | } |
| 1385 | active = queue.top(); queue.pop(); |
| 1386 | active->ensureItems(); |
| 1387 | continue; |
| 1388 | } |
| 1389 | |
| 1390 | // If no items remain, go find another tracker |
| 1391 | if (active->items.empty()) { |
| 1392 | active = nullptr; |
| 1393 | continue; |
| 1394 | } else { |
| 1395 | auto item = active->items.back(); |
| 1396 | active->items.pop_back(); |
| 1397 | |
| 1398 | LOG(DEBUG) << "Purging " << item->toString() << " from " << active->toString(); |
| 1399 | if (!noop) { |
| 1400 | item->purge(); |
| 1401 | } |
| 1402 | active->cacheUsed -= item->size; |
| 1403 | needed -= item->size; |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1404 | cleared += item->size; |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | // Verify that we're actually done before bailing, since sneaky |
| 1408 | // apps might be using hardlinks |
| 1409 | if (needed <= 0) { |
| 1410 | free = data_disk_free(data_path); |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1411 | needed = targetFreeBytes - free; |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1412 | if (needed <= 0) { |
| 1413 | break; |
| 1414 | } else { |
| 1415 | LOG(WARNING) << "Expected to be done but still need " << needed; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | ATRACE_END(); |
| 1420 | |
| 1421 | } else { |
Jeff Sharkey | 1cb4aaf | 2017-03-27 16:41:06 -0600 | [diff] [blame] | 1422 | return error("Legacy cache logic no longer supported"); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1425 | free = data_disk_free(data_path); |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1426 | if (free >= targetFreeBytes) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1427 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1428 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1429 | return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, |
Jeff Sharkey | 60f8a53 | 2017-05-30 14:38:42 -0600 | [diff] [blame] | 1430 | targetFreeBytes, data_path.c_str(), free)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1431 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1434 | binder::Status InstalldNativeService::rmdex(const std::string& codePath, |
| 1435 | const std::string& instructionSet) { |
| 1436 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 1437 | CHECK_ARGUMENT_PATH(codePath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 1438 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 1439 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1440 | char dex_path[PKG_PATH_MAX]; |
| 1441 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1442 | const char* path = codePath.c_str(); |
| 1443 | const char* instruction_set = instructionSet.c_str(); |
| 1444 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 1445 | if (validate_apk_path(path) && validate_system_app_path(path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1446 | return error("Invalid path " + codePath); |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1449 | if (!create_cache_path(dex_path, path, instruction_set)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1450 | return error("Failed to create cache path for " + codePath); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1451 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1452 | |
| 1453 | ALOGV("unlink %s\n", dex_path); |
| 1454 | if (unlink(dex_path) < 0) { |
Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 1455 | // It's ok if we don't have a dalvik cache path. Report error only when the path exists |
| 1456 | // but could not be unlinked. |
| 1457 | if (errno != ENOENT) { |
| 1458 | return error(StringPrintf("Failed to unlink %s", dex_path)); |
| 1459 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1460 | } |
Calin Juravle | 249f2d3 | 2017-05-03 17:22:27 -0700 | [diff] [blame] | 1461 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1464 | struct stats { |
| 1465 | int64_t codeSize; |
| 1466 | int64_t dataSize; |
| 1467 | int64_t cacheSize; |
| 1468 | }; |
| 1469 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1470 | #if MEASURE_DEBUG |
| 1471 | static std::string toString(std::vector<int64_t> values) { |
| 1472 | std::stringstream res; |
| 1473 | res << "["; |
| 1474 | for (size_t i = 0; i < values.size(); i++) { |
| 1475 | res << values[i]; |
| 1476 | if (i < values.size() - 1) { |
| 1477 | res << ","; |
| 1478 | } |
| 1479 | } |
| 1480 | res << "]"; |
| 1481 | return res.str(); |
| 1482 | } |
| 1483 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1484 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1485 | static void collectQuotaStats(const std::string& uuid, int32_t userId, |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1486 | int32_t appId, struct stats* stats, struct stats* extStats) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1487 | int64_t space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1488 | if (stats != nullptr) { |
| 1489 | uid_t uid = multiuser_get_uid(userId, appId); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1490 | if ((space = GetOccupiedSpaceForUid(uuid, uid)) != -1) { |
| 1491 | stats->dataSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | int cacheGid = multiuser_get_cache_gid(userId, appId); |
| 1495 | if (cacheGid != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1496 | if ((space = GetOccupiedSpaceForGid(uuid, cacheGid)) != -1) { |
| 1497 | stats->cacheSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | int sharedGid = multiuser_get_shared_gid(0, appId); |
| 1502 | if (sharedGid != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1503 | if ((space = GetOccupiedSpaceForGid(uuid, sharedGid)) != -1) { |
| 1504 | stats->codeSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1505 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1509 | if (extStats != nullptr) { |
| 1510 | int extGid = multiuser_get_ext_gid(userId, appId); |
| 1511 | if (extGid != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1512 | if ((space = GetOccupiedSpaceForGid(uuid, extGid)) != -1) { |
| 1513 | extStats->dataSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1514 | } |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1515 | } |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1516 | |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1517 | int extCacheGid = multiuser_get_ext_cache_gid(userId, appId); |
| 1518 | if (extCacheGid != -1) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1519 | if ((space = GetOccupiedSpaceForGid(uuid, extCacheGid)) != -1) { |
| 1520 | extStats->dataSize += space; |
| 1521 | extStats->cacheSize += space; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1522 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1523 | } |
| 1524 | } |
| 1525 | } |
| 1526 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1527 | static void collectManualStats(const std::string& path, struct stats* stats) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1528 | DIR *d; |
| 1529 | int dfd; |
| 1530 | struct dirent *de; |
| 1531 | struct stat s; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1532 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1533 | d = opendir(path.c_str()); |
| 1534 | if (d == nullptr) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1535 | if (errno != ENOENT) { |
| 1536 | PLOG(WARNING) << "Failed to open " << path; |
| 1537 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1538 | return; |
| 1539 | } |
| 1540 | dfd = dirfd(d); |
| 1541 | while ((de = readdir(d))) { |
| 1542 | const char *name = de->d_name; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1543 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1544 | int64_t size = 0; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1545 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1546 | size = s.st_blocks * 512; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | if (de->d_type == DT_DIR) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1550 | if (!strcmp(name, ".")) { |
| 1551 | // Don't recurse, but still count node size |
| 1552 | } else if (!strcmp(name, "..")) { |
| 1553 | // Don't recurse or count node size |
| 1554 | continue; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1555 | } else { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1556 | // Measure all children nodes |
| 1557 | size = 0; |
| 1558 | calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1559 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1560 | |
| 1561 | if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { |
| 1562 | stats->cacheSize += size; |
| 1563 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1564 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1565 | |
| 1566 | // Legacy symlink isn't owned by app |
| 1567 | if (de->d_type == DT_LNK && !strcmp(name, "lib")) { |
| 1568 | continue; |
| 1569 | } |
| 1570 | |
| 1571 | // Everything found inside is considered data |
| 1572 | stats->dataSize += size; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1573 | } |
| 1574 | closedir(d); |
| 1575 | } |
| 1576 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1577 | static void collectManualStatsForUser(const std::string& path, struct stats* stats, |
| 1578 | bool exclude_apps = false) { |
| 1579 | DIR *d; |
| 1580 | int dfd; |
| 1581 | struct dirent *de; |
| 1582 | struct stat s; |
| 1583 | |
| 1584 | d = opendir(path.c_str()); |
| 1585 | if (d == nullptr) { |
| 1586 | if (errno != ENOENT) { |
| 1587 | PLOG(WARNING) << "Failed to open " << path; |
| 1588 | } |
| 1589 | return; |
| 1590 | } |
| 1591 | dfd = dirfd(d); |
| 1592 | while ((de = readdir(d))) { |
| 1593 | if (de->d_type == DT_DIR) { |
| 1594 | const char *name = de->d_name; |
| 1595 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { |
| 1596 | continue; |
| 1597 | } |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1598 | int32_t user_uid = multiuser_get_app_id(s.st_uid); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1599 | if (!strcmp(name, ".") || !strcmp(name, "..")) { |
| 1600 | continue; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1601 | } 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] | 1602 | continue; |
| 1603 | } else { |
| 1604 | collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | closedir(d); |
| 1609 | } |
| 1610 | |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1611 | static void collectManualExternalStatsForUser(const std::string& path, struct stats* stats) { |
| 1612 | FTS *fts; |
| 1613 | FTSENT *p; |
| 1614 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1615 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1616 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 1617 | return; |
| 1618 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1619 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1620 | p->fts_number = p->fts_parent->fts_number; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1621 | switch (p->fts_info) { |
| 1622 | case FTS_D: |
| 1623 | if (p->fts_level == 4 |
| 1624 | && !strcmp(p->fts_name, "cache") |
| 1625 | && !strcmp(p->fts_parent->fts_parent->fts_name, "data") |
| 1626 | && !strcmp(p->fts_parent->fts_parent->fts_parent->fts_name, "Android")) { |
| 1627 | p->fts_number = 1; |
| 1628 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 1629 | [[fallthrough]]; // to count the directory |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1630 | case FTS_DEFAULT: |
| 1631 | case FTS_F: |
| 1632 | case FTS_SL: |
| 1633 | case FTS_SLNONE: |
| 1634 | int64_t size = (p->fts_statp->st_blocks * 512); |
| 1635 | if (p->fts_number == 1) { |
| 1636 | stats->cacheSize += size; |
| 1637 | } |
| 1638 | stats->dataSize += size; |
| 1639 | break; |
| 1640 | } |
| 1641 | } |
| 1642 | fts_close(fts); |
| 1643 | } |
| 1644 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1645 | binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1646 | const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, |
| 1647 | int32_t appId, const std::vector<int64_t>& ceDataInodes, |
| 1648 | const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1649 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1650 | CHECK_ARGUMENT_UUID(uuid); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 1651 | for (const auto& packageName : packageNames) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1652 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1653 | } |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 1654 | for (const auto& codePath : codePaths) { |
| 1655 | CHECK_ARGUMENT_PATH(codePath); |
| 1656 | } |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1657 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 1658 | // read-only measurements without mutation. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1659 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1660 | // When modifying this logic, always verify using tests: |
| 1661 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize |
| 1662 | |
| 1663 | #if MEASURE_DEBUG |
| 1664 | LOG(INFO) << "Measuring user " << userId << " app " << appId; |
| 1665 | #endif |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1666 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1667 | // Here's a summary of the common storage locations across the platform, |
| 1668 | // and how they're each tagged: |
| 1669 | // |
| 1670 | // /data/app/com.example UID system |
| 1671 | // /data/app/com.example/oat UID system |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1672 | // /data/user/0/com.example UID u0_a10 GID u0_a10 |
| 1673 | // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache |
| 1674 | // /data/media/0/foo.txt UID u0_media_rw |
| 1675 | // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image |
| 1676 | // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext |
| 1677 | // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache |
| 1678 | // /data/media/obb/com.example UID system |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1679 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1680 | struct stats stats; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1681 | struct stats extStats; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1682 | memset(&stats, 0, sizeof(stats)); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1683 | memset(&extStats, 0, sizeof(extStats)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1684 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1685 | auto uuidString = uuid ? *uuid : ""; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1686 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1687 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1688 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1689 | flags &= ~FLAG_USE_QUOTA; |
| 1690 | } |
| 1691 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1692 | ATRACE_BEGIN("obb"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 1693 | for (const auto& packageName : packageNames) { |
Jeff Sharkey | 6d775c5 | 2019-03-22 13:46:20 -0600 | [diff] [blame] | 1694 | auto obbCodePath = create_data_media_package_path(uuid_, userId, |
| 1695 | "obb", packageName.c_str()); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1696 | calculate_tree_size(obbCodePath, &extStats.codeSize); |
| 1697 | } |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1698 | ATRACE_END(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1699 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1700 | if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1701 | ATRACE_BEGIN("code"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 1702 | for (const auto& codePath : codePaths) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1703 | calculate_tree_size(codePath, &stats.codeSize, -1, |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1704 | multiuser_get_shared_gid(0, appId)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1705 | } |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1706 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1707 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1708 | ATRACE_BEGIN("quota"); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1709 | collectQuotaStats(uuidString, userId, appId, &stats, &extStats); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1710 | ATRACE_END(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1711 | } else { |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1712 | ATRACE_BEGIN("code"); |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 1713 | for (const auto& codePath : codePaths) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1714 | calculate_tree_size(codePath, &stats.codeSize); |
| 1715 | } |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1716 | ATRACE_END(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1717 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1718 | for (size_t i = 0; i < packageNames.size(); i++) { |
| 1719 | const char* pkgname = packageNames[i].c_str(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1720 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1721 | ATRACE_BEGIN("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1722 | auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); |
| 1723 | collectManualStats(cePath, &stats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1724 | auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 1725 | collectManualStats(dePath, &stats); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1726 | ATRACE_END(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1727 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1728 | if (!uuid) { |
| 1729 | ATRACE_BEGIN("profiles"); |
| 1730 | calculate_tree_size( |
| 1731 | create_primary_current_profile_package_dir_path(userId, pkgname), |
| 1732 | &stats.dataSize); |
| 1733 | calculate_tree_size( |
| 1734 | create_primary_reference_profile_package_dir_path(pkgname), |
| 1735 | &stats.codeSize); |
| 1736 | ATRACE_END(); |
| 1737 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1738 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1739 | ATRACE_BEGIN("external"); |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1740 | auto extPath = create_data_media_package_path(uuid_, userId, "data", pkgname); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1741 | collectManualStats(extPath, &extStats); |
Jeff Sharkey | 76ddaeb | 2017-01-25 22:15:42 -0700 | [diff] [blame] | 1742 | auto mediaPath = create_data_media_package_path(uuid_, userId, "media", pkgname); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1743 | calculate_tree_size(mediaPath, &extStats.dataSize); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1744 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1747 | if (!uuid) { |
| 1748 | ATRACE_BEGIN("dalvik"); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1749 | int32_t sharedGid = multiuser_get_shared_gid(0, appId); |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1750 | if (sharedGid != -1) { |
| 1751 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 1752 | sharedGid, -1); |
| 1753 | } |
| 1754 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1755 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1756 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1757 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1758 | std::vector<int64_t> ret; |
| 1759 | ret.push_back(stats.codeSize); |
| 1760 | ret.push_back(stats.dataSize); |
| 1761 | ret.push_back(stats.cacheSize); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1762 | ret.push_back(extStats.codeSize); |
| 1763 | ret.push_back(extStats.dataSize); |
| 1764 | ret.push_back(extStats.cacheSize); |
| 1765 | #if MEASURE_DEBUG |
| 1766 | LOG(DEBUG) << "Final result " << toString(ret); |
| 1767 | #endif |
| 1768 | *_aidl_return = ret; |
| 1769 | return ok(); |
| 1770 | } |
| 1771 | |
| 1772 | binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, |
| 1773 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, |
| 1774 | std::vector<int64_t>* _aidl_return) { |
| 1775 | ENFORCE_UID(AID_SYSTEM); |
| 1776 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1777 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 1778 | // read-only measurements without mutation. |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1779 | |
| 1780 | // When modifying this logic, always verify using tests: |
| 1781 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize |
| 1782 | |
| 1783 | #if MEASURE_DEBUG |
| 1784 | LOG(INFO) << "Measuring user " << userId; |
| 1785 | #endif |
| 1786 | |
| 1787 | struct stats stats; |
| 1788 | struct stats extStats; |
| 1789 | memset(&stats, 0, sizeof(stats)); |
| 1790 | memset(&extStats, 0, sizeof(extStats)); |
| 1791 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1792 | auto uuidString = uuid ? *uuid : ""; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1793 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1794 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1795 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1796 | flags &= ~FLAG_USE_QUOTA; |
| 1797 | } |
| 1798 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1799 | if (flags & FLAG_USE_QUOTA) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1800 | int64_t space; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1801 | |
| 1802 | ATRACE_BEGIN("obb"); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1803 | if ((space = GetOccupiedSpaceForGid(uuidString, AID_MEDIA_OBB)) != -1) { |
| 1804 | extStats.codeSize += space; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1805 | } |
| 1806 | ATRACE_END(); |
| 1807 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1808 | ATRACE_BEGIN("code"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1809 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1810 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1811 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1812 | ATRACE_BEGIN("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1813 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 1814 | collectManualStatsForUser(cePath, &stats, true); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1815 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 1816 | collectManualStatsForUser(dePath, &stats, true); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1817 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1818 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1819 | if (!uuid) { |
| 1820 | ATRACE_BEGIN("profile"); |
| 1821 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); |
| 1822 | calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); |
| 1823 | auto refProfilePath = create_primary_ref_profile_dir_path(); |
| 1824 | calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); |
| 1825 | ATRACE_END(); |
| 1826 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1827 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1828 | ATRACE_BEGIN("external"); |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1829 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1830 | if ((space = GetOccupiedSpaceForUid(uuidString, uid)) != -1) { |
| 1831 | extStats.dataSize += space; |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1834 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1835 | if (!uuid) { |
| 1836 | ATRACE_BEGIN("dalvik"); |
| 1837 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 1838 | -1, -1, true); |
| 1839 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize, |
| 1840 | -1, -1, true); |
| 1841 | ATRACE_END(); |
| 1842 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1843 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1844 | ATRACE_BEGIN("quota"); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1845 | int64_t dataSize = extStats.dataSize; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1846 | for (auto appId : appIds) { |
| 1847 | if (appId >= AID_APP_START) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1848 | collectQuotaStats(uuidString, userId, appId, &stats, &extStats); |
Jeff Sharkey | ec1be62 | 2017-04-10 00:19:46 -0600 | [diff] [blame] | 1849 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1850 | #if MEASURE_DEBUG |
| 1851 | // Sleep to make sure we don't lose logs |
| 1852 | usleep(1); |
| 1853 | #endif |
| 1854 | } |
| 1855 | } |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1856 | extStats.dataSize = dataSize; |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1857 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1858 | } else { |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1859 | ATRACE_BEGIN("obb"); |
| 1860 | auto obbPath = create_data_path(uuid_) + "/media/obb"; |
| 1861 | calculate_tree_size(obbPath, &extStats.codeSize); |
| 1862 | ATRACE_END(); |
| 1863 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1864 | ATRACE_BEGIN("code"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1865 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1866 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1867 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1868 | ATRACE_BEGIN("data"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1869 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 1870 | collectManualStatsForUser(cePath, &stats); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1871 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 1872 | collectManualStatsForUser(dePath, &stats); |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1873 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1874 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1875 | if (!uuid) { |
| 1876 | ATRACE_BEGIN("profile"); |
| 1877 | auto userProfilePath = create_primary_cur_profile_dir_path(userId); |
| 1878 | calculate_tree_size(userProfilePath, &stats.dataSize); |
| 1879 | auto refProfilePath = create_primary_ref_profile_dir_path(); |
| 1880 | calculate_tree_size(refProfilePath, &stats.codeSize); |
| 1881 | ATRACE_END(); |
| 1882 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1883 | |
Jeff Sharkey | 466459b | 2017-01-17 15:25:01 -0700 | [diff] [blame] | 1884 | ATRACE_BEGIN("external"); |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1885 | auto dataMediaPath = create_data_media_path(uuid_, userId); |
| 1886 | collectManualExternalStatsForUser(dataMediaPath, &extStats); |
| 1887 | #if MEASURE_DEBUG |
| 1888 | LOG(DEBUG) << "Measured external data " << extStats.dataSize << " cache " |
| 1889 | << extStats.cacheSize; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1890 | #endif |
Jeff Sharkey | 7bf5b95 | 2017-01-19 09:21:36 -0700 | [diff] [blame] | 1891 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1892 | |
Jeff Sharkey | 06a6184 | 2017-04-15 12:03:31 -0600 | [diff] [blame] | 1893 | if (!uuid) { |
| 1894 | ATRACE_BEGIN("dalvik"); |
| 1895 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); |
| 1896 | calculate_tree_size(create_primary_cur_profile_dir_path(userId), &stats.dataSize); |
| 1897 | ATRACE_END(); |
| 1898 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | std::vector<int64_t> ret; |
| 1902 | ret.push_back(stats.codeSize); |
| 1903 | ret.push_back(stats.dataSize); |
| 1904 | ret.push_back(stats.cacheSize); |
| 1905 | ret.push_back(extStats.codeSize); |
| 1906 | ret.push_back(extStats.dataSize); |
| 1907 | ret.push_back(extStats.cacheSize); |
| 1908 | #if MEASURE_DEBUG |
| 1909 | LOG(DEBUG) << "Final result " << toString(ret); |
| 1910 | #endif |
| 1911 | *_aidl_return = ret; |
| 1912 | return ok(); |
| 1913 | } |
| 1914 | |
| 1915 | binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1916 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, |
| 1917 | std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1918 | ENFORCE_UID(AID_SYSTEM); |
| 1919 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 1920 | // NOTE: Locking is relaxed on this method, since it's limited to |
| 1921 | // read-only measurements without mutation. |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1922 | |
| 1923 | // When modifying this logic, always verify using tests: |
| 1924 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize |
| 1925 | |
| 1926 | #if MEASURE_DEBUG |
| 1927 | LOG(INFO) << "Measuring external " << userId; |
| 1928 | #endif |
| 1929 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1930 | auto uuidString = uuid ? *uuid : ""; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1931 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1932 | |
| 1933 | int64_t totalSize = 0; |
| 1934 | int64_t audioSize = 0; |
| 1935 | int64_t videoSize = 0; |
| 1936 | int64_t imageSize = 0; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1937 | int64_t appSize = 0; |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 1938 | int64_t obbSize = 0; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1939 | |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1940 | if (!IsQuotaSupported(uuidString)) { |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 1941 | flags &= ~FLAG_USE_QUOTA; |
| 1942 | } |
| 1943 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1944 | if (flags & FLAG_USE_QUOTA) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1945 | int64_t space; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1946 | |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1947 | ATRACE_BEGIN("quota"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1948 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1949 | if ((space = GetOccupiedSpaceForUid(uuidString, uid)) != -1) { |
| 1950 | totalSize = space; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1954 | if ((space = GetOccupiedSpaceForGid(uuidString, audioGid)) != -1) { |
| 1955 | audioSize = space; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1956 | } |
| 1957 | gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1958 | if ((space = GetOccupiedSpaceForGid(uuidString, videoGid)) != -1) { |
| 1959 | videoSize = space; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1960 | } |
| 1961 | gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1962 | if ((space = GetOccupiedSpaceForGid(uuidString, imageGid)) != -1) { |
| 1963 | imageSize = space; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1964 | } |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1965 | if ((space = GetOccupiedSpaceForGid(uuidString, AID_MEDIA_OBB)) != -1) { |
| 1966 | obbSize = space; |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 1967 | } |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1968 | ATRACE_END(); |
| 1969 | |
| 1970 | ATRACE_BEGIN("apps"); |
| 1971 | struct stats extStats; |
| 1972 | memset(&extStats, 0, sizeof(extStats)); |
| 1973 | for (auto appId : appIds) { |
| 1974 | if (appId >= AID_APP_START) { |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 1975 | collectQuotaStats(uuidString, userId, appId, nullptr, &extStats); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1976 | } |
| 1977 | } |
Jeff Sharkey | 4ca8ff9 | 2017-06-07 15:59:03 -0600 | [diff] [blame] | 1978 | appSize = extStats.dataSize; |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1979 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1980 | } else { |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 1981 | ATRACE_BEGIN("manual"); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1982 | FTS *fts; |
| 1983 | FTSENT *p; |
| 1984 | auto path = create_data_media_path(uuid_, userId); |
| 1985 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1986 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1987 | return error("Failed to fts_open " + path); |
| 1988 | } |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1989 | while ((p = fts_read(fts)) != nullptr) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1990 | char* ext; |
| 1991 | int64_t size = (p->fts_statp->st_blocks * 512); |
| 1992 | switch (p->fts_info) { |
| 1993 | case FTS_F: |
| 1994 | // Only categorize files not belonging to apps |
Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 1995 | if (p->fts_parent->fts_number == 0) { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1996 | ext = strrchr(p->fts_name, '.'); |
| 1997 | if (ext != nullptr) { |
| 1998 | switch (MatchExtension(++ext)) { |
| 1999 | case AID_MEDIA_AUDIO: audioSize += size; break; |
| 2000 | case AID_MEDIA_VIDEO: videoSize += size; break; |
| 2001 | case AID_MEDIA_IMAGE: imageSize += size; break; |
| 2002 | } |
| 2003 | } |
| 2004 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 2005 | [[fallthrough]]; // always count against total |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2006 | case FTS_D: |
Jeff Sharkey | 444ad1e | 2017-03-12 16:25:36 -0600 | [diff] [blame] | 2007 | // Ignore data belonging to specific apps |
| 2008 | p->fts_number = p->fts_parent->fts_number; |
| 2009 | if (p->fts_level == 1 && !strcmp(p->fts_name, "Android")) { |
| 2010 | p->fts_number = 1; |
| 2011 | } |
Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 2012 | [[fallthrough]]; // always count against total |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2013 | case FTS_DEFAULT: |
| 2014 | case FTS_SL: |
| 2015 | case FTS_SLNONE: |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2016 | if (p->fts_parent->fts_number == 1) { |
| 2017 | appSize += size; |
| 2018 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2019 | totalSize += size; |
| 2020 | break; |
| 2021 | } |
| 2022 | } |
| 2023 | fts_close(fts); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2024 | ATRACE_END(); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 2025 | |
| 2026 | ATRACE_BEGIN("obb"); |
Jeff Sharkey | 6d775c5 | 2019-03-22 13:46:20 -0600 | [diff] [blame] | 2027 | auto obbPath = StringPrintf("%s/Android/obb", |
| 2028 | create_data_media_path(uuid_, userId).c_str()); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 2029 | calculate_tree_size(obbPath, &obbSize); |
| 2030 | ATRACE_END(); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | std::vector<int64_t> ret; |
| 2034 | ret.push_back(totalSize); |
| 2035 | ret.push_back(audioSize); |
| 2036 | ret.push_back(videoSize); |
| 2037 | ret.push_back(imageSize); |
Jeff Sharkey | a084fcd | 2017-04-17 16:44:41 -0600 | [diff] [blame] | 2038 | ret.push_back(appSize); |
Jeff Sharkey | a013698 | 2017-07-06 11:29:37 -0600 | [diff] [blame] | 2039 | ret.push_back(obbSize); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 2040 | #if MEASURE_DEBUG |
| 2041 | LOG(DEBUG) << "Final result " << toString(ret); |
| 2042 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 2043 | *_aidl_return = ret; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2044 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2045 | } |
| 2046 | |
Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 2047 | binder::Status InstalldNativeService::getAppCrates( |
| 2048 | const std::unique_ptr<std::string>& uuid, |
| 2049 | const std::vector<std::string>& packageNames, int32_t userId, |
| 2050 | std::unique_ptr<std::vector<std::unique_ptr<CrateMetadata>>>* _aidl_return) { |
| 2051 | ENFORCE_UID(AID_SYSTEM); |
| 2052 | CHECK_ARGUMENT_UUID(uuid); |
| 2053 | for (const auto& packageName : packageNames) { |
| 2054 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 2055 | } |
| 2056 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2057 | |
| 2058 | auto retVector = std::make_unique<std::vector<std::unique_ptr<CrateMetadata>>>(); |
| 2059 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2060 | |
| 2061 | std::function<void(CratedFolder, std::unique_ptr<CrateMetadata> &)> onCreateCrate = |
| 2062 | [&](CratedFolder cratedFolder, std::unique_ptr<CrateMetadata> &crateMetadata) -> void { |
| 2063 | if (cratedFolder == nullptr) { |
| 2064 | return; |
| 2065 | } |
| 2066 | retVector->push_back(std::move(crateMetadata)); |
| 2067 | }; |
| 2068 | |
| 2069 | for (const auto& packageName : packageNames) { |
| 2070 | #if CRATE_DEBUG |
| 2071 | LOG(DEBUG) << "packageName = " << packageName; |
| 2072 | #endif |
| 2073 | auto crateManager = std::make_unique<CrateManager>(uuid_, userId, packageName); |
| 2074 | crateManager->traverseAllCrates(onCreateCrate); |
| 2075 | } |
| 2076 | |
| 2077 | #if CRATE_DEBUG |
| 2078 | LOG(WARNING) << "retVector->size() =" << retVector->size(); |
| 2079 | for (auto iter = retVector->begin(); iter != retVector->end(); ++iter) { |
| 2080 | CrateManager::dump(*iter); |
| 2081 | } |
| 2082 | #endif |
| 2083 | |
| 2084 | *_aidl_return = std::move(retVector); |
| 2085 | return ok(); |
| 2086 | } |
| 2087 | |
| 2088 | binder::Status InstalldNativeService::getUserCrates( |
| 2089 | const std::unique_ptr<std::string>& uuid, int32_t userId, |
| 2090 | std::unique_ptr<std::vector<std::unique_ptr<CrateMetadata>>>* _aidl_return) { |
| 2091 | ENFORCE_UID(AID_SYSTEM); |
| 2092 | CHECK_ARGUMENT_UUID(uuid); |
| 2093 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2094 | |
| 2095 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2096 | auto retVector = std::make_unique<std::vector<std::unique_ptr<CrateMetadata>>>(); |
| 2097 | |
| 2098 | std::function<void(CratedFolder, std::unique_ptr<CrateMetadata> &)> onCreateCrate = |
| 2099 | [&](CratedFolder cratedFolder, std::unique_ptr<CrateMetadata> &crateMetadata) -> void { |
| 2100 | if (cratedFolder == nullptr) { |
| 2101 | return; |
| 2102 | } |
| 2103 | retVector->push_back(std::move(crateMetadata)); |
| 2104 | }; |
| 2105 | |
| 2106 | std::function<void(FTSENT*)> onHandingPackage = [&](FTSENT* packageDir) -> void { |
| 2107 | auto crateManager = std::make_unique<CrateManager>(uuid_, userId, packageDir->fts_name); |
| 2108 | crateManager->traverseAllCrates(onCreateCrate); |
| 2109 | }; |
| 2110 | CrateManager::traverseAllPackagesForUser(uuid, userId, onHandingPackage); |
| 2111 | |
| 2112 | #if CRATE_DEBUG |
| 2113 | LOG(DEBUG) << "retVector->size() =" << retVector->size(); |
| 2114 | for (auto iter = retVector->begin(); iter != retVector->end(); ++iter) { |
| 2115 | CrateManager::dump(*iter); |
| 2116 | } |
| 2117 | #endif |
| 2118 | |
| 2119 | *_aidl_return = std::move(retVector); |
| 2120 | return ok(); |
| 2121 | } |
| 2122 | |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2123 | binder::Status InstalldNativeService::setAppQuota(const std::unique_ptr<std::string>& uuid, |
| 2124 | int32_t userId, int32_t appId, int64_t cacheQuota) { |
| 2125 | ENFORCE_UID(AID_SYSTEM); |
| 2126 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2127 | std::lock_guard<std::recursive_mutex> lock(mQuotasLock); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 2128 | |
| 2129 | int32_t uid = multiuser_get_uid(userId, appId); |
| 2130 | mCacheQuotas[uid] = cacheQuota; |
| 2131 | |
| 2132 | return ok(); |
| 2133 | } |
| 2134 | |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 2135 | // Dumps the contents of a profile file, using pkgname's dex files for pretty |
| 2136 | // printing the result. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2137 | binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2138 | const std::string& profileName, const std::string& codePath, bool* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2139 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2140 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2141 | CHECK_ARGUMENT_PATH(codePath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2142 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2143 | |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2144 | *_aidl_return = dump_profiles(uid, packageName, profileName, codePath); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2145 | return ok(); |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 2148 | // Copy the contents of a system profile over the data profile. |
| 2149 | binder::Status InstalldNativeService::copySystemProfile(const std::string& systemProfile, |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2150 | int32_t packageUid, const std::string& packageName, const std::string& profileName, |
| 2151 | bool* _aidl_return) { |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 2152 | ENFORCE_UID(AID_SYSTEM); |
| 2153 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 2154 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2155 | *_aidl_return = copy_system_profile(systemProfile, packageUid, packageName, profileName); |
Mathieu Chartier | f966f2a | 2017-05-10 12:48:37 -0700 | [diff] [blame] | 2156 | return ok(); |
| 2157 | } |
| 2158 | |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 2159 | // TODO: Consider returning error codes. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2160 | binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2161 | const std::string& profileName, bool* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2162 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2163 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2164 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2165 | |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2166 | *_aidl_return = analyze_primary_profiles(uid, packageName, profileName); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2167 | return ok(); |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
Calin Juravle | c41dac2 | 2017-12-05 12:29:15 -0800 | [diff] [blame] | 2170 | binder::Status InstalldNativeService::createProfileSnapshot(int32_t appId, |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2171 | const std::string& packageName, const std::string& profileName, |
| 2172 | const std::string& classpath, bool* _aidl_return) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2173 | ENFORCE_UID(AID_SYSTEM); |
| 2174 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 2175 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2176 | |
Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 2177 | *_aidl_return = create_profile_snapshot(appId, packageName, profileName, classpath); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2178 | return ok(); |
| 2179 | } |
| 2180 | |
| 2181 | binder::Status InstalldNativeService::destroyProfileSnapshot(const std::string& packageName, |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2182 | const std::string& profileName) { |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2183 | ENFORCE_UID(AID_SYSTEM); |
| 2184 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 2185 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2186 | |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2187 | std::string snapshot = create_snapshot_profile_path(packageName, profileName); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2188 | if ((unlink(snapshot.c_str()) != 0) && (errno != ENOENT)) { |
Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 2189 | return error("Failed to destroy profile snapshot for " + packageName + ":" + profileName); |
Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 2190 | } |
| 2191 | return ok(); |
| 2192 | } |
| 2193 | |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 2194 | static const char* getCStr(const std::unique_ptr<std::string>& data, |
| 2195 | const char* default_value = nullptr) { |
| 2196 | return data == nullptr ? default_value : data->c_str(); |
| 2197 | } |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2198 | binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, |
| 2199 | const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, |
| 2200 | int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, |
| 2201 | const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, |
Calin Juravle | 1d66761 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 2202 | const std::unique_ptr<std::string>& classLoaderContext, |
Calin Juravle | 562de81 | 2018-01-20 23:34:18 -0800 | [diff] [blame] | 2203 | const std::unique_ptr<std::string>& seInfo, bool downgrade, int32_t targetSdkVersion, |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 2204 | const std::unique_ptr<std::string>& profileName, |
Calin Juravle | dcccd83 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 2205 | const std::unique_ptr<std::string>& dexMetadataPath, |
| 2206 | const std::unique_ptr<std::string>& compilationReason) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2207 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2208 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2209 | CHECK_ARGUMENT_PATH(apkPath); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2210 | if (packageName && *packageName != "*") { |
| 2211 | CHECK_ARGUMENT_PACKAGE_NAME(*packageName); |
| 2212 | } |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2213 | CHECK_ARGUMENT_PATH(outputPath); |
| 2214 | CHECK_ARGUMENT_PATH(dexMetadataPath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2215 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2216 | |
Alex Buynytskyy | 17d8f82 | 2019-06-20 16:29:57 -0700 | [diff] [blame] | 2217 | const char* oat_dir = getCStr(outputPath); |
| 2218 | const char* instruction_set = instructionSet.c_str(); |
| 2219 | if (oat_dir != nullptr && !createOatDir(oat_dir, instruction_set).isOk()) { |
| 2220 | // Can't create oat dir - let dexopt use cache dir. |
| 2221 | oat_dir = nullptr; |
| 2222 | } |
| 2223 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2224 | const char* apk_path = apkPath.c_str(); |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 2225 | const char* pkgname = getCStr(packageName, "*"); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2226 | const char* compiler_filter = compilerFilter.c_str(); |
Calin Juravle | cc3b8ae | 2018-02-01 17:03:23 +0000 | [diff] [blame] | 2227 | const char* volume_uuid = getCStr(uuid); |
| 2228 | const char* class_loader_context = getCStr(classLoaderContext); |
| 2229 | const char* se_info = getCStr(seInfo); |
| 2230 | const char* profile_name = getCStr(profileName); |
| 2231 | const char* dm_path = getCStr(dexMetadataPath); |
Calin Juravle | dcccd83 | 2018-02-13 18:31:32 -0800 | [diff] [blame] | 2232 | const char* compilation_reason = getCStr(compilationReason); |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 2233 | std::string error_msg; |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2234 | int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, |
Calin Juravle | 1d66761 | 2017-07-13 22:50:21 -0700 | [diff] [blame] | 2235 | oat_dir, dexFlags, compiler_filter, volume_uuid, class_loader_context, se_info, |
Andreas Gampe | 023b224 | 2018-02-28 16:03:25 -0800 | [diff] [blame] | 2236 | downgrade, targetSdkVersion, profile_name, dm_path, compilation_reason, &error_msg); |
| 2237 | return res ? error(res, error_msg) : ok(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Eric Holk | 0ebbe0f | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 2240 | binder::Status InstalldNativeService::compileLayouts(const std::string& apkPath, |
| 2241 | const std::string& packageName, |
| 2242 | const std ::string& outDexFile, int uid, |
| 2243 | bool* _aidl_return) { |
| 2244 | const char* apk_path = apkPath.c_str(); |
| 2245 | const char* package_name = packageName.c_str(); |
| 2246 | const char* out_dex_file = outDexFile.c_str(); |
| 2247 | *_aidl_return = android::installd::view_compiler(apk_path, package_name, out_dex_file, uid); |
| 2248 | return *_aidl_return ? ok() : error("viewcompiler failed"); |
| 2249 | } |
| 2250 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2251 | binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { |
| 2252 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2253 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2254 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2255 | const char* instruction_set = instructionSet.c_str(); |
| 2256 | |
| 2257 | char boot_marker_path[PKG_PATH_MAX]; |
| 2258 | sprintf(boot_marker_path, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 2259 | "%s/%s/%s/.booting", |
Jeff Sharkey | 1b9d9a6 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 2260 | android_data_dir.c_str(), |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 2261 | DALVIK_CACHE, |
| 2262 | instruction_set); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 2263 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2264 | ALOGV("mark_boot_complete : %s", boot_marker_path); |
| 2265 | if (unlink(boot_marker_path) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2266 | return error(StringPrintf("Failed to unlink %s", boot_marker_path)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2267 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2268 | return ok(); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2271 | binder::Status InstalldNativeService::linkNativeLibraryDirectory( |
| 2272 | const std::unique_ptr<std::string>& uuid, const std::string& packageName, |
| 2273 | const std::string& nativeLibPath32, int32_t userId) { |
| 2274 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2275 | CHECK_ARGUMENT_UUID(uuid); |
| 2276 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2277 | CHECK_ARGUMENT_PATH(nativeLibPath32); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2278 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2279 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2280 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2281 | const char* pkgname = packageName.c_str(); |
| 2282 | const char* asecLibDir = nativeLibPath32.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2283 | struct stat s, libStat; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2284 | binder::Status res = ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2285 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2286 | auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 2287 | auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 2288 | |
| 2289 | const char* pkgdir = _pkgdir.c_str(); |
| 2290 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2291 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2292 | if (stat(pkgdir, &s) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2293 | return error("Failed to stat " + _pkgdir); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2294 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2295 | |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 2296 | char *con = nullptr; |
| 2297 | if (lgetfilecon(pkgdir, &con) < 0) { |
| 2298 | return error("Failed to lgetfilecon " + _pkgdir); |
| 2299 | } |
| 2300 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2301 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 2302 | res = error("Failed to chown " + _pkgdir); |
| 2303 | goto out; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | if (chmod(pkgdir, 0700) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2307 | res = error("Failed to chmod " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2308 | goto out; |
| 2309 | } |
| 2310 | |
| 2311 | if (lstat(libsymlink, &libStat) < 0) { |
| 2312 | if (errno != ENOENT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2313 | res = error("Failed to stat " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2314 | goto out; |
| 2315 | } |
| 2316 | } else { |
| 2317 | if (S_ISDIR(libStat.st_mode)) { |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2318 | if (delete_dir_contents(libsymlink, 1, nullptr) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2319 | res = error("Failed to delete " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2320 | goto out; |
| 2321 | } |
| 2322 | } else if (S_ISLNK(libStat.st_mode)) { |
| 2323 | if (unlink(libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2324 | res = error("Failed to unlink " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2325 | goto out; |
| 2326 | } |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | if (symlink(asecLibDir, libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2331 | res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2332 | goto out; |
| 2333 | } |
| 2334 | |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 2335 | if (lsetfilecon(libsymlink, con) < 0) { |
| 2336 | res = error("Failed to lsetfilecon " + _libsymlink); |
| 2337 | goto out; |
| 2338 | } |
| 2339 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2340 | out: |
Nick Kralevich | 85958b6 | 2019-01-30 12:32:21 -0800 | [diff] [blame] | 2341 | free(con); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2342 | if (chmod(pkgdir, s.st_mode) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2343 | auto msg = "Failed to cleanup chmod " + _pkgdir; |
| 2344 | if (res.isOk()) { |
| 2345 | res = error(msg); |
| 2346 | } else { |
| 2347 | PLOG(ERROR) << msg; |
| 2348 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2352 | auto msg = "Failed to cleanup chown " + _pkgdir; |
| 2353 | if (res.isOk()) { |
| 2354 | res = error(msg); |
| 2355 | } else { |
| 2356 | PLOG(ERROR) << msg; |
| 2357 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2360 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 2361 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 2362 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2363 | binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, |
| 2364 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, |
| 2365 | const std::string& seInfo) { |
| 2366 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2367 | CHECK_ARGUMENT_UUID(uuid); |
| 2368 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2369 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2370 | |
| 2371 | binder::Status res = ok(); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2372 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 2373 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2374 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2375 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2376 | const char* pkgName = packageName.c_str(); |
| 2377 | const char* seinfo = seInfo.c_str(); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 2378 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2379 | uid_t uid = multiuser_get_uid(userId, appId); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 2380 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2381 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2382 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2383 | res = error("restorecon failed for " + path); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 2384 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2385 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 2386 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 2387 | auto path = create_data_user_de_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 2388 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2389 | res = error("restorecon failed for " + path); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 2390 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2391 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2392 | return res; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 2393 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 2394 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2395 | binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, |
| 2396 | const std::string& instructionSet) { |
| 2397 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2398 | CHECK_ARGUMENT_PATH(oatDir); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2399 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2400 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2401 | const char* oat_dir = oatDir.c_str(); |
| 2402 | const char* instruction_set = instructionSet.c_str(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2403 | char oat_instr_dir[PKG_PATH_MAX]; |
| 2404 | |
| 2405 | if (validate_apk_path(oat_dir)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2406 | return error("Invalid path " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2407 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 2408 | 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] | 2409 | return error("Failed to prepare " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2410 | } |
| 2411 | if (selinux_android_restorecon(oat_dir, 0)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2412 | return error("Failed to restorecon " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2413 | } |
| 2414 | 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] | 2415 | 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] | 2416 | return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2417 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2418 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2419 | } |
| 2420 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2421 | binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { |
| 2422 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2423 | CHECK_ARGUMENT_PATH(packageDir); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2424 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2425 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2426 | if (validate_apk_path(packageDir.c_str())) { |
| 2427 | return error("Invalid path " + packageDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2428 | } |
Oleksandr Peletskyi | d2be632 | 2019-01-24 17:59:45 +0100 | [diff] [blame] | 2429 | if (rm_package_dir(packageDir) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2430 | return error("Failed to delete " + packageDir); |
| 2431 | } |
| 2432 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 2433 | } |
| 2434 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2435 | binder::Status InstalldNativeService::linkFile(const std::string& relativePath, |
| 2436 | const std::string& fromBase, const std::string& toBase) { |
| 2437 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2438 | CHECK_ARGUMENT_PATH(fromBase); |
| 2439 | CHECK_ARGUMENT_PATH(toBase); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2440 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2441 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2442 | const char* relative_path = relativePath.c_str(); |
| 2443 | const char* from_base = fromBase.c_str(); |
| 2444 | const char* to_base = toBase.c_str(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2445 | char from_path[PKG_PATH_MAX]; |
| 2446 | char to_path[PKG_PATH_MAX]; |
| 2447 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); |
| 2448 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); |
| 2449 | |
| 2450 | if (validate_apk_path_subdirs(from_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2451 | return error(StringPrintf("Invalid from path %s", from_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | if (validate_apk_path_subdirs(to_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2455 | return error(StringPrintf("Invalid to path %s", to_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2456 | } |
| 2457 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2458 | if (link(from_path, to_path) < 0) { |
| 2459 | 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] | 2460 | } |
| 2461 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2462 | return ok(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2465 | binder::Status InstalldNativeService::moveAb(const std::string& apkPath, |
| 2466 | const std::string& instructionSet, const std::string& outputPath) { |
| 2467 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2468 | CHECK_ARGUMENT_PATH(apkPath); |
| 2469 | CHECK_ARGUMENT_PATH(outputPath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2470 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2471 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2472 | const char* apk_path = apkPath.c_str(); |
| 2473 | const char* instruction_set = instructionSet.c_str(); |
| 2474 | const char* oat_dir = outputPath.c_str(); |
Andreas Gampe | 0354bd0 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 2475 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2476 | bool success = move_ab(apk_path, instruction_set, oat_dir); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 2477 | return success ? ok() : error(); |
Andreas Gampe | e65b4fa | 2016-03-01 11:46:45 -0800 | [diff] [blame] | 2478 | } |
| 2479 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2480 | binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, |
Andreas Gampe | c523409 | 2017-05-31 16:39:58 -0700 | [diff] [blame] | 2481 | const std::string& instructionSet, const std::unique_ptr<std::string>& outputPath) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2482 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2483 | CHECK_ARGUMENT_PATH(apkPath); |
| 2484 | CHECK_ARGUMENT_PATH(outputPath); |
Jeff Sharkey | 7a9059e | 2017-01-16 19:07:18 -0700 | [diff] [blame] | 2485 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2486 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2487 | const char* apk_path = apkPath.c_str(); |
| 2488 | const char* instruction_set = instructionSet.c_str(); |
Andreas Gampe | c523409 | 2017-05-31 16:39:58 -0700 | [diff] [blame] | 2489 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 2490 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 2491 | bool res = delete_odex(apk_path, instruction_set, oat_dir); |
| 2492 | return res ? ok() : error(); |
Andreas Gampe | 3964da0 | 2016-09-09 17:07:04 -0700 | [diff] [blame] | 2493 | } |
| 2494 | |
Victor Hsieh | 7ebd513 | 2018-01-23 07:52:17 -0800 | [diff] [blame] | 2495 | // This kernel feature is experimental. |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2496 | // TODO: remove local definition once upstreamed |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2497 | #ifndef FS_IOC_ENABLE_VERITY |
| 2498 | |
| 2499 | #define FS_IOC_ENABLE_VERITY _IO('f', 133) |
| 2500 | #define FS_IOC_SET_VERITY_MEASUREMENT _IOW('f', 134, struct fsverity_measurement) |
| 2501 | |
| 2502 | #define FS_VERITY_ALG_SHA256 1 |
| 2503 | |
| 2504 | struct fsverity_measurement { |
| 2505 | __u16 digest_algorithm; |
| 2506 | __u16 digest_size; |
| 2507 | __u32 reserved1; |
| 2508 | __u64 reserved2[3]; |
| 2509 | __u8 digest[]; |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2510 | }; |
| 2511 | |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2512 | #endif |
| 2513 | |
| 2514 | binder::Status InstalldNativeService::installApkVerity(const std::string& filePath, |
Jiyong Park | 3b25ae1 | 2019-11-25 11:06:16 +0900 | [diff] [blame] | 2515 | android::base::unique_fd verityInputAshmem, int32_t contentSize) { |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 2516 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2517 | CHECK_ARGUMENT_PATH(filePath); |
| 2518 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2519 | |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 2520 | if (!android::base::GetBoolProperty(kPropApkVerityMode, false)) { |
| 2521 | return ok(); |
| 2522 | } |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2523 | #ifndef NDEBUG |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2524 | ASSERT_PAGE_SIZE_4K(); |
| 2525 | #endif |
| 2526 | // TODO: also check fsverity support in the current file system if compiled with DEBUG. |
| 2527 | // TODO: change ashmem to some temporary file to support huge apk. |
| 2528 | if (!ashmem_valid(verityInputAshmem.get())) { |
| 2529 | return error("FD is not an ashmem"); |
| 2530 | } |
| 2531 | |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2532 | // 1. Seek to the next page boundary beyond the end of the file. |
Victor Hsieh | 93a9805 | 2018-01-24 14:56:32 -0800 | [diff] [blame] | 2533 | ::android::base::unique_fd wfd(open(filePath.c_str(), O_WRONLY)); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2534 | if (wfd.get() < 0) { |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2535 | return error("Failed to open " + filePath); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2536 | } |
| 2537 | struct stat st; |
| 2538 | if (fstat(wfd.get(), &st) < 0) { |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2539 | return error("Failed to stat " + filePath); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2540 | } |
| 2541 | // fsverity starts from the block boundary. |
Victor Hsieh | 93a9805 | 2018-01-24 14:56:32 -0800 | [diff] [blame] | 2542 | off_t padding = kVerityPageSize - st.st_size % kVerityPageSize; |
| 2543 | if (padding == kVerityPageSize) { |
| 2544 | padding = 0; |
| 2545 | } |
| 2546 | if (lseek(wfd.get(), st.st_size + padding, SEEK_SET) < 0) { |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2547 | return error("Failed to lseek " + filePath); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2548 | } |
| 2549 | |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2550 | // 2. Write everything in the ashmem to the file. Note that allocated |
| 2551 | // ashmem size is multiple of page size, which is different from the |
| 2552 | // actual content size. |
| 2553 | int shmSize = ashmem_get_size_region(verityInputAshmem.get()); |
| 2554 | if (shmSize < 0) { |
| 2555 | return error("Failed to get ashmem size: " + std::to_string(shmSize)); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2556 | } |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2557 | if (contentSize < 0) { |
| 2558 | return error("Invalid content size: " + std::to_string(contentSize)); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2559 | } |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2560 | if (contentSize > shmSize) { |
| 2561 | return error("Content size overflow: " + std::to_string(contentSize) + " > " + |
| 2562 | std::to_string(shmSize)); |
| 2563 | } |
| 2564 | auto data = std::unique_ptr<void, std::function<void (void *)>>( |
Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 2565 | mmap(nullptr, contentSize, PROT_READ, MAP_SHARED, verityInputAshmem.get(), 0), |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2566 | [contentSize] (void* ptr) { |
| 2567 | if (ptr != MAP_FAILED) { |
| 2568 | munmap(ptr, contentSize); |
| 2569 | } |
| 2570 | }); |
| 2571 | |
| 2572 | if (data.get() == MAP_FAILED) { |
| 2573 | return error("Failed to mmap the ashmem"); |
| 2574 | } |
| 2575 | char* cursor = reinterpret_cast<char*>(data.get()); |
| 2576 | int remaining = contentSize; |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2577 | while (remaining > 0) { |
Victor Hsieh | 93a9805 | 2018-01-24 14:56:32 -0800 | [diff] [blame] | 2578 | int ret = TEMP_FAILURE_RETRY(write(wfd.get(), cursor, remaining)); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2579 | if (ret < 0) { |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2580 | return error("Failed to write to " + filePath + " (" + std::to_string(remaining) + |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2581 | + "/" + std::to_string(contentSize) + ")"); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2582 | } |
Victor Hsieh | 93a9805 | 2018-01-24 14:56:32 -0800 | [diff] [blame] | 2583 | cursor += ret; |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2584 | remaining -= ret; |
| 2585 | } |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2586 | wfd.reset(); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2587 | |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2588 | // 3. Enable fsverity (needs readonly fd. Once it's done, the file becomes immutable. |
| 2589 | ::android::base::unique_fd rfd(open(filePath.c_str(), O_RDONLY)); |
| 2590 | if (ioctl(rfd.get(), FS_IOC_ENABLE_VERITY, nullptr) < 0) { |
| 2591 | return error("Failed to enable fsverity on " + filePath); |
Victor Hsieh | 3f16dd6 | 2018-01-13 13:43:11 -0800 | [diff] [blame] | 2592 | } |
| 2593 | return ok(); |
Victor Hsieh | 99de516 | 2017-10-06 14:44:14 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 2596 | binder::Status InstalldNativeService::assertFsverityRootHashMatches(const std::string& filePath, |
| 2597 | const std::vector<uint8_t>& expectedHash) { |
| 2598 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2599 | CHECK_ARGUMENT_PATH(filePath); |
| 2600 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2601 | |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 2602 | if (!android::base::GetBoolProperty(kPropApkVerityMode, false)) { |
| 2603 | return ok(); |
| 2604 | } |
| 2605 | // TODO: also check fsverity support in the current file system if compiled with DEBUG. |
| 2606 | if (expectedHash.size() != kSha256Size) { |
| 2607 | return error("verity hash size should be " + std::to_string(kSha256Size) + " but is " + |
| 2608 | std::to_string(expectedHash.size())); |
| 2609 | } |
| 2610 | |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 2611 | ::android::base::unique_fd fd(open(filePath.c_str(), O_RDONLY)); |
| 2612 | if (fd.get() < 0) { |
| 2613 | return error("Failed to open " + filePath + ": " + strerror(errno)); |
| 2614 | } |
| 2615 | |
Victor Hsieh | 18ac027 | 2018-03-12 16:03:02 -0700 | [diff] [blame] | 2616 | unsigned int buffer_size = sizeof(fsverity_measurement) + kSha256Size; |
| 2617 | std::vector<char> buffer(buffer_size, 0); |
| 2618 | |
| 2619 | fsverity_measurement* config = reinterpret_cast<fsverity_measurement*>(buffer.data()); |
| 2620 | config->digest_algorithm = FS_VERITY_ALG_SHA256; |
| 2621 | config->digest_size = kSha256Size; |
| 2622 | memcpy(config->digest, expectedHash.data(), kSha256Size); |
| 2623 | if (ioctl(fd.get(), FS_IOC_SET_VERITY_MEASUREMENT, config) < 0) { |
Victor Hsieh | c6d738a | 2018-01-20 15:06:39 -0800 | [diff] [blame] | 2624 | // This includes an expected failure case with no FSVerity setup. It normally happens when |
| 2625 | // the apk does not contains the Merkle tree root hash. |
| 2626 | return error("Failed to measure fsverity on " + filePath + ": " + strerror(errno)); |
| 2627 | } |
| 2628 | return ok(); // hashes match |
| 2629 | } |
| 2630 | |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2631 | binder::Status InstalldNativeService::reconcileSecondaryDexFile( |
| 2632 | const std::string& dexPath, const std::string& packageName, int32_t uid, |
| 2633 | const std::vector<std::string>& isas, const std::unique_ptr<std::string>& volumeUuid, |
| 2634 | int32_t storage_flag, bool* _aidl_return) { |
| 2635 | ENFORCE_UID(AID_SYSTEM); |
| 2636 | CHECK_ARGUMENT_UUID(volumeUuid); |
| 2637 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2638 | CHECK_ARGUMENT_PATH(dexPath); |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2639 | std::lock_guard<std::recursive_mutex> lock(mLock); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2640 | |
Calin Juravle | bd96836 | 2017-01-25 01:17:17 -0800 | [diff] [blame] | 2641 | bool result = android::installd::reconcile_secondary_dex_file( |
| 2642 | dexPath, packageName, uid, isas, volumeUuid, storage_flag, _aidl_return); |
| 2643 | return result ? ok() : error(); |
| 2644 | } |
| 2645 | |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2646 | binder::Status InstalldNativeService::hashSecondaryDexFile( |
| 2647 | const std::string& dexPath, const std::string& packageName, int32_t uid, |
| 2648 | const std::unique_ptr<std::string>& volumeUuid, int32_t storageFlag, |
| 2649 | std::vector<uint8_t>* _aidl_return) { |
| 2650 | ENFORCE_UID(AID_SYSTEM); |
| 2651 | CHECK_ARGUMENT_UUID(volumeUuid); |
| 2652 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2653 | CHECK_ARGUMENT_PATH(dexPath); |
Alan Stokes | 753dc71 | 2017-10-16 10:56:00 +0100 | [diff] [blame] | 2654 | |
| 2655 | // mLock is not taken here since we will never modify the file system. |
| 2656 | // If a file is modified just as we are reading it this may result in an |
| 2657 | // anomalous hash, but that's ok. |
| 2658 | bool result = android::installd::hash_secondary_dex_file( |
| 2659 | dexPath, packageName, uid, volumeUuid, storageFlag, _aidl_return); |
| 2660 | return result ? ok() : error(); |
| 2661 | } |
| 2662 | |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2663 | binder::Status InstalldNativeService::invalidateMounts() { |
| 2664 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2665 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2666 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2667 | mStorageMounts.clear(); |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2668 | |
| 2669 | #if !BYPASS_QUOTA |
| 2670 | if (!InvalidateQuotaMounts()) { |
| 2671 | return error("Failed to read mounts"); |
| 2672 | } |
| 2673 | #endif |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2674 | |
| 2675 | std::ifstream in("/proc/mounts"); |
| 2676 | if (!in.is_open()) { |
| 2677 | return error("Failed to read mounts"); |
| 2678 | } |
| 2679 | |
| 2680 | std::string source; |
| 2681 | std::string target; |
| 2682 | std::string ignored; |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2683 | while (!in.eof()) { |
| 2684 | std::getline(in, source, ' '); |
| 2685 | std::getline(in, target, ' '); |
| 2686 | std::getline(in, ignored); |
| 2687 | |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 2688 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
Zim | d4de636 | 2019-12-12 16:22:27 +0000 | [diff] [blame] | 2689 | // TODO(b/146139106): Use sdcardfs mounts on devices running sdcardfs so we don't bypass |
| 2690 | // it's VFS cache |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 2691 | if (target.compare(0, 17, "/mnt/pass_through") == 0) { |
| 2692 | LOG(DEBUG) << "Found storage mount " << source << " at " << target; |
| 2693 | mStorageMounts[source] = target; |
| 2694 | } |
| 2695 | } else { |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2696 | #if !BYPASS_SDCARDFS |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 2697 | if (target.compare(0, 21, "/mnt/runtime/default/") == 0) { |
| 2698 | LOG(DEBUG) << "Found storage mount " << source << " at " << target; |
| 2699 | mStorageMounts[source] = target; |
| 2700 | } |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2701 | #endif |
Zim | 0ce832d | 2019-12-03 17:03:58 +0000 | [diff] [blame] | 2702 | } |
Jeff Sharkey | 66b1a12 | 2017-01-16 20:57:45 -0700 | [diff] [blame] | 2703 | } |
| 2704 | return ok(); |
| 2705 | } |
| 2706 | |
Ricky Wai | ff9d3ea | 2019-11-18 18:18:24 +0000 | [diff] [blame] | 2707 | // Mount volume's CE and DE storage to mirror |
| 2708 | binder::Status InstalldNativeService::onPrivateVolumeMounted( |
| 2709 | const std::unique_ptr<std::string>& uuid) { |
| 2710 | ENFORCE_UID(AID_SYSTEM); |
| 2711 | CHECK_ARGUMENT_UUID(uuid); |
| 2712 | if (!sAppDataIsolationEnabled) { |
| 2713 | return ok(); |
| 2714 | } |
| 2715 | if (!uuid) { |
| 2716 | return error("Should not happen, mounting uuid == null"); |
| 2717 | } |
| 2718 | |
| 2719 | const char* uuid_ = uuid->c_str(); |
| 2720 | // Mount CE mirror |
| 2721 | std::string mirrorVolCePath(StringPrintf("%s/%s", kDataMirrorCePath, uuid_)); |
| 2722 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2723 | if (fs_prepare_dir(mirrorVolCePath.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 2724 | return error("Failed to create CE mirror"); |
| 2725 | } |
| 2726 | auto cePath = StringPrintf("%s/user_ce", create_data_path(uuid_).c_str()); |
| 2727 | if (TEMP_FAILURE_RETRY(mount(cePath.c_str(), mirrorVolCePath.c_str(), NULL, |
| 2728 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, nullptr)) == -1) { |
| 2729 | return error("Failed to mount " + mirrorVolCePath); |
| 2730 | } |
| 2731 | |
| 2732 | // Mount DE mirror |
| 2733 | std::string mirrorVolDePath(StringPrintf("%s/%s", kDataMirrorDePath, uuid_)); |
| 2734 | if (fs_prepare_dir(mirrorVolDePath.c_str(), 0700, AID_ROOT, AID_ROOT) != 0) { |
| 2735 | return error("Failed to create DE mirror"); |
| 2736 | } |
| 2737 | auto dePath = StringPrintf("%s/user_de", create_data_path(uuid_).c_str()); |
| 2738 | if (TEMP_FAILURE_RETRY(mount(dePath.c_str(), mirrorVolDePath.c_str(), NULL, |
| 2739 | MS_NOSUID | MS_NODEV | MS_NOATIME | MS_BIND | MS_NOEXEC, nullptr)) == -1) { |
| 2740 | return error("Failed to mount " + mirrorVolDePath); |
| 2741 | } |
| 2742 | return ok(); |
| 2743 | } |
| 2744 | |
| 2745 | // Unmount volume's CE and DE storage from mirror |
| 2746 | binder::Status InstalldNativeService::onPrivateVolumeRemoved( |
| 2747 | const std::unique_ptr<std::string>& uuid) { |
| 2748 | ENFORCE_UID(AID_SYSTEM); |
| 2749 | CHECK_ARGUMENT_UUID(uuid); |
| 2750 | if (!sAppDataIsolationEnabled) { |
| 2751 | return ok(); |
| 2752 | } |
| 2753 | if (!uuid) { |
| 2754 | // It happens when private volume failed to mount. |
| 2755 | LOG(INFO) << "Ignore unmount uuid=null"; |
| 2756 | return ok(); |
| 2757 | } |
| 2758 | const char* uuid_ = uuid->c_str(); |
| 2759 | |
| 2760 | binder::Status res = ok(); |
| 2761 | |
| 2762 | std::string mirrorCeVolPath(StringPrintf("%s/%s", kDataMirrorCePath, uuid_)); |
| 2763 | std::string mirrorDeVolPath(StringPrintf("%s/%s", kDataMirrorDePath, uuid_)); |
| 2764 | |
| 2765 | // Unmount CE storage |
| 2766 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2767 | if (TEMP_FAILURE_RETRY(umount(mirrorCeVolPath.c_str())) != 0) { |
| 2768 | if (errno != ENOENT) { |
| 2769 | res = error(StringPrintf("Failed to umount %s %s", mirrorCeVolPath.c_str(), |
| 2770 | strerror(errno))); |
| 2771 | } |
| 2772 | } |
| 2773 | if (delete_dir_contents_and_dir(mirrorCeVolPath, true) != 0) { |
| 2774 | res = error("Failed to delete " + mirrorCeVolPath); |
| 2775 | } |
| 2776 | |
| 2777 | // Unmount DE storage |
| 2778 | if (TEMP_FAILURE_RETRY(umount(mirrorDeVolPath.c_str())) != 0) { |
| 2779 | if (errno != ENOENT) { |
| 2780 | res = error(StringPrintf("Failed to umount %s %s", mirrorDeVolPath.c_str(), |
| 2781 | strerror(errno))); |
| 2782 | } |
| 2783 | } |
| 2784 | if (delete_dir_contents_and_dir(mirrorDeVolPath, true) != 0) { |
| 2785 | res = error("Failed to delete " + mirrorDeVolPath); |
| 2786 | } |
| 2787 | return res; |
| 2788 | } |
| 2789 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2790 | std::string InstalldNativeService::findDataMediaPath( |
| 2791 | const std::unique_ptr<std::string>& uuid, userid_t userid) { |
| 2792 | std::lock_guard<std::recursive_mutex> lock(mMountsLock); |
| 2793 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 2794 | auto path = StringPrintf("%s/media", create_data_path(uuid_).c_str()); |
Zim | d4de636 | 2019-12-12 16:22:27 +0000 | [diff] [blame] | 2795 | if (android::base::GetBoolProperty(kFuseProp, false)) { |
| 2796 | // TODO(b/146139106): This is only safe on devices not running sdcardfs where there is no |
| 2797 | // risk of bypassing the sdcardfs VFS cache |
| 2798 | |
| 2799 | // Always use the lower filesystem path on FUSE enabled devices not running sdcardfs |
| 2800 | // The upper filesystem path, /mnt/pass_through/<userid>/<vol>/ which was a bind mount |
| 2801 | // to the lower filesytem may have been unmounted already when a user is |
| 2802 | // removed and the path will now be pointing to a tmpfs without content |
| 2803 | return StringPrintf("%s/%u", path.c_str(), userid); |
| 2804 | } |
| 2805 | |
Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 2806 | auto resolved = mStorageMounts[path]; |
| 2807 | if (resolved.empty()) { |
| 2808 | LOG(WARNING) << "Failed to find storage mount for " << path; |
| 2809 | resolved = path; |
| 2810 | } |
| 2811 | return StringPrintf("%s/%u", resolved.c_str(), userid); |
| 2812 | } |
| 2813 | |
Jeff Sharkey | 325b8c9 | 2017-02-21 10:00:53 -0700 | [diff] [blame] | 2814 | binder::Status InstalldNativeService::isQuotaSupported( |
Risan | 5f30826 | 2018-10-26 12:06:58 -0600 | [diff] [blame] | 2815 | const std::unique_ptr<std::string>& uuid, bool* _aidl_return) { |
| 2816 | auto uuidString = uuid ? *uuid : ""; |
| 2817 | *_aidl_return = IsQuotaSupported(uuidString); |
Jeff Sharkey | 325b8c9 | 2017-02-21 10:00:53 -0700 | [diff] [blame] | 2818 | return ok(); |
| 2819 | } |
| 2820 | |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2821 | binder::Status InstalldNativeService::prepareAppProfile(const std::string& packageName, |
| 2822 | int32_t userId, int32_t appId, const std::string& profileName, const std::string& codePath, |
| 2823 | const std::unique_ptr<std::string>& dexMetadata, bool* _aidl_return) { |
| 2824 | ENFORCE_UID(AID_SYSTEM); |
| 2825 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 5359430 | 2018-02-24 18:59:39 -0700 | [diff] [blame] | 2826 | CHECK_ARGUMENT_PATH(codePath); |
Calin Juravle | bc5ab87 | 2018-01-18 22:32:58 -0800 | [diff] [blame] | 2827 | std::lock_guard<std::recursive_mutex> lock(mLock); |
| 2828 | |
| 2829 | *_aidl_return = prepare_app_profile(packageName, userId, appId, profileName, codePath, |
| 2830 | dexMetadata); |
| 2831 | return ok(); |
| 2832 | } |
| 2833 | |
Narayan Kamath | 28ab93b | 2019-05-15 18:29:44 +0100 | [diff] [blame] | 2834 | binder::Status InstalldNativeService::migrateLegacyObbData() { |
| 2835 | ENFORCE_UID(AID_SYSTEM); |
| 2836 | // NOTE: The lint warning doesn't apply to the use of system(3) with |
| 2837 | // absolute parse and no command line arguments. |
| 2838 | if (system("/system/bin/migrate_legacy_obb_data.sh") != 0) { // NOLINT(cert-env33-c) |
| 2839 | LOG(ERROR) << "Unable to migrate legacy obb data"; |
| 2840 | } |
| 2841 | |
| 2842 | return ok(); |
| 2843 | } |
| 2844 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 2845 | } // namespace installd |
| 2846 | } // namespace android |