Skip to contentSkip to conversation

libsepol,checkpolicy: add --multiple-decls option

PATCH In review Inseob Kim 2 participants 3 messages Sep 28 SELinux security module

lore
Share & tools
lore

Inseob Kim

author
#0 lore raw

PATCH v2 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 SYM_TYPES is added to that allowlist, behind a new policydb flag. Like role attributes, type aliases are excluded, as in secilc; otherwise add_aliases_to_type() would silently turn a declared type into an alias of another type. The merge needs nothing else:
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.

---
v2:
- Reject multiple declarations where either the new or the existing symbol is a type alias, following the secilc -m behavior

---
 checkpolicy/checkpolicy.8                  |  3 +++
 checkpolicy/checkpolicy.c                  | 16 +++++++++++--
 libsepol/include/sepol/policydb/policydb.h |  2 ++
 libsepol/src/policydb.c                    | 28 ++++++++++++++++++++--
 4 files changed, 45 insertions(+), 4 deletions(-)
checkpolicy/checkpolicy.8 +3 -0
index caaaf67..b37d0ff 100644
@@ -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
index 1ab9598..cc866b3 100644
@@ -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
index bf948d9..ae125fd 100644
@@ -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 +26 -2
index 9716f5f..fbfc0bf 100644
@@ -1385,6 +1385,12 @@ int policydb_load_isids(policydb_t *p, sidtab_t *s)
 	return 0;
 }
 
+static int type_is_alias(const type_datum_t *type)
+{
+	return type->flavor == TYPE_ALIAS ||
+	       (type->flavor == TYPE_TYPE && !type->primary);
+}
+
 /* Declare a symbol for a certain avrule_block context.  Insert it
  * into a symbol table for a policy.  This function will handle
  * inserting the appropriate scope information in addition to
@@ -1456,8 +1462,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
@@ -1478,6 +1487,21 @@ int symtab_insert(policydb_t *pol, uint32_t sym, hashtab_key_t key,
 				return -2;
 			}
 		}
+		if (sym == SYM_TYPES) {
+			type_datum_t *base_type;
+			type_datum_t *cur_type = (type_datum_t *)datum;
+
+			base_type = (type_datum_t *)hashtab_search(
+				pol->symtab[sym].table, key);
+			assert(base_type != NULL);
+
+			if (type_is_alias(base_type) ||
+			    type_is_alias(cur_type)) {
+				/* Only regular types and attributes are
+				 * allowed to have multiple declarations. */
+				return -2;
+			}
+		}
 	} else if (scope_datum->scope == SCOPE_REQ && scope == SCOPE_DECL) {
 		scope_datum->scope = SCOPE_DECL;
 	}
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 23 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 SYM_TYPES is added to that allowlist, behind a new policydb flag. Like role attributes, type aliases are excluded, as in secilc; otherwise add_aliases_to_type() would silently turn a declared type into an alias of another type. The merge needs nothing else:
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.

Quoted 1 line
maintainer
#2 lore raw
Quoted 25 lines · Stephen Smalley
Quoted 23 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 SYM_TYPES is added to that allowlist, behind a new policydb flag. Like role attributes, type aliases are excluded, as in secilc; otherwise add_aliases_to_type() would silently turn a declared type into an alias of another type. The merge needs nothing else:
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.

Quoted 3 lines
Quoted 1 line

This has been merged.