Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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 |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Utils.h" |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/stringprintf.h> |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/mount.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/wait.h> |
| 30 | #include <sys/statvfs.h> |
| 31 | |
| 32 | #include <selinux/android.h> |
| 33 | |
| 34 | using android::base::ReadFileToString; |
| 35 | using android::base::StringPrintf; |
| 36 | |
| 37 | namespace android { |
| 38 | namespace vold { |
| 39 | |
| 40 | static const char* kKeyPath = "/data/misc/vold"; |
| 41 | |
| 42 | status_t ForkExecvp(const std::vector<std::string>& args) { |
| 43 | return ForkExecvp(args, nullptr); |
| 44 | } |
| 45 | |
| 46 | status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) { |
| 47 | size_t argc = args.size(); |
| 48 | char** argv = (char**) calloc(argc, sizeof(char*)); |
| 49 | for (size_t i = 0; i < argc; i++) { |
| 50 | argv[i] = (char*) args[i].c_str(); |
| 51 | if (i == 0) { |
| 52 | LOG(VERBOSE) << args[i]; |
| 53 | } else { |
| 54 | LOG(VERBOSE) << " " << args[i]; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | if (setexeccon(context)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 59 | LOG(ERROR) << "Failed to setexeccon" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 60 | abort(); |
| 61 | } |
| 62 | abort(); |
| 63 | status_t res = 1;//android_fork_execvp(argc, argv, NULL, false, true); |
| 64 | if (setexeccon(nullptr)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 65 | LOG(ERROR) << "Failed to setexeccon" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 66 | abort(); |
| 67 | } |
| 68 | |
| 69 | free(argv); |
| 70 | return res; |
| 71 | } |
| 72 | |
| 73 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 74 | std::vector<std::string>& output) { |
| 75 | return ForkExecvp(args, output, nullptr); |
| 76 | } |
| 77 | |
| 78 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 79 | std::vector<std::string>& output, security_context_t context) { |
| 80 | std::string cmd; |
| 81 | for (size_t i = 0; i < args.size(); i++) { |
| 82 | cmd += args[i] + " "; |
| 83 | if (i == 0) { |
| 84 | LOG(VERBOSE) << args[i]; |
| 85 | } else { |
| 86 | LOG(VERBOSE) << " " << args[i]; |
| 87 | } |
| 88 | } |
| 89 | output.clear(); |
| 90 | |
| 91 | if (setexeccon(context)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 92 | LOG(ERROR) << "Failed to setexeccon" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 93 | abort(); |
| 94 | } |
| 95 | FILE* fp = popen(cmd.c_str(), "r"); |
| 96 | if (setexeccon(nullptr)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 97 | LOG(ERROR) << "Failed to setexeccon" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 98 | abort(); |
| 99 | } |
| 100 | |
| 101 | if (!fp) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 102 | PLOG(ERROR) << "Failed to popen " << cmd << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 103 | return -errno; |
| 104 | } |
| 105 | char line[1024]; |
| 106 | while (fgets(line, sizeof(line), fp) != nullptr) { |
| 107 | LOG(VERBOSE) << line; |
| 108 | output.push_back(std::string(line)); |
| 109 | } |
| 110 | if (pclose(fp) != 0) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 111 | PLOG(ERROR) << "Failed to pclose " << cmd << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 112 | return -errno; |
| 113 | } |
| 114 | |
| 115 | return OK; |
| 116 | } |
| 117 | |
| 118 | pid_t ForkExecvpAsync(const std::vector<std::string>& args) { |
| 119 | size_t argc = args.size(); |
| 120 | char** argv = (char**) calloc(argc + 1, sizeof(char*)); |
| 121 | for (size_t i = 0; i < argc; i++) { |
| 122 | argv[i] = (char*) args[i].c_str(); |
| 123 | if (i == 0) { |
| 124 | LOG(VERBOSE) << args[i]; |
| 125 | } else { |
| 126 | LOG(VERBOSE) << " " << args[i]; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pid_t pid = fork(); |
| 131 | if (pid == 0) { |
| 132 | close(STDIN_FILENO); |
| 133 | close(STDOUT_FILENO); |
| 134 | close(STDERR_FILENO); |
| 135 | |
| 136 | if (execvp(argv[0], argv)) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 137 | PLOG(ERROR) << "Failed to exec" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | _exit(1); |
| 141 | } |
| 142 | |
| 143 | if (pid == -1) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 144 | PLOG(ERROR) << "Failed to exec" << std::endl; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | free(argv); |
| 148 | return pid; |
| 149 | } |
| 150 | |
| 151 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 152 | out.resize(bytes); |
| 153 | return ReadRandomBytes(bytes, &out[0]); |
| 154 | } |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 155 | |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 156 | status_t ReadRandomBytes(size_t bytes, char* buf) { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 157 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 158 | if (fd == -1) { |
| 159 | return -errno; |
| 160 | } |
| 161 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 162 | size_t n; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 163 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], bytes))) > 0) { |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 164 | bytes -= n; |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 165 | buf += n; |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 166 | } |
| 167 | close(fd); |
| 168 | |
| 169 | if (bytes == 0) { |
| 170 | return OK; |
| 171 | } else { |
| 172 | return -EIO; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 177 | str.clear(); |
| 178 | bool even = true; |
| 179 | char cur = 0; |
| 180 | for (size_t i = 0; i < hex.size(); i++) { |
| 181 | int val = 0; |
| 182 | switch (hex[i]) { |
| 183 | case ' ': case '-': case ':': continue; |
| 184 | case 'f': case 'F': val = 15; break; |
| 185 | case 'e': case 'E': val = 14; break; |
| 186 | case 'd': case 'D': val = 13; break; |
| 187 | case 'c': case 'C': val = 12; break; |
| 188 | case 'b': case 'B': val = 11; break; |
| 189 | case 'a': case 'A': val = 10; break; |
| 190 | case '9': val = 9; break; |
| 191 | case '8': val = 8; break; |
| 192 | case '7': val = 7; break; |
| 193 | case '6': val = 6; break; |
| 194 | case '5': val = 5; break; |
| 195 | case '4': val = 4; break; |
| 196 | case '3': val = 3; break; |
| 197 | case '2': val = 2; break; |
| 198 | case '1': val = 1; break; |
| 199 | case '0': val = 0; break; |
| 200 | default: return -EINVAL; |
| 201 | } |
| 202 | |
| 203 | if (even) { |
| 204 | cur = val << 4; |
| 205 | } else { |
| 206 | cur += val; |
| 207 | str.push_back(cur); |
| 208 | cur = 0; |
| 209 | } |
| 210 | even = !even; |
| 211 | } |
| 212 | return even ? OK : -EINVAL; |
| 213 | } |
| 214 | |
| 215 | static bool isValidFilename(const std::string& name) { |
| 216 | if (name.empty() || (name == ".") || (name == "..") |
| 217 | || (name.find('/') != std::string::npos)) { |
| 218 | return false; |
| 219 | } else { |
| 220 | return true; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | std::string BuildKeyPath(const std::string& partGuid) { |
| 225 | return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str()); |
| 226 | } |
| 227 | |
| 228 | std::string BuildDataSystemLegacyPath(userid_t userId) { |
| 229 | return StringPrintf("%s/system/users/%u", BuildDataPath(nullptr).c_str(), userId); |
| 230 | } |
| 231 | |
| 232 | std::string BuildDataSystemCePath(userid_t userId) { |
| 233 | return StringPrintf("%s/system_ce/%u", BuildDataPath(nullptr).c_str(), userId); |
| 234 | } |
| 235 | |
| 236 | std::string BuildDataSystemDePath(userid_t userId) { |
| 237 | return StringPrintf("%s/system_de/%u", BuildDataPath(nullptr).c_str(), userId); |
| 238 | } |
| 239 | |
| 240 | std::string BuildDataMiscLegacyPath(userid_t userId) { |
| 241 | return StringPrintf("%s/misc/user/%u", BuildDataPath(nullptr).c_str(), userId); |
| 242 | } |
| 243 | |
| 244 | std::string BuildDataMiscCePath(userid_t userId) { |
| 245 | return StringPrintf("%s/misc_ce/%u", BuildDataPath(nullptr).c_str(), userId); |
| 246 | } |
| 247 | |
| 248 | std::string BuildDataMiscDePath(userid_t userId) { |
| 249 | return StringPrintf("%s/misc_de/%u", BuildDataPath(nullptr).c_str(), userId); |
| 250 | } |
| 251 | |
Ethan Yonker | e9afc3d | 2018-08-30 15:16:27 -0500 | [diff] [blame^] | 252 | std::string BuildDataVendorCePath(userid_t userId) { |
| 253 | return StringPrintf("%s/vendor_ce/%u", BuildDataPath(nullptr).c_str(), userId); |
| 254 | } |
| 255 | |
| 256 | std::string BuildDataVendorDePath(userid_t userId) { |
| 257 | return StringPrintf("%s/vendor_de/%u", BuildDataPath(nullptr).c_str(), userId); |
| 258 | } |
| 259 | |
Ethan Yonker | bd7492d | 2016-12-07 13:55:01 -0600 | [diff] [blame] | 260 | // Keep in sync with installd (frameworks/native/cmds/installd/utils.h) |
| 261 | std::string BuildDataProfilesDePath(userid_t userId) { |
| 262 | return StringPrintf("%s/misc/profiles/cur/%u", BuildDataPath(nullptr).c_str(), userId); |
| 263 | } |
| 264 | |
| 265 | std::string BuildDataProfilesForeignDexDePath(userid_t userId) { |
| 266 | std::string profiles_path = BuildDataProfilesDePath(userId); |
| 267 | return StringPrintf("%s/foreign-dex", profiles_path.c_str()); |
| 268 | } |
| 269 | |
| 270 | std::string BuildDataPath(const char* volumeUuid) { |
| 271 | // TODO: unify with installd path generation logic |
| 272 | if (volumeUuid == nullptr) { |
| 273 | return "/data"; |
| 274 | } else { |
| 275 | CHECK(isValidFilename(volumeUuid)); |
| 276 | return StringPrintf("/mnt/expand/%s", volumeUuid); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | std::string BuildDataMediaCePath(const char* volumeUuid, userid_t userId) { |
| 281 | // TODO: unify with installd path generation logic |
| 282 | std::string data(BuildDataPath(volumeUuid)); |
| 283 | return StringPrintf("%s/media/%u", data.c_str(), userId); |
| 284 | } |
| 285 | |
| 286 | std::string BuildDataUserCePath(const char* volumeUuid, userid_t userId) { |
| 287 | // TODO: unify with installd path generation logic |
| 288 | std::string data(BuildDataPath(volumeUuid)); |
| 289 | if (volumeUuid == nullptr) { |
| 290 | if (userId == 0) { |
| 291 | return StringPrintf("%s/data", data.c_str()); |
| 292 | } else { |
| 293 | return StringPrintf("%s/user/%u", data.c_str(), userId); |
| 294 | } |
| 295 | } else { |
| 296 | return StringPrintf("%s/user/%u", data.c_str(), userId); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | std::string BuildDataUserDePath(const char* volumeUuid, userid_t userId) { |
| 301 | // TODO: unify with installd path generation logic |
| 302 | std::string data(BuildDataPath(volumeUuid)); |
| 303 | return StringPrintf("%s/user_de/%u", data.c_str(), userId); |
| 304 | } |
| 305 | |
| 306 | } // namespace vold |
| 307 | } // namespace android |