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