blob: 2e49c1b0135439a3567f3377aa8092b526bcce5a [file] [log] [blame]
sandromb246b1d2022-05-24 08:46:06 +00001#include <getopt.h>
2#include <stddef.h>
3#include <stdint.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/stat.h>
8
9#include <cil/cil.h>
10#include <cil/android.h>
11#include <sepol/policydb.h>
12
13void usage(const char *prog)
14{
15 printf("Usage: %s [OPTION]... FILE...\n", prog);
16 printf("Takes a binary policy file as input and applies the rules and definitions specified ");
17 printf("in the provided FILEs. Each FILE must be a policy file in CIL format.\n");
18 printf("\n");
19 printf("Options:\n");
20 printf(" -b, --base=<file> (required) base binary policy.\n");
21 printf(" -o, --output=<file> (required) write binary policy to <file>\n");
22 printf(" -v, --verbose increment verbosity level\n");
23 printf(" -h, --help display usage information\n");
24 exit(1);
25}
26
27/*
28 * Read binary policy file from path into the allocated pdb.
29 */
30static int read_binary_policy(char *path, sepol_policydb_t *pdb)
31{
32 int rc = SEPOL_OK;
33
34 FILE *file = fopen(path, "r");
35 if (!file) {
36 fprintf(stderr, "Could not open %s: %s.\n", path, strerror(errno));
37 rc = SEPOL_ERR;
38 goto exit;
39 }
40
41 struct stat binarydata;
42 rc = stat(path, &binarydata);
43 if (rc == -1) {
44 fprintf(stderr, "Could not stat %s: %s.\n", path, strerror(errno));
45 goto exit;
46 }
47 if (!binarydata.st_size) {
48 fprintf(stderr, "Binary policy file is empty.\n");
49 rc = SEPOL_ERR;
50 goto exit;
51 }
52
53 struct sepol_policy_file *pf = NULL;
54 rc = sepol_policy_file_create(&pf);
55 if (rc != 0) {
56 fprintf(stderr, "Failed to create policy file: %d.\n", rc);
57 goto exit;
58 }
59 sepol_policy_file_set_fp(pf, file);
60
61 rc = sepol_policydb_read(pdb, pf);
62 if (rc != 0) {
63 fprintf(stderr, "Failed to read binary policy: %d.\n", rc);
64 goto exit;
65 }
66
67exit:
68 if (file != NULL && fclose(file) == EOF && rc == SEPOL_OK) {
69 perror("Failure closing binary file");
70 rc = SEPOL_ERR;
71 }
72 return rc;
73}
74
75/*
76 * read_cil_files - Initialize db and parse CIL input files.
77 */
78static int read_cil_files(struct cil_db **db, char **paths,
79 unsigned int n_files)
80{
81 int rc = SEPOL_ERR;
82 FILE *file = NULL;
83 char *buff = NULL;
84
85 for (int i = 0; i < n_files; i++) {
86 char *path = paths[i];
87
88 file = fopen(path, "r");
89 if (file == NULL) {
90 rc = SEPOL_ERR;
91 fprintf(stderr, "Could not open %s: %s.\n", path, strerror(errno));
92 goto file_err;
93 }
94
95 struct stat filedata;
96 rc = stat(path, &filedata);
97 if (rc == -1) {
98 fprintf(stderr, "Could not stat %s: %s.\n", path, strerror(errno));
99 goto err;
100 }
101
102 uint32_t file_size = filedata.st_size;
103 buff = malloc(file_size);
104 if (buff == NULL) {
105 perror("malloc failed");
106 rc = SEPOL_ERR;
107 goto err;
108 }
109
110 rc = fread(buff, file_size, 1, file);
111 if (rc != 1) {
112 fprintf(stderr, "Failure reading %s: %s.\n", path, strerror(errno));
113 rc = SEPOL_ERR;
114 goto err;
115 }
116 fclose(file);
117 file = NULL;
118
119 /* create parse_tree */
120 rc = cil_add_file(*db, path, buff, file_size);
121 if (rc != SEPOL_OK) {
122 fprintf(stderr, "Failure adding %s to parse tree.\n", path);
123 goto parse_err;
124 }
125 free(buff);
George Burgess IV3f0bbd12022-07-12 10:05:11 -0700126 buff = NULL;
sandromb246b1d2022-05-24 08:46:06 +0000127 }
128
129 return SEPOL_OK;
130err:
131 fclose(file);
132parse_err:
133 free(buff);
134file_err:
135 return rc;
136}
137
138/*
139 * Write binary policy in pdb to file at path.
140 */
141static int write_binary_policy(sepol_policydb_t *pdb, char *path)
142{
143 int rc = SEPOL_OK;
144
145 FILE *file = fopen(path, "w");
146 if (file == NULL) {
147 fprintf(stderr, "Could not open %s: %s.\n", path, strerror(errno));
148 rc = SEPOL_ERR;
149 goto exit;
150 }
151
152 struct sepol_policy_file *pf = NULL;
153 rc = sepol_policy_file_create(&pf);
154 if (rc != 0) {
155 fprintf(stderr, "Failed to create policy file: %d.\n", rc);
156 goto exit;
157 }
158 sepol_policy_file_set_fp(pf, file);
159
160 rc = sepol_policydb_write(pdb, pf);
161 if (rc != 0) {
162 fprintf(stderr, "failed to write binary policy: %d.\n", rc);
163 goto exit;
164 }
165
166exit:
167 if (file != NULL && fclose(file) == EOF && rc == SEPOL_OK) {
168 perror("Failure closing binary file");
169 rc = SEPOL_ERR;
170 }
171 return rc;
172}
173
174int main(int argc, char *argv[])
175{
176 char *base = NULL;
177 char *output = NULL;
178 enum cil_log_level log_level = CIL_ERR;
179 static struct option long_opts[] = {{"base", required_argument, 0, 'b'},
180 {"output", required_argument, 0, 'o'},
181 {"verbose", no_argument, 0, 'v'},
182 {"help", no_argument, 0, 'h'},
183 {0, 0, 0, 0}};
184
185 while (1) {
186 int opt_index = 0;
187 int opt_char = getopt_long(argc, argv, "b:o:vh", long_opts, &opt_index);
188 if (opt_char == -1) {
189 break;
190 }
191 switch (opt_char)
192 {
193 case 'b':
194 base = optarg;
195 break;
196 case 'o':
197 output = optarg;
198 break;
199 case 'v':
200 log_level++;
201 break;
202 case 'h':
203 usage(argv[0]);
204 default:
205 fprintf(stderr, "Unsupported option: %s.\n", optarg);
206 usage(argv[0]);
207 }
208 }
209 if (base == NULL || output == NULL) {
210 fprintf(stderr, "Please specify required arguments.\n");
211 usage(argv[0]);
212 }
213
214 cil_set_log_level(log_level);
215
216 // Initialize and read input policydb file.
217 sepol_policydb_t *pdb = NULL;
218 int rc = sepol_policydb_create(&pdb);
219 if (rc != 0) {
220 fprintf(stderr, "Could not create policy db: %d.\n", rc);
221 exit(rc);
222 }
223
224 rc = read_binary_policy(base, pdb);
225 if (rc != SEPOL_OK) {
226 fprintf(stderr, "Failed to read binary policy: %d.\n", rc);
227 exit(rc);
228 }
229
230 // Initialize cil_db.
231 struct cil_db *incremental_db = NULL;
232 cil_db_init(&incremental_db);
233 cil_set_attrs_expand_generated(incremental_db, 1);
234
235 // Read input cil files and compile them into cil_db.
236 rc = read_cil_files(&incremental_db, argv + optind, argc - optind);
237 if (rc != SEPOL_OK) {
238 fprintf(stderr, "Failed to read CIL files: %d.\n", rc);
239 exit(rc);
240 }
241
242 rc = cil_compile(incremental_db);
243 if (rc != SEPOL_OK) {
244 fprintf(stderr, "Failed to compile cildb: %d.\n", rc);
245 exit(rc);
246 }
247
248 // Amend the policydb.
249 rc = cil_amend_policydb(incremental_db, pdb);
250 if (rc != SEPOL_OK) {
251 fprintf(stderr, "Failed to build policydb.\n");
252 exit(rc);
253 }
254
255 rc = write_binary_policy(pdb, output);
256 if (rc != SEPOL_OK) {
257 fprintf(stderr, "Failed to write binary policy: %d.\n", rc);
258 exit(rc);
259 }
260}