• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <getopt.h>
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <sepol/module.h>
8 #include <sepol/policydb/policydb.h>
9 #include <sepol/sepol.h>
10 #include <selinux/selinux.h>
11 #include <selinux/label.h>
12 #include <sys/stat.h>
13 #include <sys/types.h>
14 
15 static const char * const CHECK_FC_ASSERT_ATTRS[] = { "fs_type", "dev_type", "file_type", NULL };
16 static const char * const CHECK_PC_ASSERT_ATTRS[] = { "property_type", NULL };
17 static const char * const CHECK_SC_ASSERT_ATTRS[] = { "service_manager_type", NULL };
18 static const char * const CHECK_HW_SC_ASSERT_ATTRS[] = { "hwservice_manager_type", NULL };
19 static const char * const CHECK_VND_SC_ASSERT_ATTRS[] = { "vndservice_manager_type", NULL };
20 
21 typedef enum filemode filemode;
22 enum filemode {
23     filemode_file_contexts = 0,
24     filemode_property_contexts,
25     filemode_service_contexts,
26     filemode_hw_service_contexts,
27     filemode_vendor_service_contexts
28 };
29 
30 static struct {
31     /* policy */
32     struct {
33         union {
34             /* Union these so we don't have to cast */
35             sepol_policydb_t *sdb;
36             policydb_t *pdb;
37         };
38         sepol_policy_file_t *pf;
39         sepol_handle_t *handle;
40         FILE *file;
41 #define SEHANDLE_CNT 2
42         struct selabel_handle *sehnd[SEHANDLE_CNT];
43     } sepolicy;
44 
45     /* assertions */
46     struct {
47         const char * const *attrs; /* for the original set to print on error */
48         ebitmap_t set;             /* the ebitmap representation of the attrs */
49     } assert;
50 
51 } global_state;
52 
filemode_to_assert_attrs(filemode mode)53 static const char * const *filemode_to_assert_attrs(filemode mode)
54 {
55     switch (mode) {
56     case filemode_file_contexts:
57         return CHECK_FC_ASSERT_ATTRS;
58     case filemode_property_contexts:
59         return CHECK_PC_ASSERT_ATTRS;
60     case filemode_service_contexts:
61         return CHECK_SC_ASSERT_ATTRS;
62     case filemode_hw_service_contexts:
63         return CHECK_HW_SC_ASSERT_ATTRS;
64     case filemode_vendor_service_contexts:
65         return CHECK_VND_SC_ASSERT_ATTRS;
66     }
67     /* die on invalid parameters */
68     fprintf(stderr, "Error: Invalid mode of operation: %d\n", mode);
69     exit(1);
70 }
71 
get_attr_bit(policydb_t * policydb,const char * attr_name)72 static int get_attr_bit(policydb_t *policydb, const char *attr_name)
73 {
74     struct type_datum *attr = hashtab_search(policydb->p_types.table, (char *)attr_name);
75     if (!attr) {
76         fprintf(stderr, "Error: \"%s\" is not defined in this policy.\n", attr_name);
77         return -1;
78     }
79 
80     if (attr->flavor != TYPE_ATTRIB) {
81         fprintf(stderr, "Error: \"%s\" is not an attribute in this policy.\n", attr_name);
82         return -1;
83     }
84 
85     return attr->s.value - 1;
86 }
87 
ebitmap_attribute_assertion_init(ebitmap_t * assertions,const char * const attributes[])88 static bool ebitmap_attribute_assertion_init(ebitmap_t *assertions, const char * const attributes[])
89 {
90 
91     while (*attributes) {
92 
93         int bit_pos = get_attr_bit(global_state.sepolicy.pdb, *attributes);
94         if (bit_pos < 0) {
95             /* get_attr_bit() logs error */
96             return false;
97         }
98 
99         int err = ebitmap_set_bit(assertions, bit_pos, 1);
100         if (err) {
101             fprintf(stderr, "Error: setting bit on assertion ebitmap!\n");
102             return false;
103         }
104         attributes++;
105     }
106     return true;
107 }
108 
is_type_of_attribute_set(policydb_t * policydb,const char * type_name,ebitmap_t * attr_set)109 static bool is_type_of_attribute_set(policydb_t *policydb, const char *type_name,
110         ebitmap_t *attr_set)
111 {
112     struct type_datum *type = hashtab_search(policydb->p_types.table, (char *)type_name);
113     if (!type) {
114         fprintf(stderr, "Error: \"%s\" is not defined in this policy.\n", type_name);
115         return false;
116     }
117 
118     if (type->flavor != TYPE_TYPE) {
119         fprintf(stderr, "Error: \"%s\" is not a type in this policy.\n", type_name);
120         return false;
121     }
122 
123     ebitmap_t dst;
124     ebitmap_init(&dst);
125 
126     /* Take the intersection, if the set is empty, then its a failure */
127     int rc = ebitmap_and(&dst, attr_set, &policydb->type_attr_map[type->s.value - 1]);
128     if (rc) {
129         fprintf(stderr, "Error: Could not perform ebitmap_and: %d\n", rc);
130         exit(1);
131     }
132 
133     bool res = (bool)ebitmap_length(&dst);
134 
135     ebitmap_destroy(&dst);
136     return res;
137 }
138 
dump_char_array(FILE * stream,const char * const * strings)139 static void dump_char_array(FILE *stream, const char * const *strings)
140 {
141 
142     const char * const *p = strings;
143 
144     fprintf(stream, "\"");
145 
146     while (*p) {
147         const char *s = *p++;
148         const char *fmt = *p ? "%s, " : "%s\"";
149         fprintf(stream, fmt, s);
150     }
151 }
152 
validate(char ** contextp)153 static int validate(char **contextp)
154 {
155     bool res;
156     char *context = *contextp;
157 
158     sepol_context_t *ctx;
159     int rc = sepol_context_from_string(global_state.sepolicy.handle, context,
160             &ctx);
161     if (rc < 0) {
162         fprintf(stderr, "Error: Could not allocate context from string");
163         exit(1);
164     }
165 
166     rc = sepol_context_check(global_state.sepolicy.handle,
167             global_state.sepolicy.sdb, ctx);
168     if (rc < 0) {
169         goto out;
170     }
171 
172     const char *type_name = sepol_context_get_type(ctx);
173 
174     // Temporarily exempt hal_power_stats_vendor_service from the check.
175     // TODO(b/211953546): remove this
176     if (strcmp(type_name, "hal_power_stats_vendor_service") == 0) {
177         goto out;
178     }
179 
180     uint32_t len = ebitmap_length(&global_state.assert.set);
181     if (len > 0) {
182         res = !is_type_of_attribute_set(global_state.sepolicy.pdb, type_name,
183                 &global_state.assert.set);
184         if (res) {
185             fprintf(stderr, "Error: type \"%s\" is not of set: ", type_name);
186             dump_char_array(stderr, global_state.assert.attrs);
187             fprintf(stderr, "\n");
188             /* The calls above did not affect rc, so set error before going to out */
189             rc = -1;
190             goto out;
191         }
192     }
193     /* Success: Although it should be 0, we explicitly set rc to 0 for clarity */
194     rc = 0;
195 
196  out:
197     sepol_context_free(ctx);
198     return rc;
199 }
200 
usage(char * name)201 static void usage(char *name) {
202     fprintf(stderr, "usage1:  %s [-l|-p|-s|-v] [-e] sepolicy context_file\n\n"
203         "Parses a context file and checks for syntax errors.\n"
204         "If -p is specified, the property backend is used.\n"
205         "If -s is specified, the service backend is used to verify binder services.\n"
206         "If -l is specified, the service backend is used to verify hwbinder services.\n"
207         "If -v is specified, the service backend is used to verify vndbinder services.\n"
208         "Otherwise, context_file is assumed to be a file_contexts file\n"
209         "If -e is specified, then the context_file is allowed to be empty.\n\n"
210 
211         "usage2:  %s -c file_contexts1 file_contexts2\n\n"
212         "Compares two file contexts files and reports one of subset, equal, superset, or incomparable.\n\n",
213         name, name);
214     exit(1);
215 }
216 
cleanup(void)217 static void cleanup(void) {
218 
219     if (global_state.sepolicy.file) {
220         fclose(global_state.sepolicy.file);
221     }
222 
223     if (global_state.sepolicy.sdb) {
224         sepol_policydb_free(global_state.sepolicy.sdb);
225     }
226 
227     if (global_state.sepolicy.pf) {
228         sepol_policy_file_free(global_state.sepolicy.pf);
229     }
230 
231     if (global_state.sepolicy.handle) {
232         sepol_handle_destroy(global_state.sepolicy.handle);
233     }
234 
235     ebitmap_destroy(&global_state.assert.set);
236 
237     int i;
238     for (i = 0; i < SEHANDLE_CNT; i++) {
239         struct selabel_handle *sehnd = global_state.sepolicy.sehnd[i];
240         if (sehnd) {
241             selabel_close(sehnd);
242         }
243     }
244 }
245 
do_compare_and_die_on_error(struct selinux_opt opts[],unsigned int backend,char * paths[])246 static void do_compare_and_die_on_error(struct selinux_opt opts[], unsigned int backend, char *paths[])
247 {
248     enum selabel_cmp_result result;
249      char *result_str[] = { "subset", "equal", "superset", "incomparable" };
250      int i;
251 
252      opts[0].value = NULL; /* not validating against a policy when comparing */
253 
254      for (i = 0; i < SEHANDLE_CNT; i++) {
255          opts[1].value = paths[i];
256          global_state.sepolicy.sehnd[i] = selabel_open(backend, opts, 2);
257          if (!global_state.sepolicy.sehnd[i]) {
258              fprintf(stderr, "Error: could not load context file from %s\n", paths[i]);
259              exit(1);
260          }
261      }
262 
263      result = selabel_cmp(global_state.sepolicy.sehnd[0], global_state.sepolicy.sehnd[1]);
264      printf("%s\n", result_str[result]);
265 }
266 
do_fc_check_and_die_on_error(struct selinux_opt opts[],unsigned int backend,filemode mode,const char * sepolicy_file,const char * context_file,bool allow_empty)267 static void do_fc_check_and_die_on_error(struct selinux_opt opts[], unsigned int backend, filemode mode,
268         const char *sepolicy_file, const char *context_file, bool allow_empty)
269 {
270     struct stat sb;
271     if (stat(context_file, &sb) < 0) {
272         perror("Error: could not get stat on file contexts file");
273         exit(1);
274     }
275 
276     if (sb.st_size == 0) {
277         /* Nothing to check on empty file_contexts file if allowed*/
278         if (allow_empty) {
279             return;
280         }
281         /* else: We could throw the error here, but libselinux backend will catch it */
282     }
283 
284     global_state.sepolicy.file = fopen(sepolicy_file, "r");
285     if (!global_state.sepolicy.file) {
286       perror("Error: could not open policy file");
287       exit(1);
288     }
289 
290     global_state.sepolicy.handle = sepol_handle_create();
291     if (!global_state.sepolicy.handle) {
292         fprintf(stderr, "Error: could not create policy handle: %s\n", strerror(errno));
293         exit(1);
294     }
295 
296     if (sepol_policy_file_create(&global_state.sepolicy.pf) < 0) {
297       perror("Error: could not create policy handle");
298       exit(1);
299     }
300 
301     sepol_policy_file_set_fp(global_state.sepolicy.pf, global_state.sepolicy.file);
302     sepol_policy_file_set_handle(global_state.sepolicy.pf, global_state.sepolicy.handle);
303 
304     int rc = sepol_policydb_create(&global_state.sepolicy.sdb);
305     if (rc < 0) {
306       perror("Error: could not create policy db");
307       exit(1);
308     }
309 
310     rc = sepol_policydb_read(global_state.sepolicy.sdb, global_state.sepolicy.pf);
311     if (rc < 0) {
312       perror("Error: could not read file into policy db");
313       exit(1);
314     }
315 
316     global_state.assert.attrs = filemode_to_assert_attrs(mode);
317 
318     bool ret = ebitmap_attribute_assertion_init(&global_state.assert.set, global_state.assert.attrs);
319     if (!ret) {
320         /* error messages logged by ebitmap_attribute_assertion_init() */
321         exit(1);
322     }
323 
324     selinux_set_callback(SELINUX_CB_VALIDATE,
325                          (union selinux_callback)&validate);
326 
327     opts[1].value = context_file;
328 
329     global_state.sepolicy.sehnd[0] = selabel_open(backend, opts, 2);
330     if (!global_state.sepolicy.sehnd[0]) {
331       fprintf(stderr, "Error: could not load context file from %s\n", context_file);
332       exit(1);
333     }
334 }
335 
main(int argc,char ** argv)336 int main(int argc, char **argv)
337 {
338   struct selinux_opt opts[] = {
339     { SELABEL_OPT_VALIDATE, (void*)1 },
340     { SELABEL_OPT_PATH, NULL }
341   };
342 
343   // Default backend unless changed by input argument.
344   unsigned int backend = SELABEL_CTX_FILE;
345 
346   bool allow_empty = false;
347   bool compare = false;
348   char c;
349 
350   filemode mode = filemode_file_contexts;
351 
352   while ((c = getopt(argc, argv, "clpsve")) != -1) {
353     switch (c) {
354       case 'c':
355         compare = true;
356         break;
357       case 'e':
358         allow_empty = true;
359         break;
360       case 'p':
361         mode = filemode_property_contexts;
362         backend = SELABEL_CTX_ANDROID_PROP;
363         break;
364       case 's':
365         mode = filemode_service_contexts;
366         backend = SELABEL_CTX_ANDROID_SERVICE;
367         break;
368       case 'l':
369         mode = filemode_hw_service_contexts;
370         backend = SELABEL_CTX_ANDROID_SERVICE;
371         break;
372       case 'v':
373         mode = filemode_vendor_service_contexts;
374         backend = SELABEL_CTX_ANDROID_SERVICE;
375         break;
376       case 'h':
377       default:
378         usage(argv[0]);
379         break;
380     }
381   }
382 
383   int index = optind;
384   if (argc - optind != 2) {
385     usage(argv[0]);
386   }
387 
388   if (compare && backend != SELABEL_CTX_FILE) {
389     usage(argv[0]);
390   }
391 
392   atexit(cleanup);
393 
394   if (compare) {
395       do_compare_and_die_on_error(opts, backend, &(argv[index]));
396   } else {
397       /* remaining args are sepolicy file and context file  */
398       char *sepolicy_file = argv[index];
399       char *context_file = argv[index + 1];
400 
401       do_fc_check_and_die_on_error(opts, backend, mode, sepolicy_file, context_file, allow_empty);
402   }
403   exit(0);
404 }
405