blob: c1413ff0154a06b7d2be90103ec799f6b5786cc5 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/*
2** Copyright 2008, The Android Open Source Project
3**
Dave Allisond9370732014-01-30 14:19:23 -08004** 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 Lockwood94afecf2012-10-24 10:45:23 -07007**
Dave Allisond9370732014-01-30 14:19:23 -08008** http://www.apache.org/licenses/LICENSE-2.0
Mike Lockwood94afecf2012-10-24 10:45:23 -07009**
Dave Allisond9370732014-01-30 14:19:23 -080010** 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 Lockwood94afecf2012-10-24 10:45:23 -070014** limitations under the License.
15*/
16
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070017#include "InstalldNativeService.h"
Andreas Gampe02d0de52015-11-11 20:43:16 -080018
Jeff Sharkey466459b2017-01-17 15:25:01 -070019#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
20
Andreas Gampe02d0de52015-11-11 20:43:16 -080021#include <errno.h>
22#include <inttypes.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070023#include <fstream>
24#include <fts.h>
Andreas Gamped089ca12016-06-27 14:25:30 -070025#include <regex>
Andreas Gampe02d0de52015-11-11 20:43:16 -080026#include <stdlib.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070027#include <string.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080028#include <sys/capability.h>
29#include <sys/file.h>
30#include <sys/resource.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070031#include <sys/quota.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080032#include <sys/stat.h>
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070033#include <sys/statvfs.h>
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070034#include <sys/types.h>
Calin Juravle6a1648e2016-02-01 12:12:16 +000035#include <sys/wait.h>
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070036#include <sys/xattr.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080037#include <unistd.h>
Jeff Sharkey41ea4242015-04-09 11:34:03 -070038
Andreas Gampe6fb5a012016-06-03 16:09:32 -070039#include <android-base/logging.h>
Elliott Hughese4ec9eb2015-12-04 15:39:32 -080040#include <android-base/stringprintf.h>
David Sehr6727c2d2016-05-17 16:06:22 -070041#include <android-base/strings.h>
Roland Levillain64b59cc2016-04-05 16:41:12 +010042#include <android-base/unique_fd.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080043#include <cutils/fs.h>
Jeff Sharkey90aff262016-12-12 14:28:24 -070044#include <cutils/properties.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070045#include <cutils/sched_policy.h>
Mark Salyzyn7823e122016-09-29 08:08:05 -070046#include <log/log.h> // TODO: Move everything to base/logging.
Jeff Sharkeye3637242015-04-08 20:56:42 -070047#include <logwrap/logwrap.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080048#include <private/android_filesystem_config.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070049#include <selinux/android.h>
Andreas Gampe02d0de52015-11-11 20:43:16 -080050#include <system/thread_defs.h>
Jeff Sharkey466459b2017-01-17 15:25:01 -070051#include <utils/Trace.h>
Jeff Sharkeye3637242015-04-08 20:56:42 -070052
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070053#include "dexopt.h"
Jeff Sharkeyf3e30b92016-12-09 17:06:57 -070054#include "globals.h"
55#include "installd_deps.h"
56#include "otapreopt_utils.h"
57#include "utils.h"
Jeff Sharkey6c2c0562016-12-07 12:12:00 -070058
Jeff Sharkeydf2d7542017-01-07 09:19:35 -070059#include "MatchExtensionGen.h"
60
Andreas Gampe02d0de52015-11-11 20:43:16 -080061#ifndef LOG_TAG
62#define LOG_TAG "installd"
63#endif
Jeff Sharkey41ea4242015-04-09 11:34:03 -070064
65using android::base::StringPrintf;
Jeff Sharkey66b1a122017-01-16 20:57:45 -070066using std::endl;
Mike Lockwood94afecf2012-10-24 10:45:23 -070067
Andreas Gampe02d0de52015-11-11 20:43:16 -080068namespace android {
69namespace installd {
Mike Lockwood94afecf2012-10-24 10:45:23 -070070
Jeff Sharkeycc6281c2016-02-06 19:46:09 -070071static constexpr const char* kCpPath = "/system/bin/cp";
72static constexpr const char* kXattrDefault = "user.default";
Jeff Sharkeye3637242015-04-08 20:56:42 -070073
Andreas Gamped089ca12016-06-27 14:25:30 -070074static constexpr const char* PKG_LIB_POSTFIX = "/lib";
75static constexpr const char* CACHE_DIR_POSTFIX = "/cache";
76static constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache";
77
78static constexpr const char* IDMAP_PREFIX = "/data/resource-cache/";
79static constexpr const char* IDMAP_SUFFIX = "@idmap";
80
81// NOTE: keep in sync with StorageManager
82static constexpr int FLAG_STORAGE_DE = 1 << 0;
83static constexpr int FLAG_STORAGE_CE = 1 << 1;
84
85// NOTE: keep in sync with Installer
86static constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8;
87static constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -070088static constexpr int FLAG_USE_QUOTA = 1 << 12;
Andreas Gamped089ca12016-06-27 14:25:30 -070089
Janis Danisevskiseecb2d22016-01-12 14:45:55 +000090#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M
Jeff Sharkey0274c972016-12-06 09:32:04 -070091namespace {
92
93constexpr const char* kDump = "android.permission.DUMP";
94
Jeff Sharkey423e7462016-12-09 18:18:43 -070095static binder::Status ok() {
96 return binder::Status::ok();
97}
98
99static binder::Status exception(uint32_t code, const std::string& msg) {
100 return binder::Status::fromExceptionCode(code, String8(msg.c_str()));
101}
102
103static binder::Status error() {
104 return binder::Status::fromServiceSpecificError(errno);
105}
106
107static binder::Status error(const std::string& msg) {
108 PLOG(ERROR) << msg;
109 return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
110}
111
112static binder::Status error(uint32_t code, const std::string& msg) {
113 LOG(ERROR) << msg << " (" << code << ")";
114 return binder::Status::fromServiceSpecificError(code, String8(msg.c_str()));
115}
116
Jeff Sharkey0274c972016-12-06 09:32:04 -0700117binder::Status checkPermission(const char* permission) {
118 pid_t pid;
119 uid_t uid;
120
121 if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid),
122 reinterpret_cast<int32_t*>(&uid))) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700123 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700124 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700125 return exception(binder::Status::EX_SECURITY,
126 StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700127 }
128}
129
130binder::Status checkUid(uid_t expectedUid) {
131 uid_t uid = IPCThreadState::self()->getCallingUid();
132 if (uid == expectedUid || uid == AID_ROOT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700133 return ok();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700134 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700135 return exception(binder::Status::EX_SECURITY,
136 StringPrintf("UID %d is not expected UID %d", uid, expectedUid));
137 }
138}
139
140binder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) {
141 if (!uuid || is_valid_filename(*uuid)) {
142 return ok();
143 } else {
144 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
145 StringPrintf("UUID %s is malformed", uuid->c_str()));
146 }
147}
148
149binder::Status checkArgumentPackageName(const std::string& packageName) {
150 if (is_valid_package_name(packageName.c_str())) {
151 return ok();
152 } else {
153 return exception(binder::Status::EX_ILLEGAL_ARGUMENT,
154 StringPrintf("Package name %s is malformed", packageName.c_str()));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700155 }
156}
157
158#define ENFORCE_UID(uid) { \
159 binder::Status status = checkUid((uid)); \
160 if (!status.isOk()) { \
161 return status; \
162 } \
163}
164
Jeff Sharkey423e7462016-12-09 18:18:43 -0700165#define CHECK_ARGUMENT_UUID(uuid) { \
166 binder::Status status = checkArgumentUuid((uuid)); \
167 if (!status.isOk()) { \
168 return status; \
169 } \
170}
171
172#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \
173 binder::Status status = \
174 checkArgumentPackageName((packageName)); \
175 if (!status.isOk()) { \
176 return status; \
177 } \
178}
179
Jeff Sharkey0274c972016-12-06 09:32:04 -0700180} // namespace
181
182status_t InstalldNativeService::start() {
183 IPCThreadState::self()->disableBackgroundScheduling(true);
184 status_t ret = BinderService<InstalldNativeService>::publish();
185 if (ret != android::OK) {
186 return ret;
187 }
188 sp<ProcessState> ps(ProcessState::self());
189 ps->startThreadPool();
190 ps->giveThreadPoolName();
191 return android::OK;
192}
193
194status_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700195 auto out = std::fstream(StringPrintf("/proc/self/fd/%d", fd));
Jeff Sharkey0274c972016-12-06 09:32:04 -0700196 const binder::Status dump_permission = checkPermission(kDump);
197 if (!dump_permission.isOk()) {
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700198 out << dump_permission.toString8() << endl;
Jeff Sharkey0274c972016-12-06 09:32:04 -0700199 return PERMISSION_DENIED;
200 }
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700201 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700202
Jeff Sharkey66b1a122017-01-16 20:57:45 -0700203 out << "installd is happy!" << endl << endl;
204 out << "Devices with quota support:" << endl;
205 for (const auto& n : mQuotaDevices) {
206 out << " " << n.first << " = " << n.second << endl;
207 }
208 out << endl;
209 out.flush();
210
Jeff Sharkey0274c972016-12-06 09:32:04 -0700211 return NO_ERROR;
212}
213
Jeff Sharkey7db60412016-09-20 18:21:42 -0600214/**
215 * Perform restorecon of the given path, but only perform recursive restorecon
216 * if the label of that top-level file actually changed. This can save us
217 * significant time by avoiding no-op traversals of large filesystem trees.
218 */
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700219static int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid,
220 bool existing) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600221 int res = 0;
222 char* before = nullptr;
223 char* after = nullptr;
224
225 // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by
226 // libselinux. Not needed here.
227
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600228 if (lgetfilecon(path.c_str(), &before) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600229 PLOG(ERROR) << "Failed before getfilecon for " << path;
230 goto fail;
231 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700232 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600233 PLOG(ERROR) << "Failed top-level restorecon for " << path;
234 goto fail;
235 }
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600236 if (lgetfilecon(path.c_str(), &after) < 0) {
Jeff Sharkey7db60412016-09-20 18:21:42 -0600237 PLOG(ERROR) << "Failed after getfilecon for " << path;
238 goto fail;
239 }
240
241 // If the initial top-level restorecon above changed the label, then go
242 // back and restorecon everything recursively
243 if (strcmp(before, after)) {
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700244 if (existing) {
245 LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at "
246 << path << "; running recursive restorecon";
247 }
Jeff Sharkey0274c972016-12-06 09:32:04 -0700248 if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid,
Jeff Sharkey7db60412016-09-20 18:21:42 -0600249 SELINUX_ANDROID_RESTORECON_RECURSE) < 0) {
250 PLOG(ERROR) << "Failed recursive restorecon for " << path;
251 goto fail;
252 }
253 }
254
255 goto done;
256fail:
257 res = -1;
258done:
259 free(before);
260 free(after);
261 return res;
262}
263
Jeff Sharkey0274c972016-12-06 09:32:04 -0700264static int restorecon_app_data_lazy(const std::string& parent, const char* name,
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700265 const std::string& seInfo, uid_t uid, bool existing) {
266 return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid,
267 existing);
Jeff Sharkey95d9ac62016-10-31 11:22:19 -0600268}
269
Jeff Sharkey7db60412016-09-20 18:21:42 -0600270static int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600271 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) {
272 PLOG(ERROR) << "Failed to prepare " << path;
273 return -1;
274 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600275 return 0;
276}
277
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700278/**
279 * Prepare an app cache directory, which offers to fix-up the GID and
280 * directory mode flags during a platform upgrade.
281 */
282static int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode,
283 uid_t uid, gid_t gid) {
284 auto path = StringPrintf("%s/%s", parent.c_str(), name);
285 struct stat st;
286 if (stat(path.c_str(), &st) != 0) {
287 if (errno == ENOENT) {
288 // This is fine, just create it
289 if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) {
290 PLOG(ERROR) << "Failed to prepare " << path;
291 return -1;
292 } else {
293 return 0;
294 }
295 } else {
296 PLOG(ERROR) << "Failed to stat " << path;
297 return -1;
298 }
299 }
300
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700301 mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700302 if (st.st_uid != uid) {
303 // Mismatched UID is real trouble; we can't recover
304 LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid
305 << " but expected " << uid;
306 return -1;
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700307 } else if (st.st_gid == gid && actual_mode == target_mode) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700308 // Everything looks good!
309 return 0;
310 }
311
312 // Directory is owned correctly, but GID or mode mismatch means it's
313 // probably a platform upgrade so we need to fix them
314 FTS *fts;
315 FTSENT *p;
316 char *argv[] = { (char*) path.c_str(), nullptr };
317 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) {
318 PLOG(ERROR) << "Failed to fts_open " << path;
319 return -1;
320 }
321 while ((p = fts_read(fts)) != NULL) {
322 switch (p->fts_info) {
323 case FTS_DP:
324 if (chmod(p->fts_accpath, target_mode) != 0) {
325 PLOG(WARNING) << "Failed to chmod " << p->fts_path;
326 }
327 // Intentional fall through to also set GID
328 case FTS_F:
329 if (chown(p->fts_accpath, -1, gid) != 0) {
330 PLOG(WARNING) << "Failed to chown " << p->fts_path;
331 }
332 break;
333 case FTS_SL:
334 case FTS_SLNONE:
335 if (lchown(p->fts_accpath, -1, gid) != 0) {
336 PLOG(WARNING) << "Failed to chown " << p->fts_path;
337 }
338 break;
339 }
340 }
341 fts_close(fts);
342 return 0;
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600343}
344
Jeff Sharkey0274c972016-12-06 09:32:04 -0700345binder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid,
346 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700347 const std::string& seInfo, int32_t targetSdkVersion, int64_t* _aidl_return) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700348 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700349 CHECK_ARGUMENT_UUID(uuid);
350 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700351 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700352
353 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
354 const char* pkgname = packageName.c_str();
355
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700356 // Assume invalid inode unless filled in below
357 if (_aidl_return != nullptr) *_aidl_return = -1;
358
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700359 int32_t uid = multiuser_get_uid(userId, appId);
360 int32_t cacheGid = multiuser_get_cache_gid(userId, appId);
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700361 mode_t targetMode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751;
362
Jeff Sharkey24ef15b2017-01-12 19:27:03 -0700363 // If UID doesn't have a specific cache GID, use UID value
364 if (cacheGid == -1) {
365 cacheGid = uid;
366 }
367
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700368 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700369 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700370 bool existing = (access(path.c_str(), F_OK) == 0);
371
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700372 if (prepare_app_dir(path, targetMode, uid) ||
373 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
374 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700375 return error("Failed to prepare " + path);
Jeff Sharkey7db60412016-09-20 18:21:42 -0600376 }
377
378 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700379 if (restorecon_app_data_lazy(path, seInfo, uid, existing) ||
380 restorecon_app_data_lazy(path, "cache", seInfo, uid, existing) ||
381 restorecon_app_data_lazy(path, "code_cache", seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700382 return error("Failed to restorecon " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700383 }
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600384
385 // Remember inode numbers of cache directories so that we can clear
386 // contents while CE storage is locked
387 if (write_path_inode(path, "cache", kXattrInodeCache) ||
388 write_path_inode(path, "code_cache", kXattrInodeCodeCache)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700389 return error("Failed to write_path_inode for " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700390 }
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700391
392 // And return the CE inode of the top-level data directory so we can
393 // clear contents while CE storage is locked
394 if ((_aidl_return != nullptr)
395 && get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) != 0) {
396 return error("Failed to get_path_inode for " + path);
397 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700398 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700399 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700400 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700401 bool existing = (access(path.c_str(), F_OK) == 0);
402
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700403 if (prepare_app_dir(path, targetMode, uid) ||
404 prepare_app_cache_dir(path, "cache", 02771, uid, cacheGid) ||
405 prepare_app_cache_dir(path, "code_cache", 02771, uid, cacheGid)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700406 return error("Failed to prepare " + path);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700407 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000408
Jeff Sharkey7db60412016-09-20 18:21:42 -0600409 // Consider restorecon over contents if label changed
Jeff Sharkey35b83df2016-12-21 09:33:55 -0700410 if (restorecon_app_data_lazy(path, seInfo, uid, existing)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700411 return error("Failed to restorecon " + path);
Jeff Sharkey7db60412016-09-20 18:21:42 -0600412 }
413
Jeff Sharkey90aff262016-12-12 14:28:24 -0700414 if (property_get_bool("dalvik.vm.usejitprofiles", false)) {
Jeff Sharkey0274c972016-12-06 09:32:04 -0700415 const std::string profile_path = create_data_user_profile_package_path(userId, pkgname);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000416 // read-write-execute only for the app user.
417 if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700418 return error("Failed to prepare " + profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000419 }
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100420 std::string profile_file = create_primary_profile(profile_path);
421 // read-write only for the app user.
422 if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700423 return error("Failed to prepare " + profile_path);
Calin Juravlebc56e7d2016-05-24 15:33:12 +0100424 }
Calin Juravle6a1648e2016-02-01 12:12:16 +0000425 const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname);
426 // dex2oat/profman runs under the shared app gid and it needs to read/write reference
427 // profiles.
Jeff Sharkeyadddd982017-01-03 14:33:50 -0700428 int shared_app_gid = multiuser_get_shared_gid(0, appId);
Jeff Sharkey2e6dc9b2016-12-15 14:50:11 -0700429 if ((shared_app_gid != -1) && fs_prepare_dir_strict(
Calin Juravle6a1648e2016-02-01 12:12:16 +0000430 ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700431 return error("Failed to prepare " + ref_profile_path);
Calin Juravle6a1648e2016-02-01 12:12:16 +0000432 }
433 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700434 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700435 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700436}
437
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700438binder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid,
439 const std::string& packageName, int32_t userId, int32_t flags) {
440 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700441 CHECK_ARGUMENT_UUID(uuid);
442 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700443 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700444
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700445 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
446 const char* pkgname = packageName.c_str();
447
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700448 // This method only exists to upgrade system apps that have requested
449 // forceDeviceEncrypted, so their default storage always lives in a
450 // consistent location. This only works on non-FBE devices, since we
451 // never want to risk exposing data on a device with real CE/DE storage.
452
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700453 auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname);
454 auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700455
456 // If neither directory is marked as default, assume CE is default
457 if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1
458 && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) {
459 if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700460 return error("Failed to mark default storage " + ce_path);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700461 }
462 }
463
464 // Migrate default data location if needed
465 auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path;
466 auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path;
467
468 if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) {
469 LOG(WARNING) << "Requested default storage " << target
470 << " is not active; migrating from " << source;
471 if (delete_dir_contents_and_dir(target) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700472 return error("Failed to delete " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700473 }
474 if (rename(source.c_str(), target.c_str()) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700475 return error("Failed to rename " + source + " to " + target);
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700476 }
477 }
478
Jeff Sharkey423e7462016-12-09 18:18:43 -0700479 return ok();
Jeff Sharkeycc6281c2016-02-06 19:46:09 -0700480}
481
Calin Juravle6a1648e2016-02-01 12:12:16 +0000482
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700483binder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) {
484 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700485 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700486 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700487
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700488 const char* pkgname = packageName.c_str();
Jeff Sharkey423e7462016-12-09 18:18:43 -0700489 binder::Status res = ok();
490 if (!clear_reference_profile(pkgname)) {
491 res = error("Failed to clear reference profile for " + packageName);
492 }
493 if (!clear_current_profiles(pkgname)) {
494 res = error("Failed to clear current profiles for " + packageName);
495 }
496 return res;
Calin Juravle6a1648e2016-02-01 12:12:16 +0000497}
498
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700499binder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid,
500 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
501 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700502 CHECK_ARGUMENT_UUID(uuid);
503 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700504 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700505
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700506 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
507 const char* pkgname = packageName.c_str();
508
Jeff Sharkey423e7462016-12-09 18:18:43 -0700509 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700510 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700511 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600512 if (flags & FLAG_CLEAR_CACHE_ONLY) {
513 path = read_path_inode(path, "cache", kXattrInodeCache);
514 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
515 path = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
516 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700517 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700518 if (delete_dir_contents(path) != 0) {
519 res = error("Failed to delete contents of " + path);
520 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700521 }
522 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700523 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey9a998f42016-07-14 18:16:22 -0600524 std::string suffix = "";
525 bool only_cache = false;
526 if (flags & FLAG_CLEAR_CACHE_ONLY) {
527 suffix = CACHE_DIR_POSTFIX;
528 only_cache = true;
529 } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) {
530 suffix = CODE_CACHE_DIR_POSTFIX;
531 only_cache = true;
532 }
533
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700534 auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix;
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700535 if (access(path.c_str(), F_OK) == 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700536 if (delete_dir_contents(path) != 0) {
537 res = error("Failed to delete contents of " + path);
538 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700539 }
Calin Juravleedae6692016-03-23 13:55:31 +0000540 if (!only_cache) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700541 if (!clear_current_profile(pkgname, userId)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700542 res = error("Failed to clear current profile for " + packageName);
Calin Juravleedae6692016-03-23 13:55:31 +0000543 }
544 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700545 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700546 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700547}
548
Calin Juravle7535e8e2016-03-29 16:05:40 +0100549static int destroy_app_reference_profile(const char *pkgname) {
550 return delete_dir_contents_and_dir(
551 create_data_ref_profile_package_path(pkgname),
552 /*ignore_if_missing*/ true);
553}
554
Calin Juravleedae6692016-03-23 13:55:31 +0000555static int destroy_app_current_profiles(const char *pkgname, userid_t userid) {
Calin Juravleb06f98a2016-03-28 15:11:01 +0100556 return delete_dir_contents_and_dir(
557 create_data_user_profile_package_path(userid, pkgname),
558 /*ignore_if_missing*/ true);
Calin Juravleedae6692016-03-23 13:55:31 +0000559}
560
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700561binder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) {
562 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700563 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700564 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700565
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700566 const char* pkgname = packageName.c_str();
Jeff Sharkey423e7462016-12-09 18:18:43 -0700567 binder::Status res = ok();
Calin Juravleedae6692016-03-23 13:55:31 +0000568 std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr);
569 for (auto user : users) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700570 if (destroy_app_current_profiles(pkgname, user) != 0) {
571 res = error("Failed to destroy current profiles for " + packageName);
572 }
Calin Juravleedae6692016-03-23 13:55:31 +0000573 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700574 if (destroy_app_reference_profile(pkgname) != 0) {
575 res = error("Failed to destroy reference profile for " + packageName);
576 }
577 return res;
Calin Juravlecaa6b802016-03-22 14:15:52 +0000578}
579
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700580binder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid,
581 const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) {
582 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700583 CHECK_ARGUMENT_UUID(uuid);
584 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700585 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700586
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700587 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
588 const char* pkgname = packageName.c_str();
589
Jeff Sharkey423e7462016-12-09 18:18:43 -0700590 binder::Status res = ok();
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700591 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700592 auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode);
593 if (delete_dir_contents_and_dir(path) != 0) {
594 res = error("Failed to delete " + path);
595 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700596 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -0700597 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700598 auto path = create_data_user_de_package_path(uuid_, userId, pkgname);
599 if (delete_dir_contents_and_dir(path) != 0) {
600 res = error("Failed to delete " + path);
601 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700602 destroy_app_current_profiles(pkgname, userId);
Calin Juravle7535e8e2016-03-29 16:05:40 +0100603 // TODO(calin): If the package is still installed by other users it's probably
604 // beneficial to keep the reference profile around.
605 // Verify if it's ok to do that.
606 destroy_app_reference_profile(pkgname);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700607 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700608 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700609}
610
Jeff Sharkey0274c972016-12-06 09:32:04 -0700611binder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid,
612 const std::unique_ptr<std::string>& toUuid, const std::string& packageName,
613 const std::string& dataAppName, int32_t appId, const std::string& seInfo,
614 int32_t targetSdkVersion) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700615 ENFORCE_UID(AID_SYSTEM);
616 CHECK_ARGUMENT_UUID(fromUuid);
617 CHECK_ARGUMENT_UUID(toUuid);
618 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700619 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey0274c972016-12-06 09:32:04 -0700620
621 const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr;
622 const char* to_uuid = toUuid ? toUuid->c_str() : nullptr;
623 const char* package_name = packageName.c_str();
624 const char* data_app_name = dataAppName.c_str();
Jeff Sharkey0274c972016-12-06 09:32:04 -0700625
Jeff Sharkey423e7462016-12-09 18:18:43 -0700626 binder::Status res = ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700627 std::vector<userid_t> users = get_known_users(from_uuid);
628
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700629 // Copy app
630 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600631 auto from = create_data_app_package_path(from_uuid, data_app_name);
632 auto to = create_data_app_package_path(to_uuid, data_app_name);
633 auto to_parent = create_data_app_path(to_uuid);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700634
635 char *argv[] = {
636 (char*) kCpPath,
637 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
638 (char*) "-p", /* preserve timestamps, ownership, and permissions */
639 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
640 (char*) "-P", /* Do not follow symlinks [default] */
641 (char*) "-d", /* don't dereference symlinks */
642 (char*) from.c_str(),
643 (char*) to_parent.c_str()
644 };
645
646 LOG(DEBUG) << "Copying " << from << " to " << to;
647 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700648 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700649 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700650 goto fail;
651 }
652
653 if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700654 res = error("Failed to restorecon " + to);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700655 goto fail;
656 }
657 }
658
659 // Copy private data for all known users
Jeff Sharkeye3637242015-04-08 20:56:42 -0700660 for (auto user : users) {
Jeff Sharkeye3637242015-04-08 20:56:42 -0700661
662 // Data source may not exist for all users; that's okay
Jeff Sharkey51c94492016-05-10 17:46:39 -0600663 auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name);
664 if (access(from_ce.c_str(), F_OK) != 0) {
665 LOG(INFO) << "Missing source " << from_ce;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700666 continue;
667 }
668
Jeff Sharkey0274c972016-12-06 09:32:04 -0700669 if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId,
Jeff Sharkey36a900a2016-12-19 16:39:18 -0700670 seInfo, targetSdkVersion, nullptr).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700671 res = error("Failed to create package target");
Jeff Sharkeye3637242015-04-08 20:56:42 -0700672 goto fail;
673 }
674
675 char *argv[] = {
676 (char*) kCpPath,
677 (char*) "-F", /* delete any existing destination file first (--remove-destination) */
678 (char*) "-p", /* preserve timestamps, ownership, and permissions */
679 (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */
680 (char*) "-P", /* Do not follow symlinks [default] */
681 (char*) "-d", /* don't dereference symlinks */
Jeff Sharkey51c94492016-05-10 17:46:39 -0600682 nullptr,
683 nullptr
Jeff Sharkeye3637242015-04-08 20:56:42 -0700684 };
685
Jeff Sharkey51c94492016-05-10 17:46:39 -0600686 {
687 auto from = create_data_user_de_package_path(from_uuid, user, package_name);
688 auto to = create_data_user_de_path(to_uuid, user);
689 argv[6] = (char*) from.c_str();
690 argv[7] = (char*) to.c_str();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700691
Jeff Sharkey51c94492016-05-10 17:46:39 -0600692 LOG(DEBUG) << "Copying " << from << " to " << to;
693 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
694 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700695 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600696 goto fail;
697 }
698 }
699 {
700 auto from = create_data_user_ce_package_path(from_uuid, user, package_name);
701 auto to = create_data_user_ce_path(to_uuid, user);
702 argv[6] = (char*) from.c_str();
703 argv[7] = (char*) to.c_str();
704
705 LOG(DEBUG) << "Copying " << from << " to " << to;
706 int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true);
707 if (rc != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700708 res = error(rc, "Failed copying " + from + " to " + to);
Jeff Sharkey51c94492016-05-10 17:46:39 -0600709 goto fail;
710 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700711 }
712
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700713 if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE,
714 appId, seInfo).isOk()) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700715 res = error("Failed to restorecon");
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -0700716 goto fail;
717 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700718 }
719
Jeff Sharkey31f08982015-07-07 13:31:37 -0700720 // We let the framework scan the new location and persist that before
721 // deleting the data in the old location; this ordering ensures that
722 // we can recover from things like battery pulls.
Jeff Sharkey423e7462016-12-09 18:18:43 -0700723 return ok();
Jeff Sharkeye3637242015-04-08 20:56:42 -0700724
725fail:
726 // Nuke everything we might have already copied
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700727 {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600728 auto to = create_data_app_package_path(to_uuid, data_app_name);
Jeff Sharkeyd7921182015-04-30 15:58:19 -0700729 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
730 LOG(WARNING) << "Failed to rollback " << to;
731 }
732 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700733 for (auto user : users) {
Jeff Sharkey51c94492016-05-10 17:46:39 -0600734 {
735 auto to = create_data_user_de_package_path(to_uuid, user, package_name);
736 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
737 LOG(WARNING) << "Failed to rollback " << to;
738 }
739 }
740 {
741 auto to = create_data_user_ce_package_path(to_uuid, user, package_name);
742 if (delete_dir_contents(to.c_str(), 1, NULL) != 0) {
743 LOG(WARNING) << "Failed to rollback " << to;
744 }
Jeff Sharkeye3637242015-04-08 20:56:42 -0700745 }
746 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700747 return res;
Jeff Sharkeye3637242015-04-08 20:56:42 -0700748}
749
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700750binder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid,
751 int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) {
752 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700753 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700754 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700755
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700756 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600757 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700758 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700759 if (ensure_config_user_dirs(userId) != 0) {
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700760 return error(StringPrintf("Failed to ensure dirs for %d", userId));
Jeff Sharkey423e7462016-12-09 18:18:43 -0700761 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700762 }
763 }
Jeff Sharkey7be5ead2016-12-12 13:18:46 -0700764 return ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700765}
766
767binder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid,
768 int32_t userId, int32_t flags) {
769 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700770 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700771 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700772
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700773 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey423e7462016-12-09 18:18:43 -0700774 binder::Status res = ok();
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700775 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700776 auto path = create_data_user_de_path(uuid_, userId);
777 if (delete_dir_contents_and_dir(path, true) != 0) {
778 res = error("Failed to delete " + path);
779 }
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -0700780 if (uuid_ == nullptr) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700781 path = create_data_misc_legacy_path(userId);
782 if (delete_dir_contents_and_dir(path, true) != 0) {
783 res = error("Failed to delete " + path);
784 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700785 path = create_data_user_profile_path(userId);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700786 if (delete_dir_contents_and_dir(path, true) != 0) {
787 res = error("Failed to delete " + path);
788 }
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700789 }
Robin Lee095c7632014-04-25 15:05:19 +0100790 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600791 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700792 auto path = create_data_user_ce_path(uuid_, userId);
793 if (delete_dir_contents_and_dir(path, true) != 0) {
794 res = error("Failed to delete " + path);
795 }
796 path = create_data_media_path(uuid_, userId);
797 if (delete_dir_contents_and_dir(path, true) != 0) {
798 res = error("Failed to delete " + path);
799 }
Jeff Sharkey379a12b2016-04-14 20:45:06 -0600800 }
Jeff Sharkey423e7462016-12-09 18:18:43 -0700801 return res;
Robin Lee095c7632014-04-25 15:05:19 +0100802}
803
Mike Lockwood94afecf2012-10-24 10:45:23 -0700804/* Try to ensure free_size bytes of storage are available.
805 * Returns 0 on success.
806 * This is rather simple-minded because doing a full LRU would
807 * be potentially memory-intensive, and without atime it would
808 * also require that apps constantly modify file metadata even
809 * when just reading from the cache, which is pretty awful.
810 */
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700811binder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid,
812 int64_t freeStorageSize) {
813 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700814 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700815 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -0700816
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700817 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700818 cache_t* cache;
819 int64_t avail;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700820
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700821 auto data_path = create_data_path(uuid_);
Jeff Sharkey41ea4242015-04-09 11:34:03 -0700822
823 avail = data_disk_free(data_path);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700824 if (avail < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700825 return error("Failed to determine free space for " + data_path);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700826 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700827
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700828 ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail);
829 if (avail >= freeStorageSize) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700830 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700831 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700832
833 cache = start_cache_collection();
834
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700835 auto users = get_known_users(uuid_);
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600836 for (auto user : users) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700837 add_cache_files(cache, create_data_user_ce_path(uuid_, user));
838 add_cache_files(cache, create_data_user_de_path(uuid_, user));
Jeff Sharkey54e292e2016-05-10 17:21:13 -0600839 add_cache_files(cache,
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700840 StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str()));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700841 }
842
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700843 clear_cache_files(data_path, cache, freeStorageSize);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700844 finish_cache_collection(cache);
845
Jeff Sharkey423e7462016-12-09 18:18:43 -0700846 avail = data_disk_free(data_path);
847 if (avail >= freeStorageSize) {
848 return ok();
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700849 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700850 return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64,
851 freeStorageSize, data_path.c_str(), avail));
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700852 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700853}
854
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700855binder::Status InstalldNativeService::rmdex(const std::string& codePath,
856 const std::string& instructionSet) {
857 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -0700858 std::lock_guard<std::recursive_mutex> lock(mLock);
859
Mike Lockwood94afecf2012-10-24 10:45:23 -0700860 char dex_path[PKG_PATH_MAX];
861
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700862 const char* path = codePath.c_str();
863 const char* instruction_set = instructionSet.c_str();
864
Jeff Sharkey770180a2014-09-08 17:14:26 -0700865 if (validate_apk_path(path) && validate_system_app_path(path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700866 return error("Invalid path " + codePath);
Jeff Sharkey770180a2014-09-08 17:14:26 -0700867 }
868
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700869 if (!create_cache_path(dex_path, path, instruction_set)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700870 return error("Failed to create cache path for " + codePath);
Jeff Sharkey475c6f92016-12-07 10:37:27 -0700871 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700872
873 ALOGV("unlink %s\n", dex_path);
874 if (unlink(dex_path) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700875 return error(StringPrintf("Failed to unlink %s", dex_path));
Mike Lockwood94afecf2012-10-24 10:45:23 -0700876 } else {
Jeff Sharkey423e7462016-12-09 18:18:43 -0700877 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -0700878 }
879}
880
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700881struct stats {
882 int64_t codeSize;
883 int64_t dataSize;
884 int64_t cacheSize;
885};
886
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700887#if MEASURE_DEBUG
888static std::string toString(std::vector<int64_t> values) {
889 std::stringstream res;
890 res << "[";
891 for (size_t i = 0; i < values.size(); i++) {
892 res << values[i];
893 if (i < values.size() - 1) {
894 res << ",";
895 }
896 }
897 res << "]";
898 return res.str();
899}
900#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700901
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700902static void collectQuotaStats(const std::string& device, int32_t userId,
903 int32_t appId, struct stats* stats, struct stats* extStats ATTRIBUTE_UNUSED) {
904 if (device.empty()) return;
905
906 struct dqblk dq;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700907
908 uid_t uid = multiuser_get_uid(userId, appId);
909 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
910 reinterpret_cast<char*>(&dq)) != 0) {
911 if (errno != ESRCH) {
912 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
913 }
914 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700915#if MEASURE_DEBUG
916 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
917#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700918 stats->dataSize += dq.dqb_curspace;
919 }
920
921 int cacheGid = multiuser_get_cache_gid(userId, appId);
922 if (cacheGid != -1) {
923 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), cacheGid,
924 reinterpret_cast<char*>(&dq)) != 0) {
925 if (errno != ESRCH) {
926 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << cacheGid;
927 }
928 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700929#if MEASURE_DEBUG
930 LOG(DEBUG) << "quotactl() for GID " << cacheGid << " " << dq.dqb_curspace;
931#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700932 stats->cacheSize += dq.dqb_curspace;
933 }
934 }
935
936 int sharedGid = multiuser_get_shared_app_gid(uid);
937 if (sharedGid != -1) {
938 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), sharedGid,
939 reinterpret_cast<char*>(&dq)) != 0) {
940 if (errno != ESRCH) {
941 PLOG(ERROR) << "Failed to quotactl " << device << " for GID " << sharedGid;
942 }
943 } else {
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700944#if MEASURE_DEBUG
945 LOG(DEBUG) << "quotactl() for GID " << sharedGid << " " << dq.dqb_curspace;
946#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700947 stats->codeSize += dq.dqb_curspace;
948 }
949 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700950
951#if MEASURE_EXTERNAL
952 // TODO: measure using external GIDs
953#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700954}
955
Jeff Sharkeydf2d7542017-01-07 09:19:35 -0700956static void collectManualStats(const std::string& path, struct stats* stats) {
Mike Lockwood94afecf2012-10-24 10:45:23 -0700957 DIR *d;
958 int dfd;
959 struct dirent *de;
960 struct stat s;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700961
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600962 d = opendir(path.c_str());
963 if (d == nullptr) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700964 if (errno != ENOENT) {
965 PLOG(WARNING) << "Failed to open " << path;
966 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600967 return;
968 }
969 dfd = dirfd(d);
970 while ((de = readdir(d))) {
971 const char *name = de->d_name;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700972
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700973 int64_t size = 0;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600974 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700975 size = s.st_blocks * 512;
Jeff Sharkey2f720f72016-04-10 20:51:40 -0600976 }
977
978 if (de->d_type == DT_DIR) {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700979 if (!strcmp(name, ".")) {
980 // Don't recurse, but still count node size
981 } else if (!strcmp(name, "..")) {
982 // Don't recurse or count node size
983 continue;
Mike Lockwood94afecf2012-10-24 10:45:23 -0700984 } else {
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700985 // Measure all children nodes
986 size = 0;
987 calculate_tree_size(StringPrintf("%s/%s", path.c_str(), name), &size);
Mike Lockwood94afecf2012-10-24 10:45:23 -0700988 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700989
990 if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) {
991 stats->cacheSize += size;
992 }
Mike Lockwood94afecf2012-10-24 10:45:23 -0700993 }
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -0700994
995 // Legacy symlink isn't owned by app
996 if (de->d_type == DT_LNK && !strcmp(name, "lib")) {
997 continue;
998 }
999
1000 // Everything found inside is considered data
1001 stats->dataSize += size;
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001002 }
1003 closedir(d);
1004}
1005
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001006static void collectManualStatsForUser(const std::string& path, struct stats* stats,
1007 bool exclude_apps = false) {
1008 DIR *d;
1009 int dfd;
1010 struct dirent *de;
1011 struct stat s;
1012
1013 d = opendir(path.c_str());
1014 if (d == nullptr) {
1015 if (errno != ENOENT) {
1016 PLOG(WARNING) << "Failed to open " << path;
1017 }
1018 return;
1019 }
1020 dfd = dirfd(d);
1021 while ((de = readdir(d))) {
1022 if (de->d_type == DT_DIR) {
1023 const char *name = de->d_name;
1024 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) != 0) {
1025 continue;
1026 }
1027 if (!strcmp(name, ".") || !strcmp(name, "..")) {
1028 continue;
1029 } else if (exclude_apps && (s.st_uid >= AID_APP_START && s.st_uid <= AID_APP_END)) {
1030 continue;
1031 } else {
1032 collectManualStats(StringPrintf("%s/%s", path.c_str(), name), stats);
1033 }
1034 }
1035 }
1036 closedir(d);
1037}
1038
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001039binder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid,
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001040 const std::vector<std::string>& packageNames, int32_t userId, int32_t flags,
1041 int32_t appId, const std::vector<int64_t>& ceDataInodes,
1042 const std::vector<std::string>& codePaths, std::vector<int64_t>* _aidl_return) {
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001043 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001044 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001045 for (auto packageName : packageNames) {
1046 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
1047 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001048 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001049
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001050 // When modifying this logic, always verify using tests:
1051 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetAppSize
1052
1053#if MEASURE_DEBUG
1054 LOG(INFO) << "Measuring user " << userId << " app " << appId;
1055#endif
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001056
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001057 // Here's a summary of the common storage locations across the platform,
1058 // and how they're each tagged:
1059 //
1060 // /data/app/com.example UID system
1061 // /data/app/com.example/oat UID system
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001062 // /data/user/0/com.example UID u0_a10 GID u0_a10
1063 // /data/user/0/com.example/cache UID u0_a10 GID u0_a10_cache
1064 // /data/media/0/foo.txt UID u0_media_rw
1065 // /data/media/0/bar.jpg UID u0_media_rw GID u0_media_image
1066 // /data/media/0/Android/data/com.example UID u0_media_rw GID u0_a10_ext
1067 // /data/media/0/Android/data/com.example/cache UID u0_media_rw GID u0_a10_ext_cache
1068 // /data/media/obb/com.example UID system
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001069
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001070 struct stats stats;
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001071 struct stats extStats;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001072 memset(&stats, 0, sizeof(stats));
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001073 memset(&extStats, 0, sizeof(extStats));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001074
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001075 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001076
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001077 auto device = findQuotaDeviceForUuid(uuid);
1078 if (device.empty()) {
1079 flags &= ~FLAG_USE_QUOTA;
1080 }
1081
Jeff Sharkey466459b2017-01-17 15:25:01 -07001082 ATRACE_BEGIN("obb");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001083 for (auto packageName : packageNames) {
1084 auto obbCodePath = create_data_media_obb_path(uuid_, packageName.c_str());
1085 calculate_tree_size(obbCodePath, &extStats.codeSize);
1086 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001087 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001088
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001089 if (flags & FLAG_USE_QUOTA && appId >= AID_APP_START) {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001090 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001091 for (auto codePath : codePaths) {
1092 calculate_tree_size(codePath, &stats.codeSize, -1,
1093 multiuser_get_shared_gid(userId, appId));
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001094 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001095 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001096
Jeff Sharkey466459b2017-01-17 15:25:01 -07001097 ATRACE_BEGIN("quota");
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001098 collectQuotaStats(device, userId, appId, &stats, &extStats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001099 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001100
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001101 } else {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001102 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001103 for (auto codePath : codePaths) {
1104 calculate_tree_size(codePath, &stats.codeSize);
1105 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001106 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001107
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001108 for (size_t i = 0; i < packageNames.size(); i++) {
1109 const char* pkgname = packageNames[i].c_str();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001110
Jeff Sharkey466459b2017-01-17 15:25:01 -07001111 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001112 auto cePath = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInodes[i]);
1113 collectManualStats(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001114 auto dePath = create_data_user_de_package_path(uuid_, userId, pkgname);
1115 collectManualStats(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001116 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001117
Jeff Sharkey466459b2017-01-17 15:25:01 -07001118 ATRACE_BEGIN("profiles");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001119 auto userProfilePath = create_data_user_profile_package_path(userId, pkgname);
1120 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001121 auto refProfilePath = create_data_ref_profile_package_path(pkgname);
1122 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001123 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001124
1125#if MEASURE_EXTERNAL
Jeff Sharkey466459b2017-01-17 15:25:01 -07001126 ATRACE_BEGIN("external");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001127 auto extPath = create_data_media_package_path(uuid_, userId, pkgname, "data");
1128 collectManualStats(extPath, &extStats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001129 auto mediaPath = create_data_media_package_path(uuid_, userId, pkgname, "media");
1130 calculate_tree_size(mediaPath, &extStats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001131 ATRACE_END();
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001132#endif
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001133 }
1134
Jeff Sharkey466459b2017-01-17 15:25:01 -07001135 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001136 int32_t sharedGid = multiuser_get_shared_gid(userId, appId);
1137 if (sharedGid != -1) {
1138 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1139 sharedGid, -1);
1140 }
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001141 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize,
1142 multiuser_get_uid(userId, appId), -1);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001143 ATRACE_END();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001144 }
Jeff Sharkey2f720f72016-04-10 20:51:40 -06001145
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001146 std::vector<int64_t> ret;
1147 ret.push_back(stats.codeSize);
1148 ret.push_back(stats.dataSize);
1149 ret.push_back(stats.cacheSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001150 ret.push_back(extStats.codeSize);
1151 ret.push_back(extStats.dataSize);
1152 ret.push_back(extStats.cacheSize);
1153#if MEASURE_DEBUG
1154 LOG(DEBUG) << "Final result " << toString(ret);
1155#endif
1156 *_aidl_return = ret;
1157 return ok();
1158}
1159
1160binder::Status InstalldNativeService::getUserSize(const std::unique_ptr<std::string>& uuid,
1161 int32_t userId, int32_t flags, const std::vector<int32_t>& appIds,
1162 std::vector<int64_t>* _aidl_return) {
1163 ENFORCE_UID(AID_SYSTEM);
1164 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001165 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001166
1167 // When modifying this logic, always verify using tests:
1168 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetUserSize
1169
1170#if MEASURE_DEBUG
1171 LOG(INFO) << "Measuring user " << userId;
1172#endif
1173
1174 struct stats stats;
1175 struct stats extStats;
1176 memset(&stats, 0, sizeof(stats));
1177 memset(&extStats, 0, sizeof(extStats));
1178
1179 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1180
Jeff Sharkey466459b2017-01-17 15:25:01 -07001181 ATRACE_BEGIN("obb");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001182 auto obbPath = create_data_path(uuid_) + "/media/obb";
1183 calculate_tree_size(obbPath, &extStats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001184 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001185
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001186 auto device = findQuotaDeviceForUuid(uuid);
1187 if (device.empty()) {
1188 flags &= ~FLAG_USE_QUOTA;
1189 }
1190
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001191 if (flags & FLAG_USE_QUOTA) {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001192 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001193 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001194 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001195
Jeff Sharkey466459b2017-01-17 15:25:01 -07001196 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001197 auto cePath = create_data_user_ce_path(uuid_, userId);
1198 collectManualStatsForUser(cePath, &stats, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001199 auto dePath = create_data_user_de_path(uuid_, userId);
1200 collectManualStatsForUser(dePath, &stats, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001201 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001202
Jeff Sharkey466459b2017-01-17 15:25:01 -07001203 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001204 auto userProfilePath = create_data_user_profile_path(userId);
1205 calculate_tree_size(userProfilePath, &stats.dataSize, -1, -1, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001206 auto refProfilePath = create_data_ref_profile_path();
1207 calculate_tree_size(refProfilePath, &stats.codeSize, -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001208 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001209
1210#if MEASURE_EXTERNAL
Jeff Sharkey466459b2017-01-17 15:25:01 -07001211 ATRACE_BEGIN("external");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001212 // TODO: measure external storage paths
Jeff Sharkey466459b2017-01-17 15:25:01 -07001213 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001214#endif
1215
Jeff Sharkey466459b2017-01-17 15:25:01 -07001216 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001217 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize,
1218 -1, -1, true);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001219 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize,
1220 -1, -1, true);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001221 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001222
Jeff Sharkey466459b2017-01-17 15:25:01 -07001223 ATRACE_BEGIN("quota");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001224 for (auto appId : appIds) {
1225 if (appId >= AID_APP_START) {
1226 collectQuotaStats(device, userId, appId, &stats, &extStats);
1227#if MEASURE_DEBUG
1228 // Sleep to make sure we don't lose logs
1229 usleep(1);
1230#endif
1231 }
1232 }
Jeff Sharkey466459b2017-01-17 15:25:01 -07001233 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001234 } else {
Jeff Sharkey466459b2017-01-17 15:25:01 -07001235 ATRACE_BEGIN("code");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001236 calculate_tree_size(create_data_app_path(uuid_), &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001237 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001238
Jeff Sharkey466459b2017-01-17 15:25:01 -07001239 ATRACE_BEGIN("data");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001240 auto cePath = create_data_user_ce_path(uuid_, userId);
1241 collectManualStatsForUser(cePath, &stats);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001242 auto dePath = create_data_user_de_path(uuid_, userId);
1243 collectManualStatsForUser(dePath, &stats);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001244 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001245
Jeff Sharkey466459b2017-01-17 15:25:01 -07001246 ATRACE_BEGIN("profile");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001247 auto userProfilePath = create_data_user_profile_path(userId);
1248 calculate_tree_size(userProfilePath, &stats.dataSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001249 auto refProfilePath = create_data_ref_profile_path();
1250 calculate_tree_size(refProfilePath, &stats.codeSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001251 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001252
1253#if MEASURE_EXTERNAL
Jeff Sharkey466459b2017-01-17 15:25:01 -07001254 ATRACE_BEGIN("external");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001255 // TODO: measure external storage paths
Jeff Sharkey466459b2017-01-17 15:25:01 -07001256 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001257#endif
1258
Jeff Sharkey466459b2017-01-17 15:25:01 -07001259 ATRACE_BEGIN("dalvik");
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001260 calculate_tree_size(create_data_dalvik_cache_path(), &stats.codeSize);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001261 calculate_tree_size(create_data_misc_foreign_dex_path(userId), &stats.dataSize);
Jeff Sharkey466459b2017-01-17 15:25:01 -07001262 ATRACE_END();
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001263 }
1264
1265 std::vector<int64_t> ret;
1266 ret.push_back(stats.codeSize);
1267 ret.push_back(stats.dataSize);
1268 ret.push_back(stats.cacheSize);
1269 ret.push_back(extStats.codeSize);
1270 ret.push_back(extStats.dataSize);
1271 ret.push_back(extStats.cacheSize);
1272#if MEASURE_DEBUG
1273 LOG(DEBUG) << "Final result " << toString(ret);
1274#endif
1275 *_aidl_return = ret;
1276 return ok();
1277}
1278
1279binder::Status InstalldNativeService::getExternalSize(const std::unique_ptr<std::string>& uuid,
1280 int32_t userId, int32_t flags, std::vector<int64_t>* _aidl_return) {
1281 ENFORCE_UID(AID_SYSTEM);
1282 CHECK_ARGUMENT_UUID(uuid);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001283 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001284
1285 // When modifying this logic, always verify using tests:
1286 // runtest -x frameworks/base/services/tests/servicestests/src/com/android/server/pm/InstallerTest.java -m testGetExternalSize
1287
1288#if MEASURE_DEBUG
1289 LOG(INFO) << "Measuring external " << userId;
1290#endif
1291
1292 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1293
1294 int64_t totalSize = 0;
1295 int64_t audioSize = 0;
1296 int64_t videoSize = 0;
1297 int64_t imageSize = 0;
1298
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001299 auto device = findQuotaDeviceForUuid(uuid);
1300 if (device.empty()) {
1301 flags &= ~FLAG_USE_QUOTA;
1302 }
1303
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001304 if (flags & FLAG_USE_QUOTA) {
1305 struct dqblk dq;
1306
Jeff Sharkeydf2d7542017-01-07 09:19:35 -07001307 uid_t uid = multiuser_get_uid(userId, AID_MEDIA_RW);
1308 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), device.c_str(), uid,
1309 reinterpret_cast<char*>(&dq)) != 0) {
1310 if (errno != ESRCH) {
1311 PLOG(ERROR) << "Failed to quotactl " << device << " for UID " << uid;
1312 }
1313 } else {
1314#if MEASURE_DEBUG
1315 LOG(DEBUG) << "quotactl() for UID " << uid << " " << dq.dqb_curspace;
1316#endif
1317 totalSize = dq.dqb_curspace;
1318 }
1319
1320 gid_t audioGid = multiuser_get_uid(userId, AID_MEDIA_AUDIO);
1321 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), audioGid,
1322 reinterpret_cast<char*>(&dq)) == 0) {
1323#if MEASURE_DEBUG
1324 LOG(DEBUG) << "quotactl() for GID " << audioGid << " " << dq.dqb_curspace;
1325#endif
1326 audioSize = dq.dqb_curspace;
1327 }
1328 gid_t videoGid = multiuser_get_uid(userId, AID_MEDIA_VIDEO);
1329 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), videoGid,
1330 reinterpret_cast<char*>(&dq)) == 0) {
1331#if MEASURE_DEBUG
1332 LOG(DEBUG) << "quotactl() for GID " << videoGid << " " << dq.dqb_curspace;
1333#endif
1334 videoSize = dq.dqb_curspace;
1335 }
1336 gid_t imageGid = multiuser_get_uid(userId, AID_MEDIA_IMAGE);
1337 if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), device.c_str(), imageGid,
1338 reinterpret_cast<char*>(&dq)) == 0) {
1339#if MEASURE_DEBUG
1340 LOG(DEBUG) << "quotactl() for GID " << imageGid << " " << dq.dqb_curspace;
1341#endif
1342 imageSize = dq.dqb_curspace;
1343 }
1344 } else {
1345 FTS *fts;
1346 FTSENT *p;
1347 auto path = create_data_media_path(uuid_, userId);
1348 char *argv[] = { (char*) path.c_str(), nullptr };
1349 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) {
1350 return error("Failed to fts_open " + path);
1351 }
1352 while ((p = fts_read(fts)) != NULL) {
1353 char* ext;
1354 int64_t size = (p->fts_statp->st_blocks * 512);
1355 switch (p->fts_info) {
1356 case FTS_F:
1357 // Only categorize files not belonging to apps
1358 if (p->fts_statp->st_gid < AID_APP_START) {
1359 ext = strrchr(p->fts_name, '.');
1360 if (ext != nullptr) {
1361 switch (MatchExtension(++ext)) {
1362 case AID_MEDIA_AUDIO: audioSize += size; break;
1363 case AID_MEDIA_VIDEO: videoSize += size; break;
1364 case AID_MEDIA_IMAGE: imageSize += size; break;
1365 }
1366 }
1367 }
1368 // Fall through to always count against total
1369 case FTS_D:
1370 case FTS_DEFAULT:
1371 case FTS_SL:
1372 case FTS_SLNONE:
1373 totalSize += size;
1374 break;
1375 }
1376 }
1377 fts_close(fts);
1378 }
1379
1380 std::vector<int64_t> ret;
1381 ret.push_back(totalSize);
1382 ret.push_back(audioSize);
1383 ret.push_back(videoSize);
1384 ret.push_back(imageSize);
1385#if MEASURE_DEBUG
1386 LOG(DEBUG) << "Final result " << toString(ret);
1387#endif
Jeff Sharkey3dfae0c2016-12-12 17:32:56 -07001388 *_aidl_return = ret;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001389 return ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001390}
1391
David Sehr6727c2d2016-05-17 16:06:22 -07001392// Dumps the contents of a profile file, using pkgname's dex files for pretty
1393// printing the result.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001394binder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName,
1395 const std::string& codePaths, bool* _aidl_return) {
1396 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001397 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001398 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001399
1400 const char* pkgname = packageName.c_str();
Jeff Sharkey90aff262016-12-12 14:28:24 -07001401 const char* code_paths = codePaths.c_str();
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001402
Jeff Sharkey90aff262016-12-12 14:28:24 -07001403 *_aidl_return = dump_profiles(uid, pkgname, code_paths);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001404 return ok();
David Sehr6727c2d2016-05-17 16:06:22 -07001405}
1406
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001407// TODO: Consider returning error codes.
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001408binder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName,
Jeff Sharkey423e7462016-12-09 18:18:43 -07001409 bool* _aidl_return) {
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001410 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001411 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001412 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001413
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001414 const char* pkgname = packageName.c_str();
1415 *_aidl_return = analyse_profiles(uid, pkgname);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001416 return ok();
Andreas Gampe4d0f8252016-03-20 11:30:28 -07001417}
1418
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001419binder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid,
1420 const std::unique_ptr<std::string>& packageName, const std::string& instructionSet,
1421 int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags,
1422 const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid,
1423 const std::unique_ptr<std::string>& sharedLibraries) {
1424 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001425 CHECK_ARGUMENT_UUID(uuid);
1426 if (packageName && *packageName != "*") {
1427 CHECK_ARGUMENT_PACKAGE_NAME(*packageName);
1428 }
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001429 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001430
1431 const char* apk_path = apkPath.c_str();
1432 const char* pkgname = packageName ? packageName->c_str() : "*";
1433 const char* instruction_set = instructionSet.c_str();
1434 const char* oat_dir = outputPath ? outputPath->c_str() : nullptr;
1435 const char* compiler_filter = compilerFilter.c_str();
1436 const char* volume_uuid = uuid ? uuid->c_str() : nullptr;
1437 const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr;
1438
1439 int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded,
1440 oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001441 return res ? error(res, "Failed to dexopt") : ok();
Jeff Sharkey6c2c0562016-12-07 12:12:00 -07001442}
1443
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001444binder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) {
1445 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001446 std::lock_guard<std::recursive_mutex> lock(mLock);
1447
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001448 const char* instruction_set = instructionSet.c_str();
1449
1450 char boot_marker_path[PKG_PATH_MAX];
1451 sprintf(boot_marker_path,
Andreas Gampe02d0de52015-11-11 20:43:16 -08001452 "%s/%s/%s/.booting",
1453 android_data_dir.path,
1454 DALVIK_CACHE,
1455 instruction_set);
Narayan Kamath091ea772014-11-10 15:03:46 +00001456
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001457 ALOGV("mark_boot_complete : %s", boot_marker_path);
1458 if (unlink(boot_marker_path) != 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001459 return error(StringPrintf("Failed to unlink %s", boot_marker_path));
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001460 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001461 return ok();
Narayan Kamath091ea772014-11-10 15:03:46 +00001462}
1463
Mike Lockwood94afecf2012-10-24 10:45:23 -07001464void mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid,
1465 struct stat* statbuf)
1466{
1467 while (path[basepos] != 0) {
1468 if (path[basepos] == '/') {
1469 path[basepos] = 0;
1470 if (lstat(path, statbuf) < 0) {
1471 ALOGV("Making directory: %s\n", path);
1472 if (mkdir(path, mode) == 0) {
1473 chown(path, uid, gid);
1474 } else {
1475 ALOGW("Unable to make directory %s: %s\n", path, strerror(errno));
1476 }
1477 }
1478 path[basepos] = '/';
1479 basepos++;
1480 }
1481 basepos++;
1482 }
1483}
1484
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001485binder::Status InstalldNativeService::linkNativeLibraryDirectory(
1486 const std::unique_ptr<std::string>& uuid, const std::string& packageName,
1487 const std::string& nativeLibPath32, int32_t userId) {
1488 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001489 CHECK_ARGUMENT_UUID(uuid);
1490 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001491 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001492
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001493 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1494 const char* pkgname = packageName.c_str();
1495 const char* asecLibDir = nativeLibPath32.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001496 struct stat s, libStat;
Jeff Sharkey423e7462016-12-09 18:18:43 -07001497 binder::Status res = ok();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001498
Jeff Sharkey423e7462016-12-09 18:18:43 -07001499 auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname);
1500 auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX;
Jeff Sharkeyc03de092015-04-07 18:14:05 -07001501
1502 const char* pkgdir = _pkgdir.c_str();
1503 const char* libsymlink = _libsymlink.c_str();
Mike Lockwood94afecf2012-10-24 10:45:23 -07001504
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001505 if (stat(pkgdir, &s) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001506 return error("Failed to stat " + _pkgdir);
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001507 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001508
1509 if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001510 return error("Failed to chown " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001511 }
1512
1513 if (chmod(pkgdir, 0700) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001514 res = error("Failed to chmod " + _pkgdir);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001515 goto out;
1516 }
1517
1518 if (lstat(libsymlink, &libStat) < 0) {
1519 if (errno != ENOENT) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001520 res = error("Failed to stat " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001521 goto out;
1522 }
1523 } else {
1524 if (S_ISDIR(libStat.st_mode)) {
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001525 if (delete_dir_contents(libsymlink, 1, NULL) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001526 res = error("Failed to delete " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001527 goto out;
1528 }
1529 } else if (S_ISLNK(libStat.st_mode)) {
1530 if (unlink(libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001531 res = error("Failed to unlink " + _libsymlink);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001532 goto out;
1533 }
1534 }
1535 }
1536
1537 if (symlink(asecLibDir, libsymlink) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001538 res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32);
Mike Lockwood94afecf2012-10-24 10:45:23 -07001539 goto out;
1540 }
1541
1542out:
1543 if (chmod(pkgdir, s.st_mode) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001544 auto msg = "Failed to cleanup chmod " + _pkgdir;
1545 if (res.isOk()) {
1546 res = error(msg);
1547 } else {
1548 PLOG(ERROR) << msg;
1549 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001550 }
1551
1552 if (chown(pkgdir, s.st_uid, s.st_gid) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001553 auto msg = "Failed to cleanup chown " + _pkgdir;
1554 if (res.isOk()) {
1555 res = error(msg);
1556 } else {
1557 PLOG(ERROR) << msg;
1558 }
Mike Lockwood94afecf2012-10-24 10:45:23 -07001559 }
1560
Jeff Sharkey423e7462016-12-09 18:18:43 -07001561 return res;
Mike Lockwood94afecf2012-10-24 10:45:23 -07001562}
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001563
1564static void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd)
1565{
1566 static const char *IDMAP_BIN = "/system/bin/idmap";
1567 static const size_t MAX_INT_LEN = 32;
1568 char idmap_str[MAX_INT_LEN];
1569
1570 snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd);
1571
1572 execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL);
1573 ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno));
1574}
1575
1576// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix)
1577// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap
1578static int flatten_path(const char *prefix, const char *suffix,
1579 const char *overlay_path, char *idmap_path, size_t N)
1580{
1581 if (overlay_path == NULL || idmap_path == NULL) {
1582 return -1;
1583 }
1584 const size_t len_overlay_path = strlen(overlay_path);
1585 // will access overlay_path + 1 further below; requires absolute path
1586 if (len_overlay_path < 2 || *overlay_path != '/') {
1587 return -1;
1588 }
1589 const size_t len_idmap_root = strlen(prefix);
1590 const size_t len_suffix = strlen(suffix);
1591 if (SIZE_MAX - len_idmap_root < len_overlay_path ||
1592 SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) {
1593 // additions below would cause overflow
1594 return -1;
1595 }
1596 if (N < len_idmap_root + len_overlay_path + len_suffix) {
1597 return -1;
1598 }
1599 memset(idmap_path, 0, N);
1600 snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix);
1601 char *ch = idmap_path + len_idmap_root;
1602 while (*ch != '\0') {
1603 if (*ch == '/') {
1604 *ch = '@';
1605 }
1606 ++ch;
1607 }
1608 return 0;
1609}
1610
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001611binder::Status InstalldNativeService::idmap(const std::string& targetApkPath,
1612 const std::string& overlayApkPath, int32_t uid) {
1613 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001614 std::lock_guard<std::recursive_mutex> lock(mLock);
1615
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001616 const char* target_apk = targetApkPath.c_str();
1617 const char* overlay_apk = overlayApkPath.c_str();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001618 ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid);
1619
1620 int idmap_fd = -1;
1621 char idmap_path[PATH_MAX];
1622
1623 if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk,
1624 idmap_path, sizeof(idmap_path)) == -1) {
1625 ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk);
1626 goto fail;
1627 }
1628
1629 unlink(idmap_path);
1630 idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644);
1631 if (idmap_fd < 0) {
1632 ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno));
1633 goto fail;
1634 }
1635 if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) {
1636 ALOGE("idmap cannot chown '%s'\n", idmap_path);
1637 goto fail;
1638 }
1639 if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) {
1640 ALOGE("idmap cannot chmod '%s'\n", idmap_path);
1641 goto fail;
1642 }
1643
1644 pid_t pid;
1645 pid = fork();
1646 if (pid == 0) {
1647 /* child -- drop privileges before continuing */
1648 if (setgid(uid) != 0) {
1649 ALOGE("setgid(%d) failed during idmap\n", uid);
1650 exit(1);
1651 }
1652 if (setuid(uid) != 0) {
1653 ALOGE("setuid(%d) failed during idmap\n", uid);
1654 exit(1);
1655 }
1656 if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) {
1657 ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno));
1658 exit(1);
1659 }
1660
1661 run_idmap(target_apk, overlay_apk, idmap_fd);
1662 exit(1); /* only if exec call to idmap failed */
1663 } else {
1664 int status = wait_child(pid);
1665 if (status != 0) {
1666 ALOGE("idmap failed, status=0x%04x\n", status);
1667 goto fail;
1668 }
1669 }
1670
1671 close(idmap_fd);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001672 return ok();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001673fail:
1674 if (idmap_fd >= 0) {
1675 close(idmap_fd);
1676 unlink(idmap_path);
1677 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001678 return error();
MÃ¥rten Kongstad63568b12014-01-31 14:42:59 +01001679}
Robert Craige9887e42014-02-20 10:25:56 -05001680
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001681binder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid,
1682 const std::string& packageName, int32_t userId, int32_t flags, int32_t appId,
1683 const std::string& seInfo) {
1684 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001685 CHECK_ARGUMENT_UUID(uuid);
1686 CHECK_ARGUMENT_PACKAGE_NAME(packageName);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001687 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001688
1689 binder::Status res = ok();
Robert Craige9887e42014-02-20 10:25:56 -05001690
Robert Craigda30dc72014-03-27 10:21:12 -04001691 // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here.
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001692 unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE;
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001693 const char* uuid_ = uuid ? uuid->c_str() : nullptr;
1694 const char* pkgName = packageName.c_str();
1695 const char* seinfo = seInfo.c_str();
Robert Craigda30dc72014-03-27 10:21:12 -04001696
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001697 uid_t uid = multiuser_get_uid(userId, appId);
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001698 if (flags & FLAG_STORAGE_CE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001699 auto path = create_data_user_ce_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001700 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001701 res = error("restorecon failed for " + path);
Jeff Sharkeyebf728f2015-11-18 14:15:17 -07001702 }
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001703 }
Jeff Sharkeyaa7ddfd2016-02-03 14:03:16 -07001704 if (flags & FLAG_STORAGE_DE) {
Jeff Sharkeyc1e93e72016-12-05 23:39:32 -07001705 auto path = create_data_user_de_package_path(uuid_, userId, pkgName);
Jeff Sharkeyc7d1b222016-01-11 13:07:09 -07001706 if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001707 res = error("restorecon failed for " + path);
Jeff Sharkey41ea4242015-04-09 11:34:03 -07001708 }
Robert Craige9887e42014-02-20 10:25:56 -05001709 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001710 return res;
Robert Craige9887e42014-02-20 10:25:56 -05001711}
Narayan Kamath3aee2c52014-06-10 13:16:47 +01001712
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001713binder::Status InstalldNativeService::createOatDir(const std::string& oatDir,
1714 const std::string& instructionSet) {
1715 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001716 std::lock_guard<std::recursive_mutex> lock(mLock);
1717
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001718 const char* oat_dir = oatDir.c_str();
1719 const char* instruction_set = instructionSet.c_str();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001720 char oat_instr_dir[PKG_PATH_MAX];
1721
1722 if (validate_apk_path(oat_dir)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001723 return error("Invalid path " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001724 }
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001725 if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001726 return error("Failed to prepare " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001727 }
1728 if (selinux_android_restorecon(oat_dir, 0)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001729 return error("Failed to restorecon " + oatDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001730 }
1731 snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set);
Fyodor Kupolov8eed7e62015-04-06 19:09:02 -07001732 if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001733 return error(StringPrintf("Failed to prepare %s", oat_instr_dir));
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001734 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001735 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001736}
1737
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001738binder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) {
1739 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001740 std::lock_guard<std::recursive_mutex> lock(mLock);
1741
Jeff Sharkey423e7462016-12-09 18:18:43 -07001742 if (validate_apk_path(packageDir.c_str())) {
1743 return error("Invalid path " + packageDir);
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001744 }
Jeff Sharkey423e7462016-12-09 18:18:43 -07001745 if (delete_dir_contents_and_dir(packageDir) != 0) {
1746 return error("Failed to delete " + packageDir);
1747 }
1748 return ok();
Fyodor Kupolov88ce4ff2015-03-03 12:25:29 -08001749}
1750
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001751binder::Status InstalldNativeService::linkFile(const std::string& relativePath,
1752 const std::string& fromBase, const std::string& toBase) {
1753 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001754 std::lock_guard<std::recursive_mutex> lock(mLock);
1755
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001756 const char* relative_path = relativePath.c_str();
1757 const char* from_base = fromBase.c_str();
1758 const char* to_base = toBase.c_str();
Narayan Kamathd845c962015-06-04 13:20:27 +01001759 char from_path[PKG_PATH_MAX];
1760 char to_path[PKG_PATH_MAX];
1761 snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path);
1762 snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path);
1763
1764 if (validate_apk_path_subdirs(from_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001765 return error(StringPrintf("Invalid from path %s", from_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001766 }
1767
1768 if (validate_apk_path_subdirs(to_path)) {
Jeff Sharkey423e7462016-12-09 18:18:43 -07001769 return error(StringPrintf("Invalid to path %s", to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001770 }
1771
Jeff Sharkey423e7462016-12-09 18:18:43 -07001772 if (link(from_path, to_path) < 0) {
1773 return error(StringPrintf("Failed to link from %s to %s", from_path, to_path));
Narayan Kamathd845c962015-06-04 13:20:27 +01001774 }
1775
Jeff Sharkey423e7462016-12-09 18:18:43 -07001776 return ok();
Narayan Kamathd845c962015-06-04 13:20:27 +01001777}
1778
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001779binder::Status InstalldNativeService::moveAb(const std::string& apkPath,
1780 const std::string& instructionSet, const std::string& outputPath) {
1781 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001782 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001783
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001784 const char* apk_path = apkPath.c_str();
1785 const char* instruction_set = instructionSet.c_str();
1786 const char* oat_dir = outputPath.c_str();
Andreas Gamped089ca12016-06-27 14:25:30 -07001787
Jeff Sharkey90aff262016-12-12 14:28:24 -07001788 bool success = move_ab(apk_path, instruction_set, oat_dir);
Jeff Sharkey423e7462016-12-09 18:18:43 -07001789 return success ? ok() : error();
Andreas Gampee65b4fa2016-03-01 11:46:45 -08001790}
1791
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001792binder::Status InstalldNativeService::deleteOdex(const std::string& apkPath,
1793 const std::string& instructionSet, const std::string& outputPath) {
1794 ENFORCE_UID(AID_SYSTEM);
Jeff Sharkey7a9059e2017-01-16 19:07:18 -07001795 std::lock_guard<std::recursive_mutex> lock(mLock);
Jeff Sharkey90aff262016-12-12 14:28:24 -07001796
Jeff Sharkey475c6f92016-12-07 10:37:27 -07001797 const char* apk_path = apkPath.c_str();
1798 const char* instruction_set = instructionSet.c_str();
1799 const char* oat_dir = outputPath.c_str();
1800
Jeff Sharkey90aff262016-12-12 14:28:24 -07001801 bool res = delete_odex(apk_path, instruction_set, oat_dir);
1802 return res ? ok() : error();
Andreas Gampeb31206b2016-09-09 17:07:04 -07001803}
1804
Jeff Sharkey66b1a122017-01-16 20:57:45 -07001805binder::Status InstalldNativeService::invalidateMounts() {
1806 ENFORCE_UID(AID_SYSTEM);
1807 std::lock_guard<std::recursive_mutex> lock(mLock);
1808
1809 mQuotaDevices.clear();
1810
1811 std::ifstream in("/proc/mounts");
1812 if (!in.is_open()) {
1813 return error("Failed to read mounts");
1814 }
1815
1816 std::string source;
1817 std::string target;
1818 std::string ignored;
1819 struct dqblk dq;
1820 while (!in.eof()) {
1821 std::getline(in, source, ' ');
1822 std::getline(in, target, ' ');
1823 std::getline(in, ignored);
1824
1825 if (source.compare(0, 11, "/dev/block/") == 0) {
1826 if (quotactl(QCMD(Q_GETQUOTA, USRQUOTA), source.c_str(), 0,
1827 reinterpret_cast<char*>(&dq)) == 0) {
1828 LOG(DEBUG) << "Found " << source << " with quota";
1829 mQuotaDevices[target] = source;
1830 }
1831 }
1832 }
1833 return ok();
1834}
1835
1836std::string InstalldNativeService::findQuotaDeviceForUuid(
1837 const std::unique_ptr<std::string>& uuid) {
1838 auto path = create_data_path(uuid ? uuid->c_str() : nullptr);
1839 return mQuotaDevices[path];
1840}
1841
Andreas Gampe02d0de52015-11-11 20:43:16 -08001842} // namespace installd
1843} // namespace android