blob: 171bf294e78a2f70f75682a7d24cc5f81cb8b1e7 [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 Salyzyn68651142015-04-01 09:24:22 -070040/* The following structure is stored little endian */
41struct fs_path_config_from_file {
42 uint16_t len;
43 uint16_t mode;
44 uint16_t uid;
45 uint16_t gid;
46 uint64_t capabilities;
47 char prefix[];
48} __attribute__((__aligned__(sizeof(uint64_t))));
49
50/* My kingdom for <endian.h> */
51static inline uint16_t get2LE(const uint8_t* src)
52{
53 return src[0] | (src[1] << 8);
54}
55
56static inline uint64_t get8LE(const uint8_t* src)
57{
58 uint32_t low, high;
59
60 low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
61 high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
62 return ((uint64_t) high << 32) | (uint64_t) low;
63}
64
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -070065#define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) )
66
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -070067/* Rules for directories.
68** These rules are applied based on "first match", so they
69** should start with the most specific path and work their
70** way up to the root.
71*/
72
73static const struct fs_path_config android_dirs[] = {
74 { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
75 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
76 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
77 { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
78 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
79 { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
80 { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
81 { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
82 { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
83 { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
84 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
85 { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
86 { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
87 { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
88 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
89 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
90 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
91 { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
92 { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
93 { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
94 { 00755, AID_ROOT, AID_ROOT, 0, 0 },
95};
96
97/* Rules for files.
98** These rules are applied based on "first match", so they
99** should start with the most specific path and work their
100** way up to the root. Prefixes ending in * denotes wildcard
101** and will allow partial matches.
102*/
Mark Salyzyn68651142015-04-01 09:24:22 -0700103static const char conf_dir[] = "/system/etc/fs_config_dirs";
104static const char conf_file[] = "/system/etc/fs_config_files";
105
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700106static const struct fs_path_config android_files[] = {
107 { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
108 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
109 { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
110 { 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
111 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
112 { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
Mark Salyzyn68651142015-04-01 09:24:22 -0700113 { 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
114 { 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700115 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
116 { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
117 { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
118 { 00644, AID_APP, AID_APP, 0, "data/data/*" },
119
120 /* the following five files are INTENTIONALLY set-uid, but they
121 * are NOT included on user builds. */
122 { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
123 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
124 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
125 { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
126 { 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
127
128 /* the following files have enhanced capabilities and ARE included in user builds. */
129 { 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
130 { 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
131
132 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
133 { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
134 { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
135 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
136 { 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
137 { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
138 { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
139 { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
140 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
141 { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
142 { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
143 { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
144 { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
145 { 00644, AID_ROOT, AID_ROOT, 0, 0 },
146};
147
Mark Salyzyn68651142015-04-01 09:24:22 -0700148static int fs_config_open(int dir)
149{
150 int fd = -1;
151
152 const char *out = getenv("OUT");
153 if (out && *out) {
154 char *name = NULL;
155 asprintf(&name, "%s%s", out, dir ? conf_dir : conf_file);
156 if (name) {
157 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY));
158 free(name);
159 }
160 }
161 if (fd < 0) {
162 fd = TEMP_FAILURE_RETRY(open(dir ? conf_dir : conf_file, O_RDONLY));
163 }
164 return fd;
165}
166
167static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
168 const char *path, size_t plen)
169{
170 if (dir) {
171 if (plen < len) {
172 return false;
173 }
174 } else {
175 /* If name ends in * then allow partial matches. */
176 if (prefix[len - 1] == '*') {
177 return !strncmp(prefix, path, len - 1);
178 }
179 if (plen != len) {
180 return false;
181 }
182 }
183 return !strncmp(prefix, path, len);
184}
185
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700186void fs_config(const char *path, int dir,
187 unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
188{
189 const struct fs_path_config *pc;
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700190 int fd, plen;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700191
192 if (path[0] == '/') {
193 path++;
194 }
195
Mark Salyzyn7e826332015-04-15 22:30:30 +0000196 plen = strlen(path);
Mark Salyzyn68651142015-04-01 09:24:22 -0700197
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700198 fd = fs_config_open(dir);
199 if (fd >= 0) {
200 struct fs_path_config_from_file header;
201
202 while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
203 char *prefix;
204 uint16_t host_len = get2LE((const uint8_t *)&header.len);
205 ssize_t len, remainder = host_len - sizeof(header);
Mark Salyzyn68651142015-04-01 09:24:22 -0700206 if (remainder <= 0) {
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700207 ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700208 break;
209 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700210 prefix = calloc(1, remainder);
211 if (!prefix) {
212 ALOGE("%s out of memory", dir ? conf_dir : conf_file);
213 break;
Mark Salyzyn68651142015-04-01 09:24:22 -0700214 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700215 if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
216 free(prefix);
217 ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file);
218 break;
219 }
220 len = strnlen(prefix, remainder);
Mark Salyzyn68651142015-04-01 09:24:22 -0700221 if (len >= remainder) { /* missing a terminating null */
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700222 free(prefix);
Mark Salyzyn68651142015-04-01 09:24:22 -0700223 ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
Mark Salyzyn68651142015-04-01 09:24:22 -0700224 break;
225 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700226 if (fs_config_cmp(dir, prefix, len, path, plen)) {
227 free(prefix);
228 close(fd);
229 *uid = get2LE((const uint8_t *)&(header.uid));
230 *gid = get2LE((const uint8_t *)&(header.gid));
231 *mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode));
232 *capabilities = get8LE((const uint8_t *)&(header.capabilities));
233 return;
Mark Salyzyn68651142015-04-01 09:24:22 -0700234 }
Mark Salyzyn7977cc62015-04-15 19:27:39 -0700235 free(prefix);
Mark Salyzyn7e826332015-04-15 22:30:30 +0000236 }
Mark Salyzyn68651142015-04-01 09:24:22 -0700237 close(fd);
Mark Salyzyn68651142015-04-01 09:24:22 -0700238 }
239
240 pc = dir ? android_dirs : android_files;
241 for(; pc->prefix; pc++){
242 if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
243 break;
Mark Salyzyn8b2c7de2015-04-01 07:42:01 -0700244 }
245 }
246 *uid = pc->uid;
247 *gid = pc->gid;
248 *mode = (*mode & (~07777)) | pc->mode;
249 *capabilities = pc->capabilities;
250}
Mark Salyzyn5d9e5ef2015-04-01 11:02:00 -0700251
252ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc)
253{
254 struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer;
255 size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
256
257 if ((length < len) || (len > UINT16_MAX)) {
258 return -ENOSPC;
259 }
260 memset(p, 0, len);
261 uint16_t host_len = len;
262 p->len = get2LE((const uint8_t *)&host_len);
263 p->mode = get2LE((const uint8_t *)&(pc->mode));
264 p->uid = get2LE((const uint8_t *)&(pc->uid));
265 p->gid = get2LE((const uint8_t *)&(pc->gid));
266 p->capabilities = get8LE((const uint8_t *)&(pc->capabilities));
267 strcpy(p->prefix, pc->prefix);
268 return len;
269}