1 /* 2 * Copyright (C) 2015 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 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <string.h> 25 #include <sys/stat.h> 26 27 #include <selinux/label.h> 28 #include <selinux/selinux.h> 29 30 #include "android.h" 31 #include "private/android_filesystem_config.h" 32 #include "private/canned_fs_config.h" 33 34 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 35 36 void alloc_mounted_path(const char *mount_point, const char *subpath, char **mounted_path) { 37 *mounted_path = malloc(strlen(mount_point) + strlen(subpath) + 1); 38 if (*mounted_path == NULL) { 39 perror("Malloc Failure."); 40 exit(EXIT_FAILURE); 41 } 42 strcpy(*mounted_path, mount_point); 43 strcat(*mounted_path, subpath); 44 } 45 46 void android_fs_config(fs_config_func_t fs_config_func, const char *path, struct stat *stat, 47 const char *target_out_path, uint64_t *capabilities) { 48 // filesystem_config does not preserve file type bits 49 mode_t stat_file_type_mask = stat->st_mode & S_IFMT; 50 unsigned int uid = 0, gid = 0, mode = 0; 51 if (fs_config_func) { 52 fs_config_func(path, S_ISDIR(stat->st_mode), target_out_path, 53 &uid, &gid, &mode, capabilities); 54 stat->st_uid = uid; 55 stat->st_gid = gid; 56 stat->st_mode = mode; 57 } 58 stat->st_mode |= stat_file_type_mask; 59 } 60 61 62 struct selabel_handle *get_sehnd(const char *context_file) { 63 struct selinux_opt seopts[] = { 64 { 65 .type = SELABEL_OPT_PATH, 66 .value = context_file 67 } 68 }; 69 struct selabel_handle *sehnd = 70 selabel_open(SELABEL_CTX_FILE, seopts, ARRAY_SIZE(seopts)); 71 72 if (!sehnd) { 73 perror("Error running selabel_open."); 74 exit(EXIT_FAILURE); 75 } 76 return sehnd; 77 } 78 79 80 char *set_selabel(const char *path, unsigned int mode, struct selabel_handle *sehnd) { 81 char *secontext; 82 if (sehnd != NULL) { 83 int full_name_size = strlen(path) + 2; 84 char* full_name = (char*) malloc(full_name_size); 85 if (full_name == NULL) { 86 perror("Malloc Failure."); 87 exit(EXIT_FAILURE); 88 } 89 90 full_name[0] = '/'; 91 strncpy(full_name + 1, path, full_name_size - 1); 92 93 if (selabel_lookup(sehnd, &secontext, full_name, mode)) { 94 secontext = strdup("u:object_r:unlabeled:s0"); 95 } 96 97 free(full_name); 98 return secontext; 99 } 100 perror("Selabel handle is NULL."); 101 exit(EXIT_FAILURE); 102 } 103 104 struct vfs_cap_data set_caps(uint64_t capabilities) { 105 struct vfs_cap_data cap_data; 106 memset(&cap_data, 0, sizeof(cap_data)); 107 108 if (capabilities == 0) 109 return cap_data; 110 111 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; 112 cap_data.data[0].permitted = (uint32_t) capabilities; 113 cap_data.data[0].inheritable = 0; 114 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32); 115 cap_data.data[1].inheritable = 0; 116 117 return cap_data; 118 } 119