Contents of /tags/mkinitrd-6_3_4/busybox/selinux/setfiles.c
Parent Directory | Revision Log
Revision 1189 -
(show annotations)
(download)
Thu Dec 16 01:27:43 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 15820 byte(s)
Thu Dec 16 01:27:43 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 15820 byte(s)
tagged 'mkinitrd-6_3_4'
1 | /* |
2 | setfiles: based on policycoreutils 2.0.19 |
3 | policycoreutils was released under GPL 2. |
4 | Port to BusyBox (c) 2007 by Yuichi Nakamura <ynakam@hitachisoft.jp> |
5 | */ |
6 | |
7 | #include "libbb.h" |
8 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
9 | #include <sepol/sepol.h> |
10 | #endif |
11 | |
12 | #define MAX_EXCLUDES 50 |
13 | |
14 | struct edir { |
15 | char *directory; |
16 | size_t size; |
17 | }; |
18 | |
19 | struct globals { |
20 | FILE *outfile; |
21 | char *policyfile; |
22 | char *rootpath; |
23 | int rootpathlen; |
24 | unsigned count; |
25 | int excludeCtr; |
26 | int errors; |
27 | int verbose; /* getopt32 uses it, has to be int */ |
28 | smallint recurse; /* Recursive descent */ |
29 | smallint follow_mounts; |
30 | /* Behavior flags determined based on setfiles vs. restorecon */ |
31 | smallint expand_realpath; /* Expand paths via realpath */ |
32 | smallint abort_on_error; /* Abort the file tree walk upon an error */ |
33 | int add_assoc; /* Track inode associations for conflict detection */ |
34 | int matchpathcon_flags; /* Flags to matchpathcon */ |
35 | dev_t dev_id; /* Device id where target file exists */ |
36 | int nerr; |
37 | struct edir excludeArray[MAX_EXCLUDES]; |
38 | } FIX_ALIASING; |
39 | #define G (*(struct globals*)&bb_common_bufsiz1) |
40 | void BUG_setfiles_globals_too_big(void); |
41 | #define INIT_G() do { \ |
42 | if (sizeof(G) > COMMON_BUFSIZE) \ |
43 | BUG_setfiles_globals_too_big(); \ |
44 | /* memset(&G, 0, sizeof(G)); - already is */ \ |
45 | } while (0) |
46 | #define outfile (G.outfile ) |
47 | #define policyfile (G.policyfile ) |
48 | #define rootpath (G.rootpath ) |
49 | #define rootpathlen (G.rootpathlen ) |
50 | #define count (G.count ) |
51 | #define excludeCtr (G.excludeCtr ) |
52 | #define errors (G.errors ) |
53 | #define verbose (G.verbose ) |
54 | #define recurse (G.recurse ) |
55 | #define follow_mounts (G.follow_mounts ) |
56 | #define expand_realpath (G.expand_realpath ) |
57 | #define abort_on_error (G.abort_on_error ) |
58 | #define add_assoc (G.add_assoc ) |
59 | #define matchpathcon_flags (G.matchpathcon_flags) |
60 | #define dev_id (G.dev_id ) |
61 | #define nerr (G.nerr ) |
62 | #define excludeArray (G.excludeArray ) |
63 | |
64 | /* Must match getopt32 string! */ |
65 | enum { |
66 | OPT_d = (1 << 0), |
67 | OPT_e = (1 << 1), |
68 | OPT_f = (1 << 2), |
69 | OPT_i = (1 << 3), |
70 | OPT_l = (1 << 4), |
71 | OPT_n = (1 << 5), |
72 | OPT_p = (1 << 6), |
73 | OPT_q = (1 << 7), |
74 | OPT_r = (1 << 8), |
75 | OPT_s = (1 << 9), |
76 | OPT_v = (1 << 10), |
77 | OPT_o = (1 << 11), |
78 | OPT_F = (1 << 12), |
79 | OPT_W = (1 << 13), |
80 | OPT_c = (1 << 14), /* c only for setfiles */ |
81 | OPT_R = (1 << 14), /* R only for restorecon */ |
82 | }; |
83 | #define FLAG_d_debug (option_mask32 & OPT_d) |
84 | #define FLAG_e (option_mask32 & OPT_e) |
85 | #define FLAG_f (option_mask32 & OPT_f) |
86 | #define FLAG_i_ignore_enoent (option_mask32 & OPT_i) |
87 | #define FLAG_l_take_log (option_mask32 & OPT_l) |
88 | #define FLAG_n_dry_run (option_mask32 & OPT_n) |
89 | #define FLAG_p_progress (option_mask32 & OPT_p) |
90 | #define FLAG_q_quiet (option_mask32 & OPT_q) |
91 | #define FLAG_r (option_mask32 & OPT_r) |
92 | #define FLAG_s (option_mask32 & OPT_s) |
93 | #define FLAG_v (option_mask32 & OPT_v) |
94 | #define FLAG_o (option_mask32 & OPT_o) |
95 | #define FLAG_F_force (option_mask32 & OPT_F) |
96 | #define FLAG_W_warn_no_match (option_mask32 & OPT_W) |
97 | #define FLAG_c (option_mask32 & OPT_c) |
98 | #define FLAG_R (option_mask32 & OPT_R) |
99 | |
100 | |
101 | static void qprintf(const char *fmt UNUSED_PARAM, ...) |
102 | { |
103 | /* quiet, do nothing */ |
104 | } |
105 | |
106 | static void inc_err(void) |
107 | { |
108 | nerr++; |
109 | if (nerr > 9 && !FLAG_d_debug) { |
110 | bb_error_msg_and_die("exiting after 10 errors"); |
111 | } |
112 | } |
113 | |
114 | static void add_exclude(const char *directory) |
115 | { |
116 | struct stat sb; |
117 | size_t len; |
118 | |
119 | if (directory == NULL || directory[0] != '/') { |
120 | bb_error_msg_and_die("full path required for exclude: %s", directory); |
121 | |
122 | } |
123 | if (lstat(directory, &sb)) { |
124 | bb_error_msg("directory \"%s\" not found, ignoring", directory); |
125 | return; |
126 | } |
127 | if ((sb.st_mode & S_IFDIR) == 0) { |
128 | bb_error_msg("\"%s\" is not a directory: mode %o, ignoring", |
129 | directory, sb.st_mode); |
130 | return; |
131 | } |
132 | if (excludeCtr == MAX_EXCLUDES) { |
133 | bb_error_msg_and_die("maximum excludes %d exceeded", MAX_EXCLUDES); |
134 | } |
135 | |
136 | len = strlen(directory); |
137 | while (len > 1 && directory[len - 1] == '/') { |
138 | len--; |
139 | } |
140 | excludeArray[excludeCtr].directory = xstrndup(directory, len); |
141 | excludeArray[excludeCtr++].size = len; |
142 | } |
143 | |
144 | static bool exclude(const char *file) |
145 | { |
146 | int i = 0; |
147 | for (i = 0; i < excludeCtr; i++) { |
148 | if (strncmp(file, excludeArray[i].directory, |
149 | excludeArray[i].size) == 0) { |
150 | if (file[excludeArray[i].size] == '\0' |
151 | || file[excludeArray[i].size] == '/') { |
152 | return 1; |
153 | } |
154 | } |
155 | } |
156 | return 0; |
157 | } |
158 | |
159 | static int match(const char *name, struct stat *sb, char **con) |
160 | { |
161 | int ret; |
162 | char path[PATH_MAX + 1]; |
163 | char *tmp_path = xstrdup(name); |
164 | |
165 | if (excludeCtr > 0 && exclude(name)) { |
166 | goto err; |
167 | } |
168 | ret = lstat(name, sb); |
169 | if (ret) { |
170 | if (FLAG_i_ignore_enoent && errno == ENOENT) { |
171 | free(tmp_path); |
172 | return 0; |
173 | } |
174 | bb_error_msg("stat(%s)", name); |
175 | goto err; |
176 | } |
177 | |
178 | if (expand_realpath) { |
179 | if (S_ISLNK(sb->st_mode)) { |
180 | char *p = NULL; |
181 | char *file_sep; |
182 | |
183 | size_t len = 0; |
184 | |
185 | if (verbose > 1) |
186 | bb_error_msg("warning! %s refers to a symbolic link, not following last component", name); |
187 | |
188 | file_sep = strrchr(tmp_path, '/'); |
189 | if (file_sep == tmp_path) { |
190 | file_sep++; |
191 | path[0] = '\0'; |
192 | p = path; |
193 | } else if (file_sep) { |
194 | *file_sep++ = '\0'; |
195 | p = realpath(tmp_path, path); |
196 | } else { |
197 | file_sep = tmp_path; |
198 | p = realpath("./", path); |
199 | } |
200 | if (p) |
201 | len = strlen(p); |
202 | if (!p || len + strlen(file_sep) + 2 > PATH_MAX) { |
203 | bb_perror_msg("realpath(%s) failed", name); |
204 | goto err; |
205 | } |
206 | p += len; |
207 | /* ensure trailing slash of directory name */ |
208 | if (len == 0 || p[-1] != '/') { |
209 | *p++ = '/'; |
210 | } |
211 | strcpy(p, file_sep); |
212 | name = path; |
213 | if (excludeCtr > 0 && exclude(name)) |
214 | goto err; |
215 | |
216 | } else { |
217 | char *p; |
218 | p = realpath(name, path); |
219 | if (!p) { |
220 | bb_perror_msg("realpath(%s)", name); |
221 | goto err; |
222 | } |
223 | name = p; |
224 | if (excludeCtr > 0 && exclude(name)) |
225 | goto err; |
226 | } |
227 | } |
228 | |
229 | /* name will be what is matched in the policy */ |
230 | if (NULL != rootpath) { |
231 | if (0 != strncmp(rootpath, name, rootpathlen)) { |
232 | bb_error_msg("%s is not located in %s", |
233 | name, rootpath); |
234 | goto err; |
235 | } |
236 | name += rootpathlen; |
237 | } |
238 | |
239 | free(tmp_path); |
240 | if (rootpath != NULL && name[0] == '\0') |
241 | /* this is actually the root dir of the alt root */ |
242 | return matchpathcon_index("/", sb->st_mode, con); |
243 | return matchpathcon_index(name, sb->st_mode, con); |
244 | err: |
245 | free(tmp_path); |
246 | return -1; |
247 | } |
248 | |
249 | /* Compare two contexts to see if their differences are "significant", |
250 | * or whether the only difference is in the user. */ |
251 | static bool only_changed_user(const char *a, const char *b) |
252 | { |
253 | if (FLAG_F_force) |
254 | return 0; |
255 | if (!a || !b) |
256 | return 0; |
257 | a = strchr(a, ':'); /* Rest of the context after the user */ |
258 | b = strchr(b, ':'); |
259 | if (!a || !b) |
260 | return 0; |
261 | return (strcmp(a, b) == 0); |
262 | } |
263 | |
264 | static int restore(const char *file) |
265 | { |
266 | char *my_file; |
267 | struct stat my_sb; |
268 | int i, j, ret; |
269 | char *context = NULL; |
270 | char *newcon = NULL; |
271 | bool user_only_changed = 0; |
272 | int retval = 0; |
273 | |
274 | my_file = bb_simplify_path(file); |
275 | |
276 | i = match(my_file, &my_sb, &newcon); |
277 | |
278 | if (i < 0) /* No matching specification. */ |
279 | goto out; |
280 | |
281 | if (FLAG_p_progress) { |
282 | count++; |
283 | if (count % 0x400 == 0) { /* every 1024 times */ |
284 | count = (count % (80*0x400)); |
285 | if (count == 0) |
286 | bb_putchar('\n'); |
287 | bb_putchar('*'); |
288 | fflush_all(); |
289 | } |
290 | } |
291 | |
292 | /* |
293 | * Try to add an association between this inode and |
294 | * this specification. If there is already an association |
295 | * for this inode and it conflicts with this specification, |
296 | * then use the last matching specification. |
297 | */ |
298 | if (add_assoc) { |
299 | j = matchpathcon_filespec_add(my_sb.st_ino, i, my_file); |
300 | if (j < 0) |
301 | goto err; |
302 | |
303 | if (j != i) { |
304 | /* There was already an association and it took precedence. */ |
305 | goto out; |
306 | } |
307 | } |
308 | |
309 | if (FLAG_d_debug) |
310 | printf("%s: %s matched by %s\n", applet_name, my_file, newcon); |
311 | |
312 | /* Get the current context of the file. */ |
313 | ret = lgetfilecon_raw(my_file, &context); |
314 | if (ret < 0) { |
315 | if (errno == ENODATA) { |
316 | context = NULL; /* paranoia */ |
317 | } else { |
318 | bb_perror_msg("lgetfilecon_raw on %s", my_file); |
319 | goto err; |
320 | } |
321 | user_only_changed = 0; |
322 | } else |
323 | user_only_changed = only_changed_user(context, newcon); |
324 | |
325 | /* |
326 | * Do not relabel the file if the matching specification is |
327 | * <<none>> or the file is already labeled according to the |
328 | * specification. |
329 | */ |
330 | if ((strcmp(newcon, "<<none>>") == 0) |
331 | || (context && (strcmp(context, newcon) == 0) && !FLAG_F_force)) { |
332 | goto out; |
333 | } |
334 | |
335 | if (!FLAG_F_force && context && (is_context_customizable(context) > 0)) { |
336 | if (verbose > 1) { |
337 | bb_error_msg("skipping %s. %s is customizable_types", |
338 | my_file, context); |
339 | } |
340 | goto out; |
341 | } |
342 | |
343 | if (verbose) { |
344 | /* If we're just doing "-v", trim out any relabels where |
345 | * the user has changed but the role and type are the |
346 | * same. For "-vv", emit everything. */ |
347 | if (verbose > 1 || !user_only_changed) { |
348 | bb_info_msg("%s: reset %s context %s->%s", |
349 | applet_name, my_file, context ? context : "", newcon); |
350 | } |
351 | } |
352 | |
353 | if (FLAG_l_take_log && !user_only_changed) { |
354 | if (context) |
355 | bb_info_msg("relabeling %s from %s to %s", my_file, context, newcon); |
356 | else |
357 | bb_info_msg("labeling %s to %s", my_file, newcon); |
358 | } |
359 | |
360 | if (outfile && !user_only_changed) |
361 | fprintf(outfile, "%s\n", my_file); |
362 | |
363 | /* |
364 | * Do not relabel the file if -n was used. |
365 | */ |
366 | if (FLAG_n_dry_run || user_only_changed) |
367 | goto out; |
368 | |
369 | /* |
370 | * Relabel the file to the specified context. |
371 | */ |
372 | ret = lsetfilecon(my_file, newcon); |
373 | if (ret) { |
374 | bb_perror_msg("lsetfileconon(%s,%s)", my_file, newcon); |
375 | goto err; |
376 | } |
377 | |
378 | out: |
379 | freecon(context); |
380 | freecon(newcon); |
381 | free(my_file); |
382 | return retval; |
383 | err: |
384 | retval--; /* -1 */ |
385 | goto out; |
386 | } |
387 | |
388 | /* |
389 | * Apply the last matching specification to a file. |
390 | * This function is called by recursive_action on each file during |
391 | * the directory traversal. |
392 | */ |
393 | static int FAST_FUNC apply_spec( |
394 | const char *file, |
395 | struct stat *sb, |
396 | void *userData UNUSED_PARAM, |
397 | int depth UNUSED_PARAM) |
398 | { |
399 | if (!follow_mounts) { |
400 | /* setfiles does not process across different mount points */ |
401 | if (sb->st_dev != dev_id) { |
402 | return SKIP; |
403 | } |
404 | } |
405 | errors |= restore(file); |
406 | if (abort_on_error && errors) |
407 | return FALSE; |
408 | return TRUE; |
409 | } |
410 | |
411 | |
412 | static int canoncon(const char *path, unsigned lineno, char **contextp) |
413 | { |
414 | static const char err_msg[] ALIGN1 = "%s: line %u has invalid context %s"; |
415 | |
416 | char *tmpcon; |
417 | char *context = *contextp; |
418 | int invalid = 0; |
419 | |
420 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
421 | if (policyfile) { |
422 | if (sepol_check_context(context) >= 0) |
423 | return 0; |
424 | /* Exit immediately if we're in checking mode. */ |
425 | bb_error_msg_and_die(err_msg, path, lineno, context); |
426 | } |
427 | #endif |
428 | |
429 | if (security_canonicalize_context_raw(context, &tmpcon) < 0) { |
430 | if (errno != ENOENT) { |
431 | invalid = 1; |
432 | inc_err(); |
433 | } |
434 | } else { |
435 | free(context); |
436 | *contextp = tmpcon; |
437 | } |
438 | |
439 | if (invalid) { |
440 | bb_error_msg(err_msg, path, lineno, context); |
441 | } |
442 | |
443 | return invalid; |
444 | } |
445 | |
446 | static int process_one(char *name) |
447 | { |
448 | struct stat sb; |
449 | int rc; |
450 | |
451 | rc = lstat(name, &sb); |
452 | if (rc < 0) { |
453 | if (FLAG_i_ignore_enoent && errno == ENOENT) |
454 | return 0; |
455 | bb_perror_msg("stat(%s)", name); |
456 | goto err; |
457 | } |
458 | dev_id = sb.st_dev; |
459 | |
460 | if (S_ISDIR(sb.st_mode) && recurse) { |
461 | if (recursive_action(name, |
462 | ACTION_RECURSE, |
463 | apply_spec, |
464 | apply_spec, |
465 | NULL, 0) != TRUE) { |
466 | bb_error_msg("error while labeling %s", name); |
467 | goto err; |
468 | } |
469 | } else { |
470 | rc = restore(name); |
471 | if (rc) |
472 | goto err; |
473 | } |
474 | |
475 | out: |
476 | if (add_assoc) { |
477 | if (FLAG_q_quiet) |
478 | set_matchpathcon_printf(&qprintf); |
479 | matchpathcon_filespec_eval(); |
480 | set_matchpathcon_printf(NULL); |
481 | matchpathcon_filespec_destroy(); |
482 | } |
483 | |
484 | return rc; |
485 | |
486 | err: |
487 | rc = -1; |
488 | goto out; |
489 | } |
490 | |
491 | int setfiles_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
492 | int setfiles_main(int argc UNUSED_PARAM, char **argv) |
493 | { |
494 | struct stat sb; |
495 | int rc, i = 0; |
496 | const char *input_filename = NULL; |
497 | char *buf = NULL; |
498 | size_t buf_len; |
499 | int flags; |
500 | llist_t *exclude_dir = NULL; |
501 | char *out_filename = NULL; |
502 | |
503 | INIT_G(); |
504 | |
505 | if (applet_name[0] == 's') { /* "setfiles" */ |
506 | /* |
507 | * setfiles: |
508 | * Recursive descent, |
509 | * Does not expand paths via realpath, |
510 | * Aborts on errors during the file tree walk, |
511 | * Try to track inode associations for conflict detection, |
512 | * Does not follow mounts, |
513 | * Validates all file contexts at init time. |
514 | */ |
515 | recurse = 1; |
516 | abort_on_error = 1; |
517 | add_assoc = 1; |
518 | /* follow_mounts = 0; - already is */ |
519 | matchpathcon_flags = MATCHPATHCON_VALIDATE | MATCHPATHCON_NOTRANS; |
520 | } else { |
521 | /* |
522 | * restorecon: |
523 | * No recursive descent unless -r/-R, |
524 | * Expands paths via realpath, |
525 | * Do not abort on errors during the file tree walk, |
526 | * Do not try to track inode associations for conflict detection, |
527 | * Follows mounts, |
528 | * Does lazy validation of contexts upon use. |
529 | */ |
530 | expand_realpath = 1; |
531 | follow_mounts = 1; |
532 | matchpathcon_flags = MATCHPATHCON_NOTRANS; |
533 | /* restorecon only */ |
534 | selinux_or_die(); |
535 | } |
536 | |
537 | set_matchpathcon_flags(matchpathcon_flags); |
538 | |
539 | opt_complementary = "e::vv:v--p:p--v:v--q:q--v"; |
540 | /* Option order must match OPT_x definitions! */ |
541 | if (applet_name[0] == 'r') { /* restorecon */ |
542 | flags = getopt32(argv, "de:f:ilnpqrsvo:FWR", |
543 | &exclude_dir, &input_filename, &out_filename, &verbose); |
544 | } else { /* setfiles */ |
545 | flags = getopt32(argv, "de:f:ilnpqr:svo:FW" |
546 | IF_FEATURE_SETFILES_CHECK_OPTION("c:"), |
547 | &exclude_dir, &input_filename, &rootpath, &out_filename, |
548 | IF_FEATURE_SETFILES_CHECK_OPTION(&policyfile,) |
549 | &verbose); |
550 | } |
551 | argv += optind; |
552 | |
553 | #if ENABLE_FEATURE_SETFILES_CHECK_OPTION |
554 | if ((applet_name[0] == 's') && (flags & OPT_c)) { |
555 | FILE *policystream; |
556 | |
557 | policystream = xfopen_for_read(policyfile); |
558 | if (sepol_set_policydb_from_file(policystream) < 0) { |
559 | bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile); |
560 | } |
561 | fclose(policystream); |
562 | |
563 | /* Only process the specified file_contexts file, not |
564 | any .homedirs or .local files, and do not perform |
565 | context translations. */ |
566 | set_matchpathcon_flags(MATCHPATHCON_BASEONLY | |
567 | MATCHPATHCON_NOTRANS | |
568 | MATCHPATHCON_VALIDATE); |
569 | } |
570 | #endif |
571 | |
572 | while (exclude_dir) |
573 | add_exclude(llist_pop(&exclude_dir)); |
574 | |
575 | if (flags & OPT_o) { |
576 | outfile = stdout; |
577 | if (NOT_LONE_CHAR(out_filename, '-')) { |
578 | outfile = xfopen_for_write(out_filename); |
579 | } |
580 | } |
581 | if (applet_name[0] == 'r') { /* restorecon */ |
582 | if (flags & (OPT_r | OPT_R)) |
583 | recurse = 1; |
584 | } else { /* setfiles */ |
585 | if (flags & OPT_r) |
586 | rootpathlen = strlen(rootpath); |
587 | } |
588 | if (flags & OPT_s) { |
589 | input_filename = "-"; |
590 | add_assoc = 0; |
591 | } |
592 | |
593 | if (applet_name[0] == 's') { /* setfiles */ |
594 | /* Use our own invalid context checking function so that |
595 | we can support either checking against the active policy or |
596 | checking against a binary policy file. */ |
597 | set_matchpathcon_canoncon(&canoncon); |
598 | if (!argv[0]) |
599 | bb_show_usage(); |
600 | xstat(argv[0], &sb); |
601 | if (!S_ISREG(sb.st_mode)) { |
602 | bb_error_msg_and_die("spec file %s is not a regular file", argv[0]); |
603 | } |
604 | /* Load the file contexts configuration and check it. */ |
605 | rc = matchpathcon_init(argv[0]); |
606 | if (rc < 0) { |
607 | bb_simple_perror_msg_and_die(argv[0]); |
608 | } |
609 | if (nerr) |
610 | exit(EXIT_FAILURE); |
611 | argv++; |
612 | } |
613 | |
614 | if (input_filename) { |
615 | ssize_t len; |
616 | FILE *f = stdin; |
617 | |
618 | if (NOT_LONE_CHAR(input_filename, '-')) |
619 | f = xfopen_for_read(input_filename); |
620 | while ((len = getline(&buf, &buf_len, f)) > 0) { |
621 | buf[len - 1] = '\0'; |
622 | errors |= process_one(buf); |
623 | } |
624 | if (ENABLE_FEATURE_CLEAN_UP) |
625 | fclose_if_not_stdin(f); |
626 | } else { |
627 | if (!argv[0]) |
628 | bb_show_usage(); |
629 | for (i = 0; argv[i]; i++) { |
630 | errors |= process_one(argv[i]); |
631 | } |
632 | } |
633 | |
634 | if (FLAG_W_warn_no_match) |
635 | matchpathcon_checkmatches(argv[0]); |
636 | |
637 | if (ENABLE_FEATURE_CLEAN_UP && outfile) |
638 | fclose(outfile); |
639 | |
640 | return errors; |
641 | } |