blob: 3fda34ffb4accdf74e3b7d0fbb1d13d49580da53 [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
Chris Fries79f33842013-09-05 13:19:21 -050047#include "ext4_utils.h"
48#include "wipe.h"
49
Ken Sumrallc1bf8962012-01-06 19:09:42 -080050#include "fs_mgr_priv.h"
Geremy Condra3ad3d1c2013-02-22 18:11:41 -080051#include "fs_mgr_priv_verity.h"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080052
53#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
54#define KEY_IN_FOOTER "footer"
55
56#define E2FSCK_BIN "/system/bin/e2fsck"
JP Abgrall12351582014-06-17 17:01:14 -070057#define F2FS_FSCK_BIN "/system/bin/fsck.f2fs"
Ken Sumrall5bc31a22013-07-08 19:11:55 -070058#define MKSWAP_BIN "/system/bin/mkswap"
59
Ken Sumrall4eaf9052013-09-18 17:49:21 -070060#define FSCK_LOG_FILE "/dev/fscklogs/log"
61
Ken Sumrall5bc31a22013-07-08 19:11:55 -070062#define ZRAM_CONF_DEV "/sys/block/zram0/disksize"
Ken Sumrallc1bf8962012-01-06 19:09:42 -080063
Ken Sumrallbf021b42013-03-19 19:38:44 -070064#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
65
Ken Sumrallc1bf8962012-01-06 19:09:42 -080066/*
67 * gettime() - returns the time in seconds of the system's monotonic clock or
68 * zero on error.
69 */
70static time_t gettime(void)
71{
72 struct timespec ts;
73 int ret;
74
75 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
76 if (ret < 0) {
77 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
78 return 0;
79 }
80
81 return ts.tv_sec;
82}
83
84static int wait_for_file(const char *filename, int timeout)
85{
86 struct stat info;
87 time_t timeout_time = gettime() + timeout;
88 int ret = -1;
89
90 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
91 usleep(10000);
92
93 return ret;
94}
95
Ken Sumrallab6b8522013-02-13 12:58:40 -080096static void check_fs(char *blk_device, char *fs_type, char *target)
Ken Sumrallc1bf8962012-01-06 19:09:42 -080097{
Ken Sumrallc1bf8962012-01-06 19:09:42 -080098 int status;
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -070099 int ret;
100 long tmpmnt_flags = MS_NOATIME | MS_NOEXEC | MS_NOSUID;
Oreste Salerno6ed84c92015-05-20 17:01:39 +0000101 char tmpmnt_opts[64] = "errors=remount-ro";
Ken Sumrallbf021b42013-03-19 19:38:44 -0700102 char *e2fsck_argv[] = {
103 E2FSCK_BIN,
104 "-y",
105 blk_device
106 };
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800107
108 /* Check for the types of filesystems we know how to check */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800109 if (!strcmp(fs_type, "ext2") || !strcmp(fs_type, "ext3") || !strcmp(fs_type, "ext4")) {
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700110 /*
111 * First try to mount and unmount the filesystem. We do this because
112 * the kernel is more efficient than e2fsck in running the journal and
113 * processing orphaned inodes, and on at least one device with a
114 * performance issue in the emmc firmware, it can take e2fsck 2.5 minutes
115 * to do what the kernel does in about a second.
116 *
117 * After mounting and unmounting the filesystem, run e2fsck, and if an
118 * error is recorded in the filesystem superblock, e2fsck will do a full
119 * check. Otherwise, it does nothing. If the kernel cannot mount the
120 * filesytsem due to an error, e2fsck is still run to do a full check
121 * fix the filesystem.
122 */
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700123 errno = 0;
Oreste Salerno6ed84c92015-05-20 17:01:39 +0000124 if (!strcmp(fs_type, "ext4")) {
125 // This option is only valid with ext4
126 strlcat(tmpmnt_opts, ",nomblk_io_submit", sizeof(tmpmnt_opts));
127 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800128 ret = mount(blk_device, target, fs_type, tmpmnt_flags, tmpmnt_opts);
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700129 INFO("%s(): mount(%s,%s,%s)=%d: %s\n",
130 __func__, blk_device, target, fs_type, ret, strerror(errno));
Ken Sumrallab6b8522013-02-13 12:58:40 -0800131 if (!ret) {
Nick Kralevich7294eb62015-02-05 16:02:42 -0800132 int i;
133 for (i = 0; i < 5; i++) {
134 // Try to umount 5 times before continuing on.
135 // Should we try rebooting if all attempts fail?
136 int result = umount(target);
137 if (result == 0) {
Elliott Hughes5e7dd442015-04-24 11:05:48 -0700138 INFO("%s(): unmount(%s) succeeded\n", __func__, target);
Nick Kralevich7294eb62015-02-05 16:02:42 -0800139 break;
140 }
141 ERROR("%s(): umount(%s)=%d: %s\n", __func__, target, result, strerror(errno));
142 sleep(1);
143 }
Ken Sumrall5dc5bfe2012-07-23 19:34:00 -0700144 }
145
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100146 /*
147 * Some system images do not have e2fsck for licensing reasons
148 * (e.g. recent SDK system images). Detect these and skip the check.
149 */
150 if (access(E2FSCK_BIN, X_OK)) {
151 INFO("Not running %s on %s (executable not in system image)\n",
152 E2FSCK_BIN, blk_device);
153 } else {
154 INFO("Running %s on %s\n", E2FSCK_BIN, blk_device);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800155
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100156 ret = android_fork_execvp_ext(ARRAY_SIZE(e2fsck_argv), e2fsck_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700157 &status, true, LOG_KLOG | LOG_FILE,
158 true, FSCK_LOG_FILE, NULL, 0);
Ken Sumrallbf021b42013-03-19 19:38:44 -0700159
David 'Digit' Turner28483d72014-02-17 11:14:44 +0100160 if (ret < 0) {
161 /* No need to check for error in fork, we can't really handle it now */
162 ERROR("Failed trying to run %s\n", E2FSCK_BIN);
163 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800164 }
JP Abgrall12351582014-06-17 17:01:14 -0700165 } else if (!strcmp(fs_type, "f2fs")) {
166 char *f2fs_fsck_argv[] = {
167 F2FS_FSCK_BIN,
Yusuke Sato0df08272015-07-08 14:57:07 -0700168 "-a",
JP Abgrall12351582014-06-17 17:01:14 -0700169 blk_device
170 };
Yusuke Sato0df08272015-07-08 14:57:07 -0700171 INFO("Running %s -a %s\n", F2FS_FSCK_BIN, blk_device);
JP Abgrall12351582014-06-17 17:01:14 -0700172
173 ret = android_fork_execvp_ext(ARRAY_SIZE(f2fs_fsck_argv), f2fs_fsck_argv,
174 &status, true, LOG_KLOG | LOG_FILE,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700175 true, FSCK_LOG_FILE, NULL, 0);
JP Abgrall12351582014-06-17 17:01:14 -0700176 if (ret < 0) {
177 /* No need to check for error in fork, we can't really handle it now */
178 ERROR("Failed trying to run %s\n", F2FS_FSCK_BIN);
179 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800180 }
181
182 return;
183}
184
185static void remove_trailing_slashes(char *n)
186{
187 int len;
188
189 len = strlen(n) - 1;
190 while ((*(n + len) == '/') && len) {
191 *(n + len) = '\0';
192 len--;
193 }
194}
195
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700196/*
197 * Mark the given block device as read-only, using the BLKROSET ioctl.
198 * Return 0 on success, and -1 on error.
199 */
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000200int fs_mgr_set_blk_ro(const char *blockdev)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700201{
202 int fd;
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000203 int rc = -1;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700204 int ON = 1;
205
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000206 fd = TEMP_FAILURE_RETRY(open(blockdev, O_RDONLY | O_CLOEXEC));
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700207 if (fd < 0) {
208 // should never happen
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000209 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700210 }
211
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000212 rc = ioctl(fd, BLKROSET, &ON);
Elliott Hughes9fc83432015-05-15 19:16:40 -0700213 close(fd);
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000214
215 return rc;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700216}
217
218/*
219 * __mount(): wrapper around the mount() system call which also
220 * sets the underlying block device to read-only if the mount is read-only.
221 * See "man 2 mount" for return values.
222 */
JP Abgrall5c01dac2014-06-18 14:54:37 -0700223static int __mount(const char *source, const char *target, const struct fstab_rec *rec)
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700224{
JP Abgrall5c01dac2014-06-18 14:54:37 -0700225 unsigned long mountflags = rec->flags;
226 int ret;
227 int save_errno;
Daniel Rosenbergf67d6bd2014-06-26 14:55:04 -0700228
229 /* We need this because sometimes we have legacy symlinks
230 * that are lingering around and need cleaning up.
231 */
232 struct stat info;
233 if (!lstat(target, &info))
234 if ((info.st_mode & S_IFMT) == S_IFLNK)
235 unlink(target);
Daniel Rosenbergf530c932014-05-28 14:10:01 -0700236 mkdir(target, 0755);
JP Abgrall5c01dac2014-06-18 14:54:37 -0700237 ret = mount(source, target, rec->fs_type, mountflags, rec->fs_options);
238 save_errno = errno;
239 INFO("%s(source=%s,target=%s,type=%s)=%d\n", __func__, source, target, rec->fs_type, ret);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700240 if ((ret == 0) && (mountflags & MS_RDONLY) != 0) {
Sami Tolvanen214f33b2014-12-18 16:15:30 +0000241 fs_mgr_set_blk_ro(source);
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700242 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700243 errno = save_errno;
Nick Kraleviche18c0d52013-04-16 16:41:32 -0700244 return ret;
245}
246
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800247static int fs_match(char *in1, char *in2)
248{
249 char *n1;
250 char *n2;
251 int ret;
252
253 n1 = strdup(in1);
254 n2 = strdup(in2);
255
256 remove_trailing_slashes(n1);
257 remove_trailing_slashes(n2);
258
259 ret = !strcmp(n1, n2);
260
261 free(n1);
262 free(n2);
263
264 return ret;
265}
266
Geremy Condracd642fc2014-04-02 13:42:06 -0700267static int device_is_debuggable() {
268 int ret = -1;
269 char value[PROP_VALUE_MAX];
270 ret = __system_property_get("ro.debuggable", value);
271 if (ret < 0)
272 return ret;
273 return strcmp(value, "1") ? 0 : 1;
274}
275
Paul Lawrencebbb36312014-10-09 14:22:49 +0000276static int device_is_secure() {
277 int ret = -1;
278 char value[PROP_VALUE_MAX];
279 ret = __system_property_get("ro.secure", value);
280 /* If error, we want to fail secure */
281 if (ret < 0)
282 return 1;
283 return strcmp(value, "0") ? 1 : 0;
284}
285
Paul Lawrence703b87d2015-01-07 11:44:51 -0800286static int device_is_force_encrypted() {
287 int ret = -1;
288 char value[PROP_VALUE_MAX];
289 ret = __system_property_get("ro.vold.forceencryption", value);
290 if (ret < 0)
291 return 0;
292 return strcmp(value, "1") ? 0 : 1;
293}
294
JP Abgrallf22b7452014-07-02 13:16:04 -0700295/*
296 * Tries to mount any of the consecutive fstab entries that match
297 * the mountpoint of the one given by fstab->recs[start_idx].
298 *
299 * end_idx: On return, will be the last rec that was looked at.
300 * attempted_idx: On return, will indicate which fstab rec
301 * succeeded. In case of failure, it will be the start_idx.
302 * Returns
303 * -1 on failure with errno set to match the 1st mount failure.
304 * 0 on success.
305 */
306static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx)
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700307{
JP Abgrallf22b7452014-07-02 13:16:04 -0700308 int i;
309 int mount_errno = 0;
310 int mounted = 0;
311
312 if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) {
313 errno = EINVAL;
314 if (end_idx) *end_idx = start_idx;
315 if (attempted_idx) *end_idx = start_idx;
316 return -1;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700317 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700318
319 /* Hunt down an fstab entry for the same mount point that might succeed */
320 for (i = start_idx;
321 /* We required that fstab entries for the same mountpoint be consecutive */
322 i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point);
323 i++) {
324 /*
325 * Don't try to mount/encrypt the same mount point again.
326 * Deal with alternate entries for the same point which are required to be all following
327 * each other.
328 */
329 if (mounted) {
330 ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__,
331 fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
332 continue;
333 }
334
335 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
336 check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
337 fstab->recs[i].mount_point);
338 }
339 if (!__mount(fstab->recs[i].blk_device, fstab->recs[i].mount_point, &fstab->recs[i])) {
340 *attempted_idx = i;
341 mounted = 1;
342 if (i != start_idx) {
343 ERROR("%s(): Mounted %s on %s with fs_type=%s instead of %s\n", __func__,
344 fstab->recs[i].blk_device, fstab->recs[i].mount_point, fstab->recs[i].fs_type,
345 fstab->recs[start_idx].fs_type);
346 }
347 } else {
348 /* back up errno for crypto decisions */
349 mount_errno = errno;
350 }
351 }
352
353 /* Adjust i for the case where it was still withing the recs[] */
354 if (i < fstab->num_entries) --i;
355
356 *end_idx = i;
357 if (!mounted) {
358 *attempted_idx = start_idx;
359 errno = mount_errno;
360 return -1;
361 }
362 return 0;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700363}
364
Christoffer Dall82982342014-12-17 21:26:54 +0100365static int translate_ext_labels(struct fstab_rec *rec)
366{
367 DIR *blockdir = NULL;
368 struct dirent *ent;
369 char *label;
370 size_t label_len;
371 int ret = -1;
372
373 if (strncmp(rec->blk_device, "LABEL=", 6))
374 return 0;
375
376 label = rec->blk_device + 6;
377 label_len = strlen(label);
378
379 if (label_len > 16) {
380 ERROR("FS label is longer than allowed by filesystem\n");
381 goto out;
382 }
383
384
385 blockdir = opendir("/dev/block");
386 if (!blockdir) {
387 ERROR("couldn't open /dev/block\n");
388 goto out;
389 }
390
391 while ((ent = readdir(blockdir))) {
392 int fd;
393 char super_buf[1024];
394 struct ext4_super_block *sb;
395
396 if (ent->d_type != DT_BLK)
397 continue;
398
399 fd = openat(dirfd(blockdir), ent->d_name, O_RDONLY);
400 if (fd < 0) {
401 ERROR("Cannot open block device /dev/block/%s\n", ent->d_name);
402 goto out;
403 }
404
405 if (TEMP_FAILURE_RETRY(lseek(fd, 1024, SEEK_SET)) < 0 ||
406 TEMP_FAILURE_RETRY(read(fd, super_buf, 1024)) != 1024) {
407 /* Probably a loopback device or something else without a readable
408 * superblock.
409 */
410 close(fd);
411 continue;
412 }
413
414 sb = (struct ext4_super_block *)super_buf;
415 if (sb->s_magic != EXT4_SUPER_MAGIC) {
416 INFO("/dev/block/%s not ext{234}\n", ent->d_name);
417 continue;
418 }
419
420 if (!strncmp(label, sb->s_volume_name, label_len)) {
421 char *new_blk_device;
422
423 if (asprintf(&new_blk_device, "/dev/block/%s", ent->d_name) < 0) {
424 ERROR("Could not allocate block device string\n");
425 goto out;
426 }
427
428 INFO("resolved label %s to %s\n", rec->blk_device, new_blk_device);
429
430 free(rec->blk_device);
431 rec->blk_device = new_blk_device;
432 ret = 0;
433 break;
434 }
435 }
436
437out:
438 closedir(blockdir);
439 return ret;
440}
441
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000442// Check to see if a mountable volume has encryption requirements
443static int handle_encryptable(struct fstab *fstab, const struct fstab_rec* rec)
444{
Paul Lawrenceaecb1e22015-11-06 11:18:48 -0800445 /* Check for existence of convert_fbe breadcrumb file */
446 char convert_fbe_name[PATH_MAX];
447 snprintf(convert_fbe_name, sizeof(convert_fbe_name),
448 "%s/convert_fbe", rec->mount_point);
449 bool convert_fbe = (access(convert_fbe_name, F_OK) == 0);
450
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000451 /* If this is block encryptable, need to trigger encryption */
452 if ( (rec->fs_mgr_flags & MF_FORCECRYPT)
Paul Lawrenceaecb1e22015-11-06 11:18:48 -0800453 || ((rec->fs_mgr_flags & MF_FORCEFDEORFBE) && !convert_fbe)
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000454 || (device_is_force_encrypted() && fs_mgr_is_encryptable(rec))) {
455 if (umount(rec->mount_point) == 0) {
456 return FS_MGR_MNTALL_DEV_NEEDS_ENCRYPTION;
457 } else {
458 WARNING("Could not umount %s (%s) - allow continue unencrypted\n",
459 rec->mount_point, strerror(errno));
460 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
461 }
462 }
463
464 // Deal with file level encryption
Paul Lawrenceaecb1e22015-11-06 11:18:48 -0800465 if ( (rec->fs_mgr_flags & MF_FILEENCRYPTION)
466 || ((rec->fs_mgr_flags & MF_FORCEFDEORFBE) && convert_fbe)) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000467 // Default or not yet initialized encryption requires no more work here
468 if (!e4crypt_non_default_key(rec->mount_point)) {
469 INFO("%s is default file encrypted\n", rec->mount_point);
470 return FS_MGR_MNTALL_DEV_DEFAULT_FILE_ENCRYPTED;
471 }
472
473 INFO("%s is non-default file encrypted\n", rec->mount_point);
474
475 // Uses non-default key, so must unmount and set up temp file system
476 if (umount(rec->mount_point)) {
477 ERROR("Failed to umount %s - rebooting\n", rec->mount_point);
478 return FS_MGR_MNTALL_FAIL;
479 }
480
481 if (fs_mgr_do_tmpfs_mount(rec->mount_point) != 0) {
482 ERROR("Failed to mount a tmpfs at %s\n", rec->mount_point);
483 return FS_MGR_MNTALL_FAIL;
484 }
485
486 // Mount data temporarily so we can access unencrypted dir
487 char tmp_mnt[PATH_MAX];
488 strlcpy(tmp_mnt, rec->mount_point, sizeof(tmp_mnt));
489 strlcat(tmp_mnt, "/tmp_mnt", sizeof(tmp_mnt));
490 if (mkdir(tmp_mnt, 0700)) {
491 ERROR("Failed to create temp mount point\n");
492 return FS_MGR_MNTALL_FAIL;
493 }
494
495 if (fs_mgr_do_mount(fstab, rec->mount_point,
496 rec->blk_device, tmp_mnt)) {
497 ERROR("Error temp mounting encrypted file system\n");
498 return FS_MGR_MNTALL_FAIL;
499 }
500
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000501 return FS_MGR_MNTALL_DEV_NON_DEFAULT_FILE_ENCRYPTED;
502 }
503
504 return FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
505}
506
JP Abgrall5c01dac2014-06-18 14:54:37 -0700507/* When multiple fstab records share the same mount_point, it will
508 * try to mount each one in turn, and ignore any duplicates after a
509 * first successful mount.
JP Abgrallf22b7452014-07-02 13:16:04 -0700510 * Returns -1 on error, and FS_MGR_MNTALL_* otherwise.
JP Abgrall5c01dac2014-06-18 14:54:37 -0700511 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800512int fs_mgr_mount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800513{
JP Abgrallf22b7452014-07-02 13:16:04 -0700514 int i = 0;
515 int encryptable = FS_MGR_MNTALL_DEV_NOT_ENCRYPTED;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700516 int error_count = 0;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700517 int mret = -1;
518 int mount_errno = 0;
JP Abgrallf22b7452014-07-02 13:16:04 -0700519 int attempted_idx = -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800520
Ken Sumrallab6b8522013-02-13 12:58:40 -0800521 if (!fstab) {
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700522 return -1;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800523 }
524
Ken Sumrallab6b8522013-02-13 12:58:40 -0800525 for (i = 0; i < fstab->num_entries; i++) {
526 /* Don't mount entries that are managed by vold */
Ken Sumrall6c2c1212013-02-22 17:36:21 -0800527 if (fstab->recs[i].fs_mgr_flags & (MF_VOLDMANAGED | MF_RECOVERYONLY)) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800528 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800529 }
530
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700531 /* Skip swap and raw partition entries such as boot, recovery, etc */
532 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
533 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800534 !strcmp(fstab->recs[i].fs_type, "mtd")) {
535 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800536 }
537
Daniel Rosenberg31a4faf2015-06-29 17:33:05 -0700538 /* Skip mounting the root partition, as it will already have been mounted */
539 if (!strcmp(fstab->recs[i].mount_point, "/")) {
540 if ((fstab->recs[i].fs_mgr_flags & MS_RDONLY) != 0) {
541 fs_mgr_set_blk_ro(fstab->recs[i].blk_device);
542 }
543 continue;
544 }
545
Christoffer Dall82982342014-12-17 21:26:54 +0100546 /* Translate LABEL= file system labels into block devices */
547 if (!strcmp(fstab->recs[i].fs_type, "ext2") ||
548 !strcmp(fstab->recs[i].fs_type, "ext3") ||
549 !strcmp(fstab->recs[i].fs_type, "ext4")) {
550 int tret = translate_ext_labels(&fstab->recs[i]);
551 if (tret < 0) {
552 ERROR("Could not translate label to block device\n");
553 continue;
554 }
555 }
556
Ken Sumrallab6b8522013-02-13 12:58:40 -0800557 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
558 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
559 }
560
Paul Lawrencebbb36312014-10-09 14:22:49 +0000561 if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
562 int rc = fs_mgr_setup_verity(&fstab->recs[i]);
563 if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
564 INFO("Verity disabled");
565 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700566 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800567 continue;
568 }
569 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700570 int last_idx_inspected;
Chris Fries79f33842013-09-05 13:19:21 -0500571 int top_idx = i;
572
JP Abgrallf22b7452014-07-02 13:16:04 -0700573 mret = mount_with_alternatives(fstab, i, &last_idx_inspected, &attempted_idx);
574 i = last_idx_inspected;
575 mount_errno = errno;
JP Abgrallf786fe52014-06-18 07:28:14 +0000576
JP Abgrall5c01dac2014-06-18 14:54:37 -0700577 /* Deal with encryptability. */
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800578 if (!mret) {
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000579 int status = handle_encryptable(fstab, &fstab->recs[attempted_idx]);
580
581 if (status == FS_MGR_MNTALL_FAIL) {
582 /* Fatal error - no point continuing */
583 return status;
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800584 }
Paul Lawrenceb8c9d272015-03-26 15:49:42 +0000585
586 if (status != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
587 if (encryptable != FS_MGR_MNTALL_DEV_NOT_ENCRYPTED) {
588 // Log and continue
589 ERROR("Only one encryptable/encrypted partition supported\n");
590 }
591 encryptable = status;
592 }
593
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800594 /* Success! Go get the next one */
595 continue;
596 }
597
Chris Fries79f33842013-09-05 13:19:21 -0500598 /* mount(2) returned an error, handle the encryptable/formattable case */
599 bool wiped = partition_wiped(fstab->recs[top_idx].blk_device);
600 if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
601 fs_mgr_is_formattable(&fstab->recs[top_idx]) && wiped) {
602 /* top_idx and attempted_idx point at the same partition, but sometimes
603 * at two different lines in the fstab. Use the top one for formatting
604 * as that is the preferred one.
605 */
606 ERROR("%s(): %s is wiped and %s %s is formattable. Format it.\n", __func__,
607 fstab->recs[top_idx].blk_device, fstab->recs[top_idx].mount_point,
608 fstab->recs[top_idx].fs_type);
609 if (fs_mgr_is_encryptable(&fstab->recs[top_idx]) &&
610 strcmp(fstab->recs[top_idx].key_loc, KEY_IN_FOOTER)) {
611 int fd = open(fstab->recs[top_idx].key_loc, O_WRONLY, 0644);
612 if (fd >= 0) {
613 INFO("%s(): also wipe %s\n", __func__, fstab->recs[top_idx].key_loc);
614 wipe_block_device(fd, get_file_size(fd));
615 close(fd);
616 } else {
617 ERROR("%s(): %s wouldn't open (%s)\n", __func__,
618 fstab->recs[top_idx].key_loc, strerror(errno));
619 }
620 }
621 if (fs_mgr_do_format(&fstab->recs[top_idx]) == 0) {
622 /* Let's replay the mount actions. */
623 i = top_idx - 1;
624 continue;
625 }
626 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700627 if (mret && mount_errno != EBUSY && mount_errno != EACCES &&
JP Abgrallcee20682014-07-02 14:26:54 -0700628 fs_mgr_is_encryptable(&fstab->recs[attempted_idx])) {
Chris Fries79f33842013-09-05 13:19:21 -0500629 if (wiped) {
JP Abgrallcee20682014-07-02 14:26:54 -0700630 ERROR("%s(): %s is wiped and %s %s is encryptable. Suggest recovery...\n", __func__,
631 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
632 fstab->recs[attempted_idx].fs_type);
633 encryptable = FS_MGR_MNTALL_DEV_NEEDS_RECOVERY;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700634 continue;
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700635 } else {
636 /* Need to mount a tmpfs at this mountpoint for now, and set
637 * properties that vold will query later for decrypting
638 */
JP Abgrallf22b7452014-07-02 13:16:04 -0700639 ERROR("%s(): possibly an encryptable blkdev %s for mount %s type %s )\n", __func__,
640 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
641 fstab->recs[attempted_idx].fs_type);
642 if (fs_mgr_do_tmpfs_mount(fstab->recs[attempted_idx].mount_point) < 0) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700643 ++error_count;
644 continue;
645 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800646 }
JP Abgrallf22b7452014-07-02 13:16:04 -0700647 encryptable = FS_MGR_MNTALL_DEV_MIGHT_BE_ENCRYPTED;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800648 } else {
William Roberts071f28a2014-01-14 14:33:44 -0500649 ERROR("Failed to mount an un-encryptable or wiped partition on"
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700650 "%s at %s options: %s error: %s\n",
JP Abgrallf22b7452014-07-02 13:16:04 -0700651 fstab->recs[attempted_idx].blk_device, fstab->recs[attempted_idx].mount_point,
652 fstab->recs[attempted_idx].fs_options, strerror(mount_errno));
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700653 ++error_count;
654 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800655 }
656 }
657
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700658 if (error_count) {
659 return -1;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700660 } else {
Paul Lawrence166fa3d2014-02-03 13:27:49 -0800661 return encryptable;
Mohamad Ayyash38afe5f2014-03-10 15:40:29 -0700662 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800663}
664
Ken Sumrallab6b8522013-02-13 12:58:40 -0800665/* If tmp_mount_point is non-null, mount the filesystem there. This is for the
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800666 * tmp mount we do to check the user password
JP Abgrall5c01dac2014-06-18 14:54:37 -0700667 * If multiple fstab entries are to be mounted on "n_name", it will try to mount each one
668 * in turn, and stop on 1st success, or no more match.
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800669 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800670int fs_mgr_do_mount(struct fstab *fstab, char *n_name, char *n_blk_device,
671 char *tmp_mount_point)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800672{
673 int i = 0;
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700674 int ret = FS_MGR_DOMNT_FAILED;
JP Abgrall5c01dac2014-06-18 14:54:37 -0700675 int mount_errors = 0;
676 int first_mount_errno = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800677 char *m;
678
Ken Sumrallab6b8522013-02-13 12:58:40 -0800679 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800680 return ret;
681 }
682
Ken Sumrallab6b8522013-02-13 12:58:40 -0800683 for (i = 0; i < fstab->num_entries; i++) {
684 if (!fs_match(fstab->recs[i].mount_point, n_name)) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800685 continue;
686 }
687
688 /* We found our match */
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700689 /* If this swap or a raw partition, report an error */
690 if (!strcmp(fstab->recs[i].fs_type, "swap") ||
691 !strcmp(fstab->recs[i].fs_type, "emmc") ||
Ken Sumrallab6b8522013-02-13 12:58:40 -0800692 !strcmp(fstab->recs[i].fs_type, "mtd")) {
693 ERROR("Cannot mount filesystem of type %s on %s\n",
694 fstab->recs[i].fs_type, n_blk_device);
695 goto out;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800696 }
697
Ken Sumrallab6b8522013-02-13 12:58:40 -0800698 /* First check the filesystem if requested */
699 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
700 wait_for_file(n_blk_device, WAIT_TIMEOUT);
701 }
702
703 if (fstab->recs[i].fs_mgr_flags & MF_CHECK) {
704 check_fs(n_blk_device, fstab->recs[i].fs_type,
705 fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800706 }
707
Paul Lawrencebbb36312014-10-09 14:22:49 +0000708 if ((fstab->recs[i].fs_mgr_flags & MF_VERIFY) && device_is_secure()) {
709 int rc = fs_mgr_setup_verity(&fstab->recs[i]);
710 if (device_is_debuggable() && rc == FS_MGR_SETUP_VERITY_DISABLED) {
711 INFO("Verity disabled");
712 } else if (rc != FS_MGR_SETUP_VERITY_SUCCESS) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700713 ERROR("Could not set up verified partition, skipping!\n");
Geremy Condra3ad3d1c2013-02-22 18:11:41 -0800714 continue;
715 }
716 }
717
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800718 /* Now mount it where requested */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800719 if (tmp_mount_point) {
720 m = tmp_mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800721 } else {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800722 m = fstab->recs[i].mount_point;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800723 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700724 if (__mount(n_blk_device, m, &fstab->recs[i])) {
725 if (!first_mount_errno) first_mount_errno = errno;
726 mount_errors++;
727 continue;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800728 } else {
729 ret = 0;
730 goto out;
731 }
732 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700733 if (mount_errors) {
734 ERROR("Cannot mount filesystem on %s at %s. error: %s\n",
735 n_blk_device, m, strerror(first_mount_errno));
Paul Lawrencecf234dc2014-09-09 10:44:51 -0700736 if (first_mount_errno == EBUSY) {
737 ret = FS_MGR_DOMNT_BUSY;
738 } else {
739 ret = FS_MGR_DOMNT_FAILED;
740 }
JP Abgrall5c01dac2014-06-18 14:54:37 -0700741 } else {
742 /* We didn't find a match, say so and return an error */
743 ERROR("Cannot find mount point %s in fstab\n", fstab->recs[i].mount_point);
744 }
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800745
746out:
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800747 return ret;
748}
749
750/*
751 * mount a tmpfs filesystem at the given point.
752 * return 0 on success, non-zero on failure.
753 */
754int fs_mgr_do_tmpfs_mount(char *n_name)
755{
756 int ret;
757
758 ret = mount("tmpfs", n_name, "tmpfs",
759 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
760 if (ret < 0) {
761 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
762 return -1;
763 }
764
765 /* Success */
766 return 0;
767}
768
Ken Sumrallab6b8522013-02-13 12:58:40 -0800769int fs_mgr_unmount_all(struct fstab *fstab)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800770{
771 int i = 0;
772 int ret = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800773
Ken Sumrallab6b8522013-02-13 12:58:40 -0800774 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800775 return -1;
776 }
777
Ken Sumrallab6b8522013-02-13 12:58:40 -0800778 while (fstab->recs[i].blk_device) {
779 if (umount(fstab->recs[i].mount_point)) {
780 ERROR("Cannot unmount filesystem at %s\n", fstab->recs[i].mount_point);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800781 ret = -1;
782 }
783 i++;
784 }
785
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800786 return ret;
787}
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700788
789/* This must be called after mount_all, because the mkswap command needs to be
790 * available.
791 */
792int fs_mgr_swapon_all(struct fstab *fstab)
793{
794 int i = 0;
795 int flags = 0;
796 int err = 0;
797 int ret = 0;
798 int status;
799 char *mkswap_argv[2] = {
800 MKSWAP_BIN,
801 NULL
802 };
803
804 if (!fstab) {
805 return -1;
806 }
807
808 for (i = 0; i < fstab->num_entries; i++) {
809 /* Skip non-swap entries */
810 if (strcmp(fstab->recs[i].fs_type, "swap")) {
811 continue;
812 }
813
814 if (fstab->recs[i].zram_size > 0) {
815 /* A zram_size was specified, so we need to configure the
816 * device. There is no point in having multiple zram devices
817 * on a system (all the memory comes from the same pool) so
818 * we can assume the device number is 0.
819 */
820 FILE *zram_fp;
821
822 zram_fp = fopen(ZRAM_CONF_DEV, "r+");
823 if (zram_fp == NULL) {
JP Abgrall4bb7bba2014-06-19 22:12:20 -0700824 ERROR("Unable to open zram conf device %s\n", ZRAM_CONF_DEV);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700825 ret = -1;
826 continue;
827 }
828 fprintf(zram_fp, "%d\n", fstab->recs[i].zram_size);
829 fclose(zram_fp);
830 }
831
832 if (fstab->recs[i].fs_mgr_flags & MF_WAIT) {
833 wait_for_file(fstab->recs[i].blk_device, WAIT_TIMEOUT);
834 }
835
836 /* Initialize the swap area */
837 mkswap_argv[1] = fstab->recs[i].blk_device;
838 err = android_fork_execvp_ext(ARRAY_SIZE(mkswap_argv), mkswap_argv,
Yusuke Satod81c3c62015-08-14 01:22:53 -0700839 &status, true, LOG_KLOG, false, NULL,
840 NULL, 0);
Ken Sumrall5bc31a22013-07-08 19:11:55 -0700841 if (err) {
842 ERROR("mkswap failed for %s\n", fstab->recs[i].blk_device);
843 ret = -1;
844 continue;
845 }
846
847 /* If -1, then no priority was specified in fstab, so don't set
848 * SWAP_FLAG_PREFER or encode the priority */
849 if (fstab->recs[i].swap_prio >= 0) {
850 flags = (fstab->recs[i].swap_prio << SWAP_FLAG_PRIO_SHIFT) &
851 SWAP_FLAG_PRIO_MASK;
852 flags |= SWAP_FLAG_PREFER;
853 } else {
854 flags = 0;
855 }
856 err = swapon(fstab->recs[i].blk_device, flags);
857 if (err) {
858 ERROR("swapon failed for %s\n", fstab->recs[i].blk_device);
859 ret = -1;
860 }
861 }
862
863 return ret;
864}
865
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800866/*
867 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
868 *
Ken Sumrallab6b8522013-02-13 12:58:40 -0800869 * real_blk_device must be at least PROPERTY_VALUE_MAX bytes long
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800870 */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800871int fs_mgr_get_crypt_info(struct fstab *fstab, char *key_loc, char *real_blk_device, int size)
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800872{
873 int i = 0;
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800874
Ken Sumrallab6b8522013-02-13 12:58:40 -0800875 if (!fstab) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800876 return -1;
877 }
878 /* Initialize return values to null strings */
879 if (key_loc) {
880 *key_loc = '\0';
881 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800882 if (real_blk_device) {
883 *real_blk_device = '\0';
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800884 }
885
886 /* Look for the encryptable partition to find the data */
Ken Sumrallab6b8522013-02-13 12:58:40 -0800887 for (i = 0; i < fstab->num_entries; i++) {
888 /* Don't deal with vold managed enryptable partitions here */
889 if (fstab->recs[i].fs_mgr_flags & MF_VOLDMANAGED) {
890 continue;
891 }
Paul Lawrenceb262d682015-10-29 10:31:02 -0700892 if (!(fstab->recs[i].fs_mgr_flags
893 & (MF_CRYPT | MF_FORCECRYPT | MF_FORCEFDEORFBE))) {
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800894 continue;
895 }
896
897 /* We found a match */
898 if (key_loc) {
Ken Sumrallab6b8522013-02-13 12:58:40 -0800899 strlcpy(key_loc, fstab->recs[i].key_loc, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800900 }
Ken Sumrallab6b8522013-02-13 12:58:40 -0800901 if (real_blk_device) {
902 strlcpy(real_blk_device, fstab->recs[i].blk_device, size);
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800903 }
904 break;
905 }
906
Ken Sumrallc1bf8962012-01-06 19:09:42 -0800907 return 0;
908}