forked from SELinuxProject/selinux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semodule.c
777 lines (686 loc) · 19.1 KB
/
semodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/* Authors: Karl MacMillan <[email protected]>
* Joshua Brindle <[email protected]>
* Jason Tang <[email protected]>
*
* Copyright (C) 2004-2005 Tresys Technology, LLC
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, version 2.
*/
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libgen.h>
#include <limits.h>
#include <semanage/modules.h>
enum client_modes {
NO_MODE, INSTALL_M, REMOVE_M, EXTRACT_M, CIL_M, HLL_M,
LIST_M, RELOAD, PRIORITY_M, ENABLE_M, DISABLE_M
};
/* list of modes in which one ought to commit afterwards */
static const int do_commit[] = {
0, 1, 1, 0, 0, 0,
0, 0, 0, 1, 1,
};
struct command {
enum client_modes mode;
char *arg;
};
static struct command *commands = NULL;
static int num_commands = 0;
/* options given on command line */
static int verbose;
static int reload;
static int no_reload;
static int build;
static int disable_dontaudit;
static int preserve_tunables;
static int ignore_module_cache;
static uint16_t priority;
static int priority_set = 0;
static semanage_handle_t *sh = NULL;
static char *store;
static char *store_root;
int extract_cil = 0;
extern char *optarg;
extern int optind;
static void cleanup(void)
{
while (--num_commands >= 0) {
free(commands[num_commands].arg);
}
free(commands);
}
/* Signal handlers. */
static void handle_signal(int sig_num)
{
if (sig_num == SIGINT || sig_num == SIGQUIT || sig_num == SIGTERM) {
/* catch these signals, and then drop them */
}
}
static void set_store(char *storename)
{
/* For now this only supports a store name, later on this
* should support an address for a remote connection */
if ((store = strdup(storename)) == NULL) {
fprintf(stderr, "Out of memory!\n");
goto bad;
}
return;
bad:
cleanup();
exit(1);
}
static void set_store_root(char *path)
{
if ((store_root = strdup(path)) == NULL) {
fprintf(stderr, "Out of memory!\n");
goto bad;
}
return;
bad:
cleanup();
exit(1);
}
/* Establish signal handlers for the process. */
static void create_signal_handlers(void)
{
if (signal(SIGINT, handle_signal) == SIG_ERR ||
signal(SIGQUIT, handle_signal) == SIG_ERR ||
signal(SIGTERM, handle_signal) == SIG_ERR) {
fprintf(stderr, "Could not set up signal handler.\n");
exit(255);
}
}
static void usage(char *progname)
{
printf("usage: %s [option]... MODE...\n", progname);
printf("Manage SELinux policy modules.\n");
printf("MODES:\n");
printf(" -R, --reload reload policy\n");
printf(" -B, --build build and reload policy\n");
printf(" -D,--disable_dontaudit Remove dontaudits from policy\n");
printf(" -i,--install=MODULE_PKG install a new module\n");
printf(" -r,--remove=MODULE_NAME remove existing module at desired priority\n");
printf(" -l[KIND],--list-modules[=KIND] display list of installed modules\n");
printf(" KIND: standard list highest priority, enabled modules\n");
printf(" full list all modules\n");
printf(" -X,--priority=PRIORITY set priority for following operations (1-999)\n");
printf(" -e,--enable=MODULE_NAME enable module\n");
printf(" -d,--disable=MODULE_NAME disable module\n");
printf(" -E,--extract=MODULE_NAME extract module\n");
printf("Options:\n");
printf(" -s,--store name of the store to operate on\n");
printf(" -N,-n,--noreload do not reload policy after commit\n");
printf(" -h,--help print this message and quit\n");
printf(" -v,--verbose be verbose\n");
printf(" -P,--preserve_tunables Preserve tunables in policy\n");
printf(" -C,--ignore-module-cache Rebuild CIL modules compiled from HLL files\n");
printf(" -p,--path use an alternate path for the policy root\n");
printf(" -S,--store-path use an alternate path for the policy store root\n");
printf(" -c, --cil extract module as cil. This only affects module extraction.\n");
printf(" -H, --hll extract module as hll. This only affects module extraction.\n");
}
/* Sets the global mode variable to new_mode, but only if no other
* mode has been given. */
static void set_mode(enum client_modes new_mode, char *arg)
{
struct command *c;
char *s;
if ((c = realloc(commands, sizeof(*c) * (num_commands + 1))) == NULL) {
fprintf(stderr, "Out of memory!\n");
cleanup();
exit(1);
}
commands = c;
commands[num_commands].mode = new_mode;
commands[num_commands].arg = NULL;
num_commands++;
if (arg != NULL) {
if ((s = strdup(arg)) == NULL) {
fprintf(stderr, "Out of memory!\n");
cleanup();
exit(1);
}
commands[num_commands - 1].arg = s;
}
}
/* Parse command line and set global options. */
static void parse_command_line(int argc, char **argv)
{
static struct option opts[] = {
{"store", required_argument, NULL, 's'},
{"base", required_argument, NULL, 'b'},
{"help", 0, NULL, 'h'},
{"install", required_argument, NULL, 'i'},
{"extract", required_argument, NULL, 'E'},
{"cil", 0, NULL, 'c'},
{"hll", 0, NULL, 'H'},
{"list-modules", optional_argument, NULL, 'l'},
{"verbose", 0, NULL, 'v'},
{"remove", required_argument, NULL, 'r'},
{"upgrade", required_argument, NULL, 'u'},
{"reload", 0, NULL, 'R'},
{"noreload", 0, NULL, 'n'},
{"build", 0, NULL, 'B'},
{"disable_dontaudit", 0, NULL, 'D'},
{"preserve_tunables", 0, NULL, 'P'},
{"ignore-module-cache", 0, NULL, 'C'},
{"priority", required_argument, NULL, 'X'},
{"enable", required_argument, NULL, 'e'},
{"disable", required_argument, NULL, 'd'},
{"path", required_argument, NULL, 'p'},
{"store-path", required_argument, NULL, 'S'},
{NULL, 0, NULL, 0}
};
int extract_selected = 0;
int cil_hll_set = 0;
int i;
verbose = 0;
reload = 0;
no_reload = 0;
priority = 400;
while ((i =
getopt_long(argc, argv, "s:b:hi:l::vr:u:RnNBDCPX:e:d:p:S:E:cH", opts,
NULL)) != -1) {
switch (i) {
case 'b':
fprintf(stderr, "The --base option is deprecated. Use --install instead.\n");
set_mode(INSTALL_M, optarg);
break;
case 'h':
usage(argv[0]);
exit(0);
case 'i':
set_mode(INSTALL_M, optarg);
break;
case 'E':
set_mode(EXTRACT_M, optarg);
extract_selected = 1;
break;
case 'c':
set_mode(CIL_M, NULL);
cil_hll_set = 1;
break;
case 'H':
set_mode(HLL_M, NULL);
cil_hll_set = 1;
break;
case 'l':
set_mode(LIST_M, optarg);
break;
case 'v':
verbose = 1;
break;
case 'r':
set_mode(REMOVE_M, optarg);
break;
case 'u':
fprintf(stderr, "The --upgrade option is deprecated. Use --install instead.\n");
set_mode(INSTALL_M, optarg);
break;
case 's':
set_store(optarg);
break;
case 'p':
semanage_set_root(optarg);
break;
case 'S':
set_store_root(optarg);
break;
case 'R':
reload = 1;
break;
case 'n':
no_reload = 1;
break;
case 'N':
no_reload = 1;
break;
case 'B':
build = 1;
break;
case 'D':
disable_dontaudit = 1;
break;
case 'P':
preserve_tunables = 1;
break;
case 'C':
ignore_module_cache = 1;
break;
case 'X':
set_mode(PRIORITY_M, optarg);
break;
case 'e':
set_mode(ENABLE_M, optarg);
break;
case 'd':
set_mode(DISABLE_M, optarg);
break;
case '?':
default:{
usage(argv[0]);
exit(1);
}
}
}
if ((build || reload) && num_commands) {
fprintf(stderr,
"build or reload should not be used with other commands\n");
usage(argv[0]);
exit(1);
}
if (num_commands == 0 && reload == 0 && build == 0) {
fprintf(stderr, "At least one mode must be specified.\n");
usage(argv[0]);
exit(1);
}
if (extract_selected == 0 && cil_hll_set == 1) {
fprintf(stderr, "--cil and --hll require a module to export with the --extract option.\n");
usage(argv[0]);
exit(1);
}
if (optind < argc) {
int mode = commands ? (int) commands[num_commands - 1].mode : -1;
/* if -i/u/r/E was the last command treat any remaining
* arguments as args. Will allow 'semodule -i *.pp' to
* work as expected.
*/
switch (mode) {
case INSTALL_M:
case REMOVE_M:
case EXTRACT_M:
case ENABLE_M:
case DISABLE_M:
while (optind < argc)
set_mode(mode, argv[optind++]);
break;
default:
fprintf(stderr, "unknown additional arguments:\n");
while (optind < argc)
fprintf(stderr, " %s", argv[optind++]);
fprintf(stderr, "\n\n");
usage(argv[0]);
exit(1);
}
}
}
int main(int argc, char *argv[])
{
int i, commit = 0;
int result;
int status = EXIT_FAILURE;
const char *genhomedirconargv[] = { "genhomedircon", "-B", "-n" };
create_signal_handlers();
if (strcmp(basename(argv[0]), "genhomedircon") == 0) {
argc = 3;
argv = (char **)genhomedirconargv;
}
parse_command_line(argc, argv);
if (build)
commit = 1;
sh = semanage_handle_create();
if (!sh) {
fprintf(stderr, "%s: Could not create semanage handle\n",
argv[0]);
goto cleanup_nohandle;
}
if (store) {
/* Set the store we want to connect to, before connecting.
* this will always set a direct connection now, an additional
* option will need to be used later to specify a policy server
* location */
semanage_select_store(sh, store, SEMANAGE_CON_DIRECT);
}
if (store_root) {
semanage_set_store_root(sh, store_root);
}
/* create store if necessary, for bootstrapping */
semanage_set_create_store(sh, 1);
if ((result = semanage_connect(sh)) < 0) {
fprintf(stderr, "%s: Could not connect to policy handler\n",
argv[0]);
goto cleanup;
}
if (reload) {
if ((result = semanage_reload_policy(sh)) < 0) {
fprintf(stderr, "%s: Could not reload policy\n",
argv[0]);
goto cleanup;
}
}
if (build) {
if ((result = semanage_begin_transaction(sh)) < 0) {
fprintf(stderr, "%s: Could not begin transaction: %s\n",
argv[0], errno ? strerror(errno) : "");
goto cleanup;
}
}
if ((result = semanage_set_default_priority(sh, priority)) != 0) {
fprintf(stderr,
"%s: Invalid priority %d (needs to be between 1 and 999)\n",
argv[0],
priority);
goto cleanup;
}
for (i = 0; i < num_commands; i++) {
enum client_modes mode = commands[i].mode;
char *mode_arg = commands[i].arg;
switch (mode) {
case INSTALL_M:{
if (verbose) {
printf
("Attempting to install module '%s':\n",
mode_arg);
}
result =
semanage_module_install_file(sh, mode_arg);
break;
}
case EXTRACT_M:{
semanage_module_info_t *extract_info = NULL;
semanage_module_key_t *modkey = NULL;
uint16_t curr_priority;
void *data = NULL;
size_t data_len = 0;
char output_path[PATH_MAX];
const char *output_name = NULL;
const char *lang_ext = NULL;
int rlen;
FILE *output_fd = NULL;
result = semanage_module_key_create(sh, &modkey);
if (result != 0) {
goto cleanup_extract;
}
result = semanage_module_key_set_name(sh, modkey, mode_arg);
if (result != 0) {
goto cleanup_extract;
}
if (priority_set == 0) {
result = semanage_module_get_module_info(sh, modkey, &extract_info);
if (result != 0) {
goto cleanup_extract;
}
semanage_module_info_get_priority(sh, extract_info, &curr_priority);
printf("Module '%s' does not exist at the default priority '%d'. "
"Extracting at highest existing priority '%d'.\n", mode_arg, priority, curr_priority);
priority = curr_priority;
}
result = semanage_module_key_set_priority(sh, modkey, priority);
if (result != 0) {
goto cleanup_extract;
}
if (verbose) {
printf
("Attempting to extract module '%s':\n",
mode_arg);
}
result = semanage_module_extract(sh, modkey, extract_cil, &data, &data_len, &extract_info);
if (result != 0) {
goto cleanup_extract;
}
if (extract_cil) {
lang_ext = "cil";
} else {
result = semanage_module_info_get_lang_ext(sh, extract_info, &lang_ext);
if (result != 0) {
goto cleanup_extract;
}
}
result = semanage_module_info_get_name(sh, extract_info, &output_name);
if (result != 0) {
goto cleanup_extract;
}
rlen = snprintf(output_path, PATH_MAX, "%s.%s", output_name, lang_ext);
if (rlen < 0 || rlen >= PATH_MAX) {
fprintf(stderr, "%s: Failed to generate output path.\n", argv[0]);
result = -1;
goto cleanup_extract;
}
if (access(output_path, F_OK) == 0) {
fprintf(stderr, "%s: %s is already extracted with extension %s.\n", argv[0], mode_arg, lang_ext);
result = -1;
goto cleanup_extract;
}
output_fd = fopen(output_path, "w");
if (output_fd == NULL) {
fprintf(stderr, "%s: Unable to open %s\n", argv[0], output_path);
result = -1;
goto cleanup_extract;
}
if (fwrite(data, 1, data_len, output_fd) < data_len) {
fprintf(stderr, "%s: Unable to write to %s\n", argv[0], output_path);
result = -1;
goto cleanup_extract;
}
cleanup_extract:
if (output_fd != NULL) {
fclose(output_fd);
}
if (data_len > 0) {
munmap(data, data_len);
}
semanage_module_info_destroy(sh, extract_info);
free(extract_info);
semanage_module_key_destroy(sh, modkey);
free(modkey);
break;
}
case CIL_M:
extract_cil = 1;
break;
case HLL_M:
extract_cil = 0;
break;
case REMOVE_M:{
if (verbose) {
printf
("Attempting to remove module '%s':\n",
mode_arg);
}
result = semanage_module_remove(sh, mode_arg);
if ( result == -2 ) {
continue;
}
break;
}
case LIST_M:{
semanage_module_info_t *modinfos = NULL;
int modinfos_len = 0;
semanage_module_info_t *m = NULL;
int j = 0;
if (verbose) {
printf
("Attempting to list active modules:\n");
}
if (mode_arg == NULL || strcmp(mode_arg, "standard") == 0) {
result = semanage_module_list(sh,
&modinfos,
&modinfos_len);
if (result < 0) goto cleanup_list;
if (modinfos_len == 0) {
printf("No modules.\n");
}
const char *name = NULL;
for (j = 0; j < modinfos_len; j++) {
m = semanage_module_list_nth(modinfos, j);
result = semanage_module_info_get_name(sh, m, &name);
if (result != 0) goto cleanup_list;
printf("%s\n", name);
}
}
else if (strcmp(mode_arg, "full") == 0) {
/* get the modules */
result = semanage_module_list_all(sh,
&modinfos,
&modinfos_len);
if (result != 0) goto cleanup_list;
if (modinfos_len == 0) {
printf("No modules.\n");
}
/* calculate column widths */
size_t column[4] = { 0, 0, 0, 0 };
/* fixed width columns */
column[0] = sizeof("000") - 1;
column[3] = sizeof("disabled") - 1;
/* variable width columns */
const char *tmp = NULL;
size_t size;
for (j = 0; j < modinfos_len; j++) {
m = semanage_module_list_nth(modinfos, j);
result = semanage_module_info_get_name(sh, m, &tmp);
if (result != 0) goto cleanup_list;
size = strlen(tmp);
if (size > column[1]) column[1] = size;
result = semanage_module_info_get_lang_ext(sh, m, &tmp);
if (result != 0) goto cleanup_list;
size = strlen(tmp);
if (size > column[3]) column[3] = size;
}
/* print out each module */
for (j = 0; j < modinfos_len; j++) {
uint16_t pri = 0;
const char *name = NULL;
int enabled = 0;
const char *lang_ext = NULL;
m = semanage_module_list_nth(modinfos, j);
result = semanage_module_info_get_priority(sh, m, &pri);
if (result != 0) goto cleanup_list;
result = semanage_module_info_get_name(sh, m, &name);
if (result != 0) goto cleanup_list;
result = semanage_module_info_get_enabled(sh, m, &enabled);
if (result != 0) goto cleanup_list;
result = semanage_module_info_get_lang_ext(sh, m, &lang_ext);
if (result != 0) goto cleanup_list;
printf("%0*u %-*s %-*s %-*s\n",
(int)column[0], pri,
(int)column[1], name,
(int)column[2], lang_ext,
(int)column[3], enabled ? "" : "disabled");
}
}
else {
result = -1;
}
cleanup_list:
for (j = 0; j < modinfos_len; j++) {
m = semanage_module_list_nth(modinfos, j);
semanage_module_info_destroy(sh, m);
}
free(modinfos);
break;
}
case PRIORITY_M:{
char *endptr = NULL;
priority = (uint16_t)strtoul(mode_arg, &endptr, 10);
priority_set = 1;
if ((result = semanage_set_default_priority(sh, priority)) != 0) {
fprintf(stderr,
"%s: Invalid priority %d (needs to be between 1 and 999)\n",
argv[0],
priority);
goto cleanup;
}
break;
}
case ENABLE_M:{
if (verbose) {
printf
("Attempting to enable module '%s':\n",
mode_arg);
}
semanage_module_key_t *modkey = NULL;
result = semanage_module_key_create(sh, &modkey);
if (result != 0) goto cleanup_enable;
result = semanage_module_key_set_name(sh, modkey, mode_arg);
if (result != 0) goto cleanup_enable;
result = semanage_module_set_enabled(sh, modkey, 1);
if (result != 0) goto cleanup_enable;
cleanup_enable:
semanage_module_key_destroy(sh, modkey);
free(modkey);
break;
}
case DISABLE_M:{
if (verbose) {
printf
("Attempting to disable module '%s':\n",
mode_arg);
}
semanage_module_key_t *modkey = NULL;
result = semanage_module_key_create(sh, &modkey);
if (result != 0) goto cleanup_disable;
result = semanage_module_key_set_name(sh, modkey, mode_arg);
if (result != 0) goto cleanup_disable;
result = semanage_module_set_enabled(sh, modkey, 0);
if (result != 0) goto cleanup_disable;
cleanup_disable:
semanage_module_key_destroy(sh, modkey);
free(modkey);
break;
}
default:{
fprintf(stderr,
"%s: Unknown mode specified.\n",
argv[0]);
usage(argv[0]);
goto cleanup;
}
}
commit += do_commit[mode];
if (result < 0) {
fprintf(stderr, "%s: Failed on %s!\n", argv[0],
mode_arg ? : "list");
goto cleanup;
} else if (verbose) {
printf("Ok: return value of %d.\n", result);
}
}
if (commit) {
if (verbose)
printf("Committing changes:\n");
if (no_reload)
semanage_set_reload(sh, 0);
if (build)
semanage_set_rebuild(sh, 1);
if (disable_dontaudit)
semanage_set_disable_dontaudit(sh, 1);
else if (build)
semanage_set_disable_dontaudit(sh, 0);
if (preserve_tunables)
semanage_set_preserve_tunables(sh, 1);
if (ignore_module_cache)
semanage_set_ignore_module_cache(sh, 1);
result = semanage_commit(sh);
}
if (result < 0) {
fprintf(stderr, "%s: Failed!\n", argv[0]);
goto cleanup;
} else if (commit && verbose) {
printf("Ok: transaction number %d.\n", result);
}
if (semanage_disconnect(sh) < 0) {
fprintf(stderr, "%s: Error disconnecting\n", argv[0]);
goto cleanup;
}
status = EXIT_SUCCESS;
cleanup:
if (semanage_is_connected(sh)) {
if (semanage_disconnect(sh) < 0) {
fprintf(stderr, "%s: Error disconnecting\n", argv[0]);
}
}
semanage_handle_destroy(sh);
cleanup_nohandle:
cleanup();
exit(status);
}