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