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