blob: 9f8023e4f3946a2f76a229f222e8d20ebe74726f [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 Salyzyn68651142015-04-01 09:24:22 -070036#include <log/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" },
79 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
80 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
81 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
82 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
83 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
84 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
85 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
86 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
87 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
88 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
89 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
90 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
91 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
92 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
93 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
94 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
95 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
96 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
97 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
98 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
99};
100
101/* Rules for files.
102** These rules are applied based on "first match", so they
103** should start with the most specific path and work their
104** way up to the root. Prefixes ending in * denotes wildcard
105** and will allow partial matches.
106*/
Mark Salyzyn68651142015-04-01 09:24:22 -0700107static const char conf_dir[] = "/system/etc/fs_config_dirs";
108static const char conf_file[] = "/system/etc/fs_config_files";
109
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700110static const struct fs_path_config android_files[] = {
111 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
112 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
113 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
114 { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
115 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
116 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700117 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
118 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700119 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
120 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
121 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
122 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
123
124 /* the following five files are INTENTIONALLY set-uid, but they
125 * are NOT included on user builds. */
126 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
127 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
128 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
129 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
130 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
131
132 /* the following files have enhanced capabilities and ARE included in user builds. */
133 { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
134 { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
135
136 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
137 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
138 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
139 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
140 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
141 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
142 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
143 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
144 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
145 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
146 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
147 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
148 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
149 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
150};
151
Mark Salyzyn68651142015-04-01 09:24:22 -0700152static int fs_config_open(int dir)
153{
154 int fd = -1;
155
156 const char *out = getenv("OUT");
157 if (out && *out) {
158 char *name = NULL;
159 asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file);
160 if (name) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700161 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700162 free(name);
163 }
164 }
165 if (fd < 0) {
Mark Salyzyn4232b372015-04-16 08:40:55 -0700166 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY | O_BINARY));
Mark Salyzyn68651142015-04-01 09:24:22 -0700167 }
168 return fd;
169}
170
171static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
172 const char *path, size_t plen)
173{
174 if (dir) {
175 if (plen < len) {
176 return false;
177 }
178 } else {
179 /* If name ends in * then allow partial matches. */
180 if (prefix[len - 1] == '*') {
181 return !strncmp(prefix, path, len - 1);
182 }
183 if (plen != len) {
184 return false;
185 }
186 }
187 return !strncmp(prefix, path, len);
188}
189
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700190void fs_config(const char *path, int dir,
191 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
192{
193 const struct fs_path_config *pc;
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700194 int fd, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700195
196 if (path[0] == '/') {
197 path++;
198 }
199
Mark Salyzyn7e826332015-04-15 22:30:30 +0000200 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700201
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700202 fd = fs_config_open(dir);
203 if (fd >= 0) {
204 struct fs_path_config_from_file header;
205
206 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
207 char *prefix;
208 uint16_t host_len = get2LE((const uint8_t *)&header.len);
209 ssize_t len, remainder = host_len - sizeof(header);
Mark Salyzyn68651142015-04-01 09:24:22 -0700210 if (remainder <= 0) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700211 ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700212 break;
213 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700214 prefix = calloc(1, remainder);
215 if (!prefix) {
216 ALOGE("%s out of memory", dir ? conf_dir : conf_file);
217 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700218 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700219 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
220 free(prefix);
221 ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file);
222 break;
223 }
224 len = strnlen(prefix, remainder);
Mark Salyzyn68651142015-04-01 09:24:22 -0700225 if (len >= remainder) { /* missing a terminating null */
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700226 free(prefix);
Mark Salyzyn68651142015-04-01 09:24:22 -0700227 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700228 break;
229 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700230 if (fs_config_cmp(dir, prefix, len, path, plen)) {
231 free(prefix);
232 close(fd);
233 *uid = get2LE((const uint8_t *)&(header.uid));
234 *gid = get2LE((const uint8_t *)&(header.gid));
235 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode));
236 *capabilities = get8LE((const uint8_t *)&(header.capabilities));
237 return;
Mark Salyzyn68651142015-04-01 09:24:22 -0700238 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700239 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000240 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700241 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700242 }
243
244 pc = dir ? android_dirs : android_files;
245 for(; pc->prefix; pc++){
246 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
247 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700248 }
249 }
250 *uid = pc->uid;
251 *gid = pc->gid;
252 *mode = (*mode & (~07777)) | pc->mode;
253 *capabilities = pc->capabilities;
254}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700255
256ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc)
257{
258 struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer;
259 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
260
261 if ((length < len) || (len > UINT16_MAX)) {
262 return -ENOSPC;
263 }
264 memset(p, 0, len);
265 uint16_t host_len = len;
266 p->len = get2LE((const uint8_t *)&host_len);
267 p->mode = get2LE((const uint8_t *)&(pc->mode));
268 p->uid = get2LE((const uint8_t *)&(pc->uid));
269 p->gid = get2LE((const uint8_t *)&(pc->gid));
270 p->capabilities = get8LE((const uint8_t *)&(pc->capabilities));
271 strcpy(p->prefix, pc->prefix);
272 return len;
273}