fs_config.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. #include <private/fs_config.h>
  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. #define LOG_TAG "fs_config"
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <fnmatch.h>
  24. #include <stdint.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <string>
  31. #include <android-base/strings.h>
  32. #include <log/log.h>
  33. #include <private/android_filesystem_config.h>
  34. #include <utils/Compat.h>
  35. #ifndef O_BINARY
  36. #define O_BINARY 0
  37. #endif
  38. using android::base::EndsWith;
  39. using android::base::StartsWith;
  40. // My kingdom for <endian.h>
  41. static inline uint16_t get2LE(const uint8_t* src) {
  42. return src[0] | (src[1] << 8);
  43. }
  44. static inline uint64_t get8LE(const uint8_t* src) {
  45. uint32_t low, high;
  46. low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
  47. high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
  48. return ((uint64_t)high << 32) | (uint64_t)low;
  49. }
  50. #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
  51. // Rules for directories.
  52. // These rules are applied based on "first match", so they
  53. // should start with the most specific path and work their
  54. // way up to the root.
  55. static const struct fs_path_config android_dirs[] = {
  56. // clang-format off
  57. { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
  58. { 00555, AID_ROOT, AID_ROOT, 0, "config" },
  59. { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
  60. { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
  61. { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral" },
  62. { 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
  63. { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
  64. { 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
  65. { 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
  66. { 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
  67. { 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
  68. { 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
  69. { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
  70. { 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
  71. { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest" },
  72. { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64" },
  73. { 00775, AID_ROOT, AID_ROOT, 0, "data/preloads" },
  74. { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
  75. { 00755, AID_ROOT, AID_SYSTEM, 0, "mnt" },
  76. { 00751, AID_ROOT, AID_SHELL, 0, "product/bin" },
  77. { 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
  78. { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
  79. { 00751, AID_ROOT, AID_SDCARD_R, 0, "storage" },
  80. { 00751, AID_ROOT, AID_SHELL, 0, "system/bin" },
  81. { 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
  82. { 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
  83. { 00751, AID_ROOT, AID_SHELL, 0, "system/xbin" },
  84. { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin" },
  85. { 00751, AID_ROOT, AID_SHELL, 0, "vendor/bin" },
  86. { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
  87. { 00755, AID_ROOT, AID_ROOT, 0, 0 },
  88. // clang-format on
  89. };
  90. #ifndef __ANDROID_VNDK__
  91. auto __for_testing_only__android_dirs = android_dirs;
  92. #endif
  93. // Rules for files.
  94. // These rules are applied based on "first match", so they
  95. // should start with the most specific path and work their
  96. // way up to the root. Prefixes ending in * denotes wildcard
  97. // and will allow partial matches.
  98. static const char sys_conf_dir[] = "/system/etc/fs_config_dirs";
  99. static const char sys_conf_file[] = "/system/etc/fs_config_files";
  100. // No restrictions are placed on the vendor and oem file-system config files,
  101. // although the developer is advised to restrict the scope to the /vendor or
  102. // oem/ file-system since the intent is to provide support for customized
  103. // portions of a separate vendor.img or oem.img. Has to remain open so that
  104. // customization can also land on /system/vendor, /system/oem, /system/odm,
  105. // /system/product or /system/product_services.
  106. //
  107. // We expect build-time checking or filtering when constructing the associated
  108. // fs_config_* files (see build/tools/fs_config/fs_config_generate.c)
  109. static const char ven_conf_dir[] = "/vendor/etc/fs_config_dirs";
  110. static const char ven_conf_file[] = "/vendor/etc/fs_config_files";
  111. static const char oem_conf_dir[] = "/oem/etc/fs_config_dirs";
  112. static const char oem_conf_file[] = "/oem/etc/fs_config_files";
  113. static const char odm_conf_dir[] = "/odm/etc/fs_config_dirs";
  114. static const char odm_conf_file[] = "/odm/etc/fs_config_files";
  115. static const char product_conf_dir[] = "/product/etc/fs_config_dirs";
  116. static const char product_conf_file[] = "/product/etc/fs_config_files";
  117. static const char product_services_conf_dir[] = "/product_services/etc/fs_config_dirs";
  118. static const char product_services_conf_file[] = "/product_services/etc/fs_config_files";
  119. static const char* conf[][2] = {
  120. {sys_conf_file, sys_conf_dir},
  121. {ven_conf_file, ven_conf_dir},
  122. {oem_conf_file, oem_conf_dir},
  123. {odm_conf_file, odm_conf_dir},
  124. {product_conf_file, product_conf_dir},
  125. {product_services_conf_file, product_services_conf_dir},
  126. };
  127. // Do not use android_files to grant Linux capabilities. Use ambient capabilities in their
  128. // associated init.rc file instead. See https://source.android.com/devices/tech/config/ambient.
  129. // Do not place any new vendor/, data/vendor/, etc entries in android_files.
  130. // Vendor entries should be done via a vendor or device specific config.fs.
  131. // See https://source.android.com/devices/tech/config/filesystem#using-file-system-capabilities
  132. static const struct fs_path_config android_files[] = {
  133. // clang-format off
  134. { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
  135. { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-ephemeral/*" },
  136. { 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
  137. { 00644, AID_APP, AID_APP, 0, "data/data/*" },
  138. { 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
  139. { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest/tests.txt" },
  140. { 00640, AID_ROOT, AID_SHELL, 0, "data/nativetest64/tests.txt" },
  141. { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest/*" },
  142. { 00750, AID_ROOT, AID_SHELL, 0, "data/nativetest64/*" },
  143. { 00600, AID_ROOT, AID_ROOT, 0, "default.prop" }, // legacy
  144. { 00600, AID_ROOT, AID_ROOT, 0, "system/etc/prop.default" },
  145. { 00600, AID_ROOT, AID_ROOT, 0, "odm/build.prop" }, // legacy; only for P release
  146. { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" }, // legacy; only for P release
  147. { 00600, AID_ROOT, AID_ROOT, 0, "odm/etc/build.prop" },
  148. { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_dir + 1 },
  149. { 00444, AID_ROOT, AID_ROOT, 0, odm_conf_file + 1 },
  150. { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_dir + 1 },
  151. { 00444, AID_ROOT, AID_ROOT, 0, oem_conf_file + 1 },
  152. { 00600, AID_ROOT, AID_ROOT, 0, "product/build.prop" },
  153. { 00444, AID_ROOT, AID_ROOT, 0, product_conf_dir + 1 },
  154. { 00444, AID_ROOT, AID_ROOT, 0, product_conf_file + 1 },
  155. { 00600, AID_ROOT, AID_ROOT, 0, "product_services/build.prop" },
  156. { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_dir + 1 },
  157. { 00444, AID_ROOT, AID_ROOT, 0, product_services_conf_file + 1 },
  158. { 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
  159. { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump32" },
  160. { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/crash_dump64" },
  161. { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/debuggerd" },
  162. { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
  163. { 00550, AID_LOGD, AID_LOGD, 0, "system/bin/logd" },
  164. { 00700, AID_ROOT, AID_ROOT, 0, "system/bin/secilc" },
  165. { 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
  166. { 00600, AID_ROOT, AID_ROOT, 0, "system/build.prop" },
  167. { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_dir + 1 },
  168. { 00444, AID_ROOT, AID_ROOT, 0, sys_conf_file + 1 },
  169. { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
  170. { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
  171. { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
  172. { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
  173. { 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
  174. { 00440, AID_ROOT, AID_ROOT, 0, "system/etc/recovery.img" },
  175. { 00600, AID_ROOT, AID_ROOT, 0, "vendor/build.prop" },
  176. { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
  177. { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_dir + 1 },
  178. { 00444, AID_ROOT, AID_ROOT, 0, ven_conf_file + 1 },
  179. // the following two files are INTENTIONALLY set-uid, but they
  180. // are NOT included on user builds.
  181. { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
  182. { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
  183. // the following files have enhanced capabilities and ARE included
  184. // in user builds.
  185. { 00700, AID_SYSTEM, AID_SHELL, CAP_MASK_LONG(CAP_BLOCK_SUSPEND),
  186. "system/bin/inputflinger" },
  187. { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
  188. CAP_MASK_LONG(CAP_SETGID),
  189. "system/bin/run-as" },
  190. { 00750, AID_ROOT, AID_SHELL, CAP_MASK_LONG(CAP_SETUID) |
  191. CAP_MASK_LONG(CAP_SETGID),
  192. "system/bin/simpleperf_app_runner" },
  193. // Support FIFO scheduling mode in SurfaceFlinger.
  194. { 00755, AID_SYSTEM, AID_GRAPHICS, CAP_MASK_LONG(CAP_SYS_NICE),
  195. "system/bin/surfaceflinger" },
  196. // generic defaults
  197. { 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
  198. { 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
  199. { 00750, AID_ROOT, AID_SHELL, 0, "init*" },
  200. { 00755, AID_ROOT, AID_SHELL, 0, "product/bin/*" },
  201. { 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
  202. { 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
  203. { 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
  204. { 00755, AID_ROOT, AID_SHELL, 0, "system/apex/*/bin/*" },
  205. { 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
  206. { 00755, AID_ROOT, AID_SHELL, 0, "vendor/xbin/*" },
  207. { 00644, AID_ROOT, AID_ROOT, 0, 0 },
  208. // clang-format on
  209. };
  210. #ifndef __ANDROID_VNDK__
  211. auto __for_testing_only__android_files = android_files;
  212. #endif
  213. static size_t strip(const char* path, size_t len, const char suffix[]) {
  214. if (len < strlen(suffix)) return len;
  215. if (strncmp(path + len - strlen(suffix), suffix, strlen(suffix))) return len;
  216. return len - strlen(suffix);
  217. }
  218. static int fs_config_open(int dir, int which, const char* target_out_path) {
  219. int fd = -1;
  220. if (target_out_path && *target_out_path) {
  221. // target_out_path is the path to the directory holding content of
  222. // system partition but as we cannot guarantee it ends with '/system'
  223. // or with or without a trailing slash, need to strip them carefully.
  224. char* name = NULL;
  225. size_t len = strlen(target_out_path);
  226. len = strip(target_out_path, len, "/");
  227. len = strip(target_out_path, len, "/system");
  228. if (asprintf(&name, "%.*s%s", (int)len, target_out_path, conf[which][dir]) != -1) {
  229. fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_BINARY));
  230. free(name);
  231. }
  232. }
  233. if (fd < 0) {
  234. fd = TEMP_FAILURE_RETRY(open(conf[which][dir], O_RDONLY | O_BINARY));
  235. }
  236. return fd;
  237. }
  238. // if path is "odm/<stuff>", "oem/<stuff>", "product/<stuff>",
  239. // "product_services/<stuff>" or "vendor/<stuff>"
  240. static bool is_partition(const std::string& path) {
  241. static const char* partitions[] = {"odm/", "oem/", "product/", "product_services/", "vendor/"};
  242. for (size_t i = 0; i < (sizeof(partitions) / sizeof(partitions[0])); ++i) {
  243. if (StartsWith(path, partitions[i])) return true;
  244. }
  245. return false;
  246. }
  247. // alias prefixes of "<partition>/<stuff>" to "system/<partition>/<stuff>" or
  248. // "system/<partition>/<stuff>" to "<partition>/<stuff>"
  249. static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
  250. std::string pattern(prefix, len);
  251. std::string input(path, plen);
  252. // Massage pattern and input so that they can be used by fnmatch where
  253. // directories have to end with /.
  254. if (dir) {
  255. if (!EndsWith(input, "/")) {
  256. input.append("/");
  257. }
  258. if (!EndsWith(pattern, "/*")) {
  259. if (EndsWith(pattern, "/")) {
  260. pattern.append("*");
  261. } else {
  262. pattern.append("/*");
  263. }
  264. }
  265. }
  266. // no FNM_PATHNAME is set in order to match a/b/c/d with a/*
  267. // FNM_ESCAPE is set in order to prevent using \\? and \\* and maintenance issues.
  268. const int fnm_flags = FNM_NOESCAPE;
  269. if (fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0) return true;
  270. static constexpr const char* kSystem = "system/";
  271. if (StartsWith(input, kSystem)) {
  272. input.erase(0, strlen(kSystem));
  273. } else if (input.size() <= strlen(kSystem)) {
  274. return false;
  275. } else if (StartsWith(pattern, kSystem)) {
  276. pattern.erase(0, strlen(kSystem));
  277. } else {
  278. return false;
  279. }
  280. if (!is_partition(pattern)) return false;
  281. if (!is_partition(input)) return false;
  282. return fnmatch(pattern.c_str(), input.c_str(), fnm_flags) == 0;
  283. }
  284. #ifndef __ANDROID_VNDK__
  285. auto __for_testing_only__fs_config_cmp = fs_config_cmp;
  286. #endif
  287. void 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. if (path[0] == '/') {
  292. path++;
  293. }
  294. plen = strlen(path);
  295. for (which = 0; which < (sizeof(conf) / sizeof(conf[0])); ++which) {
  296. struct fs_path_config_from_file header;
  297. int fd = fs_config_open(dir, which, target_out_path);
  298. if (fd < 0) continue;
  299. while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
  300. char* prefix;
  301. uint16_t host_len = get2LE((const uint8_t*)&header.len);
  302. ssize_t len, remainder = host_len - sizeof(header);
  303. if (remainder <= 0) {
  304. ALOGE("%s len is corrupted", conf[which][dir]);
  305. break;
  306. }
  307. prefix = static_cast<char*>(calloc(1, remainder));
  308. if (!prefix) {
  309. ALOGE("%s out of memory", conf[which][dir]);
  310. break;
  311. }
  312. if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
  313. free(prefix);
  314. ALOGE("%s prefix is truncated", conf[which][dir]);
  315. break;
  316. }
  317. len = strnlen(prefix, remainder);
  318. if (len >= remainder) { // missing a terminating null
  319. free(prefix);
  320. ALOGE("%s is corrupted", conf[which][dir]);
  321. break;
  322. }
  323. if (fs_config_cmp(dir, prefix, len, path, plen)) {
  324. free(prefix);
  325. close(fd);
  326. *uid = get2LE((const uint8_t*)&(header.uid));
  327. *gid = get2LE((const uint8_t*)&(header.gid));
  328. *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
  329. *capabilities = get8LE((const uint8_t*)&(header.capabilities));
  330. return;
  331. }
  332. free(prefix);
  333. }
  334. close(fd);
  335. }
  336. for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
  337. if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
  338. break;
  339. }
  340. }
  341. *uid = pc->uid;
  342. *gid = pc->gid;
  343. *mode = (*mode & (~07777)) | pc->mode;
  344. *capabilities = pc->capabilities;
  345. }
  346. ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
  347. struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
  348. size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
  349. if ((length < len) || (len > UINT16_MAX)) {
  350. return -ENOSPC;
  351. }
  352. memset(p, 0, len);
  353. uint16_t host_len = len;
  354. p->len = get2LE((const uint8_t*)&host_len);
  355. p->mode = get2LE((const uint8_t*)&(pc->mode));
  356. p->uid = get2LE((const uint8_t*)&(pc->uid));
  357. p->gid = get2LE((const uint8_t*)&(pc->gid));
  358. p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
  359. strcpy(p->prefix, pc->prefix);
  360. return len;
  361. }