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> |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 21 | #include <regex> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <sys/capability.h> |
| 24 | #include <sys/file.h> |
| 25 | #include <sys/resource.h> |
| 26 | #include <sys/stat.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 27 | #include <sys/types.h> |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 28 | #include <sys/wait.h> |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 29 | #include <sys/xattr.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 30 | #include <unistd.h> |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 6fb5a01 | 2016-06-03 16:09:32 -0700 | [diff] [blame] | 32 | #include <android-base/logging.h> |
Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 34 | #include <android-base/strings.h> |
Roland Levillain | 64b59cc | 2016-04-05 16:41:12 +0100 | [diff] [blame] | 35 | #include <android-base/unique_fd.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 36 | #include <cutils/fs.h> |
| 37 | #include <cutils/log.h> // TODO: Move everything to base/logging. |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 38 | #include <cutils/properties.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 39 | #include <cutils/sched_policy.h> |
| 40 | #include <diskusage/dirsize.h> |
| 41 | #include <logwrap/logwrap.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 42 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 43 | #include <selinux/android.h> |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 44 | #include <system/thread_defs.h> |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 45 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 46 | #include "dexopt.h" |
Jeff Sharkey | f3e30b9 | 2016-12-09 17:06:57 -0700 | [diff] [blame] | 47 | #include "globals.h" |
| 48 | #include "installd_deps.h" |
| 49 | #include "otapreopt_utils.h" |
| 50 | #include "utils.h" |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 51 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 52 | #ifndef LOG_TAG |
| 53 | #define LOG_TAG "installd" |
| 54 | #endif |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 55 | |
| 56 | using android::base::StringPrintf; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 57 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 58 | namespace android { |
| 59 | namespace installd { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 60 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 61 | static constexpr const char* kCpPath = "/system/bin/cp"; |
| 62 | static constexpr const char* kXattrDefault = "user.default"; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 63 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 64 | static constexpr const char* PKG_LIB_POSTFIX = "/lib"; |
| 65 | static constexpr const char* CACHE_DIR_POSTFIX = "/cache"; |
| 66 | static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; |
| 67 | |
| 68 | static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; |
| 69 | static constexpr const char* IDMAP_SUFFIX = "@idmap"; |
| 70 | |
| 71 | // NOTE: keep in sync with StorageManager |
| 72 | static constexpr int FLAG_STORAGE_DE = 1 << 0; |
| 73 | static constexpr int FLAG_STORAGE_CE = 1 << 1; |
| 74 | |
| 75 | // NOTE: keep in sync with Installer |
| 76 | static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; |
| 77 | static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; |
| 78 | |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 79 | |
Janis Danisevskis | eecb2d2 | 2016-01-12 14:45:55 +0000 | [diff] [blame] | 80 | #define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 81 | namespace { |
| 82 | |
| 83 | constexpr const char* kDump = "android.permission.DUMP"; |
| 84 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 85 | static binder::Status ok() { |
| 86 | return binder::Status::ok(); |
| 87 | } |
| 88 | |
| 89 | static binder::Status exception(uint32_t code, const std::string& msg) { |
| 90 | return binder::Status::fromExceptionCode(code, String8(msg.c_str())); |
| 91 | } |
| 92 | |
| 93 | static binder::Status error() { |
| 94 | return binder::Status::fromServiceSpecificError(errno); |
| 95 | } |
| 96 | |
| 97 | static binder::Status error(const std::string& msg) { |
| 98 | PLOG(ERROR) << msg; |
| 99 | return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); |
| 100 | } |
| 101 | |
| 102 | static binder::Status error(uint32_t code, const std::string& msg) { |
| 103 | LOG(ERROR) << msg << " (" << code << ")"; |
| 104 | return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); |
| 105 | } |
| 106 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 107 | binder::Status checkPermission(const char* permission) { |
| 108 | pid_t pid; |
| 109 | uid_t uid; |
| 110 | |
| 111 | if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), |
| 112 | reinterpret_cast<int32_t*>(&uid))) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 113 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 114 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 115 | return exception(binder::Status::EX_SECURITY, |
| 116 | StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | binder::Status checkUid(uid_t expectedUid) { |
| 121 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 122 | if (uid == expectedUid || uid == AID_ROOT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 123 | return ok(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 124 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 125 | return exception(binder::Status::EX_SECURITY, |
| 126 | StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { |
| 131 | if (!uuid || is_valid_filename(*uuid)) { |
| 132 | return ok(); |
| 133 | } else { |
| 134 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 135 | StringPrintf("UUID %s is malformed", uuid->c_str())); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | binder::Status checkArgumentPackageName(const std::string& packageName) { |
| 140 | if (is_valid_package_name(packageName.c_str())) { |
| 141 | return ok(); |
| 142 | } else { |
| 143 | return exception(binder::Status::EX_ILLEGAL_ARGUMENT, |
| 144 | StringPrintf("Package name %s is malformed", packageName.c_str())); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | #define ENFORCE_UID(uid) { \ |
| 149 | binder::Status status = checkUid((uid)); \ |
| 150 | if (!status.isOk()) { \ |
| 151 | return status; \ |
| 152 | } \ |
| 153 | } |
| 154 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 155 | #define CHECK_ARGUMENT_UUID(uuid) { \ |
| 156 | binder::Status status = checkArgumentUuid((uuid)); \ |
| 157 | if (!status.isOk()) { \ |
| 158 | return status; \ |
| 159 | } \ |
| 160 | } |
| 161 | |
| 162 | #define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ |
| 163 | binder::Status status = \ |
| 164 | checkArgumentPackageName((packageName)); \ |
| 165 | if (!status.isOk()) { \ |
| 166 | return status; \ |
| 167 | } \ |
| 168 | } |
| 169 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 170 | } // namespace |
| 171 | |
| 172 | status_t InstalldNativeService::start() { |
| 173 | IPCThreadState::self()->disableBackgroundScheduling(true); |
| 174 | status_t ret = BinderService<InstalldNativeService>::publish(); |
| 175 | if (ret != android::OK) { |
| 176 | return ret; |
| 177 | } |
| 178 | sp<ProcessState> ps(ProcessState::self()); |
| 179 | ps->startThreadPool(); |
| 180 | ps->giveThreadPoolName(); |
| 181 | return android::OK; |
| 182 | } |
| 183 | |
| 184 | status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { |
| 185 | const binder::Status dump_permission = checkPermission(kDump); |
| 186 | if (!dump_permission.isOk()) { |
| 187 | const String8 msg(dump_permission.toString8()); |
| 188 | write(fd, msg.string(), msg.size()); |
| 189 | return PERMISSION_DENIED; |
| 190 | } |
| 191 | |
| 192 | std::string msg = "installd is happy\n"; |
| 193 | write(fd, msg.c_str(), strlen(msg.c_str())); |
| 194 | return NO_ERROR; |
| 195 | } |
| 196 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 197 | /** |
| 198 | * Perform restorecon of the given path, but only perform recursive restorecon |
| 199 | * if the label of that top-level file actually changed. This can save us |
| 200 | * significant time by avoiding no-op traversals of large filesystem trees. |
| 201 | */ |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 202 | static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 203 | int res = 0; |
| 204 | char* before = nullptr; |
| 205 | char* after = nullptr; |
| 206 | |
| 207 | // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by |
| 208 | // libselinux. Not needed here. |
| 209 | |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 210 | if (lgetfilecon(path.c_str(), &before) < 0) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 211 | PLOG(ERROR) << "Failed before getfilecon for " << path; |
| 212 | goto fail; |
| 213 | } |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 214 | 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] | 215 | PLOG(ERROR) << "Failed top-level restorecon for " << path; |
| 216 | goto fail; |
| 217 | } |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 218 | if (lgetfilecon(path.c_str(), &after) < 0) { |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 219 | PLOG(ERROR) << "Failed after getfilecon for " << path; |
| 220 | goto fail; |
| 221 | } |
| 222 | |
| 223 | // If the initial top-level restorecon above changed the label, then go |
| 224 | // back and restorecon everything recursively |
| 225 | if (strcmp(before, after)) { |
| 226 | LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " << path |
| 227 | << "; running recursive restorecon"; |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 228 | if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 229 | SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { |
| 230 | PLOG(ERROR) << "Failed recursive restorecon for " << path; |
| 231 | goto fail; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | goto done; |
| 236 | fail: |
| 237 | res = -1; |
| 238 | done: |
| 239 | free(before); |
| 240 | free(after); |
| 241 | return res; |
| 242 | } |
| 243 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 244 | static int restorecon_app_data_lazy(const std::string& parent, const char* name, |
| 245 | const std::string& seInfo, uid_t uid) { |
| 246 | return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid); |
Jeff Sharkey | 95d9ac6 | 2016-10-31 11:22:19 -0600 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 249 | 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] | 250 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { |
| 251 | PLOG(ERROR) << "Failed to prepare " << path; |
| 252 | return -1; |
| 253 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int prepare_app_dir(const std::string& parent, const char* name, mode_t target_mode, |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 258 | uid_t uid) { |
| 259 | return prepare_app_dir(StringPrintf("%s/%s", parent.c_str(), name), target_mode, uid); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 260 | } |
| 261 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 262 | binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, |
| 263 | 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^] | 264 | const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 265 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 266 | CHECK_ARGUMENT_UUID(uuid); |
| 267 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 268 | |
| 269 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 270 | const char* pkgname = packageName.c_str(); |
| 271 | |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame^] | 272 | // Assume invalid inode unless filled in below |
| 273 | if (_aidl_return != nullptr) *_aidl_return = -1; |
| 274 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 275 | uid_t uid = multiuser_get_uid(userId, appId); |
| 276 | mode_t target_mode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 277 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 278 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 279 | if (prepare_app_dir(path, target_mode, uid) || |
| 280 | prepare_app_dir(path, "cache", 0771, uid) || |
| 281 | prepare_app_dir(path, "code_cache", 0771, uid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 282 | return error("Failed to prepare " + path); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 286 | if (restorecon_app_data_lazy(path, seInfo, uid) || |
| 287 | restorecon_app_data_lazy(path, "cache", seInfo, uid) || |
| 288 | restorecon_app_data_lazy(path, "code_cache", seInfo, uid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 289 | return error("Failed to restorecon " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 290 | } |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 291 | |
| 292 | // Remember inode numbers of cache directories so that we can clear |
| 293 | // contents while CE storage is locked |
| 294 | if (write_path_inode(path, "cache", kXattrInodeCache) || |
| 295 | write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 296 | return error("Failed to write_path_inode for " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 297 | } |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame^] | 298 | |
| 299 | // And return the CE inode of the top-level data directory so we can |
| 300 | // clear contents while CE storage is locked |
| 301 | if ((_aidl_return != nullptr) |
| 302 | && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) { |
| 303 | return error("Failed to get_path_inode for " + path); |
| 304 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 305 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 306 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 307 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 308 | if (prepare_app_dir(path, target_mode, uid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 309 | return error("Failed to prepare " + path); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 310 | } |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 311 | |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 312 | // Consider restorecon over contents if label changed |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 313 | if (restorecon_app_data_lazy(path, seInfo, uid)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 314 | return error("Failed to restorecon " + path); |
Jeff Sharkey | 7db6041 | 2016-09-20 18:21:42 -0600 | [diff] [blame] | 315 | } |
| 316 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 317 | if (property_get_bool("dalvik.vm.usejitprofiles", false)) { |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 318 | 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] | 319 | // read-write-execute only for the app user. |
| 320 | 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] | 321 | return error("Failed to prepare " + profile_path); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 322 | } |
Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 323 | std::string profile_file = create_primary_profile(profile_path); |
| 324 | // read-write only for the app user. |
| 325 | 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] | 326 | return error("Failed to prepare " + profile_path); |
Calin Juravle | bc56e7d | 2016-05-24 15:33:12 +0100 | [diff] [blame] | 327 | } |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 328 | const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); |
| 329 | // dex2oat/profman runs under the shared app gid and it needs to read/write reference |
| 330 | // profiles. |
Jeff Sharkey | 2e6dc9b | 2016-12-15 14:50:11 -0700 | [diff] [blame] | 331 | int shared_app_gid = multiuser_get_shared_app_gid(uid); |
| 332 | if ((shared_app_gid != -1) && fs_prepare_dir_strict( |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 333 | 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] | 334 | return error("Failed to prepare " + ref_profile_path); |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 337 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 338 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 341 | binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, |
| 342 | const std::string& packageName, int32_t userId, int32_t flags) { |
| 343 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 344 | CHECK_ARGUMENT_UUID(uuid); |
| 345 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 346 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 347 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 348 | const char* pkgname = packageName.c_str(); |
| 349 | |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 350 | // This method only exists to upgrade system apps that have requested |
| 351 | // forceDeviceEncrypted, so their default storage always lives in a |
| 352 | // consistent location. This only works on non-FBE devices, since we |
| 353 | // never want to risk exposing data on a device with real CE/DE storage. |
| 354 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 355 | auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 356 | auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 357 | |
| 358 | // If neither directory is marked as default, assume CE is default |
| 359 | if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 |
| 360 | && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 361 | if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 362 | return error("Failed to mark default storage " + ce_path); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | // Migrate default data location if needed |
| 367 | auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; |
| 368 | auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; |
| 369 | |
| 370 | if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { |
| 371 | LOG(WARNING) << "Requested default storage " << target |
| 372 | << " is not active; migrating from " << source; |
| 373 | if (delete_dir_contents_and_dir(target) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 374 | return error("Failed to delete " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 375 | } |
| 376 | if (rename(source.c_str(), target.c_str()) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 377 | return error("Failed to rename " + source + " to " + target); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 381 | return ok(); |
Jeff Sharkey | cc6281c | 2016-02-06 19:46:09 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 384 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 385 | binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { |
| 386 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 387 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 388 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 389 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 390 | binder::Status res = ok(); |
| 391 | if (!clear_reference_profile(pkgname)) { |
| 392 | res = error("Failed to clear reference profile for " + packageName); |
| 393 | } |
| 394 | if (!clear_current_profiles(pkgname)) { |
| 395 | res = error("Failed to clear current profiles for " + packageName); |
| 396 | } |
| 397 | return res; |
Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 400 | binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, |
| 401 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 402 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 403 | CHECK_ARGUMENT_UUID(uuid); |
| 404 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 405 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 406 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 407 | const char* pkgname = packageName.c_str(); |
| 408 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 409 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 410 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 411 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 412 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 413 | path = read_path_inode(path, "cache", kXattrInodeCache); |
| 414 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 415 | path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 416 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 417 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 418 | if (delete_dir_contents(path) != 0) { |
| 419 | res = error("Failed to delete contents of " + path); |
| 420 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 423 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 424 | std::string suffix = ""; |
| 425 | bool only_cache = false; |
| 426 | if (flags & FLAG_CLEAR_CACHE_ONLY) { |
| 427 | suffix = CACHE_DIR_POSTFIX; |
| 428 | only_cache = true; |
| 429 | } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { |
| 430 | suffix = CODE_CACHE_DIR_POSTFIX; |
| 431 | only_cache = true; |
| 432 | } |
| 433 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 434 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 435 | if (access(path.c_str(), F_OK) == 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 436 | if (delete_dir_contents(path) != 0) { |
| 437 | res = error("Failed to delete contents of " + path); |
| 438 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 439 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 440 | if (!only_cache) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 441 | if (!clear_current_profile(pkgname, userId)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 442 | res = error("Failed to clear current profile for " + packageName); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 445 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 446 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 449 | static int destroy_app_reference_profile(const char *pkgname) { |
| 450 | return delete_dir_contents_and_dir( |
| 451 | create_data_ref_profile_package_path(pkgname), |
| 452 | /*ignore_if_missing*/ true); |
| 453 | } |
| 454 | |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 455 | static int destroy_app_current_profiles(const char *pkgname, userid_t userid) { |
Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 456 | return delete_dir_contents_and_dir( |
| 457 | create_data_user_profile_package_path(userid, pkgname), |
| 458 | /*ignore_if_missing*/ true); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 461 | binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { |
| 462 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 463 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 464 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 465 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 466 | binder::Status res = ok(); |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 467 | std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); |
| 468 | for (auto user : users) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 469 | if (destroy_app_current_profiles(pkgname, user) != 0) { |
| 470 | res = error("Failed to destroy current profiles for " + packageName); |
| 471 | } |
Calin Juravle | edae669 | 2016-03-23 13:55:31 +0000 | [diff] [blame] | 472 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 473 | if (destroy_app_reference_profile(pkgname) != 0) { |
| 474 | res = error("Failed to destroy reference profile for " + packageName); |
| 475 | } |
| 476 | return res; |
Calin Juravle | caa6b80 | 2016-03-22 14:15:52 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 479 | binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, |
| 480 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { |
| 481 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 482 | CHECK_ARGUMENT_UUID(uuid); |
| 483 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 484 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 485 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 486 | const char* pkgname = packageName.c_str(); |
| 487 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 488 | binder::Status res = ok(); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 489 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 490 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
| 491 | if (delete_dir_contents_and_dir(path) != 0) { |
| 492 | res = error("Failed to delete " + path); |
| 493 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 494 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 495 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 496 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 497 | if (delete_dir_contents_and_dir(path) != 0) { |
| 498 | res = error("Failed to delete " + path); |
| 499 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 500 | destroy_app_current_profiles(pkgname, userId); |
Calin Juravle | 7535e8e | 2016-03-29 16:05:40 +0100 | [diff] [blame] | 501 | // TODO(calin): If the package is still installed by other users it's probably |
| 502 | // beneficial to keep the reference profile around. |
| 503 | // Verify if it's ok to do that. |
| 504 | destroy_app_reference_profile(pkgname); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 505 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 506 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 509 | binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, |
| 510 | const std::unique_ptr<std::string>& toUuid, const std::string& packageName, |
| 511 | const std::string& dataAppName, int32_t appId, const std::string& seInfo, |
| 512 | int32_t targetSdkVersion) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 513 | ENFORCE_UID(AID_SYSTEM); |
| 514 | CHECK_ARGUMENT_UUID(fromUuid); |
| 515 | CHECK_ARGUMENT_UUID(toUuid); |
| 516 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 517 | |
| 518 | const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; |
| 519 | const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; |
| 520 | const char* package_name = packageName.c_str(); |
| 521 | const char* data_app_name = dataAppName.c_str(); |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 522 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 523 | binder::Status res = ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 524 | std::vector<userid_t> users = get_known_users(from_uuid); |
| 525 | |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 526 | // Copy app |
| 527 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 528 | auto from = create_data_app_package_path(from_uuid, data_app_name); |
| 529 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
| 530 | auto to_parent = create_data_app_path(to_uuid); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 531 | |
| 532 | char *argv[] = { |
| 533 | (char*) kCpPath, |
| 534 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 535 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 536 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 537 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 538 | (char*) "-d", /* don't dereference symlinks */ |
| 539 | (char*) from.c_str(), |
| 540 | (char*) to_parent.c_str() |
| 541 | }; |
| 542 | |
| 543 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 544 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 545 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 546 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 547 | goto fail; |
| 548 | } |
| 549 | |
| 550 | if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 551 | res = error("Failed to restorecon " + to); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 552 | goto fail; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // Copy private data for all known users |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 557 | for (auto user : users) { |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 558 | |
| 559 | // Data source may not exist for all users; that's okay |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 560 | auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 561 | if (access(from_ce.c_str(), F_OK) != 0) { |
| 562 | LOG(INFO) << "Missing source " << from_ce; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 563 | continue; |
| 564 | } |
| 565 | |
Jeff Sharkey | 0274c97 | 2016-12-06 09:32:04 -0700 | [diff] [blame] | 566 | if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, |
Jeff Sharkey | 36a900a | 2016-12-19 16:39:18 -0700 | [diff] [blame^] | 567 | seInfo, targetSdkVersion, nullptr).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 568 | res = error("Failed to create package target"); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 569 | goto fail; |
| 570 | } |
| 571 | |
| 572 | char *argv[] = { |
| 573 | (char*) kCpPath, |
| 574 | (char*) "-F", /* delete any existing destination file first (--remove-destination) */ |
| 575 | (char*) "-p", /* preserve timestamps, ownership, and permissions */ |
| 576 | (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ |
| 577 | (char*) "-P", /* Do not follow symlinks [default] */ |
| 578 | (char*) "-d", /* don't dereference symlinks */ |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 579 | nullptr, |
| 580 | nullptr |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 583 | { |
| 584 | auto from = create_data_user_de_package_path(from_uuid, user, package_name); |
| 585 | auto to = create_data_user_de_path(to_uuid, user); |
| 586 | argv[6] = (char*) from.c_str(); |
| 587 | argv[7] = (char*) to.c_str(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 588 | |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 589 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 590 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
| 591 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 592 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 593 | goto fail; |
| 594 | } |
| 595 | } |
| 596 | { |
| 597 | auto from = create_data_user_ce_package_path(from_uuid, user, package_name); |
| 598 | auto to = create_data_user_ce_path(to_uuid, user); |
| 599 | argv[6] = (char*) from.c_str(); |
| 600 | argv[7] = (char*) to.c_str(); |
| 601 | |
| 602 | LOG(DEBUG) << "Copying " << from << " to " << to; |
| 603 | int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); |
| 604 | if (rc != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 605 | res = error(rc, "Failed copying " + from + " to " + to); |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 606 | goto fail; |
| 607 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 610 | if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, |
| 611 | appId, seInfo).isOk()) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 612 | res = error("Failed to restorecon"); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 613 | goto fail; |
| 614 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Jeff Sharkey | 31f0898 | 2015-07-07 13:31:37 -0700 | [diff] [blame] | 617 | // We let the framework scan the new location and persist that before |
| 618 | // deleting the data in the old location; this ordering ensures that |
| 619 | // we can recover from things like battery pulls. |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 620 | return ok(); |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 621 | |
| 622 | fail: |
| 623 | // Nuke everything we might have already copied |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 624 | { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 625 | auto to = create_data_app_package_path(to_uuid, data_app_name); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 626 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 627 | LOG(WARNING) << "Failed to rollback " << to; |
| 628 | } |
| 629 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 630 | for (auto user : users) { |
Jeff Sharkey | 51c9449 | 2016-05-10 17:46:39 -0600 | [diff] [blame] | 631 | { |
| 632 | auto to = create_data_user_de_package_path(to_uuid, user, package_name); |
| 633 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 634 | LOG(WARNING) << "Failed to rollback " << to; |
| 635 | } |
| 636 | } |
| 637 | { |
| 638 | auto to = create_data_user_ce_package_path(to_uuid, user, package_name); |
| 639 | if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { |
| 640 | LOG(WARNING) << "Failed to rollback " << to; |
| 641 | } |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 642 | } |
| 643 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 644 | return res; |
Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 647 | binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, |
| 648 | int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { |
| 649 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 650 | CHECK_ARGUMENT_UUID(uuid); |
| 651 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 652 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 653 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 654 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 655 | if (ensure_config_user_dirs(userId) != 0) { |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 656 | return error(StringPrintf("Failed to ensure dirs for %d", userId)); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 657 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 658 | } |
| 659 | } |
Jeff Sharkey | 7be5ead | 2016-12-12 13:18:46 -0700 | [diff] [blame] | 660 | return ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, |
| 664 | int32_t userId, int32_t flags) { |
| 665 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 666 | CHECK_ARGUMENT_UUID(uuid); |
| 667 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 668 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 669 | binder::Status res = ok(); |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 670 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 671 | auto path = create_data_user_de_path(uuid_, userId); |
| 672 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 673 | res = error("Failed to delete " + path); |
| 674 | } |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 675 | if (uuid_ == nullptr) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 676 | path = create_data_misc_legacy_path(userId); |
| 677 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 678 | res = error("Failed to delete " + path); |
| 679 | } |
| 680 | path = create_data_user_profiles_path(userId); |
| 681 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 682 | res = error("Failed to delete " + path); |
| 683 | } |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 684 | } |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 685 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 686 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 687 | auto path = create_data_user_ce_path(uuid_, userId); |
| 688 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 689 | res = error("Failed to delete " + path); |
| 690 | } |
| 691 | path = create_data_media_path(uuid_, userId); |
| 692 | if (delete_dir_contents_and_dir(path, true) != 0) { |
| 693 | res = error("Failed to delete " + path); |
| 694 | } |
Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 695 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 696 | return res; |
Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 697 | } |
| 698 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 699 | /* Try to ensure free_size bytes of storage are available. |
| 700 | * Returns 0 on success. |
| 701 | * This is rather simple-minded because doing a full LRU would |
| 702 | * be potentially memory-intensive, and without atime it would |
| 703 | * also require that apps constantly modify file metadata even |
| 704 | * when just reading from the cache, which is pretty awful. |
| 705 | */ |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 706 | binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, |
| 707 | int64_t freeStorageSize) { |
| 708 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 709 | CHECK_ARGUMENT_UUID(uuid); |
| 710 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 711 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 712 | cache_t* cache; |
| 713 | int64_t avail; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 714 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 715 | auto data_path = create_data_path(uuid_); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 716 | |
| 717 | avail = data_disk_free(data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 718 | if (avail < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 719 | return error("Failed to determine free space for " + data_path); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 720 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 721 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 722 | ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); |
| 723 | if (avail >= freeStorageSize) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 724 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 725 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 726 | |
| 727 | cache = start_cache_collection(); |
| 728 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 729 | auto users = get_known_users(uuid_); |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 730 | for (auto user : users) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 731 | add_cache_files(cache, create_data_user_ce_path(uuid_, user)); |
| 732 | add_cache_files(cache, create_data_user_de_path(uuid_, user)); |
Jeff Sharkey | 54e292e | 2016-05-10 17:21:13 -0600 | [diff] [blame] | 733 | add_cache_files(cache, |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 734 | StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 737 | clear_cache_files(data_path, cache, freeStorageSize); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 738 | finish_cache_collection(cache); |
| 739 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 740 | avail = data_disk_free(data_path); |
| 741 | if (avail >= freeStorageSize) { |
| 742 | return ok(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 743 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 744 | return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, |
| 745 | freeStorageSize, data_path.c_str(), avail)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 746 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 749 | binder::Status InstalldNativeService::rmdex(const std::string& codePath, |
| 750 | const std::string& instructionSet) { |
| 751 | ENFORCE_UID(AID_SYSTEM); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 752 | char dex_path[PKG_PATH_MAX]; |
| 753 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 754 | const char* path = codePath.c_str(); |
| 755 | const char* instruction_set = instructionSet.c_str(); |
| 756 | |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 757 | if (validate_apk_path(path) && validate_system_app_path(path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 758 | return error("Invalid path " + codePath); |
Jeff Sharkey | 770180a | 2014-09-08 17:14:26 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 761 | if (!create_cache_path(dex_path, path, instruction_set)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 762 | return error("Failed to create cache path for " + codePath); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 763 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 764 | |
| 765 | ALOGV("unlink %s\n", dex_path); |
| 766 | if (unlink(dex_path) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 767 | return error(StringPrintf("Failed to unlink %s", dex_path)); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 768 | } else { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 769 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 773 | static void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize, |
| 774 | int64_t *cachesize) { |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 775 | DIR *d; |
| 776 | int dfd; |
| 777 | struct dirent *de; |
| 778 | struct stat s; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 779 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 780 | d = opendir(path.c_str()); |
| 781 | if (d == nullptr) { |
| 782 | PLOG(WARNING) << "Failed to open " << path; |
| 783 | return; |
| 784 | } |
| 785 | dfd = dirfd(d); |
| 786 | while ((de = readdir(d))) { |
| 787 | const char *name = de->d_name; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 788 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 789 | int64_t statsize = 0; |
| 790 | if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { |
| 791 | statsize = stat_size(&s); |
| 792 | } |
| 793 | |
| 794 | if (de->d_type == DT_DIR) { |
| 795 | int subfd; |
| 796 | int64_t dirsize = 0; |
| 797 | /* always skip "." and ".." */ |
| 798 | if (name[0] == '.') { |
| 799 | if (name[1] == 0) continue; |
| 800 | if ((name[1] == '.') && (name[2] == 0)) continue; |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 801 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 802 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); |
| 803 | if (subfd >= 0) { |
| 804 | dirsize = calculate_dir_size(subfd); |
| 805 | close(subfd); |
| 806 | } |
| 807 | // TODO: check xattrs! |
| 808 | if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { |
| 809 | *datasize += statsize; |
| 810 | *cachesize += dirsize; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 811 | } else { |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 812 | *datasize += dirsize + statsize; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 813 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 814 | } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) { |
| 815 | *codesize += statsize; |
| 816 | } else { |
| 817 | *datasize += statsize; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 818 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 819 | } |
| 820 | closedir(d); |
| 821 | } |
| 822 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 823 | binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 824 | const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode, |
| 825 | const std::string& codePath, std::vector<int64_t>* _aidl_return) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 826 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 827 | CHECK_ARGUMENT_UUID(uuid); |
| 828 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 829 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 830 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 831 | const char* pkgname = packageName.c_str(); |
| 832 | const char* code_path = codePath.c_str(); |
| 833 | |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 834 | DIR *d; |
| 835 | int dfd; |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 836 | int64_t codesize = 0; |
| 837 | int64_t datasize = 0; |
| 838 | int64_t cachesize = 0; |
| 839 | int64_t asecsize = 0; |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 840 | |
| 841 | d = opendir(code_path); |
| 842 | if (d != nullptr) { |
| 843 | dfd = dirfd(d); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 844 | codesize += calculate_dir_size(dfd); |
Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 845 | closedir(d); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 846 | } |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 847 | |
| 848 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 849 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); |
| 850 | add_app_data_size(path, &codesize, &datasize, &cachesize); |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 851 | } |
| 852 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 853 | auto path = create_data_user_de_package_path(uuid_, userId, pkgname); |
| 854 | add_app_data_size(path, &codesize, &datasize, &cachesize); |
Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 855 | } |
| 856 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 857 | std::vector<int64_t> res; |
| 858 | res.push_back(codesize); |
| 859 | res.push_back(datasize); |
| 860 | res.push_back(cachesize); |
| 861 | res.push_back(asecsize); |
| 862 | *_aidl_return = res; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 863 | return ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 864 | } |
| 865 | |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 866 | // Dumps the contents of a profile file, using pkgname's dex files for pretty |
| 867 | // printing the result. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 868 | binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, |
| 869 | const std::string& codePaths, bool* _aidl_return) { |
| 870 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 871 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 872 | |
| 873 | const char* pkgname = packageName.c_str(); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 874 | const char* code_paths = codePaths.c_str(); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 875 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 876 | *_aidl_return = dump_profiles(uid, pkgname, code_paths); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 877 | return ok(); |
David Sehr | 6727c2d | 2016-05-17 16:06:22 -0700 | [diff] [blame] | 878 | } |
| 879 | |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 880 | // TODO: Consider returning error codes. |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 881 | binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 882 | bool* _aidl_return) { |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 883 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 884 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 885 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 886 | const char* pkgname = packageName.c_str(); |
| 887 | *_aidl_return = analyse_profiles(uid, pkgname); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 888 | return ok(); |
Andreas Gampe | 4d0f825 | 2016-03-20 11:30:28 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 891 | binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, |
| 892 | const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, |
| 893 | int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, |
| 894 | const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, |
| 895 | const std::unique_ptr<std::string>& sharedLibraries) { |
| 896 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 897 | CHECK_ARGUMENT_UUID(uuid); |
| 898 | if (packageName && *packageName != "*") { |
| 899 | CHECK_ARGUMENT_PACKAGE_NAME(*packageName); |
| 900 | } |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 901 | |
| 902 | const char* apk_path = apkPath.c_str(); |
| 903 | const char* pkgname = packageName ? packageName->c_str() : "*"; |
| 904 | const char* instruction_set = instructionSet.c_str(); |
| 905 | const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; |
| 906 | const char* compiler_filter = compilerFilter.c_str(); |
| 907 | const char* volume_uuid = uuid ? uuid->c_str() : nullptr; |
| 908 | const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; |
| 909 | |
| 910 | int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, |
| 911 | oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 912 | return res ? error(res, "Failed to dexopt") : ok(); |
Jeff Sharkey | 6c2c056 | 2016-12-07 12:12:00 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 915 | binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { |
| 916 | ENFORCE_UID(AID_SYSTEM); |
| 917 | const char* instruction_set = instructionSet.c_str(); |
| 918 | |
| 919 | char boot_marker_path[PKG_PATH_MAX]; |
| 920 | sprintf(boot_marker_path, |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 921 | "%s/%s/%s/.booting", |
| 922 | android_data_dir.path, |
| 923 | DALVIK_CACHE, |
| 924 | instruction_set); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 925 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 926 | ALOGV("mark_boot_complete : %s", boot_marker_path); |
| 927 | if (unlink(boot_marker_path) != 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 928 | return error(StringPrintf("Failed to unlink %s", boot_marker_path)); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 929 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 930 | return ok(); |
Narayan Kamath | 091ea77 | 2014-11-10 15:03:46 +0000 | [diff] [blame] | 931 | } |
| 932 | |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 933 | void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, |
| 934 | struct stat* statbuf) |
| 935 | { |
| 936 | while (path[basepos] != 0) { |
| 937 | if (path[basepos] == '/') { |
| 938 | path[basepos] = 0; |
| 939 | if (lstat(path, statbuf) < 0) { |
| 940 | ALOGV("Making directory: %s\n", path); |
| 941 | if (mkdir(path, mode) == 0) { |
| 942 | chown(path, uid, gid); |
| 943 | } else { |
| 944 | ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); |
| 945 | } |
| 946 | } |
| 947 | path[basepos] = '/'; |
| 948 | basepos++; |
| 949 | } |
| 950 | basepos++; |
| 951 | } |
| 952 | } |
| 953 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 954 | binder::Status InstalldNativeService::linkNativeLibraryDirectory( |
| 955 | const std::unique_ptr<std::string>& uuid, const std::string& packageName, |
| 956 | const std::string& nativeLibPath32, int32_t userId) { |
| 957 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 958 | CHECK_ARGUMENT_UUID(uuid); |
| 959 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 960 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 961 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 962 | const char* pkgname = packageName.c_str(); |
| 963 | const char* asecLibDir = nativeLibPath32.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 964 | struct stat s, libStat; |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 965 | binder::Status res = ok(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 966 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 967 | auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); |
| 968 | auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; |
Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 969 | |
| 970 | const char* pkgdir = _pkgdir.c_str(); |
| 971 | const char* libsymlink = _libsymlink.c_str(); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 972 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 973 | if (stat(pkgdir, &s) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 974 | return error("Failed to stat " + _pkgdir); |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 975 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 976 | |
| 977 | if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 978 | return error("Failed to chown " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if (chmod(pkgdir, 0700) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 982 | res = error("Failed to chmod " + _pkgdir); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 983 | goto out; |
| 984 | } |
| 985 | |
| 986 | if (lstat(libsymlink, &libStat) < 0) { |
| 987 | if (errno != ENOENT) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 988 | res = error("Failed to stat " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 989 | goto out; |
| 990 | } |
| 991 | } else { |
| 992 | if (S_ISDIR(libStat.st_mode)) { |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 993 | if (delete_dir_contents(libsymlink, 1, NULL) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 994 | res = error("Failed to delete " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 995 | goto out; |
| 996 | } |
| 997 | } else if (S_ISLNK(libStat.st_mode)) { |
| 998 | if (unlink(libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 999 | res = error("Failed to unlink " + _libsymlink); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1000 | goto out; |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | if (symlink(asecLibDir, libsymlink) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1006 | res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1007 | goto out; |
| 1008 | } |
| 1009 | |
| 1010 | out: |
| 1011 | if (chmod(pkgdir, s.st_mode) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1012 | auto msg = "Failed to cleanup chmod " + _pkgdir; |
| 1013 | if (res.isOk()) { |
| 1014 | res = error(msg); |
| 1015 | } else { |
| 1016 | PLOG(ERROR) << msg; |
| 1017 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1021 | auto msg = "Failed to cleanup chown " + _pkgdir; |
| 1022 | if (res.isOk()) { |
| 1023 | res = error(msg); |
| 1024 | } else { |
| 1025 | PLOG(ERROR) << msg; |
| 1026 | } |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1029 | return res; |
Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1030 | } |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1031 | |
| 1032 | static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) |
| 1033 | { |
| 1034 | static const char *IDMAP_BIN = "/system/bin/idmap"; |
| 1035 | static const size_t MAX_INT_LEN = 32; |
| 1036 | char idmap_str[MAX_INT_LEN]; |
| 1037 | |
| 1038 | snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); |
| 1039 | |
| 1040 | execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); |
| 1041 | ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); |
| 1042 | } |
| 1043 | |
| 1044 | // Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) |
| 1045 | // eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap |
| 1046 | static int flatten_path(const char *prefix, const char *suffix, |
| 1047 | const char *overlay_path, char *idmap_path, size_t N) |
| 1048 | { |
| 1049 | if (overlay_path == NULL || idmap_path == NULL) { |
| 1050 | return -1; |
| 1051 | } |
| 1052 | const size_t len_overlay_path = strlen(overlay_path); |
| 1053 | // will access overlay_path + 1 further below; requires absolute path |
| 1054 | if (len_overlay_path < 2 || *overlay_path != '/') { |
| 1055 | return -1; |
| 1056 | } |
| 1057 | const size_t len_idmap_root = strlen(prefix); |
| 1058 | const size_t len_suffix = strlen(suffix); |
| 1059 | if (SIZE_MAX - len_idmap_root < len_overlay_path || |
| 1060 | SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { |
| 1061 | // additions below would cause overflow |
| 1062 | return -1; |
| 1063 | } |
| 1064 | if (N < len_idmap_root + len_overlay_path + len_suffix) { |
| 1065 | return -1; |
| 1066 | } |
| 1067 | memset(idmap_path, 0, N); |
| 1068 | snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); |
| 1069 | char *ch = idmap_path + len_idmap_root; |
| 1070 | while (*ch != '\0') { |
| 1071 | if (*ch == '/') { |
| 1072 | *ch = '@'; |
| 1073 | } |
| 1074 | ++ch; |
| 1075 | } |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1079 | binder::Status InstalldNativeService::idmap(const std::string& targetApkPath, |
| 1080 | const std::string& overlayApkPath, int32_t uid) { |
| 1081 | ENFORCE_UID(AID_SYSTEM); |
| 1082 | const char* target_apk = targetApkPath.c_str(); |
| 1083 | const char* overlay_apk = overlayApkPath.c_str(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1084 | ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); |
| 1085 | |
| 1086 | int idmap_fd = -1; |
| 1087 | char idmap_path[PATH_MAX]; |
| 1088 | |
| 1089 | if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, |
| 1090 | idmap_path, sizeof(idmap_path)) == -1) { |
| 1091 | ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); |
| 1092 | goto fail; |
| 1093 | } |
| 1094 | |
| 1095 | unlink(idmap_path); |
| 1096 | idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); |
| 1097 | if (idmap_fd < 0) { |
| 1098 | ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); |
| 1099 | goto fail; |
| 1100 | } |
| 1101 | if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { |
| 1102 | ALOGE("idmap cannot chown '%s'\n", idmap_path); |
| 1103 | goto fail; |
| 1104 | } |
| 1105 | if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { |
| 1106 | ALOGE("idmap cannot chmod '%s'\n", idmap_path); |
| 1107 | goto fail; |
| 1108 | } |
| 1109 | |
| 1110 | pid_t pid; |
| 1111 | pid = fork(); |
| 1112 | if (pid == 0) { |
| 1113 | /* child -- drop privileges before continuing */ |
| 1114 | if (setgid(uid) != 0) { |
| 1115 | ALOGE("setgid(%d) failed during idmap\n", uid); |
| 1116 | exit(1); |
| 1117 | } |
| 1118 | if (setuid(uid) != 0) { |
| 1119 | ALOGE("setuid(%d) failed during idmap\n", uid); |
| 1120 | exit(1); |
| 1121 | } |
| 1122 | if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { |
| 1123 | ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); |
| 1124 | exit(1); |
| 1125 | } |
| 1126 | |
| 1127 | run_idmap(target_apk, overlay_apk, idmap_fd); |
| 1128 | exit(1); /* only if exec call to idmap failed */ |
| 1129 | } else { |
| 1130 | int status = wait_child(pid); |
| 1131 | if (status != 0) { |
| 1132 | ALOGE("idmap failed, status=0x%04x\n", status); |
| 1133 | goto fail; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | close(idmap_fd); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1138 | return ok(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1139 | fail: |
| 1140 | if (idmap_fd >= 0) { |
| 1141 | close(idmap_fd); |
| 1142 | unlink(idmap_path); |
| 1143 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1144 | return error(); |
MÃ¥rten Kongstad | 63568b1 | 2014-01-31 14:42:59 +0100 | [diff] [blame] | 1145 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1146 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1147 | binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, |
| 1148 | const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, |
| 1149 | const std::string& seInfo) { |
| 1150 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1151 | CHECK_ARGUMENT_UUID(uuid); |
| 1152 | CHECK_ARGUMENT_PACKAGE_NAME(packageName); |
| 1153 | |
| 1154 | binder::Status res = ok(); |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1155 | |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1156 | // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1157 | unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1158 | const char* uuid_ = uuid ? uuid->c_str() : nullptr; |
| 1159 | const char* pkgName = packageName.c_str(); |
| 1160 | const char* seinfo = seInfo.c_str(); |
Robert Craig | da30dc7 | 2014-03-27 10:21:12 -0400 | [diff] [blame] | 1161 | |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1162 | uid_t uid = multiuser_get_uid(userId, appId); |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1163 | if (flags & FLAG_STORAGE_CE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1164 | auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1165 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1166 | res = error("restorecon failed for " + path); |
Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 1167 | } |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1168 | } |
Jeff Sharkey | aa7ddfd | 2016-02-03 14:03:16 -0700 | [diff] [blame] | 1169 | if (flags & FLAG_STORAGE_DE) { |
Jeff Sharkey | c1e93e7 | 2016-12-05 23:39:32 -0700 | [diff] [blame] | 1170 | auto path = create_data_user_de_package_path(uuid_, userId, pkgName); |
Jeff Sharkey | c7d1b22 | 2016-01-11 13:07:09 -0700 | [diff] [blame] | 1171 | if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1172 | res = error("restorecon failed for " + path); |
Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 1173 | } |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1174 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1175 | return res; |
Robert Craig | e9887e4 | 2014-02-20 10:25:56 -0500 | [diff] [blame] | 1176 | } |
Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 1177 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1178 | binder::Status InstalldNativeService::createOatDir(const std::string& oatDir, |
| 1179 | const std::string& instructionSet) { |
| 1180 | ENFORCE_UID(AID_SYSTEM); |
| 1181 | const char* oat_dir = oatDir.c_str(); |
| 1182 | const char* instruction_set = instructionSet.c_str(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1183 | char oat_instr_dir[PKG_PATH_MAX]; |
| 1184 | |
| 1185 | if (validate_apk_path(oat_dir)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1186 | return error("Invalid path " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1187 | } |
Fyodor Kupolov | 8eed7e6 | 2015-04-06 19:09:02 -0700 | [diff] [blame] | 1188 | 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] | 1189 | return error("Failed to prepare " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1190 | } |
| 1191 | if (selinux_android_restorecon(oat_dir, 0)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1192 | return error("Failed to restorecon " + oatDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1193 | } |
| 1194 | 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] | 1195 | 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] | 1196 | return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1197 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1198 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1201 | binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { |
| 1202 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1203 | if (validate_apk_path(packageDir.c_str())) { |
| 1204 | return error("Invalid path " + packageDir); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1205 | } |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1206 | if (delete_dir_contents_and_dir(packageDir) != 0) { |
| 1207 | return error("Failed to delete " + packageDir); |
| 1208 | } |
| 1209 | return ok(); |
Fyodor Kupolov | 88ce4ff | 2015-03-03 12:25:29 -0800 | [diff] [blame] | 1210 | } |
| 1211 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1212 | binder::Status InstalldNativeService::linkFile(const std::string& relativePath, |
| 1213 | const std::string& fromBase, const std::string& toBase) { |
| 1214 | ENFORCE_UID(AID_SYSTEM); |
| 1215 | const char* relative_path = relativePath.c_str(); |
| 1216 | const char* from_base = fromBase.c_str(); |
| 1217 | const char* to_base = toBase.c_str(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1218 | char from_path[PKG_PATH_MAX]; |
| 1219 | char to_path[PKG_PATH_MAX]; |
| 1220 | snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); |
| 1221 | snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); |
| 1222 | |
| 1223 | if (validate_apk_path_subdirs(from_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1224 | return error(StringPrintf("Invalid from path %s", from_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | if (validate_apk_path_subdirs(to_path)) { |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1228 | return error(StringPrintf("Invalid to path %s", to_path)); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1229 | } |
| 1230 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1231 | if (link(from_path, to_path) < 0) { |
| 1232 | 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] | 1233 | } |
| 1234 | |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1235 | return ok(); |
Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 1236 | } |
| 1237 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1238 | binder::Status InstalldNativeService::moveAb(const std::string& apkPath, |
| 1239 | const std::string& instructionSet, const std::string& outputPath) { |
| 1240 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1241 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1242 | const char* apk_path = apkPath.c_str(); |
| 1243 | const char* instruction_set = instructionSet.c_str(); |
| 1244 | const char* oat_dir = outputPath.c_str(); |
Andreas Gampe | d089ca1 | 2016-06-27 14:25:30 -0700 | [diff] [blame] | 1245 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1246 | bool success = move_ab(apk_path, instruction_set, oat_dir); |
Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 1247 | return success ? ok() : error(); |
Andreas Gampe | e65b4fa | 2016-03-01 11:46:45 -0800 | [diff] [blame] | 1248 | } |
| 1249 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1250 | binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, |
| 1251 | const std::string& instructionSet, const std::string& outputPath) { |
| 1252 | ENFORCE_UID(AID_SYSTEM); |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1253 | |
Jeff Sharkey | 475c6f9 | 2016-12-07 10:37:27 -0700 | [diff] [blame] | 1254 | const char* apk_path = apkPath.c_str(); |
| 1255 | const char* instruction_set = instructionSet.c_str(); |
| 1256 | const char* oat_dir = outputPath.c_str(); |
| 1257 | |
Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 1258 | bool res = delete_odex(apk_path, instruction_set, oat_dir); |
| 1259 | return res ? ok() : error(); |
Andreas Gampe | b31206b | 2016-09-09 17:07:04 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1262 | } // namespace installd |
| 1263 | } // namespace android |