InstalldNativeService.cpp revision 90aff26f0135379db19432ae90c40c0831ba5954
194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* 294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** Copyright 2008, The Android Open Source Project 394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 4d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Licensed under the Apache License, Version 2.0 (the "License"); 5d93707342a61e66bc3eb2145628158452f577f42Dave Allison** you may not use this file except in compliance with the License. 6d93707342a61e66bc3eb2145628158452f577f42Dave Allison** You may obtain a copy of the License at 794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 8d93707342a61e66bc3eb2145628158452f577f42Dave Allison** http://www.apache.org/licenses/LICENSE-2.0 994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** 10d93707342a61e66bc3eb2145628158452f577f42Dave Allison** Unless required by applicable law or agreed to in writing, software 11d93707342a61e66bc3eb2145628158452f577f42Dave Allison** distributed under the License is distributed on an "AS IS" BASIS, 12d93707342a61e66bc3eb2145628158452f577f42Dave Allison** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d93707342a61e66bc3eb2145628158452f577f42Dave Allison** See the License for the specific language governing permissions and 1494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood** limitations under the License. 1594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood*/ 1694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 17f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "InstalldNativeService.h" 1802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe 1902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <errno.h> 2002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <inttypes.h> 21d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe#include <regex> 2202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <stdlib.h> 2302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/capability.h> 2402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/file.h> 2502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/resource.h> 2602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <sys/stat.h> 27cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/types.h> 286a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle#include <sys/wait.h> 29cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey#include <sys/xattr.h> 3002d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <unistd.h> 3141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 326fb5a0179d4de9c85351fd016277949804c809edAndreas Gampe#include <android-base/logging.h> 33e4ec9eb7b4c452493589983970ba5ccc501728d1Elliott Hughes#include <android-base/stringprintf.h> 346727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr#include <android-base/strings.h> 3564b59cc1c89225813295d1b5d6bf4183615da204Roland Levillain#include <android-base/unique_fd.h> 3602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/fs.h> 3702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <cutils/log.h> // TODO: Move everything to base/logging. 3890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey#include <cutils/properties.h> 390378aaf257aee92539d30543914a50c4481c6a18Brian Carlstrom#include <cutils/sched_policy.h> 4094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood#include <diskusage/dirsize.h> 41e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <logwrap/logwrap.h> 4202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <private/android_filesystem_config.h> 43e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey#include <selinux/android.h> 4402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#include <system/thread_defs.h> 45e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 466c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey#include "dexopt.h" 47f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "globals.h" 48f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "installd_deps.h" 49f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "otapreopt_utils.h" 50f3e30b936ef103dc0f3d8697e0f86ba82b49609eJeff Sharkey#include "utils.h" 516c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 5202d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#ifndef LOG_TAG 5302d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#define LOG_TAG "installd" 5402d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe#endif 5541ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 5641ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkeyusing android::base::StringPrintf; 5794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace android { 5902d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampenamespace installd { 6094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 61cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kCpPath = "/system/bin/cp"; 62cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkeystatic constexpr const char* kXattrDefault = "user.default"; 63e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 64d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* PKG_LIB_POSTFIX = "/lib"; 65d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CACHE_DIR_POSTFIX = "/cache"; 66d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* CODE_CACHE_DIR_POSTFIX = "/code_cache"; 67d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 68d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_PREFIX = "/data/resource-cache/"; 69d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr const char* IDMAP_SUFFIX = "@idmap"; 70d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 71d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with StorageManager 72d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_DE = 1 << 0; 73d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_STORAGE_CE = 1 << 1; 74d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 75d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe// NOTE: keep in sync with Installer 76d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CACHE_ONLY = 1 << 8; 77d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampestatic constexpr int FLAG_CLEAR_CODE_CACHE_ONLY = 1 << 9; 78d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 79d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 80eecb2d205366351af42fd0cd9e1a95de3980764eJanis Danisevskis#define MIN_RESTRICTED_HOME_SDK_VERSION 24 // > M 810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeynamespace { 820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeyconstexpr const char* kDump = "android.permission.DUMP"; 840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 85423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status ok() { 86423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::ok(); 87423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 88423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 897be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkeystatic binder::Status exception(uint32_t code) { 907be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return binder::Status::fromExceptionCode(code); 917be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey} 927be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey 93423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status exception(uint32_t code, const std::string& msg) { 94423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromExceptionCode(code, String8(msg.c_str())); 95423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 96423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 97423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error() { 98423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno); 99423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 100423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 101423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(const std::string& msg) { 102423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 103423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str())); 104423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 105423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 106423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeystatic binder::Status error(uint32_t code, const std::string& msg) { 107423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey LOG(ERROR) << msg << " (" << code << ")"; 108423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return binder::Status::fromServiceSpecificError(code, String8(msg.c_str())); 109423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 110423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkPermission(const char* permission) { 1120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey pid_t pid; 1130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid; 1140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (checkCallingPermission(String16(permission), reinterpret_cast<int32_t*>(&pid), 1160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey reinterpret_cast<int32_t*>(&uid))) { 117423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 119423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 120423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission)); 1210274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1220274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1230274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1240274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status checkUid(uid_t expectedUid) { 1250274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = IPCThreadState::self()->getCallingUid(); 1260274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (uid == expectedUid || uid == AID_ROOT) { 127423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1280274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } else { 129423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_SECURITY, 130423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UID %d is not expected UID %d", uid, expectedUid)); 131423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 132423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 133423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 134423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentUuid(const std::unique_ptr<std::string>& uuid) { 135423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!uuid || is_valid_filename(*uuid)) { 136423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 137423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 138423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 139423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("UUID %s is malformed", uuid->c_str())); 140423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 141423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 142423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 143423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkeybinder::Status checkArgumentPackageName(const std::string& packageName) { 144423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (is_valid_package_name(packageName.c_str())) { 145423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 146423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 147423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return exception(binder::Status::EX_ILLEGAL_ARGUMENT, 148423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey StringPrintf("Package name %s is malformed", packageName.c_str())); 1490274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1500274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1510274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1520274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey#define ENFORCE_UID(uid) { \ 1530274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey binder::Status status = checkUid((uid)); \ 1540274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!status.isOk()) { \ 1550274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return status; \ 1560274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } \ 1570274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1580274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 159423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_UUID(uuid) { \ 160423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = checkArgumentUuid((uuid)); \ 161423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 162423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 163423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 164423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 165423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey#define CHECK_ARGUMENT_PACKAGE_NAME(packageName) { \ 167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status status = \ 168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey checkArgumentPackageName((packageName)); \ 169423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!status.isOk()) { \ 170423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return status; \ 171423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } \ 172423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey} 173423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1740274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} // namespace 1750274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1760274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::start() { 1770274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey IPCThreadState::self()->disableBackgroundScheduling(true); 1780274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey status_t ret = BinderService<InstalldNativeService>::publish(); 1790274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (ret != android::OK) { 1800274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return ret; 1810274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1820274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey sp<ProcessState> ps(ProcessState::self()); 1830274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->startThreadPool(); 1840274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ps->giveThreadPoolName(); 1850274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return android::OK; 1860274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 1870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatus_t InstalldNativeService::dump(int fd, const Vector<String16> & /* args */) { 1890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const binder::Status dump_permission = checkPermission(kDump); 1900274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!dump_permission.isOk()) { 1910274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const String8 msg(dump_permission.toString8()); 1920274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.string(), msg.size()); 1930274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return PERMISSION_DENIED; 1940274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey } 1950274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 1960274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey std::string msg = "installd is happy\n"; 1970274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey write(fd, msg.c_str(), strlen(msg.c_str())); 1980274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return NO_ERROR; 1990274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey} 2000274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2017db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey/** 2027db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * Perform restorecon of the given path, but only perform recursive restorecon 2037db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * if the label of that top-level file actually changed. This can save us 2047db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey * significant time by avoiding no-op traversals of large filesystem trees. 2057db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey */ 2060274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& path, const std::string& seInfo, uid_t uid) { 2077db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey int res = 0; 2087db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* before = nullptr; 2097db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey char* after = nullptr; 2107db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2117db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Note that SELINUX_ANDROID_RESTORECON_DATADATA flag is set by 2127db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // libselinux. Not needed here. 2137db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 21495d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &before) < 0) { 2157db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed before getfilecon for " << path; 2167db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2177db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2180274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 0) < 0) { 2197db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed top-level restorecon for " << path; 2207db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2217db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 22295d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey if (lgetfilecon(path.c_str(), &after) < 0) { 2237db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed after getfilecon for " << path; 2247db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2257db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2267db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2277db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // If the initial top-level restorecon above changed the label, then go 2287db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // back and restorecon everything recursively 2297db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (strcmp(before, after)) { 2307db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey LOG(DEBUG) << "Detected label change from " << before << " to " << after << " at " << path 2317db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey << "; running recursive restorecon"; 2320274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seInfo.c_str(), uid, 2337db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey SELINUX_ANDROID_RESTORECON_RECURSE) < 0) { 2347db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey PLOG(ERROR) << "Failed recursive restorecon for " << path; 2357db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto fail; 2367db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2377db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2387db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2397db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey goto done; 2407db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeyfail: 2417db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey res = -1; 2427db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeydone: 2437db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(before); 2447db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey free(after); 2457db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return res; 2467db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey} 2477db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2480274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeystatic int restorecon_app_data_lazy(const std::string& parent, const char* name, 2490274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& seInfo, uid_t uid) { 2500274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey return restorecon_app_data_lazy(StringPrintf("%s/%s", parent.c_str(), name), seInfo, uid); 25195d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey} 25295d9ac642f082870fd4d2419f534171a9b82bbcdJeff Sharkey 2537db6041d156b750689b32e340d8a0753061262a3Jeff Sharkeystatic int prepare_app_dir(const std::string& path, mode_t target_mode, uid_t uid) { 2549a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, uid) != 0) { 2559a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey PLOG(ERROR) << "Failed to prepare " << path; 2569a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return -1; 2579a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 2589a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey return 0; 2599a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2609a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2619a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkeystatic int prepare_app_dir(const std::string& parent, const char* name, mode_t target_mode, 2627db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey uid_t uid) { 2637db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey return prepare_app_dir(StringPrintf("%s/%s", parent.c_str(), name), target_mode, uid); 2649a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey} 2659a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2660274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::createAppData(const std::unique_ptr<std::string>& uuid, 2670274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 2680274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& seInfo, int32_t targetSdkVersion) { 2690274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 270423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 271423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 2720274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2730274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 2740274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* pkgname = packageName.c_str(); 2750274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 2760274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 2770274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey mode_t target_mode = targetSdkVersion >= MIN_RESTRICTED_HOME_SDK_VERSION ? 0700 : 0751; 278aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 2790274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 2807db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (prepare_app_dir(path, target_mode, uid) || 2817db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey prepare_app_dir(path, "cache", 0771, uid) || 2827db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey prepare_app_dir(path, "code_cache", 0771, uid)) { 283423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 2847db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 2857db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 2867db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 2870274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid) || 2880274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey restorecon_app_data_lazy(path, "cache", seInfo, uid) || 2890274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey restorecon_app_data_lazy(path, "code_cache", seInfo, uid)) { 290423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 291c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 2929a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 2939a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // Remember inode numbers of cache directories so that we can clear 2949a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey // contents while CE storage is locked 2959a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (write_path_inode(path, "cache", kXattrInodeCache) || 2969a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey write_path_inode(path, "code_cache", kXattrInodeCodeCache)) { 297423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to write_path_inode for " + path); 298c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 29994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 300aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 3010274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 3027db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey if (prepare_app_dir(path, target_mode, uid)) { 303423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + path); 304c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 3056a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 3067db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey // Consider restorecon over contents if label changed 3070274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (restorecon_app_data_lazy(path, seInfo, uid)) { 308423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + path); 3097db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey } 3107db6041d156b750689b32e340d8a0753061262a3Jeff Sharkey 31190aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey if (property_get_bool("dalvik.vm.usejitprofiles", false)) { 3120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string profile_path = create_data_user_profile_package_path(userId, pkgname); 3136a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // read-write-execute only for the app user. 3146a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict(profile_path.c_str(), 0700, uid, uid) != 0) { 315423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 3166a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 317bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle std::string profile_file = create_primary_profile(profile_path); 318bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle // read-write only for the app user. 319bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle if (fs_prepare_file_strict(profile_file.c_str(), 0600, uid, uid) != 0) { 320423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + profile_path); 321bc56e7dcf07e633867e1525435373a1231216b22Calin Juravle } 3226a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle const std::string ref_profile_path = create_data_ref_profile_package_path(pkgname); 3236a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // dex2oat/profman runs under the shared app gid and it needs to read/write reference 3246a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle // profiles. 3256a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle appid_t shared_app_gid = multiuser_get_shared_app_gid(uid); 3266a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle if (fs_prepare_dir_strict( 3276a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle ref_profile_path.c_str(), 0700, shared_app_gid, shared_app_gid) != 0) { 328423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + ref_profile_path); 3296a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 3306a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle } 33194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 332423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 33394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 33494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 335c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::migrateAppData(const std::unique_ptr<std::string>& uuid, 336c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags) { 337c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 338423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 339423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 340423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 341c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 342c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 343c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 344cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // This method only exists to upgrade system apps that have requested 345cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // forceDeviceEncrypted, so their default storage always lives in a 346cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // consistent location. This only works on non-FBE devices, since we 347cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // never want to risk exposing data on a device with real CE/DE storage. 348cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 349c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto ce_path = create_data_user_ce_package_path(uuid_, userId, pkgname); 350c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto de_path = create_data_user_de_package_path(uuid_, userId, pkgname); 351cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 352cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // If neither directory is marked as default, assume CE is default 353cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(ce_path.c_str(), kXattrDefault, nullptr, 0) == -1 354cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey && getxattr(de_path.c_str(), kXattrDefault, nullptr, 0) == -1) { 355cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (setxattr(ce_path.c_str(), kXattrDefault, nullptr, 0, 0) != 0) { 356423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to mark default storage " + ce_path); 357cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 358cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 359cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 360cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey // Migrate default data location if needed 361cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto target = (flags & FLAG_STORAGE_DE) ? de_path : ce_path; 362cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey auto source = (flags & FLAG_STORAGE_DE) ? ce_path : de_path; 363cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 364cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (getxattr(target.c_str(), kXattrDefault, nullptr, 0) == -1) { 365cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey LOG(WARNING) << "Requested default storage " << target 366cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey << " is not active; migrating from " << source; 367cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (delete_dir_contents_and_dir(target) != 0) { 368423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + target); 369cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 370cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey if (rename(source.c_str(), target.c_str()) != 0) { 371423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to rename " + source + " to " + target); 372cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 373cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey } 374cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 375423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 376cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey} 377cc6281cf8146cddb8ace7cbd58c67321639c1520Jeff Sharkey 3786a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 379475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::clearAppProfiles(const std::string& packageName) { 380475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 381423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 382423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 383475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 384423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 385423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_reference_profile(pkgname)) { 386423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear reference profile for " + packageName); 387423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 388423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (!clear_current_profiles(pkgname)) { 389423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profiles for " + packageName); 390423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 391423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 3926a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle} 3936a1648e2f161cb1d7c46aa9d27e8062521a9f314Calin Juravle 394c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::clearAppData(const std::unique_ptr<std::string>& uuid, 395c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 396c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 397423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 398423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 399423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 400c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 401c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 402c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 403423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 404aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 405c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 4069a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4079a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "cache", kXattrInodeCache); 4089a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4099a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey path = read_path_inode(path, "code_cache", kXattrInodeCodeCache); 4109a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 411c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 412423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 413423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 414423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 415c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 41694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 417aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 4189a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey std::string suffix = ""; 4199a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey bool only_cache = false; 4209a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey if (flags & FLAG_CLEAR_CACHE_ONLY) { 4219a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CACHE_DIR_POSTFIX; 4229a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 4239a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } else if (flags & FLAG_CLEAR_CODE_CACHE_ONLY) { 4249a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey suffix = CODE_CACHE_DIR_POSTFIX; 4259a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey only_cache = true; 4269a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey } 4279a998f4762cb5ad71c786229be748ea0ab9eb7a0Jeff Sharkey 428c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname) + suffix; 429c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (access(path.c_str(), F_OK) == 0) { 430423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents(path) != 0) { 431423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete contents of " + path); 432423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 433c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 434edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle if (!only_cache) { 435c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!clear_current_profile(pkgname, userId)) { 436423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to clear current profile for " + packageName); 437edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 438edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 43994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 440423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 441c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey} 44294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 4437535e8e1995398befc866062d63c05d90eb70cccCalin Juravlestatic int destroy_app_reference_profile(const char *pkgname) { 4447535e8e1995398befc866062d63c05d90eb70cccCalin Juravle return delete_dir_contents_and_dir( 4457535e8e1995398befc866062d63c05d90eb70cccCalin Juravle create_data_ref_profile_package_path(pkgname), 4467535e8e1995398befc866062d63c05d90eb70cccCalin Juravle /*ignore_if_missing*/ true); 4477535e8e1995398befc866062d63c05d90eb70cccCalin Juravle} 4487535e8e1995398befc866062d63c05d90eb70cccCalin Juravle 449edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravlestatic int destroy_app_current_profiles(const char *pkgname, userid_t userid) { 450b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle return delete_dir_contents_and_dir( 451b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle create_data_user_profile_package_path(userid, pkgname), 452b06f98aabc5381fd6366526d9b31b5d0345481b6Calin Juravle /*ignore_if_missing*/ true); 453edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle} 454edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle 455475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::destroyAppProfiles(const std::string& packageName) { 456475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 457423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 458423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 459475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 460423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 461edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle std::vector<userid_t> users = get_known_users(/*volume_uuid*/ nullptr); 462edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle for (auto user : users) { 463423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_current_profiles(pkgname, user) != 0) { 464423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy current profiles for " + packageName); 465423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 466edae669f18eb99b9316891fdde627e2f385c3c64Calin Juravle } 467423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (destroy_app_reference_profile(pkgname) != 0) { 468423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to destroy reference profile for " + packageName); 469423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 470423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 471caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle} 472caa6b809a5ed024a8f3f551c5c109a5ad50ab772Calin Juravle 473c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyAppData(const std::unique_ptr<std::string>& uuid, 474c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode) { 475c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 476423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 477423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 478423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 479c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 480c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 481c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 482423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 483aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 484423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 485423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 486423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 487423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 48894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 489aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 490423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 491423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path) != 0) { 492423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 493423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 494c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey destroy_app_current_profiles(pkgname, userId); 4957535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // TODO(calin): If the package is still installed by other users it's probably 4967535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // beneficial to keep the reference profile around. 4977535e8e1995398befc866062d63c05d90eb70cccCalin Juravle // Verify if it's ok to do that. 4987535e8e1995398befc866062d63c05d90eb70cccCalin Juravle destroy_app_reference_profile(pkgname); 49994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 500423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 50194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 50294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 5030274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkeybinder::Status InstalldNativeService::moveCompleteApp(const std::unique_ptr<std::string>& fromUuid, 5040274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::unique_ptr<std::string>& toUuid, const std::string& packageName, 5050274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const std::string& dataAppName, int32_t appId, const std::string& seInfo, 5060274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey int32_t targetSdkVersion) { 507423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 508423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(fromUuid); 509423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(toUuid); 510423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 5110274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 5120274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* from_uuid = fromUuid ? fromUuid->c_str() : nullptr; 5130274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* to_uuid = toUuid ? toUuid->c_str() : nullptr; 5140274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* package_name = packageName.c_str(); 5150274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey const char* data_app_name = dataAppName.c_str(); 5160274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey 517423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 518e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey std::vector<userid_t> users = get_known_users(from_uuid); 519e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 520d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy app 521d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 52251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_app_package_path(from_uuid, data_app_name); 52351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 52451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to_parent = create_data_app_path(to_uuid); 525d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 526d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey char *argv[] = { 527d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) kCpPath, 528d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 529d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 530d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 531d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 532d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) "-d", /* don't dereference symlinks */ 533d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) from.c_str(), 534d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey (char*) to_parent.c_str() 535d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey }; 536d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 537d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 538d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 539d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (rc != 0) { 540423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 541d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 542d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 543d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 544d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (selinux_android_restorecon(to.c_str(), SELINUX_ANDROID_RESTORECON_RECURSE) != 0) { 545423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon " + to); 546d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey goto fail; 547d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 548d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 549d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 550d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey // Copy private data for all known users 551e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 552e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 553e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Data source may not exist for all users; that's okay 55451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from_ce = create_data_user_ce_package_path(from_uuid, user, package_name); 55551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (access(from_ce.c_str(), F_OK) != 0) { 55651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(INFO) << "Missing source " << from_ce; 557e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey continue; 558e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 559e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 5600274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey if (!createAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, appId, 5610274c977e3b10b52936fd5b2eb464857f0ca6358Jeff Sharkey seInfo, targetSdkVersion).isOk()) { 562423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to create package target"); 563e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey goto fail; 564e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 565e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 566e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey char *argv[] = { 567e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) kCpPath, 568e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-F", /* delete any existing destination file first (--remove-destination) */ 569e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-p", /* preserve timestamps, ownership, and permissions */ 570e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-R", /* recurse into subdirectories (DEST must be a directory) */ 571e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-P", /* Do not follow symlinks [default] */ 572e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey (char*) "-d", /* don't dereference symlinks */ 57351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr, 57451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey nullptr 575e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey }; 576e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 57751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 57851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_de_package_path(from_uuid, user, package_name); 57951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_path(to_uuid, user); 58051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 58151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 58251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 58351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 58451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 58551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 586423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 58751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 58851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 58951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 59051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 59151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto from = create_data_user_ce_package_path(from_uuid, user, package_name); 59251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_path(to_uuid, user); 59351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[6] = (char*) from.c_str(); 59451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey argv[7] = (char*) to.c_str(); 59551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey 59651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(DEBUG) << "Copying " << from << " to " << to; 59751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey int rc = android_fork_execvp(ARRAY_SIZE(argv), argv, NULL, false, true); 59851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (rc != 0) { 599423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(rc, "Failed copying " + from + " to " + to); 60051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey goto fail; 60151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 602e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 603e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 604c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (!restoreconAppData(toUuid, packageName, user, FLAG_STORAGE_CE | FLAG_STORAGE_DE, 605c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey appId, seInfo).isOk()) { 606423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to restorecon"); 607c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey goto fail; 608c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 609e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 610e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 61131f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // We let the framework scan the new location and persist that before 61231f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // deleting the data in the old location; this ordering ensures that 61331f08986f83fa6f2dcf55523b2cf706460aeed7cJeff Sharkey // we can recover from things like battery pulls. 614423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 615e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 616e36372423000a906bafae68844ebc6c42d09335aJeff Sharkeyfail: 617e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey // Nuke everything we might have already copied 618d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey { 61951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_app_package_path(to_uuid, data_app_name); 620d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 621d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 622d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 623d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey } 624e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey for (auto user : users) { 62551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 62651c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_de_package_path(to_uuid, user, package_name); 62751c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 62851c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 62951c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 63051c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 63151c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey { 63251c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey auto to = create_data_user_ce_package_path(to_uuid, user, package_name); 63351c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey if (delete_dir_contents(to.c_str(), 1, NULL) != 0) { 63451c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey LOG(WARNING) << "Failed to rollback " << to; 63551c94495be151c1da03c8266592ed33f253d4b48Jeff Sharkey } 636e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 637e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey } 638423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 639e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey} 640e36372423000a906bafae68844ebc6c42d09335aJeff Sharkey 641c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::createUserData(const std::unique_ptr<std::string>& uuid, 642c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t userSerial ATTRIBUTE_UNUSED, int32_t flags) { 643c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 644423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 645423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 646c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 647379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 648c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 649423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (ensure_config_user_dirs(userId) != 0) { 6507be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error(StringPrintf("Failed to ensure dirs for %d", userId)); 651423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 652379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 65394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 6547be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 655095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee} 656095c763dd9aa26a206d10ab7c1d7e1c569298fb3Robin Lee 657c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::destroyUserData(const std::unique_ptr<std::string>& uuid, 658c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey int32_t userId, int32_t flags) { 659c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 660423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 661423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 662c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 663423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 664379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 665423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_de_path(uuid_, userId); 666423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 667423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 668423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 669c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey if (uuid_ == nullptr) { 670423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_misc_legacy_path(userId); 671423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 672423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 673423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 674423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_user_profiles_path(userId); 675423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 676423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 677423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 67841ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey } 67994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 680379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 681423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto path = create_data_user_ce_path(uuid_, userId); 682423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 683423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 684423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 685423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey path = create_data_media_path(uuid_, userId); 686423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(path, true) != 0) { 687423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + path); 688423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 689379a12b0072b322c7f86e690a8e8a220e500861cJeff Sharkey } 690423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 69194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 69294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 69394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood/* Try to ensure free_size bytes of storage are available. 69494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * Returns 0 on success. 69594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * This is rather simple-minded because doing a full LRU would 69694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * be potentially memory-intensive, and without atime it would 69794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * also require that apps constantly modify file metadata even 69894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood * when just reading from the cache, which is pretty awful. 69994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood */ 700475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::freeCache(const std::unique_ptr<std::string>& uuid, 701475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey int64_t freeStorageSize) { 702475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 703423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 704423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 705475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 70694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache_t* cache; 70794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int64_t avail; 70894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 709475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto data_path = create_data_path(uuid_); 71041ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey 71141ea424413c0381ef2aa15fc5bd5d4b88abd23c4Jeff Sharkey avail = data_disk_free(data_path); 712475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail < 0) { 713423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to determine free space for " + data_path); 714475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 71594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 716475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGI("free_cache(%" PRId64 ") avail %" PRId64 "\n", freeStorageSize, avail); 717475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (avail >= freeStorageSize) { 718423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 719475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 72094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 72194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood cache = start_cache_collection(); 72294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 723475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey auto users = get_known_users(uuid_); 72454e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey for (auto user : users) { 725475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_ce_path(uuid_, user)); 726475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey add_cache_files(cache, create_data_user_de_path(uuid_, user)); 72754e292e1ea87b504b552393d2e9bc800458bd0c1Jeff Sharkey add_cache_files(cache, 728475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey StringPrintf("%s/Android/data", create_data_media_path(uuid_, user).c_str())); 72994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 73094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 731475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey clear_cache_files(data_path, cache, freeStorageSize); 73294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood finish_cache_collection(cache); 73394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 734423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey avail = data_disk_free(data_path); 735423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (avail >= freeStorageSize) { 736423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 737475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } else { 738423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to free up %" PRId64 " on %s; final free space %" PRId64, 739423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey freeStorageSize, data_path.c_str(), avail)); 740475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 74194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 74294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 743475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmdex(const std::string& codePath, 744475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 745475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 74694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood char dex_path[PKG_PATH_MAX]; 74794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 748475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* path = codePath.c_str(); 749475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 750475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 751770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey if (validate_apk_path(path) && validate_system_app_path(path)) { 752423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + codePath); 753770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey } 754770180a4dd86f8bda6af2e6db4676e99a5bb1548Jeff Sharkey 755475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (!create_cache_path(dex_path, path, instruction_set)) { 756423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to create cache path for " + codePath); 757475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 75894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 75994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("unlink %s\n", dex_path); 76094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(dex_path) < 0) { 761423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", dex_path)); 76294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 763423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 76494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 76594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 76694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7672f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkeystatic void add_app_data_size(std::string& path, int64_t *codesize, int64_t *datasize, 7682f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t *cachesize) { 76994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood DIR *d; 77094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood int dfd; 77194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct dirent *de; 77294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s; 773d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7742f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(path.c_str()); 7752f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d == nullptr) { 7762f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey PLOG(WARNING) << "Failed to open " << path; 7772f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey return; 77894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 7792f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 7802f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey while ((de = readdir(d))) { 7812f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey const char *name = de->d_name; 782d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 7832f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t statsize = 0; 7842f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) { 7852f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey statsize = stat_size(&s); 78694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 78794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 7882f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (de->d_type == DT_DIR) { 7892f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int subfd; 7902f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int64_t dirsize = 0; 7912f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey /* always skip "." and ".." */ 7922f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[0] == '.') { 7932f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (name[1] == 0) continue; 7942f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if ((name[1] == '.') && (name[2] == 0)) continue; 7952f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 7962f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY); 7972f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (subfd >= 0) { 7982f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dirsize = calculate_dir_size(subfd); 7992f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey close(subfd); 8002f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 8012f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey // TODO: check xattrs! 8022f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (!strcmp(name, "cache") || !strcmp(name, "code_cache")) { 8032f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 8042f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *cachesize += dirsize; 8052f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 8062f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += dirsize + statsize; 8072f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 8082f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else if (de->d_type == DT_LNK && !strcmp(name, "lib")) { 8092f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *codesize += statsize; 8102f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } else { 8112f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey *datasize += statsize; 81294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 81394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 8142f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 8152f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 81694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8176c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::getAppSize(const std::unique_ptr<std::string>& uuid, 818423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t ceDataInode, 819423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey const std::string& codePath, std::vector<int64_t>* _aidl_return) { 8206c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 821423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 822423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 823423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 8246c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 8256c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName.c_str(); 8266c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* code_path = codePath.c_str(); 8276c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 8282f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey DIR *d; 8292f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey int dfd; 8306c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int64_t codesize = 0; 8316c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int64_t datasize = 0; 8326c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int64_t cachesize = 0; 8336c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int64_t asecsize = 0; 83494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8352f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey d = opendir(code_path); 8362f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (d != nullptr) { 8372f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey dfd = dirfd(d); 8386c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey codesize += calculate_dir_size(dfd); 8392f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey closedir(d); 84094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 84194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8422f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 8436c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname, ceDataInode); 8446c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey add_app_data_size(path, &codesize, &datasize, &cachesize); 8452f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 8462f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_DE) { 8476c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgname); 8486c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey add_app_data_size(path, &codesize, &datasize, &cachesize); 8492f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey } 85094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8516c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey std::vector<int64_t> res; 8526c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey res.push_back(codesize); 8536c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey res.push_back(datasize); 8546c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey res.push_back(cachesize); 8556c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey res.push_back(asecsize); 8566c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey *_aidl_return = res; 857423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 8582f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey} 859d792118c493806eeb24a8203f508e6e18fe93bd7Jeff Sharkey 860c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::getAppDataInode(const std::unique_ptr<std::string>& uuid, 8616c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int64_t* _aidl_return) { 862c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 863423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 864423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 865423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 866c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 867c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgname = packageName.c_str(); 868c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey 8692f720f7ec5c9d0b91defc85878e7330b10f8e89aJeff Sharkey if (flags & FLAG_STORAGE_CE) { 870c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgname); 8717be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey if (get_path_inode(path, reinterpret_cast<ino_t*>(_aidl_return)) == 0) { 8727be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return ok(); 8737be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey } else { 8747be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return error("Failed to get_path_inode for " + path); 875423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 87694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 8777be5ead81aa4d3b2536556c762bab03445a98155Jeff Sharkey return exception(binder::Status::EX_UNSUPPORTED_OPERATION); 87894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 87994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 8806727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// Dumps the contents of a profile file, using pkgname's dex files for pretty 8816727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr// printing the result. 882475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::dumpProfiles(int32_t uid, const std::string& packageName, 883475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& codePaths, bool* _aidl_return) { 884475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 885423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 886475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 887475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 88890aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey const char* code_paths = codePaths.c_str(); 8896727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 89090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey *_aidl_return = dump_profiles(uid, pkgname, code_paths); 891423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 8926727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr} 8936727c2d0bfc876c62d6ee60d8a05d26bef1d56efDavid Sehr 8944d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe// TODO: Consider returning error codes. 895475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::mergeProfiles(int32_t uid, const std::string& packageName, 896423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey bool* _aidl_return) { 897475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 898423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 899423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 900475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 901475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey *_aidl_return = analyse_profiles(uid, pkgname); 902423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 9034d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe} 9044d0f825dd76a1972a3d081e771cde28513a1c6ffAndreas Gampe 9056c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkeybinder::Status InstalldNativeService::dexopt(const std::string& apkPath, int32_t uid, 9066c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& packageName, const std::string& instructionSet, 9076c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int32_t dexoptNeeded, const std::unique_ptr<std::string>& outputPath, int32_t dexFlags, 9086c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::string& compilerFilter, const std::unique_ptr<std::string>& uuid, 9096c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const std::unique_ptr<std::string>& sharedLibraries) { 9106c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 911423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 912423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (packageName && *packageName != "*") { 913423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(*packageName); 914423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 9156c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 9166c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* apk_path = apkPath.c_str(); 9176c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* pkgname = packageName ? packageName->c_str() : "*"; 9186c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 9196c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* oat_dir = outputPath ? outputPath->c_str() : nullptr; 9206c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* compiler_filter = compilerFilter.c_str(); 9216c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* volume_uuid = uuid ? uuid->c_str() : nullptr; 9226c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey const char* shared_libraries = sharedLibraries ? sharedLibraries->c_str() : nullptr; 9236c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 9246c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey int res = android::installd::dexopt(apk_path, uid, pkgname, instruction_set, dexoptNeeded, 9256c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey oat_dir, dexFlags, compiler_filter, volume_uuid, shared_libraries); 926423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res ? error(res, "Failed to dexopt") : ok(); 9276c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey} 9286c2c056193010cf93b4264810d462c120ce801c8Jeff Sharkey 929475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::markBootComplete(const std::string& instructionSet) { 930475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 931475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 932475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 933475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey char boot_marker_path[PKG_PATH_MAX]; 934475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey sprintf(boot_marker_path, 93502d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe "%s/%s/%s/.booting", 93602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe android_data_dir.path, 93702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe DALVIK_CACHE, 93802d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe instruction_set); 939091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 940475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ALOGV("mark_boot_complete : %s", boot_marker_path); 941475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (unlink(boot_marker_path) != 0) { 942423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to unlink %s", boot_marker_path)); 943475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 944423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 945091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath} 946091ea779d4e575856c04d51d82f45cc8a6155b5eNarayan Kamath 94794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodvoid mkinnerdirs(char* path, int basepos, mode_t mode, int uid, int gid, 94894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat* statbuf) 94994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood{ 95094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood while (path[basepos] != 0) { 95194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (path[basepos] == '/') { 95294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = 0; 95394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(path, statbuf) < 0) { 95494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGV("Making directory: %s\n", path); 95594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (mkdir(path, mode) == 0) { 95694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood chown(path, uid, gid); 95794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 95894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood ALOGW("Unable to make directory %s: %s\n", path, strerror(errno)); 95994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 96094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 96194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood path[basepos] = '/'; 96294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 96394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 96494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood basepos++; 96594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 96694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 96794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 968475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkNativeLibraryDirectory( 969475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::unique_ptr<std::string>& uuid, const std::string& packageName, 970475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& nativeLibPath32, int32_t userId) { 971475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 972423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 973423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 974423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 975475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 976475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* pkgname = packageName.c_str(); 977475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* asecLibDir = nativeLibPath32.c_str(); 97894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood struct stat s, libStat; 979423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 98094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 981423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _pkgdir = create_data_user_ce_package_path(uuid_, userId, pkgname); 982423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto _libsymlink = _pkgdir + PKG_LIB_POSTFIX; 983c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey 984c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* pkgdir = _pkgdir.c_str(); 985c03de09173f98506e73e7cf7df21fe11795d4b24Jeff Sharkey const char* libsymlink = _libsymlink.c_str(); 98694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 987475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey if (stat(pkgdir, &s) < 0) { 988423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to stat " + _pkgdir); 989475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey } 99094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 99194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, AID_INSTALL, AID_INSTALL) < 0) { 992423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to chown " + _pkgdir); 99394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 99494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 99594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, 0700) < 0) { 996423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to chmod " + _pkgdir); 99794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 99894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 99994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 100094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (lstat(libsymlink, &libStat) < 0) { 100194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (errno != ENOENT) { 1002423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to stat " + _libsymlink); 100394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 100494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 100594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else { 100694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (S_ISDIR(libStat.st_mode)) { 10073aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath if (delete_dir_contents(libsymlink, 1, NULL) < 0) { 1008423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to delete " + _libsymlink); 100994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 101094afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 101194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } else if (S_ISLNK(libStat.st_mode)) { 101294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (unlink(libsymlink) < 0) { 1013423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to unlink " + _libsymlink); 101494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 101594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 101694afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 101794afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 101894afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 101994afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (symlink(asecLibDir, libsymlink) < 0) { 1020423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("Failed to symlink " + _libsymlink + " to " + nativeLibPath32); 102194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood goto out; 102294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 102394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 102494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwoodout: 102594afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chmod(pkgdir, s.st_mode) < 0) { 1026423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chmod " + _pkgdir; 1027423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1028423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1029423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1030423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1031423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 103294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 103394afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 103494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood if (chown(pkgdir, s.st_uid, s.st_gid) < 0) { 1035423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey auto msg = "Failed to cleanup chown " + _pkgdir; 1036423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (res.isOk()) { 1037423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error(msg); 1038423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } else { 1039423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey PLOG(ERROR) << msg; 1040423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 104194afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood } 104294afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood 1043423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 104494afecf4b6f437b3ee9a076242402e421c6c07a6Mike Lockwood} 104563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 104663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic void run_idmap(const char *target_apk, const char *overlay_apk, int idmap_fd) 104763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 104863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const char *IDMAP_BIN = "/system/bin/idmap"; 104963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad static const size_t MAX_INT_LEN = 32; 105063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_str[MAX_INT_LEN]; 105163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 105263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_str, sizeof(idmap_str), "%d", idmap_fd); 105363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 105463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad execl(IDMAP_BIN, IDMAP_BIN, "--fd", target_apk, overlay_apk, idmap_str, (char*)NULL); 105563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("execl(%s) failed: %s\n", IDMAP_BIN, strerror(errno)); 105663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 105763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 105863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// Transform string /a/b/c.apk to (prefix)/a@b@c.apk@(suffix) 105963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad// eg /a/b/c.apk to /data/resource-cache/a@b@c.apk@idmap 106063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadstatic int flatten_path(const char *prefix, const char *suffix, 106163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const char *overlay_path, char *idmap_path, size_t N) 106263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad{ 106363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (overlay_path == NULL || idmap_path == NULL) { 106463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 106563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 106663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_overlay_path = strlen(overlay_path); 106763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // will access overlay_path + 1 further below; requires absolute path 106863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (len_overlay_path < 2 || *overlay_path != '/') { 106963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 107063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 107163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_idmap_root = strlen(prefix); 107263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad const size_t len_suffix = strlen(suffix); 107363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (SIZE_MAX - len_idmap_root < len_overlay_path || 107463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad SIZE_MAX - (len_idmap_root + len_overlay_path) < len_suffix) { 107563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad // additions below would cause overflow 107663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 107763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 107863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (N < len_idmap_root + len_overlay_path + len_suffix) { 107963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return -1; 108063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 108163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad memset(idmap_path, 0, N); 108263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad snprintf(idmap_path, N, "%s%s%s", prefix, overlay_path + 1, suffix); 108363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char *ch = idmap_path + len_idmap_root; 108463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad while (*ch != '\0') { 108563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (*ch == '/') { 108663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad *ch = '@'; 108763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 108863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ++ch; 108963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 109063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad return 0; 109163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 109263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 1093475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::idmap(const std::string& targetApkPath, 1094475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& overlayApkPath, int32_t uid) { 1095475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1096475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* target_apk = targetApkPath.c_str(); 1097475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* overlay_apk = overlayApkPath.c_str(); 109863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGV("idmap target_apk=%s overlay_apk=%s uid=%d\n", target_apk, overlay_apk, uid); 109963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 110063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int idmap_fd = -1; 110163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad char idmap_path[PATH_MAX]; 110263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 110363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flatten_path(IDMAP_PREFIX, IDMAP_SUFFIX, overlay_apk, 110463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_path, sizeof(idmap_path)) == -1) { 110563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot generate idmap path for overlay %s\n", overlay_apk); 110663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 110763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 110863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 110963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 111063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad idmap_fd = open(idmap_path, O_RDWR | O_CREAT | O_EXCL, 0644); 111163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd < 0) { 111263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot open '%s' for output: %s\n", idmap_path, strerror(errno)); 111363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 111463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 111563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchown(idmap_fd, AID_SYSTEM, uid) < 0) { 111663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chown '%s'\n", idmap_path); 111763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 111863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 111963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (fchmod(idmap_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) < 0) { 112063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap cannot chmod '%s'\n", idmap_path); 112163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 112263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 112363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 112463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid_t pid; 112563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad pid = fork(); 112663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (pid == 0) { 112763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad /* child -- drop privileges before continuing */ 112863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setgid(uid) != 0) { 112963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setgid(%d) failed during idmap\n", uid); 113063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 113163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 113263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (setuid(uid) != 0) { 113363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("setuid(%d) failed during idmap\n", uid); 113463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 113563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 113663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (flock(idmap_fd, LOCK_EX | LOCK_NB) != 0) { 113763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("flock(%s) failed during idmap: %s\n", idmap_path, strerror(errno)); 113863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); 113963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 114063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 114163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad run_idmap(target_apk, overlay_apk, idmap_fd); 114263568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad exit(1); /* only if exec call to idmap failed */ 114363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } else { 114463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad int status = wait_child(pid); 114563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (status != 0) { 114663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad ALOGE("idmap failed, status=0x%04x\n", status); 114763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad goto fail; 114863568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 114963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 115063568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad 115163568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 1152423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 115363568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstadfail: 115463568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad if (idmap_fd >= 0) { 115563568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad close(idmap_fd); 115663568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad unlink(idmap_path); 115763568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad } 1158423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(); 115963568b1430d741f40ca008391c854ef1cc880138MÃ¥rten Kongstad} 1160e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1161c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkeybinder::Status InstalldNativeService::restoreconAppData(const std::unique_ptr<std::string>& uuid, 1162c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& packageName, int32_t userId, int32_t flags, int32_t appId, 1163c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const std::string& seInfo) { 1164c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1165423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_UUID(uuid); 1166423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey CHECK_ARGUMENT_PACKAGE_NAME(packageName); 1167423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey 1168423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey binder::Status res = ok(); 1169e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1170da30dc7336f03ca629fe173db1425fdce989119cRobert Craig // SELINUX_ANDROID_RESTORECON_DATADATA flag is set by libselinux. Not needed here. 1171c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey unsigned int seflags = SELINUX_ANDROID_RESTORECON_RECURSE; 1172c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* uuid_ = uuid ? uuid->c_str() : nullptr; 1173c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* pkgName = packageName.c_str(); 1174c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey const char* seinfo = seInfo.c_str(); 1175e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig 1176c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey uid_t uid = multiuser_get_uid(userId, appId); 1177aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_CE) { 1178c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_ce_package_path(uuid_, userId, pkgName); 1179c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1180423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1181da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1182c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey } 1183aa7ddfd8347fa947618a7668b013521e95722c5cJeff Sharkey if (flags & FLAG_STORAGE_DE) { 1184c1e93e7d746e07791b0667c80ad43a407c515fa8Jeff Sharkey auto path = create_data_user_de_package_path(uuid_, userId, pkgName); 1185c7d1b2250e8245a7e4e144758bc3ccd33b8d6319Jeff Sharkey if (selinux_android_restorecon_pkgdir(path.c_str(), seinfo, uid, seflags) < 0) { 1186423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey res = error("restorecon failed for " + path); 1187da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1188da30dc7336f03ca629fe173db1425fdce989119cRobert Craig } 1189423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return res; 1190e9887e46cea4a095e4219927eadbe4c57bb1a5eeRobert Craig} 11913aee2c5c749dc2589f001b26fae1ec958ec89524Narayan Kamath 1192475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::createOatDir(const std::string& oatDir, 1193475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet) { 1194475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1195475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = oatDir.c_str(); 1196475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 119788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov char oat_instr_dir[PKG_PATH_MAX]; 119888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 119988ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (validate_apk_path(oat_dir)) { 1200423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + oatDir); 120188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 12028eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1203423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to prepare " + oatDir); 120488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 120588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov if (selinux_android_restorecon(oat_dir, 0)) { 1206423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to restorecon " + oatDir); 120788ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 120888ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov snprintf(oat_instr_dir, PKG_PATH_MAX, "%s/%s", oat_dir, instruction_set); 12098eed7e6a9bb527af7380ec13b390c6e2955eba6aFyodor Kupolov if (fs_prepare_dir(oat_instr_dir, S_IRWXU | S_IRWXG | S_IXOTH, AID_SYSTEM, AID_INSTALL)) { 1210423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to prepare %s", oat_instr_dir)); 121188ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1212423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 121388ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 121488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1215475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::rmPackageDir(const std::string& packageDir) { 1216475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1217423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (validate_apk_path(packageDir.c_str())) { 1218423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Invalid path " + packageDir); 1219423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey } 1220423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (delete_dir_contents_and_dir(packageDir) != 0) { 1221423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error("Failed to delete " + packageDir); 122288ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov } 1223423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 122488ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov} 122588ce4ff7a95ea2008fa28f12b880ee526e331440Fyodor Kupolov 1226475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::linkFile(const std::string& relativePath, 1227475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& fromBase, const std::string& toBase) { 1228475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 1229475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* relative_path = relativePath.c_str(); 1230475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* from_base = fromBase.c_str(); 1231475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* to_base = toBase.c_str(); 1232d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char from_path[PKG_PATH_MAX]; 1233d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath char to_path[PKG_PATH_MAX]; 1234d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(from_path, PKG_PATH_MAX, "%s/%s", from_base, relative_path); 1235d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath snprintf(to_path, PKG_PATH_MAX, "%s/%s", to_base, relative_path); 1236d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1237d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(from_path)) { 1238423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid from path %s", from_path)); 1239d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1240d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1241d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath if (validate_apk_path_subdirs(to_path)) { 1242423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Invalid to path %s", to_path)); 1243d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1244d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1245423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey if (link(from_path, to_path) < 0) { 1246423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return error(StringPrintf("Failed to link from %s to %s", from_path, to_path)); 1247d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath } 1248d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1249423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return ok(); 1250d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath} 1251d845c96128a40ca5802c0840ae190fa0af7d4735Narayan Kamath 1252475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::moveAb(const std::string& apkPath, 1253475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1254475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 125590aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1256475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1257475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1258475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1259d089ca1703769854356a263ca640d3e07ab8548dAndreas Gampe 126090aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool success = move_ab(apk_path, instruction_set, oat_dir); 1261423e746ac7d4b3a3d772dd0e01bdb9fd6029d439Jeff Sharkey return success ? ok() : error(); 1262e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe} 1263e65b4faa798269ab6ee0d27dab03b8b5caa6f956Andreas Gampe 1264475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkeybinder::Status InstalldNativeService::deleteOdex(const std::string& apkPath, 1265475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const std::string& instructionSet, const std::string& outputPath) { 1266475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey ENFORCE_UID(AID_SYSTEM); 126790aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey 1268475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* apk_path = apkPath.c_str(); 1269475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* instruction_set = instructionSet.c_str(); 1270475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey const char* oat_dir = outputPath.c_str(); 1271475c6f9743e5f6696c38fb22b8b8e9d49a82ab56Jeff Sharkey 127290aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey bool res = delete_odex(apk_path, instruction_set, oat_dir); 127390aff26f0135379db19432ae90c40c0831ba5954Jeff Sharkey return res ? ok() : error(); 1274b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe} 1275b31206b67805617c79fb46ca39ccc5b26cdefd63Andreas Gampe 127602d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace installd 127702d0de56c75347a0cb8d5a8565dc8c4ee7616057Andreas Gampe} // namespace android 1278