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