Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright 2008, The Android Open Source Project |
| 3 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | ** you may not use this file except in compliance with the License. |
| 6 | ** You may obtain a copy of the License at |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** |
Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software |
| 11 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | ** See the License for the specific language governing permissions and |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 17 | #include "InstalldNativeService.h" |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 18 | |
| 19 | #include <errno.h> |
| 20 | #include <inttypes.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 21 | #include <fstream> |
| 22 | #include <fts.h> |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 23 | #include <regex> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 25 | #include <string.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 26 | #include <sys/capability.h> |
| 27 | #include <sys/file.h> |
| 28 | #include <sys/resource.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 29 | #include <sys/quota.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <sys/stat.h> |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 31 | #include <sys/statvfs.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 32 | #include <sys/types.h> |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 33 | #include <sys/wait.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 34 | #include <sys/xattr.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 35 | #include <unistd.h> |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 36 | |
Andreas Gampe | 6fb5a01 | 2016-06-03 16:09:32 -0700 | [diff] [blame] | 37 | #include <android-base/logging.h> |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Roland Levillain | 64b59cc | 2016-04-05 16:41:12 +0100 | [diff] [blame] | 40 | #include <android-base/unique_fd.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 41 | #include <cutils/fs.h> |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 42 | #include <cutils/properties.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 43 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 44 | #include <log/log.h> // TODO: Move everything to base/logging. |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 45 | #include <logwrap/logwrap.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 46 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 47 | #include <selinux/android.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 48 | #include <system/thread_defs.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 50 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 51 | #include "globals.h" |
| 52 | #include "installd_deps.h" |
| 53 | #include "otapreopt_utils.h" |
| 54 | #include "utils.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 55 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 56 | #include "MatchExtensionGen.h" |
| 57 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 58 | #ifndef LOG_TAG |
| 59 | #define LOG_TAG "installd" |
| 60 | #endif |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 61 | |
| 62 | using android::base::StringPrintf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 63 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 64 | namespace android { |
| 65 | namespace installd { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 66 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 67 | static constexpr const char* kCpPath = "/system/bin/cp"; |
| 68 | static constexpr const char* kXattrDefault = "user.default"; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 69 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 70 | static constexpr const char* PKG_LIB_POSTFIX = "/lib"; |
| 71 | static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; |
| 72 | static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; |
| 73 | |
| 74 | static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; |
| 75 | static constexpr const char* IDMAP_SUFFIX = "@idmap"; |
| 76 | |
| 77 | // NOTE: keep in sync with StorageManager |
| 78 | static constexpr int FLAG_STORAGE_DE = 1 << 0; |
| 79 | static constexpr int FLAG_STORAGE_CE = 1 << 1; |
| 80 | |
| 81 | // NOTE: keep in sync with Installer |
| 82 | static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; |
| 83 | static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 84 | static constexpr int FLAG_USE_QUOTA = 1 << 12; |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 85 | |
Janis Danisevskis | eecb2d2 | 2016-01-12 14:45:55 +0000 | [diff] [blame] | 86 | #define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 87 | namespace { |
| 88 | |
| 89 | constexpr const char* kDump = "android.permission.DUMP"; |
| 90 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 91 | static binder::Status ok() { |
| 92 | return binder::Status::ok(); |
| 93 | } |
| 94 | |
| 95 | static binder::Status exception(uint32_t code, const std::string& msg) { |
| 96 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 97 | } |
| 98 | |
| 99 | static binder::Status error() { |
| 100 | return binder::Status::fromServiceSpecificError(errno); |
| 101 | } |
| 102 | |
| 103 | static binder::Status error(const std::string& msg) { |
| 104 | PLOG(ERROR) << msg; |
| 105 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 106 | } |
| 107 | |
| 108 | static binder::Status error(uint32_t code, const std::string& msg) { |
| 109 | LOG(ERROR) << msg << " (" << code << ")"; |
| 110 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); |
| 111 | } |
| 112 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 113 | binder::Status checkPermission(const char* permission) { |
| 114 | pid_t pid; |
| 115 | uid_t uid; |
| 116 | |
| 117 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), |
| 118 | reinterpret_cast<int32_t*>(&uid))) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 119 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 120 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 121 | return exception(binder::Status::EX_SECURITY, |
| 122 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | binder::Status checkUid(uid_t expectedUid) { |
| 127 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 128 | if (uid == expectedUid || uid == AID_ROOT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 129 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 130 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 131 | return exception(binder::Status::EX_SECURITY, |
| 132 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { |
| 137 | if (!uuid || is_valid_filename(*uuid)) { |
| 138 | return ok(); |
| 139 | } else { |
| 140 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 141 | StringPrintf("UUID %s is malformed", uuid->c_str())); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | binder::Status checkArgumentPackageName(const std::string& packageName) { |
| 146 | if (is_valid_package_name(packageName.c_str())) { |
| 147 | return ok(); |
| 148 | } else { |
| 149 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 150 | StringPrintf("Package name %s is malformed", packageName.c_str())); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | #define ENFORCE_UID(uid) { \ |
| 155 | binder::Status status = checkUid((uid)); \ |
| 156 | if (!status.isOk()) { \ |
| 157 | return status; \ |
| 158 | } \ |
| 159 | } |
| 160 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 161 | #define CHECK_ARGUMENT_UUID(uuid) { \ |
| 162 | binder::Status status = checkArgumentUuid((uuid)); \ |
| 163 | if (!status.isOk()) { \ |
| 164 | return status; \ |
| 165 | } \ |
| 166 | } |
| 167 | |
| 168 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ |
| 169 | binder::Status status = \ |
| 170 | checkArgumentPackageName((packageName)); \ |
| 171 | if (!status.isOk()) { \ |
| 172 | return status; \ |
| 173 | } \ |
| 174 | } |
| 175 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 176 | } // namespace |
| 177 | |
| 178 | status_t InstalldNativeService::start() { |
| 179 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 180 | status_t ret = BinderService<InstalldNativeService>::publish(); |
| 181 | if (ret != android::OK) { |
| 182 | return ret; |
| 183 | } |
| 184 | sp<ProcessState> ps(ProcessState::self()); |
| 185 | ps->startThreadPool(); |
| 186 | ps->giveThreadPoolName(); |
| 187 | return android::OK; |
| 188 | } |
| 189 | |
| 190 | status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { |
| 191 | const binder::Status dump_permission = checkPermission(kDump); |
| 192 | if (!dump_permission.isOk()) { |
| 193 | const String8 msg(dump_permission.toString8()); |
| 194 | write(fd, msg.string(), msg.size()); |
| 195 | return PERMISSION_DENIED; |
| 196 | } |
| 197 | |
| 198 | std::string msg = "installd is happy\n"; |
| 199 | write(fd, msg.c_str(), strlen(msg.c_str())); |
| 200 | return NO_ERROR; |
| 201 | } |
| 202 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 203 | /** |
| 204 | * Perform restorecon of the given path, but only perform recursive restorecon |
| 205 | * if the label of that top-level file actually changed. This can save us |
| 206 | * significant time by avoiding no-op traversals of large filesystem trees. |
| 207 | */ |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 208 | static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid, |
| 209 | bool existing) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 210 | int res = 0; |
| 211 | char* before = nullptr; |
| 212 | char* after = nullptr; |
| 213 | |
| 214 | // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by |
| 215 | // libselinux. Not needed here. |
| 216 | |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 217 | if (lgetfilecon(path.c_str(), &before) < 0) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 218 | PLOG(ERROR) << "Failed before getfilecon for " << path; |
| 219 | goto fail; |
| 220 | } |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 221 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 222 | PLOG(ERROR) << "Failed top-level restorecon for " << path; |
| 223 | goto fail; |
| 224 | } |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 225 | if (lgetfilecon(path.c_str(), &after) < 0) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 226 | PLOG(ERROR) << "Failed after getfilecon for " << path; |
| 227 | goto fail; |
| 228 | } |
| 229 | |
| 230 | // If the initial top-level restorecon above changed the label, then go |
| 231 | // back and restorecon everything recursively |
| 232 | if (strcmp(before, after)) { |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 233 | if (existing) { |
| 234 | LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " |
| 235 | << path << "; running recursive restorecon"; |
| 236 | } |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 237 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 238 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { |
| 239 | PLOG(ERROR) << "Failed recursive restorecon for " << path; |
| 240 | goto fail; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | goto done; |
| 245 | fail: |
| 246 | res = -1; |
| 247 | done: |
| 248 | free(before); |
| 249 | free(after); |
| 250 | return res; |
| 251 | } |
| 252 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 253 | 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] | 254 | const std::string& seInfo, uid_t uid, bool existing) { |
| 255 | return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid, |
| 256 | existing); |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 257 | } |
| 258 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 259 | static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 260 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { |
| 261 | PLOG(ERROR) << "Failed to prepare " << path; |
| 262 | return -1; |
| 263 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Prepare an app cache directory, which offers to fix-up the GID and |
| 269 | * directory mode flags during a platform upgrade. |
| 270 | */ |
| 271 | static int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, |
| 272 | uid_t uid, gid_t gid) { |
| 273 | auto path = StringPrintf("%s/%s", parent.c_str(), name); |
| 274 | struct stat st; |
| 275 | if (stat(path.c_str(), &st) != 0) { |
| 276 | if (errno == ENOENT) { |
| 277 | // This is fine, just create it |
| 278 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { |
| 279 | PLOG(ERROR) << "Failed to prepare " << path; |
| 280 | return -1; |
| 281 | } else { |
| 282 | return 0; |
| 283 | } |
| 284 | } else { |
| 285 | PLOG(ERROR) << "Failed to stat " << path; |
| 286 | return -1; |
| 287 | } |
| 288 | } |
| 289 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame^] | 290 | mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 291 | if (st.st_uid != uid) { |
| 292 | // Mismatched UID is real trouble; we can't recover |
| 293 | LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid |
| 294 | << " but expected " << uid; |
| 295 | return -1; |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame^] | 296 | } else if (st.st_gid == gid && actual_mode == target_mode) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 297 | // Everything looks good! |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | // Directory is owned correctly, but GID or mode mismatch means it's |
| 302 | // probably a platform upgrade so we need to fix them |
| 303 | FTS *fts; |
| 304 | FTSENT *p; |
| 305 | char *argv[] = { (char*) path.c_str(), nullptr }; |
| 306 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { |
| 307 | PLOG(ERROR) << "Failed to fts_open " << path; |
| 308 | return -1; |
| 309 | } |
| 310 | while ((p = fts_read(fts)) != NULL) { |
| 311 | switch (p->fts_info) { |
| 312 | case FTS_DP: |
| 313 | if (chmod(p->fts_accpath, target_mode) != 0) { |
| 314 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; |
| 315 | } |
| 316 | // Intentional fall through to also set GID |
| 317 | case FTS_F: |
| 318 | if (chown(p->fts_accpath, -1, gid) != 0) { |
| 319 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 320 | } |
| 321 | break; |
| 322 | case FTS_SL: |
| 323 | case FTS_SLNONE: |
| 324 | if (lchown(p->fts_accpath, -1, gid) != 0) { |
| 325 | PLOG(WARNING) << "Failed to chown " << p->fts_path; |
| 326 | } |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | fts_close(fts); |
| 331 | return 0; |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 332 | } |
| 333 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 334 | binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, |
| 335 | 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] | 336 | const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 337 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 338 | CHECK_ARGUMENT_UUID(uuid); |
| 339 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 340 | |
| 341 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 342 | const char* pkgname = packageName.c_str(); |
| 343 | |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 344 | // Assume invalid inode unless filled in below |
| 345 | if (_aidl_return != nullptr) *_aidl_return = -1; |
| 346 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame^] | 347 | int32_t uid = multiuser_get_uid(userId, appId); |
| 348 | int32_t cacheGid = multiuser_get_cache_gid(userId, appId); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 349 | mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; |
| 350 | |
Jeff Sharkey | 24ef15b | 2017-01-12 19:27:03 -0700 | [diff] [blame^] | 351 | // If UID doesn't have a specific cache GID, use UID value |
| 352 | if (cacheGid == -1) { |
| 353 | cacheGid = uid; |
| 354 | } |
| 355 | |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 356 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 357 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 358 | bool existing = (access(path.c_str(), F_OK) == 0); |
| 359 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 360 | if (prepare_app_dir(path, targetMode, uid) || |
| 361 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || |
| 362 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 363 | return error("Failed to prepare " + path); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 367 | if (restorecon_app_data_lazy(path, seInfo, uid, existing) || |
| 368 | restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) || |
| 369 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 370 | return error("Failed to restorecon " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 371 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 372 | |
| 373 | // Remember inode numbers of cache directories so that we can clear |
| 374 | // contents while CE storage is locked |
| 375 | if (write_path_inode(path, "cache", kXattrInodeCache) || |
| 376 | write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 377 | return error("Failed to write_path_inode for " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 378 | } |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 379 | |
| 380 | // And return the CE inode of the top-level data directory so we can |
| 381 | // clear contents while CE storage is locked |
| 382 | if ((_aidl_return != nullptr) |
| 383 | && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { |
| 384 | return error("Failed to get_path_inode for " + path); |
| 385 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 386 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 387 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 388 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 389 | bool existing = (access(path.c_str(), F_OK) == 0); |
| 390 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 391 | if (prepare_app_dir(path, targetMode, uid) || |
| 392 | prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) || |
| 393 | prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 394 | return error("Failed to prepare " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 395 | } |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 396 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 397 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 35b83df | 2016-12-21 09:33:55 -0700 | [diff] [blame] | 398 | if (restorecon_app_data_lazy(path, seInfo, uid, existing)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 399 | return error("Failed to restorecon " + path); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 400 | } |
| 401 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 402 | if (property_get_bool("dalvik.vm.usejitprofiles", false)) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 403 | const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 404 | // read-write-execute only for the app user. |
| 405 | if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 406 | return error("Failed to prepare " + profile_path); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 407 | } |
Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 408 | std::string profile_file = create_primary_profile(profile_path); |
| 409 | // read-write only for the app user. |
| 410 | if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 411 | return error("Failed to prepare " + profile_path); |
Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 412 | } |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 413 | const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); |
| 414 | // dex2oat/profman runs under the shared app gid and it needs to read/write reference |
| 415 | // profiles. |
Jeff Sharkey | adddd98 | 2017-01-03 14:33:50 -0700 | [diff] [blame] | 416 | int shared_app_gid = multiuser_get_shared_gid(0, appId); |
Jeff Sharkey | 2e6dc9b | 2016-12-15 14:50:11 -0700 | [diff] [blame] | 417 | if ((shared_app_gid != -1) && fs_prepare_dir_strict( |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 418 | ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 419 | return error("Failed to prepare " + ref_profile_path); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 422 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 423 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 426 | binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, |
| 427 | const std::string& packageName, int32_t userId, int32_t flags) { |
| 428 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 429 | CHECK_ARGUMENT_UUID(uuid); |
| 430 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 431 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 432 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 433 | const char* pkgname = packageName.c_str(); |
| 434 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 435 | // This method only exists to upgrade system apps that have requested |
| 436 | // forceDeviceEncrypted, so their default storage always lives in a |
| 437 | // consistent location. This only works on non-FBE devices, since we |
| 438 | // never want to risk exposing data on a device with real CE/DE storage. |
| 439 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 440 | auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 441 | auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 442 | |
| 443 | // If neither directory is marked as default, assume CE is default |
| 444 | if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 |
| 445 | && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 446 | if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 447 | return error("Failed to mark default storage " + ce_path); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | // Migrate default data location if needed |
| 452 | auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; |
| 453 | auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; |
| 454 | |
| 455 | if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 456 | LOG(WARNING) << "Requested default storage " << target |
| 457 | << " is not active; migrating from " << source; |
| 458 | if (delete_dir_contents_and_dir(target) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 459 | return error("Failed to delete " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 460 | } |
| 461 | if (rename(source.c_str(), target.c_str()) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 462 | return error("Failed to rename " + source + " to " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 466 | return ok(); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 469 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 470 | binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { |
| 471 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 472 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 473 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 474 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 475 | binder::Status res = ok(); |
| 476 | if (!clear_reference_profile(pkgname)) { |
| 477 | res = error("Failed to clear reference profile for " + packageName); |
| 478 | } |
| 479 | if (!clear_current_profiles(pkgname)) { |
| 480 | res = error("Failed to clear current profiles for " + packageName); |
| 481 | } |
| 482 | return res; |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 485 | binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, |
| 486 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 487 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 488 | CHECK_ARGUMENT_UUID(uuid); |
| 489 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 490 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 491 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 492 | const char* pkgname = packageName.c_str(); |
| 493 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 494 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 495 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 496 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 497 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 498 | path = read_path_inode(path, "cache", kXattrInodeCache); |
| 499 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 500 | path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 501 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 502 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 503 | if (delete_dir_contents(path) != 0) { |
| 504 | res = error("Failed to delete contents of " + path); |
| 505 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 508 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 509 | std::string suffix = ""; |
| 510 | bool only_cache = false; |
| 511 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 512 | suffix = CACHE_DIR_POSTFIX; |
| 513 | only_cache = true; |
| 514 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 515 | suffix = CODE_CACHE_DIR_POSTFIX; |
| 516 | only_cache = true; |
| 517 | } |
| 518 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 519 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 520 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 521 | if (delete_dir_contents(path) != 0) { |
| 522 | res = error("Failed to delete contents of " + path); |
| 523 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 524 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 525 | if (!only_cache) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 526 | if (!clear_current_profile(pkgname, userId)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 527 | res = error("Failed to clear current profile for " + packageName); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 530 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 531 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 532 | } |
| 533 | |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 534 | static int destroy_app_reference_profile(const char *pkgname) { |
| 535 | return delete_dir_contents_and_dir( |
| 536 | create_data_ref_profile_package_path(pkgname), |
| 537 | /*ignore_if_missing*/ true); |
| 538 | } |
| 539 | |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 540 | static int destroy_app_current_profiles(const char *pkgname, userid_t userid) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 541 | return delete_dir_contents_and_dir( |
| 542 | create_data_user_profile_package_path(userid, pkgname), |
| 543 | /*ignore_if_missing*/ true); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 546 | binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { |
| 547 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 548 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 549 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 550 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 551 | binder::Status res = ok(); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 552 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); |
| 553 | for (auto user : users) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 554 | if (destroy_app_current_profiles(pkgname, user) != 0) { |
| 555 | res = error("Failed to destroy current profiles for " + packageName); |
| 556 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 557 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 558 | if (destroy_app_reference_profile(pkgname) != 0) { |
| 559 | res = error("Failed to destroy reference profile for " + packageName); |
| 560 | } |
| 561 | return res; |
Calin Juravle | caa6b80 | 2016-03-22 14:15:52 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 564 | binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, |
| 565 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 566 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 567 | CHECK_ARGUMENT_UUID(uuid); |
| 568 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 569 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 570 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 571 | const char* pkgname = packageName.c_str(); |
| 572 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 573 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 574 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 575 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
| 576 | if (delete_dir_contents_and_dir(path) != 0) { |
| 577 | res = error("Failed to delete " + path); |
| 578 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 579 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 580 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 581 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 582 | if (delete_dir_contents_and_dir(path) != 0) { |
| 583 | res = error("Failed to delete " + path); |
| 584 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 585 | destroy_app_current_profiles(pkgname, userId); |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 586 | // TODO(calin): If the package is still installed by other users it's probably |
| 587 | // beneficial to keep the reference profile around. |
| 588 | // Verify if it's ok to do that. |
| 589 | destroy_app_reference_profile(pkgname); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 590 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 591 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 594 | binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, |
| 595 | const std::unique_ptr<std::string>& toUuid, const std::string& packageName, |
| 596 | const std::string& dataAppName, int32_t appId, const std::string& seInfo, |
| 597 | int32_t targetSdkVersion) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 598 | ENFORCE_UID(AID_SYSTEM); |
| 599 | CHECK_ARGUMENT_UUID(fromUuid); |
| 600 | CHECK_ARGUMENT_UUID(toUuid); |
| 601 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 602 | |
| 603 | const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; |
| 604 | const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; |
| 605 | const char* package_name = packageName.c_str(); |
| 606 | const char* data_app_name = dataAppName.c_str(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 607 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 608 | binder::Status res = ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 609 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 610 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 611 | // Copy app |
| 612 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 613 | auto from = create_data_app_package_path(from_uuid, data_app_name); |
| 614 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
| 615 | auto to_parent = create_data_app_path(to_uuid); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 616 | |
| 617 | char *argv[] = { |
| 618 | (char*) kCpPath, |
| 619 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 620 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 621 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 622 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 623 | (char*) "-d", /* don't dereference symlinks */ |
| 624 | (char*) from.c_str(), |
| 625 | (char*) to_parent.c_str() |
| 626 | }; |
| 627 | |
| 628 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 629 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 630 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 631 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 632 | goto fail; |
| 633 | } |
| 634 | |
| 635 | if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 636 | res = error("Failed to restorecon " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 637 | goto fail; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // Copy private data for all known users |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 642 | for (auto user : users) { |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 643 | |
| 644 | // Data source may not exist for all users; that's okay |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 645 | auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 646 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 647 | LOG(INFO) << "Missing source " << from_ce; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 648 | continue; |
| 649 | } |
| 650 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 651 | if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame] | 652 | seInfo, targetSdkVersion, nullptr).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 653 | res = error("Failed to create package target"); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 654 | goto fail; |
| 655 | } |
| 656 | |
| 657 | char *argv[] = { |
| 658 | (char*) kCpPath, |
| 659 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 660 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 661 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 662 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 663 | (char*) "-d", /* don't dereference symlinks */ |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 664 | nullptr, |
| 665 | nullptr |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 666 | }; |
| 667 | |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 668 | { |
| 669 | auto from = create_data_user_de_package_path(from_uuid, user, package_name); |
| 670 | auto to = create_data_user_de_path(to_uuid, user); |
| 671 | argv[6] = (char*) from.c_str(); |
| 672 | argv[7] = (char*) to.c_str(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 673 | |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 674 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 675 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
| 676 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 677 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 678 | goto fail; |
| 679 | } |
| 680 | } |
| 681 | { |
| 682 | auto from = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 683 | auto to = create_data_user_ce_path(to_uuid, user); |
| 684 | argv[6] = (char*) from.c_str(); |
| 685 | argv[7] = (char*) to.c_str(); |
| 686 | |
| 687 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 688 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
| 689 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 690 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 691 | goto fail; |
| 692 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 695 | if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
| 696 | appId, seInfo).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 697 | res = error("Failed to restorecon"); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 698 | goto fail; |
| 699 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 702 | // We let the framework scan the new location and persist that before |
| 703 | // deleting the data in the old location; this ordering ensures that |
| 704 | // we can recover from things like battery pulls. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 705 | return ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 706 | |
| 707 | fail: |
| 708 | // Nuke everything we might have already copied |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 709 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 710 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 711 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 712 | LOG(WARNING) << "Failed to rollback " << to; |
| 713 | } |
| 714 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 715 | for (auto user : users) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 716 | { |
| 717 | auto to = create_data_user_de_package_path(to_uuid, user, package_name); |
| 718 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 719 | LOG(WARNING) << "Failed to rollback " << to; |
| 720 | } |
| 721 | } |
| 722 | { |
| 723 | auto to = create_data_user_ce_package_path(to_uuid, user, package_name); |
| 724 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 725 | LOG(WARNING) << "Failed to rollback " << to; |
| 726 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 729 | return res; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 732 | binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, |
| 733 | int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { |
| 734 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 735 | CHECK_ARGUMENT_UUID(uuid); |
| 736 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 737 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 738 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 739 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 740 | if (ensure_config_user_dirs(userId) != 0) { |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 741 | return error(StringPrintf("Failed to ensure dirs for %d", userId)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 742 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 743 | } |
| 744 | } |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 745 | return ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, |
| 749 | int32_t userId, int32_t flags) { |
| 750 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 751 | CHECK_ARGUMENT_UUID(uuid); |
| 752 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 753 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 754 | binder::Status res = ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 755 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 756 | auto path = create_data_user_de_path(uuid_, userId); |
| 757 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 758 | res = error("Failed to delete " + path); |
| 759 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 760 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 761 | path = create_data_misc_legacy_path(userId); |
| 762 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 763 | res = error("Failed to delete " + path); |
| 764 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 765 | path = create_data_user_profile_path(userId); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 766 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 767 | res = error("Failed to delete " + path); |
| 768 | } |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 769 | } |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 770 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 771 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 772 | auto path = create_data_user_ce_path(uuid_, userId); |
| 773 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 774 | res = error("Failed to delete " + path); |
| 775 | } |
| 776 | path = create_data_media_path(uuid_, userId); |
| 777 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 778 | res = error("Failed to delete " + path); |
| 779 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 780 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 781 | return res; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 782 | } |
| 783 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 784 | /* Try to ensure free_size bytes of storage are available. |
| 785 | * Returns 0 on success. |
| 786 | * This is rather simple-minded because doing a full LRU would |
| 787 | * be potentially memory-intensive, and without atime it would |
| 788 | * also require that apps constantly modify file metadata even |
| 789 | * when just reading from the cache, which is pretty awful. |
| 790 | */ |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 791 | binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, |
| 792 | int64_t freeStorageSize) { |
| 793 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 794 | CHECK_ARGUMENT_UUID(uuid); |
| 795 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 796 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 797 | cache_t* cache; |
| 798 | int64_t avail; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 799 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 800 | auto data_path = create_data_path(uuid_); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 801 | |
| 802 | avail = data_disk_free(data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 803 | if (avail < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 804 | return error("Failed to determine free space for " + data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 805 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 806 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 807 | ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); |
| 808 | if (avail >= freeStorageSize) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 809 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 810 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 811 | |
| 812 | cache = start_cache_collection(); |
| 813 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 814 | auto users = get_known_users(uuid_); |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 815 | for (auto user : users) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 816 | add_cache_files(cache, create_data_user_ce_path(uuid_, user)); |
| 817 | add_cache_files(cache, create_data_user_de_path(uuid_, user)); |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 818 | add_cache_files(cache, |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 819 | StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 820 | } |
| 821 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 822 | clear_cache_files(data_path, cache, freeStorageSize); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 823 | finish_cache_collection(cache); |
| 824 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 825 | avail = data_disk_free(data_path); |
| 826 | if (avail >= freeStorageSize) { |
| 827 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 828 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 829 | return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, |
| 830 | freeStorageSize, data_path.c_str(), avail)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 831 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 834 | binder::Status InstalldNativeService::rmdex(const std::string& codePath, |
| 835 | const std::string& instructionSet) { |
| 836 | ENFORCE_UID(AID_SYSTEM); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 837 | char dex_path[PKG_PATH_MAX]; |
| 838 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 839 | const char* path = codePath.c_str(); |
| 840 | const char* instruction_set = instructionSet.c_str(); |
| 841 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 842 | if (validate_apk_path(path) && validate_system_app_path(path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 843 | return error("Invalid path " + codePath); |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 846 | if (!create_cache_path(dex_path, path, instruction_set)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 847 | return error("Failed to create cache path for " + codePath); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 848 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 849 | |
| 850 | ALOGV("unlink %s\n", dex_path); |
| 851 | if (unlink(dex_path) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 852 | return error(StringPrintf("Failed to unlink %s", dex_path)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 853 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 854 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 858 | struct stats { |
| 859 | int64_t codeSize; |
| 860 | int64_t dataSize; |
| 861 | int64_t cacheSize; |
| 862 | }; |
| 863 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 864 | #if MEASURE_DEBUG |
| 865 | static std::string toString(std::vector<int64_t> values) { |
| 866 | std::stringstream res; |
| 867 | res << "["; |
| 868 | for (size_t i = 0; i < values.size(); i++) { |
| 869 | res << values[i]; |
| 870 | if (i < values.size() - 1) { |
| 871 | res << ","; |
| 872 | } |
| 873 | } |
| 874 | res << "]"; |
| 875 | return res.str(); |
| 876 | } |
| 877 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 878 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 879 | static std::string findDeviceForUuid(const std::unique_ptr<std::string>& uuid) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 880 | auto path = create_data_path(uuid ? uuid->c_str() : nullptr); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 881 | std::ifstream in("/proc/mounts"); |
| 882 | if (!in.is_open()) { |
| 883 | PLOG(ERROR) << "Failed to read mounts"; |
| 884 | return ""; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 885 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 886 | std::string source; |
| 887 | std::string target; |
| 888 | while (!in.eof()) { |
| 889 | std::getline(in, source, ' '); |
| 890 | std::getline(in, target, ' '); |
| 891 | if (target == path) { |
| 892 | return source; |
| 893 | } |
| 894 | // Skip to next line |
| 895 | std::getline(in, source); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 896 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 897 | PLOG(ERROR) << "Failed to resolve block device for " << path; |
| 898 | return ""; |
| 899 | } |
| 900 | |
| 901 | static void collectQuotaStats(const std::string& device, int32_t userId, |
| 902 | int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) { |
| 903 | if (device.empty()) return; |
| 904 | |
| 905 | struct dqblk dq; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 906 | |
| 907 | uid_t uid = multiuser_get_uid(userId, appId); |
| 908 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, |
| 909 | reinterpret_cast<char*>(&dq)) != 0) { |
| 910 | if (errno != ESRCH) { |
| 911 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; |
| 912 | } |
| 913 | } else { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 914 | #if MEASURE_DEBUG |
| 915 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; |
| 916 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 917 | stats->dataSize += dq.dqb_curspace; |
| 918 | } |
| 919 | |
| 920 | int cacheGid = multiuser_get_cache_gid(userId, appId); |
| 921 | if (cacheGid != -1) { |
| 922 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid, |
| 923 | reinterpret_cast<char*>(&dq)) != 0) { |
| 924 | if (errno != ESRCH) { |
| 925 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid; |
| 926 | } |
| 927 | } else { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 928 | #if MEASURE_DEBUG |
| 929 | LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace; |
| 930 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 931 | stats->cacheSize += dq.dqb_curspace; |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | int sharedGid = multiuser_get_shared_app_gid(uid); |
| 936 | if (sharedGid != -1) { |
| 937 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid, |
| 938 | reinterpret_cast<char*>(&dq)) != 0) { |
| 939 | if (errno != ESRCH) { |
| 940 | PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid; |
| 941 | } |
| 942 | } else { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 943 | #if MEASURE_DEBUG |
| 944 | LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace; |
| 945 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 946 | stats->codeSize += dq.dqb_curspace; |
| 947 | } |
| 948 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 949 | |
| 950 | #if MEASURE_EXTERNAL |
| 951 | // TODO: measure using external GIDs |
| 952 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 955 | static void collectQuotaStats(const std::unique_ptr<std::string>& uuid, int32_t userId, |
| 956 | int32_t appId, struct stats* stats, struct stats* extStats) { |
| 957 | collectQuotaStats(findDeviceForUuid(uuid), userId, appId, stats, extStats); |
| 958 | } |
| 959 | |
| 960 | static void collectManualStats(const std::string& path, struct stats* stats) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 961 | DIR *d; |
| 962 | int dfd; |
| 963 | struct dirent *de; |
| 964 | struct stat s; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 965 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 966 | d = opendir(path.c_str()); |
| 967 | if (d == nullptr) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 968 | if (errno != ENOENT) { |
| 969 | PLOG(WARNING) << "Failed to open " << path; |
| 970 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | dfd = dirfd(d); |
| 974 | while ((de = readdir(d))) { |
| 975 | const char *name = de->d_name; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 976 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 977 | int64_t size = 0; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 978 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 979 | size = s.st_blocks * 512; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | if (de->d_type == DT_DIR) { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 983 | if (!strcmp(name, ".")) { |
| 984 | // Don't recurse, but still count node size |
| 985 | } else if (!strcmp(name, "..")) { |
| 986 | // Don't recurse or count node size |
| 987 | continue; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 988 | } else { |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 989 | // Measure all children nodes |
| 990 | size = 0; |
| 991 | calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 992 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 993 | |
| 994 | if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { |
| 995 | stats->cacheSize += size; |
| 996 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 997 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 998 | |
| 999 | // Legacy symlink isn't owned by app |
| 1000 | if (de->d_type == DT_LNK && !strcmp(name, "lib")) { |
| 1001 | continue; |
| 1002 | } |
| 1003 | |
| 1004 | // Everything found inside is considered data |
| 1005 | stats->dataSize += size; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1006 | } |
| 1007 | closedir(d); |
| 1008 | } |
| 1009 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1010 | static void collectManualStatsForUser(const std::string& path, struct stats* stats, |
| 1011 | bool exclude_apps = false) { |
| 1012 | DIR *d; |
| 1013 | int dfd; |
| 1014 | struct dirent *de; |
| 1015 | struct stat s; |
| 1016 | |
| 1017 | d = opendir(path.c_str()); |
| 1018 | if (d == nullptr) { |
| 1019 | if (errno != ENOENT) { |
| 1020 | PLOG(WARNING) << "Failed to open " << path; |
| 1021 | } |
| 1022 | return; |
| 1023 | } |
| 1024 | dfd = dirfd(d); |
| 1025 | while ((de = readdir(d))) { |
| 1026 | if (de->d_type == DT_DIR) { |
| 1027 | const char *name = de->d_name; |
| 1028 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) { |
| 1029 | continue; |
| 1030 | } |
| 1031 | if (!strcmp(name, ".") || !strcmp(name, "..")) { |
| 1032 | continue; |
| 1033 | } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) { |
| 1034 | continue; |
| 1035 | } else { |
| 1036 | collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats); |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | closedir(d); |
| 1041 | } |
| 1042 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1043 | binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1044 | const std::vector<std::string>& packageNames, int32_t userId, int32_t flags, |
| 1045 | int32_t appId, const std::vector<int64_t>& ceDataInodes, |
| 1046 | const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1047 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1048 | CHECK_ARGUMENT_UUID(uuid); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1049 | for (auto packageName : packageNames) { |
| 1050 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1051 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1052 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1053 | // When modifying this logic, always verify using tests: |
| 1054 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize |
| 1055 | |
| 1056 | #if MEASURE_DEBUG |
| 1057 | LOG(INFO) << "Measuring user " << userId << " app " << appId; |
| 1058 | #endif |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1059 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1060 | // Here's a summary of the common storage locations across the platform, |
| 1061 | // and how they're each tagged: |
| 1062 | // |
| 1063 | // /data/app/com.example UID system |
| 1064 | // /data/app/com.example/oat UID system |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1065 | // /data/user/0/com.example UID u0_a10 GID u0_a10 |
| 1066 | // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache |
| 1067 | // /data/media/0/foo.txt UID u0_media_rw |
| 1068 | // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image |
| 1069 | // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext |
| 1070 | // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache |
| 1071 | // /data/media/obb/com.example UID system |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1072 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1073 | struct stats stats; |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1074 | struct stats extStats; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1075 | memset(&stats, 0, sizeof(stats)); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1076 | memset(&extStats, 0, sizeof(extStats)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1077 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1078 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1079 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1080 | for (auto packageName : packageNames) { |
| 1081 | auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str()); |
| 1082 | calculate_tree_size(obbCodePath, &extStats.codeSize); |
| 1083 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1084 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1085 | if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) { |
| 1086 | for (auto codePath : codePaths) { |
| 1087 | calculate_tree_size(codePath, &stats.codeSize, -1, |
| 1088 | multiuser_get_shared_gid(userId, appId)); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1089 | } |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1090 | |
| 1091 | collectQuotaStats(uuid, userId, appId, &stats, &extStats); |
| 1092 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1093 | } else { |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1094 | for (auto codePath : codePaths) { |
| 1095 | calculate_tree_size(codePath, &stats.codeSize); |
| 1096 | } |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1097 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1098 | for (size_t i = 0; i < packageNames.size(); i++) { |
| 1099 | const char* pkgname = packageNames[i].c_str(); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1100 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1101 | auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]); |
| 1102 | collectManualStats(cePath, &stats); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1103 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1104 | auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 1105 | collectManualStats(dePath, &stats); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1106 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1107 | auto userProfilePath = create_data_user_profile_package_path(userId, pkgname); |
| 1108 | calculate_tree_size(userProfilePath, &stats.dataSize); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1109 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1110 | auto refProfilePath = create_data_ref_profile_package_path(pkgname); |
| 1111 | calculate_tree_size(refProfilePath, &stats.codeSize); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1112 | |
| 1113 | #if MEASURE_EXTERNAL |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1114 | auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data"); |
| 1115 | collectManualStats(extPath, &extStats); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1116 | |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1117 | auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media"); |
| 1118 | calculate_tree_size(mediaPath, &extStats.dataSize); |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1119 | #endif |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | int32_t sharedGid = multiuser_get_shared_gid(userId, appId); |
| 1123 | if (sharedGid != -1) { |
| 1124 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 1125 | sharedGid, -1); |
| 1126 | } |
| 1127 | |
| 1128 | calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, |
| 1129 | multiuser_get_uid(userId, appId), -1); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1130 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 1131 | |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1132 | std::vector<int64_t> ret; |
| 1133 | ret.push_back(stats.codeSize); |
| 1134 | ret.push_back(stats.dataSize); |
| 1135 | ret.push_back(stats.cacheSize); |
Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 1136 | ret.push_back(extStats.codeSize); |
| 1137 | ret.push_back(extStats.dataSize); |
| 1138 | ret.push_back(extStats.cacheSize); |
| 1139 | #if MEASURE_DEBUG |
| 1140 | LOG(DEBUG) << "Final result " << toString(ret); |
| 1141 | #endif |
| 1142 | *_aidl_return = ret; |
| 1143 | return ok(); |
| 1144 | } |
| 1145 | |
| 1146 | binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid, |
| 1147 | int32_t userId, int32_t flags, const std::vector<int32_t>& appIds, |
| 1148 | std::vector<int64_t>* _aidl_return) { |
| 1149 | ENFORCE_UID(AID_SYSTEM); |
| 1150 | CHECK_ARGUMENT_UUID(uuid); |
| 1151 | |
| 1152 | // When modifying this logic, always verify using tests: |
| 1153 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize |
| 1154 | |
| 1155 | #if MEASURE_DEBUG |
| 1156 | LOG(INFO) << "Measuring user " << userId; |
| 1157 | #endif |
| 1158 | |
| 1159 | struct stats stats; |
| 1160 | struct stats extStats; |
| 1161 | memset(&stats, 0, sizeof(stats)); |
| 1162 | memset(&extStats, 0, sizeof(extStats)); |
| 1163 | |
| 1164 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1165 | |
| 1166 | auto obbPath = create_data_path(uuid_) + "/media/obb"; |
| 1167 | calculate_tree_size(obbPath, &extStats.codeSize); |
| 1168 | |
| 1169 | if (flags & FLAG_USE_QUOTA) { |
| 1170 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true); |
| 1171 | |
| 1172 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 1173 | collectManualStatsForUser(cePath, &stats, true); |
| 1174 | |
| 1175 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 1176 | collectManualStatsForUser(dePath, &stats, true); |
| 1177 | |
| 1178 | auto userProfilePath = create_data_user_profile_path(userId); |
| 1179 | calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true); |
| 1180 | |
| 1181 | auto refProfilePath = create_data_ref_profile_path(); |
| 1182 | calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true); |
| 1183 | |
| 1184 | #if MEASURE_EXTERNAL |
| 1185 | // TODO: measure external storage paths |
| 1186 | #endif |
| 1187 | |
| 1188 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize, |
| 1189 | -1, -1, true); |
| 1190 | |
| 1191 | calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize, |
| 1192 | -1, -1, true); |
| 1193 | |
| 1194 | auto device = findDeviceForUuid(uuid); |
| 1195 | for (auto appId : appIds) { |
| 1196 | if (appId >= AID_APP_START) { |
| 1197 | collectQuotaStats(device, userId, appId, &stats, &extStats); |
| 1198 | #if MEASURE_DEBUG |
| 1199 | // Sleep to make sure we don't lose logs |
| 1200 | usleep(1); |
| 1201 | #endif |
| 1202 | } |
| 1203 | } |
| 1204 | } else { |
| 1205 | calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize); |
| 1206 | |
| 1207 | auto cePath = create_data_user_ce_path(uuid_, userId); |
| 1208 | collectManualStatsForUser(cePath, &stats); |
| 1209 | |
| 1210 | auto dePath = create_data_user_de_path(uuid_, userId); |
| 1211 | collectManualStatsForUser(dePath, &stats); |
| 1212 | |
| 1213 | auto userProfilePath = create_data_user_profile_path(userId); |
| 1214 | calculate_tree_size(userProfilePath, &stats.dataSize); |
| 1215 | |
| 1216 | auto refProfilePath = create_data_ref_profile_path(); |
| 1217 | calculate_tree_size(refProfilePath, &stats.codeSize); |
| 1218 | |
| 1219 | #if MEASURE_EXTERNAL |
| 1220 | // TODO: measure external storage paths |
| 1221 | #endif |
| 1222 | |
| 1223 | calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize); |
| 1224 | |
| 1225 | calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize); |
| 1226 | } |
| 1227 | |
| 1228 | std::vector<int64_t> ret; |
| 1229 | ret.push_back(stats.codeSize); |
| 1230 | ret.push_back(stats.dataSize); |
| 1231 | ret.push_back(stats.cacheSize); |
| 1232 | ret.push_back(extStats.codeSize); |
| 1233 | ret.push_back(extStats.dataSize); |
| 1234 | ret.push_back(extStats.cacheSize); |
| 1235 | #if MEASURE_DEBUG |
| 1236 | LOG(DEBUG) << "Final result " << toString(ret); |
| 1237 | #endif |
| 1238 | *_aidl_return = ret; |
| 1239 | return ok(); |
| 1240 | } |
| 1241 | |
| 1242 | binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid, |
| 1243 | int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) { |
| 1244 | ENFORCE_UID(AID_SYSTEM); |
| 1245 | CHECK_ARGUMENT_UUID(uuid); |
| 1246 | |
| 1247 | // When modifying this logic, always verify using tests: |
| 1248 | // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize |
| 1249 | |
| 1250 | #if MEASURE_DEBUG |
| 1251 | LOG(INFO) << "Measuring external " << userId; |
| 1252 | #endif |
| 1253 | |
| 1254 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1255 | |
| 1256 | int64_t totalSize = 0; |
| 1257 | int64_t audioSize = 0; |
| 1258 | int64_t videoSize = 0; |
| 1259 | int64_t imageSize = 0; |
| 1260 | |
| 1261 | if (flags & FLAG_USE_QUOTA) { |
| 1262 | struct dqblk dq; |
| 1263 | |
| 1264 | auto device = findDeviceForUuid(uuid); |
| 1265 | |
| 1266 | uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW); |
| 1267 | if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid, |
| 1268 | reinterpret_cast<char*>(&dq)) != 0) { |
| 1269 | if (errno != ESRCH) { |
| 1270 | PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid; |
| 1271 | } |
| 1272 | } else { |
| 1273 | #if MEASURE_DEBUG |
| 1274 | LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace; |
| 1275 | #endif |
| 1276 | totalSize = dq.dqb_curspace; |
| 1277 | } |
| 1278 | |
| 1279 | gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO); |
| 1280 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid, |
| 1281 | reinterpret_cast<char*>(&dq)) == 0) { |
| 1282 | #if MEASURE_DEBUG |
| 1283 | LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace; |
| 1284 | #endif |
| 1285 | audioSize = dq.dqb_curspace; |
| 1286 | } |
| 1287 | gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO); |
| 1288 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid, |
| 1289 | reinterpret_cast<char*>(&dq)) == 0) { |
| 1290 | #if MEASURE_DEBUG |
| 1291 | LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace; |
| 1292 | #endif |
| 1293 | videoSize = dq.dqb_curspace; |
| 1294 | } |
| 1295 | gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE); |
| 1296 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid, |
| 1297 | reinterpret_cast<char*>(&dq)) == 0) { |
| 1298 | #if MEASURE_DEBUG |
| 1299 | LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace; |
| 1300 | #endif |
| 1301 | imageSize = dq.dqb_curspace; |
| 1302 | } |
| 1303 | } else { |
| 1304 | FTS *fts; |
| 1305 | FTSENT *p; |
| 1306 | auto path = create_data_media_path(uuid_, userId); |
| 1307 | char *argv[] = { (char*) path.c_str(), nullptr }; |
| 1308 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { |
| 1309 | return error("Failed to fts_open " + path); |
| 1310 | } |
| 1311 | while ((p = fts_read(fts)) != NULL) { |
| 1312 | char* ext; |
| 1313 | int64_t size = (p->fts_statp->st_blocks * 512); |
| 1314 | switch (p->fts_info) { |
| 1315 | case FTS_F: |
| 1316 | // Only categorize files not belonging to apps |
| 1317 | if (p->fts_statp->st_gid < AID_APP_START) { |
| 1318 | ext = strrchr(p->fts_name, '.'); |
| 1319 | if (ext != nullptr) { |
| 1320 | switch (MatchExtension(++ext)) { |
| 1321 | case AID_MEDIA_AUDIO: audioSize += size; break; |
| 1322 | case AID_MEDIA_VIDEO: videoSize += size; break; |
| 1323 | case AID_MEDIA_IMAGE: imageSize += size; break; |
| 1324 | } |
| 1325 | } |
| 1326 | } |
| 1327 | // Fall through to always count against total |
| 1328 | case FTS_D: |
| 1329 | case FTS_DEFAULT: |
| 1330 | case FTS_SL: |
| 1331 | case FTS_SLNONE: |
| 1332 | totalSize += size; |
| 1333 | break; |
| 1334 | } |
| 1335 | } |
| 1336 | fts_close(fts); |
| 1337 | } |
| 1338 | |
| 1339 | std::vector<int64_t> ret; |
| 1340 | ret.push_back(totalSize); |
| 1341 | ret.push_back(audioSize); |
| 1342 | ret.push_back(videoSize); |
| 1343 | ret.push_back(imageSize); |
| 1344 | #if MEASURE_DEBUG |
| 1345 | LOG(DEBUG) << "Final result " << toString(ret); |
| 1346 | #endif |
Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 1347 | *_aidl_return = ret; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1348 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 1351 | // Dumps the contents of a profile file, using pkgname's dex files for pretty |
| 1352 | // printing the result. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1353 | binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, |
| 1354 | const std::string& codePaths, bool* _aidl_return) { |
| 1355 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1356 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1357 | |
| 1358 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1359 | const char* code_paths = codePaths.c_str(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1360 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1361 | *_aidl_return = dump_profiles(uid, pkgname, code_paths); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1362 | return ok(); |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 1365 | // TODO: Consider returning error codes. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1366 | binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1367 | bool* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1368 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1369 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1370 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1371 | const char* pkgname = packageName.c_str(); |
| 1372 | *_aidl_return = analyse_profiles(uid, pkgname); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1373 | return ok(); |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1376 | binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, |
| 1377 | const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, |
| 1378 | int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, |
| 1379 | const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, |
| 1380 | const std::unique_ptr<std::string>& sharedLibraries) { |
| 1381 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1382 | CHECK_ARGUMENT_UUID(uuid); |
| 1383 | if (packageName && *packageName != "*") { |
| 1384 | CHECK_ARGUMENT_PACKAGE_NAME(*packageName); |
| 1385 | } |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1386 | |
| 1387 | const char* apk_path = apkPath.c_str(); |
| 1388 | const char* pkgname = packageName ? packageName->c_str() : "*"; |
| 1389 | const char* instruction_set = instructionSet.c_str(); |
| 1390 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; |
| 1391 | const char* compiler_filter = compilerFilter.c_str(); |
| 1392 | const char* volume_uuid = uuid ? uuid->c_str() : nullptr; |
| 1393 | const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; |
| 1394 | |
| 1395 | int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, |
| 1396 | oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1397 | return res ? error(res, "Failed to dexopt") : ok(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1400 | binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { |
| 1401 | ENFORCE_UID(AID_SYSTEM); |
| 1402 | const char* instruction_set = instructionSet.c_str(); |
| 1403 | |
| 1404 | char boot_marker_path[PKG_PATH_MAX]; |
| 1405 | sprintf(boot_marker_path, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1406 | "%s/%s/%s/.booting", |
| 1407 | android_data_dir.path, |
| 1408 | DALVIK_CACHE, |
| 1409 | instruction_set); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1410 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1411 | ALOGV("mark_boot_complete : %s", boot_marker_path); |
| 1412 | if (unlink(boot_marker_path) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1413 | return error(StringPrintf("Failed to unlink %s", boot_marker_path)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1414 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1415 | return ok(); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1418 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 1419 | struct stat* statbuf) |
| 1420 | { |
| 1421 | while (path[basepos] != 0) { |
| 1422 | if (path[basepos] == '/') { |
| 1423 | path[basepos] = 0; |
| 1424 | if (lstat(path, statbuf) < 0) { |
| 1425 | ALOGV("Making directory: %s\n", path); |
| 1426 | if (mkdir(path, mode) == 0) { |
| 1427 | chown(path, uid, gid); |
| 1428 | } else { |
| 1429 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
| 1430 | } |
| 1431 | } |
| 1432 | path[basepos] = '/'; |
| 1433 | basepos++; |
| 1434 | } |
| 1435 | basepos++; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1439 | binder::Status InstalldNativeService::linkNativeLibraryDirectory( |
| 1440 | const std::unique_ptr<std::string>& uuid, const std::string& packageName, |
| 1441 | const std::string& nativeLibPath32, int32_t userId) { |
| 1442 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1443 | CHECK_ARGUMENT_UUID(uuid); |
| 1444 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1445 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1446 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1447 | const char* pkgname = packageName.c_str(); |
| 1448 | const char* asecLibDir = nativeLibPath32.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1449 | struct stat s, libStat; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1450 | binder::Status res = ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1451 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1452 | auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 1453 | auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 1454 | |
| 1455 | const char* pkgdir = _pkgdir.c_str(); |
| 1456 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1457 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1458 | if (stat(pkgdir, &s) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1459 | return error("Failed to stat " + _pkgdir); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1460 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1461 | |
| 1462 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1463 | return error("Failed to chown " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | if (chmod(pkgdir, 0700) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1467 | res = error("Failed to chmod " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1468 | goto out; |
| 1469 | } |
| 1470 | |
| 1471 | if (lstat(libsymlink, &libStat) < 0) { |
| 1472 | if (errno != ENOENT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1473 | res = error("Failed to stat " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1474 | goto out; |
| 1475 | } |
| 1476 | } else { |
| 1477 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1478 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1479 | res = error("Failed to delete " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1480 | goto out; |
| 1481 | } |
| 1482 | } else if (S_ISLNK(libStat.st_mode)) { |
| 1483 | if (unlink(libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1484 | res = error("Failed to unlink " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1485 | goto out; |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | |
| 1490 | if (symlink(asecLibDir, libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1491 | res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1492 | goto out; |
| 1493 | } |
| 1494 | |
| 1495 | out: |
| 1496 | if (chmod(pkgdir, s.st_mode) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1497 | auto msg = "Failed to cleanup chmod " + _pkgdir; |
| 1498 | if (res.isOk()) { |
| 1499 | res = error(msg); |
| 1500 | } else { |
| 1501 | PLOG(ERROR) << msg; |
| 1502 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1506 | auto msg = "Failed to cleanup chown " + _pkgdir; |
| 1507 | if (res.isOk()) { |
| 1508 | res = error(msg); |
| 1509 | } else { |
| 1510 | PLOG(ERROR) << msg; |
| 1511 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1514 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1515 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1516 | |
| 1517 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) |
| 1518 | { |
| 1519 | static const char *IDMAP_BIN = "/system/bin/idmap"; |
| 1520 | static const size_t MAX_INT_LEN = 32; |
| 1521 | char idmap_str[MAX_INT_LEN]; |
| 1522 | |
| 1523 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); |
| 1524 | |
| 1525 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); |
| 1526 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); |
| 1527 | } |
| 1528 | |
| 1529 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) |
| 1530 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap |
| 1531 | static int flatten_path(const char *prefix, const char *suffix, |
| 1532 | const char *overlay_path, char *idmap_path, size_t N) |
| 1533 | { |
| 1534 | if (overlay_path == NULL || idmap_path == NULL) { |
| 1535 | return -1; |
| 1536 | } |
| 1537 | const size_t len_overlay_path = strlen(overlay_path); |
| 1538 | // will access overlay_path + 1 further below; requires absolute path |
| 1539 | if (len_overlay_path < 2 || *overlay_path != '/') { |
| 1540 | return -1; |
| 1541 | } |
| 1542 | const size_t len_idmap_root = strlen(prefix); |
| 1543 | const size_t len_suffix = strlen(suffix); |
| 1544 | if (SIZE_MAX - len_idmap_root < len_overlay_path || |
| 1545 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { |
| 1546 | // additions below would cause overflow |
| 1547 | return -1; |
| 1548 | } |
| 1549 | if (N < len_idmap_root + len_overlay_path + len_suffix) { |
| 1550 | return -1; |
| 1551 | } |
| 1552 | memset(idmap_path, 0, N); |
| 1553 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); |
| 1554 | char *ch = idmap_path + len_idmap_root; |
| 1555 | while (*ch != '\0') { |
| 1556 | if (*ch == '/') { |
| 1557 | *ch = '@'; |
| 1558 | } |
| 1559 | ++ch; |
| 1560 | } |
| 1561 | return 0; |
| 1562 | } |
| 1563 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1564 | binder::Status InstalldNativeService::idmap(const std::string& targetApkPath, |
| 1565 | const std::string& overlayApkPath, int32_t uid) { |
| 1566 | ENFORCE_UID(AID_SYSTEM); |
| 1567 | const char* target_apk = targetApkPath.c_str(); |
| 1568 | const char* overlay_apk = overlayApkPath.c_str(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1569 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); |
| 1570 | |
| 1571 | int idmap_fd = -1; |
| 1572 | char idmap_path[PATH_MAX]; |
| 1573 | |
| 1574 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, |
| 1575 | idmap_path, sizeof(idmap_path)) == -1) { |
| 1576 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); |
| 1577 | goto fail; |
| 1578 | } |
| 1579 | |
| 1580 | unlink(idmap_path); |
| 1581 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1582 | if (idmap_fd < 0) { |
| 1583 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); |
| 1584 | goto fail; |
| 1585 | } |
| 1586 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { |
| 1587 | ALOGE("idmap cannot chown '%s'\n", idmap_path); |
| 1588 | goto fail; |
| 1589 | } |
| 1590 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { |
| 1591 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); |
| 1592 | goto fail; |
| 1593 | } |
| 1594 | |
| 1595 | pid_t pid; |
| 1596 | pid = fork(); |
| 1597 | if (pid == 0) { |
| 1598 | /* child -- drop privileges before continuing */ |
| 1599 | if (setgid(uid) != 0) { |
| 1600 | ALOGE("setgid(%d) failed during idmap\n", uid); |
| 1601 | exit(1); |
| 1602 | } |
| 1603 | if (setuid(uid) != 0) { |
| 1604 | ALOGE("setuid(%d) failed during idmap\n", uid); |
| 1605 | exit(1); |
| 1606 | } |
| 1607 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1608 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); |
| 1609 | exit(1); |
| 1610 | } |
| 1611 | |
| 1612 | run_idmap(target_apk, overlay_apk, idmap_fd); |
| 1613 | exit(1); /* only if exec call to idmap failed */ |
| 1614 | } else { |
| 1615 | int status = wait_child(pid); |
| 1616 | if (status != 0) { |
| 1617 | ALOGE("idmap failed, status=0x%04x\n", status); |
| 1618 | goto fail; |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | close(idmap_fd); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1623 | return ok(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1624 | fail: |
| 1625 | if (idmap_fd >= 0) { |
| 1626 | close(idmap_fd); |
| 1627 | unlink(idmap_path); |
| 1628 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1629 | return error(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1630 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1631 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1632 | binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, |
| 1633 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, |
| 1634 | const std::string& seInfo) { |
| 1635 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1636 | CHECK_ARGUMENT_UUID(uuid); |
| 1637 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1638 | |
| 1639 | binder::Status res = ok(); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1640 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1641 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1642 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1643 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1644 | const char* pkgName = packageName.c_str(); |
| 1645 | const char* seinfo = seInfo.c_str(); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1646 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1647 | uid_t uid = multiuser_get_uid(userId, appId); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1648 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1649 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1650 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1651 | res = error("restorecon failed for " + path); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 1652 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1653 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1654 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1655 | auto path = create_data_user_de_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1656 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1657 | res = error("restorecon failed for " + path); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1658 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1659 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1660 | return res; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1661 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1662 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1663 | binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, |
| 1664 | const std::string& instructionSet) { |
| 1665 | ENFORCE_UID(AID_SYSTEM); |
| 1666 | const char* oat_dir = oatDir.c_str(); |
| 1667 | const char* instruction_set = instructionSet.c_str(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1668 | char oat_instr_dir[PKG_PATH_MAX]; |
| 1669 | |
| 1670 | if (validate_apk_path(oat_dir)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1671 | return error("Invalid path " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1672 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 1673 | 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] | 1674 | return error("Failed to prepare " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1675 | } |
| 1676 | if (selinux_android_restorecon(oat_dir, 0)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1677 | return error("Failed to restorecon " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1678 | } |
| 1679 | 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] | 1680 | 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] | 1681 | return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1682 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1683 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1686 | binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { |
| 1687 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1688 | if (validate_apk_path(packageDir.c_str())) { |
| 1689 | return error("Invalid path " + packageDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1690 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1691 | if (delete_dir_contents_and_dir(packageDir) != 0) { |
| 1692 | return error("Failed to delete " + packageDir); |
| 1693 | } |
| 1694 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1695 | } |
| 1696 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1697 | binder::Status InstalldNativeService::linkFile(const std::string& relativePath, |
| 1698 | const std::string& fromBase, const std::string& toBase) { |
| 1699 | ENFORCE_UID(AID_SYSTEM); |
| 1700 | const char* relative_path = relativePath.c_str(); |
| 1701 | const char* from_base = fromBase.c_str(); |
| 1702 | const char* to_base = toBase.c_str(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1703 | char from_path[PKG_PATH_MAX]; |
| 1704 | char to_path[PKG_PATH_MAX]; |
| 1705 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); |
| 1706 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); |
| 1707 | |
| 1708 | if (validate_apk_path_subdirs(from_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1709 | return error(StringPrintf("Invalid from path %s", from_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | if (validate_apk_path_subdirs(to_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1713 | return error(StringPrintf("Invalid to path %s", to_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1716 | if (link(from_path, to_path) < 0) { |
| 1717 | 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] | 1718 | } |
| 1719 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1720 | return ok(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1723 | binder::Status InstalldNativeService::moveAb(const std::string& apkPath, |
| 1724 | const std::string& instructionSet, const std::string& outputPath) { |
| 1725 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1726 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1727 | const char* apk_path = apkPath.c_str(); |
| 1728 | const char* instruction_set = instructionSet.c_str(); |
| 1729 | const char* oat_dir = outputPath.c_str(); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 1730 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1731 | bool success = move_ab(apk_path, instruction_set, oat_dir); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1732 | return success ? ok() : error(); |
Andreas Gampe | e65b4fa | 2016-03-01 11:46:45 -0800 | [diff] [blame] | 1733 | } |
| 1734 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1735 | binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, |
| 1736 | const std::string& instructionSet, const std::string& outputPath) { |
| 1737 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1738 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1739 | const char* apk_path = apkPath.c_str(); |
| 1740 | const char* instruction_set = instructionSet.c_str(); |
| 1741 | const char* oat_dir = outputPath.c_str(); |
| 1742 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1743 | bool res = delete_odex(apk_path, instruction_set, oat_dir); |
| 1744 | return res ? ok() : error(); |
Andreas Gampe | b31206b | 2016-09-09 17:07:04 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1747 | } // namespace installd |
| 1748 | } // namespace android |