Android 打包镜像对权限的管理
程序员文章站
2022-11-01 16:54:21
Android ext4镜像制作的主要逻辑都在system/extras/ext4_utils/make_ext4fs.c文件中,其实就是根据某个文件夹创建ext4格式的二进制文件。 ext4包含superblock,inode, inode table, group descriptors, block bitmap 和block等主要的结构。文件的权限信息一般保存在inode中。正常情况下打包文件的权限和目标目录下的权限应该是相同的static u32 build_directory_structu...
Android ext4镜像制作的主要逻辑都在system/extras/ext4_utils/make_ext4fs.c文件中,其实就是根据某个文件夹创建ext4格式的二进制文件。 ext4包含superblock,inode, inode table, group descriptors, block bitmap 和block等主要的结构。
文件的权限信息一般保存在inode中。正常情况下打包文件的权限和目标目录下的权限应该是相同的
static u32 build_directory_structure(const char *full_path, const char *dir_path, const char *target_out_path,
u32 dir_inode, fs_config_func_t fs_config_func,
struct selabel_handle *sehnd, int verbose, time_t fixed_time)
{
......
entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
......
for (i = 0; i < entries; i++) {
dentries[i].filename = strdup(namelist[i]->d_name);
ret = lstat(dentries[i].full_path, &stat); // 获取原来文件stat信息
if (ret < 0) {
error_errno("lstat");
i--;
entries--;
continue;
}
dentries[i].size = stat.st_size;
dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO); // 保留原来权限信息
if (fixed_time == -1) { // 跟新mtime
dentries[i].mtime = stat.st_mtime;
} else {
dentries[i].mtime = fixed_time;
}
uint64_t capabilities;
if (fs_config_func != NULL) { // 如果是Android系统还需要使用fs_config_func来设置文件权限和属主属组
#ifdef ANDROID
unsigned int mode = 0;
unsigned int uid = 0;
unsigned int gid = 0;
int dir = S_ISDIR(stat.st_mode);
fs_config_func(dentries[i].path, dir, target_out_path, &uid, &gid, &mode, &capabilities);
dentries[i].mode = mode;
dentries[i].uid = uid;
dentries[i].gid = gid;
dentries[i].capabilities = capabilities;
#else
error("can't set android permissions - built without android support");
#endif
}
代码首先使用lstat函数获取文件的属性信息, 属性保留 (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO)。
如果是android系统,则使用fs_config_func修正属主数组,和权限信息以及capabilities。
fs_config_func一般为system/core/libcutils/fs_config.c 中fs_config方法
void fs_config(const char *path, int dir, const char *target_out_path,
unsigned *uid, unsigned *gid, unsigned *mode, uint64_t *capabilities)
{
const struct fs_path_config *pc;
int fd, plen;
if (path[0] == '/') {
path++;
}
plen = strlen(path);
fd = fs_config_open(dir, target_out_path);
if (fd >= 0) { // 检查配置文件是否有设置该目录或者文件属性
struct fs_path_config_from_file header;
while (TEMP_FAILURE_RETRY(read(fd, &header, sizeof(header))) == sizeof(header)) {
char *prefix;
uint16_t host_len = get2LE((const uint8_t *)&header.len);
ssize_t len, remainder = host_len - sizeof(header);
if (remainder <= 0) {
ALOGE("%s len is corrupted", dir ? conf_dir : conf_file);
break;
}
prefix = calloc(1, remainder);
if (!prefix) {
ALOGE("%s out of memory", dir ? conf_dir : conf_file);
break;
}
if (TEMP_FAILURE_RETRY(read(fd, prefix, remainder)) != remainder) {
free(prefix);
ALOGE("%s prefix is truncated", dir ? conf_dir : conf_file);
break;
}
len = strnlen(prefix, remainder);
if (len >= remainder) { /* missing a terminating null */
free(prefix);
ALOGE("%s is corrupted", dir ? conf_dir : conf_file);
break;
}
if (fs_config_cmp(dir, prefix, len, path, plen)) {
free(prefix);
close(fd);
*uid = get2LE((const uint8_t *)&(header.uid));
*gid = get2LE((const uint8_t *)&(header.gid));
*mode = (*mode & (~07777)) | get2LE((const uint8_t *)&(header.mode));
*capabilities = get8LE((const uint8_t *)&(header.capabilities));
return;
}
free(prefix);
}
close(fd);
}
// 使用android_dirs或者 android_files数组检查配置属性
pc = dir ? android_dirs : android_files;
for(; pc->prefix; pc++){
if (fs_config_cmp(dir, pc->prefix, strlen(pc->prefix), path, plen)) {
break;
}
}
*uid = pc->uid;
*gid = pc->gid;
*mode = (*mode & (~07777)) | pc->mode;
*capabilities = pc->capabilities;
}
函数先检查配置文件中有没有配置目录属性, 再检查android_dirs或者android_files中有没有设置配置属性,最终使用配置属性来更新目录的属性(uid,gid,mode,capabilities)。
配置属性信息如下:
static const struct fs_path_config android_dirs[] = {
{ 00770, AID_SYSTEM, AID_CACHE, 0, "cache" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private" },
{ 00771, AID_ROOT, AID_ROOT, 0, "data/dalvik-cache" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data/data" },
{ 00771, AID_SHELL, AID_SHELL, 0, "data/local/tmp" },
{ 00771, AID_SHELL, AID_SHELL, 0, "data/local" },
{ 01771, AID_SYSTEM, AID_MISC, 0, "data/misc" },
{ 00770, AID_DHCP, AID_DHCP, 0, "data/misc/dhcp" },
{ 00771, AID_SHARED_RELRO, AID_SHARED_RELRO, 0, "data/misc/shared_relro" },
{ 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media" },
{ 00775, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/Music" },
{ 00771, AID_SYSTEM, AID_SYSTEM, 0, "data" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/vendor" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/xbin" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/etc/ppp" },
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor" },
{ 00777, AID_ROOT, AID_ROOT, 0, "sdcard" },
{ 00755, AID_ROOT, AID_ROOT, 0, 0 },
};
static const struct fs_path_config android_files[] = {
{ 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.ril" },
{ 00550, AID_DHCP, AID_SHELL, 0, "system/etc/dhcpcd/dhcpcd-run-hooks" },
{ 00555, AID_ROOT, AID_ROOT, 0, "system/etc/ppp/*" },
{ 00555, AID_ROOT, AID_ROOT, 0, "system/etc/rc.*" },
{ 00444, AID_ROOT, AID_ROOT, 0, conf_dir + 1 },
{ 00444, AID_ROOT, AID_ROOT, 0, conf_file + 1 },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app/*" },
{ 00644, AID_MEDIA_RW, AID_MEDIA_RW, 0, "data/media/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, 0, "data/app-private/*" },
{ 00644, AID_APP, AID_APP, 0, "data/data/*" },
/* the following five files are INTENTIONALLY set-uid, but they
* are NOT included on user builds. */
{ 06755, AID_ROOT, AID_SHELL, 0, "system/xbin/su" },
{ 07777, AID_ROOT, AID_SHELL, 0, "system/xbin/deamonsu" },
{ 06755, AID_ROOT, AID_SHELL, 0, "system/xbin/supolicy" },
{ 06755, AID_ROOT, AID_SHELL, 0, "system/xbin/dropbear" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/librank" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procrank" },
{ 06755, AID_ROOT, AID_ROOT, 0, "system/xbin/procmem" },
{ 04770, AID_ROOT, AID_RADIO, 0, "system/bin/pppd-ril" },
/* the following files have enhanced capabilities and ARE included in user builds. */
{ 00750, AID_ROOT, AID_SHELL, (1ULL << CAP_SETUID) | (1ULL << CAP_SETGID), "system/bin/run-as" },
{ 00700, AID_SYSTEM, AID_SHELL, (1ULL << CAP_BLOCK_SUSPEND), "system/bin/inputflinger" },
{ 00750, AID_ROOT, AID_ROOT, 0, "system/bin/uncrypt" },
{ 00750, AID_ROOT, AID_ROOT, 0, "system/bin/install-recovery.sh" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/bin/*" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/lib/valgrind/*" },
{ 00755, AID_ROOT, AID_ROOT, 0, "system/lib64/valgrind/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/xbin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "system/vendor/bin/*" },
{ 00755, AID_ROOT, AID_SHELL, 0, "vendor/bin/*" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/*" },
{ 00755, AID_ROOT, AID_ROOT, 0, "bin/*" },
{ 00750, AID_ROOT, AID_SHELL, 0, "init*" },
{ 00750, AID_ROOT, AID_SHELL, 0, "sbin/fs_mgr" },
{ 00640, AID_ROOT, AID_SHELL, 0, "fstab.*" },
{ 00644, AID_ROOT, AID_ROOT, 0, 0 },
};
本文地址:https://blog.csdn.net/woai110120130/article/details/112549228
上一篇: 首页下雪效果