blob: 1328afb24e7c4bdddafca2f21a3447c234efbcef [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);
126 }
127
128 return SEPOL_OK;
129err:
130 fclose(file);
131parse_err:
132 free(buff);
133file_err:
134 return rc;
135}
136
137/*
138 * Write binary policy in pdb to file at path.
139 */
140static int write_binary_policy(sepol_policydb_t *pdb, char *path)
141{
142 int rc = SEPOL_OK;
143
144 FILE *file = fopen(path, "w");
145 if (file == NULL) {
146 fprintf(stderr, "Could not open %s: %s.\n", path, strerror(errno));
147 rc = SEPOL_ERR;
148 goto exit;
149 }
150
151 struct sepol_policy_file *pf = NULL;
152 rc = sepol_policy_file_create(&pf);
153 if (rc != 0) {
154 fprintf(stderr, "Failed to create policy file: %d.\n", rc);
155 goto exit;
156 }
157 sepol_policy_file_set_fp(pf, file);
158
159 rc = sepol_policydb_write(pdb, pf);
160 if (rc != 0) {
161 fprintf(stderr, "failed to write binary policy: %d.\n", rc);
162 goto exit;
163 }
164
165exit:
166 if (file != NULL && fclose(file) == EOF && rc == SEPOL_OK) {
167 perror("Failure closing binary file");
168 rc = SEPOL_ERR;
169 }
170 return rc;
171}
172
173int main(int argc, char *argv[])
174{
175 char *base = NULL;
176 char *output = NULL;
177 enum cil_log_level log_level = CIL_ERR;
178 static struct option long_opts[] = {{"base", required_argument, 0, 'b'},
179 {"output", required_argument, 0, 'o'},
180 {"verbose", no_argument, 0, 'v'},
181 {"help", no_argument, 0, 'h'},
182 {0, 0, 0, 0}};
183
184 while (1) {
185 int opt_index = 0;
186 int opt_char = getopt_long(argc, argv, "b:o:vh", long_opts, &opt_index);
187 if (opt_char == -1) {
188 break;
189 }
190 switch (opt_char)
191 {
192 case 'b':
193 base = optarg;
194 break;
195 case 'o':
196 output = optarg;
197 break;
198 case 'v':
199 log_level++;
200 break;
201 case 'h':
202 usage(argv[0]);
203 default:
204 fprintf(stderr, "Unsupported option: %s.\n", optarg);
205 usage(argv[0]);
206 }
207 }
208 if (base == NULL || output == NULL) {
209 fprintf(stderr, "Please specify required arguments.\n");
210 usage(argv[0]);
211 }
212
213 cil_set_log_level(log_level);
214
215 // Initialize and read input policydb file.
216 sepol_policydb_t *pdb = NULL;
217 int rc = sepol_policydb_create(&pdb);
218 if (rc != 0) {
219 fprintf(stderr, "Could not create policy db: %d.\n", rc);
220 exit(rc);
221 }
222
223 rc = read_binary_policy(base, pdb);
224 if (rc != SEPOL_OK) {
225 fprintf(stderr, "Failed to read binary policy: %d.\n", rc);
226 exit(rc);
227 }
228
229 // Initialize cil_db.
230 struct cil_db *incremental_db = NULL;
231 cil_db_init(&incremental_db);
232 cil_set_attrs_expand_generated(incremental_db, 1);
233
234 // Read input cil files and compile them into cil_db.
235 rc = read_cil_files(&incremental_db, argv + optind, argc - optind);
236 if (rc != SEPOL_OK) {
237 fprintf(stderr, "Failed to read CIL files: %d.\n", rc);
238 exit(rc);
239 }
240
241 rc = cil_compile(incremental_db);
242 if (rc != SEPOL_OK) {
243 fprintf(stderr, "Failed to compile cildb: %d.\n", rc);
244 exit(rc);
245 }
246
247 // Amend the policydb.
248 rc = cil_amend_policydb(incremental_db, pdb);
249 if (rc != SEPOL_OK) {
250 fprintf(stderr, "Failed to build policydb.\n");
251 exit(rc);
252 }
253
254 rc = write_binary_policy(pdb, output);
255 if (rc != SEPOL_OK) {
256 fprintf(stderr, "Failed to write binary policy: %d.\n", rc);
257 exit(rc);
258 }
259}