• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android-base/properties.h>
31 #include <android-base/stringprintf.h>
32 #include <android-base/unique_fd.h>
33 #include <ext4_utils/wipe.h>
34 #include <fs_mgr.h>
35 
36 #include "common.h"
37 #include "mounts.h"
38 #include "cryptfs.h"
39 
40 static struct fstab *fstab = NULL;
41 
42 extern struct selabel_handle *sehandle;
43 
load_volume_table()44 void load_volume_table()
45 {
46     int i;
47     int ret;
48 
49     fstab = fs_mgr_read_fstab_default();
50     if (!fstab) {
51         LOG(ERROR) << "failed to read default fstab";
52         return;
53     }
54 
55     ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");
56     if (ret < 0 ) {
57         LOG(ERROR) << "failed to add /tmp entry to fstab";
58         fs_mgr_free_fstab(fstab);
59         fstab = NULL;
60         return;
61     }
62 
63     printf("recovery filesystem table\n");
64     printf("=========================\n");
65     for (i = 0; i < fstab->num_entries; ++i) {
66         Volume* v = &fstab->recs[i];
67         printf("  %d %s %s %s %lld\n", i, v->mount_point, v->fs_type,
68                v->blk_device, v->length);
69     }
70     printf("\n");
71 }
72 
volume_for_path(const char * path)73 Volume* volume_for_path(const char* path) {
74     return fs_mgr_get_entry_for_mount_point(fstab, path);
75 }
76 
77 // Mount the volume specified by path at the given mount_point.
ensure_path_mounted_at(const char * path,const char * mount_point)78 int ensure_path_mounted_at(const char* path, const char* mount_point) {
79     Volume* v = volume_for_path(path);
80     if (v == NULL) {
81         LOG(ERROR) << "unknown volume for path [" << path << "]";
82         return -1;
83     }
84     if (strcmp(v->fs_type, "ramdisk") == 0) {
85         // the ramdisk is always mounted.
86         return 0;
87     }
88 
89     if (!scan_mounted_volumes()) {
90         LOG(ERROR) << "failed to scan mounted volumes";
91         return -1;
92     }
93 
94     if (!mount_point) {
95         mount_point = v->mount_point;
96     }
97 
98     MountedVolume* mv = find_mounted_volume_by_mount_point(mount_point);
99     if (mv) {
100         // volume is already mounted
101         return 0;
102     }
103 
104     mkdir(mount_point, 0755);  // in case it doesn't already exist
105 
106     if (strcmp(v->fs_type, "ext4") == 0 ||
107                strcmp(v->fs_type, "squashfs") == 0 ||
108                strcmp(v->fs_type, "vfat") == 0) {
109         int result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
110         if (result == -1 && fs_mgr_is_formattable(v)) {
111             LOG(ERROR) << "failed to mount " << mount_point << " (" << strerror(errno)
112                        << ") , formatting.....";
113             bool crypt_footer = fs_mgr_is_encryptable(v) && !strcmp(v->key_loc, "footer");
114             if (fs_mgr_do_format(v, crypt_footer) == 0) {
115                 result = mount(v->blk_device, mount_point, v->fs_type, v->flags, v->fs_options);
116             } else {
117                 PLOG(ERROR) << "failed to format " << mount_point;
118                 return -1;
119             }
120         }
121 
122         if (result == -1) {
123             PLOG(ERROR) << "failed to mount " << mount_point;
124             return -1;
125         }
126         return 0;
127     }
128 
129     LOG(ERROR) << "unknown fs_type \"" << v->fs_type << "\" for " << mount_point;
130     return -1;
131 }
132 
ensure_path_mounted(const char * path)133 int ensure_path_mounted(const char* path) {
134     // Mount at the default mount point.
135     return ensure_path_mounted_at(path, nullptr);
136 }
137 
ensure_path_unmounted(const char * path)138 int ensure_path_unmounted(const char* path) {
139     Volume* v = volume_for_path(path);
140     if (v == NULL) {
141         LOG(ERROR) << "unknown volume for path [" << path << "]";
142         return -1;
143     }
144     if (strcmp(v->fs_type, "ramdisk") == 0) {
145         // the ramdisk is always mounted; you can't unmount it.
146         return -1;
147     }
148 
149     if (!scan_mounted_volumes()) {
150         LOG(ERROR) << "failed to scan mounted volumes";
151         return -1;
152     }
153 
154     MountedVolume* mv = find_mounted_volume_by_mount_point(v->mount_point);
155     if (mv == NULL) {
156         // volume is already unmounted
157         return 0;
158     }
159 
160     return unmount_mounted_volume(mv);
161 }
162 
exec_cmd(const char * path,char * const argv[])163 static int exec_cmd(const char* path, char* const argv[]) {
164     int status;
165     pid_t child;
166     if ((child = vfork()) == 0) {
167         execv(path, argv);
168         _exit(EXIT_FAILURE);
169     }
170     waitpid(child, &status, 0);
171     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
172         LOG(ERROR) << path << " failed with status " << WEXITSTATUS(status);
173     }
174     return WEXITSTATUS(status);
175 }
176 
get_file_size(int fd,uint64_t reserve_len)177 static ssize_t get_file_size(int fd, uint64_t reserve_len) {
178   struct stat buf;
179   int ret = fstat(fd, &buf);
180   if (ret) return 0;
181 
182   ssize_t computed_size;
183   if (S_ISREG(buf.st_mode)) {
184     computed_size = buf.st_size - reserve_len;
185   } else if (S_ISBLK(buf.st_mode)) {
186     computed_size = get_block_device_size(fd) - reserve_len;
187   } else {
188     computed_size = 0;
189   }
190 
191   return computed_size;
192 }
193 
format_volume(const char * volume,const char * directory)194 int format_volume(const char* volume, const char* directory) {
195     Volume* v = volume_for_path(volume);
196     if (v == NULL) {
197         LOG(ERROR) << "unknown volume \"" << volume << "\"";
198         return -1;
199     }
200     if (strcmp(v->fs_type, "ramdisk") == 0) {
201         // you can't format the ramdisk.
202         LOG(ERROR) << "can't format_volume \"" << volume << "\"";
203         return -1;
204     }
205     if (strcmp(v->mount_point, volume) != 0) {
206         LOG(ERROR) << "can't give path \"" << volume << "\" to format_volume";
207         return -1;
208     }
209 
210     if (ensure_path_unmounted(volume) != 0) {
211         LOG(ERROR) << "format_volume failed to unmount \"" << v->mount_point << "\"";
212         return -1;
213     }
214 
215     if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "f2fs") == 0) {
216         // if there's a key_loc that looks like a path, it should be a
217         // block device for storing encryption metadata.  wipe it too.
218         if (v->key_loc != NULL && v->key_loc[0] == '/') {
219             LOG(INFO) << "wiping " << v->key_loc;
220             int fd = open(v->key_loc, O_WRONLY | O_CREAT, 0644);
221             if (fd < 0) {
222                 LOG(ERROR) << "format_volume: failed to open " << v->key_loc;
223                 return -1;
224             }
225             wipe_block_device(fd, get_file_size(fd));
226             close(fd);
227         }
228 
229         ssize_t length = 0;
230         if (v->length != 0) {
231             length = v->length;
232         } else if (v->key_loc != NULL && strcmp(v->key_loc, "footer") == 0) {
233           android::base::unique_fd fd(open(v->blk_device, O_RDONLY));
234           if (fd < 0) {
235             PLOG(ERROR) << "get_file_size: failed to open " << v->blk_device;
236             return -1;
237           }
238           length = get_file_size(fd.get(), CRYPT_FOOTER_OFFSET);
239           if (length <= 0) {
240             LOG(ERROR) << "get_file_size: invalid size " << length << " for " << v->blk_device;
241             return -1;
242           }
243         }
244         int result;
245         if (strcmp(v->fs_type, "ext4") == 0) {
246           static constexpr int block_size = 4096;
247           int raid_stride = v->logical_blk_size / block_size;
248           int raid_stripe_width = v->erase_blk_size / block_size;
249 
250           // stride should be the max of 8kb and logical block size
251           if (v->logical_blk_size != 0 && v->logical_blk_size < 8192) {
252             raid_stride = 8192 / block_size;
253           }
254 
255           const char* mke2fs_argv[] = { "/sbin/mke2fs_static",
256                                         "-F",
257                                         "-t",
258                                         "ext4",
259                                         "-b",
260                                         nullptr,
261                                         nullptr,
262                                         nullptr,
263                                         nullptr,
264                                         nullptr,
265                                         nullptr };
266 
267           int i = 5;
268           std::string block_size_str = std::to_string(block_size);
269           mke2fs_argv[i++] = block_size_str.c_str();
270 
271           std::string ext_args;
272           if (v->erase_blk_size != 0 && v->logical_blk_size != 0) {
273             ext_args = android::base::StringPrintf("stride=%d,stripe-width=%d", raid_stride,
274                                                    raid_stripe_width);
275             mke2fs_argv[i++] = "-E";
276             mke2fs_argv[i++] = ext_args.c_str();
277           }
278 
279           mke2fs_argv[i++] = v->blk_device;
280 
281           std::string size_str = std::to_string(length / block_size);
282           if (length != 0) {
283             mke2fs_argv[i++] = size_str.c_str();
284           }
285 
286           result = exec_cmd(mke2fs_argv[0], const_cast<char**>(mke2fs_argv));
287           if (result == 0 && directory != nullptr) {
288             const char* e2fsdroid_argv[] = { "/sbin/e2fsdroid_static",
289                                              "-e",
290                                              "-f",
291                                              directory,
292                                              "-a",
293                                              volume,
294                                              v->blk_device,
295                                              nullptr };
296 
297             result = exec_cmd(e2fsdroid_argv[0], const_cast<char**>(e2fsdroid_argv));
298           }
299         } else {   /* Has to be f2fs because we checked earlier. */
300             char *num_sectors = nullptr;
301             if (length >= 512 && asprintf(&num_sectors, "%zd", length / 512) <= 0) {
302                 LOG(ERROR) << "format_volume: failed to create " << v->fs_type
303                            << " command for " << v->blk_device;
304                 return -1;
305             }
306             const char *f2fs_path = "/sbin/mkfs.f2fs";
307             const char* const f2fs_argv[] = {"mkfs.f2fs", "-t", "-d1", v->blk_device, num_sectors, nullptr};
308 
309             result = exec_cmd(f2fs_path, (char* const*)f2fs_argv);
310             free(num_sectors);
311         }
312         if (result != 0) {
313             PLOG(ERROR) << "format_volume: make " << v->fs_type << " failed on " << v->blk_device;
314             return -1;
315         }
316         return 0;
317     }
318 
319     LOG(ERROR) << "format_volume: fs_type \"" << v->fs_type << "\" unsupported";
320     return -1;
321 }
322 
format_volume(const char * volume)323 int format_volume(const char* volume) {
324     return format_volume(volume, NULL);
325 }
326 
setup_install_mounts()327 int setup_install_mounts() {
328   if (fstab == nullptr) {
329     LOG(ERROR) << "can't set up install mounts: no fstab loaded";
330     return -1;
331   }
332   for (int i = 0; i < fstab->num_entries; ++i) {
333     const Volume* v = fstab->recs + i;
334 
335     // We don't want to do anything with "/".
336     if (strcmp(v->mount_point, "/") == 0) {
337       continue;
338     }
339 
340     if (strcmp(v->mount_point, "/tmp") == 0 || strcmp(v->mount_point, "/cache") == 0) {
341       if (ensure_path_mounted(v->mount_point) != 0) {
342         LOG(ERROR) << "failed to mount " << v->mount_point;
343         return -1;
344       }
345     } else {
346       if (ensure_path_unmounted(v->mount_point) != 0) {
347         LOG(ERROR) << "failed to unmount " << v->mount_point;
348         return -1;
349       }
350     }
351   }
352   return 0;
353 }
354