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