Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ctype.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <errno.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
| 28 | #include <libgen.h> |
| 29 | #include <time.h> |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 30 | #include <sys/swap.h> |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 31 | #include <dirent.h> |
| 32 | #include <ext4.h> |
| 33 | #include <ext4_sb.h> |
Paul Lawrence | 806d10b | 2015-04-28 22:07:10 +0000 | [diff] [blame] | 34 | #include <ext4_crypt_init_extensions.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 35 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 36 | #include <linux/loop.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 38 | #include <cutils/android_reboot.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 39 | #include <cutils/partition_utils.h> |
| 40 | #include <cutils/properties.h> |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 41 | #include <logwrap/logwrap.h> |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 42 | |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 43 | #include "mincrypt/rsa.h" |
| 44 | #include "mincrypt/sha.h" |
| 45 | #include "mincrypt/sha256.h" |
| 46 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 47 | #include "ext4_utils.h" |
| 48 | #include "wipe.h" |
| 49 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 50 | #include "fs_mgr_priv.h" |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 51 | #include "fs_mgr_priv_verity.h" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 52 | |
| 53 | #define KEY_LOC_PROP "ro.crypto.keyfile.userdata" |
| 54 | #define KEY_IN_FOOTER "footer" |
| 55 | |
| 56 | #define E2FSCK_BIN "/system/bin/e2fsck" |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 57 | #define F2FS_FSCK_BIN "/system/bin/fsck.f2fs" |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 58 | #define MKSWAP_BIN "/system/bin/mkswap" |
| 59 | |
Ken Sumrall | 4eaf905 | 2013-09-18 17:49:21 -0700 | [diff] [blame] | 60 | #define FSCK_LOG_FILE "/dev/fscklogs/log" |
| 61 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 62 | #define ZRAM_CONF_DEV "/sys/block/zram0/disksize" |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 63 | |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 64 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) |
| 65 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 66 | /* |
| 67 | * gettime() - returns the time in seconds of the system's monotonic clock or |
| 68 | * zero on error. |
| 69 | */ |
| 70 | static time_t gettime(void) |
| 71 | { |
| 72 | struct timespec ts; |
| 73 | int ret; |
| 74 | |
| 75 | ret = clock_gettime(CLOCK_MONOTONIC, &ts); |
| 76 | if (ret < 0) { |
| 77 | ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno)); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | return ts.tv_sec; |
| 82 | } |
| 83 | |
| 84 | static int wait_for_file(const char *filename, int timeout) |
| 85 | { |
| 86 | struct stat info; |
| 87 | time_t timeout_time = gettime() + timeout; |
| 88 | int ret = -1; |
| 89 | |
| 90 | while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0)) |
| 91 | usleep(10000); |
| 92 | |
| 93 | return ret; |
| 94 | } |
| 95 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 96 | static void check_fs(char *blk_device, char *fs_type, char *target) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 97 | { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 98 | int status; |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 99 | int ret; |
| 100 | long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID; |
Oreste Salerno | 6ed84c9 | 2015-05-20 17:01:39 +0000 | [diff] [blame] | 101 | char tmpmnt_opts[64] = "errors=remount-ro"; |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 102 | char *e2fsck_argv[] = { |
| 103 | E2FSCK_BIN, |
Daniel Rosenberg | 2cd762d | 2015-12-02 17:53:29 -0800 | [diff] [blame] | 104 | "-f", |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 105 | "-y", |
| 106 | blk_device |
| 107 | }; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 108 | |
| 109 | /* Check for the types of filesystems we know how to check */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 110 | if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) { |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 111 | /* |
| 112 | * First try to mount and unmount the filesystem. We do this because |
| 113 | * the kernel is more efficient than e2fsck in running the journal and |
| 114 | * processing orphaned inodes, and on at least one device with a |
| 115 | * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes |
| 116 | * to do what the kernel does in about a second. |
| 117 | * |
| 118 | * After mounting and unmounting the filesystem, run e2fsck, and if an |
| 119 | * error is recorded in the filesystem superblock, e2fsck will do a full |
| 120 | * check. Otherwise, it does nothing. If the kernel cannot mount the |
| 121 | * filesytsem due to an error, e2fsck is still run to do a full check |
| 122 | * fix the filesystem. |
| 123 | */ |
Elliott Hughes | 5e7dd44 | 2015-04-24 11:05:48 -0700 | [diff] [blame] | 124 | errno = 0; |
Oreste Salerno | 6ed84c9 | 2015-05-20 17:01:39 +0000 | [diff] [blame] | 125 | if (!strcmp(fs_type, "ext4")) { |
| 126 | // This option is only valid with ext4 |
| 127 | strlcat(tmpmnt_opts, ",nomblk_io_submit", sizeof(tmpmnt_opts)); |
| 128 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 129 | ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts); |
Elliott Hughes | 5e7dd44 | 2015-04-24 11:05:48 -0700 | [diff] [blame] | 130 | INFO("%s(): mount(%s,%s,%s)=%d: %s\n", |
| 131 | __func__, blk_device, target, fs_type, ret, strerror(errno)); |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 132 | if (!ret) { |
Nick Kralevich | 7294eb6 | 2015-02-05 16:02:42 -0800 | [diff] [blame] | 133 | int i; |
| 134 | for (i = 0; i < 5; i++) { |
| 135 | // Try to umount 5 times before continuing on. |
| 136 | // Should we try rebooting if all attempts fail? |
| 137 | int result = umount(target); |
| 138 | if (result == 0) { |
Elliott Hughes | 5e7dd44 | 2015-04-24 11:05:48 -0700 | [diff] [blame] | 139 | INFO("%s(): unmount(%s) succeeded\n", __func__, target); |
Nick Kralevich | 7294eb6 | 2015-02-05 16:02:42 -0800 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno)); |
| 143 | sleep(1); |
| 144 | } |
Ken Sumrall | 5dc5bfe | 2012-07-23 19:34:00 -0700 | [diff] [blame] | 145 | } |
| 146 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 147 | /* |
| 148 | * Some system images do not have e2fsck for licensing reasons |
| 149 | * (e.g. recent SDK system images). Detect these and skip the check. |
| 150 | */ |
| 151 | if (access(E2FSCK_BIN, X_OK)) { |
| 152 | INFO("Not running %s on %s (executable not in system image)\n", |
| 153 | E2FSCK_BIN, blk_device); |
| 154 | } else { |
| 155 | INFO("Running %s on %s\n", E2FSCK_BIN, blk_device); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 156 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 157 | ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 158 | &status, true, LOG_KLOG | LOG_FILE, |
| 159 | true, FSCK_LOG_FILE, NULL, 0); |
Ken Sumrall | bf021b4 | 2013-03-19 19:38:44 -0700 | [diff] [blame] | 160 | |
David 'Digit' Turner | 28483d7 | 2014-02-17 11:14:44 +0100 | [diff] [blame] | 161 | if (ret < 0) { |
| 162 | /* No need to check for error in fork, we can't really handle it now */ |
| 163 | ERROR("Failed trying to run %s\n", E2FSCK_BIN); |
| 164 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 165 | } |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 166 | } else if (!strcmp(fs_type, "f2fs")) { |
| 167 | char *f2fs_fsck_argv[] = { |
| 168 | F2FS_FSCK_BIN, |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 169 | "-a", |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 170 | blk_device |
| 171 | }; |
Yusuke Sato | 0df0827 | 2015-07-08 14:57:07 -0700 | [diff] [blame] | 172 | INFO("Running %s -a %s\n", F2FS_FSCK_BIN, blk_device); |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 173 | |
| 174 | ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv, |
| 175 | &status, true, LOG_KLOG | LOG_FILE, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 176 | true, FSCK_LOG_FILE, NULL, 0); |
JP Abgrall | 1235158 | 2014-06-17 17:01:14 -0700 | [diff] [blame] | 177 | if (ret < 0) { |
| 178 | /* No need to check for error in fork, we can't really handle it now */ |
| 179 | ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN); |
| 180 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | static void remove_trailing_slashes(char *n) |
| 187 | { |
| 188 | int len; |
| 189 | |
| 190 | len = strlen(n) - 1; |
| 191 | while ((*(n + len) == '/') && len) { |
| 192 | *(n + len) = '\0'; |
| 193 | len--; |
| 194 | } |
| 195 | } |
| 196 | |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 197 | /* |
| 198 | * Mark the given block device as read-only, using the BLKROSET ioctl. |
| 199 | * Return 0 on success, and -1 on error. |
| 200 | */ |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 201 | int fs_mgr_set_blk_ro(const char *blockdev) |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 202 | { |
| 203 | int fd; |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 204 | int rc = -1; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 205 | int ON = 1; |
| 206 | |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 207 | fd = TEMP_FAILURE_RETRY(open(blockdev, O_RDONLY | O_CLOEXEC)); |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 208 | if (fd < 0) { |
| 209 | // should never happen |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 210 | return rc; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 213 | rc = ioctl(fd, BLKROSET, &ON); |
Elliott Hughes | 9fc8343 | 2015-05-15 19:16:40 -0700 | [diff] [blame] | 214 | close(fd); |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 215 | |
| 216 | return rc; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /* |
| 220 | * __mount(): wrapper around the mount() system call which also |
| 221 | * sets the underlying block device to read-only if the mount is read-only. |
| 222 | * See "man 2 mount" for return values. |
| 223 | */ |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 224 | static int __mount(const char *source, const char *target, const struct fstab_rec *rec) |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 225 | { |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 226 | unsigned long mountflags = rec->flags; |
| 227 | int ret; |
| 228 | int save_errno; |
Daniel Rosenberg | f67d6bd | 2014-06-26 14:55:04 -0700 | [diff] [blame] | 229 | |
| 230 | /* We need this because sometimes we have legacy symlinks |
| 231 | * that are lingering around and need cleaning up. |
| 232 | */ |
| 233 | struct stat info; |
| 234 | if (!lstat(target, &info)) |
| 235 | if ((info.st_mode & S_IFMT) == S_IFLNK) |
| 236 | unlink(target); |
Daniel Rosenberg | f530c93 | 2014-05-28 14:10:01 -0700 | [diff] [blame] | 237 | mkdir(target, 0755); |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 238 | ret = mount(source, target, rec->fs_type, mountflags, rec->fs_options); |
| 239 | save_errno = errno; |
| 240 | INFO("%s(source=%s,target=%s,type=%s)=%d\n", __func__, source, target, rec->fs_type, ret); |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 241 | if ((ret == 0) && (mountflags & MS_RDONLY) != 0) { |
Sami Tolvanen | 214f33b | 2014-12-18 16:15:30 +0000 | [diff] [blame] | 242 | fs_mgr_set_blk_ro(source); |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 243 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 244 | errno = save_errno; |
Nick Kralevich | e18c0d5 | 2013-04-16 16:41:32 -0700 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 248 | static int fs_match(char *in1, char *in2) |
| 249 | { |
| 250 | char *n1; |
| 251 | char *n2; |
| 252 | int ret; |
| 253 | |
| 254 | n1 = strdup(in1); |
| 255 | n2 = strdup(in2); |
| 256 | |
| 257 | remove_trailing_slashes(n1); |
| 258 | remove_trailing_slashes(n2); |
| 259 | |
| 260 | ret = !strcmp(n1, n2); |
| 261 | |
| 262 | free(n1); |
| 263 | free(n2); |
| 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
Geremy Condra | cd642fc | 2014-04-02 13:42:06 -0700 | [diff] [blame] | 268 | static int device_is_debuggable() { |
| 269 | int ret = -1; |
| 270 | char value[PROP_VALUE_MAX]; |
| 271 | ret = __system_property_get("ro.debuggable", value); |
| 272 | if (ret < 0) |
| 273 | return ret; |
| 274 | return strcmp(value, "1") ? 0 : 1; |
| 275 | } |
| 276 | |
Paul Lawrence | bbb3631 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 277 | static int device_is_secure() { |
| 278 | int ret = -1; |
| 279 | char value[PROP_VALUE_MAX]; |
| 280 | ret = __system_property_get("ro.secure", value); |
| 281 | /* If error, we want to fail secure */ |
| 282 | if (ret < 0) |
| 283 | return 1; |
| 284 | return strcmp(value, "0") ? 1 : 0; |
| 285 | } |
| 286 | |
Paul Lawrence | 703b87d | 2015-01-07 11:44:51 -0800 | [diff] [blame] | 287 | static int device_is_force_encrypted() { |
| 288 | int ret = -1; |
| 289 | char value[PROP_VALUE_MAX]; |
| 290 | ret = __system_property_get("ro.vold.forceencryption", value); |
| 291 | if (ret < 0) |
| 292 | return 0; |
| 293 | return strcmp(value, "1") ? 0 : 1; |
| 294 | } |
| 295 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 296 | /* |
| 297 | * Tries to mount any of the consecutive fstab entries that match |
| 298 | * the mountpoint of the one given by fstab->recs[start_idx]. |
| 299 | * |
| 300 | * end_idx: On return, will be the last rec that was looked at. |
| 301 | * attempted_idx: On return, will indicate which fstab rec |
| 302 | * succeeded. In case of failure, it will be the start_idx. |
| 303 | * Returns |
| 304 | * -1 on failure with errno set to match the 1st mount failure. |
| 305 | * 0 on success. |
| 306 | */ |
| 307 | static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx) |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 308 | { |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 309 | int i; |
| 310 | int mount_errno = 0; |
| 311 | int mounted = 0; |
| 312 | |
| 313 | if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) { |
| 314 | errno = EINVAL; |
| 315 | if (end_idx) *end_idx = start_idx; |
| 316 | if (attempted_idx) *end_idx = start_idx; |
| 317 | return -1; |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 318 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 319 | |
| 320 | /* Hunt down an fstab entry for the same mount point that might succeed */ |
| 321 | for (i = start_idx; |
| 322 | /* We required that fstab entries for the same mountpoint be consecutive */ |
| 323 | i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point); |
| 324 | i++) { |
| 325 | /* |
| 326 | * Don't try to mount/encrypt the same mount point again. |
| 327 | * Deal with alternate entries for the same point which are required to be all following |
| 328 | * each other. |
| 329 | */ |
| 330 | if (mounted) { |
| 331 | ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__, |
| 332 | fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type); |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 337 | check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type, |
| 338 | fstab->recs[i].mount_point); |
| 339 | } |
| 340 | if (!__mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, &fstab->recs[i])) { |
| 341 | *attempted_idx = i; |
| 342 | mounted = 1; |
| 343 | if (i != start_idx) { |
| 344 | ERROR("%s(): Mounted %s on %s with fs_type=%s instead of %s\n", __func__, |
| 345 | fstab->recs[i].blk_device, fstab->recs[i].mount_point, fstab->recs[i].fs_type, |
| 346 | fstab->recs[start_idx].fs_type); |
| 347 | } |
| 348 | } else { |
| 349 | /* back up errno for crypto decisions */ |
| 350 | mount_errno = errno; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /* Adjust i for the case where it was still withing the recs[] */ |
| 355 | if (i < fstab->num_entries) --i; |
| 356 | |
| 357 | *end_idx = i; |
| 358 | if (!mounted) { |
| 359 | *attempted_idx = start_idx; |
| 360 | errno = mount_errno; |
| 361 | return -1; |
| 362 | } |
| 363 | return 0; |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 366 | static int translate_ext_labels(struct fstab_rec *rec) |
| 367 | { |
| 368 | DIR *blockdir = NULL; |
| 369 | struct dirent *ent; |
| 370 | char *label; |
| 371 | size_t label_len; |
| 372 | int ret = -1; |
| 373 | |
| 374 | if (strncmp(rec->blk_device, "LABEL=", 6)) |
| 375 | return 0; |
| 376 | |
| 377 | label = rec->blk_device + 6; |
| 378 | label_len = strlen(label); |
| 379 | |
| 380 | if (label_len > 16) { |
| 381 | ERROR("FS label is longer than allowed by filesystem\n"); |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | blockdir = opendir("/dev/block"); |
| 387 | if (!blockdir) { |
| 388 | ERROR("couldn't open /dev/block\n"); |
| 389 | goto out; |
| 390 | } |
| 391 | |
| 392 | while ((ent = readdir(blockdir))) { |
| 393 | int fd; |
| 394 | char super_buf[1024]; |
| 395 | struct ext4_super_block *sb; |
| 396 | |
| 397 | if (ent->d_type != DT_BLK) |
| 398 | continue; |
| 399 | |
| 400 | fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY); |
| 401 | if (fd < 0) { |
| 402 | ERROR("Cannot open block device /dev/block/%s\n", ent->d_name); |
| 403 | goto out; |
| 404 | } |
| 405 | |
| 406 | if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 || |
| 407 | TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) { |
| 408 | /* Probably a loopback device or something else without a readable |
| 409 | * superblock. |
| 410 | */ |
| 411 | close(fd); |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | sb = (struct ext4_super_block *)super_buf; |
| 416 | if (sb->s_magic != EXT4_SUPER_MAGIC) { |
| 417 | INFO("/dev/block/%s not ext{234}\n", ent->d_name); |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | if (!strncmp(label, sb->s_volume_name, label_len)) { |
| 422 | char *new_blk_device; |
| 423 | |
| 424 | if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) { |
| 425 | ERROR("Could not allocate block device string\n"); |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device); |
| 430 | |
| 431 | free(rec->blk_device); |
| 432 | rec->blk_device = new_blk_device; |
| 433 | ret = 0; |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | out: |
| 439 | closedir(blockdir); |
| 440 | return ret; |
| 441 | } |
| 442 | |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 443 | // Check to see if a mountable volume has encryption requirements |
Paul Lawrence | 6908018 | 2016-02-02 10:31:30 -0800 | [diff] [blame] | 444 | static int handle_encryptable(const struct fstab_rec* rec) |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 445 | { |
Paul Lawrence | aecb1e2 | 2015-11-06 11:18:48 -0800 | [diff] [blame] | 446 | /* Check for existence of convert_fbe breadcrumb file */ |
| 447 | char convert_fbe_name[PATH_MAX]; |
| 448 | snprintf(convert_fbe_name, sizeof(convert_fbe_name), |
| 449 | "%s/convert_fbe", rec->mount_point); |
| 450 | bool convert_fbe = (access(convert_fbe_name, F_OK) == 0); |
| 451 | |
Paul Lawrence | 8d63383 | 2015-11-20 07:07:12 -0800 | [diff] [blame] | 452 | /* Check for existence of convert_fbe breadcrumb file */ |
| 453 | char convert_fde_name[PATH_MAX]; |
| 454 | snprintf(convert_fde_name, sizeof(convert_fbe_name), |
| 455 | "%s/misc/vold/convert_fde", rec->mount_point); |
| 456 | bool convert_fde = (access(convert_fde_name, F_OK) == 0); |
| 457 | |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 458 | /* If this is block encryptable, need to trigger encryption */ |
| 459 | if ( (rec->fs_mgr_flags & MF_FORCECRYPT) |
Paul Lawrence | 8d63383 | 2015-11-20 07:07:12 -0800 | [diff] [blame] | 460 | || ((rec->fs_mgr_flags & MF_CRYPT) && convert_fde) |
Paul Lawrence | aecb1e2 | 2015-11-06 11:18:48 -0800 | [diff] [blame] | 461 | || ((rec->fs_mgr_flags & MF_FORCEFDEORFBE) && !convert_fbe) |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 462 | || (device_is_force_encrypted() && fs_mgr_is_encryptable(rec))) { |
| 463 | if (umount(rec->mount_point) == 0) { |
| 464 | return FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION; |
| 465 | } else { |
| 466 | WARNING("Could not umount %s (%s) - allow continue unencrypted\n", |
| 467 | rec->mount_point, strerror(errno)); |
| 468 | return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Deal with file level encryption |
Paul Lawrence | aecb1e2 | 2015-11-06 11:18:48 -0800 | [diff] [blame] | 473 | if ( (rec->fs_mgr_flags & MF_FILEENCRYPTION) |
| 474 | || ((rec->fs_mgr_flags & MF_FORCEFDEORFBE) && convert_fbe)) { |
Paul Lawrence | 6908018 | 2016-02-02 10:31:30 -0800 | [diff] [blame] | 475 | INFO("%s is file encrypted\n", rec->mount_point); |
| 476 | return FS_MGR_MNTALL_DEV_FILE_ENCRYPTED; |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED; |
| 480 | } |
| 481 | |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 482 | /* When multiple fstab records share the same mount_point, it will |
| 483 | * try to mount each one in turn, and ignore any duplicates after a |
| 484 | * first successful mount. |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 485 | * Returns -1 on error, and FS_MGR_MNTALL_* otherwise. |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 486 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 487 | int fs_mgr_mount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 488 | { |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 489 | int i = 0; |
| 490 | int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 491 | int error_count = 0; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 492 | int mret = -1; |
| 493 | int mount_errno = 0; |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 494 | int attempted_idx = -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 495 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 496 | if (!fstab) { |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 497 | return -1; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 500 | for (i = 0; i < fstab->num_entries; i++) { |
| 501 | /* Don't mount entries that are managed by vold */ |
Ken Sumrall | 6c2c121 | 2013-02-22 17:36:21 -0800 | [diff] [blame] | 502 | if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 503 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 504 | } |
| 505 | |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 506 | /* Skip swap and raw partition entries such as boot, recovery, etc */ |
| 507 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 508 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 509 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 510 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Daniel Rosenberg | 31a4faf | 2015-06-29 17:33:05 -0700 | [diff] [blame] | 513 | /* Skip mounting the root partition, as it will already have been mounted */ |
| 514 | if (!strcmp(fstab->recs[i].mount_point, "/")) { |
| 515 | if ((fstab->recs[i].fs_mgr_flags & MS_RDONLY) != 0) { |
| 516 | fs_mgr_set_blk_ro(fstab->recs[i].blk_device); |
| 517 | } |
| 518 | continue; |
| 519 | } |
| 520 | |
Christoffer Dall | 8298234 | 2014-12-17 21:26:54 +0100 | [diff] [blame] | 521 | /* Translate LABEL= file system labels into block devices */ |
| 522 | if (!strcmp(fstab->recs[i].fs_type, "ext2") || |
| 523 | !strcmp(fstab->recs[i].fs_type, "ext3") || |
| 524 | !strcmp(fstab->recs[i].fs_type, "ext4")) { |
| 525 | int tret = translate_ext_labels(&fstab->recs[i]); |
| 526 | if (tret < 0) { |
| 527 | ERROR("Could not translate label to block device\n"); |
| 528 | continue; |
| 529 | } |
| 530 | } |
| 531 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 532 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 533 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 534 | } |
| 535 | |
Paul Lawrence | bbb3631 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 536 | if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) { |
| 537 | int rc = fs_mgr_setup_verity(&fstab->recs[i]); |
| 538 | if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) { |
| 539 | INFO("Verity disabled"); |
| 540 | } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 541 | ERROR("Could not set up verified partition, skipping!\n"); |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 542 | continue; |
| 543 | } |
| 544 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 545 | int last_idx_inspected; |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 546 | int top_idx = i; |
| 547 | |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 548 | mret = mount_with_alternatives(fstab, i, &last_idx_inspected, &attempted_idx); |
| 549 | i = last_idx_inspected; |
| 550 | mount_errno = errno; |
JP Abgrall | f786fe5 | 2014-06-18 07:28:14 +0000 | [diff] [blame] | 551 | |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 552 | /* Deal with encryptability. */ |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 553 | if (!mret) { |
Paul Lawrence | 6908018 | 2016-02-02 10:31:30 -0800 | [diff] [blame] | 554 | int status = handle_encryptable(&fstab->recs[attempted_idx]); |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 555 | |
| 556 | if (status == FS_MGR_MNTALL_FAIL) { |
| 557 | /* Fatal error - no point continuing */ |
| 558 | return status; |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 559 | } |
Paul Lawrence | b8c9d27 | 2015-03-26 15:49:42 +0000 | [diff] [blame] | 560 | |
| 561 | if (status != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
| 562 | if (encryptable != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) { |
| 563 | // Log and continue |
| 564 | ERROR("Only one encryptable/encrypted partition supported\n"); |
| 565 | } |
| 566 | encryptable = status; |
| 567 | } |
| 568 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 569 | /* Success! Go get the next one */ |
| 570 | continue; |
| 571 | } |
| 572 | |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 573 | /* mount(2) returned an error, handle the encryptable/formattable case */ |
| 574 | bool wiped = partition_wiped(fstab->recs[top_idx].blk_device); |
| 575 | if (mret && mount_errno != EBUSY && mount_errno != EACCES && |
| 576 | fs_mgr_is_formattable(&fstab->recs[top_idx]) && wiped) { |
| 577 | /* top_idx and attempted_idx point at the same partition, but sometimes |
| 578 | * at two different lines in the fstab. Use the top one for formatting |
| 579 | * as that is the preferred one. |
| 580 | */ |
| 581 | ERROR("%s(): %s is wiped and %s %s is formattable. Format it.\n", __func__, |
| 582 | fstab->recs[top_idx].blk_device, fstab->recs[top_idx].mount_point, |
| 583 | fstab->recs[top_idx].fs_type); |
| 584 | if (fs_mgr_is_encryptable(&fstab->recs[top_idx]) && |
| 585 | strcmp(fstab->recs[top_idx].key_loc, KEY_IN_FOOTER)) { |
| 586 | int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY, 0644); |
| 587 | if (fd >= 0) { |
| 588 | INFO("%s(): also wipe %s\n", __func__, fstab->recs[top_idx].key_loc); |
| 589 | wipe_block_device(fd, get_file_size(fd)); |
| 590 | close(fd); |
| 591 | } else { |
| 592 | ERROR("%s(): %s wouldn't open (%s)\n", __func__, |
| 593 | fstab->recs[top_idx].key_loc, strerror(errno)); |
| 594 | } |
| 595 | } |
| 596 | if (fs_mgr_do_format(&fstab->recs[top_idx]) == 0) { |
| 597 | /* Let's replay the mount actions. */ |
| 598 | i = top_idx - 1; |
| 599 | continue; |
| 600 | } |
| 601 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 602 | if (mret && mount_errno != EBUSY && mount_errno != EACCES && |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 603 | fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) { |
Chris Fries | 79f3384 | 2013-09-05 13:19:21 -0500 | [diff] [blame] | 604 | if (wiped) { |
JP Abgrall | cee2068 | 2014-07-02 14:26:54 -0700 | [diff] [blame] | 605 | ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__, |
| 606 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 607 | fstab->recs[attempted_idx].fs_type); |
| 608 | encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 609 | continue; |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 610 | } else { |
| 611 | /* Need to mount a tmpfs at this mountpoint for now, and set |
| 612 | * properties that vold will query later for decrypting |
| 613 | */ |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 614 | ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__, |
| 615 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 616 | fstab->recs[attempted_idx].fs_type); |
| 617 | if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 618 | ++error_count; |
| 619 | continue; |
| 620 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 621 | } |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 622 | encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 623 | } else { |
William Roberts | 071f28a | 2014-01-14 14:33:44 -0500 | [diff] [blame] | 624 | ERROR("Failed to mount an un-encryptable or wiped partition on" |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 625 | "%s at %s options: %s error: %s\n", |
JP Abgrall | f22b745 | 2014-07-02 13:16:04 -0700 | [diff] [blame] | 626 | fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point, |
| 627 | fstab->recs[attempted_idx].fs_options, strerror(mount_errno)); |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 628 | ++error_count; |
| 629 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 633 | if (error_count) { |
| 634 | return -1; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 635 | } else { |
Paul Lawrence | 166fa3d | 2014-02-03 13:27:49 -0800 | [diff] [blame] | 636 | return encryptable; |
Mohamad Ayyash | 38afe5f | 2014-03-10 15:40:29 -0700 | [diff] [blame] | 637 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 638 | } |
| 639 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 640 | /* If tmp_mount_point is non-null, mount the filesystem there. This is for the |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 641 | * tmp mount we do to check the user password |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 642 | * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one |
| 643 | * in turn, and stop on 1st success, or no more match. |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 644 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 645 | int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device, |
| 646 | char *tmp_mount_point) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 647 | { |
| 648 | int i = 0; |
Paul Lawrence | cf234dc | 2014-09-09 10:44:51 -0700 | [diff] [blame] | 649 | int ret = FS_MGR_DOMNT_FAILED; |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 650 | int mount_errors = 0; |
| 651 | int first_mount_errno = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 652 | char *m; |
| 653 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 654 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 655 | return ret; |
| 656 | } |
| 657 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 658 | for (i = 0; i < fstab->num_entries; i++) { |
| 659 | if (!fs_match(fstab->recs[i].mount_point, n_name)) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 660 | continue; |
| 661 | } |
| 662 | |
| 663 | /* We found our match */ |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 664 | /* If this swap or a raw partition, report an error */ |
| 665 | if (!strcmp(fstab->recs[i].fs_type, "swap") || |
| 666 | !strcmp(fstab->recs[i].fs_type, "emmc") || |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 667 | !strcmp(fstab->recs[i].fs_type, "mtd")) { |
| 668 | ERROR("Cannot mount filesystem of type %s on %s\n", |
| 669 | fstab->recs[i].fs_type, n_blk_device); |
| 670 | goto out; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 673 | /* First check the filesystem if requested */ |
| 674 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 675 | wait_for_file(n_blk_device, WAIT_TIMEOUT); |
| 676 | } |
| 677 | |
| 678 | if (fstab->recs[i].fs_mgr_flags & MF_CHECK) { |
| 679 | check_fs(n_blk_device, fstab->recs[i].fs_type, |
| 680 | fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 681 | } |
| 682 | |
Paul Lawrence | bbb3631 | 2014-10-09 14:22:49 +0000 | [diff] [blame] | 683 | if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) { |
| 684 | int rc = fs_mgr_setup_verity(&fstab->recs[i]); |
| 685 | if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) { |
| 686 | INFO("Verity disabled"); |
| 687 | } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 688 | ERROR("Could not set up verified partition, skipping!\n"); |
Geremy Condra | 3ad3d1c | 2013-02-22 18:11:41 -0800 | [diff] [blame] | 689 | continue; |
| 690 | } |
| 691 | } |
| 692 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 693 | /* Now mount it where requested */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 694 | if (tmp_mount_point) { |
| 695 | m = tmp_mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 696 | } else { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 697 | m = fstab->recs[i].mount_point; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 698 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 699 | if (__mount(n_blk_device, m, &fstab->recs[i])) { |
| 700 | if (!first_mount_errno) first_mount_errno = errno; |
| 701 | mount_errors++; |
| 702 | continue; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 703 | } else { |
| 704 | ret = 0; |
| 705 | goto out; |
| 706 | } |
| 707 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 708 | if (mount_errors) { |
| 709 | ERROR("Cannot mount filesystem on %s at %s. error: %s\n", |
| 710 | n_blk_device, m, strerror(first_mount_errno)); |
Paul Lawrence | cf234dc | 2014-09-09 10:44:51 -0700 | [diff] [blame] | 711 | if (first_mount_errno == EBUSY) { |
| 712 | ret = FS_MGR_DOMNT_BUSY; |
| 713 | } else { |
| 714 | ret = FS_MGR_DOMNT_FAILED; |
| 715 | } |
JP Abgrall | 5c01dac | 2014-06-18 14:54:37 -0700 | [diff] [blame] | 716 | } else { |
| 717 | /* We didn't find a match, say so and return an error */ |
| 718 | ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point); |
| 719 | } |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 720 | |
| 721 | out: |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * mount a tmpfs filesystem at the given point. |
| 727 | * return 0 on success, non-zero on failure. |
| 728 | */ |
| 729 | int fs_mgr_do_tmpfs_mount(char *n_name) |
| 730 | { |
| 731 | int ret; |
| 732 | |
| 733 | ret = mount("tmpfs", n_name, "tmpfs", |
| 734 | MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS); |
| 735 | if (ret < 0) { |
| 736 | ERROR("Cannot mount tmpfs filesystem at %s\n", n_name); |
| 737 | return -1; |
| 738 | } |
| 739 | |
| 740 | /* Success */ |
| 741 | return 0; |
| 742 | } |
| 743 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 744 | int fs_mgr_unmount_all(struct fstab *fstab) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 745 | { |
| 746 | int i = 0; |
| 747 | int ret = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 748 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 749 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 750 | return -1; |
| 751 | } |
| 752 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 753 | while (fstab->recs[i].blk_device) { |
| 754 | if (umount(fstab->recs[i].mount_point)) { |
| 755 | ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 756 | ret = -1; |
| 757 | } |
| 758 | i++; |
| 759 | } |
| 760 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 761 | return ret; |
| 762 | } |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 763 | |
| 764 | /* This must be called after mount_all, because the mkswap command needs to be |
| 765 | * available. |
| 766 | */ |
| 767 | int fs_mgr_swapon_all(struct fstab *fstab) |
| 768 | { |
| 769 | int i = 0; |
| 770 | int flags = 0; |
| 771 | int err = 0; |
| 772 | int ret = 0; |
| 773 | int status; |
| 774 | char *mkswap_argv[2] = { |
| 775 | MKSWAP_BIN, |
| 776 | NULL |
| 777 | }; |
| 778 | |
| 779 | if (!fstab) { |
| 780 | return -1; |
| 781 | } |
| 782 | |
| 783 | for (i = 0; i < fstab->num_entries; i++) { |
| 784 | /* Skip non-swap entries */ |
| 785 | if (strcmp(fstab->recs[i].fs_type, "swap")) { |
| 786 | continue; |
| 787 | } |
| 788 | |
| 789 | if (fstab->recs[i].zram_size > 0) { |
| 790 | /* A zram_size was specified, so we need to configure the |
| 791 | * device. There is no point in having multiple zram devices |
| 792 | * on a system (all the memory comes from the same pool) so |
| 793 | * we can assume the device number is 0. |
| 794 | */ |
| 795 | FILE *zram_fp; |
| 796 | |
| 797 | zram_fp = fopen(ZRAM_CONF_DEV, "r+"); |
| 798 | if (zram_fp == NULL) { |
JP Abgrall | 4bb7bba | 2014-06-19 22:12:20 -0700 | [diff] [blame] | 799 | ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 800 | ret = -1; |
| 801 | continue; |
| 802 | } |
| 803 | fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size); |
| 804 | fclose(zram_fp); |
| 805 | } |
| 806 | |
| 807 | if (fstab->recs[i].fs_mgr_flags & MF_WAIT) { |
| 808 | wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT); |
| 809 | } |
| 810 | |
| 811 | /* Initialize the swap area */ |
| 812 | mkswap_argv[1] = fstab->recs[i].blk_device; |
| 813 | err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv, |
Yusuke Sato | d81c3c6 | 2015-08-14 01:22:53 -0700 | [diff] [blame] | 814 | &status, true, LOG_KLOG, false, NULL, |
| 815 | NULL, 0); |
Ken Sumrall | 5bc31a2 | 2013-07-08 19:11:55 -0700 | [diff] [blame] | 816 | if (err) { |
| 817 | ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device); |
| 818 | ret = -1; |
| 819 | continue; |
| 820 | } |
| 821 | |
| 822 | /* If -1, then no priority was specified in fstab, so don't set |
| 823 | * SWAP_FLAG_PREFER or encode the priority */ |
| 824 | if (fstab->recs[i].swap_prio >= 0) { |
| 825 | flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) & |
| 826 | SWAP_FLAG_PRIO_MASK; |
| 827 | flags |= SWAP_FLAG_PREFER; |
| 828 | } else { |
| 829 | flags = 0; |
| 830 | } |
| 831 | err = swapon(fstab->recs[i].blk_device, flags); |
| 832 | if (err) { |
| 833 | ERROR("swapon failed for %s\n", fstab->recs[i].blk_device); |
| 834 | ret = -1; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | return ret; |
| 839 | } |
| 840 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 841 | /* |
| 842 | * key_loc must be at least PROPERTY_VALUE_MAX bytes long |
| 843 | * |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 844 | * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 845 | */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 846 | int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size) |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 847 | { |
| 848 | int i = 0; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 849 | |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 850 | if (!fstab) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 851 | return -1; |
| 852 | } |
| 853 | /* Initialize return values to null strings */ |
| 854 | if (key_loc) { |
| 855 | *key_loc = '\0'; |
| 856 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 857 | if (real_blk_device) { |
| 858 | *real_blk_device = '\0'; |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* Look for the encryptable partition to find the data */ |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 862 | for (i = 0; i < fstab->num_entries; i++) { |
| 863 | /* Don't deal with vold managed enryptable partitions here */ |
| 864 | if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) { |
| 865 | continue; |
| 866 | } |
Paul Lawrence | b262d68 | 2015-10-29 10:31:02 -0700 | [diff] [blame] | 867 | if (!(fstab->recs[i].fs_mgr_flags |
| 868 | & (MF_CRYPT | MF_FORCECRYPT | MF_FORCEFDEORFBE))) { |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 869 | continue; |
| 870 | } |
| 871 | |
| 872 | /* We found a match */ |
| 873 | if (key_loc) { |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 874 | strlcpy(key_loc, fstab->recs[i].key_loc, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 875 | } |
Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 876 | if (real_blk_device) { |
| 877 | strlcpy(real_blk_device, fstab->recs[i].blk_device, size); |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 878 | } |
| 879 | break; |
| 880 | } |
| 881 | |
Ken Sumrall | c1bf896 | 2012-01-06 19:09:42 -0800 | [diff] [blame] | 882 | return 0; |
| 883 | } |