blob: 99086371f9a1f523b200278c741b9e069a8cd256 [file] [log] [blame]
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -07001/*
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
Mark Salyzyn68651142015-04-01 09:24:22 -070022#define LOG_TAG "fs_config"
Mark Salyzyn4e5f71a2015-04-01 09:24:22 -070023
Mark Salyzyn68651142015-04-01 09:24:22 -070024#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>
Mark Salyzyn68651142015-04-01 09:24:22 -070033#include <sys/stat.h>
34#include <sys/types.h>
35
Mark Salyzyn23ed4c22016-09-28 13:33:27 -070036#include <android/log.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070037#include <private/android_filesystem_config.h>
Mark Salyzynfd5b4252015-04-16 08:13:32 -070038#include <utils/Compat.h>
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070039
Mark Salyzyn4232b372015-04-16 08:40:55 -070040#ifndef O_BINARY
41#define O_BINARY 0
42#endif
43
Mark Salyzyn68651142015-04-01 09:24:22 -070044/* The following structure is stored little endian */
45struct fs_path_config_from_file {
46 uint16_t len;
47 uint16_t mode;
48 uint16_t uid;
49 uint16_t gid;
50 uint64_t capabilities;
51 char prefix[];
52} __attribute__((__aligned__(sizeof(uint64_t))));
53
54/* My kingdom for <endian.h> */
55static inline uint16_t get2LE(const uint8_t* src)
56{
57 return src[0] | (src[1] << 8);
58}
59
60static inline uint64_t get8LE(const uint8_t* src)
61{
62 uint32_t low, high;
63
64 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
65 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
66 return ((uint64_t) high << 32) | (uint64_t) low;
67}
68
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -070069#define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) )
70
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070071/* Rules for directories.
72** These rules are applied based on "first match", so they
73** should start with the most specific path and work their
74** way up to the root.
75*/
76
77static const struct fs_path_config android_dirs[] = {
78 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
Daniel Rosenbergbbe796d2015-06-12 15:00:54 -070079 { 00500, AID_ROOT, AID_ROOT, 0, "config" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070080 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
81 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
Todd Kennedy0a273352015-11-23 15:24:13 -080082 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070083 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
84 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
85 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
86 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
87 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
88 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
89 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
90 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
91 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
Gilad Arnold47af47f2015-11-12 14:34:57 -080092 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" },
93 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070094 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
Daniel Rosenbergbbe796d2015-06-12 15:00:54 -070095 { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
96 { 00755, AID_ROOT, AID_ROOT, 0, "root" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070097 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
Daniel Rosenbergbbe796d2015-06-12 15:00:54 -070098 { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070099 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
100 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
101 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
102 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
103 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
104 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
105 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
106};
107
108/* Rules for files.
109** These rules are applied based on "first match", so they
110** should start with the most specific path and work their
111** way up to the root. Prefixes ending in * denotes wildcard
112** and will allow partial matches.
113*/
Mark Salyzyn68651142015-04-01 09:24:22 -0700114static const char conf_dir[] = "/system/etc/fs_config_dirs";
115static const char conf_file[] = "/system/etc/fs_config_files";
116
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700117static const struct fs_path_config android_files[] = {
118 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
119 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
120 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700121 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
122 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Tao Baoc6e93fe2015-07-18 19:46:16 -0700123 { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700124 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
125 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700126 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
127 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
128 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
Todd Kennedy0a273352015-11-23 15:24:13 -0800129 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700130 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
Gilad Arnold1bc78882015-11-14 18:25:31 -0800131 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
132 { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
Daniel Erat5510c132015-10-28 16:52:37 -0600133 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
134 { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700135
Nick Kralevichcbbc6612015-11-30 09:17:18 -0800136 /* the following two files are INTENTIONALLY set-uid, but they
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700137 * are NOT included on user builds. */
138 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700139 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700140
141 /* the following files have enhanced capabilities and ARE included in user builds. */
Jorge Lucangeli Obese920c462015-09-02 16:19:59 -0700142 { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) | CAP_MASK_LONG(CAP_SETGID), "system/bin/run-as" },
143 { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700144
Christopher Wiley6f8e12e2016-07-12 14:30:21 -0700145 /* Support hostapd administering a network interface. */
146 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) |
147 CAP_MASK_LONG(CAP_NET_RAW), "system/bin/hostapd" },
148
Mitchell Willsd44d3b52016-09-02 11:14:35 -0700149 /* Support wifi_hal_legacy administering a network interface. */
Roshan Piusf6ac81e2016-10-04 11:32:30 -0700150 { 00755, AID_WIFI, AID_WIFI, CAP_MASK_LONG(CAP_NET_ADMIN) | CAP_MASK_LONG(CAP_NET_RAW), "system/bin/hw/android.hardware.wifi@1.0-service" },
Mitchell Willsd44d3b52016-09-02 11:14:35 -0700151
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700152 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
153 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
154 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
155 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
156 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
157 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
158 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
Erik Klinea5a9c742016-05-27 13:13:07 +0900159 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/xbin/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700160 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
Erik Klinea5a9c742016-05-27 13:13:07 +0900161 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700162 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
163 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
164 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
165 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
166 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
167 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
168};
169
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700170static int fs_config_open(int dir, const char *target_out_path)
Mark Salyzyn68651142015-04-01 09:24:22 -0700171{
172 int fd = -1;
173
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700174 if (target_out_path && *target_out_path) {
175 /* target_out_path is the path to the directory holding content of system partition
176 but as we cannot guaranty it ends with '/system' we need this below skip_len logic */
Mark Salyzyn68651142015-04-01 09:24:22 -0700177 char *name = NULL;
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700178 int target_out_path_len = strlen(target_out_path);
179 int skip_len = strlen("/system");
180
181 if (target_out_path[target_out_path_len] == '/') {
182 skip_len++;
183 }
Elliott Hughes8b262b92015-07-24 20:02:31 -0700184 if (asprintf(&name, "%s%s", target_out_path, (dir ? conf_dir : conf_file) + skip_len) != -1) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700185 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700186 free(name);
187 }
188 }
189 if (fd < 0) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700190 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700191 }
192 return fd;
193}
194
195static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
196 const char *path, size_t plen)
197{
198 if (dir) {
199 if (plen < len) {
200 return false;
201 }
202 } else {
203 /* If name ends in * then allow partial matches. */
204 if (prefix[len - 1] == '*') {
205 return !strncmp(prefix, path, len - 1);
206 }
207 if (plen != len) {
208 return false;
209 }
210 }
211 return !strncmp(prefix, path, len);
212}
213
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700214void fs_config(const char *path, int dir, const char *target_out_path,
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700215 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
216{
217 const struct fs_path_config *pc;
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700218 int fd, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700219
220 if (path[0] == '/') {
221 path++;
222 }
223
Mark Salyzyn7e826332015-04-15 22:30:30 +0000224 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700225
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700226 fd = fs_config_open(dir, target_out_path);
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700227 if (fd >= 0) {
228 struct fs_path_config_from_file header;
229
230 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
231 char *prefix;
232 uint16_t host_len = get2LE((const uint8_t *)&header.len);
233 ssize_t len, remainder = host_len - sizeof(header);
Mark Salyzyn68651142015-04-01 09:24:22 -0700234 if (remainder <= 0) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700235 ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700236 break;
237 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700238 prefix = calloc(1, remainder);
239 if (!prefix) {
240 ALOGE("%s out of memory", dir ? conf_dir : conf_file);
241 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700242 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700243 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
244 free(prefix);
245 ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file);
246 break;
247 }
248 len = strnlen(prefix, remainder);
Mark Salyzyn68651142015-04-01 09:24:22 -0700249 if (len >= remainder) { /* missing a terminating null */
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700250 free(prefix);
Mark Salyzyn68651142015-04-01 09:24:22 -0700251 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700252 break;
253 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700254 if (fs_config_cmp(dir, prefix, len, path, plen)) {
255 free(prefix);
256 close(fd);
257 *uid = get2LE((const uint8_t *)&(header.uid));
258 *gid = get2LE((const uint8_t *)&(header.gid));
259 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode));
260 *capabilities = get8LE((const uint8_t *)&(header.capabilities));
261 return;
Mark Salyzyn68651142015-04-01 09:24:22 -0700262 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700263 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000264 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700265 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700266 }
267
268 pc = dir ? android_dirs : android_files;
269 for(; pc->prefix; pc++){
270 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
271 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700272 }
273 }
274 *uid = pc->uid;
275 *gid = pc->gid;
276 *mode = (*mode & (~07777)) | pc->mode;
277 *capabilities = pc->capabilities;
278}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700279
280ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc)
281{
282 struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer;
283 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
284
285 if ((length < len) || (len > UINT16_MAX)) {
286 return -ENOSPC;
287 }
288 memset(p, 0, len);
289 uint16_t host_len = len;
290 p->len = get2LE((const uint8_t *)&host_len);
291 p->mode = get2LE((const uint8_t *)&(pc->mode));
292 p->uid = get2LE((const uint8_t *)&(pc->uid));
293 p->gid = get2LE((const uint8_t *)&(pc->gid));
294 p->capabilities = get8LE((const uint8_t *)&(pc->capabilities));
295 strcpy(p->prefix, pc->prefix);
296 return len;
297}