| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | ** Copyright 2008, The Android Open Source Project | 
|  | 3 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 4 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | ** you may not use this file except in compliance with the License. | 
|  | 6 | ** You may obtain a copy of the License at | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 7 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 8 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 9 | ** | 
| Dave Allison | d937073 | 2014-01-30 14:19:23 -0800 | [diff] [blame] | 10 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 11 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | ** See the License for the specific language governing permissions and | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 14 | ** limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 17 | #include "utils.h" | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 18 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 19 | #include <errno.h> | 
|  | 20 | #include <fcntl.h> | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 21 | #include <fts.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 22 | #include <stdlib.h> | 
| Eric Holk | 2af5e6a | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 23 | #include <sys/capability.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> | 
|  | 25 | #include <sys/wait.h> | 
| Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 26 | #include <sys/xattr.h> | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 27 | #include <sys/statvfs.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 28 |  | 
| Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 29 | #include <android-base/file.h> | 
| Elliott Hughes | e4ec9eb | 2015-12-04 15:39:32 -0800 | [diff] [blame] | 30 | #include <android-base/logging.h> | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 31 | #include <android-base/strings.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 33 | #include <android-base/unique_fd.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 34 | #include <cutils/fs.h> | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 35 | #include <cutils/properties.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 36 | #include <log/log.h> | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> | 
| Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 38 | #include <private/android_projectid_config.h> | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 39 |  | 
| Eric Holk | 2af5e6a | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 40 | #include "dexopt_return_codes.h" | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 41 | #include "globals.h"  // extern variables. | 
| Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 42 | #include "QuotaUtils.h" | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 43 |  | 
|  | 44 | #ifndef LOG_TAG | 
|  | 45 | #define LOG_TAG "installd" | 
|  | 46 | #endif | 
| Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 47 |  | 
| Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 48 | #define DEBUG_XATTRS 0 | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 49 |  | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 50 | using android::base::EndsWith; | 
| Mathieu Chartier | 89883e3 | 2018-11-01 11:39:00 -0700 | [diff] [blame] | 51 | using android::base::Fdopendir; | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 52 | using android::base::StringPrintf; | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 53 | using android::base::unique_fd; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 54 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 55 | namespace android { | 
|  | 56 | namespace installd { | 
|  | 57 |  | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 58 | /** | 
|  | 59 | * Check that given string is valid filename, and that it attempts no | 
|  | 60 | * parent or child directory traversal. | 
|  | 61 | */ | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 62 | bool is_valid_filename(const std::string& name) { | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 63 | if (name.empty() || (name == ".") || (name == "..") | 
|  | 64 | || (name.find('/') != std::string::npos)) { | 
|  | 65 | return false; | 
|  | 66 | } else { | 
|  | 67 | return true; | 
|  | 68 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 71 | static void check_package_name(const char* package_name) { | 
|  | 72 | CHECK(is_valid_filename(package_name)); | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 73 | CHECK(is_valid_package_name(package_name)); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Nikita Ioffe | 8755f79 | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 76 | static std::string resolve_ce_path_by_inode_or_fallback(const std::string& root_path, | 
|  | 77 | ino_t ce_data_inode, const std::string& fallback) { | 
|  | 78 | if (ce_data_inode != 0) { | 
|  | 79 | DIR* dir = opendir(root_path.c_str()); | 
|  | 80 | if (dir == nullptr) { | 
|  | 81 | PLOG(ERROR) << "Failed to opendir " << root_path; | 
|  | 82 | return fallback; | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | struct dirent* ent; | 
|  | 86 | while ((ent = readdir(dir))) { | 
|  | 87 | if (ent->d_ino == ce_data_inode) { | 
|  | 88 | auto resolved = StringPrintf("%s/%s", root_path.c_str(), ent->d_name); | 
|  | 89 | if (resolved != fallback) { | 
|  | 90 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << ce_data_inode | 
|  | 91 | << " instead of " << fallback; | 
|  | 92 | } | 
|  | 93 | closedir(dir); | 
|  | 94 | return resolved; | 
|  | 95 | } | 
|  | 96 | } | 
|  | 97 | LOG(WARNING) << "Failed to resolve inode " << ce_data_inode << "; using " << fallback; | 
|  | 98 | closedir(dir); | 
|  | 99 | return fallback; | 
|  | 100 | } else { | 
|  | 101 | return fallback; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 105 | /** | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 106 | * Create the path name where package data should be stored for the given | 
|  | 107 | * volume UUID, package name, and user ID. An empty UUID is assumed to be | 
|  | 108 | * internal storage. | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 109 | */ | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 110 | std::string create_data_user_ce_package_path(const char* volume_uuid, | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 111 | userid_t user, const char* package_name) { | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 112 | check_package_name(package_name); | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 113 | return StringPrintf("%s/%s", | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 114 | create_data_user_ce_path(volume_uuid, user).c_str(), package_name); | 
|  | 115 | } | 
|  | 116 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 117 | /** | 
|  | 118 | * Create the path name where package data should be stored for the given | 
|  | 119 | * volume UUID, package name, and user ID. An empty UUID is assumed to be | 
|  | 120 | * internal storage. | 
|  | 121 | * Compared to create_data_user_ce_package_path this method always return the | 
|  | 122 | * ".../user/..." directory. | 
|  | 123 | */ | 
|  | 124 | std::string create_data_user_ce_package_path_as_user_link( | 
|  | 125 | const char* volume_uuid, userid_t userid, const char* package_name) { | 
|  | 126 | check_package_name(package_name); | 
|  | 127 | std::string data(create_data_path(volume_uuid)); | 
|  | 128 | return StringPrintf("%s/user/%u/%s", data.c_str(), userid, package_name); | 
|  | 129 | } | 
|  | 130 |  | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 131 | std::string create_data_user_ce_package_path(const char* volume_uuid, userid_t user, | 
|  | 132 | const char* package_name, ino_t ce_data_inode) { | 
|  | 133 | // For testing purposes, rely on the inode when defined; this could be | 
|  | 134 | // optimized to use access() in the future. | 
|  | 135 | auto fallback = create_data_user_ce_package_path(volume_uuid, user, package_name); | 
| Nikita Ioffe | 8755f79 | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 136 | auto user_path = create_data_user_ce_path(volume_uuid, user); | 
|  | 137 | return resolve_ce_path_by_inode_or_fallback(user_path, ce_data_inode, fallback); | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 138 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 139 |  | 
| Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 140 | std::string create_data_user_de_package_path(const char* volume_uuid, | 
|  | 141 | userid_t user, const char* package_name) { | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 142 | check_package_name(package_name); | 
| Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 143 | return StringPrintf("%s/%s", | 
|  | 144 | create_data_user_de_path(volume_uuid, user).c_str(), package_name); | 
|  | 145 | } | 
|  | 146 |  | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 147 | std::string create_data_path(const char* volume_uuid) { | 
|  | 148 | if (volume_uuid == nullptr) { | 
|  | 149 | return "/data"; | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 150 | } else if (!strcmp(volume_uuid, "TEST")) { | 
|  | 151 | CHECK(property_get_bool("ro.debuggable", false)); | 
|  | 152 | return "/data/local/tmp"; | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 153 | } else { | 
|  | 154 | CHECK(is_valid_filename(volume_uuid)); | 
|  | 155 | return StringPrintf("/mnt/expand/%s", volume_uuid); | 
|  | 156 | } | 
|  | 157 | } | 
|  | 158 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 159 | /** | 
| Jeff Sharkey | d792118 | 2015-04-30 15:58:19 -0700 | [diff] [blame] | 160 | * Create the path name for app data. | 
|  | 161 | */ | 
|  | 162 | std::string create_data_app_path(const char* volume_uuid) { | 
|  | 163 | return StringPrintf("%s/app", create_data_path(volume_uuid).c_str()); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | /** | 
| Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 167 | * Create the path name for user data for a certain userid. | 
| cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 168 | * Keep same implementation as vold to minimize path walking overhead | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 169 | */ | 
| Jeff Sharkey | 2f720f7 | 2016-04-10 20:51:40 -0600 | [diff] [blame] | 170 | std::string create_data_user_ce_path(const char* volume_uuid, userid_t userid) { | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 171 | std::string data(create_data_path(volume_uuid)); | 
| cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 172 | if (volume_uuid == nullptr && userid == 0) { | 
|  | 173 | std::string legacy = StringPrintf("%s/data", data.c_str()); | 
|  | 174 | struct stat sb; | 
|  | 175 | if (lstat(legacy.c_str(), &sb) == 0 && S_ISDIR(sb.st_mode)) { | 
|  | 176 | /* /data/data is dir, return /data/data for legacy system */ | 
|  | 177 | return legacy; | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 178 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 179 | } | 
| cjbao | 75d4e57 | 2017-04-12 00:12:24 +0800 | [diff] [blame] | 180 | return StringPrintf("%s/user/%u", data.c_str(), userid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 181 | } | 
|  | 182 |  | 
|  | 183 | /** | 
| Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 184 | * Create the path name for device encrypted user data for a certain userid. | 
|  | 185 | */ | 
|  | 186 | std::string create_data_user_de_path(const char* volume_uuid, userid_t userid) { | 
|  | 187 | std::string data(create_data_path(volume_uuid)); | 
|  | 188 | return StringPrintf("%s/user_de/%u", data.c_str(), userid); | 
|  | 189 | } | 
|  | 190 |  | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 191 | std::string create_data_misc_ce_rollback_base_path(const char* volume_uuid, userid_t user) { | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 192 | return StringPrintf("%s/misc_ce/%u/rollback", create_data_path(volume_uuid).c_str(), user); | 
|  | 193 | } | 
|  | 194 |  | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 195 | std::string create_data_misc_de_rollback_base_path(const char* volume_uuid, userid_t user) { | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 196 | return StringPrintf("%s/misc_de/%u/rollback", create_data_path(volume_uuid).c_str(), user); | 
|  | 197 | } | 
|  | 198 |  | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 199 | std::string create_data_misc_ce_rollback_path(const char* volume_uuid, userid_t user, | 
|  | 200 | int32_t snapshot_id) { | 
|  | 201 | return StringPrintf("%s/%d", create_data_misc_ce_rollback_base_path(volume_uuid, user).c_str(), | 
|  | 202 | snapshot_id); | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | std::string create_data_misc_de_rollback_path(const char* volume_uuid, userid_t user, | 
|  | 206 | int32_t snapshot_id) { | 
|  | 207 | return StringPrintf("%s/%d", create_data_misc_de_rollback_base_path(volume_uuid, user).c_str(), | 
|  | 208 | snapshot_id); | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 209 | } | 
|  | 210 |  | 
| Nikita Ioffe | 8755f79 | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 211 | std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid, | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 212 | userid_t user, int32_t snapshot_id, const char* package_name) { | 
|  | 213 | return StringPrintf("%s/%s", | 
|  | 214 | create_data_misc_ce_rollback_path(volume_uuid, user, snapshot_id).c_str(), package_name); | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | std::string create_data_misc_ce_rollback_package_path(const char* volume_uuid, | 
|  | 218 | userid_t user, int32_t snapshot_id, const char* package_name, ino_t ce_rollback_inode) { | 
|  | 219 | auto fallback = create_data_misc_ce_rollback_package_path(volume_uuid, user, snapshot_id, | 
|  | 220 | package_name); | 
|  | 221 | auto user_path = create_data_misc_ce_rollback_path(volume_uuid, user, snapshot_id); | 
| Nikita Ioffe | 8755f79 | 2019-01-25 13:54:43 +0000 | [diff] [blame] | 222 | return resolve_ce_path_by_inode_or_fallback(user_path, ce_rollback_inode, fallback); | 
|  | 223 | } | 
|  | 224 |  | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 225 | std::string create_data_misc_de_rollback_package_path(const char* volume_uuid, | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 226 | userid_t user, int32_t snapshot_id, const char* package_name) { | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 227 | return StringPrintf("%s/%s", | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 228 | create_data_misc_de_rollback_path(volume_uuid, user, snapshot_id).c_str(), package_name); | 
| Narayan Kamath | dfdfb79 | 2019-01-14 15:21:52 +0000 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Jeff Sharkey | 63ec2d6 | 2015-11-09 13:10:36 -0800 | [diff] [blame] | 231 | /** | 
| Jeff Sharkey | abe4fe5 | 2013-07-10 16:55:46 -0700 | [diff] [blame] | 232 | * Create the path name for media for a certain userid. | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 233 | */ | 
| Jeff Sharkey | 41ea424 | 2015-04-09 11:34:03 -0700 | [diff] [blame] | 234 | std::string create_data_media_path(const char* volume_uuid, userid_t userid) { | 
|  | 235 | return StringPrintf("%s/media/%u", create_data_path(volume_uuid).c_str(), userid); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 238 | std::string create_data_media_package_path(const char* volume_uuid, userid_t userid, | 
|  | 239 | const char* data_type, const char* package_name) { | 
|  | 240 | return StringPrintf("%s/Android/%s/%s", create_data_media_path(volume_uuid, userid).c_str(), | 
|  | 241 | data_type, package_name); | 
|  | 242 | } | 
|  | 243 |  | 
| Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 244 | std::string create_data_misc_legacy_path(userid_t userid) { | 
|  | 245 | return StringPrintf("%s/misc/user/%u", create_data_path(nullptr).c_str(), userid); | 
|  | 246 | } | 
|  | 247 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 248 | std::string create_primary_cur_profile_dir_path(userid_t userid) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 249 | return StringPrintf("%s/cur/%u", android_profiles_dir.c_str(), userid); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 250 | } | 
|  | 251 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 252 | std::string create_primary_current_profile_package_dir_path(userid_t user, | 
|  | 253 | const std::string& package_name) { | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 254 | check_package_name(package_name.c_str()); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 255 | return StringPrintf("%s/%s", | 
|  | 256 | create_primary_cur_profile_dir_path(user).c_str(), package_name.c_str()); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 257 | } | 
|  | 258 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 259 | std::string create_primary_ref_profile_dir_path() { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 260 | return StringPrintf("%s/ref", android_profiles_dir.c_str()); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 261 | } | 
|  | 262 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 263 | std::string create_primary_reference_profile_package_dir_path(const std::string& package_name) { | 
| Calin Juravle | 76268c5 | 2017-03-09 13:19:42 -0800 | [diff] [blame] | 264 | check_package_name(package_name.c_str()); | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 265 | return StringPrintf("%s/ref/%s", android_profiles_dir.c_str(), package_name.c_str()); | 
| Calin Juravle | 6a1648e | 2016-02-01 12:12:16 +0000 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 268 | std::string create_data_dalvik_cache_path() { | 
|  | 269 | return "/data/dalvik-cache"; | 
|  | 270 | } | 
|  | 271 |  | 
| Felka Chang | 2a0a246 | 2019-11-20 14:20:40 +0800 | [diff] [blame] | 272 | std::string create_system_user_ce_path(userid_t userId) { | 
|  | 273 | return StringPrintf("%s/system_ce/%u", create_data_path(nullptr).c_str(), userId); | 
|  | 274 | } | 
|  | 275 |  | 
|  | 276 | std::string create_system_user_ce_package_path(userid_t userId, const char* package_name) { | 
|  | 277 | check_package_name(package_name); | 
|  | 278 | return StringPrintf("%s/%s", create_system_user_ce_path(userId).c_str(), package_name); | 
|  | 279 | } | 
|  | 280 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 281 | // Keep profile paths in sync with ActivityThread and LoadedApk. | 
|  | 282 | const std::string PROFILE_EXT = ".prof"; | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 283 | const std::string CURRENT_PROFILE_EXT = ".cur"; | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 284 | const std::string SNAPSHOT_PROFILE_EXT = ".snapshot"; | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 285 |  | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 286 | // Gets the parent directory and the file name for the given secondary dex path. | 
|  | 287 | // Returns true on success, false on failure (if the dex_path does not have the expected | 
|  | 288 | // structure). | 
|  | 289 | static bool get_secondary_dex_location(const std::string& dex_path, | 
|  | 290 | std::string* out_dir_name, std::string* out_file_name) { | 
|  | 291 | size_t dirIndex = dex_path.rfind('/'); | 
|  | 292 | if (dirIndex == std::string::npos) { | 
|  | 293 | return false; | 
|  | 294 | } | 
|  | 295 | if (dirIndex == dex_path.size() - 1) { | 
|  | 296 | return false; | 
|  | 297 | } | 
|  | 298 | *out_dir_name = dex_path.substr(0, dirIndex); | 
|  | 299 | *out_file_name = dex_path.substr(dirIndex + 1); | 
|  | 300 |  | 
|  | 301 | return true; | 
|  | 302 | } | 
|  | 303 |  | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 304 | std::string create_current_profile_path(userid_t user, const std::string& package_name, | 
|  | 305 | const std::string& location, bool is_secondary_dex) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 306 | if (is_secondary_dex) { | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 307 | // Secondary dex current profiles are stored next to the dex files under the oat folder. | 
|  | 308 | std::string dex_dir; | 
|  | 309 | std::string dex_name; | 
|  | 310 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) | 
|  | 311 | << "Unexpected dir structure for secondary dex " << location; | 
|  | 312 | return StringPrintf("%s/oat/%s%s%s", | 
|  | 313 | dex_dir.c_str(), dex_name.c_str(), CURRENT_PROFILE_EXT.c_str(), | 
|  | 314 | PROFILE_EXT.c_str()); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 315 | } else { | 
|  | 316 | // Profiles for primary apks are under /data/misc/profiles/cur. | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 317 | std::string profile_dir = create_primary_current_profile_package_dir_path( | 
|  | 318 | user, package_name); | 
|  | 319 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 320 | } | 
|  | 321 | } | 
|  | 322 |  | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 323 | std::string create_reference_profile_path(const std::string& package_name, | 
|  | 324 | const std::string& location, bool is_secondary_dex) { | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 325 | if (is_secondary_dex) { | 
|  | 326 | // Secondary dex reference profiles are stored next to the dex files under the oat folder. | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 327 | std::string dex_dir; | 
|  | 328 | std::string dex_name; | 
|  | 329 | CHECK(get_secondary_dex_location(location, &dex_dir, &dex_name)) | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 330 | << "Unexpected dir structure for secondary dex " << location; | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 331 | return StringPrintf("%s/oat/%s%s", | 
|  | 332 | dex_dir.c_str(), dex_name.c_str(), PROFILE_EXT.c_str()); | 
|  | 333 | } else { | 
|  | 334 | // Reference profiles for primary apks are stored in /data/misc/profile/ref. | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 335 | std::string profile_dir = create_primary_reference_profile_package_dir_path(package_name); | 
|  | 336 | return StringPrintf("%s/%s", profile_dir.c_str(), location.c_str()); | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 337 | } | 
| Jeff Sharkey | 90aff26 | 2016-12-12 14:28:24 -0700 | [diff] [blame] | 338 | } | 
|  | 339 |  | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 340 | std::string create_snapshot_profile_path(const std::string& package, | 
| Calin Juravle | cfcd6aa | 2018-01-18 20:23:17 -0800 | [diff] [blame] | 341 | const std::string& profile_name) { | 
|  | 342 | std::string ref_profile = create_reference_profile_path(package, profile_name, | 
|  | 343 | /*is_secondary_dex*/ false); | 
| Calin Juravle | 2959173 | 2017-11-20 17:46:19 -0800 | [diff] [blame] | 344 | return ref_profile + SNAPSHOT_PROFILE_EXT; | 
|  | 345 | } | 
|  | 346 |  | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 347 | std::vector<userid_t> get_known_users(const char* volume_uuid) { | 
|  | 348 | std::vector<userid_t> users; | 
|  | 349 |  | 
|  | 350 | // We always have an owner | 
|  | 351 | users.push_back(0); | 
|  | 352 |  | 
|  | 353 | std::string path(create_data_path(volume_uuid) + "/" + SECONDARY_USER_PREFIX); | 
|  | 354 | DIR* dir = opendir(path.c_str()); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 355 | if (dir == nullptr) { | 
| Jeff Sharkey | e363724 | 2015-04-08 20:56:42 -0700 | [diff] [blame] | 356 | // Unable to discover other users, but at least return owner | 
|  | 357 | PLOG(ERROR) << "Failed to opendir " << path; | 
|  | 358 | return users; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | struct dirent* ent; | 
|  | 362 | while ((ent = readdir(dir))) { | 
|  | 363 | if (ent->d_type != DT_DIR) { | 
|  | 364 | continue; | 
|  | 365 | } | 
|  | 366 |  | 
|  | 367 | char* end; | 
|  | 368 | userid_t user = strtol(ent->d_name, &end, 10); | 
|  | 369 | if (*end == '\0' && user != 0) { | 
|  | 370 | LOG(DEBUG) << "Found valid user " << user; | 
|  | 371 | users.push_back(user); | 
|  | 372 | } | 
|  | 373 | } | 
|  | 374 | closedir(dir); | 
|  | 375 |  | 
|  | 376 | return users; | 
|  | 377 | } | 
|  | 378 |  | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 379 | int calculate_tree_size(const std::string& path, int64_t* size, | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 380 | int32_t include_gid, int32_t exclude_gid, bool exclude_apps) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 381 | FTS *fts; | 
|  | 382 | FTSENT *p; | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 383 | int64_t matchedSize = 0; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 384 | char *argv[] = { (char*) path.c_str(), nullptr }; | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 385 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 386 | if (errno != ENOENT) { | 
|  | 387 | PLOG(ERROR) << "Failed to fts_open " << path; | 
|  | 388 | } | 
|  | 389 | return -1; | 
|  | 390 | } | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 391 | while ((p = fts_read(fts)) != nullptr) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 392 | switch (p->fts_info) { | 
|  | 393 | case FTS_D: | 
|  | 394 | case FTS_DEFAULT: | 
|  | 395 | case FTS_F: | 
|  | 396 | case FTS_SL: | 
|  | 397 | case FTS_SLNONE: | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 398 | int32_t uid = p->fts_statp->st_uid; | 
|  | 399 | int32_t gid = p->fts_statp->st_gid; | 
|  | 400 | int32_t user_uid = multiuser_get_app_id(uid); | 
|  | 401 | int32_t user_gid = multiuser_get_app_id(gid); | 
|  | 402 | if (exclude_apps && ((user_uid >= AID_APP_START && user_uid <= AID_APP_END) | 
|  | 403 | || (user_gid >= AID_CACHE_GID_START && user_gid <= AID_CACHE_GID_END) | 
|  | 404 | || (user_gid >= AID_SHARED_GID_START && user_gid <= AID_SHARED_GID_END))) { | 
|  | 405 | // Don't traverse inside or measure | 
|  | 406 | fts_set(fts, p, FTS_SKIP); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 407 | break; | 
|  | 408 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 409 | if (include_gid != -1 && gid != include_gid) { | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 410 | break; | 
|  | 411 | } | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 412 | if (exclude_gid != -1 && gid == exclude_gid) { | 
|  | 413 | break; | 
|  | 414 | } | 
|  | 415 | matchedSize += (p->fts_statp->st_blocks * 512); | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 416 | break; | 
|  | 417 | } | 
|  | 418 | } | 
|  | 419 | fts_close(fts); | 
| Jeff Sharkey | df2d754 | 2017-01-07 09:19:35 -0700 | [diff] [blame] | 420 | #if MEASURE_DEBUG | 
|  | 421 | if ((include_gid == -1) && (exclude_gid == -1)) { | 
|  | 422 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize; | 
|  | 423 | } else { | 
|  | 424 | LOG(DEBUG) << "Measured " << path << " size " << matchedSize << "; include " << include_gid | 
|  | 425 | << " exclude " << exclude_gid; | 
|  | 426 | } | 
|  | 427 | #endif | 
|  | 428 | *size += matchedSize; | 
| Jeff Sharkey | 3dfae0c | 2016-12-12 17:32:56 -0700 | [diff] [blame] | 429 | return 0; | 
|  | 430 | } | 
|  | 431 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 432 | /** | 
|  | 433 | * Checks whether the package name is valid. Returns -1 on error and | 
|  | 434 | * 0 on success. | 
|  | 435 | */ | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 436 | bool is_valid_package_name(const std::string& packageName) { | 
| Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 437 | // This logic is borrowed from PackageParser.java | 
|  | 438 | bool hasSep = false; | 
|  | 439 | bool front = true; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 440 |  | 
| Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 441 | auto it = packageName.begin(); | 
|  | 442 | for (; it != packageName.end() && *it != '-'; it++) { | 
|  | 443 | char c = *it; | 
|  | 444 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { | 
|  | 445 | front = false; | 
|  | 446 | continue; | 
|  | 447 | } | 
|  | 448 | if (!front) { | 
|  | 449 | if ((c >= '0' && c <= '9') || c == '_') { | 
|  | 450 | continue; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 | if (c == '.') { | 
|  | 454 | hasSep = true; | 
|  | 455 | front = true; | 
|  | 456 | continue; | 
|  | 457 | } | 
|  | 458 | LOG(WARNING) << "Bad package character " << c << " in " << packageName; | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 459 | return false; | 
| Jeff Sharkey | c03de09 | 2015-04-07 18:14:05 -0700 | [diff] [blame] | 460 | } | 
|  | 461 |  | 
| Jeff Sharkey | ab7ac8d | 2017-03-08 12:39:46 -0700 | [diff] [blame] | 462 | if (front) { | 
| Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 463 | LOG(WARNING) << "Missing separator in " << packageName; | 
|  | 464 | return false; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
| Jeff Sharkey | 367ace2 | 2017-03-07 22:12:03 -0700 | [diff] [blame] | 467 | for (; it != packageName.end(); it++) { | 
|  | 468 | char c = *it; | 
|  | 469 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) continue; | 
|  | 470 | if ((c >= '0' && c <= '9') || c == '_' || c == '-' || c == '=') continue; | 
|  | 471 | LOG(WARNING) << "Bad suffix character " << c << " in " << packageName; | 
|  | 472 | return false; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 473 | } | 
|  | 474 |  | 
| Jeff Sharkey | 423e746 | 2016-12-09 18:18:43 -0700 | [diff] [blame] | 475 | return true; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 476 | } | 
|  | 477 |  | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 478 | static int _delete_dir_contents(DIR *d, | 
|  | 479 | int (*exclusion_predicate)(const char *name, const int is_dir)) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 480 | { | 
|  | 481 | int result = 0; | 
|  | 482 | struct dirent *de; | 
|  | 483 | int dfd; | 
|  | 484 |  | 
|  | 485 | dfd = dirfd(d); | 
|  | 486 |  | 
|  | 487 | if (dfd < 0) return -1; | 
|  | 488 |  | 
|  | 489 | while ((de = readdir(d))) { | 
|  | 490 | const char *name = de->d_name; | 
|  | 491 |  | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 492 | /* check using the exclusion predicate, if provided */ | 
|  | 493 | if (exclusion_predicate && exclusion_predicate(name, (de->d_type == DT_DIR))) { | 
|  | 494 | continue; | 
|  | 495 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 496 |  | 
|  | 497 | if (de->d_type == DT_DIR) { | 
| Chih-Hung Hsieh | 99d9fb1 | 2014-09-11 14:44:46 -0700 | [diff] [blame] | 498 | int subfd; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 499 | DIR *subdir; | 
|  | 500 |  | 
|  | 501 | /* always skip "." and ".." */ | 
|  | 502 | if (name[0] == '.') { | 
|  | 503 | if (name[1] == 0) continue; | 
|  | 504 | if ((name[1] == '.') && (name[2] == 0)) continue; | 
|  | 505 | } | 
|  | 506 |  | 
| Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 507 | subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 508 | if (subfd < 0) { | 
|  | 509 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); | 
|  | 510 | result = -1; | 
|  | 511 | continue; | 
|  | 512 | } | 
|  | 513 | subdir = fdopendir(subfd); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 514 | if (subdir == nullptr) { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 515 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); | 
|  | 516 | close(subfd); | 
|  | 517 | result = -1; | 
|  | 518 | continue; | 
|  | 519 | } | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 520 | if (_delete_dir_contents(subdir, exclusion_predicate)) { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 521 | result = -1; | 
|  | 522 | } | 
|  | 523 | closedir(subdir); | 
|  | 524 | if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) { | 
|  | 525 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); | 
|  | 526 | result = -1; | 
|  | 527 | } | 
|  | 528 | } else { | 
|  | 529 | if (unlinkat(dfd, name, 0) < 0) { | 
|  | 530 | ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno)); | 
|  | 531 | result = -1; | 
|  | 532 | } | 
|  | 533 | } | 
|  | 534 | } | 
|  | 535 |  | 
|  | 536 | return result; | 
|  | 537 | } | 
|  | 538 |  | 
| Nikita Ioffe | ad5da1e | 2019-02-04 11:06:37 +0000 | [diff] [blame] | 539 | int create_dir_if_needed(const std::string& pathname, mode_t perms) { | 
|  | 540 | struct stat st; | 
|  | 541 |  | 
|  | 542 | int rc; | 
|  | 543 | if ((rc = stat(pathname.c_str(), &st)) != 0) { | 
|  | 544 | if (errno == ENOENT) { | 
|  | 545 | return mkdir(pathname.c_str(), perms); | 
|  | 546 | } else { | 
|  | 547 | return rc; | 
|  | 548 | } | 
|  | 549 | } else if (!S_ISDIR(st.st_mode)) { | 
|  | 550 | LOG(DEBUG) << pathname << " is not a folder"; | 
|  | 551 | return -1; | 
|  | 552 | } | 
|  | 553 |  | 
|  | 554 | mode_t actual_perms = st.st_mode & ALLPERMS; | 
|  | 555 | if (actual_perms != perms) { | 
|  | 556 | LOG(WARNING) << pathname << " permissions " << actual_perms << " expected " << perms; | 
|  | 557 | return -1; | 
|  | 558 | } | 
|  | 559 |  | 
|  | 560 | return 0; | 
|  | 561 | } | 
|  | 562 |  | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 563 | int delete_dir_contents(const std::string& pathname, bool ignore_if_missing) { | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 564 | return delete_dir_contents(pathname.c_str(), 0, nullptr, ignore_if_missing); | 
| Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 567 | int delete_dir_contents_and_dir(const std::string& pathname, bool ignore_if_missing) { | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 568 | return delete_dir_contents(pathname.c_str(), 1, nullptr, ignore_if_missing); | 
| Jeff Sharkey | ebf728f | 2015-11-18 14:15:17 -0700 | [diff] [blame] | 569 | } | 
|  | 570 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 571 | int delete_dir_contents(const char *pathname, | 
|  | 572 | int also_delete_dir, | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 573 | int (*exclusion_predicate)(const char*, const int), | 
|  | 574 | bool ignore_if_missing) | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 575 | { | 
|  | 576 | int res = 0; | 
|  | 577 | DIR *d; | 
|  | 578 |  | 
|  | 579 | d = opendir(pathname); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 580 | if (d == nullptr) { | 
| Calin Juravle | b06f98a | 2016-03-28 15:11:01 +0100 | [diff] [blame] | 581 | if (ignore_if_missing && (errno == ENOENT)) { | 
|  | 582 | return 0; | 
|  | 583 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 584 | ALOGE("Couldn't opendir %s: %s\n", pathname, strerror(errno)); | 
|  | 585 | return -errno; | 
|  | 586 | } | 
| Narayan Kamath | 3aee2c5 | 2014-06-10 13:16:47 +0100 | [diff] [blame] | 587 | res = _delete_dir_contents(d, exclusion_predicate); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 588 | closedir(d); | 
|  | 589 | if (also_delete_dir) { | 
|  | 590 | if (rmdir(pathname)) { | 
|  | 591 | ALOGE("Couldn't rmdir %s: %s\n", pathname, strerror(errno)); | 
|  | 592 | res = -1; | 
|  | 593 | } | 
|  | 594 | } | 
|  | 595 | return res; | 
|  | 596 | } | 
|  | 597 |  | 
|  | 598 | int delete_dir_contents_fd(int dfd, const char *name) | 
|  | 599 | { | 
|  | 600 | int fd, res; | 
|  | 601 | DIR *d; | 
|  | 602 |  | 
| Nick Kralevich | 8b7acac | 2015-08-10 13:43:00 -0700 | [diff] [blame] | 603 | fd = openat(dfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 604 | if (fd < 0) { | 
|  | 605 | ALOGE("Couldn't openat %s: %s\n", name, strerror(errno)); | 
|  | 606 | return -1; | 
|  | 607 | } | 
|  | 608 | d = fdopendir(fd); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 609 | if (d == nullptr) { | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 610 | ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno)); | 
|  | 611 | close(fd); | 
|  | 612 | return -1; | 
|  | 613 | } | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 614 | res = _delete_dir_contents(d, nullptr); | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 615 | closedir(d); | 
|  | 616 | return res; | 
|  | 617 | } | 
|  | 618 |  | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 619 | static int _copy_owner_permissions(int srcfd, int dstfd) | 
|  | 620 | { | 
|  | 621 | struct stat st; | 
|  | 622 | if (fstat(srcfd, &st) != 0) { | 
|  | 623 | return -1; | 
|  | 624 | } | 
|  | 625 | if (fchmod(dstfd, st.st_mode) != 0) { | 
|  | 626 | return -1; | 
|  | 627 | } | 
|  | 628 | return 0; | 
|  | 629 | } | 
|  | 630 |  | 
|  | 631 | static int _copy_dir_files(int sdfd, int ddfd, uid_t owner, gid_t group) | 
|  | 632 | { | 
|  | 633 | int result = 0; | 
|  | 634 | if (_copy_owner_permissions(sdfd, ddfd) != 0) { | 
|  | 635 | ALOGE("_copy_dir_files failed to copy dir permissions\n"); | 
|  | 636 | } | 
|  | 637 | if (fchown(ddfd, owner, group) != 0) { | 
|  | 638 | ALOGE("_copy_dir_files failed to change dir owner\n"); | 
|  | 639 | } | 
|  | 640 |  | 
|  | 641 | DIR *ds = fdopendir(sdfd); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 642 | if (ds == nullptr) { | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 643 | ALOGE("Couldn't fdopendir: %s\n", strerror(errno)); | 
|  | 644 | return -1; | 
|  | 645 | } | 
|  | 646 | struct dirent *de; | 
|  | 647 | while ((de = readdir(ds))) { | 
|  | 648 | if (de->d_type != DT_REG) { | 
|  | 649 | continue; | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | const char *name = de->d_name; | 
|  | 653 | int fsfd = openat(sdfd, name, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); | 
|  | 654 | int fdfd = openat(ddfd, name, O_WRONLY | O_NOFOLLOW | O_CLOEXEC | O_CREAT, 0600); | 
|  | 655 | if (fsfd == -1 || fdfd == -1) { | 
|  | 656 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); | 
|  | 657 | } else { | 
|  | 658 | if (_copy_owner_permissions(fsfd, fdfd) != 0) { | 
|  | 659 | ALOGE("Failed to change file permissions\n"); | 
|  | 660 | } | 
|  | 661 | if (fchown(fdfd, owner, group) != 0) { | 
|  | 662 | ALOGE("Failed to change file owner\n"); | 
|  | 663 | } | 
|  | 664 |  | 
|  | 665 | char buf[8192]; | 
|  | 666 | ssize_t size; | 
|  | 667 | while ((size = read(fsfd, buf, sizeof(buf))) > 0) { | 
|  | 668 | write(fdfd, buf, size); | 
|  | 669 | } | 
|  | 670 | if (size < 0) { | 
|  | 671 | ALOGW("Couldn't copy %s: %s\n", name, strerror(errno)); | 
|  | 672 | result = -1; | 
|  | 673 | } | 
|  | 674 | } | 
|  | 675 | close(fdfd); | 
|  | 676 | close(fsfd); | 
|  | 677 | } | 
|  | 678 |  | 
|  | 679 | return result; | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | int copy_dir_files(const char *srcname, | 
|  | 683 | const char *dstname, | 
|  | 684 | uid_t owner, | 
|  | 685 | uid_t group) | 
|  | 686 | { | 
|  | 687 | int res = 0; | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 688 | DIR *ds = nullptr; | 
|  | 689 | DIR *dd = nullptr; | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 690 |  | 
|  | 691 | ds = opendir(srcname); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 692 | if (ds == nullptr) { | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 693 | ALOGE("Couldn't opendir %s: %s\n", srcname, strerror(errno)); | 
|  | 694 | return -errno; | 
|  | 695 | } | 
|  | 696 |  | 
|  | 697 | mkdir(dstname, 0600); | 
|  | 698 | dd = opendir(dstname); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 699 | if (dd == nullptr) { | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 700 | ALOGE("Couldn't opendir %s: %s\n", dstname, strerror(errno)); | 
|  | 701 | closedir(ds); | 
|  | 702 | return -errno; | 
|  | 703 | } | 
|  | 704 |  | 
|  | 705 | int sdfd = dirfd(ds); | 
|  | 706 | int ddfd = dirfd(dd); | 
|  | 707 | if (sdfd != -1 && ddfd != -1) { | 
|  | 708 | res = _copy_dir_files(sdfd, ddfd, owner, group); | 
|  | 709 | } else { | 
|  | 710 | res = -errno; | 
|  | 711 | } | 
|  | 712 | closedir(dd); | 
|  | 713 | closedir(ds); | 
|  | 714 | return res; | 
|  | 715 | } | 
|  | 716 |  | 
| Jeff Sharkey | a836c47 | 2017-04-02 23:29:30 -0600 | [diff] [blame] | 717 | int64_t data_disk_free(const std::string& data_path) { | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 718 | struct statvfs sfs; | 
|  | 719 | if (statvfs(data_path.c_str(), &sfs) == 0) { | 
| Jeff Sharkey | 4f7be17 | 2017-08-11 15:13:31 -0600 | [diff] [blame] | 720 | return static_cast<int64_t>(sfs.f_bavail) * sfs.f_frsize; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 721 | } else { | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 722 | PLOG(ERROR) << "Couldn't statvfs " << data_path; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 723 | return -1; | 
|  | 724 | } | 
|  | 725 | } | 
|  | 726 |  | 
| Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 727 | int get_path_inode(const std::string& path, ino_t *inode) { | 
|  | 728 | struct stat buf; | 
|  | 729 | memset(&buf, 0, sizeof(buf)); | 
|  | 730 | if (stat(path.c_str(), &buf) != 0) { | 
|  | 731 | PLOG(WARNING) << "Failed to stat " << path; | 
|  | 732 | return -1; | 
|  | 733 | } else { | 
|  | 734 | *inode = buf.st_ino; | 
|  | 735 | return 0; | 
|  | 736 | } | 
|  | 737 | } | 
|  | 738 |  | 
|  | 739 | /** | 
|  | 740 | * Write the inode of a specific child file into the given xattr on the | 
|  | 741 | * parent directory. This allows you to find the child later, even if its | 
|  | 742 | * name is encrypted. | 
|  | 743 | */ | 
|  | 744 | int write_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { | 
|  | 745 | ino_t inode = 0; | 
|  | 746 | uint64_t inode_raw = 0; | 
|  | 747 | auto path = StringPrintf("%s/%s", parent.c_str(), name); | 
|  | 748 |  | 
|  | 749 | if (get_path_inode(path, &inode) != 0) { | 
|  | 750 | // Path probably doesn't exist yet; ignore | 
|  | 751 | return 0; | 
|  | 752 | } | 
|  | 753 |  | 
|  | 754 | // Check to see if already set correctly | 
|  | 755 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { | 
|  | 756 | if (inode_raw == inode) { | 
|  | 757 | // Already set correctly; skip writing | 
|  | 758 | return 0; | 
|  | 759 | } else { | 
|  | 760 | PLOG(WARNING) << "Mismatched inode value; found " << inode | 
|  | 761 | << " on disk but marked value was " << inode_raw << "; overwriting"; | 
|  | 762 | } | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | inode_raw = inode; | 
| Jeff Sharkey | 4ed6507 | 2016-07-22 11:38:54 -0600 | [diff] [blame] | 766 | if (setxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw), 0) != 0 && errno != EOPNOTSUPP) { | 
| Jeff Sharkey | 9a998f4 | 2016-07-14 18:16:22 -0600 | [diff] [blame] | 767 | PLOG(ERROR) << "Failed to write xattr " << inode_xattr << " at " << parent; | 
|  | 768 | return -1; | 
|  | 769 | } else { | 
|  | 770 | return 0; | 
|  | 771 | } | 
|  | 772 | } | 
|  | 773 |  | 
|  | 774 | /** | 
|  | 775 | * Read the inode of a specific child file from the given xattr on the | 
|  | 776 | * parent directory. Returns a currently valid path for that child, which | 
|  | 777 | * might have an encrypted name. | 
|  | 778 | */ | 
|  | 779 | std::string read_path_inode(const std::string& parent, const char* name, const char* inode_xattr) { | 
|  | 780 | ino_t inode = 0; | 
|  | 781 | uint64_t inode_raw = 0; | 
|  | 782 | auto fallback = StringPrintf("%s/%s", parent.c_str(), name); | 
|  | 783 |  | 
|  | 784 | // Lookup the inode value written earlier | 
|  | 785 | if (getxattr(parent.c_str(), inode_xattr, &inode_raw, sizeof(inode_raw)) == sizeof(inode_raw)) { | 
|  | 786 | inode = inode_raw; | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | // For testing purposes, rely on the inode when defined; this could be | 
|  | 790 | // optimized to use access() in the future. | 
|  | 791 | if (inode != 0) { | 
|  | 792 | DIR* dir = opendir(parent.c_str()); | 
|  | 793 | if (dir == nullptr) { | 
|  | 794 | PLOG(ERROR) << "Failed to opendir " << parent; | 
|  | 795 | return fallback; | 
|  | 796 | } | 
|  | 797 |  | 
|  | 798 | struct dirent* ent; | 
|  | 799 | while ((ent = readdir(dir))) { | 
|  | 800 | if (ent->d_ino == inode) { | 
|  | 801 | auto resolved = StringPrintf("%s/%s", parent.c_str(), ent->d_name); | 
|  | 802 | #if DEBUG_XATTRS | 
|  | 803 | if (resolved != fallback) { | 
|  | 804 | LOG(DEBUG) << "Resolved path " << resolved << " for inode " << inode | 
|  | 805 | << " instead of " << fallback; | 
|  | 806 | } | 
|  | 807 | #endif | 
|  | 808 | closedir(dir); | 
|  | 809 | return resolved; | 
|  | 810 | } | 
|  | 811 | } | 
|  | 812 | LOG(WARNING) << "Failed to resolve inode " << inode << "; using " << fallback; | 
|  | 813 | closedir(dir); | 
|  | 814 | return fallback; | 
|  | 815 | } else { | 
|  | 816 | return fallback; | 
|  | 817 | } | 
|  | 818 | } | 
|  | 819 |  | 
| Ryuki Nakamura | c7342f8 | 2017-09-30 11:57:00 +0900 | [diff] [blame] | 820 | void remove_path_xattr(const std::string& path, const char* inode_xattr) { | 
|  | 821 | if (removexattr(path.c_str(), inode_xattr) && errno != ENODATA) { | 
|  | 822 | PLOG(ERROR) << "Failed to remove xattr " << inode_xattr << " at " << path; | 
|  | 823 | } | 
|  | 824 | } | 
|  | 825 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 826 | /** | 
| Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 827 | * Validate that the path is valid in the context of the provided directory. | 
|  | 828 | * The path is allowed to have at most one subdirectory and no indirections | 
|  | 829 | * to top level directories (i.e. have ".."). | 
|  | 830 | */ | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 831 | static int validate_path(const std::string& dir, const std::string& path, int maxSubdirs) { | 
|  | 832 | // Argument sanity checking | 
|  | 833 | if (dir.find('/') != 0 || dir.rfind('/') != dir.size() - 1 | 
|  | 834 | || dir.find("..") != std::string::npos) { | 
|  | 835 | LOG(ERROR) << "Invalid directory " << dir; | 
|  | 836 | return -1; | 
|  | 837 | } | 
|  | 838 | if (path.find("..") != std::string::npos) { | 
|  | 839 | LOG(ERROR) << "Invalid path " << path; | 
|  | 840 | return -1; | 
| Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 841 | } | 
|  | 842 |  | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 843 | if (path.compare(0, dir.size(), dir) != 0) { | 
|  | 844 | // Common case, path isn't under directory | 
|  | 845 | return -1; | 
|  | 846 | } | 
|  | 847 |  | 
|  | 848 | // Count number of subdirectories | 
|  | 849 | auto pos = path.find('/', dir.size()); | 
|  | 850 | int count = 0; | 
|  | 851 | while (pos != std::string::npos) { | 
| Jeff Sharkey | 172fac0 | 2017-10-06 13:09:46 -0600 | [diff] [blame] | 852 | auto next = path.find('/', pos + 1); | 
|  | 853 | if (next > pos + 1) { | 
|  | 854 | count++; | 
|  | 855 | } | 
|  | 856 | pos = next; | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 857 | } | 
|  | 858 |  | 
|  | 859 | if (count > maxSubdirs) { | 
|  | 860 | LOG(ERROR) << "Invalid path depth " << path << " when tested against " << dir; | 
| Calin Juravle | c597b6d | 2014-08-19 17:43:05 +0100 | [diff] [blame] | 861 | return -1; | 
|  | 862 | } | 
|  | 863 |  | 
|  | 864 | return 0; | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | /** | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 868 | * Checks whether a path points to a system app (.apk file). Returns 0 | 
|  | 869 | * if it is a system app or -1 if it is not. | 
|  | 870 | */ | 
|  | 871 | int validate_system_app_path(const char* path) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 872 | std::string path_ = path; | 
|  | 873 | for (const auto& dir : android_system_dirs) { | 
|  | 874 | if (validate_path(dir, path, 1) == 0) { | 
|  | 875 | return 0; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 876 | } | 
|  | 877 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 878 | return -1; | 
|  | 879 | } | 
|  | 880 |  | 
| Calin Juravle | 114f081 | 2017-03-08 19:05:07 -0800 | [diff] [blame] | 881 | bool validate_secondary_dex_path(const std::string& pkgname, const std::string& dex_path, | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 882 | const char* volume_uuid, int uid, int storage_flag) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 883 | CHECK(storage_flag == FLAG_STORAGE_CE || storage_flag == FLAG_STORAGE_DE); | 
|  | 884 |  | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 885 | // Empty paths are not allowed. | 
|  | 886 | if (dex_path.empty()) { return false; } | 
|  | 887 | // First character should always be '/'. No relative paths. | 
|  | 888 | if (dex_path[0] != '/') { return false; } | 
|  | 889 | // The last character should not be '/'. | 
|  | 890 | if (dex_path[dex_path.size() - 1] == '/') { return false; } | 
|  | 891 | // There should be no '.' after the directory marker. | 
|  | 892 | if (dex_path.find("/.") != std::string::npos) { return false; } | 
|  | 893 | // The path should be at most PKG_PATH_MAX long. | 
|  | 894 | if (dex_path.size() > PKG_PATH_MAX) { return false; } | 
|  | 895 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 896 | // The dex_path should be under the app data directory. | 
|  | 897 | std::string app_private_dir = storage_flag == FLAG_STORAGE_CE | 
| Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 898 | ? create_data_user_ce_package_path( | 
|  | 899 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()) | 
|  | 900 | : create_data_user_de_package_path( | 
|  | 901 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 902 |  | 
| Calin Juravle | 7d76546 | 2017-09-04 15:57:10 -0700 | [diff] [blame] | 903 | if (strncmp(dex_path.c_str(), app_private_dir.c_str(), app_private_dir.size()) != 0) { | 
|  | 904 | // The check above might fail if the dex file is accessed via the /data/user/0 symlink. | 
|  | 905 | // If that's the case, attempt to validate against the user data link. | 
|  | 906 | std::string app_private_dir_symlink = create_data_user_ce_package_path_as_user_link( | 
|  | 907 | volume_uuid, multiuser_get_user_id(uid), pkgname.c_str()); | 
|  | 908 | if (strncmp(dex_path.c_str(), app_private_dir_symlink.c_str(), | 
|  | 909 | app_private_dir_symlink.size()) != 0) { | 
| Calin Juravle | dd42e27 | 2017-09-11 11:50:36 -0700 | [diff] [blame] | 910 | return false; | 
|  | 911 | } | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 912 | } | 
| Calin Juravle | 3760ad3 | 2017-07-27 16:31:55 -0700 | [diff] [blame] | 913 |  | 
|  | 914 | // If we got here we have a valid path. | 
|  | 915 | return true; | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 916 | } | 
|  | 917 |  | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 918 | /** | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 919 | * Check whether path points to a valid path for an APK file. The path must | 
|  | 920 | * begin with a whitelisted prefix path and must be no deeper than |maxSubdirs| within | 
|  | 921 | * that path. Returns -1 when an invalid path is encountered and 0 when a valid path | 
|  | 922 | * is encountered. | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 923 | */ | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 924 | static int validate_apk_path_internal(const std::string& path, int maxSubdirs) { | 
|  | 925 | if (validate_path(android_app_dir, path, maxSubdirs) == 0) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 926 | return 0; | 
| shafik | b43faa9 | 2019-02-19 12:19:48 +0000 | [diff] [blame] | 927 | } else if (validate_path(android_staging_dir, path, maxSubdirs) == 0) { | 
|  | 928 | return 0; | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 929 | } else if (validate_path(android_app_private_dir, path, maxSubdirs) == 0) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 930 | return 0; | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 931 | } else if (validate_path(android_app_ephemeral_dir, path, maxSubdirs) == 0) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 932 | return 0; | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 933 | } else if (validate_path(android_asec_dir, path, maxSubdirs) == 0) { | 
| Jeff Sharkey | c1149c9 | 2017-09-21 14:51:09 -0600 | [diff] [blame] | 934 | return 0; | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 935 | } else if (android::base::StartsWith(path, android_mnt_expand_dir)) { | 
|  | 936 | // Rewrite the path as if it were on internal storage, and test that | 
|  | 937 | size_t end = path.find('/', android_mnt_expand_dir.size() + 1); | 
|  | 938 | if (end != std::string::npos) { | 
|  | 939 | auto modified = path; | 
|  | 940 | modified.replace(0, end + 1, android_data_dir); | 
|  | 941 | return validate_apk_path_internal(modified, maxSubdirs); | 
|  | 942 | } | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 943 | } | 
| Jeff Sharkey | 8fa803a | 2018-04-09 18:46:45 -0600 | [diff] [blame] | 944 | return -1; | 
| Mike Lockwood | 94afecf | 2012-10-24 10:45:23 -0700 | [diff] [blame] | 945 | } | 
|  | 946 |  | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 947 | int validate_apk_path(const char* path) { | 
| Songchun Fan | 2955609 | 2020-01-23 14:51:45 -0800 | [diff] [blame] | 948 | return validate_apk_path_internal(path, 2 /* maxSubdirs */); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 949 | } | 
|  | 950 |  | 
|  | 951 | int validate_apk_path_subdirs(const char* path) { | 
| Songchun Fan | 2955609 | 2020-01-23 14:51:45 -0800 | [diff] [blame] | 952 | return validate_apk_path_internal(path, 4 /* maxSubdirs */); | 
| Narayan Kamath | d845c96 | 2015-06-04 13:20:27 +0100 | [diff] [blame] | 953 | } | 
|  | 954 |  | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 955 | int ensure_config_user_dirs(userid_t userid) { | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 956 | // writable by system, readable by any app within the same user | 
| Robin Lee | 60fd3fe | 2014-10-07 16:55:02 +0100 | [diff] [blame] | 957 | const int uid = multiuser_get_uid(userid, AID_SYSTEM); | 
|  | 958 | const int gid = multiuser_get_uid(userid, AID_EVERYBODY); | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 959 |  | 
|  | 960 | // Ensure /data/misc/user/<userid> exists | 
| Jeff Sharkey | 379a12b | 2016-04-14 20:45:06 -0600 | [diff] [blame] | 961 | auto path = create_data_misc_legacy_path(userid); | 
|  | 962 | return fs_prepare_dir(path.c_str(), 0750, uid, gid); | 
| Robin Lee | 095c763 | 2014-04-25 15:05:19 +0100 | [diff] [blame] | 963 | } | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 964 |  | 
|  | 965 | int wait_child(pid_t pid) | 
|  | 966 | { | 
|  | 967 | int status; | 
|  | 968 | pid_t got_pid; | 
|  | 969 |  | 
|  | 970 | while (1) { | 
|  | 971 | got_pid = waitpid(pid, &status, 0); | 
|  | 972 | if (got_pid == -1 && errno == EINTR) { | 
|  | 973 | printf("waitpid interrupted, retrying\n"); | 
|  | 974 | } else { | 
|  | 975 | break; | 
|  | 976 | } | 
|  | 977 | } | 
|  | 978 | if (got_pid != pid) { | 
|  | 979 | ALOGW("waitpid failed: wanted %d, got %d: %s\n", | 
|  | 980 | (int) pid, (int) got_pid, strerror(errno)); | 
|  | 981 | return 1; | 
|  | 982 | } | 
|  | 983 |  | 
|  | 984 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | 
|  | 985 | return 0; | 
|  | 986 | } else { | 
|  | 987 | return status;      /* always nonzero */ | 
|  | 988 | } | 
|  | 989 | } | 
|  | 990 |  | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 991 | /** | 
|  | 992 | * Prepare an app cache directory, which offers to fix-up the GID and | 
|  | 993 | * directory mode flags during a platform upgrade. | 
|  | 994 | * The app cache directory path will be 'parent'/'name'. | 
|  | 995 | */ | 
|  | 996 | int prepare_app_cache_dir(const std::string& parent, const char* name, mode_t target_mode, | 
|  | 997 | uid_t uid, gid_t gid) { | 
|  | 998 | auto path = StringPrintf("%s/%s", parent.c_str(), name); | 
|  | 999 | struct stat st; | 
|  | 1000 | if (stat(path.c_str(), &st) != 0) { | 
|  | 1001 | if (errno == ENOENT) { | 
|  | 1002 | // This is fine, just create it | 
|  | 1003 | if (fs_prepare_dir_strict(path.c_str(), target_mode, uid, gid) != 0) { | 
|  | 1004 | PLOG(ERROR) << "Failed to prepare " << path; | 
|  | 1005 | return -1; | 
|  | 1006 | } else { | 
|  | 1007 | return 0; | 
|  | 1008 | } | 
|  | 1009 | } else { | 
|  | 1010 | PLOG(ERROR) << "Failed to stat " << path; | 
|  | 1011 | return -1; | 
|  | 1012 | } | 
|  | 1013 | } | 
|  | 1014 |  | 
|  | 1015 | mode_t actual_mode = st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO | S_ISGID); | 
|  | 1016 | if (st.st_uid != uid) { | 
|  | 1017 | // Mismatched UID is real trouble; we can't recover | 
|  | 1018 | LOG(ERROR) << "Mismatched UID at " << path << ": found " << st.st_uid | 
|  | 1019 | << " but expected " << uid; | 
|  | 1020 | return -1; | 
|  | 1021 | } else if (st.st_gid == gid && actual_mode == target_mode) { | 
|  | 1022 | // Everything looks good! | 
|  | 1023 | return 0; | 
| Jeff Sharkey | e59c85c | 2017-04-02 21:53:14 -0600 | [diff] [blame] | 1024 | } else { | 
|  | 1025 | // Mismatched GID/mode is recoverable; fall through to update | 
|  | 1026 | LOG(DEBUG) << "Mismatched cache GID/mode at " << path << ": found " << st.st_gid | 
| Shubham Ajmera | ec0afbf | 2017-09-14 11:07:33 -0700 | [diff] [blame] | 1027 | << "/" << actual_mode << " but expected " << gid << "/" << target_mode; | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1028 | } | 
|  | 1029 |  | 
|  | 1030 | // Directory is owned correctly, but GID or mode mismatch means it's | 
|  | 1031 | // probably a platform upgrade so we need to fix them | 
|  | 1032 | FTS *fts; | 
|  | 1033 | FTSENT *p; | 
|  | 1034 | char *argv[] = { (char*) path.c_str(), nullptr }; | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1035 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1036 | PLOG(ERROR) << "Failed to fts_open " << path; | 
|  | 1037 | return -1; | 
|  | 1038 | } | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1039 | while ((p = fts_read(fts)) != nullptr) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1040 | switch (p->fts_info) { | 
|  | 1041 | case FTS_DP: | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1042 | if (chmod(p->fts_path, target_mode) != 0) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1043 | PLOG(WARNING) << "Failed to chmod " << p->fts_path; | 
|  | 1044 | } | 
| Chih-Hung Hsieh | f1dd98e | 2018-10-16 14:17:11 -0700 | [diff] [blame] | 1045 | [[fallthrough]]; // to also set GID | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1046 | case FTS_F: | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1047 | if (chown(p->fts_path, -1, gid) != 0) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1048 | PLOG(WARNING) << "Failed to chown " << p->fts_path; | 
|  | 1049 | } | 
|  | 1050 | break; | 
|  | 1051 | case FTS_SL: | 
|  | 1052 | case FTS_SLNONE: | 
| Jeff Sharkey | e12d596 | 2017-04-03 16:41:02 -0600 | [diff] [blame] | 1053 | if (lchown(p->fts_path, -1, gid) != 0) { | 
| Calin Juravle | 42451c0 | 2017-01-17 14:43:25 -0800 | [diff] [blame] | 1054 | PLOG(WARNING) << "Failed to chown " << p->fts_path; | 
|  | 1055 | } | 
|  | 1056 | break; | 
|  | 1057 | } | 
|  | 1058 | } | 
|  | 1059 | fts_close(fts); | 
|  | 1060 | return 0; | 
|  | 1061 | } | 
|  | 1062 |  | 
| Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 1063 | static const char* kProcFilesystems = "/proc/filesystems"; | 
|  | 1064 | bool supports_sdcardfs() { | 
| Daniel Rosenberg | f618443 | 2020-07-23 00:01:23 -0700 | [diff] [blame] | 1065 | if (!property_get_bool("external_storage.sdcardfs.enabled", true)) | 
|  | 1066 | return false; | 
| Martijn Coenen | 771cc34 | 2020-02-19 23:26:56 +0100 | [diff] [blame] | 1067 | std::string supported; | 
|  | 1068 | if (!android::base::ReadFileToString(kProcFilesystems, &supported)) { | 
|  | 1069 | PLOG(ERROR) << "Failed to read supported filesystems"; | 
|  | 1070 | return false; | 
|  | 1071 | } | 
|  | 1072 | return supported.find("sdcardfs\n") != std::string::npos; | 
|  | 1073 | } | 
|  | 1074 |  | 
|  | 1075 | int64_t get_occupied_app_space_external(const std::string& uuid, int32_t userId, int32_t appId) { | 
|  | 1076 | static const bool supportsSdcardFs = supports_sdcardfs(); | 
|  | 1077 |  | 
|  | 1078 | if (supportsSdcardFs) { | 
|  | 1079 | int extGid = multiuser_get_ext_gid(userId, appId); | 
|  | 1080 |  | 
|  | 1081 | if (extGid == -1) { | 
|  | 1082 | return -1; | 
|  | 1083 | } | 
|  | 1084 |  | 
|  | 1085 | return GetOccupiedSpaceForGid(uuid, extGid); | 
|  | 1086 | } else { | 
|  | 1087 | uid_t uid = multiuser_get_uid(userId, appId); | 
|  | 1088 | long projectId = uid - AID_APP_START + PROJECT_ID_EXT_DATA_START; | 
|  | 1089 | return GetOccupiedSpaceForProjectId(uuid, projectId); | 
|  | 1090 | } | 
|  | 1091 | } | 
|  | 1092 | int64_t get_occupied_app_cache_space_external(const std::string& uuid, int32_t userId, int32_t appId) { | 
|  | 1093 | static const bool supportsSdcardFs = supports_sdcardfs(); | 
|  | 1094 |  | 
|  | 1095 | if (supportsSdcardFs) { | 
|  | 1096 | int extCacheGid = multiuser_get_ext_cache_gid(userId, appId); | 
|  | 1097 |  | 
|  | 1098 | if (extCacheGid == -1) { | 
|  | 1099 | return -1; | 
|  | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | return GetOccupiedSpaceForGid(uuid, extCacheGid); | 
|  | 1103 | } else { | 
|  | 1104 | uid_t uid = multiuser_get_uid(userId, appId); | 
|  | 1105 | long projectId = uid - AID_APP_START + PROJECT_ID_EXT_CACHE_START; | 
|  | 1106 | return GetOccupiedSpaceForProjectId(uuid, projectId); | 
|  | 1107 | } | 
|  | 1108 | } | 
|  | 1109 |  | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1110 | // Collect all non empty profiles from the given directory and puts then into profile_paths. | 
|  | 1111 | // The profiles are identified based on PROFILE_EXT extension. | 
|  | 1112 | // If a subdirectory or profile file cannot be opened the method logs a warning and moves on. | 
|  | 1113 | // It returns true if there were no errors at all, and false otherwise. | 
|  | 1114 | static bool collect_profiles(DIR* d, | 
|  | 1115 | const std::string& current_path, | 
|  | 1116 | std::vector<std::string>* profiles_paths) { | 
|  | 1117 | int32_t dir_fd = dirfd(d); | 
|  | 1118 | if (dir_fd < 0) { | 
|  | 1119 | return false; | 
|  | 1120 | } | 
|  | 1121 |  | 
|  | 1122 | bool result = true; | 
|  | 1123 | struct dirent* dir_entry; | 
|  | 1124 | while ((dir_entry = readdir(d))) { | 
|  | 1125 | std::string name = dir_entry->d_name; | 
|  | 1126 | std::string local_path = current_path + "/" + name; | 
|  | 1127 |  | 
|  | 1128 | if (dir_entry->d_type == DT_REG) { | 
|  | 1129 | // Check if this is a non empty profile file. | 
|  | 1130 | if (EndsWith(name, PROFILE_EXT)) { | 
|  | 1131 | struct stat st; | 
|  | 1132 | if (stat(local_path.c_str(), &st) != 0) { | 
|  | 1133 | PLOG(WARNING) << "Cannot stat local path " << local_path; | 
|  | 1134 | result = false; | 
|  | 1135 | continue; | 
|  | 1136 | } else if (st.st_size > 0) { | 
|  | 1137 | profiles_paths->push_back(local_path); | 
|  | 1138 | } | 
|  | 1139 | } | 
|  | 1140 | } else if (dir_entry->d_type == DT_DIR) { | 
|  | 1141 | // always skip "." and ".." | 
|  | 1142 | if (name == "." || name == "..") { | 
|  | 1143 | continue; | 
|  | 1144 | } | 
|  | 1145 |  | 
|  | 1146 | unique_fd subdir_fd(openat(dir_fd, name.c_str(), | 
|  | 1147 | O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC)); | 
|  | 1148 | if (subdir_fd < 0) { | 
|  | 1149 | PLOG(WARNING) << "Could not open dir path " << local_path; | 
|  | 1150 | result = false; | 
|  | 1151 | continue; | 
|  | 1152 | } | 
|  | 1153 |  | 
| Mathieu Chartier | 89883e3 | 2018-11-01 11:39:00 -0700 | [diff] [blame] | 1154 | DIR* subdir = Fdopendir(std::move(subdir_fd)); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1155 | if (subdir == nullptr) { | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1156 | PLOG(WARNING) << "Could not open dir path " << local_path; | 
|  | 1157 | result = false; | 
|  | 1158 | continue; | 
|  | 1159 | } | 
|  | 1160 | bool new_result = collect_profiles(subdir, local_path, profiles_paths); | 
|  | 1161 | result = result && new_result; | 
|  | 1162 | if (closedir(subdir) != 0) { | 
|  | 1163 | PLOG(WARNING) << "Could not close dir path " << local_path; | 
|  | 1164 | } | 
|  | 1165 | } | 
|  | 1166 | } | 
|  | 1167 |  | 
|  | 1168 | return result; | 
|  | 1169 | } | 
|  | 1170 |  | 
|  | 1171 | bool collect_profiles(std::vector<std::string>* profiles_paths) { | 
|  | 1172 | DIR* d = opendir(android_profiles_dir.c_str()); | 
| Yi Kong | 954cf64 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 1173 | if (d == nullptr) { | 
| Calin Juravle | e61189e | 2018-01-23 19:54:11 -0800 | [diff] [blame] | 1174 | return false; | 
|  | 1175 | } else { | 
|  | 1176 | return collect_profiles(d, android_profiles_dir, profiles_paths); | 
|  | 1177 | } | 
|  | 1178 | } | 
|  | 1179 |  | 
| Eric Holk | 2af5e6a | 2019-01-09 18:17:27 -0800 | [diff] [blame] | 1180 | void drop_capabilities(uid_t uid) { | 
|  | 1181 | if (setgid(uid) != 0) { | 
|  | 1182 | PLOG(ERROR) << "setgid(" << uid << ") failed in installd during dexopt"; | 
|  | 1183 | exit(DexoptReturnCodes::kSetGid); | 
|  | 1184 | } | 
|  | 1185 | if (setuid(uid) != 0) { | 
|  | 1186 | PLOG(ERROR) << "setuid(" << uid << ") failed in installd during dexopt"; | 
|  | 1187 | exit(DexoptReturnCodes::kSetUid); | 
|  | 1188 | } | 
|  | 1189 | // drop capabilities | 
|  | 1190 | struct __user_cap_header_struct capheader; | 
|  | 1191 | struct __user_cap_data_struct capdata[2]; | 
|  | 1192 | memset(&capheader, 0, sizeof(capheader)); | 
|  | 1193 | memset(&capdata, 0, sizeof(capdata)); | 
|  | 1194 | capheader.version = _LINUX_CAPABILITY_VERSION_3; | 
|  | 1195 | if (capset(&capheader, &capdata[0]) < 0) { | 
|  | 1196 | PLOG(ERROR) << "capset failed"; | 
|  | 1197 | exit(DexoptReturnCodes::kCapSet); | 
|  | 1198 | } | 
|  | 1199 | } | 
|  | 1200 |  | 
| Andreas Gampe | 02d0de5 | 2015-11-11 20:43:16 -0800 | [diff] [blame] | 1201 | }  // namespace installd | 
|  | 1202 | }  // namespace android |