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