summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'libcutils/fs_config.cpp')
-rw-r--r--libcutils/fs_config.cpp393
1 files changed, 393 insertions, 0 deletions
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
new file mode 100644
index 000000000..f73dabfb2
--- /dev/null
+++ b/libcutils/fs_config.cpp
@@ -0,0 +1,393 @@
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// This file is used to define the properties of the filesystem
18// images generated by build tools (mkbootfs and mkyaffs2image) and
19// by the device side of adb.
20
21#define LOG_TAG "fs_config"
22
23#include <errno.h>
24#include <fcntl.h>
25#include <stdbool.h>
26#include <stdint.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32
33#include <log/log.h>
34#include <private/android_filesystem_config.h>
35#include <private/fs_config.h>
36#include <utils/Compat.h>
37
38#ifndef O_BINARY
39#define O_BINARY 0
40#endif
41
42// My kingdom for <endian.h>
43static inline uint16_t get2LE(const uint8_t* src) {
44 return src[0] | (src[1] << 8);
45}
46
47static inline uint64_t get8LE(const uint8_t* src) {
48 uint32_t low, high;
49
50 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
51 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
52 return ((uint64_t)high << 32) | (uint64_t)low;
53}
54
55#define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
56
57// Rules for directories.
58// These rules are applied based on "first match", so they
59// should start with the most specific path and work their
60// way up to the root.
61
62static const struct fs_path_config android_dirs[] = {
63 // clang-format off
64 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
65 { 00500, AID_ROOT, AID_ROOT, 0, "config" },
66 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
67 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
68 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" },
69 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
70 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
71 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
72 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
73 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
74 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
75 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
76 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
77 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
78 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" },
79 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" },
80 { 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" },
81 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
82 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
83 { 00755, AID_ROOT, AID_ROOT, 0, "root" },
84 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
85 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
86 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
87 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
88 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
89 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
90 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
91 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
92 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
93 // clang-format on
94};
95#ifndef __ANDROID_VNDK__
96auto __for_testing_only__android_dirs = android_dirs;
97#endif
98
99// Rules for files.
100// These rules are applied based on "first match", so they
101// should start with the most specific path and work their
102// way up to the root. Prefixes ending in * denotes wildcard
103// and will allow partial matches.
104static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
105static const char sys_conf_file[] = "/system/etc/fs_config_files";
106// No restrictions are placed on the vendor and oem file-system config files,
107// although the developer is advised to restrict the scope to the /vendor or
108// oem/ file-system since the intent is to provide support for customized
109// portions of a separate vendor.img or oem.img. Has to remain open so that
110// customization can also land on /system/vendor, /system/oem or /system/odm.
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)
113static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
114static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
115static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
116static const char oem_conf_file[] = "/oem/etc/fs_config_files";
117static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
118static const char odm_conf_file[] = "/odm/etc/fs_config_files";
119static const char* conf[][2] = {
120 {sys_conf_file, sys_conf_dir},
121 {ven_conf_file, ven_conf_dir},
122 {oem_conf_file, oem_conf_dir},
123 {odm_conf_file, odm_conf_dir},
124};
125
126static const struct fs_path_config android_files[] = {
127 // clang-format off
128 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
129 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
130 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
131 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
132 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
133 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
134 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
135 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
136 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
137 { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" },
138 { 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" },
139 { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" },
140 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
141 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
142 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
143 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
144 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
145 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
146 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
147 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
148 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
149 { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
150 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
151 { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
152 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
153 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
154 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
155 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
156 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
157 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
158 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
159 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
160 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
161 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
162 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
163 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
164
165 // the following two files are INTENTIONALLY set-uid, but they
166 // are NOT included on user builds.
167 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
168 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
169
170 // the following files have enhanced capabilities and ARE included
171 // in user builds.
172 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
173 "system/bin/inputflinger" },
174 { 00550, AID_LOGD, AID_LOGD, CAP_MASK_LONG(CAP_SYSLOG) |
175 CAP_MASK_LONG(CAP_AUDIT_CONTROL) |
176 CAP_MASK_LONG(CAP_SETGID),
177 "system/bin/logd" },
178 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
179 CAP_MASK_LONG(CAP_SETGID),
180 "system/bin/run-as" },
181
182 // Support FIFO scheduling mode in SurfaceFlinger.
183 { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
184 "system/bin/surfaceflinger" },
185
186 // Support hostapd administering a network interface.
187 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
188 CAP_MASK_LONG(CAP_NET_RAW),
189 "system/vendor/bin/hostapd" },
190 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
191 CAP_MASK_LONG(CAP_NET_RAW),
192 "vendor/bin/hostapd" },
193
194 // Support Bluetooth legacy hal accessing /sys/class/rfkill
195 // Support RT scheduling in Bluetooth
196 { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
197 CAP_MASK_LONG(CAP_SYS_NICE),
198 "vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
199
200 // Support wifi_hal_legacy administering a network interface.
201 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
202 CAP_MASK_LONG(CAP_NET_RAW),
203 "system/vendor/bin/hw/android.hardware.wifi@1.0-service" },
204 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
205 CAP_MASK_LONG(CAP_NET_RAW),
206 "vendor/bin/hw/android.hardware.wifi@1.0-service" },
207
208 // A non-privileged zygote that spawns isolated processes for web rendering.
209 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
210 CAP_MASK_LONG(CAP_SETGID) |
211 CAP_MASK_LONG(CAP_SETPCAP),
212 "system/bin/webview_zygote32" },
213 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
214 CAP_MASK_LONG(CAP_SETGID) |
215 CAP_MASK_LONG(CAP_SETPCAP),
216 "system/bin/webview_zygote64" },
217
218 // generic defaults
219 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
220 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
221 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
222 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
223 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
224 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
225 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
226 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
227 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
228 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
229 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
230 // clang-format on
231};
232#ifndef __ANDROID_VNDK__
233auto __for_testing_only__android_files = android_files;
234#endif
235
236static size_t strip(const char* path, size_t len, const char suffix[]) {
237 if (len < strlen(suffix)) return len;
238 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
239 return len - strlen(suffix);
240}
241
242static int fs_config_open(int dir, int which, const char* target_out_path) {
243 int fd = -1;
244
245 if (target_out_path && *target_out_path) {
246 // target_out_path is the path to the directory holding content of
247 // system partition but as we cannot guarantee it ends with '/system'
248 // or with or without a trailing slash, need to strip them carefully.
249 char* name = NULL;
250 size_t len = strlen(target_out_path);
251 len = strip(target_out_path, len, "/");
252 len = strip(target_out_path, len, "/system");
253 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
254 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
255 free(name);
256 }
257 }
258 if (fd < 0) {
259 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
260 }
261 return fd;
262}
263
264// if path is "vendor/<stuff>", "oem/<stuff>" or "odm/<stuff>"
265static bool is_partition(const char* path, size_t len) {
266 static const char* partitions[] = {"vendor/", "oem/", "odm/"};
267 for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
268 size_t plen = strlen(partitions[i]);
269 if (len <= plen) continue;
270 if (!strncmp(path, partitions[i], plen)) 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>"
277static bool prefix_cmp(const char* prefix, const char* path, size_t len) {
278 if (!strncmp(prefix, path, len)) return true;
279
280 static const char system[] = "system/";
281 if (!strncmp(path, system, strlen(system))) {
282 path += strlen(system);
283 } else if (len <= strlen(system)) {
284 return false;
285 } else if (strncmp(prefix, system, strlen(system))) {
286 return false;
287 } else {
288 prefix += strlen(system);
289 len -= strlen(system);
290 }
291 return is_partition(prefix, len) && !strncmp(prefix, path, len);
292}
293
294static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
295 if (dir) {
296 if (plen < len) {
297 return false;
298 }
299 } else {
300 // If name ends in * then allow partial matches.
301 if (prefix[len - 1] == '*') {
302 return prefix_cmp(prefix, path, len - 1);
303 }
304 if (plen != len) {
305 return false;
306 }
307 }
308 return prefix_cmp(prefix, path, len);
309}
310
311void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
312 unsigned* mode, uint64_t* capabilities) {
313 const struct fs_path_config* pc;
314 size_t which, plen;
315
316 if (path[0] == '/') {
317 path++;
318 }
319
320 plen = strlen(path);
321
322 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
323 struct fs_path_config_from_file header;
324
325 int fd = fs_config_open(dir, which, target_out_path);
326 if (fd < 0) continue;
327
328 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
329 char* prefix;
330 uint16_t host_len = get2LE((const uint8_t*)&header.len);
331 ssize_t len, remainder = host_len - sizeof(header);
332 if (remainder <= 0) {
333 ALOGE("%s len is corrupted", conf[which][dir]);
334 break;
335 }
336 prefix = static_cast<char*>(calloc(1, remainder));
337 if (!prefix) {
338 ALOGE("%s out of memory", conf[which][dir]);
339 break;
340 }
341 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
342 free(prefix);
343 ALOGE("%s prefix is truncated", conf[which][dir]);
344 break;
345 }
346 len = strnlen(prefix, remainder);
347 if (len >= remainder) { // missing a terminating null
348 free(prefix);
349 ALOGE("%s is corrupted", conf[which][dir]);
350 break;
351 }
352 if (fs_config_cmp(dir, prefix, len, path, plen)) {
353 free(prefix);
354 close(fd);
355 *uid = get2LE((const uint8_t*)&(header.uid));
356 *gid = get2LE((const uint8_t*)&(header.gid));
357 *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
358 *capabilities = get8LE((const uint8_t*)&(header.capabilities));
359 return;
360 }
361 free(prefix);
362 }
363 close(fd);
364 }
365
366 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
367 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
368 break;
369 }
370 }
371 *uid = pc->uid;
372 *gid = pc->gid;
373 *mode = (*mode & (~07777)) | pc->mode;
374 *capabilities = pc->capabilities;
375}
376
377ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
378 struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
379 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
380
381 if ((length < len) || (len > UINT16_MAX)) {
382 return -ENOSPC;
383 }
384 memset(p, 0, len);
385 uint16_t host_len = len;
386 p->len = get2LE((const uint8_t*)&host_len);
387 p->mode = get2LE((const uint8_t*)&(pc->mode));
388 p->uid = get2LE((const uint8_t*)&(pc->uid));
389 p->gid = get2LE((const uint8_t*)&(pc->gid));
390 p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
391 strcpy(p->prefix, pc->prefix);
392 return len;
393}