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