• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <fcntl.h>
2 #include <sepol/policydb/policydb.h>
3 #include <sepol/policydb/util.h>
4 #include <sys/mman.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 
8 #include "utils.h"
9 
10 bool USAGE_ERROR = false;
11 
display_allow(policydb_t * policydb,avtab_key_t * key,int idx,uint32_t perms)12 void display_allow(policydb_t *policydb, avtab_key_t *key, int idx, uint32_t perms)
13 {
14     printf("    allow %s %s:%s { %s };\n",
15            policydb->p_type_val_to_name[key->source_type
16                                         ? key->source_type - 1 : idx],
17            key->target_type == key->source_type ? "self" :
18            policydb->p_type_val_to_name[key->target_type
19                                         ? key->target_type - 1 : idx],
20            policydb->p_class_val_to_name[key->target_class - 1],
21            sepol_av_to_string
22            (policydb, key->target_class, perms));
23 }
24 
load_policy(char * filename,policydb_t * policydb,struct policy_file * pf)25 int load_policy(char *filename, policydb_t * policydb, struct policy_file *pf)
26 {
27     int fd;
28     struct stat sb;
29     void *map;
30     int ret;
31 
32     fd = open(filename, O_RDONLY);
33     if (fd < 0) {
34         fprintf(stderr, "Can't open '%s':  %s\n", filename, strerror(errno));
35         return 1;
36     }
37     if (fstat(fd, &sb) < 0) {
38         fprintf(stderr, "Can't stat '%s':  %s\n", filename, strerror(errno));
39         close(fd);
40         return 1;
41     }
42     map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
43     if (map == MAP_FAILED) {
44         fprintf(stderr, "Can't mmap '%s':  %s\n", filename, strerror(errno));
45         close(fd);
46         return 1;
47     }
48 
49     policy_file_init(pf);
50     pf->type = PF_USE_MEMORY;
51     pf->data = map;
52     pf->len = sb.st_size;
53     if (policydb_init(policydb)) {
54         fprintf(stderr, "Could not initialize policydb!\n");
55         close(fd);
56         munmap(map, sb.st_size);
57         return 1;
58     }
59     ret = policydb_read(policydb, pf, 0);
60     if (ret) {
61         fprintf(stderr, "error(s) encountered while parsing configuration\n");
62         close(fd);
63         munmap(map, sb.st_size);
64         return 1;
65     }
66 
67     return 0;
68 }
69