blob: 06222c3449efb3c9901a810575e1276c354b43c8 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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 Sharkeydeb24052015-03-02 21:01:40 -080017#include "sehandle.h"
18#include "Utils.h"
19#include "Process.h"
20
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070021#include <base/file.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include <base/logging.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070023#include <base/stringprintf.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024#include <cutils/fs.h>
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070025#include <cutils/properties.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080026#include <private/android_filesystem_config.h>
Jeff Sharkey9c484982015-03-31 10:35:33 -070027#include <logwrap/logwrap.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070029#include <mutex>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070030#include <dirent.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <fcntl.h>
32#include <linux/fs.h>
33#include <stdlib.h>
34#include <sys/mount.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/wait.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070038#include <sys/statvfs.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039
40#ifndef UMOUNT_NOFOLLOW
41#define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */
42#endif
43
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070044using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070045using android::base::StringPrintf;
46
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047namespace android {
48namespace vold {
49
Jeff Sharkey95c87cc2015-04-01 11:54:32 -070050security_context_t sBlkidContext = nullptr;
51security_context_t sBlkidUntrustedContext = nullptr;
52security_context_t sFsckContext = nullptr;
53security_context_t sFsckUntrustedContext = nullptr;
54
Jeff Sharkey9c484982015-03-31 10:35:33 -070055static const char* kBlkidPath = "/system/bin/blkid";
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070056static const char* kKeyPath = "/data/misc/vold";
Jeff Sharkey9c484982015-03-31 10:35:33 -070057
Jeff Sharkeyd0640f62015-05-21 22:35:42 -070058static const char* kProcFilesystems = "/proc/filesystems";
59
Jeff Sharkeydeb24052015-03-02 21:01:40 -080060status_t CreateDeviceNode(const std::string& path, dev_t dev) {
61 const char* cpath = path.c_str();
62 status_t res = 0;
63
64 char* secontext = nullptr;
65 if (sehandle) {
66 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFBLK)) {
67 setfscreatecon(secontext);
68 }
69 }
70
71 mode_t mode = 0660 | S_IFBLK;
72 if (mknod(cpath, mode, dev) < 0) {
73 if (errno != EEXIST) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 PLOG(ERROR) << "Failed to create device node for " << major(dev)
75 << ":" << minor(dev) << " at " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 res = -errno;
77 }
78 }
79
80 if (secontext) {
81 setfscreatecon(nullptr);
82 freecon(secontext);
83 }
84
85 return res;
86}
87
88status_t DestroyDeviceNode(const std::string& path) {
89 const char* cpath = path.c_str();
90 if (TEMP_FAILURE_RETRY(unlink(cpath))) {
91 return -errno;
92 } else {
93 return OK;
94 }
95}
96
Jeff Sharkeyf0121c52015-04-06 14:08:45 -070097status_t PrepareDir(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
98 const char* cpath = path.c_str();
99
100 char* secontext = nullptr;
101 if (sehandle) {
102 if (!selabel_lookup(sehandle, &secontext, cpath, S_IFDIR)) {
103 setfscreatecon(secontext);
104 }
105 }
106
107 int res = fs_prepare_dir(cpath, mode, uid, gid);
108
109 if (secontext) {
110 setfscreatecon(nullptr);
111 freecon(secontext);
112 }
113
114 if (res == 0) {
115 return OK;
116 } else {
117 return -errno;
118 }
119}
120
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121status_t ForceUnmount(const std::string& path) {
122 const char* cpath = path.c_str();
123 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
124 return OK;
125 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700126 PLOG(WARNING) << "Failed to unmount " << path;
127
128 sleep(5);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700129 Process::killProcessesWithOpenFiles(cpath, SIGINT);
130
131 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
132 return OK;
133 }
134 PLOG(WARNING) << "Failed to unmount " << path;
135
136 sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 Process::killProcessesWithOpenFiles(cpath, SIGTERM);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138
139 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
140 return OK;
141 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700142 PLOG(WARNING) << "Failed to unmount " << path;
143
144 sleep(5);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800145 Process::killProcessesWithOpenFiles(cpath, SIGKILL);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800146
147 if (!umount2(cpath, UMOUNT_NOFOLLOW) || errno == EINVAL || errno == ENOENT) {
148 return OK;
149 }
Jeff Sharkeyf0121c52015-04-06 14:08:45 -0700150 PLOG(ERROR) << "Failed to unmount " << path;
151
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800152 return -errno;
153}
154
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155status_t BindMount(const std::string& source, const std::string& target) {
156 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
157 PLOG(ERROR) << "Failed to bind mount " << source << " to " << target;
158 return -errno;
159 }
160 return OK;
161}
162
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700163static status_t readMetadata(const std::string& path, std::string& fsType,
164 std::string& fsUuid, std::string& fsLabel, bool untrusted) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700165 fsType.clear();
166 fsUuid.clear();
167 fsLabel.clear();
168
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700169 std::vector<std::string> cmd;
170 cmd.push_back(kBlkidPath);
171 cmd.push_back("-c");
172 cmd.push_back("/dev/null");
173 cmd.push_back(path);
174
175 std::vector<std::string> output;
176 status_t res = ForkExecvp(cmd, output, untrusted ? sBlkidUntrustedContext : sBlkidContext);
177 if (res != OK) {
178 LOG(WARNING) << "blkid failed to identify " << path;
179 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700180 }
181
Jeff Sharkey9c484982015-03-31 10:35:33 -0700182 char value[128];
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700183 for (auto line : output) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 // Extract values from blkid output, if defined
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700185 const char* cline = line.c_str();
186 char* start = strstr(cline, "TYPE=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
188 fsType = value;
189 }
190
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700191 start = strstr(cline, "UUID=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
193 fsUuid = value;
194 }
195
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700196 start = strstr(cline, "LABEL=");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197 if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
198 fsLabel = value;
199 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700200 }
201
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700202 return OK;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203}
204
Jeff Sharkey95c87cc2015-04-01 11:54:32 -0700205status_t ReadMetadata(const std::string& path, std::string& fsType,
206 std::string& fsUuid, std::string& fsLabel) {
207 return readMetadata(path, fsType, fsUuid, fsLabel, false);
208}
209
210status_t ReadMetadataUntrusted(const std::string& path, std::string& fsType,
211 std::string& fsUuid, std::string& fsLabel) {
212 return readMetadata(path, fsType, fsUuid, fsLabel, true);
213}
214
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700215status_t ForkExecvp(const std::vector<std::string>& args) {
216 return ForkExecvp(args, nullptr);
217}
218
219status_t ForkExecvp(const std::vector<std::string>& args, security_context_t context) {
220 size_t argc = args.size();
Jeff Sharkey9c484982015-03-31 10:35:33 -0700221 char** argv = (char**) calloc(argc, sizeof(char*));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700222 for (size_t i = 0; i < argc; i++) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700223 argv[i] = (char*) args[i].c_str();
224 if (i == 0) {
225 LOG(VERBOSE) << args[i];
226 } else {
227 LOG(VERBOSE) << " " << args[i];
228 }
229 }
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700230
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700231 if (setexeccon(context)) {
232 LOG(ERROR) << "Failed to setexeccon";
233 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700234 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700235 status_t res = android_fork_execvp(argc, argv, NULL, false, true);
236 if (setexeccon(nullptr)) {
237 LOG(ERROR) << "Failed to setexeccon";
238 abort();
239 }
240
Jeff Sharkey9c484982015-03-31 10:35:33 -0700241 free(argv);
242 return res;
243}
244
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700245status_t ForkExecvp(const std::vector<std::string>& args,
246 std::vector<std::string>& output) {
247 return ForkExecvp(args, output, nullptr);
248}
249
250status_t ForkExecvp(const std::vector<std::string>& args,
251 std::vector<std::string>& output, security_context_t context) {
252 std::string cmd;
253 for (size_t i = 0; i < args.size(); i++) {
254 cmd += args[i] + " ";
255 if (i == 0) {
256 LOG(VERBOSE) << args[i];
257 } else {
258 LOG(VERBOSE) << " " << args[i];
259 }
260 }
261 output.clear();
262
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700263 if (setexeccon(context)) {
264 LOG(ERROR) << "Failed to setexeccon";
265 abort();
266 }
267 FILE* fp = popen(cmd.c_str(), "r");
268 if (setexeccon(nullptr)) {
269 LOG(ERROR) << "Failed to setexeccon";
270 abort();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700271 }
272
273 if (!fp) {
274 PLOG(ERROR) << "Failed to popen " << cmd;
275 return -errno;
276 }
277 char line[1024];
278 while (fgets(line, sizeof(line), fp) != nullptr) {
279 LOG(VERBOSE) << line;
280 output.push_back(std::string(line));
281 }
282 if (pclose(fp) != 0) {
283 PLOG(ERROR) << "Failed to pclose " << cmd;
284 return -errno;
285 }
286
287 return OK;
288}
289
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700290pid_t ForkExecvpAsync(const std::vector<std::string>& args) {
291 size_t argc = args.size();
292 char** argv = (char**) calloc(argc + 1, sizeof(char*));
293 for (size_t i = 0; i < argc; i++) {
294 argv[i] = (char*) args[i].c_str();
295 if (i == 0) {
296 LOG(VERBOSE) << args[i];
297 } else {
298 LOG(VERBOSE) << " " << args[i];
299 }
300 }
301
302 pid_t pid = fork();
303 if (pid == 0) {
304 close(STDIN_FILENO);
305 close(STDOUT_FILENO);
306 close(STDERR_FILENO);
307
308 if (execvp(argv[0], argv)) {
309 PLOG(ERROR) << "Failed to exec";
310 }
311
312 _exit(1);
313 }
314
315 if (pid == -1) {
316 PLOG(ERROR) << "Failed to exec";
317 }
318
319 free(argv);
320 return pid;
321}
322
Jeff Sharkey9c484982015-03-31 10:35:33 -0700323status_t ReadRandomBytes(size_t bytes, std::string& out) {
324 out.clear();
325
326 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOFOLLOW));
327 if (fd == -1) {
328 return -errno;
329 }
330
331 char buf[BUFSIZ];
332 size_t n;
333 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], std::min(sizeof(buf), bytes)))) > 0) {
334 out.append(buf, n);
335 bytes -= n;
336 }
Elliott Hughesa6231082015-05-15 18:34:24 -0700337 close(fd);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700338
339 if (bytes == 0) {
340 return OK;
341 } else {
342 return -EIO;
343 }
344}
345
346status_t HexToStr(const std::string& hex, std::string& str) {
347 str.clear();
348 bool even = true;
349 char cur = 0;
350 for (size_t i = 0; i < hex.size(); i++) {
351 int val = 0;
352 switch (hex[i]) {
353 case ' ': case '-': case ':': continue;
354 case 'f': case 'F': val = 15; break;
355 case 'e': case 'E': val = 14; break;
356 case 'd': case 'D': val = 13; break;
357 case 'c': case 'C': val = 12; break;
358 case 'b': case 'B': val = 11; break;
359 case 'a': case 'A': val = 10; break;
360 case '9': val = 9; break;
361 case '8': val = 8; break;
362 case '7': val = 7; break;
363 case '6': val = 6; break;
364 case '5': val = 5; break;
365 case '4': val = 4; break;
366 case '3': val = 3; break;
367 case '2': val = 2; break;
368 case '1': val = 1; break;
369 case '0': val = 0; break;
370 default: return -EINVAL;
371 }
372
373 if (even) {
374 cur = val << 4;
375 } else {
376 cur += val;
377 str.push_back(cur);
378 cur = 0;
379 }
380 even = !even;
381 }
382 return even ? OK : -EINVAL;
383}
384
385static const char* kLookup = "0123456789abcdef";
386
387status_t StrToHex(const std::string& str, std::string& hex) {
388 hex.clear();
389 for (size_t i = 0; i < str.size(); i++) {
Jeff Sharkeyef369752015-04-29 15:57:48 -0700390 hex.push_back(kLookup[(str[i] & 0xF0) >> 4]);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700391 hex.push_back(kLookup[str[i] & 0x0F]);
392 }
393 return OK;
394}
395
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700396status_t NormalizeHex(const std::string& in, std::string& out) {
397 std::string tmp;
398 if (HexToStr(in, tmp)) {
399 return -EINVAL;
400 }
401 return StrToHex(tmp, out);
402}
403
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700404uint64_t GetFreeBytes(const std::string& path) {
405 struct statvfs sb;
406 if (statvfs(path.c_str(), &sb) == 0) {
407 return sb.f_bfree * sb.f_bsize;
408 } else {
409 return -1;
410 }
411}
412
413// TODO: borrowed from frameworks/native/libs/diskusage/ which should
414// eventually be migrated into system/
415static int64_t stat_size(struct stat *s) {
416 int64_t blksize = s->st_blksize;
417 // count actual blocks used instead of nominal file size
418 int64_t size = s->st_blocks * 512;
419
420 if (blksize) {
421 /* round up to filesystem block size */
422 size = (size + blksize - 1) & (~(blksize - 1));
423 }
424
425 return size;
426}
427
428// TODO: borrowed from frameworks/native/libs/diskusage/ which should
429// eventually be migrated into system/
430int64_t calculate_dir_size(int dfd) {
431 int64_t size = 0;
432 struct stat s;
433 DIR *d;
434 struct dirent *de;
435
436 d = fdopendir(dfd);
437 if (d == NULL) {
438 close(dfd);
439 return 0;
440 }
441
442 while ((de = readdir(d))) {
443 const char *name = de->d_name;
444 if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
445 size += stat_size(&s);
446 }
447 if (de->d_type == DT_DIR) {
448 int subfd;
449
450 /* always skip "." and ".." */
451 if (name[0] == '.') {
452 if (name[1] == 0)
453 continue;
454 if ((name[1] == '.') && (name[2] == 0))
455 continue;
456 }
457
458 subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
459 if (subfd >= 0) {
460 size += calculate_dir_size(subfd);
461 }
462 }
463 }
464 closedir(d);
465 return size;
466}
467
468uint64_t GetTreeBytes(const std::string& path) {
469 int dirfd = open(path.c_str(), O_DIRECTORY, O_RDONLY);
470 if (dirfd < 0) {
471 PLOG(WARNING) << "Failed to open " << path;
472 return -1;
473 } else {
474 uint64_t res = calculate_dir_size(dirfd);
475 close(dirfd);
476 return res;
477 }
478}
479
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700480bool IsFilesystemSupported(const std::string& fsType) {
481 std::string supported;
482 if (!ReadFileToString(kProcFilesystems, &supported)) {
483 PLOG(ERROR) << "Failed to read supported filesystems";
484 return false;
485 }
486 return supported.find(fsType + "\n") != std::string::npos;
487}
488
489status_t WipeBlockDevice(const std::string& path) {
490 status_t res = -1;
491 const char* c_path = path.c_str();
492 unsigned long nr_sec = 0;
493 unsigned long long range[2];
494
495 int fd = TEMP_FAILURE_RETRY(open(c_path, O_RDWR | O_CLOEXEC));
496 if (fd == -1) {
497 PLOG(ERROR) << "Failed to open " << path;
498 goto done;
499 }
500
501 if ((ioctl(fd, BLKGETSIZE, nr_sec)) == -1) {
502 PLOG(ERROR) << "Failed to determine size of " << path;
503 goto done;
504 }
505
506 range[0] = 0;
507 range[1] = (unsigned long long) nr_sec * 512;
508
509 LOG(INFO) << "About to discard " << range[1] << " on " << path;
510 if (ioctl(fd, BLKDISCARD, &range) == 0) {
511 LOG(INFO) << "Discard success on " << path;
512 res = 0;
513 } else {
514 PLOG(ERROR) << "Discard failure on " << path;
515 }
516
517done:
518 close(fd);
519 return res;
520}
521
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700522std::string BuildKeyPath(const std::string& partGuid) {
523 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
524}
525
Jeff Sharkey66270a22015-06-24 11:49:24 -0700526dev_t GetDevice(const std::string& path) {
527 struct stat sb;
528 if (stat(path.c_str(), &sb)) {
529 PLOG(WARNING) << "Failed to stat " << path;
530 return 0;
531 } else {
532 return sb.st_dev;
533 }
534}
535
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700536std::string DefaultFstabPath() {
537 char hardware[PROPERTY_VALUE_MAX];
538 property_get("ro.hardware", hardware, "");
539 return StringPrintf("/fstab.%s", hardware);
540}
541
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800542} // namespace vold
543} // namespace android