summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2017-05-03 11:57:09 -0500
committerandroid-build-merger2017-05-03 11:57:09 -0500
commit5cad367060170f11ba2065c14084e4ea3e2c455b (patch)
tree5e7560e5299c3d83cbe4392b704780fc9d89a92d /libcutils/fs_config.cpp
parent378c2a0c4f9ef9b78298bbb83eb17676f0428f84 (diff)
parent5f82055a2ee745e1fe8edbc71fb5ac0c8f385241 (diff)
downloadplatform-system-core-5cad367060170f11ba2065c14084e4ea3e2c455b.tar.gz
platform-system-core-5cad367060170f11ba2065c14084e4ea3e2c455b.tar.xz
platform-system-core-5cad367060170f11ba2065c14084e4ea3e2c455b.zip
Merge "libcutils: convert fs_config.cpp" am: c50245e43e am: a255ab0cb0
am: 5f82055a2e Change-Id: I93bdac8212b6d913c0851d4ef1145a1943a76e3b
Diffstat (limited to 'libcutils/fs_config.cpp')
-rw-r--r--libcutils/fs_config.cpp372
1 files changed, 372 insertions, 0 deletions
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
new file mode 100644
index 000000000..27750fe06
--- /dev/null
+++ b/libcutils/fs_config.cpp
@@ -0,0 +1,372 @@
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
96// Rules for files.
97// These rules are applied based on "first match", so they
98// should start with the most specific path and work their
99// way up to the root. Prefixes ending in * denotes wildcard
100// and will allow partial matches.
101static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
102static const char sys_conf_file[] = "/system/etc/fs_config_files";
103// No restrictions are placed on the vendor and oem file-system config files,
104// although the developer is advised to restrict the scope to the /vendor or
105// oem/ file-system since the intent is to provide support for customized
106// portions of a separate vendor.img or oem.img. Has to remain open so that
107// customization can also land on /system/vendor, /system/oem or /system/odm.
108// We expect build-time checking or filtering when constructing the associated
109// fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
110static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
111static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
112static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
113static const char oem_conf_file[] = "/oem/etc/fs_config_files";
114static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
115static const char odm_conf_file[] = "/odm/etc/fs_config_files";
116static const char* conf[][2] = {
117 {sys_conf_file, sys_conf_dir},
118 {ven_conf_file, ven_conf_dir},
119 {oem_conf_file, oem_conf_dir},
120 {odm_conf_file, odm_conf_dir},
121};
122
123static const struct fs_path_config android_files[] = {
124 // clang-format off
125 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
126 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
127 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
128 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
129 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
130 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
131 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
132 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
133 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
134 { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" },
135 { 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" },
136 { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" },
137 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
138 { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
139 { 00600, AID_ROOT, AID_ROOT, 0, "system/odm/build.prop" },
140 { 00600, AID_ROOT, AID_ROOT, 0, "system/odm/default.prop" },
141 { 00444, AID_ROOT, AID_ROOT, 0, "system/odm/etc/fs_config_dirs" },
142 { 00444, AID_ROOT, AID_ROOT, 0, "system/odm/etc/fs_config_files" },
143 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
144 { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
145 { 00444, AID_ROOT, AID_ROOT, 0, "system/oem/etc/fs_config_dirs" },
146 { 00444, AID_ROOT, AID_ROOT, 0, "system/oem/etc/fs_config_files" },
147 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
148 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
149 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
150 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
151 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
152 { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
153 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
154 { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
155 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
156 { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
157 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
158 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
159 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
160 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
161 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
162 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
163 { 00600, AID_ROOT, AID_ROOT, 0, "system/vendor/build.prop" },
164 { 00600, AID_ROOT, AID_ROOT, 0, "system/vendor/default.prop" },
165 { 00444, AID_ROOT, AID_ROOT, 0, "system/vendor/etc/fs_config_dirs" },
166 { 00444, AID_ROOT, AID_ROOT, 0, "system/vendor/etc/fs_config_files" },
167 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
168 { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
169 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
170 { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
171
172 // the following two files are INTENTIONALLY set-uid, but they
173 // are NOT included on user builds.
174 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
175 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
176
177 // the following files have enhanced capabilities and ARE included
178 // in user builds.
179 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
180 "system/bin/inputflinger" },
181 { 00550, AID_LOGD, AID_LOGD, CAP_MASK_LONG(CAP_SYSLOG) |
182 CAP_MASK_LONG(CAP_AUDIT_CONTROL) |
183 CAP_MASK_LONG(CAP_SETGID),
184 "system/bin/logd" },
185 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
186 CAP_MASK_LONG(CAP_SETGID),
187 "system/bin/run-as" },
188
189 // Support FIFO scheduling mode in SurfaceFlinger.
190 { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
191 "system/bin/surfaceflinger" },
192
193 // Support hostapd administering a network interface.
194 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
195 CAP_MASK_LONG(CAP_NET_RAW),
196 "system/vendor/bin/hostapd" },
197 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
198 CAP_MASK_LONG(CAP_NET_RAW),
199 "vendor/bin/hostapd" },
200
201 // Support Bluetooth legacy hal accessing /sys/class/rfkill
202 // Support RT scheduling in Bluetooth
203 { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
204 CAP_MASK_LONG(CAP_SYS_NICE),
205 "system/vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
206 { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
207 CAP_MASK_LONG(CAP_SYS_NICE),
208 "vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
209
210 // Support wifi_hal_legacy administering a network interface.
211 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
212 CAP_MASK_LONG(CAP_NET_RAW),
213 "system/vendor/bin/hw/android.hardware.wifi@1.0-service" },
214 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
215 CAP_MASK_LONG(CAP_NET_RAW),
216 "vendor/bin/hw/android.hardware.wifi@1.0-service" },
217
218 // A non-privileged zygote that spawns isolated processes for web rendering.
219 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
220 CAP_MASK_LONG(CAP_SETGID) |
221 CAP_MASK_LONG(CAP_SETPCAP),
222 "system/bin/webview_zygote32" },
223 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
224 CAP_MASK_LONG(CAP_SETGID) |
225 CAP_MASK_LONG(CAP_SETPCAP),
226 "system/bin/webview_zygote64" },
227
228 // generic defaults
229 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
230 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
231 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
232 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
233 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
234 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
235 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
236 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
237 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/xbin/*" },
238 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
239 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
240 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
241 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
242 // clang-format on
243};
244
245static size_t strip(const char* path, size_t len, const char suffix[]) {
246 if (len < strlen(suffix)) return len;
247 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
248 return len - strlen(suffix);
249}
250
251static int fs_config_open(int dir, int which, const char* target_out_path) {
252 int fd = -1;
253
254 if (target_out_path && *target_out_path) {
255 // target_out_path is the path to the directory holding content of
256 // system partition but as we cannot guarantee it ends with '/system'
257 // or with or without a trailing slash, need to strip them carefully.
258 char* name = NULL;
259 size_t len = strlen(target_out_path);
260 len = strip(target_out_path, len, "/");
261 len = strip(target_out_path, len, "/system");
262 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
263 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
264 free(name);
265 }
266 }
267 if (fd < 0) {
268 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
269 }
270 return fd;
271}
272
273static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
274 if (dir) {
275 if (plen < len) {
276 return false;
277 }
278 } else {
279 // If name ends in * then allow partial matches.
280 if (prefix[len - 1] == '*') {
281 return !strncmp(prefix, path, len - 1);
282 }
283 if (plen != len) {
284 return false;
285 }
286 }
287 return !strncmp(prefix, path, len);
288}
289
290void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
291 unsigned* mode, uint64_t* capabilities) {
292 const struct fs_path_config* pc;
293 size_t which, plen;
294
295 if (path[0] == '/') {
296 path++;
297 }
298
299 plen = strlen(path);
300
301 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
302 struct fs_path_config_from_file header;
303
304 int fd = fs_config_open(dir, which, target_out_path);
305 if (fd < 0) continue;
306
307 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
308 char* prefix;
309 uint16_t host_len = get2LE((const uint8_t*)&header.len);
310 ssize_t len, remainder = host_len - sizeof(header);
311 if (remainder <= 0) {
312 ALOGE("%s len is corrupted", conf[which][dir]);
313 break;
314 }
315 prefix = static_cast<char*>(calloc(1, remainder));
316 if (!prefix) {
317 ALOGE("%s out of memory", conf[which][dir]);
318 break;
319 }
320 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
321 free(prefix);
322 ALOGE("%s prefix is truncated", conf[which][dir]);
323 break;
324 }
325 len = strnlen(prefix, remainder);
326 if (len >= remainder) { // missing a terminating null
327 free(prefix);
328 ALOGE("%s is corrupted", conf[which][dir]);
329 break;
330 }
331 if (fs_config_cmp(dir, prefix, len, path, plen)) {
332 free(prefix);
333 close(fd);
334 *uid = get2LE((const uint8_t*)&(header.uid));
335 *gid = get2LE((const uint8_t*)&(header.gid));
336 *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
337 *capabilities = get8LE((const uint8_t*)&(header.capabilities));
338 return;
339 }
340 free(prefix);
341 }
342 close(fd);
343 }
344
345 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
346 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
347 break;
348 }
349 }
350 *uid = pc->uid;
351 *gid = pc->gid;
352 *mode = (*mode & (~07777)) | pc->mode;
353 *capabilities = pc->capabilities;
354}
355
356ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
357 struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
358 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
359
360 if ((length < len) || (len > UINT16_MAX)) {
361 return -ENOSPC;
362 }
363 memset(p, 0, len);
364 uint16_t host_len = len;
365 p->len = get2LE((const uint8_t*)&host_len);
366 p->mode = get2LE((const uint8_t*)&(pc->mode));
367 p->uid = get2LE((const uint8_t*)&(pc->uid));
368 p->gid = get2LE((const uint8_t*)&(pc->gid));
369 p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
370 strcpy(p->prefix, pc->prefix);
371 return len;
372}