blob: bebb252613bfc99520c091d58cd95cb987876e18 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/*
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
17/* TO DO:
18 * 1. Re-direct fsck output to the kernel log?
19 *
20 */
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <ctype.h>
28#include <sys/mount.h>
29#include <sys/stat.h>
30#include <errno.h>
31#include <sys/types.h>
32#include <sys/wait.h>
33#include <libgen.h>
34#include <time.h>
35
36#include <private/android_filesystem_config.h>
37#include <cutils/partition_utils.h>
38#include <cutils/properties.h>
39
40#include "fs_mgr_priv.h"
41
42#define KEY_LOC_PROP "ro.crypto.keyfile.userdata"
43#define KEY_IN_FOOTER "footer"
44
45#define E2FSCK_BIN "/system/bin/e2fsck"
46
47struct flag_list {
48 const char *name;
49 unsigned flag;
50};
51
52static struct flag_list mount_flags[] = {
53 { "noatime", MS_NOATIME },
54 { "noexec", MS_NOEXEC },
55 { "nosuid", MS_NOSUID },
56 { "nodev", MS_NODEV },
57 { "nodiratime", MS_NODIRATIME },
58 { "ro", MS_RDONLY },
59 { "rw", 0 },
60 { "remount", MS_REMOUNT },
61 { "defaults", 0 },
62 { 0, 0 },
63};
64
65static struct flag_list fs_mgr_flags[] = {
66 { "wait", MF_WAIT },
67 { "check", MF_CHECK },
68 { "encryptable=",MF_CRYPT },
69 { "defaults", 0 },
70 { 0, 0 },
71};
72
73/*
74 * gettime() - returns the time in seconds of the system's monotonic clock or
75 * zero on error.
76 */
77static time_t gettime(void)
78{
79 struct timespec ts;
80 int ret;
81
82 ret = clock_gettime(CLOCK_MONOTONIC, &ts);
83 if (ret < 0) {
84 ERROR("clock_gettime(CLOCK_MONOTONIC) failed: %s\n", strerror(errno));
85 return 0;
86 }
87
88 return ts.tv_sec;
89}
90
91static int wait_for_file(const char *filename, int timeout)
92{
93 struct stat info;
94 time_t timeout_time = gettime() + timeout;
95 int ret = -1;
96
97 while (gettime() < timeout_time && ((ret = stat(filename, &info)) < 0))
98 usleep(10000);
99
100 return ret;
101}
102
103static int parse_flags(char *flags, struct flag_list *fl, char **key_loc,
104 char *fs_options, int fs_options_len)
105{
106 int f = 0;
107 int i;
108 char *p;
109 char *savep;
110
111 /* initialize key_loc to null, if we find an MF_CRYPT flag,
112 * then we'll set key_loc to the proper value */
113 if (key_loc) {
114 *key_loc = NULL;
115 }
116 /* initialize fs_options to the null string */
117 if (fs_options && (fs_options_len > 0)) {
118 fs_options[0] = '\0';
119 }
120
121 p = strtok_r(flags, ",", &savep);
122 while (p) {
123 /* Look for the flag "p" in the flag list "fl"
124 * If not found, the loop exits with fl[i].name being null.
125 */
126 for (i = 0; fl[i].name; i++) {
127 if (!strncmp(p, fl[i].name, strlen(fl[i].name))) {
128 f |= fl[i].flag;
129 if ((fl[i].flag == MF_CRYPT) && key_loc) {
130 /* The encryptable flag is followed by an = and the
131 * location of the keys. Get it and return it.
132 */
133 *key_loc = strdup(strchr(p, '=') + 1);
134 }
135 break;
136 }
137 }
138
139 if (!fl[i].name) {
140 if (fs_options) {
141 /* It's not a known flag, so it must be a filesystem specific
142 * option. Add it to fs_options if it was passed in.
143 */
144 strlcat(fs_options, p, fs_options_len);
145 strlcat(fs_options, ",", fs_options_len);
146 } else {
147 /* fs_options was not passed in, so if the flag is unknown
148 * it's an error.
149 */
150 ERROR("Warning: unknown flag %s\n", p);
151 }
152 }
153 p = strtok_r(NULL, ",", &savep);
154 }
155
156out:
157 if (fs_options && fs_options[0]) {
158 /* remove the last trailing comma from the list of options */
159 fs_options[strlen(fs_options) - 1] = '\0';
160 }
161
162 return f;
163}
164
165/* Read a line of text till the next newline character.
166 * If no newline is found before the buffer is full, continue reading till a new line is seen,
167 * then return an empty buffer. This effectively ignores lines that are too long.
168 * On EOF, return null.
169 */
170static char *getline(char *buf, int size, FILE *file)
171{
172 int cnt = 0;
173 int eof = 0;
174 int eol = 0;
175 int c;
176
177 if (size < 1) {
178 return NULL;
179 }
180
181 while (cnt < (size - 1)) {
182 c = getc(file);
183 if (c == EOF) {
184 eof = 1;
185 break;
186 }
187
188 *(buf + cnt) = c;
189 cnt++;
190
191 if (c == '\n') {
192 eol = 1;
193 break;
194 }
195 }
196
197 /* Null terminate what we've read */
198 *(buf + cnt) = '\0';
199
200 if (eof) {
201 if (cnt) {
202 return buf;
203 } else {
204 return NULL;
205 }
206 } else if (eol) {
207 return buf;
208 } else {
209 /* The line is too long. Read till a newline or EOF.
210 * If EOF, return null, if newline, return an empty buffer.
211 */
212 while(1) {
213 c = getc(file);
214 if (c == EOF) {
215 return NULL;
216 } else if (c == '\n') {
217 *buf = '\0';
218 return buf;
219 }
220 }
221 }
222}
223
224static struct fstab_rec *read_fstab(char *fstab_path)
225{
226 FILE *fstab_file;
227 int cnt, entries;
228 int len;
229 char line[256];
230 const char *delim = " \t";
231 char *save_ptr, *p;
232 struct fstab_rec *fstab;
233 char *key_loc;
234#define FS_OPTIONS_LEN 1024
235 char tmp_fs_options[FS_OPTIONS_LEN];
236
237 fstab_file = fopen(fstab_path, "r");
238 if (!fstab_file) {
239 ERROR("Cannot open file %s\n", fstab_path);
240 return 0;
241 }
242
243 entries = 0;
244 while (getline(line, sizeof(line), fstab_file)) {
245 /* if the last character is a newline, shorten the string by 1 byte */
246 len = strlen(line);
247 if (line[len - 1] == '\n') {
248 line[len - 1] = '\0';
249 }
250 /* Skip any leading whitespace */
251 p = line;
252 while (isspace(*p)) {
253 p++;
254 }
255 /* ignore comments or empty lines */
256 if (*p == '#' || *p == '\0')
257 continue;
258 entries++;
259 }
260
261 if (!entries) {
262 ERROR("No entries found in fstab\n");
263 return 0;
264 }
265
266 fstab = calloc(entries + 1, sizeof(struct fstab_rec));
267
268 fseek(fstab_file, 0, SEEK_SET);
269
270 cnt = 0;
271 while (getline(line, sizeof(line), fstab_file)) {
272 /* if the last character is a newline, shorten the string by 1 byte */
273 len = strlen(line);
274 if (line[len - 1] == '\n') {
275 line[len - 1] = '\0';
276 }
277
278 /* Skip any leading whitespace */
279 p = line;
280 while (isspace(*p)) {
281 p++;
282 }
283 /* ignore comments or empty lines */
284 if (*p == '#' || *p == '\0')
285 continue;
286
287 /* If a non-comment entry is greater than the size we allocated, give an
288 * error and quit. This can happen in the unlikely case the file changes
289 * between the two reads.
290 */
291 if (cnt >= entries) {
292 ERROR("Tried to process more entries than counted\n");
293 break;
294 }
295
296 if (!(p = strtok_r(line, delim, &save_ptr))) {
297 ERROR("Error parsing mount source\n");
298 return 0;
299 }
300 fstab[cnt].blk_dev = strdup(p);
301
302 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
303 ERROR("Error parsing mnt_point\n");
304 return 0;
305 }
306 fstab[cnt].mnt_point = strdup(p);
307
308 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
309 ERROR("Error parsing fs_type\n");
310 return 0;
311 }
312 fstab[cnt].type = strdup(p);
313
314 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
315 ERROR("Error parsing mount_flags\n");
316 return 0;
317 }
318 tmp_fs_options[0] = '\0';
319 fstab[cnt].flags = parse_flags(p, mount_flags, 0, tmp_fs_options, FS_OPTIONS_LEN);
320
321 /* fs_options are optional */
322 if (tmp_fs_options[0]) {
323 fstab[cnt].fs_options = strdup(tmp_fs_options);
324 } else {
325 fstab[cnt].fs_options = NULL;
326 }
327
328 if (!(p = strtok_r(NULL, delim, &save_ptr))) {
329 ERROR("Error parsing fs_mgr_options\n");
330 return 0;
331 }
332 fstab[cnt].fs_mgr_flags = parse_flags(p, fs_mgr_flags, &key_loc, 0, 0);
333 fstab[cnt].key_loc = key_loc;
334
335 cnt++;
336 }
337 fclose(fstab_file);
338
339 return fstab;
340}
341
342static void free_fstab(struct fstab_rec *fstab)
343{
344 int i = 0;
345
346 while (fstab[i].blk_dev) {
347 /* Free the pointers return by strdup(3) */
348 free(fstab[i].blk_dev);
349 free(fstab[i].mnt_point);
350 free(fstab[i].type);
351 free(fstab[i].fs_options);
352 free(fstab[i].key_loc);
353
354 i++;
355 }
356
357 /* Free the actual fstab array created by calloc(3) */
358 free(fstab);
359}
360
361static void check_fs(char *blk_dev, char *type)
362{
363 pid_t pid;
364 int status;
365
366 /* Check for the types of filesystems we know how to check */
367 if (!strcmp(type, "ext2") || !strcmp(type, "ext3") || !strcmp(type, "ext4")) {
368 INFO("Running %s on %s\n", E2FSCK_BIN, blk_dev);
369 pid = fork();
370 if (pid > 0) {
371 /* Parent, wait for the child to return */
372 waitpid(pid, &status, 0);
373 } else if (pid == 0) {
374 /* child, run checker */
375 execlp(E2FSCK_BIN, E2FSCK_BIN, "-y", blk_dev, (char *)NULL);
376
377 /* Only gets here on error */
378 ERROR("Cannot run fs_mgr binary %s\n", E2FSCK_BIN);
379 } else {
380 /* No need to check for error in fork, we can't really handle it now */
381 ERROR("Fork failed trying to run %s\n", E2FSCK_BIN);
382 }
383 }
384
385 return;
386}
387
388static void remove_trailing_slashes(char *n)
389{
390 int len;
391
392 len = strlen(n) - 1;
393 while ((*(n + len) == '/') && len) {
394 *(n + len) = '\0';
395 len--;
396 }
397}
398
399static int fs_match(char *in1, char *in2)
400{
401 char *n1;
402 char *n2;
403 int ret;
404
405 n1 = strdup(in1);
406 n2 = strdup(in2);
407
408 remove_trailing_slashes(n1);
409 remove_trailing_slashes(n2);
410
411 ret = !strcmp(n1, n2);
412
413 free(n1);
414 free(n2);
415
416 return ret;
417}
418
419int fs_mgr_mount_all(char *fstab_file)
420{
421 int i = 0;
422 int encrypted = 0;
423 int ret = -1;
424 int mret;
425 struct fstab_rec *fstab = 0;
426
427 if (!(fstab = read_fstab(fstab_file))) {
428 return ret;
429 }
430
431 for (i = 0; fstab[i].blk_dev; i++) {
432 if (fstab[i].fs_mgr_flags & MF_WAIT) {
433 wait_for_file(fstab[i].blk_dev, WAIT_TIMEOUT);
434 }
435
436 if (fstab[i].fs_mgr_flags & MF_CHECK) {
437 check_fs(fstab[i].blk_dev, fstab[i].type);
438 }
439
440 mret = mount(fstab[i].blk_dev, fstab[i].mnt_point, fstab[i].type,
441 fstab[i].flags, fstab[i].fs_options);
442 if (!mret) {
443 /* Success! Go get the next one */
444 continue;
445 }
446
447 /* mount(2) returned an error, check if it's encrypted and deal with it */
448 if ((fstab[i].fs_mgr_flags & MF_CRYPT) && !partition_wiped(fstab[i].blk_dev)) {
449 /* Need to mount a tmpfs at this mountpoint for now, and set
450 * properties that vold will query later for decrypting
451 */
452 if (mount("tmpfs", fstab[i].mnt_point, "tmpfs",
453 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS) < 0) {
454 ERROR("Cannot mount tmpfs filesystem for encrypted fs at %s\n",
455 fstab[i].mnt_point);
456 goto out;
457 }
458 encrypted = 1;
459 } else {
460 ERROR("Cannot mount filesystem on %s at %s\n",
461 fstab[i].blk_dev, fstab[i].mnt_point);
462 goto out;
463 }
464 }
465
466 if (encrypted) {
467 ret = 1;
468 } else {
469 ret = 0;
470 }
471
472out:
473 free_fstab(fstab);
474 return ret;
475}
476
477/* If tmp_mnt_point is non-null, mount the filesystem there. This is for the
478 * tmp mount we do to check the user password
479 */
480int fs_mgr_do_mount(char *fstab_file, char *n_name, char *n_blk_dev, char *tmp_mnt_point)
481{
482 int i = 0;
483 int ret = -1;
484 struct fstab_rec *fstab = 0;
485 char *m;
486
487 if (!(fstab = read_fstab(fstab_file))) {
488 return ret;
489 }
490
491 for (i = 0; fstab[i].blk_dev; i++) {
492 if (!fs_match(fstab[i].mnt_point, n_name)) {
493 continue;
494 }
495
496 /* We found our match */
497 /* First check the filesystem if requested */
498 if (fstab[i].fs_mgr_flags & MF_WAIT) {
499 wait_for_file(fstab[i].blk_dev, WAIT_TIMEOUT);
500 }
501
Dees_Troy014c8942012-11-19 17:10:46 +0000502 if ((fstab[i].fs_mgr_flags & MF_CHECK) && strcmp("ext4", fstab[i].type) != 0) {
Dees_Troy51a0e822012-09-05 15:24:24 -0400503 check_fs(fstab[i].blk_dev, fstab[i].type);
504 }
505
506 /* Now mount it where requested */
507 if (tmp_mnt_point) {
508 m = tmp_mnt_point;
509 } else {
510 m = fstab[i].mnt_point;
511 }
512 if (mount(n_blk_dev, m, fstab[i].type,
513 fstab[i].flags, fstab[i].fs_options)) {
514 ERROR("Cannot mount filesystem on %s at %s\n",
515 n_blk_dev, m);
516 goto out;
517 } else {
518 ret = 0;
519 goto out;
520 }
521 }
522
523 /* We didn't find a match, say so and return an error */
524 ERROR("Cannot find mount point %s in fstab\n", fstab[i].mnt_point);
525
526out:
527 free_fstab(fstab);
528 return ret;
529}
530
531/*
532 * mount a tmpfs filesystem at the given point.
533 * return 0 on success, non-zero on failure.
534 */
535int fs_mgr_do_tmpfs_mount(char *n_name)
536{
537 int ret;
538
539 ret = mount("tmpfs", n_name, "tmpfs",
540 MS_NOATIME | MS_NOSUID | MS_NODEV, CRYPTO_TMPFS_OPTIONS);
541 if (ret < 0) {
542 ERROR("Cannot mount tmpfs filesystem at %s\n", n_name);
543 return -1;
544 }
545
546 /* Success */
547 return 0;
548}
549
550int fs_mgr_unmount_all(char *fstab_file)
551{
552 int i = 0;
553 int ret = 0;
554 struct fstab_rec *fstab = 0;
555
556 if (!(fstab = read_fstab(fstab_file))) {
557 return -1;
558 }
559
560 while (fstab[i].blk_dev) {
561 if (umount(fstab[i].mnt_point)) {
562 ERROR("Cannot unmount filesystem at %s\n", fstab[i].mnt_point);
563 ret = -1;
564 }
565 i++;
566 }
567
568 free_fstab(fstab);
569 return ret;
570}
571/*
572 * key_loc must be at least PROPERTY_VALUE_MAX bytes long
573 *
574 * real_blk_dev must be at least PROPERTY_VALUE_MAX bytes long
575 */
576int fs_mgr_get_crypt_info(char *fstab_file, char *key_loc, char *real_blk_dev, int size)
577{
578 int i = 0;
579 struct fstab_rec *fstab = 0;
580
581 if (!(fstab = read_fstab(fstab_file))) {
582 return -1;
583 }
584 /* Initialize return values to null strings */
585 if (key_loc) {
586 *key_loc = '\0';
587 }
588 if (real_blk_dev) {
589 *real_blk_dev = '\0';
590 }
591
592 /* Look for the encryptable partition to find the data */
593 for (i = 0; fstab[i].blk_dev; i++) {
594 if (!(fstab[i].fs_mgr_flags & MF_CRYPT)) {
595 continue;
596 }
597
598 /* We found a match */
599 if (key_loc) {
600 strlcpy(key_loc, fstab[i].key_loc, size);
601 }
602 if (real_blk_dev) {
603 strlcpy(real_blk_dev, fstab[i].blk_dev, size);
604 }
605 break;
606 }
607
608 free_fstab(fstab);
609 return 0;
610}
611