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