Skip to contentSkip to conversation

checkpolicy: validate policydb before writing

RFC PATCH In review Stephen Smalley 1 participant 2 messages Oct 13 – Oct 14 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

RFC PATCH v2 1/1 checkpolicy: validate policydb before writing

checkpolicy does not currently validate the policydb with policydb_validate() before writing it, which could consequently generate a policy file that can't be consumed by the readers. Validate and reject it before writing.

Only validate when the output policydb is a kernel policy. On the source -> CIL path (-C without -b) policydbp points at the POLICY_BASE parse_policy, which is parser-shaped rather than read-side-shaped:
classes with no access_vectors entry have a NULL permissions.table, initial SIDs with no assigned context carry a zero context, and policyvers is a kernel-range value. policydb_validate() assumes read-side invariants and would reject or crash on these; the parser is authoritative on source input.

For -b input policydb_read() has already run policydb_validate(), so this is only new coverage for the expand_module() output and for re-validation after -O or a -c version downgrade.

---
v2 only calls policydb_validate() when the output policy is a kernel policy, since policydb_validate() will reject a POLICY_BASE parse_policy and the parser is authoritative on source policies.

 checkpolicy/checkpolicy.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
checkpolicy/checkpolicy.c +19 -0
index cc866b3f..6ea7cdd6 100644
@@ -374,6 +374,8 @@ static int check_level(hashtab_key_t key, hashtab_datum_t datum,
 	return 0;
 }
 
+extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
+
 int main(int argc, char **argv)
 {
 	policydb_t parse_policy;
@@ -685,6 +687,23 @@ int main(int argc, char **argv)
 		}
 	}
 
+	/*
+	 * Validate the kernel policydb before writing it so we fail here
+	 * rather than producing a policy file that policydb_read() will
+	 * reject. The POLICY_BASE parse_policy on the source->CIL path is
+	 * not validated: policydb_validate() assumes read-side invariants
+	 * that the parser does not establish, and the parser is authoritative
+	 * on source input.
+	 */
+	if (policydbp->policy_type == POLICY_KERN) {
+		ret = policydb_validate(NULL, policydbp);
+		if (ret) {
+			fprintf(stderr, "%s: validation of policydb failed\n",
+				argv[0]);
+			exit(1);
+		}
+	}
+
 	if (outfile) {
 		if (!strcmp(outfile, "-")) {
 			outfp = stdout;
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 15 lines · Stephen Smalley

checkpolicy does not currently validate the policydb with policydb_validate() before writing it, which could consequently generate a policy file that can't be consumed by the readers. Validate and reject it before writing.

Only validate when the output policydb is a kernel policy. On the source -> CIL path (-C without -b) policydbp points at the POLICY_BASE parse_policy, which is parser-shaped rather than read-side-shaped:
classes with no access_vectors entry have a NULL permissions.table, initial SIDs with no assigned context carry a zero context, and policyvers is a kernel-range value. policydb_validate() assumes read-side invariants and would reject or crash on these; the parser is authoritative on source input.

Quoted 5 lines

For -b input policydb_read() has already run policydb_validate(), so this is only new coverage for the expand_module() output and for re-validation after -O or a -c version downgrade.

This patch has been merged.