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/android.h> 28 #include <selinux/label.h> 29 #include <selinux/selinux.h> 30 31 #include "android.h" 32 #include "private/android_filesystem_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(const char *path, struct stat *stat, const char *target_out_path) { 47 unsigned long capabilities = 0; 48 fs_config(path, S_ISDIR(stat->st_mode), target_out_path, 49 &stat->st_uid, &stat->st_gid, &stat->st_mode, &capabilities); 50 } 51 52 53 struct selabel_handle *get_sehnd(const char *context_file) { 54 struct selinux_opt seopts[] = { 55 { 56 .type = SELABEL_OPT_PATH, 57 .value = context_file 58 } 59 }; 60 struct selabel_handle *sehnd = 61 selabel_open(SELABEL_CTX_FILE, seopts, ARRAY_SIZE(seopts)); 62 63 if (!sehnd) { 64 perror("Error running selabel_open."); 65 exit(EXIT_FAILURE); 66 } 67 return sehnd; 68 } 69 70 71 char *set_selabel(const char *path, unsigned int mode, struct selabel_handle *sehnd) { 72 char *secontext; 73 if (sehnd != NULL) { 74 int full_name_size = strlen(path) + 2; 75 char* full_name = (char*) malloc(full_name_size); 76 if (full_name == NULL) { 77 perror("Malloc Failure."); 78 exit(EXIT_FAILURE); 79 } 80 81 full_name[0] = '/'; 82 strncpy(full_name + 1, path, full_name_size - 1); 83 84 if (selabel_lookup(sehnd, &secontext, full_name, mode)) { 85 secontext = strdup("u:object_r:unlabeled:s0"); 86 } 87 88 free(full_name); 89 return secontext; 90 } 91 perror("Selabel handle is NULL."); 92 exit(EXIT_FAILURE); 93 } 94