• 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 <private/fs_config.h>
18 
19 // This file is used to define the properties of the filesystem
20 // images generated by build tools (mkbootfs and mkyaffs2image) and
21 // by the device side of adb.
22 
23 #define LOG_TAG "fs_config"
24 
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/stat.h>
33 #include <sys/types.h>
34 
35 #include <string>
36 
37 #include <android-base/strings.h>
38 #include <cutils/fs.h>
39 #include <log/log.h>
40 #include <private/android_filesystem_config.h>
41 
42 #include "fs_config.h"
43 
44 #ifndef O_BINARY
45 #define O_BINARY 0
46 #endif
47 
48 using android::base::EndsWith;
49 using android::base::StartsWith;
50 
51 #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
52 #define CAP_MASK_LONG(cap_name) (1ULL << (cap_name))
53 
54 // Rules for directories.
55 // These rules are applied based on "first match", so they
56 // should start with the most specific path and work their
57 // way up to the root.
58 
59 static const struct fs_path_config android_dirs[] = {
60         // clang-format off
61     { 00770, AID_SYSTEM,       AID_CACHE,        0, "cache" },
62     { 00555, AID_ROOT,         AID_ROOT,         0, "config" },
63     { 00771, AID_SYSTEM,       AID_SYSTEM,       0, "data/app" },
64     { 00771, AID_SYSTEM,       AID_SYSTEM,       0, "data/app-private" },
65     { 00771, AID_SYSTEM,       AID_SYSTEM,       0, "data/app-ephemeral" },
66     { 00771, AID_ROOT,         AID_ROOT,         0, "data/dalvik-cache" },
67     { 00771, AID_SYSTEM,       AID_SYSTEM,       0, "data/data" },
68     { 00771, AID_SHELL,        AID_SHELL,        0, "data/local/tmp" },
69     { 00771, AID_SHELL,        AID_SHELL,        0, "data/local" },
70     { 00770, AID_DHCP,         AID_DHCP,         0, "data/misc/dhcp" },
71     { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
72     { 01771, AID_SYSTEM,       AID_MISC,         0, "data/misc" },
73     { 00775, AID_MEDIA_RW,     AID_MEDIA_RW,     0, "data/media/Music" },
74     { 00775, AID_MEDIA_RW,     AID_MEDIA_RW,     0, "data/media" },
75     { 00750, AID_ROOT,         AID_SHELL,        0, "data/nativetest" },
76     { 00750, AID_ROOT,         AID_SHELL,        0, "data/nativetest64" },
77     { 00750, AID_ROOT,         AID_SHELL,        0, "data/benchmarktest" },
78     { 00750, AID_ROOT,         AID_SHELL,        0, "data/benchmarktest64" },
79     { 00775, AID_ROOT,         AID_ROOT,         0, "data/preloads" },
80     { 00771, AID_SYSTEM,       AID_SYSTEM,       0, "data" },
81     { 00755, AID_ROOT,         AID_SYSTEM,       0, "mnt" },
82     { 00751, AID_ROOT,         AID_SHELL,        0, "product/bin" },
83     { 00751, AID_ROOT,         AID_SHELL,        0, "product/apex/*/bin" },
84     { 00777, AID_ROOT,         AID_ROOT,         0, "sdcard" },
85     { 00751, AID_ROOT,         AID_SDCARD_R,     0, "storage" },
86     { 00751, AID_ROOT,         AID_SHELL,        0, "system/bin" },
87     { 00755, AID_ROOT,         AID_ROOT,         0, "system/etc/ppp" },
88     { 00755, AID_ROOT,         AID_SHELL,        0, "system/vendor" },
89     { 00750, AID_ROOT,         AID_SHELL,        0, "system/xbin" },
90     { 00751, AID_ROOT,         AID_SHELL,        0, "system/apex/*/bin" },
91     { 00751, AID_ROOT,         AID_SHELL,        0, "system_ext/bin" },
92     { 00751, AID_ROOT,         AID_SHELL,        0, "system_ext/apex/*/bin" },
93     { 00751, AID_ROOT,         AID_SHELL,        0, "vendor/bin" },
94     { 00751, AID_ROOT,         AID_SHELL,        0, "vendor/apex/*/bin" },
95     { 00755, AID_ROOT,         AID_SHELL,        0, "vendor" },
96     { 00755, AID_ROOT,         AID_ROOT,         0, 0 },
97         // clang-format on
98 };
99 #ifndef __ANDROID_VNDK__
100 auto __for_testing_only__android_dirs = android_dirs;
101 #endif
102 
103 // Rules for files.
104 // These rules are applied based on "first match", so they
105 // should start with the most specific path and work their
106 // way up to the root. Prefixes ending in * denotes wildcard
107 // and will allow partial matches.
108 static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
109 static const char sys_conf_file[] = "/system/etc/fs_config_files";
110 // No restrictions are placed on the vendor and oem file-system config files,
111 // although the developer is advised to restrict the scope to the /vendor or
112 // oem/ file-system since the intent is to provide support for customized
113 // portions of a separate vendor.img or oem.img.  Has to remain open so that
114 // customization can also land on /system/vendor, /system/oem, /system/odm,
115 // /system/product or /system/system_ext.
116 //
117 // We expect build-time checking or filtering when constructing the associated
118 // fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
119 static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
120 static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
121 static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
122 static const char oem_conf_file[] = "/oem/etc/fs_config_files";
123 static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
124 static const char odm_conf_file[] = "/odm/etc/fs_config_files";
125 static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
126 static const char product_conf_file[] = "/product/etc/fs_config_files";
127 static const char system_ext_conf_dir[] = "/system_ext/etc/fs_config_dirs";
128 static const char system_ext_conf_file[] = "/system_ext/etc/fs_config_files";
129 static const char* conf[][2] = {
130         {sys_conf_file, sys_conf_dir},         {ven_conf_file, ven_conf_dir},
131         {oem_conf_file, oem_conf_dir},         {odm_conf_file, odm_conf_dir},
132         {product_conf_file, product_conf_dir}, {system_ext_conf_file, system_ext_conf_dir},
133 };
134 
135 // Do not use android_files to grant Linux capabilities.  Use ambient capabilities in their
136 // associated init.rc file instead.  See https://source.android.com/devices/tech/config/ambient.
137 
138 // Do not place any new vendor/, data/vendor/, etc entries in android_files.
139 // Vendor entries should be done via a vendor or device specific config.fs.
140 // See https://source.android.com/devices/tech/config/filesystem#using-file-system-capabilities
141 static const struct fs_path_config android_files[] = {
142         // clang-format off
143     { 00644, AID_SYSTEM,    AID_SYSTEM,    0, "data/app/*" },
144     { 00644, AID_SYSTEM,    AID_SYSTEM,    0, "data/app-ephemeral/*" },
145     { 00644, AID_SYSTEM,    AID_SYSTEM,    0, "data/app-private/*" },
146     { 00644, AID_APP,       AID_APP,       0, "data/data/*" },
147     { 00644, AID_MEDIA_RW,  AID_MEDIA_RW,  0, "data/media/*" },
148     { 00640, AID_ROOT,      AID_SHELL,     0, "data/nativetest/tests.txt" },
149     { 00640, AID_ROOT,      AID_SHELL,     0, "data/nativetest64/tests.txt" },
150     { 00750, AID_ROOT,      AID_SHELL,     0, "data/nativetest/*" },
151     { 00750, AID_ROOT,      AID_SHELL,     0, "data/nativetest64/*" },
152     { 00750, AID_ROOT,      AID_SHELL,     0, "data/benchmarktest/*" },
153     { 00750, AID_ROOT,      AID_SHELL,     0, "data/benchmarktest64/*" },
154     { 00600, AID_ROOT,      AID_ROOT,      0, "default.prop" }, // legacy
155     { 00600, AID_ROOT,      AID_ROOT,      0, "system/etc/prop.default" },
156     { 00600, AID_ROOT,      AID_ROOT,      0, "odm/build.prop" }, // legacy; only for P release
157     { 00600, AID_ROOT,      AID_ROOT,      0, "odm/default.prop" }, // legacy; only for P release
158     { 00600, AID_ROOT,      AID_ROOT,      0, "odm/etc/build.prop" },
159     { 00444, AID_ROOT,      AID_ROOT,      0, odm_conf_dir + 1 },
160     { 00444, AID_ROOT,      AID_ROOT,      0, odm_conf_file + 1 },
161     { 00444, AID_ROOT,      AID_ROOT,      0, oem_conf_dir + 1 },
162     { 00444, AID_ROOT,      AID_ROOT,      0, oem_conf_file + 1 },
163     { 00600, AID_ROOT,      AID_ROOT,      0, "product/build.prop" },
164     { 00444, AID_ROOT,      AID_ROOT,      0, product_conf_dir + 1 },
165     { 00444, AID_ROOT,      AID_ROOT,      0, product_conf_file + 1 },
166     { 00600, AID_ROOT,      AID_ROOT,      0, "system_ext/build.prop" },
167     { 00444, AID_ROOT,      AID_ROOT,      0, system_ext_conf_dir + 1 },
168     { 00444, AID_ROOT,      AID_ROOT,      0, system_ext_conf_file + 1 },
169     { 00755, AID_ROOT,      AID_SHELL,     0, "system/bin/crash_dump32" },
170     { 00755, AID_ROOT,      AID_SHELL,     0, "system/bin/crash_dump64" },
171     { 00755, AID_ROOT,      AID_SHELL,     0, "system/bin/debuggerd" },
172     { 00550, AID_LOGD,      AID_LOGD,      0, "system/bin/logd" },
173     { 00700, AID_ROOT,      AID_ROOT,      0, "system/bin/secilc" },
174     { 00750, AID_ROOT,      AID_ROOT,      0, "system/bin/uncrypt" },
175     { 00600, AID_ROOT,      AID_ROOT,      0, "system/build.prop" },
176     { 00444, AID_ROOT,      AID_ROOT,      0, sys_conf_dir + 1 },
177     { 00444, AID_ROOT,      AID_ROOT,      0, sys_conf_file + 1 },
178     { 00440, AID_ROOT,      AID_SHELL,     0, "system/etc/init.goldfish.rc" },
179     { 00550, AID_ROOT,      AID_SHELL,     0, "system/etc/init.goldfish.sh" },
180     { 00550, AID_ROOT,      AID_SHELL,     0, "system/etc/init.ril" },
181     { 00555, AID_ROOT,      AID_ROOT,      0, "system/etc/ppp/*" },
182     { 00555, AID_ROOT,      AID_ROOT,      0, "system/etc/rc.*" },
183     { 00750, AID_ROOT,      AID_ROOT,      0, "vendor/bin/install-recovery.sh" },
184     { 00600, AID_ROOT,      AID_ROOT,      0, "vendor/build.prop" },
185     { 00600, AID_ROOT,      AID_ROOT,      0, "vendor/default.prop" },
186     { 00440, AID_ROOT,      AID_ROOT,      0, "vendor/etc/recovery.img" },
187     { 00444, AID_ROOT,      AID_ROOT,      0, ven_conf_dir + 1 },
188     { 00444, AID_ROOT,      AID_ROOT,      0, ven_conf_file + 1 },
189 
190     // the following two files are INTENTIONALLY set-uid, but they
191     // are NOT included on user builds.
192     { 06755, AID_ROOT,      AID_ROOT,      0, "system/xbin/procmem" },
193     { 04750, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
194 
195     // the following files have enhanced capabilities and ARE included
196     // in user builds.
197     { 00700, AID_SYSTEM,    AID_SHELL,     CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
198                                               "system/bin/inputflinger" },
199     { 00750, AID_ROOT,      AID_SHELL,     CAP_MASK_LONG(CAP_SETUID) |
200                                            CAP_MASK_LONG(CAP_SETGID),
201                                               "system/bin/run-as" },
202     { 00750, AID_ROOT,      AID_SHELL,     CAP_MASK_LONG(CAP_SETUID) |
203                                            CAP_MASK_LONG(CAP_SETGID),
204                                               "system/bin/simpleperf_app_runner" },
205     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/e2fsck" },
206 #ifdef __LP64__
207     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/linker64" },
208 #else
209     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/linker" },
210 #endif
211     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/resize2fs" },
212     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/snapuserd" },
213     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/tune2fs" },
214     { 00755, AID_ROOT,      AID_ROOT,      0, "first_stage_ramdisk/system/bin/fsck.f2fs" },
215     // generic defaults
216     { 00755, AID_ROOT,      AID_ROOT,      0, "bin/*" },
217     { 00640, AID_ROOT,      AID_SHELL,     0, "fstab.*" },
218     { 00750, AID_ROOT,      AID_SHELL,     0, "init*" },
219     { 00755, AID_ROOT,      AID_SHELL,     0, "odm/bin/*" },
220     { 00755, AID_ROOT,      AID_SHELL,     0, "product/bin/*" },
221     { 00755, AID_ROOT,      AID_SHELL,     0, "product/apex/*bin/*" },
222     { 00755, AID_ROOT,      AID_SHELL,     0, "system/bin/*" },
223     { 00755, AID_ROOT,      AID_SHELL,     0, "system/xbin/*" },
224     { 00755, AID_ROOT,      AID_SHELL,     0, "system/apex/*/bin/*" },
225     { 00755, AID_ROOT,      AID_SHELL,     0, "system_ext/bin/*" },
226     { 00755, AID_ROOT,      AID_SHELL,     0, "system_ext/apex/*/bin/*" },
227     { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/bin/*" },
228     { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/apex/*bin/*" },
229     { 00755, AID_ROOT,      AID_SHELL,     0, "vendor/xbin/*" },
230     { 00644, AID_ROOT,      AID_ROOT,      0, 0 },
231         // clang-format on
232 };
233 #ifndef __ANDROID_VNDK__
234 auto __for_testing_only__android_files = android_files;
235 #endif
236 
strip(const char * path,size_t len,const char suffix[])237 static size_t strip(const char* path, size_t len, const char suffix[]) {
238     if (len < strlen(suffix)) return len;
239     if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
240     return len - strlen(suffix);
241 }
242 
fs_config_open(int dir,int which,const char * target_out_path)243 static int fs_config_open(int dir, int which, const char* target_out_path) {
244     int fd = -1;
245 
246     if (target_out_path && *target_out_path) {
247         // target_out_path is the path to the directory holding content of
248         // system partition but as we cannot guarantee it ends with '/system'
249         // or with or without a trailing slash, need to strip them carefully.
250         char* name = NULL;
251         size_t len = strlen(target_out_path);
252         len = strip(target_out_path, len, "/");
253         len = strip(target_out_path, len, "/system");
254         if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
255             fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
256             free(name);
257         }
258     }
259     if (fd < 0) {
260         fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
261     }
262     return fd;
263 }
264 
265 // if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
266 // "system_ext/<stuff>" or "vendor/<stuff>"
is_partition(const std::string & path)267 static bool is_partition(const std::string& path) {
268     static const char* partitions[] = {"odm/", "oem/", "product/", "system_ext/", "vendor/"};
269     for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
270         if (StartsWith(path, partitions[i])) return true;
271     }
272     return false;
273 }
274 
275 // alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or
276 // "system/<partition>/<stuff>" to "<partition>/<stuff>"
fs_config_cmp(bool dir,const char * prefix,size_t len,const char * path,size_t plen)277 static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
278     std::string pattern(prefix, len);
279     std::string input(path, plen);
280 
281     // Massage pattern and input so that they can be used by fnmatch where
282     // directories have to end with /.
283     if (dir) {
284         if (!EndsWith(input, "/")) {
285             input.append("/");
286         }
287 
288         if (!EndsWith(pattern, "/*")) {
289             if (EndsWith(pattern, "/")) {
290                 pattern.append("*");
291             } else {
292                 pattern.append("/*");
293             }
294         }
295     }
296 
297     // no FNM_PATHNAME is set in order to match a/b/c/d with a/*
298     // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues.
299     const int fnm_flags = FNM_NOESCAPE;
300     if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true;
301 
302     // Check match between logical partition's files and patterns.
303     static constexpr const char* kLogicalPartitions[] = {"system/product/", "system/system_ext/",
304                                                          "system/vendor/", "vendor/odm/"};
305     for (auto& logical_partition : kLogicalPartitions) {
306         if (StartsWith(input, logical_partition)) {
307             std::string input_in_partition = input.substr(input.find('/') + 1);
308             if (!is_partition(input_in_partition)) continue;
309             if (fnmatch(pattern.c_str(), input_in_partition.c_str(), fnm_flags) == 0) {
310                 return true;
311             }
312         }
313     }
314     return false;
315 }
316 #ifndef __ANDROID_VNDK__
317 auto __for_testing_only__fs_config_cmp = fs_config_cmp;
318 #endif
319 
fs_config(const char * path,int dir,const char * target_out_path,unsigned * uid,unsigned * gid,unsigned * mode,uint64_t * capabilities)320 void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
321                unsigned* mode, uint64_t* capabilities) {
322     const struct fs_path_config* pc;
323     size_t which, plen;
324 
325     if (path[0] == '/') {
326         path++;
327     }
328 
329     plen = strlen(path);
330 
331     for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
332         struct fs_path_config_from_file header;
333 
334         int fd = fs_config_open(dir, which, target_out_path);
335         if (fd < 0) continue;
336 
337         while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
338             char* prefix;
339             uint16_t host_len = header.len;
340             ssize_t len, remainder = host_len - sizeof(header);
341             if (remainder <= 0) {
342                 ALOGE("%s len is corrupted", conf[which][dir]);
343                 break;
344             }
345             prefix = static_cast<char*>(calloc(1, remainder));
346             if (!prefix) {
347                 ALOGE("%s out of memory", conf[which][dir]);
348                 break;
349             }
350             if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
351                 free(prefix);
352                 ALOGE("%s prefix is truncated", conf[which][dir]);
353                 break;
354             }
355             len = strnlen(prefix, remainder);
356             if (len >= remainder) {  // missing a terminating null
357                 free(prefix);
358                 ALOGE("%s is corrupted", conf[which][dir]);
359                 break;
360             }
361             if (fs_config_cmp(dir, prefix, len, path, plen)) {
362                 free(prefix);
363                 close(fd);
364                 *uid = header.uid;
365                 *gid = header.gid;
366                 *mode = (*mode & (~07777)) | header.mode;
367                 *capabilities = header.capabilities;
368                 return;
369             }
370             free(prefix);
371         }
372         close(fd);
373     }
374 
375     for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
376         if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
377             break;
378         }
379     }
380     *uid = pc->uid;
381     *gid = pc->gid;
382     *mode = (*mode & (~07777)) | pc->mode;
383     *capabilities = pc->capabilities;
384 }
385