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