libsepol,checkpolicy: add --multiple-decls option
PATCH 1/1 libsepol,checkpolicy: add --multiple-decls option
Add a -m/--multiple-decls option to checkpolicy, mirroring the option of the same name in secilc, which allows a type or an attribute to be declared more than once. The declarations are merged into a single symbol whose attribute set is the union of the attribute sets of the individual declarations. Without the option a repeated declaration remains an error.
Policies assembled from several independently maintained sources may declare the same type in more than one of them. secilc merges such declarations when it links the resulting CIL files, but checkpolicy rejects them when the same sources are compiled in a single run.
symtab_insert() already permits multiple declarations of roles and users, so only SYM_TYPES is added to that allowlist, behind a new policydb flag. Nothing else is needed: create_type() returns the existing datum when the symbol exists and define_type() sets one attribute bit per declaration against it. Using the same identifier for both a type and an attribute remains an error, as does a repeated declaration of any other kind of symbol.
--- checkpolicy/checkpolicy.8 | 3 +++ checkpolicy/checkpolicy.c | 16 ++++++++++++++-- libsepol/include/sepol/policydb/policydb.h | 2 ++ libsepol/src/policydb.c | 7 +++++-- 4 files changed, 24 insertions(+), 4 deletions(-)
checkpolicy/checkpolicy.8 +3 -0
@@ -46,6 +46,9 @@ Output line markers for allow rules, in addition to neverallow rules. This optio of the output CIL policy file, but the additional line markers helps debugging, especially neverallow failure reports. Can only be used when writing a CIL policy file. .TP +.B \-m,\-\-multiple\-decls +Allow types and attributes to be re-declared. +.TP .B \-c policyvers Specify the policy version, defaults to the latest. .TP
checkpolicy/checkpolicy.c +14 -2
@@ -107,7 +107,7 @@ static __attribute__((__noreturn__)) void usage(const char *progname) {printf ("usage: %s [-b[F]] [-C] [-d] [-U handle_unknown (allow,deny,reject)] [-M] " "[-N] [-c policyvers (%d-%d)] [-o output_file|-] [-S] [-O] " -"[-t target_platform (selinux,xen)] [-E] [-V] [-L] [input_file]\n" , +"[-t target_platform (selinux,xen)] [-E] [-V] [-L] [-m] [input_file]\n" , progname,POLICYDB_VERSION_MIN ,POLICYDB_VERSION_MAX );exit (1 ); }
@@ -391,6 +391,7 @@ int main(int argc, char **argv)unsigned int binary =0 , debug =0 , sort =0 , cil =0 , conf =0 , optimize =0 , disable_neverallow =0 ;unsigned int line_marker_for_allow =0 ; +unsigned int multiple_decls =0 ;struct val_to_name v;int ret, ch, fd, target =SEPOL_TARGET_SELINUX ;unsigned int policyvers =0 ;
@@ -422,11 +423,12 @@ int main(int argc, char **argv) { "optimize", no_argument, NULL, 'O' }, { "werror", no_argument, NULL, 'E' }, { "line-marker-for-allow", no_argument, NULL, 'L' }, + { "multiple-decls", no_argument, NULL, 'm' }, { "help", no_argument, NULL, 'h' }, { NULL, 0, NULL, 0 } }; -while ((ch =getopt_long (argc, argv,"o:t:dbU:MNCFSVc:OELh" , +while ((ch =getopt_long (argc, argv,"o:t:dbU:MNCFSVc:OELmh" , long_options,NULL )) !=-1 ) {switch (ch) {case 'o' :
@@ -507,6 +509,9 @@ int main(int argc, char **argv)case 'L' : line_marker_for_allow =1 ;break ; +case 'm' : + multiple_decls =1 ; +break ;case 'h' :default :usage (argv[0 ]);
@@ -555,6 +560,12 @@ int main(int argc, char **argv)exit (1 ); } +if (multiple_decls && binary) { +fprintf (stderr, +"Multiple declarations are only meaningful when compiling source policy\n" ); +exit (1 ); + } +if (binary) { fd =open (file,O_RDONLY );if (fd <0 ) {
@@ -628,6 +639,7 @@ int main(int argc, char **argv) parse_policy.mls = mlspol; parse_policy.handle_unknown = handle_unknown; parse_policy.policyvers = policyvers; + parse_policy.multiple_decls = multiple_decls; policydbp = &parse_policy;
libsepol/include/sepol/policydb/policydb.h +2 -0
@@ -630,6 +630,8 @@ typedef struct policydb {/* avrules whose line markes will be printed. Defaults to neverallow and neverallowxperm */ uint32_t line_marker_avrules; + +int multiple_decls; } policydb_t;struct sepol_policydb {
libsepol/src/policydb.c +5 -2
@@ -1456,8 +1456,11 @@ int symtab_insert(policydb_t *pol, uint32_t sym, hashtab_key_t key,return rc; } }else if (scope_datum->scope ==SCOPE_DECL && scope ==SCOPE_DECL ) { -/* disallow multiple declarations for non-roles/users */ -if (sym !=SYM_ROLES && sym !=SYM_USERS ) { +/* disallow multiple declarations for non-roles/users, unless +* the policy opted in to multiple declarations of types and +* attributes */ +if (sym !=SYM_ROLES && sym !=SYM_USERS && + !(sym ==SYM_TYPES && pol->multiple_decls)) {return -2 ; }/* Further confine that a role attribute can't have the same
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Stephen Smalley
maintainerQuoted 29 lines · Inseob Kim
Add a -m/--multiple-decls option to checkpolicy, mirroring the option of the same name in secilc, which allows a type or an attribute to be declared more than once. The declarations are merged into a single symbol whose attribute set is the union of the attribute sets of the individual declarations. Without the option a repeated declaration remains an error.
Policies assembled from several independently maintained sources may declare the same type in more than one of them. secilc merges such declarations when it links the resulting CIL files, but checkpolicy rejects them when the same sources are compiled in a single run.
symtab_insert()already permits multiple declarations of roles and users, so onlySYM_TYPESis added to that allowlist, behind a new policydb flag. Nothing else is needed:create_type()returns the existing datum when the symbol exists anddefine_type()sets one attribute bit per declaration against it. Using the same identifier for both a type and an attribute remains an error, as does a repeated declaration of any other kind of symbol.Signed-off-by Inseob Kim <[email protected]>---
libsepol/src/policydb.c +0
-0
libsepol/src/policydb.c int symtab_insert(policydb_t *pol, uint32_t sym, hashtab_key_t key, · line 1463
Quoted 10 lines
libsepol/src/policydb.c +5
-2@@ -1456,8 +1456,11 @@ int symtab_insert(policydb_t *pol, uint32_t sym, hashtab_key_t key,return rc; } }else if (scope_datum->scope ==SCOPE_DECL && scope ==SCOPE_DECL ) { -/* disallow multiple declarations for non-roles/users */ -if (sym !=SYM_ROLES && sym !=SYM_USERS ) { +/* disallow multiple declarations for non-roles/users, unless +* the policy opted in to multiple declarations of types and +* attributes */ +if (sym !=SYM_ROLES && sym !=SYM_USERS && + !(sym ==SYM_TYPES && pol->multiple_decls)) {
sym == SYM_TYPES can also be true for type aliases. policy_define.c:add_aliases_to_type() case 1 was written for the require->decl path
(turn a required stub into an alias). Under -m it is now reachable from DECL/DECL and
does existing->flavor = TYPE_ALIAS; existing->primary = <T>;
type TYPE2;
allow TYPE2 self:CLASS1 PERM2;
typealias TYPE1 alias TYPE2;secilc -m rejects this via cil_allow_multiple_decls() f_new==f_old (CIL_TYPE != CIL_TYPEALIAS
at cil_build_ast.c:151. Reverse order (alias then type X;) is a harmless no-op; create_type() returns the alias datum with flavor==TYPE_TYPE(0)==isattr(0), define_type() has nothing to set.
Suggested fix: mirror the SYM_ROLES flavor guard in symtab_insert() - add_aliases_to_type()'s
incoming aliasdatum has s.value pre-set to the primary's value whereas create_type()'s incoming
datum has s.value==0 from type_datum_init() memset; reject ((type_datum_t*)datum)->s.value != 0
at the DECL/DECL_SYM_TYPES branch.
Or refuse in add_aliases_to_type() case 1 when the existing datum's scope was already DECL - butsymtab_insert() has already flipped/appended by then, so the former is cleaner.