Skip to contentSkip to conversation

checkpolicy: validate policydb before writing

RFC PATCH Superseded Stephen Smalley 1 participant 1 message Oct 13 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

RFC PATCH 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.

---
This patch depends on
https://lore.kernel.org/selinux/[email protected]/T/#u
since otherwise policydb_validate() fails after expand_module(). Similar patches could be created for checkmodule and secilc. I am unclear though as to why we haven't been doing this all along, which is why this is RFC-only at the moment. policydb_validate() is currently private to libsepol but since checkpolicy already requires the static libsepol it doesn't seem to be an onerous dependency to add this. If/when the libsepol public API for policy introspection lands, we could export policydb_validate() and use it more nicely.

 checkpolicy/checkpolicy.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
checkpolicy/checkpolicy.c +23 -0
index cc866b3f..18039b3a 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;
@@ -714,8 +716,22 @@ int main(int argc, char **argv)
 						exit(1);
 					}
 				}
+				ret = policydb_validate(NULL, &policydb);
+				if (ret) {
+					fprintf(stderr,
+						"%s: invalid policydb from %s\n",
+						argv[0], file);
+					exit(1);
+				}
 				ret = policydb_write(&policydb, &pf);
 			} else {
+				ret = policydb_validate(NULL, policydbp);
+				if (ret) {
+					fprintf(stderr,
+						"%s: invalid policydb from %s\n",
+						argv[0], file);
+					exit(1);
+				}
 				ret = sepol_kernel_policydb_to_conf(outfp,
 								    policydbp);
 			}
@@ -729,6 +745,13 @@ int main(int argc, char **argv)
 				policydbp->line_marker_avrules |=
 					AVRULE_ALLOWED | AVRULE_XPERMS_ALLOWED;
 			}
+			ret = policydb_validate(NULL, policydbp);
+			if (ret) {
+				fprintf(stderr,
+					"%s: invalid policydb from %s\n",
+					argv[0], file);
+				exit(1);
+			}
 			if (binary) {
 				ret = sepol_kernel_policydb_to_cil(outfp,
 								   policydbp);
Signature · 1 line
2.55.0