summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Salyzyn2017-05-02 10:56:15 -0500
committerMark Salyzyn2017-05-03 10:04:35 -0500
commit163ecc68c7a7ee06a384880fec0d05d303812a4e (patch)
treead606f2d47fb000b5e4205a979391d5c337db29e /libcutils/fs_config.cpp
parent0447a3e5d1591b0a232106534ca0463bac5346dc (diff)
downloadplatform-system-core-163ecc68c7a7ee06a384880fec0d05d303812a4e.tar.gz
platform-system-core-163ecc68c7a7ee06a384880fec0d05d303812a4e.tar.xz
platform-system-core-163ecc68c7a7ee06a384880fec0d05d303812a4e.zip
libcutils: convert fs_config.cpp
C++ify fs_config.c Test: gTest libcutils-test Bug: 37703469 Change-Id: Id48ae22f203ed923942257575296c69b32345ae6
Diffstat (limited to 'libcutils/fs_config.cpp')
-rw-r--r--libcutils/fs_config.cpp369
1 files changed, 369 insertions, 0 deletions
diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
new file mode 100644
index 000000000..a2dd67776
--- /dev/null
+++ b/libcutils/fs_config.cpp
@@ -0,0 +1,369 @@
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/bin/hostapd" },
197
198 // Support Bluetooth legacy hal accessing /sys/class/rfkill
199 // Support RT scheduling in Bluetooth
200 { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
201 CAP_MASK_LONG(CAP_SYS_NICE),
202 "system/vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
203 { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN) |
204 CAP_MASK_LONG(CAP_SYS_NICE),
205 "vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
206
207 // Support wifi_hal_legacy administering a network interface.
208 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
209 CAP_MASK_LONG(CAP_NET_RAW),
210 "system/vendor/bin/hw/android.hardware.wifi@1.0-service" },
211 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
212 CAP_MASK_LONG(CAP_NET_RAW),
213 "vendor/bin/hw/android.hardware.wifi@1.0-service" },
214
215 // A non-privileged zygote that spawns isolated processes for web rendering.
216 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
217 CAP_MASK_LONG(CAP_SETGID) |
218 CAP_MASK_LONG(CAP_SETPCAP),
219 "system/bin/webview_zygote32" },
220 { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
221 CAP_MASK_LONG(CAP_SETGID) |
222 CAP_MASK_LONG(CAP_SETPCAP),
223 "system/bin/webview_zygote64" },
224
225 // generic defaults
226 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
227 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
228 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
229 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
230 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
231 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
232 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
233 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
234 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/xbin/*" },
235 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
236 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
237 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
238 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
239 // clang-format on
240};
241
242static size_t strip(const char* path, size_t len, const char suffix[]) {
243 if (len < strlen(suffix)) return len;
244 if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
245 return len - strlen(suffix);
246}
247
248static int fs_config_open(int dir, int which, const char* target_out_path) {
249 int fd = -1;
250
251 if (target_out_path && *target_out_path) {
252 // target_out_path is the path to the directory holding content of
253 // system partition but as we cannot guarantee it ends with '/system'
254 // or with or without a trailing slash, need to strip them carefully.
255 char* name = NULL;
256 size_t len = strlen(target_out_path);
257 len = strip(target_out_path, len, "/");
258 len = strip(target_out_path, len, "/system");
259 if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
260 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
261 free(name);
262 }
263 }
264 if (fd < 0) {
265 fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
266 }
267 return fd;
268}
269
270static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
271 if (dir) {
272 if (plen < len) {
273 return false;
274 }
275 } else {
276 // If name ends in * then allow partial matches.
277 if (prefix[len - 1] == '*') {
278 return !strncmp(prefix, path, len - 1);
279 }
280 if (plen != len) {
281 return false;
282 }
283 }
284 return !strncmp(prefix, path, len);
285}
286
287void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
288 unsigned* mode, uint64_t* capabilities) {
289 const struct fs_path_config* pc;
290 size_t which, plen;
291
292 if (path[0] == '/') {
293 path++;
294 }
295
296 plen = strlen(path);
297
298 for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
299 struct fs_path_config_from_file header;
300
301 int fd = fs_config_open(dir, which, target_out_path);
302 if (fd < 0) continue;
303
304 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
305 char* prefix;
306 uint16_t host_len = get2LE((const uint8_t*)&header.len);
307 ssize_t len, remainder = host_len - sizeof(header);
308 if (remainder <= 0) {
309 ALOGE("%s len is corrupted", conf[which][dir]);
310 break;
311 }
312 prefix = static_cast<char*>(calloc(1, remainder));
313 if (!prefix) {
314 ALOGE("%s out of memory", conf[which][dir]);
315 break;
316 }
317 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
318 free(prefix);
319 ALOGE("%s prefix is truncated", conf[which][dir]);
320 break;
321 }
322 len = strnlen(prefix, remainder);
323 if (len >= remainder) { // missing a terminating null
324 free(prefix);
325 ALOGE("%s is corrupted", conf[which][dir]);
326 break;
327 }
328 if (fs_config_cmp(dir, prefix, len, path, plen)) {
329 free(prefix);
330 close(fd);
331 *uid = get2LE((const uint8_t*)&(header.uid));
332 *gid = get2LE((const uint8_t*)&(header.gid));
333 *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
334 *capabilities = get8LE((const uint8_t*)&(header.capabilities));
335 return;
336 }
337 free(prefix);
338 }
339 close(fd);
340 }
341
342 for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
343 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
344 break;
345 }
346 }
347 *uid = pc->uid;
348 *gid = pc->gid;
349 *mode = (*mode & (~07777)) | pc->mode;
350 *capabilities = pc->capabilities;
351}
352
353ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
354 struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
355 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
356
357 if ((length < len) || (len > UINT16_MAX)) {
358 return -ENOSPC;
359 }
360 memset(p, 0, len);
361 uint16_t host_len = len;
362 p->len = get2LE((const uint8_t*)&host_len);
363 p->mode = get2LE((const uint8_t*)&(pc->mode));
364 p->uid = get2LE((const uint8_t*)&(pc->uid));
365 p->gid = get2LE((const uint8_t*)&(pc->gid));
366 p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
367 strcpy(p->prefix, pc->prefix);
368 return len;
369}