1 /*
2  * Copyright (C) 2007 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 #include "roots.h"
18 
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <sys/mount.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 
29 #include <android-base/logging.h>
30 #include <ext4_utils/make_ext4fs.h>
31 #include <ext4_utils/wipe.h>
32 #include <fs_mgr.h>
33 
34 #include "common.h"
35 #include "mounts.h"
36 #include "cryptfs.h"
37 
38 static struct fstab *fstab = NULL;
39 
40 extern struct selabel_handle *sehandle;
41 
load_volume_table()42 void load_volume_table()
43 {
44     int i;
45     int ret;
46 
47     fstab = fs_mgr_read_fstab_default();
48     if (!fstab) {
49         LOG(ERROR) << "failed to read default fstab";
50         return;
51     }
52 
53     ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
54     if (ret < 0 ) {
55         LOG(ERROR) << "failed to add /tmp entry to fstab";
56         fs_mgr_free_fstab(fstab);
57         fstab = NULL;
58         return;
59     }
60 
61     printf("recovery filesystem table\n");
62     printf("=========================\n");
63     for (i = 0; i < fstab->num_entries; ++i) {
64         Volume* v = &fstab->recs[i];
65         printf("  %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
66                v->blk_device, v->length);
67     }
68     printf("\n");
69 }
70 
volume_for_path(const char * path)71 Volume* volume_for_path(const char* path) {
72     return fs_mgr_get_entry_for_mount_point(fstab, path);
73 }
74 
75 // Mount the volume specified by path at the given mount_point.
ensure_path_mounted_at(const char * path,const char * mount_point)76 int ensure_path_mounted_at(const char* path, const char* mount_point) {
77     Volume* v = volume_for_path(path);
78     if (v == NULL) {
79         LOG(ERROR) << "unknown volume for path [" << path << "]";
80         return -1;
81     }
82     if (strcmp(v->fs_type, "ramdisk") == 0) {
83         // the ramdisk is always mounted.
84         return 0;
85     }
86 
87     if (!scan_mounted_volumes()) {
88         LOG(ERROR) << "failed to scan mounted volumes";
89         return -1;
90     }
91 
92     if (!mount_point) {
93         mount_point = v->mount_point;
94     }
95 
96     MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
97     if (mv) {
98         // volume is already mounted
99         return 0;
100     }
101 
102     mkdir(mount_point, 0755);  // in case it doesn't already exist
103 
104     if (strcmp(v->fs_type, "ext4") == 0 ||
105                strcmp(v->fs_type, "squashfs") == 0 ||
106                strcmp(v->fs_type, "vfat") == 0) {
107         int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
108         if (result == -1 && fs_mgr_is_formattable(v)) {
109             LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno)
110                        << ") , formatting.....";
111             bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
112             if (fs_mgr_do_format(v, crypt_footer) == 0) {
113                 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
114             } else {
115                 PLOG(ERROR) << "failed to format " << mount_point;
116                 return -1;
117             }
118         }
119 
120         if (result == -1) {
121             PLOG(ERROR) << "failed to mount " << mount_point;
122             return -1;
123         }
124         return 0;
125     }
126 
127     LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
128     return -1;
129 }
130 
ensure_path_mounted(const char * path)131 int ensure_path_mounted(const char* path) {
132     // Mount at the default mount point.
133     return ensure_path_mounted_at(path, nullptr);
134 }
135 
ensure_path_unmounted(const char * path)136 int ensure_path_unmounted(const char* path) {
137     Volume* v = volume_for_path(path);
138     if (v == NULL) {
139         LOG(ERROR) << "unknown volume for path [" << path << "]";
140         return -1;
141     }
142     if (strcmp(v->fs_type, "ramdisk") == 0) {
143         // the ramdisk is always mounted; you can't unmount it.
144         return -1;
145     }
146 
147     if (!scan_mounted_volumes()) {
148         LOG(ERROR) << "failed to scan mounted volumes";
149         return -1;
150     }
151 
152     MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
153     if (mv == NULL) {
154         // volume is already unmounted
155         return 0;
156     }
157 
158     return unmount_mounted_volume(mv);
159 }
160 
exec_cmd(const char * path,char * const argv[])161 static int exec_cmd(const char* path, char* const argv[]) {
162     int status;
163     pid_t child;
164     if ((child = vfork()) == 0) {
165         execv(path, argv);
166         _exit(EXIT_FAILURE);
167     }
168     waitpid(child, &status, 0);
169     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
170         LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
171     }
172     return WEXITSTATUS(status);
173 }
174 
format_volume(const char * volume,const char * directory)175 int format_volume(const char* volume, const char* directory) {
176     Volume* v = volume_for_path(volume);
177     if (v == NULL) {
178         LOG(ERROR) << "unknown volume \"" << volume << "\"";
179         return -1;
180     }
181     if (strcmp(v->fs_type, "ramdisk") == 0) {
182         // you can't format the ramdisk.
183         LOG(ERROR) << "can't format_volume \"" << volume << "\"";
184         return -1;
185     }
186     if (strcmp(v->mount_point, volume) != 0) {
187         LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
188         return -1;
189     }
190 
191     if (ensure_path_unmounted(volume) != 0) {
192         LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
193         return -1;
194     }
195 
196     if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
197         // if there's a key_loc that looks like a path, it should be a
198         // block device for storing encryption metadata.  wipe it too.
199         if (v->key_loc != NULL && v->key_loc[0] == '/') {
200             LOG(INFO) << "wiping " << v->key_loc;
201             int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
202             if (fd < 0) {
203                 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
204                 return -1;
205             }
206             wipe_block_device(fd, get_file_size(fd));
207             close(fd);
208         }
209 
210         ssize_t length = 0;
211         if (v->length != 0) {
212             length = v->length;
213         } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
214             length = -CRYPT_FOOTER_OFFSET;
215         }
216         int result;
217         if (strcmp(v->fs_type, "ext4") == 0) {
218             if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
219                 result = make_ext4fs_directory_align(v->blk_device, length, volume, sehandle,
220                         directory, v->erase_blk_size, v->logical_blk_size);
221             } else {
222                 result = make_ext4fs_directory(v->blk_device, length, volume, sehandle, directory);
223             }
224         } else {   /* Has to be f2fs because we checked earlier. */
225             if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0 && length < 0) {
226                 LOG(ERROR) << "format_volume: crypt footer + negative length (" << length
227                            << ") not supported on " << v->fs_type;
228                 return -1;
229             }
230             if (length < 0) {
231                 LOG(ERROR) << "format_volume: negative length (" << length
232                            << ") not supported on " << v->fs_type;
233                 return -1;
234             }
235             char *num_sectors;
236             if (asprintf(&num_sectors, "%zd", length / 512) <= 0) {
237                 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
238                            << " command for " << v->blk_device;
239                 return -1;
240             }
241             const char *f2fs_path = "/sbin/mkfs.f2fs";
242             const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, NULL};
243 
244             result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
245             free(num_sectors);
246         }
247         if (result != 0) {
248             PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
249             return -1;
250         }
251         return 0;
252     }
253 
254     LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
255     return -1;
256 }
257 
format_volume(const char * volume)258 int format_volume(const char* volume) {
259     return format_volume(volume, NULL);
260 }
261 
setup_install_mounts()262 int setup_install_mounts() {
263     if (fstab == NULL) {
264         LOG(ERROR) << "can't set up install mounts: no fstab loaded";
265         return -1;
266     }
267     for (int i = 0; i < fstab->num_entries; ++i) {
268         Volume* v = fstab->recs + i;
269 
270         if (strcmp(v->mount_point, "/tmp") == 0 ||
271             strcmp(v->mount_point, "/cache") == 0) {
272             if (ensure_path_mounted(v->mount_point) != 0) {
273                 LOG(ERROR) << "failed to mount " << v->mount_point;
274                 return -1;
275             }
276 
277         } else {
278             if (ensure_path_unmounted(v->mount_point) != 0) {
279                 LOG(ERROR) << "failed to unmount " << v->mount_point;
280                 return -1;
281             }
282         }
283     }
284     return 0;
285 }
286