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 if (fs_config_func) 51 fs_config_func(path, S_ISDIR(stat->st_mode), target_out_path, 52 &stat->st_uid, &stat->st_gid, &stat->st_mode, capabilities); 53 stat->st_mode |= stat_file_type_mask; 54 } 55 56 57 struct selabel_handle *get_sehnd(const char *context_file) { 58 struct selinux_opt seopts[] = { 59 { 60 .type = SELABEL_OPT_PATH, 61 .value = context_file 62 } 63 }; 64 struct selabel_handle *sehnd = 65 selabel_open(SELABEL_CTX_FILE, seopts, ARRAY_SIZE(seopts)); 66 67 if (!sehnd) { 68 perror("Error running selabel_open."); 69 exit(EXIT_FAILURE); 70 } 71 return sehnd; 72 } 73 74 75 char *set_selabel(const char *path, unsigned int mode, struct selabel_handle *sehnd) { 76 char *secontext; 77 if (sehnd != NULL) { 78 int full_name_size = strlen(path) + 2; 79 char* full_name = (char*) malloc(full_name_size); 80 if (full_name == NULL) { 81 perror("Malloc Failure."); 82 exit(EXIT_FAILURE); 83 } 84 85 full_name[0] = '/'; 86 strncpy(full_name + 1, path, full_name_size - 1); 87 88 if (selabel_lookup(sehnd, &secontext, full_name, mode)) { 89 secontext = strdup("u:object_r:unlabeled:s0"); 90 } 91 92 free(full_name); 93 return secontext; 94 } 95 perror("Selabel handle is NULL."); 96 exit(EXIT_FAILURE); 97 } 98 99 struct vfs_cap_data set_caps(uint64_t capabilities) { 100 struct vfs_cap_data cap_data; 101 memset(&cap_data, 0, sizeof(cap_data)); 102 103 if (capabilities == 0) 104 return cap_data; 105 106 cap_data.magic_etc = VFS_CAP_REVISION | VFS_CAP_FLAGS_EFFECTIVE; 107 cap_data.data[0].permitted = (uint32_t) capabilities; 108 cap_data.data[0].inheritable = 0; 109 cap_data.data[1].permitted = (uint32_t) (capabilities >> 32); 110 cap_data.data[1].inheritable = 0; 111 112 return cap_data; 113 } 114