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