blob: d5e94ac7ed00f094ee34c526be1eb0c8780b6a6f [file] [log] [blame]
Ken Sumrallc1bf8962012-01-06 19:09:42 -08001/*
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 Sumrallc1bf8962012-01-06 19:09:42 -080017#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 Sumrall5bc31a22013-07-08 19:11:55 -070030#include <sys/swap.h>
Christoffer Dall82982342014-12-17 21:26:54 +010031#include <dirent.h>
32#include <ext4.h>
33#include <ext4_sb.h>
Paul Lawrence806d10b2015-04-28 22:07:10 +000034#include <ext4_crypt_init_extensions.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080035
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080036#include <linux/loop.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080037#include <private/android_filesystem_config.h>
JP Abgrall4bb7bba2014-06-19 22:12:20 -070038#include <cutils/android_reboot.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080039#include <cutils/partition_utils.h>
40#include <cutils/properties.h>
Ken Sumrallbf021b42013-03-19 19:38:44 -070041#include <logwrap/logwrap.h>
Ken Sumrallc1bf8962012-01-06 19:09:42 -080042
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080043#include "mincrypt/rsa.h"
44#include "mincrypt/sha.h"
45#include "mincrypt/sha256.h"
46
Ken Sumrallc1bf8962012-01-06 19:09:42 -080047#include "fs_mgr_priv.h"
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080048#include "fs_mgr_priv_verity.h"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080049
50#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
51#define KEY_IN_FOOTER "footer"
52
53#define E2FSCK_BIN "/system/bin/e2fsck"
JP Abgrall12351582014-06-17 17:01:14 -070054#define F2FS_FSCK_BIN "/system/bin/fsck.f2fs"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070055#define MKSWAP_BIN "/system/bin/mkswap"
56
Ken Sumrall4eaf9052013-09-18 17:49:21 -070057#define FSCK_LOG_FILE "/dev/fscklogs/log"
58
Ken Sumrall5bc31a22013-07-08 19:11:55 -070059#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080060
Ken Sumrallbf021b42013-03-19 19:38:44 -070061#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
62
Ken Sumrallc1bf8962012-01-06 19:09:42 -080063/*
64 * gettime() - returns the time in seconds of the system's monotonic clock or
65 * zero on error.
66 */
67static 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
81static 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 Sumrallab6b8522013-02-13 12:58:40 -080093static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -080094{
Ken Sumrallc1bf8962012-01-06 19:09:42 -080095 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -070096 int ret;
97 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
98 char *tmpmnt_opts = "nomblk_io_submit,errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -070099 char *e2fsck_argv[] = {
100 E2FSCK_BIN,
101 "-y",
102 blk_device
103 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800104
105 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800106 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700107 /*
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 Hughes5e7dd442015-04-24 11:05:48 -0700120 errno = 0;
Ken Sumrallab6b8522013-02-13 12:58:40 -0800121 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700122 INFO("%s(): mount(%s,%s,%s)=%d: %s\n",
123 __func__, blk_device, target, fs_type, ret, strerror(errno));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800124 if (!ret) {
Nick Kralevich7294eb62015-02-05 16:02:42 -0800125 int i;
126 for (i = 0; i < 5; i++) {
127 // Try to umount 5 times before continuing on.
128 // Should we try rebooting if all attempts fail?
129 int result = umount(target);
130 if (result == 0) {
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700131 INFO("%s(): unmount(%s) succeeded\n", __func__, target);
Nick Kralevich7294eb62015-02-05 16:02:42 -0800132 break;
133 }
134 ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
135 sleep(1);
136 }
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700137 }
138
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100139 /*
140 * Some system images do not have e2fsck for licensing reasons
141 * (e.g. recent SDK system images). Detect these and skip the check.
142 */
143 if (access(E2FSCK_BIN, X_OK)) {
144 INFO("Not running %s on %s (executable not in system image)\n",
145 E2FSCK_BIN, blk_device);
146 } else {
147 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800148
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100149 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
150 &status, true, LOG_KLOG | LOG_FILE,
151 true, FSCK_LOG_FILE);
Ken Sumrallbf021b42013-03-19 19:38:44 -0700152
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100153 if (ret < 0) {
154 /* No need to check for error in fork, we can't really handle it now */
155 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
156 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800157 }
JP Abgrall12351582014-06-17 17:01:14 -0700158 } else if (!strcmp(fs_type, "f2fs")) {
159 char *f2fs_fsck_argv[] = {
160 F2FS_FSCK_BIN,
JP Abgralleb6036a2014-11-07 17:14:14 -0800161 "-f",
JP Abgrall12351582014-06-17 17:01:14 -0700162 blk_device
163 };
JP Abgralleb6036a2014-11-07 17:14:14 -0800164 INFO("Running %s -f %s\n", F2FS_FSCK_BIN, blk_device);
JP Abgrall12351582014-06-17 17:01:14 -0700165
166 ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv,
167 &status, true, LOG_KLOG | LOG_FILE,
168 true, FSCK_LOG_FILE);
169 if (ret < 0) {
170 /* No need to check for error in fork, we can't really handle it now */
171 ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN);
172 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800173 }
174
175 return;
176}
177
178static void remove_trailing_slashes(char *n)
179{
180 int len;
181
182 len = strlen(n) - 1;
183 while ((*(n + len) == '/') && len) {
184 *(n + len) = '\0';
185 len--;
186 }
187}
188
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700189/*
190 * Mark the given block device as read-only, using the BLKROSET ioctl.
191 * Return 0 on success, and -1 on error.
192 */
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000193int fs_mgr_set_blk_ro(const char *blockdev)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700194{
195 int fd;
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000196 int rc = -1;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700197 int ON = 1;
198
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000199 fd = TEMP_FAILURE_RETRY(open(blockdev, O_RDONLY | O_CLOEXEC));
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700200 if (fd < 0) {
201 // should never happen
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000202 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700203 }
204
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000205 rc = ioctl(fd, BLKROSET, &ON);
206 TEMP_FAILURE_RETRY(close(fd));
207
208 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700209}
210
211/*
212 * __mount(): wrapper around the mount() system call which also
213 * sets the underlying block device to read-only if the mount is read-only.
214 * See "man 2 mount" for return values.
215 */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700216static int __mount(const char *source, const char *target, const struct fstab_rec *rec)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700217{
JP Abgrall5c01dac2014-06-18 14:54:37 -0700218 unsigned long mountflags = rec->flags;
219 int ret;
220 int save_errno;
Daniel Rosenbergf67d6bd2014-06-26 14:55:04 -0700221
222 /* We need this because sometimes we have legacy symlinks
223 * that are lingering around and need cleaning up.
224 */
225 struct stat info;
226 if (!lstat(target, &info))
227 if ((info.st_mode & S_IFMT) == S_IFLNK)
228 unlink(target);
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700229 mkdir(target, 0755);
JP Abgrall5c01dac2014-06-18 14:54:37 -0700230 ret = mount(source, target, rec->fs_type, mountflags, rec->fs_options);
231 save_errno = errno;
232 INFO("%s(source=%s,target=%s,type=%s)=%d\n", __func__, source, target, rec->fs_type, ret);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700233 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000234 fs_mgr_set_blk_ro(source);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700235 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700236 errno = save_errno;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700237 return ret;
238}
239
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800240static int fs_match(char *in1, char *in2)
241{
242 char *n1;
243 char *n2;
244 int ret;
245
246 n1 = strdup(in1);
247 n2 = strdup(in2);
248
249 remove_trailing_slashes(n1);
250 remove_trailing_slashes(n2);
251
252 ret = !strcmp(n1, n2);
253
254 free(n1);
255 free(n2);
256
257 return ret;
258}
259
Geremy Condracd642fc2014-04-02 13:42:06 -0700260static int device_is_debuggable() {
261 int ret = -1;
262 char value[PROP_VALUE_MAX];
263 ret = __system_property_get("ro.debuggable", value);
264 if (ret < 0)
265 return ret;
266 return strcmp(value, "1") ? 0 : 1;
267}
268
Paul Lawrencebbb36312014-10-09 14:22:49 +0000269static int device_is_secure() {
270 int ret = -1;
271 char value[PROP_VALUE_MAX];
272 ret = __system_property_get("ro.secure", value);
273 /* If error, we want to fail secure */
274 if (ret < 0)
275 return 1;
276 return strcmp(value, "0") ? 1 : 0;
277}
278
Paul Lawrence703b87d2015-01-07 11:44:51 -0800279static int device_is_force_encrypted() {
280 int ret = -1;
281 char value[PROP_VALUE_MAX];
282 ret = __system_property_get("ro.vold.forceencryption", value);
283 if (ret < 0)
284 return 0;
285 return strcmp(value, "1") ? 0 : 1;
286}
287
JP Abgrallf22b7452014-07-02 13:16:04 -0700288/*
289 * Tries to mount any of the consecutive fstab entries that match
290 * the mountpoint of the one given by fstab->recs[start_idx].
291 *
292 * end_idx: On return, will be the last rec that was looked at.
293 * attempted_idx: On return, will indicate which fstab rec
294 * succeeded. In case of failure, it will be the start_idx.
295 * Returns
296 * -1 on failure with errno set to match the 1st mount failure.
297 * 0 on success.
298 */
299static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx)
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700300{
JP Abgrallf22b7452014-07-02 13:16:04 -0700301 int i;
302 int mount_errno = 0;
303 int mounted = 0;
304
305 if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) {
306 errno = EINVAL;
307 if (end_idx) *end_idx = start_idx;
308 if (attempted_idx) *end_idx = start_idx;
309 return -1;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700310 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700311
312 /* Hunt down an fstab entry for the same mount point that might succeed */
313 for (i = start_idx;
314 /* We required that fstab entries for the same mountpoint be consecutive */
315 i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point);
316 i++) {
317 /*
318 * Don't try to mount/encrypt the same mount point again.
319 * Deal with alternate entries for the same point which are required to be all following
320 * each other.
321 */
322 if (mounted) {
323 ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__,
324 fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
325 continue;
326 }
327
328 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
329 check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
330 fstab->recs[i].mount_point);
331 }
332 if (!__mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, &fstab->recs[i])) {
333 *attempted_idx = i;
334 mounted = 1;
335 if (i != start_idx) {
336 ERROR("%s(): Mounted %s on %s with fs_type=%s instead of %s\n", __func__,
337 fstab->recs[i].blk_device, fstab->recs[i].mount_point, fstab->recs[i].fs_type,
338 fstab->recs[start_idx].fs_type);
339 }
340 } else {
341 /* back up errno for crypto decisions */
342 mount_errno = errno;
343 }
344 }
345
346 /* Adjust i for the case where it was still withing the recs[] */
347 if (i < fstab->num_entries) --i;
348
349 *end_idx = i;
350 if (!mounted) {
351 *attempted_idx = start_idx;
352 errno = mount_errno;
353 return -1;
354 }
355 return 0;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700356}
357
Christoffer Dall82982342014-12-17 21:26:54 +0100358static int translate_ext_labels(struct fstab_rec *rec)
359{
360 DIR *blockdir = NULL;
361 struct dirent *ent;
362 char *label;
363 size_t label_len;
364 int ret = -1;
365
366 if (strncmp(rec->blk_device, "LABEL=", 6))
367 return 0;
368
369 label = rec->blk_device + 6;
370 label_len = strlen(label);
371
372 if (label_len > 16) {
373 ERROR("FS label is longer than allowed by filesystem\n");
374 goto out;
375 }
376
377
378 blockdir = opendir("/dev/block");
379 if (!blockdir) {
380 ERROR("couldn't open /dev/block\n");
381 goto out;
382 }
383
384 while ((ent = readdir(blockdir))) {
385 int fd;
386 char super_buf[1024];
387 struct ext4_super_block *sb;
388
389 if (ent->d_type != DT_BLK)
390 continue;
391
392 fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY);
393 if (fd < 0) {
394 ERROR("Cannot open block device /dev/block/%s\n", ent->d_name);
395 goto out;
396 }
397
398 if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
399 TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) {
400 /* Probably a loopback device or something else without a readable
401 * superblock.
402 */
403 close(fd);
404 continue;
405 }
406
407 sb = (struct ext4_super_block *)super_buf;
408 if (sb->s_magic != EXT4_SUPER_MAGIC) {
409 INFO("/dev/block/%s not ext{234}\n", ent->d_name);
410 continue;
411 }
412
413 if (!strncmp(label, sb->s_volume_name, label_len)) {
414 char *new_blk_device;
415
416 if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) {
417 ERROR("Could not allocate block device string\n");
418 goto out;
419 }
420
421 INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device);
422
423 free(rec->blk_device);
424 rec->blk_device = new_blk_device;
425 ret = 0;
426 break;
427 }
428 }
429
430out:
431 closedir(blockdir);
432 return ret;
433}
434
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000435// Check to see if a mountable volume has encryption requirements
436static int handle_encryptable(struct fstab *fstab, const struct fstab_rec* rec)
437{
438 /* If this is block encryptable, need to trigger encryption */
439 if ( (rec->fs_mgr_flags & MF_FORCECRYPT)
440 || (device_is_force_encrypted() && fs_mgr_is_encryptable(rec))) {
441 if (umount(rec->mount_point) == 0) {
442 return FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION;
443 } else {
444 WARNING("Could not umount %s (%s) - allow continue unencrypted\n",
445 rec->mount_point, strerror(errno));
446 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
447 }
448 }
449
450 // Deal with file level encryption
451 if (rec->fs_mgr_flags & MF_FILEENCRYPTION) {
452 // Default or not yet initialized encryption requires no more work here
453 if (!e4crypt_non_default_key(rec->mount_point)) {
454 INFO("%s is default file encrypted\n", rec->mount_point);
455 return FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED;
456 }
457
458 INFO("%s is non-default file encrypted\n", rec->mount_point);
459
460 // Uses non-default key, so must unmount and set up temp file system
461 if (umount(rec->mount_point)) {
462 ERROR("Failed to umount %s - rebooting\n", rec->mount_point);
463 return FS_MGR_MNTALL_FAIL;
464 }
465
466 if (fs_mgr_do_tmpfs_mount(rec->mount_point) != 0) {
467 ERROR("Failed to mount a tmpfs at %s\n", rec->mount_point);
468 return FS_MGR_MNTALL_FAIL;
469 }
470
471 // Mount data temporarily so we can access unencrypted dir
472 char tmp_mnt[PATH_MAX];
473 strlcpy(tmp_mnt, rec->mount_point, sizeof(tmp_mnt));
474 strlcat(tmp_mnt, "/tmp_mnt", sizeof(tmp_mnt));
475 if (mkdir(tmp_mnt, 0700)) {
476 ERROR("Failed to create temp mount point\n");
477 return FS_MGR_MNTALL_FAIL;
478 }
479
480 if (fs_mgr_do_mount(fstab, rec->mount_point,
481 rec->blk_device, tmp_mnt)) {
482 ERROR("Error temp mounting encrypted file system\n");
483 return FS_MGR_MNTALL_FAIL;
484 }
485
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000486 return FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED;
487 }
488
489 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
490}
491
JP Abgrall5c01dac2014-06-18 14:54:37 -0700492/* When multiple fstab records share the same mount_point, it will
493 * try to mount each one in turn, and ignore any duplicates after a
494 * first successful mount.
JP Abgrallf22b7452014-07-02 13:16:04 -0700495 * Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
JP Abgrall5c01dac2014-06-18 14:54:37 -0700496 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800497int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800498{
JP Abgrallf22b7452014-07-02 13:16:04 -0700499 int i = 0;
500 int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700501 int error_count = 0;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700502 int mret = -1;
503 int mount_errno = 0;
JP Abgrallf22b7452014-07-02 13:16:04 -0700504 int attempted_idx = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800505
Ken Sumrallab6b8522013-02-13 12:58:40 -0800506 if (!fstab) {
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700507 return -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800508 }
509
Ken Sumrallab6b8522013-02-13 12:58:40 -0800510 for (i = 0; i < fstab->num_entries; i++) {
511 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800512 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800513 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800514 }
515
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700516 /* Skip swap and raw partition entries such as boot, recovery, etc */
517 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
518 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800519 !strcmp(fstab->recs[i].fs_type, "mtd")) {
520 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800521 }
522
Christoffer Dall82982342014-12-17 21:26:54 +0100523 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800534 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
535 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
536 }
537
Paul Lawrencebbb36312014-10-09 14:22:49 +0000538 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700543 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800544 continue;
545 }
546 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700547 int last_idx_inspected;
548 mret = mount_with_alternatives(fstab, i, &last_idx_inspected, &attempted_idx);
549 i = last_idx_inspected;
550 mount_errno = errno;
JP Abgrallf786fe52014-06-18 07:28:14 +0000551
JP Abgrall5c01dac2014-06-18 14:54:37 -0700552 /* Deal with encryptability. */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800553 if (!mret) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000554 int status = handle_encryptable(fstab, &fstab->recs[attempted_idx]);
555
556 if (status == FS_MGR_MNTALL_FAIL) {
557 /* Fatal error - no point continuing */
558 return status;
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800559 }
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000560
561 if (status != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
562 if (encryptable != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
563 // Log and continue
564 ERROR("Only one encryptable/encrypted partition supported\n");
565 }
566 encryptable = status;
567 }
568
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800569 /* Success! Go get the next one */
570 continue;
571 }
572
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700573 /* mount(2) returned an error, check if it's encryptable and deal with it */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700574 if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
JP Abgrallcee20682014-07-02 14:26:54 -0700575 fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) {
576 if(partition_wiped(fstab->recs[attempted_idx].blk_device)) {
577 ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__,
578 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
579 fstab->recs[attempted_idx].fs_type);
580 encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700581 continue;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700582 } else {
583 /* Need to mount a tmpfs at this mountpoint for now, and set
584 * properties that vold will query later for decrypting
585 */
JP Abgrallf22b7452014-07-02 13:16:04 -0700586 ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__,
587 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
588 fstab->recs[attempted_idx].fs_type);
589 if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700590 ++error_count;
591 continue;
592 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800593 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700594 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800595 } else {
William Roberts071f28a2014-01-14 14:33:44 -0500596 ERROR("Failed to mount an un-encryptable or wiped partition on"
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700597 "%s at %s options: %s error: %s\n",
JP Abgrallf22b7452014-07-02 13:16:04 -0700598 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
599 fstab->recs[attempted_idx].fs_options, strerror(mount_errno));
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700600 ++error_count;
601 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800602 }
603 }
604
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700605 if (error_count) {
606 return -1;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700607 } else {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800608 return encryptable;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700609 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800610}
611
Ken Sumrallab6b8522013-02-13 12:58:40 -0800612/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800613 * tmp mount we do to check the user password
JP Abgrall5c01dac2014-06-18 14:54:37 -0700614 * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one
615 * in turn, and stop on 1st success, or no more match.
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800616 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800617int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
618 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800619{
620 int i = 0;
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700621 int ret = FS_MGR_DOMNT_FAILED;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700622 int mount_errors = 0;
623 int first_mount_errno = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800624 char *m;
625
Ken Sumrallab6b8522013-02-13 12:58:40 -0800626 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800627 return ret;
628 }
629
Ken Sumrallab6b8522013-02-13 12:58:40 -0800630 for (i = 0; i < fstab->num_entries; i++) {
631 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800632 continue;
633 }
634
635 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700636 /* If this swap or a raw partition, report an error */
637 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
638 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800639 !strcmp(fstab->recs[i].fs_type, "mtd")) {
640 ERROR("Cannot mount filesystem of type %s on %s\n",
641 fstab->recs[i].fs_type, n_blk_device);
642 goto out;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800643 }
644
Ken Sumrallab6b8522013-02-13 12:58:40 -0800645 /* First check the filesystem if requested */
646 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
647 wait_for_file(n_blk_device, WAIT_TIMEOUT);
648 }
649
650 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
651 check_fs(n_blk_device, fstab->recs[i].fs_type,
652 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800653 }
654
Paul Lawrencebbb36312014-10-09 14:22:49 +0000655 if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
656 int rc = fs_mgr_setup_verity(&fstab->recs[i]);
657 if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
658 INFO("Verity disabled");
659 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700660 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800661 continue;
662 }
663 }
664
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800665 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800666 if (tmp_mount_point) {
667 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800668 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800669 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800670 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700671 if (__mount(n_blk_device, m, &fstab->recs[i])) {
672 if (!first_mount_errno) first_mount_errno = errno;
673 mount_errors++;
674 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800675 } else {
676 ret = 0;
677 goto out;
678 }
679 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700680 if (mount_errors) {
681 ERROR("Cannot mount filesystem on %s at %s. error: %s\n",
682 n_blk_device, m, strerror(first_mount_errno));
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700683 if (first_mount_errno == EBUSY) {
684 ret = FS_MGR_DOMNT_BUSY;
685 } else {
686 ret = FS_MGR_DOMNT_FAILED;
687 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700688 } else {
689 /* We didn't find a match, say so and return an error */
690 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
691 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800692
693out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800694 return ret;
695}
696
697/*
698 * mount a tmpfs filesystem at the given point.
699 * return 0 on success, non-zero on failure.
700 */
701int fs_mgr_do_tmpfs_mount(char *n_name)
702{
703 int ret;
704
705 ret = mount("tmpfs", n_name, "tmpfs",
706 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
707 if (ret < 0) {
708 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
709 return -1;
710 }
711
712 /* Success */
713 return 0;
714}
715
Ken Sumrallab6b8522013-02-13 12:58:40 -0800716int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800717{
718 int i = 0;
719 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800720
Ken Sumrallab6b8522013-02-13 12:58:40 -0800721 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800722 return -1;
723 }
724
Ken Sumrallab6b8522013-02-13 12:58:40 -0800725 while (fstab->recs[i].blk_device) {
726 if (umount(fstab->recs[i].mount_point)) {
727 ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800728 ret = -1;
729 }
730 i++;
731 }
732
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800733 return ret;
734}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700735
736/* This must be called after mount_all, because the mkswap command needs to be
737 * available.
738 */
739int fs_mgr_swapon_all(struct fstab *fstab)
740{
741 int i = 0;
742 int flags = 0;
743 int err = 0;
744 int ret = 0;
745 int status;
746 char *mkswap_argv[2] = {
747 MKSWAP_BIN,
748 NULL
749 };
750
751 if (!fstab) {
752 return -1;
753 }
754
755 for (i = 0; i < fstab->num_entries; i++) {
756 /* Skip non-swap entries */
757 if (strcmp(fstab->recs[i].fs_type, "swap")) {
758 continue;
759 }
760
761 if (fstab->recs[i].zram_size > 0) {
762 /* A zram_size was specified, so we need to configure the
763 * device. There is no point in having multiple zram devices
764 * on a system (all the memory comes from the same pool) so
765 * we can assume the device number is 0.
766 */
767 FILE *zram_fp;
768
769 zram_fp = fopen(ZRAM_CONF_DEV, "r+");
770 if (zram_fp == NULL) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700771 ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700772 ret = -1;
773 continue;
774 }
775 fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size);
776 fclose(zram_fp);
777 }
778
779 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
780 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
781 }
782
783 /* Initialize the swap area */
784 mkswap_argv[1] = fstab->recs[i].blk_device;
785 err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv,
Ken Sumrall4eaf9052013-09-18 17:49:21 -0700786 &status, true, LOG_KLOG, false, NULL);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700787 if (err) {
788 ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device);
789 ret = -1;
790 continue;
791 }
792
793 /* If -1, then no priority was specified in fstab, so don't set
794 * SWAP_FLAG_PREFER or encode the priority */
795 if (fstab->recs[i].swap_prio >= 0) {
796 flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) &
797 SWAP_FLAG_PRIO_MASK;
798 flags |= SWAP_FLAG_PREFER;
799 } else {
800 flags = 0;
801 }
802 err = swapon(fstab->recs[i].blk_device, flags);
803 if (err) {
804 ERROR("swapon failed for %s\n", fstab->recs[i].blk_device);
805 ret = -1;
806 }
807 }
808
809 return ret;
810}
811
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800812/*
813 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
814 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800815 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800816 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800817int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800818{
819 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800820
Ken Sumrallab6b8522013-02-13 12:58:40 -0800821 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800822 return -1;
823 }
824 /* Initialize return values to null strings */
825 if (key_loc) {
826 *key_loc = '\0';
827 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800828 if (real_blk_device) {
829 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800830 }
831
832 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800833 for (i = 0; i < fstab->num_entries; i++) {
834 /* Don't deal with vold managed enryptable partitions here */
835 if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) {
836 continue;
837 }
Paul Lawrence2e5ae0a2014-04-04 09:34:19 -0700838 if (!(fstab->recs[i].fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT))) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800839 continue;
840 }
841
842 /* We found a match */
843 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800844 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800845 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800846 if (real_blk_device) {
847 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800848 }
849 break;
850 }
851
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800852 return 0;
853}