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