Skip to contentSkip to conversation

libsepol,checkpolicy: validate devicetree paths

PATCH In review Stephen Smalley 1 participant 2 messages Sep 2 – Sep 4 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v3 1/1 libsepol,checkpolicy: validate devicetree paths

devicetreecon paths have their own validity rules specified by:
https://devicetree-specification.readthedocs.io/en/latest/index.html

libsepol was (correctly) preventing them from embedding control characters but checkpolicy was not. Provide helpers for validating devicetree paths according to the specification and use them in checkpolicy and libsepol to reject invalid devicetree paths.

---
v3 makes the helper functions inline to avoid creating duplicates for source files that include the header, adds one more validity check to the helper, and fixes a memory leak on the error path.

 checkpolicy/policy_define.c              |  21 +++--
 libsepol/include/sepol/policydb/dtpath.h | 100 +++++++++++++++++++++++
 libsepol/src/policydb_validate.c         |   3 +-
 3 files changed, 118 insertions(+), 6 deletions(-)
 create mode 100644 libsepol/include/sepol/policydb/dtpath.h
checkpolicy/policy_define.c +16 -5
index 1984c52f..c0f546a1 100644
@@ -54,6 +54,7 @@
 #include <sepol/policydb/conditional.h>
 #include <sepol/policydb/hierarchy.h>
 #include <sepol/policydb/polcaps.h>
+#include <sepol/policydb/dtpath.h>
 #include "queue.h"
 #include "module_compiler.h"
 #include "policy_define.h"
@@ -5136,6 +5137,7 @@ bad:
 int define_devicetree_context(void)
 {
 	ocontext_t *newc, *c, *l, *head;
+	char *path;
 
 	if (policydbp->target_platform != SEPOL_TARGET_XEN) {
 		yyerror("devicetreecon not supported for target");
@@ -5148,17 +5150,26 @@ int define_devicetree_context(void)
 		return 0;
 	}
 
+	path = (char *)queue_remove(id_queue);
+	if (!path) {
+		yyerror("Missing devicetreepath");
+		return -1;
+	}
+
+	if (!is_valid_dt_path(path)) {
+		yyerror2("invalid devicetreecon path \"%s\"", path);
+		free(path);
+		return -1;
+	}
+
 	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
+		free(path);
 		return -1;
 	}
 
-	newc->u.name = (char *)queue_remove(id_queue);
-	if (!newc->u.name) {
-		free(newc);
-		return -1;
-	}
+	newc->u.name = path;
 
 	if (parse_security_context(&newc->context[0])) {
 		free(newc->u.name);
libsepol/include/sepol/policydb/dtpath.h new file +100 -0

100 lines not on this page · raw message

libsepol/src/policydb_validate.c +2 -1
index 34182e8e..c60001c9 100644
@@ -5,6 +5,7 @@
 #include <sepol/policydb/polcaps.h>
 #include <sepol/policydb/policydb.h>
 #include <sepol/policydb/services.h>
+#include <sepol/policydb/dtpath.h>
 
 #include "debug.h"
 #include "private.h"
@@ -1613,7 +1614,7 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p,
 						goto bad;
 					break;
 				case OCON_XEN_DEVICETREE:
-					if (validate_string_field(octx->u.name))
+					if (!is_valid_dt_path(octx->u.name))
 						goto bad;
 					break;
 				}
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 9 lines · Stephen Smalley

devicetreecon paths have their own validity rules specified by:
https://devicetree-specification.readthedocs.io/en/latest/index.html

libsepol was (correctly) preventing them from embedding control characters but checkpolicy was not. Provide helpers for validating devicetree paths according to the specification and use them in checkpolicy and libsepol to reject invalid devicetree paths.

Quoted 5 lines

---
v3 makes the helper functions inline to avoid creating duplicates for source files that include the header, adds one more validity check to the helper, and fixes a memory leak on the error path.

This is now merged.