• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <getopt.h>
35 #include <sys/stat.h>
36 
37 #ifdef ANDROID
38 #include <cil/cil.h>
39 #else
40 #include <sepol/cil/cil.h>
41 #endif
42 #include <sepol/policydb.h>
43 
usage(char * prog)44 void usage(char *prog)
45 {
46 	printf("Usage: %s [OPTION]... FILE...\n", prog);
47 	printf("\n");
48 	printf("Options:\n");
49 	printf("  -o, --output=<file>            write binary policy to <file>\n");
50 	printf("                                 (default: policy.<version>)\n");
51 	printf("  -f, --filecontext=<file>       write file contexts to <file>\n");
52 	printf("                                 (default: file_contexts)\n");
53 	printf("  -t, --target=<type>            specify target architecture. may be selinux or\n");
54 	printf("                                 xen. (default: selinux)\n");
55 	printf("  -M, --mls true|false           build an mls policy. Must be true or false.\n");
56 	printf("                                 This will override the (mls boolean) statement\n");
57 	printf("                                 if present in the policy\n");
58 	printf("  -c, --policyvers=<version>     build a binary policy with a given <version>\n");
59 	printf("                                 (default: %i)\n", POLICYDB_VERSION_MAX);
60 	printf("  -U, --handle-unknown=<action>  how to handle unknown classes or permissions.\n");
61 	printf("                                 may be deny, allow, or reject. (default: deny)\n");
62 	printf("                                 This will override the (handleunknown action)\n");
63 	printf("                                 statement if present in the policy\n");
64 	printf("  -D, --disable-dontaudit        do not add dontaudit rules to the binary policy\n");
65 	printf("  -P, --preserve-tunables        treat tunables as booleans\n");
66 	printf("  -N, --disable-neverallow       do not check neverallow rules\n");
67 	printf("  -G, --expand-generated         Expand and remove auto-generated attributes\n");
68 	printf("  -X, --expand-size <SIZE>       Expand type attributes with fewer than <SIZE>\n");
69 	printf("                                 members.\n");
70 	printf("  -v, --verbose                  increment verbosity level\n");
71 	printf("  -h, --help                     display usage information\n");
72 	exit(1);
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 	int rc = SEPOL_ERR;
78 	sepol_policydb_t *pdb = NULL;
79 	struct sepol_policy_file *pf = NULL;
80 	FILE *binary = NULL;
81 	FILE *file_contexts;
82 	FILE *file = NULL;
83 	char *buffer = NULL;
84 	struct stat filedata;
85 	uint32_t file_size;
86 	char *output = NULL;
87 	char *filecontexts = NULL;
88 	struct cil_db *db = NULL;
89 	int target = SEPOL_TARGET_SELINUX;
90 	int mls = -1;
91 	int disable_dontaudit = 0;
92 	int disable_neverallow = 0;
93 	int preserve_tunables = 0;
94 	int handle_unknown = -1;
95 	int policyvers = POLICYDB_VERSION_MAX;
96 	int attrs_expand_generated = 0;
97 	int attrs_expand_size = -1;
98 	int opt_char;
99 	int opt_index = 0;
100 	char *fc_buf = NULL;
101 	size_t fc_size;
102 	enum cil_log_level log_level = CIL_ERR;
103 	static struct option long_opts[] = {
104 		{"help", no_argument, 0, 'h'},
105 		{"verbose", no_argument, 0, 'v'},
106 		{"target", required_argument, 0, 't'},
107 		{"mls", required_argument, 0, 'M'},
108 		{"policyversion", required_argument, 0, 'c'},
109 		{"handle-unknown", required_argument, 0, 'U'},
110 		{"disable-dontaudit", no_argument, 0, 'D'},
111 		{"disable-neverallow", no_argument, 0, 'N'},
112 		{"preserve-tunables", no_argument, 0, 'P'},
113 		{"output", required_argument, 0, 'o'},
114 		{"filecontexts", required_argument, 0, 'f'},
115 		{"expand-generated", no_argument, 0, 'G'},
116 		{"expand-size", required_argument, 0, 'X'},
117 		{0, 0, 0, 0}
118 	};
119 	int i;
120 
121 	while (1) {
122 		opt_char = getopt_long(argc, argv, "o:f:U:hvt:M:PDNc:GX:", long_opts, &opt_index);
123 		if (opt_char == -1) {
124 			break;
125 		}
126 		switch (opt_char) {
127 			case 'v':
128 				log_level++;
129 				break;
130 			case 't':
131 				if (!strcmp(optarg, "selinux")) {
132 					target = SEPOL_TARGET_SELINUX;
133 				} else if (!strcmp(optarg, "xen")) {
134 					target = SEPOL_TARGET_XEN;
135 				} else {
136 					fprintf(stderr, "Unknown target: %s\n", optarg);
137 					usage(argv[0]);
138 				}
139 				break;
140 			case 'M':
141 				if (!strcasecmp(optarg, "true") || !strcasecmp(optarg, "1")) {
142 					mls = 1;
143 				} else if (!strcasecmp(optarg, "false") || !strcasecmp(optarg, "0")) {
144 					mls = 0;
145 				} else {
146 					usage(argv[0]);
147 				}
148 				break;
149 			case 'c': {
150 				char *endptr = NULL;
151 				errno = 0;
152 				policyvers = strtol(optarg, &endptr, 10);
153 				if (errno != 0 || endptr == optarg || *endptr != '\0') {
154 					fprintf(stderr, "Bad policy version: %s\n", optarg);
155 					usage(argv[0]);
156 				}
157 				if (policyvers > POLICYDB_VERSION_MAX || policyvers < POLICYDB_VERSION_MIN) {
158 					fprintf(stderr, "Policy version must be between %d and %d\n",
159 					       POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
160 					usage(argv[0]);
161 				}
162 				break;
163 			}
164 			case 'U':
165 				if (!strcasecmp(optarg, "deny")) {
166 					handle_unknown = SEPOL_DENY_UNKNOWN;
167 				} else if (!strcasecmp(optarg, "allow")) {
168 					handle_unknown = SEPOL_ALLOW_UNKNOWN;
169 				} else if (!strcasecmp(optarg, "reject")) {
170 					handle_unknown = SEPOL_REJECT_UNKNOWN;
171 				} else {
172 					usage(argv[0]);
173 				}
174 				break;
175 			case 'D':
176 				disable_dontaudit = 1;
177 				break;
178 			case 'N':
179 				disable_neverallow = 1;
180 				break;
181 			case 'P':
182 				preserve_tunables = 1;
183 				break;
184 			case 'o':
185 				output = strdup(optarg);
186 				break;
187 			case 'f':
188 				filecontexts = strdup(optarg);
189 				break;
190 			case 'G':
191 				attrs_expand_generated = 1;
192 				break;
193 			case 'X': {
194 				char *endptr = NULL;
195 				errno = 0;
196 				attrs_expand_size = strtol(optarg, &endptr, 10);
197 				if (errno != 0 || endptr == optarg || *endptr != '\0') {
198 					fprintf(stderr, "Bad attribute expand size: %s\n", optarg);
199 					usage(argv[0]);
200 				}
201 
202 				if (attrs_expand_size < 0) {
203 					fprintf(stderr, "Attribute expand size must be > 0\n");
204 					usage(argv[0]);
205 				}
206 				break;
207 			}
208 			case 'h':
209 				usage(argv[0]);
210 			case '?':
211 				break;
212 			default:
213 					fprintf(stderr, "Unsupported option: %s\n", optarg);
214 				usage(argv[0]);
215 		}
216 	}
217 	if (optind >= argc) {
218 		fprintf(stderr, "No cil files specified\n");
219 		usage(argv[0]);
220 	}
221 
222 	cil_set_log_level(log_level);
223 
224 	cil_db_init(&db);
225 	cil_set_disable_dontaudit(db, disable_dontaudit);
226 	cil_set_disable_neverallow(db, disable_neverallow);
227 	cil_set_preserve_tunables(db, preserve_tunables);
228 	if (handle_unknown != -1) {
229 		rc = cil_set_handle_unknown(db, handle_unknown);
230 		if (rc != SEPOL_OK) {
231 			goto exit;
232 		}
233 	}
234 
235 	cil_set_mls(db, mls);
236 	cil_set_target_platform(db, target);
237 	cil_set_policy_version(db, policyvers);
238 	cil_set_attrs_expand_generated(db, attrs_expand_generated);
239 	if (attrs_expand_size >= 0) {
240 		cil_set_attrs_expand_size(db, (unsigned)attrs_expand_size);
241 	}
242 
243 	for (i = optind; i < argc; i++) {
244 		file = fopen(argv[i], "r");
245 		if (!file) {
246 			fprintf(stderr, "Could not open file: %s\n", argv[i]);
247 			rc = SEPOL_ERR;
248 			goto exit;
249 		}
250 		rc = stat(argv[i], &filedata);
251 		if (rc == -1) {
252 			fprintf(stderr, "Could not stat file: %s\n", argv[i]);
253 			goto exit;
254 		}
255 		file_size = filedata.st_size;
256 
257 		buffer = malloc(file_size);
258 		rc = fread(buffer, file_size, 1, file);
259 		if (rc != 1) {
260 			fprintf(stderr, "Failure reading file: %s\n", argv[i]);
261 			goto exit;
262 		}
263 		fclose(file);
264 		file = NULL;
265 
266 		rc = cil_add_file(db, argv[i], buffer, file_size);
267 		if (rc != SEPOL_OK) {
268 			fprintf(stderr, "Failure adding %s\n", argv[i]);
269 			goto exit;
270 		}
271 
272 		free(buffer);
273 		buffer = NULL;
274 	}
275 
276 	rc = cil_compile(db);
277 	if (rc != SEPOL_OK) {
278 		fprintf(stderr, "Failed to compile cildb: %d\n", rc);
279 		goto exit;
280 	}
281 
282 	rc = cil_build_policydb(db, &pdb);
283 	if (rc != SEPOL_OK) {
284 		fprintf(stderr, "Failed to build policydb\n");
285 		goto exit;
286 	}
287 
288 	if (output == NULL) {
289 		int size = snprintf(NULL, 0, "policy.%d", policyvers);
290 		output = malloc((size + 1) * sizeof(char));
291 		if (output == NULL) {
292 			fprintf(stderr, "Failed to create output filename\n");
293 			rc = SEPOL_ERR;
294 			goto exit;
295 		}
296 		if (snprintf(output, size + 1, "policy.%d", policyvers) != size) {
297 			fprintf(stderr, "Failed to create output filename\n");
298 			rc = SEPOL_ERR;
299 			goto exit;
300 		}
301 	}
302 
303 	binary = fopen(output, "w");
304 	if (binary == NULL) {
305 		fprintf(stderr, "Failure opening binary file for writing\n");
306 		rc = SEPOL_ERR;
307 		goto exit;
308 	}
309 
310 	rc = sepol_policy_file_create(&pf);
311 	if (rc != 0) {
312 		fprintf(stderr, "Failed to create policy file: %d\n", rc);
313 		goto exit;
314 	}
315 
316 	sepol_policy_file_set_fp(pf, binary);
317 
318 	rc = sepol_policydb_write(pdb, pf);
319 	if (rc != 0) {
320 		fprintf(stderr, "Failed to write binary policy: %d\n", rc);
321 		goto exit;
322 	}
323 
324 	fclose(binary);
325 	binary = NULL;
326 
327 	rc = cil_filecons_to_string(db, &fc_buf, &fc_size);
328 	if (rc != SEPOL_OK) {
329 		fprintf(stderr, "Failed to get file context data\n");
330 		goto exit;
331 	}
332 
333 	if (filecontexts == NULL) {
334 		file_contexts = fopen("file_contexts", "w+");
335 	} else {
336 		file_contexts = fopen(filecontexts, "w+");
337 	}
338 
339 	if (file_contexts == NULL) {
340 		fprintf(stderr, "Failed to open file_contexts file\n");
341 		goto exit;
342 	}
343 
344 	if (fwrite(fc_buf, sizeof(char), fc_size, file_contexts) != fc_size) {
345 		fprintf(stderr, "Failed to write file_contexts file\n");
346 		goto exit;
347 	}
348 
349 	fclose(file_contexts);
350 	file_contexts = NULL;
351 
352 	rc = SEPOL_OK;
353 
354 exit:
355 	if (binary != NULL) {
356 		fclose(binary);
357 	}
358 	if (file != NULL) {
359 		fclose(file);
360 	}
361 	free(buffer);
362 	free(output);
363 	free(filecontexts);
364 	cil_db_destroy(&db);
365 	sepol_policydb_free(pdb);
366 	sepol_policy_file_free(pf);
367 	free(fc_buf);
368 	return rc;
369 }
370