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