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