Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "sehandle.h" |
| 18 | #include "Utils.h" |
| 19 | #include "Process.h" |
| 20 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 22 | #include <base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | #include <cutils/fs.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 25 | #include <logwrap/logwrap.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 26 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 27 | #include <mutex> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | #include <fcntl.h> |
| 29 | #include <linux/fs.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <sys/mount.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/stat.h> |
| 34 | #include <sys/wait.h> |
| 35 | |
| 36 | #ifndef UMOUNT_NOFOLLOW |
| 37 | #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ |
| 38 | #endif |
| 39 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 40 | using android::base::StringPrintf; |
| 41 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | namespace vold { |
| 44 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 45 | /* Since we use setexeccon(), we need to carefully lock around any |
| 46 | * code that calls exec() to avoid crossing the streams. */ |
| 47 | static std::mutex sExecLock; |
| 48 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 49 | security_context_t sBlkidContext = nullptr; |
| 50 | security_context_t sBlkidUntrustedContext = nullptr; |
| 51 | security_context_t sFsckContext = nullptr; |
| 52 | security_context_t sFsckUntrustedContext = nullptr; |
| 53 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 54 | static const char* kBlkidPath = "/system/bin/blkid"; |
| 55 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 56 | status_t CreateDeviceNode(const std::string& path, dev_t dev) { |
| 57 | const char* cpath = path.c_str(); |
| 58 | status_t res = 0; |
| 59 | |
| 60 | char* secontext = nullptr; |
| 61 | if (sehandle) { |
| 62 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) { |
| 63 | setfscreatecon(secontext); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | mode_t mode = 0660 | S_IFBLK; |
| 68 | if (mknod(cpath, mode, dev) < 0) { |
| 69 | if (errno != EEXIST) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 70 | PLOG(ERROR) << "Failed to create device node for " << major(dev) |
| 71 | << ":" << minor(dev) << " at " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 72 | res = -errno; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (secontext) { |
| 77 | setfscreatecon(nullptr); |
| 78 | freecon(secontext); |
| 79 | } |
| 80 | |
| 81 | return res; |
| 82 | } |
| 83 | |
| 84 | status_t DestroyDeviceNode(const std::string& path) { |
| 85 | const char* cpath = path.c_str(); |
| 86 | if (TEMP_FAILURE_RETRY(unlink(cpath))) { |
| 87 | return -errno; |
| 88 | } else { |
| 89 | return OK; |
| 90 | } |
| 91 | } |
| 92 | |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 93 | status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) { |
| 94 | const char* cpath = path.c_str(); |
| 95 | |
| 96 | char* secontext = nullptr; |
| 97 | if (sehandle) { |
| 98 | if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) { |
| 99 | setfscreatecon(secontext); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | int res = fs_prepare_dir(cpath, mode, uid, gid); |
| 104 | |
| 105 | if (secontext) { |
| 106 | setfscreatecon(nullptr); |
| 107 | freecon(secontext); |
| 108 | } |
| 109 | |
| 110 | if (res == 0) { |
| 111 | return OK; |
| 112 | } else { |
| 113 | return -errno; |
| 114 | } |
| 115 | } |
| 116 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 117 | status_t ForceUnmount(const std::string& path) { |
| 118 | const char* cpath = path.c_str(); |
| 119 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 120 | return OK; |
| 121 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 122 | PLOG(WARNING) << "Failed to unmount " << path; |
| 123 | |
| 124 | sleep(5); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 125 | Process::killProcessesWithOpenFiles(cpath, SIGINT); |
| 126 | |
| 127 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 128 | return OK; |
| 129 | } |
| 130 | PLOG(WARNING) << "Failed to unmount " << path; |
| 131 | |
| 132 | sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 133 | Process::killProcessesWithOpenFiles(cpath, SIGTERM); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 134 | |
| 135 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 136 | return OK; |
| 137 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 138 | PLOG(WARNING) << "Failed to unmount " << path; |
| 139 | |
| 140 | sleep(5); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 141 | Process::killProcessesWithOpenFiles(cpath, SIGKILL); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 142 | |
| 143 | if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) { |
| 144 | return OK; |
| 145 | } |
Jeff Sharkey | f0121c5 | 2015-04-06 14:08:45 -0700 | [diff] [blame] | 146 | PLOG(ERROR) << "Failed to unmount " << path; |
| 147 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 148 | return -errno; |
| 149 | } |
| 150 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 151 | status_t BindMount(const std::string& source, const std::string& target) { |
| 152 | if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) { |
| 153 | PLOG(ERROR) << "Failed to bind mount " << source << " to " << target; |
| 154 | return -errno; |
| 155 | } |
| 156 | return OK; |
| 157 | } |
| 158 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 159 | static status_t readMetadata(const std::string& path, std::string& fsType, |
| 160 | std::string& fsUuid, std::string& fsLabel, bool untrusted) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 161 | fsType.clear(); |
| 162 | fsUuid.clear(); |
| 163 | fsLabel.clear(); |
| 164 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 165 | std::vector<std::string> cmd; |
| 166 | cmd.push_back(kBlkidPath); |
| 167 | cmd.push_back("-c"); |
| 168 | cmd.push_back("/dev/null"); |
| 169 | cmd.push_back(path); |
| 170 | |
| 171 | std::vector<std::string> output; |
| 172 | status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext); |
| 173 | if (res != OK) { |
| 174 | LOG(WARNING) << "blkid failed to identify " << path; |
| 175 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 178 | char value[128]; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 179 | for (auto line : output) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 180 | // Extract values from blkid output, if defined |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 181 | const char* cline = line.c_str(); |
| 182 | char* start = strstr(cline, "TYPE="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 183 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 184 | fsType = value; |
| 185 | } |
| 186 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 187 | start = strstr(cline, "UUID="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 188 | if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
| 189 | fsUuid = value; |
| 190 | } |
| 191 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 192 | start = strstr(cline, "LABEL="); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 193 | if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) { |
| 194 | fsLabel = value; |
| 195 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 198 | return OK; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Sharkey | 95c87cc | 2015-04-01 11:54:32 -0700 | [diff] [blame] | 201 | status_t ReadMetadata(const std::string& path, std::string& fsType, |
| 202 | std::string& fsUuid, std::string& fsLabel) { |
| 203 | return readMetadata(path, fsType, fsUuid, fsLabel, false); |
| 204 | } |
| 205 | |
| 206 | status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType, |
| 207 | std::string& fsUuid, std::string& fsLabel) { |
| 208 | return readMetadata(path, fsType, fsUuid, fsLabel, true); |
| 209 | } |
| 210 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 211 | status_t ForkExecvp(const std::vector<std::string>& args) { |
| 212 | return ForkExecvp(args, nullptr); |
| 213 | } |
| 214 | |
| 215 | status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) { |
| 216 | size_t argc = args.size(); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 217 | char** argv = (char**) calloc(argc, sizeof(char*)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 218 | for (size_t i = 0; i < argc; i++) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 219 | argv[i] = (char*) args[i].c_str(); |
| 220 | if (i == 0) { |
| 221 | LOG(VERBOSE) << args[i]; |
| 222 | } else { |
| 223 | LOG(VERBOSE) << " " << args[i]; |
| 224 | } |
| 225 | } |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 226 | |
| 227 | status_t res = OK; |
| 228 | { |
| 229 | std::lock_guard<std::mutex> lock(sExecLock); |
| 230 | if (setexeccon(context)) { |
| 231 | LOG(ERROR) << "Failed to setexeccon"; |
| 232 | abort(); |
| 233 | } |
| 234 | res = android_fork_execvp(argc, argv, NULL, false, true); |
| 235 | if (setexeccon(nullptr)) { |
| 236 | LOG(ERROR) << "Failed to setexeccon"; |
| 237 | abort(); |
| 238 | } |
| 239 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 240 | free(argv); |
| 241 | return res; |
| 242 | } |
| 243 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame^] | 244 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 245 | std::vector<std::string>& output) { |
| 246 | return ForkExecvp(args, output, nullptr); |
| 247 | } |
| 248 | |
| 249 | status_t ForkExecvp(const std::vector<std::string>& args, |
| 250 | std::vector<std::string>& output, security_context_t context) { |
| 251 | std::string cmd; |
| 252 | for (size_t i = 0; i < args.size(); i++) { |
| 253 | cmd += args[i] + " "; |
| 254 | if (i == 0) { |
| 255 | LOG(VERBOSE) << args[i]; |
| 256 | } else { |
| 257 | LOG(VERBOSE) << " " << args[i]; |
| 258 | } |
| 259 | } |
| 260 | output.clear(); |
| 261 | |
| 262 | FILE* fp = nullptr; |
| 263 | { |
| 264 | std::lock_guard<std::mutex> lock(sExecLock); |
| 265 | if (setexeccon(context)) { |
| 266 | LOG(ERROR) << "Failed to setexeccon"; |
| 267 | abort(); |
| 268 | } |
| 269 | fp = popen(cmd.c_str(), "r"); |
| 270 | if (setexeccon(nullptr)) { |
| 271 | LOG(ERROR) << "Failed to setexeccon"; |
| 272 | abort(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | if (!fp) { |
| 277 | PLOG(ERROR) << "Failed to popen " << cmd; |
| 278 | return -errno; |
| 279 | } |
| 280 | char line[1024]; |
| 281 | while (fgets(line, sizeof(line), fp) != nullptr) { |
| 282 | LOG(VERBOSE) << line; |
| 283 | output.push_back(std::string(line)); |
| 284 | } |
| 285 | if (pclose(fp) != 0) { |
| 286 | PLOG(ERROR) << "Failed to pclose " << cmd; |
| 287 | return -errno; |
| 288 | } |
| 289 | |
| 290 | return OK; |
| 291 | } |
| 292 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 293 | status_t ReadRandomBytes(size_t bytes, std::string& out) { |
| 294 | out.clear(); |
| 295 | |
| 296 | int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW)); |
| 297 | if (fd == -1) { |
| 298 | return -errno; |
| 299 | } |
| 300 | |
| 301 | char buf[BUFSIZ]; |
| 302 | size_t n; |
| 303 | while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) { |
| 304 | out.append(buf, n); |
| 305 | bytes -= n; |
| 306 | } |
| 307 | TEMP_FAILURE_RETRY(close(fd)); |
| 308 | |
| 309 | if (bytes == 0) { |
| 310 | return OK; |
| 311 | } else { |
| 312 | return -EIO; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | status_t HexToStr(const std::string& hex, std::string& str) { |
| 317 | str.clear(); |
| 318 | bool even = true; |
| 319 | char cur = 0; |
| 320 | for (size_t i = 0; i < hex.size(); i++) { |
| 321 | int val = 0; |
| 322 | switch (hex[i]) { |
| 323 | case ' ': case '-': case ':': continue; |
| 324 | case 'f': case 'F': val = 15; break; |
| 325 | case 'e': case 'E': val = 14; break; |
| 326 | case 'd': case 'D': val = 13; break; |
| 327 | case 'c': case 'C': val = 12; break; |
| 328 | case 'b': case 'B': val = 11; break; |
| 329 | case 'a': case 'A': val = 10; break; |
| 330 | case '9': val = 9; break; |
| 331 | case '8': val = 8; break; |
| 332 | case '7': val = 7; break; |
| 333 | case '6': val = 6; break; |
| 334 | case '5': val = 5; break; |
| 335 | case '4': val = 4; break; |
| 336 | case '3': val = 3; break; |
| 337 | case '2': val = 2; break; |
| 338 | case '1': val = 1; break; |
| 339 | case '0': val = 0; break; |
| 340 | default: return -EINVAL; |
| 341 | } |
| 342 | |
| 343 | if (even) { |
| 344 | cur = val << 4; |
| 345 | } else { |
| 346 | cur += val; |
| 347 | str.push_back(cur); |
| 348 | cur = 0; |
| 349 | } |
| 350 | even = !even; |
| 351 | } |
| 352 | return even ? OK : -EINVAL; |
| 353 | } |
| 354 | |
| 355 | static const char* kLookup = "0123456789abcdef"; |
| 356 | |
| 357 | status_t StrToHex(const std::string& str, std::string& hex) { |
| 358 | hex.clear(); |
| 359 | for (size_t i = 0; i < str.size(); i++) { |
| 360 | hex.push_back(kLookup[str[i] >> 4]); |
| 361 | hex.push_back(kLookup[str[i] & 0x0F]); |
| 362 | } |
| 363 | return OK; |
| 364 | } |
| 365 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 366 | } // namespace vold |
| 367 | } // namespace android |