Merge "libcutils: fs_config.c code compliance"

This commit is contained in:
Mark Salyzyn 2017-03-21 14:48:31 +00:00 committed by Gerrit Code Review
commit b501400a76

View file

@ -52,21 +52,17 @@ struct fs_path_config_from_file {
} __attribute__((__aligned__(sizeof(uint64_t)))); } __attribute__((__aligned__(sizeof(uint64_t))));
/* My kingdom for <endian.h> */ /* My kingdom for <endian.h> */
static inline uint16_t get2LE(const uint8_t* src) static inline uint16_t get2LE(const uint8_t* src) { return src[0] | (src[1] << 8); }
{
return src[0] | (src[1] << 8);
}
static inline uint64_t get8LE(const uint8_t* src) static inline uint64_t get8LE(const uint8_t* src) {
{
uint32_t low, high; uint32_t low, high;
low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24); low = src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24); high = src[4] | (src[5] << 8) | (src[6] << 16) | (src[7] << 24);
return ((uint64_t) high << 32) | (uint64_t) low; return ((uint64_t)high << 32) | (uint64_t)low;
} }
#define ALIGN(x, alignment) ( ((x) + ((alignment) - 1)) & ~((alignment) - 1) ) #define ALIGN(x, alignment) (((x) + ((alignment)-1)) & ~((alignment)-1))
/* Rules for directories. /* Rules for directories.
** These rules are applied based on "first match", so they ** These rules are applied based on "first match", so they
@ -75,6 +71,7 @@ static inline uint64_t get8LE(const uint8_t* src)
*/ */
static const struct fs_path_config android_dirs[] = { static const struct fs_path_config android_dirs[] = {
/* clang-format off */
{ 00770, AID_SYSTEM, AID_CACHE, 0, "cache" }, { 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
{ 00500, AID_ROOT, AID_ROOT, 0, "config" }, { 00500, AID_ROOT, AID_ROOT, 0, "config" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" }, { 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
@ -104,6 +101,7 @@ static const struct fs_path_config android_dirs[] = {
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor" }, { 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
{ 00777, AID_ROOT, AID_ROOT, 0, "sdcard" }, { 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
{ 00755, AID_ROOT, AID_ROOT, 0, 0 }, { 00755, AID_ROOT, AID_ROOT, 0, 0 },
/* clang-format on */
}; };
/* Rules for files. /* Rules for files.
@ -116,6 +114,7 @@ static const char conf_dir[] = "/system/etc/fs_config_dirs";
static const char conf_file[] = "/system/etc/fs_config_files"; static const char conf_file[] = "/system/etc/fs_config_files";
static const struct fs_path_config android_files[] = { static const struct fs_path_config android_files[] = {
/* clang-format off */
{ 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" }, { 00440, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.rc" },
{ 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" }, { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.goldfish.sh" },
{ 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" }, { 00550, AID_ROOT, AID_SHELL, 0, "system/etc/init.ril" },
@ -139,7 +138,8 @@ static const struct fs_path_config android_files[] = {
{ 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" }, { 04750, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" }, { 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
/* the following files have enhanced capabilities and ARE included in user builds. */ /* the following files have enhanced capabilities and ARE included
* in user builds. */
{ 00550, AID_LOGD, AID_LOGD, CAP_MASK_LONG(CAP_SYSLOG) | { 00550, AID_LOGD, AID_LOGD, CAP_MASK_LONG(CAP_SYSLOG) |
CAP_MASK_LONG(CAP_AUDIT_CONTROL) | CAP_MASK_LONG(CAP_AUDIT_CONTROL) |
CAP_MASK_LONG(CAP_SETGID), CAP_MASK_LONG(CAP_SETGID),
@ -167,7 +167,8 @@ static const struct fs_path_config android_files[] = {
{ 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN), { 00700, AID_BLUETOOTH, AID_BLUETOOTH, CAP_MASK_LONG(CAP_NET_ADMIN),
"vendor/bin/hw/android.hardware.bluetooth@1.0-service" }, "vendor/bin/hw/android.hardware.bluetooth@1.0-service" },
/* A non-privileged zygote that spawns isolated processes for web rendering. */ /* A non-privileged zygote that spawns
* isolated processes for web rendering. */
{ 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) | { 0750, AID_ROOT, AID_ROOT, CAP_MASK_LONG(CAP_SETUID) |
CAP_MASK_LONG(CAP_SETGID) | CAP_MASK_LONG(CAP_SETGID) |
CAP_MASK_LONG(CAP_SETPCAP), CAP_MASK_LONG(CAP_SETPCAP),
@ -203,16 +204,17 @@ static const struct fs_path_config android_files[] = {
{ 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" }, { 00600, AID_ROOT, AID_ROOT, 0, "vendor/default.prop" },
{ 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" }, { 00600, AID_ROOT, AID_ROOT, 0, "odm/default.prop" },
{ 00644, AID_ROOT, AID_ROOT, 0, 0 }, { 00644, AID_ROOT, AID_ROOT, 0, 0 },
/* clang-format on */
}; };
static int fs_config_open(int dir, const char *target_out_path) static int fs_config_open(int dir, const char* target_out_path) {
{
int fd = -1; int fd = -1;
if (target_out_path && *target_out_path) { if (target_out_path && *target_out_path) {
/* target_out_path is the path to the directory holding content of system partition /* target_out_path is the path to the directory holding content of
but as we cannot guaranty it ends with '/system' we need this below skip_len logic */ * system partition but as we cannot guaranty it ends with '/system'
char *name = NULL; * we need this below skip_len logic */
char* name = NULL;
int target_out_path_len = strlen(target_out_path); int target_out_path_len = strlen(target_out_path);
int skip_len = strlen("/system"); int skip_len = strlen("/system");
@ -230,9 +232,7 @@ static int fs_config_open(int dir, const char *target_out_path)
return fd; return fd;
} }
static bool fs_config_cmp(bool dir, const char *prefix, size_t len, static bool fs_config_cmp(bool dir, const char* prefix, size_t len, const char* path, size_t plen) {
const char *path, size_t plen)
{
if (dir) { if (dir) {
if (plen < len) { if (plen < len) {
return false; return false;
@ -249,11 +249,11 @@ static bool fs_config_cmp(bool dir, const char *prefix, size_t len,
return !strncmp(prefix, path, len); return !strncmp(prefix, path, len);
} }
void fs_config(const char *path, int dir, const char *target_out_path, void fs_config(const char* path, int dir, const char* target_out_path, unsigned* uid, unsigned* gid,
unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities) unsigned* mode, uint64_t* capabilities) {
{ const struct fs_path_config* pc;
const struct fs_path_config *pc; size_t plen;
int fd, plen; int fd;
if (path[0] == '/') { if (path[0] == '/') {
path++; path++;
@ -266,8 +266,8 @@ void fs_config(const char *path, int dir, const char *target_out_path,
struct fs_path_config_from_file header; struct fs_path_config_from_file header;
while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) { while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
char *prefix; char* prefix;
uint16_t host_len = get2LE((const uint8_t *)&header.len); uint16_t host_len = get2LE((const uint8_t*)&header.len);
ssize_t len, remainder = host_len - sizeof(header); ssize_t len, remainder = host_len - sizeof(header);
if (remainder <= 0) { if (remainder <= 0) {
ALOGE("%s len is corrupted", dir ? conf_dir : conf_file); ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
@ -292,10 +292,10 @@ void fs_config(const char *path, int dir, const char *target_out_path,
if (fs_config_cmp(dir, prefix, len, path, plen)) { if (fs_config_cmp(dir, prefix, len, path, plen)) {
free(prefix); free(prefix);
close(fd); close(fd);
*uid = get2LE((const uint8_t *)&(header.uid)); *uid = get2LE((const uint8_t*)&(header.uid));
*gid = get2LE((const uint8_t *)&(header.gid)); *gid = get2LE((const uint8_t*)&(header.gid));
*mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode)); *mode = (*mode & (~07777)) | get2LE((const uint8_t*)&(header.mode));
*capabilities = get8LE((const uint8_t *)&(header.capabilities)); *capabilities = get8LE((const uint8_t*)&(header.capabilities));
return; return;
} }
free(prefix); free(prefix);
@ -303,8 +303,7 @@ void fs_config(const char *path, int dir, const char *target_out_path,
close(fd); close(fd);
} }
pc = dir ? android_dirs : android_files; for (pc = dir ? android_dirs : android_files; pc->prefix; pc++) {
for(; pc->prefix; pc++){
if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) { if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
break; break;
} }
@ -315,9 +314,8 @@ void fs_config(const char *path, int dir, const char *target_out_path,
*capabilities = pc->capabilities; *capabilities = pc->capabilities;
} }
ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_config *pc) ssize_t fs_config_generate(char* buffer, size_t length, const struct fs_path_config* pc) {
{ struct fs_path_config_from_file* p = (struct fs_path_config_from_file*)buffer;
struct fs_path_config_from_file *p = (struct fs_path_config_from_file *)buffer;
size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t)); size_t len = ALIGN(sizeof(*p) + strlen(pc->prefix) + 1, sizeof(uint64_t));
if ((length < len) || (len > UINT16_MAX)) { if ((length < len) || (len > UINT16_MAX)) {
@ -325,11 +323,11 @@ ssize_t fs_config_generate(char *buffer, size_t length, const struct fs_path_con
} }
memset(p, 0, len); memset(p, 0, len);
uint16_t host_len = len; uint16_t host_len = len;
p->len = get2LE((const uint8_t *)&host_len); p->len = get2LE((const uint8_t*)&host_len);
p->mode = get2LE((const uint8_t *)&(pc->mode)); p->mode = get2LE((const uint8_t*)&(pc->mode));
p->uid = get2LE((const uint8_t *)&(pc->uid)); p->uid = get2LE((const uint8_t*)&(pc->uid));
p->gid = get2LE((const uint8_t *)&(pc->gid)); p->gid = get2LE((const uint8_t*)&(pc->gid));
p->capabilities = get8LE((const uint8_t *)&(pc->capabilities)); p->capabilities = get8LE((const uint8_t*)&(pc->capabilities));
strcpy(p->prefix, pc->prefix); strcpy(p->prefix, pc->prefix);
return len; return len;
} }