Skip to content

Commit b1d9456

Browse files
committed
checkpolicy: Add support for generating CIL
Add support to checkpolicy and checkmodule for generating CIL as their output. Add new options "-C" and "--cil" to specify CIL as the output format. Signed-off-by: James Carter <[email protected]>
1 parent 4514332 commit b1d9456

File tree

4 files changed

+105
-56
lines changed

4 files changed

+105
-56
lines changed

checkpolicy/checkmodule.8

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
checkmodule \- SELinux policy module compiler
44
.SH SYNOPSIS
55
.B checkmodule
6-
.I "[\-h] [\-b] [\-m] [\-M] [\-U handle_unknown ] [\-V] [\-o output_file] [input_file]"
6+
.I "[\-h] [\-b] [\-C] [\-m] [\-M] [\-U handle_unknown ] [\-V] [\-o output_file] [input_file]"
77
.SH "DESCRIPTION"
88
This manual page describes the
99
.BR checkmodule
@@ -25,6 +25,9 @@ the module package into the module store and load the resulting policy.
2525
Read an existing binary policy module file rather than a source policy
2626
module file. This option is a development/debugging aid.
2727
.TP
28+
.B \-C,\-\-cil
29+
Write CIL policy file rather than binary policy file.
30+
.TP
2831
.B \-h,\-\-help
2932
Print usage.
3033
.TP

checkpolicy/checkmodule.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <errno.h>
2121
#include <sys/mman.h>
2222

23+
#include <sepol/module_to_cil.h>
2324
#include <sepol/policydb/policydb.h>
2425
#include <sepol/policydb/services.h>
2526
#include <sepol/policydb/conditional.h>
@@ -108,20 +109,9 @@ static int read_binary_policy(policydb_t * p, const char *file, const char *prog
108109
return 0;
109110
}
110111

111-
static int write_binary_policy(policydb_t * p, const char *file, char *progname)
112+
static int write_binary_policy(policydb_t * p, FILE *outfp)
112113
{
113-
FILE *outfp = NULL;
114114
struct policy_file pf;
115-
int ret;
116-
117-
printf("%s: writing binary representation (version %d) to %s\n",
118-
progname, policyvers, file);
119-
120-
outfp = fopen(file, "w");
121-
if (!outfp) {
122-
perror(file);
123-
exit(1);
124-
}
125115

126116
p->policy_type = policy_type;
127117
p->policyvers = policyvers;
@@ -130,24 +120,19 @@ static int write_binary_policy(policydb_t * p, const char *file, char *progname)
130120
policy_file_init(&pf);
131121
pf.type = PF_USE_STDIO;
132122
pf.fp = outfp;
133-
ret = policydb_write(p, &pf);
134-
if (ret) {
135-
fprintf(stderr, "%s: error writing %s\n", progname, file);
136-
return -1;
137-
}
138-
fclose(outfp);
139-
return 0;
123+
return policydb_write(p, &pf);
140124
}
141125

142126
static void usage(char *progname)
143127
{
144-
printf("usage: %s [-h] [-V] [-b] [-U handle_unknown] [-m] [-M] [-o FILE] [INPUT]\n", progname);
128+
printf("usage: %s [-h] [-V] [-b] [-C] [-U handle_unknown] [-m] [-M] [-o FILE] [INPUT]\n", progname);
145129
printf("Build base and policy modules.\n");
146130
printf("Options:\n");
147131
printf(" INPUT build module from INPUT (else read from \"%s\")\n",
148132
txtfile);
149133
printf(" -V show policy versions created by this program\n");
150134
printf(" -b treat input as a binary policy file\n");
135+
printf(" -C output CIL policy instead of binary policy\n");
151136
printf(" -h print usage\n");
152137
printf(" -U OPTION How to handle unknown classes and permissions\n");
153138
printf(" deny: Deny unknown kernel checks\n");
@@ -162,7 +147,7 @@ static void usage(char *progname)
162147
int main(int argc, char **argv)
163148
{
164149
const char *file = txtfile, *outfile = NULL;
165-
unsigned int binary = 0;
150+
unsigned int binary = 0, cil = 0;
166151
int ch;
167152
int show_version = 0;
168153
policydb_t modpolicydb;
@@ -173,10 +158,11 @@ int main(int argc, char **argv)
173158
{"version", no_argument, NULL, 'V'},
174159
{"handle-unknown", required_argument, NULL, 'U'},
175160
{"mls", no_argument, NULL, 'M'},
161+
{"cil", no_argument, NULL, 'C'},
176162
{NULL, 0, NULL, 0}
177163
};
178164

179-
while ((ch = getopt_long(argc, argv, "ho:bVU:mM", long_options, NULL)) != -1) {
165+
while ((ch = getopt_long(argc, argv, "ho:bVU:mMC", long_options, NULL)) != -1) {
180166
switch (ch) {
181167
case 'h':
182168
usage(argv[0]);
@@ -212,6 +198,9 @@ int main(int argc, char **argv)
212198
case 'M':
213199
mlspol = 1;
214200
break;
201+
case 'C':
202+
cil = 1;
203+
break;
215204
default:
216205
usage(argv[0]);
217206
}
@@ -269,7 +258,7 @@ int main(int argc, char **argv)
269258
}
270259
}
271260

272-
if (modpolicydb.policy_type == POLICY_BASE) {
261+
if (modpolicydb.policy_type == POLICY_BASE && !cil) {
273262
/* Verify that we can successfully expand the base module. */
274263
policydb_t kernpolicydb;
275264

@@ -295,10 +284,37 @@ int main(int argc, char **argv)
295284

296285
printf("%s: policy configuration loaded\n", argv[0]);
297286

298-
if (outfile &&
299-
write_binary_policy(&modpolicydb, outfile, argv[0]) == -1) {
287+
if (outfile) {
288+
FILE *outfp = fopen(outfile, "w");
289+
290+
if (!outfp) {
291+
perror(outfile);
292+
exit(1);
293+
}
294+
295+
if (!cil) {
296+
printf("%s: writing binary representation (version %d) to %s\n",
297+
argv[0], policyvers, file);
298+
299+
if (write_binary_policy(&modpolicydb, outfp) != 0) {
300+
fprintf(stderr, "%s: error writing %s\n", argv[0], outfile);
301+
exit(1);
302+
}
303+
} else {
304+
printf("%s: writing CIL to %s\n",argv[0], outfile);
305+
306+
if (sepol_module_policydb_to_cil(outfp, &modpolicydb, 0) != 0) {
307+
fprintf(stderr, "%s: error writing %s\n", argv[0], outfile);
308+
exit(1);
309+
}
310+
}
311+
312+
fclose(outfp);
313+
} else if (cil) {
314+
fprintf(stderr, "%s: No file to write CIL was specified\n", argv[0]);
300315
exit(1);
301316
}
317+
302318
policydb_destroy(&modpolicydb);
303319

304320
return 0;

checkpolicy/checkpolicy.8

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
checkpolicy \- SELinux policy compiler
44
.SH SYNOPSIS
55
.B checkpolicy
6-
.I "[\-b] [\-d] [\-M] [\-c policyvers] [\-o output_file] [input_file]"
6+
.I "[\-b] [\-C] [\-d] [\-M] [\-c policyvers] [\-o output_file] [input_file]"
77
.br
88
.SH "DESCRIPTION"
99
This manual page describes the
@@ -21,6 +21,9 @@ policy.conf or policy, depending on whether the \-b flag is specified.
2121
.B \-b,\-\-binary
2222
Read an existing binary policy file rather than a source policy.conf file.
2323
.TP
24+
.B \-C,\-\-cil
25+
Write CIL policy file rather than binary policy file.
26+
.TP
2427
.B \-d,\-\-debug
2528
Enter debug mode after loading the policy.
2629
.TP

checkpolicy/checkpolicy.c

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <ctype.h>
7575
#endif
7676

77+
#include <sepol/module_to_cil.h>
7778
#include <sepol/policydb/policydb.h>
7879
#include <sepol/policydb/services.h>
7980
#include <sepol/policydb/conditional.h>
@@ -104,7 +105,7 @@ unsigned int policyvers = POLICYDB_VERSION_MAX;
104105
void usage(char *progname)
105106
{
106107
printf
107-
("usage: %s [-b] [-d] [-U handle_unknown (allow,deny,reject)] [-M]"
108+
("usage: %s [-b] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M]"
108109
"[-c policyvers (%d-%d)] [-o output_file] [-t target_platform (selinux,xen)]"
109110
"[input_file]\n",
110111
progname, POLICYDB_VERSION_MIN, POLICYDB_VERSION_MAX);
@@ -376,6 +377,7 @@ static int check_level(hashtab_key_t key, hashtab_datum_t datum, void *arg __att
376377

377378
int main(int argc, char **argv)
378379
{
380+
policydb_t parse_policy;
379381
sepol_security_class_t tclass;
380382
sepol_security_id_t ssid, tsid, *sids, oldsid, newsid, tasksid;
381383
sepol_security_context_t scontext;
@@ -386,7 +388,7 @@ int main(int argc, char **argv)
386388
size_t scontext_len, pathlen;
387389
unsigned int i;
388390
unsigned int protocol, port;
389-
unsigned int binary = 0, debug = 0;
391+
unsigned int binary = 0, debug = 0, cil = 0;
390392
struct val_to_name v;
391393
int ret, ch, fd, target = SEPOL_TARGET_SELINUX;
392394
unsigned int nel, uret;
@@ -408,11 +410,12 @@ int main(int argc, char **argv)
408410
{"version", no_argument, NULL, 'V'},
409411
{"handle-unknown", required_argument, NULL, 'U'},
410412
{"mls", no_argument, NULL, 'M'},
413+
{"cil", no_argument, NULL, 'C'},
411414
{"help", no_argument, NULL, 'h'},
412415
{NULL, 0, NULL, 0}
413416
};
414417

415-
while ((ch = getopt_long(argc, argv, "o:t:dbU:MVc:h", long_options, NULL)) != -1) {
418+
while ((ch = getopt_long(argc, argv, "o:t:dbU:MCVc:h", long_options, NULL)) != -1) {
416419
switch (ch) {
417420
case 'o':
418421
outfile = optarg;
@@ -455,6 +458,9 @@ int main(int argc, char **argv)
455458
case 'M':
456459
mlspol = 1;
457460
break;
461+
case 'C':
462+
cil = 1;
463+
break;
458464
case 'c':{
459465
long int n;
460466
errno = 0;
@@ -505,6 +511,11 @@ int main(int argc, char **argv)
505511
sepol_set_sidtab(&sidtab);
506512

507513
if (binary) {
514+
if (cil) {
515+
fprintf(stderr, "%s: Converting kernel policy to CIL is not supported\n",
516+
argv[0]);
517+
exit(1);
518+
}
508519
fd = open(file, O_RDONLY);
509520
if (fd < 0) {
510521
fprintf(stderr, "Can't open '%s': %s\n",
@@ -557,8 +568,6 @@ int main(int argc, char **argv)
557568
}
558569
}
559570
} else {
560-
policydb_t parse_policy;
561-
562571
if (policydb_init(&parse_policy))
563572
exit(1);
564573
/* We build this as a base policy first since that is all the parser understands */
@@ -577,23 +586,24 @@ int main(int argc, char **argv)
577586
if (hashtab_map(policydbp->p_levels.table, check_level, NULL))
578587
exit(1);
579588

580-
if (policydb_init(&policydb)) {
581-
fprintf(stderr, "%s: policydb_init failed\n", argv[0]);
582-
exit(1);
583-
}
584-
585589
/* Linking takes care of optional avrule blocks */
586-
if (link_modules(NULL, &parse_policy, NULL, 0, 0)) {
590+
if (link_modules(NULL, policydbp, NULL, 0, 0)) {
587591
fprintf(stderr, "Error while resolving optionals\n");
588592
exit(1);
589593
}
590594

591-
if (expand_module(NULL, &parse_policy, &policydb, 0, 1)) {
592-
fprintf(stderr, "Error while expanding policy\n");
593-
exit(1);
595+
if (!cil) {
596+
if (policydb_init(&policydb)) {
597+
fprintf(stderr, "%s: policydb_init failed\n", argv[0]);
598+
exit(1);
599+
}
600+
if (expand_module(NULL, policydbp, &policydb, 0, 1)) {
601+
fprintf(stderr, "Error while expanding policy\n");
602+
exit(1);
603+
}
604+
policydb_destroy(policydbp);
605+
policydbp = &policydb;
594606
}
595-
policydb_destroy(&parse_policy);
596-
policydbp = &policydb;
597607
}
598608

599609
if (policydb_load_isids(&policydb, &sidtab))
@@ -602,29 +612,46 @@ int main(int argc, char **argv)
602612
printf("%s: policy configuration loaded\n", argv[0]);
603613

604614
if (outfile) {
605-
printf
606-
("%s: writing binary representation (version %d) to %s\n",
607-
argv[0], policyvers, outfile);
608615
outfp = fopen(outfile, "w");
609616
if (!outfp) {
610617
perror(outfile);
611618
exit(1);
612619
}
613620

614-
policydb.policy_type = POLICY_KERN;
615621
policydb.policyvers = policyvers;
616622

617-
policy_file_init(&pf);
618-
pf.type = PF_USE_STDIO;
619-
pf.fp = outfp;
620-
ret = policydb_write(&policydb, &pf);
621-
if (ret) {
622-
fprintf(stderr, "%s: error writing %s\n",
623-
argv[0], outfile);
624-
exit(1);
623+
if (!cil) {
624+
printf
625+
("%s: writing binary representation (version %d) to %s\n",
626+
argv[0], policyvers, outfile);
627+
policydb.policy_type = POLICY_KERN;
628+
629+
policy_file_init(&pf);
630+
pf.type = PF_USE_STDIO;
631+
pf.fp = outfp;
632+
ret = policydb_write(&policydb, &pf);
633+
if (ret) {
634+
fprintf(stderr, "%s: error writing %s\n",
635+
argv[0], outfile);
636+
exit(1);
637+
}
638+
} else {
639+
printf("%s: writing CIL to %s\n",argv[0], outfile);
640+
ret = sepol_module_policydb_to_cil(outfp, policydbp, 1);
641+
if (ret) {
642+
fprintf(stderr, "%s: error writing %s\n", argv[0], outfile);
643+
exit(1);
644+
}
645+
}
646+
647+
if (outfile) {
648+
fclose(outfp);
625649
}
626-
fclose(outfp);
650+
} else if (cil) {
651+
fprintf(stderr, "%s: No file to write CIL was specified\n", argv[0]);
652+
exit(1);
627653
}
654+
628655
if (!debug) {
629656
policydb_destroy(&policydb);
630657
exit(0);

0 commit comments

Comments
 (0)