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