blob: 044c89525453ab4c5f893650ded30397072d4130 [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;
Oreste Salerno6ed84c92015-05-20 17:01:39 +000098 char tmpmnt_opts[64] = "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;
Oreste Salerno6ed84c92015-05-20 17:01:39 +0000121 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 Sumrallab6b8522013-02-13 12:58:40 -0800125 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700126 INFO("%s(): mount(%s,%s,%s)=%d: %s\n",
127 __func__, blk_device, target, fs_type, ret, strerror(errno));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800128 if (!ret) {
Nick Kralevich7294eb62015-02-05 16:02:42 -0800129 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 Hughes5e7dd442015-04-24 11:05:48 -0700135 INFO("%s(): unmount(%s) succeeded\n", __func__, target);
Nick Kralevich7294eb62015-02-05 16:02:42 -0800136 break;
137 }
138 ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
139 sleep(1);
140 }
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700141 }
142
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100143 /*
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 Sumrallc1bf8962012-01-06 19:09:42 -0800152
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100153 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700154 &status, true, LOG_KLOG | LOG_FILE,
155 true, FSCK_LOG_FILE, NULL, 0);
Ken Sumrallbf021b42013-03-19 19:38:44 -0700156
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100157 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 Sumrallc1bf8962012-01-06 19:09:42 -0800161 }
JP Abgrall12351582014-06-17 17:01:14 -0700162 } else if (!strcmp(fs_type, "f2fs")) {
163 char *f2fs_fsck_argv[] = {
164 F2FS_FSCK_BIN,
Yusuke Sato0df08272015-07-08 14:57:07 -0700165 "-a",
JP Abgrall12351582014-06-17 17:01:14 -0700166 blk_device
167 };
Yusuke Sato0df08272015-07-08 14:57:07 -0700168 INFO("Running %s -a %s\n", F2FS_FSCK_BIN, blk_device);
JP Abgrall12351582014-06-17 17:01:14 -0700169
170 ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv,
171 &status, true, LOG_KLOG | LOG_FILE,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700172 true, FSCK_LOG_FILE, NULL, 0);
JP Abgrall12351582014-06-17 17:01:14 -0700173 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 Sumrallc1bf8962012-01-06 19:09:42 -0800177 }
178
179 return;
180}
181
182static 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 Kraleviche18c0d52013-04-16 16:41:32 -0700193/*
194 * Mark the given block device as read-only, using the BLKROSET ioctl.
195 * Return 0 on success, and -1 on error.
196 */
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000197int fs_mgr_set_blk_ro(const char *blockdev)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700198{
199 int fd;
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000200 int rc = -1;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700201 int ON = 1;
202
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000203 fd = TEMP_FAILURE_RETRY(open(blockdev, O_RDONLY | O_CLOEXEC));
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700204 if (fd < 0) {
205 // should never happen
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000206 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700207 }
208
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000209 rc = ioctl(fd, BLKROSET, &ON);
Elliott Hughes47b01342015-05-15 19:16:40 -0700210 close(fd);
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000211
212 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700213}
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 Abgrall5c01dac2014-06-18 14:54:37 -0700220static int __mount(const char *source, const char *target, const struct fstab_rec *rec)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700221{
JP Abgrall5c01dac2014-06-18 14:54:37 -0700222 unsigned long mountflags = rec->flags;
223 int ret;
224 int save_errno;
Daniel Rosenbergf67d6bd2014-06-26 14:55:04 -0700225
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 Rosenbergf530c932014-05-28 14:10:01 -0700233 mkdir(target, 0755);
JP Abgrall5c01dac2014-06-18 14:54:37 -0700234 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 Kraleviche18c0d52013-04-16 16:41:32 -0700237 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000238 fs_mgr_set_blk_ro(source);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700239 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700240 errno = save_errno;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700241 return ret;
242}
243
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800244static 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 Condracd642fc2014-04-02 13:42:06 -0700264static 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 Lawrencebbb36312014-10-09 14:22:49 +0000273static 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 Lawrence703b87d2015-01-07 11:44:51 -0800283static 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 Abgrallf22b7452014-07-02 13:16:04 -0700292/*
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 */
303static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx)
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700304{
JP Abgrallf22b7452014-07-02 13:16:04 -0700305 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700314 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700315
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 Abgrall4bb7bba2014-06-19 22:12:20 -0700360}
361
Christoffer Dall82982342014-12-17 21:26:54 +0100362static 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
434out:
435 closedir(blockdir);
436 return ret;
437}
438
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000439// Check to see if a mountable volume has encryption requirements
440static 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 Lawrenceb8c9d272015-03-26 15:49:42 +0000490 return FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED;
491 }
492
493 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
494}
495
JP Abgrall5c01dac2014-06-18 14:54:37 -0700496/* 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 Abgrallf22b7452014-07-02 13:16:04 -0700499 * Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
JP Abgrall5c01dac2014-06-18 14:54:37 -0700500 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800501int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800502{
JP Abgrallf22b7452014-07-02 13:16:04 -0700503 int i = 0;
504 int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700505 int error_count = 0;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700506 int mret = -1;
507 int mount_errno = 0;
JP Abgrallf22b7452014-07-02 13:16:04 -0700508 int attempted_idx = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800509
Ken Sumrallab6b8522013-02-13 12:58:40 -0800510 if (!fstab) {
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700511 return -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800512 }
513
Ken Sumrallab6b8522013-02-13 12:58:40 -0800514 for (i = 0; i < fstab->num_entries; i++) {
515 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800516 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800517 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800518 }
519
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700520 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800523 !strcmp(fstab->recs[i].fs_type, "mtd")) {
524 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800525 }
526
Daniel Rosenberg31a4faf2015-06-29 17:33:05 -0700527 /* 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 Dall82982342014-12-17 21:26:54 +0100535 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800546 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
547 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
548 }
549
Paul Lawrencebbb36312014-10-09 14:22:49 +0000550 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700555 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800556 continue;
557 }
558 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700559 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 Abgrallf786fe52014-06-18 07:28:14 +0000563
JP Abgrall5c01dac2014-06-18 14:54:37 -0700564 /* Deal with encryptability. */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800565 if (!mret) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000566 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 Lawrence166fa3d2014-02-03 13:27:49 -0800571 }
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000572
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 Sumrallc1bf8962012-01-06 19:09:42 -0800581 /* Success! Go get the next one */
582 continue;
583 }
584
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700585 /* mount(2) returned an error, check if it's encryptable and deal with it */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700586 if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
JP Abgrallcee20682014-07-02 14:26:54 -0700587 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 Ayyash38afe5f2014-03-10 15:40:29 -0700593 continue;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700594 } 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 Abgrallf22b7452014-07-02 13:16:04 -0700598 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700602 ++error_count;
603 continue;
604 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800605 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700606 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800607 } else {
William Roberts071f28a2014-01-14 14:33:44 -0500608 ERROR("Failed to mount an un-encryptable or wiped partition on"
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700609 "%s at %s options: %s error: %s\n",
JP Abgrallf22b7452014-07-02 13:16:04 -0700610 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
611 fstab->recs[attempted_idx].fs_options, strerror(mount_errno));
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700612 ++error_count;
613 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800614 }
615 }
616
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700617 if (error_count) {
618 return -1;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700619 } else {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800620 return encryptable;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700621 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800622}
623
Ken Sumrallab6b8522013-02-13 12:58:40 -0800624/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800625 * tmp mount we do to check the user password
JP Abgrall5c01dac2014-06-18 14:54:37 -0700626 * 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 Sumrallc1bf8962012-01-06 19:09:42 -0800628 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800629int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
630 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800631{
632 int i = 0;
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700633 int ret = FS_MGR_DOMNT_FAILED;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700634 int mount_errors = 0;
635 int first_mount_errno = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800636 char *m;
637
Ken Sumrallab6b8522013-02-13 12:58:40 -0800638 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800639 return ret;
640 }
641
Ken Sumrallab6b8522013-02-13 12:58:40 -0800642 for (i = 0; i < fstab->num_entries; i++) {
643 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800644 continue;
645 }
646
647 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700648 /* 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 Sumrallab6b8522013-02-13 12:58:40 -0800651 !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 Sumrallc1bf8962012-01-06 19:09:42 -0800655 }
656
Ken Sumrallab6b8522013-02-13 12:58:40 -0800657 /* 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 Sumrallc1bf8962012-01-06 19:09:42 -0800665 }
666
Paul Lawrencebbb36312014-10-09 14:22:49 +0000667 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700672 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800673 continue;
674 }
675 }
676
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800677 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800678 if (tmp_mount_point) {
679 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800680 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800681 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800682 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700683 if (__mount(n_blk_device, m, &fstab->recs[i])) {
684 if (!first_mount_errno) first_mount_errno = errno;
685 mount_errors++;
686 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800687 } else {
688 ret = 0;
689 goto out;
690 }
691 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700692 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 Lawrencecf234dc2014-09-09 10:44:51 -0700695 if (first_mount_errno == EBUSY) {
696 ret = FS_MGR_DOMNT_BUSY;
697 } else {
698 ret = FS_MGR_DOMNT_FAILED;
699 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700700 } 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 Sumrallc1bf8962012-01-06 19:09:42 -0800704
705out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800706 return ret;
707}
708
709/*
710 * mount a tmpfs filesystem at the given point.
711 * return 0 on success, non-zero on failure.
712 */
713int 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 Sumrallab6b8522013-02-13 12:58:40 -0800728int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800729{
730 int i = 0;
731 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800732
Ken Sumrallab6b8522013-02-13 12:58:40 -0800733 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800734 return -1;
735 }
736
Ken Sumrallab6b8522013-02-13 12:58:40 -0800737 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 Sumrallc1bf8962012-01-06 19:09:42 -0800740 ret = -1;
741 }
742 i++;
743 }
744
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800745 return ret;
746}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700747
748/* This must be called after mount_all, because the mkswap command needs to be
749 * available.
750 */
751int 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 Abgrall4bb7bba2014-06-19 22:12:20 -0700783 ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700784 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 Satod81c3c62015-08-14 01:22:53 -0700798 &status, true, LOG_KLOG, false, NULL,
799 NULL, 0);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700800 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 Sumrallc1bf8962012-01-06 19:09:42 -0800825/*
826 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
827 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800828 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800829 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800830int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800831{
832 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800833
Ken Sumrallab6b8522013-02-13 12:58:40 -0800834 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800835 return -1;
836 }
837 /* Initialize return values to null strings */
838 if (key_loc) {
839 *key_loc = '\0';
840 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800841 if (real_blk_device) {
842 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800843 }
844
845 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800846 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 Lawrence2e5ae0a2014-04-04 09:34:19 -0700851 if (!(fstab->recs[i].fs_mgr_flags & (MF_CRYPT | MF_FORCECRYPT))) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800852 continue;
853 }
854
855 /* We found a match */
856 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800857 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800858 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800859 if (real_blk_device) {
860 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800861 }
862 break;
863 }
864
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800865 return 0;
866}