74
74
#include <ctype.h>
75
75
#endif
76
76
77
+ #include <sepol/module_to_cil.h>
77
78
#include <sepol/policydb/policydb.h>
78
79
#include <sepol/policydb/services.h>
79
80
#include <sepol/policydb/conditional.h>
@@ -104,7 +105,7 @@ unsigned int policyvers = POLICYDB_VERSION_MAX;
104
105
void usage (char * progname )
105
106
{
106
107
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]"
108
109
"[-c policyvers (%d-%d)] [-o output_file] [-t target_platform (selinux,xen)]"
109
110
"[input_file]\n" ,
110
111
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
376
377
377
378
int main (int argc , char * * argv )
378
379
{
380
+ policydb_t parse_policy ;
379
381
sepol_security_class_t tclass ;
380
382
sepol_security_id_t ssid , tsid , * sids , oldsid , newsid , tasksid ;
381
383
sepol_security_context_t scontext ;
@@ -386,7 +388,7 @@ int main(int argc, char **argv)
386
388
size_t scontext_len , pathlen ;
387
389
unsigned int i ;
388
390
unsigned int protocol , port ;
389
- unsigned int binary = 0 , debug = 0 ;
391
+ unsigned int binary = 0 , debug = 0 , cil = 0 ;
390
392
struct val_to_name v ;
391
393
int ret , ch , fd , target = SEPOL_TARGET_SELINUX ;
392
394
unsigned int nel , uret ;
@@ -408,11 +410,12 @@ int main(int argc, char **argv)
408
410
{"version" , no_argument , NULL , 'V' },
409
411
{"handle-unknown" , required_argument , NULL , 'U' },
410
412
{"mls" , no_argument , NULL , 'M' },
413
+ {"cil" , no_argument , NULL , 'C' },
411
414
{"help" , no_argument , NULL , 'h' },
412
415
{NULL , 0 , NULL , 0 }
413
416
};
414
417
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 ) {
416
419
switch (ch ) {
417
420
case 'o' :
418
421
outfile = optarg ;
@@ -455,6 +458,9 @@ int main(int argc, char **argv)
455
458
case 'M' :
456
459
mlspol = 1 ;
457
460
break ;
461
+ case 'C' :
462
+ cil = 1 ;
463
+ break ;
458
464
case 'c' :{
459
465
long int n ;
460
466
errno = 0 ;
@@ -505,6 +511,11 @@ int main(int argc, char **argv)
505
511
sepol_set_sidtab (& sidtab );
506
512
507
513
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
+ }
508
519
fd = open (file , O_RDONLY );
509
520
if (fd < 0 ) {
510
521
fprintf (stderr , "Can't open '%s': %s\n" ,
@@ -557,8 +568,6 @@ int main(int argc, char **argv)
557
568
}
558
569
}
559
570
} else {
560
- policydb_t parse_policy ;
561
-
562
571
if (policydb_init (& parse_policy ))
563
572
exit (1 );
564
573
/* 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)
577
586
if (hashtab_map (policydbp -> p_levels .table , check_level , NULL ))
578
587
exit (1 );
579
588
580
- if (policydb_init (& policydb )) {
581
- fprintf (stderr , "%s: policydb_init failed\n" , argv [0 ]);
582
- exit (1 );
583
- }
584
-
585
589
/* 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 )) {
587
591
fprintf (stderr , "Error while resolving optionals\n" );
588
592
exit (1 );
589
593
}
590
594
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 ;
594
606
}
595
- policydb_destroy (& parse_policy );
596
- policydbp = & policydb ;
597
607
}
598
608
599
609
if (policydb_load_isids (& policydb , & sidtab ))
@@ -602,29 +612,46 @@ int main(int argc, char **argv)
602
612
printf ("%s: policy configuration loaded\n" , argv [0 ]);
603
613
604
614
if (outfile ) {
605
- printf
606
- ("%s: writing binary representation (version %d) to %s\n" ,
607
- argv [0 ], policyvers , outfile );
608
615
outfp = fopen (outfile , "w" );
609
616
if (!outfp ) {
610
617
perror (outfile );
611
618
exit (1 );
612
619
}
613
620
614
- policydb .policy_type = POLICY_KERN ;
615
621
policydb .policyvers = policyvers ;
616
622
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 );
625
649
}
626
- fclose (outfp );
650
+ } else if (cil ) {
651
+ fprintf (stderr , "%s: No file to write CIL was specified\n" , argv [0 ]);
652
+ exit (1 );
627
653
}
654
+
628
655
if (!debug ) {
629
656
policydb_destroy (& policydb );
630
657
exit (0 );
0 commit comments