Skip to contentSkip to conversation

libsepol: reject out-of-range OCON_PORT field at read time

PATCH In review Stephen Smalley 1 participant 5 messages Sep 15 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/4 libsepol: reject out-of-range OCON_PORT field at read time

The on-disk portcon record encodes protocol, low_port, and high_port as 32-bit values, but ocontext_t stores them as uint8_t / uint_16_t. ocontext_read_selinux() currently narrows without a range check, so a crafted or corrupt policy with e.g. protocol 0x106 is silently read as IPPROTO_TCP. policydb_validate() then sees only the truncated values and accepts the entry, and checkpolicy -b re-emits it as protocol 6, changing a binary the kernel would have rejected into one that will load with a different meaning.

The kernel reader already bounds all three fields (U8_MAX / U16_MAX) before the narrowing store, and OCON_IBPKEY / OCON_IBENDPORT do the same in this function. Apply the same pattern to OCON_PORT.

---
 libsepol/src/policydb.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
libsepol/src/policydb.c +16 -4
index 551bdad9..6a9254f3 100644
@@ -2741,17 +2741,29 @@ static int ocontext_read_selinux(const struct policydb_compat_info *info,
 					return -1;
 				break;
 			}
-			case OCON_PORT:
+			case OCON_PORT: {
+				uint32_t proto, port_lo, port_hi;
+
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
 				if (rc < 0)
 					return -1;
-				c->u.port.protocol = le32_to_cpu(buf[0]);
-				c->u.port.low_port = le32_to_cpu(buf[1]);
-				c->u.port.high_port = le32_to_cpu(buf[2]);
+
+				proto = le32_to_cpu(buf[0]);
+				port_lo = le32_to_cpu(buf[1]);
+				port_hi = le32_to_cpu(buf[2]);
+
+				if (proto > UINT8_MAX || port_lo > UINT16_MAX ||
+				    port_hi > UINT16_MAX)
+					return -1;
+
+				c->u.port.protocol = proto;
+				c->u.port.low_port = port_lo;
+				c->u.port.high_port = port_hi;
 				if (context_read_and_validate(&c->context[0], p,
 							      fp))
 					return -1;
 				break;
+			}
 			case OCON_NODE:
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
 				if (rc < 0)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/4 libsepol,checkpolicy: reject out-of-range pirqcon values

ocontext_t stores u.pirq as uint16_t, matching the Xen Flask security server, but the on-disk field is 32 bits and both policy compilers accept a full 32-bit integer for pirqcon. A source policy with "pirqcon 65537" is silently emitted as pirq 1, and reading a crafted Xen policy binary with a 32-bit pirq truncates likewise.

Bound the value to UINT16_MAX in the binary reader and in both compilers before the narrowing store, mirroring the OCON_IBPKEY / OCON_IBENDPORT pattern.

---
 checkpolicy/policy_define.c      |  5 +++++
 libsepol/cil/src/cil_build_ast.c |  6 ++++++
 libsepol/src/policydb.c          | 10 +++++++++-
 3 files changed, 20 insertions(+), 1 deletion(-)
checkpolicy/policy_define.c +5 -0
index 726060c7..fbacdaf4 100644
@@ -4923,6 +4923,11 @@ int define_pirq_context(unsigned int pirq)
 		return 0;
 	}
 
+	if (pirq > 0xffff) {
+		yyerror2("pirq %u out of range", pirq);
+		return -1;
+	}
+
 	newc = calloc(1, sizeof(ocontext_t));
 	if (!newc) {
 		yyerror("out of memory");
libsepol/cil/src/cil_build_ast.c +6 -0
index e4f6af7b..4cc9ccf1 100644
@@ -4859,6 +4859,12 @@ int cil_gen_pirqcon(struct cil_db *db, struct cil_tree_node *parse_current,
 		goto exit;
 	}
 
+	if (pirqcon->pirq > 0xffff) {
+		cil_log(CIL_ERR, "Invalid pirq %u\n", pirqcon->pirq);
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+
 	if (parse_current->next->next->cl_head == NULL) {
 		pirqcon->context_str = parse_current->next->next->data;
 	} else {
libsepol/src/policydb.c +9 -1
index 6a9254f3..60707fb4 100644
@@ -2571,15 +2571,23 @@ static int ocontext_read_xen(const struct policydb_compat_info *info,
 							      fp))
 					return -1;
 				break;
-			case OCON_XEN_PIRQ:
+			case OCON_XEN_PIRQ: {
+				uint32_t pirq;
+
 				rc = next_entry(buf, fp, sizeof(uint32_t));
 				if (rc < 0)
 					return -1;
+
+				pirq = le32_to_cpu(buf[0]);
+				if (pirq > UINT16_MAX)
+					return -1;
+				c->u.pirq = pirq;
 				c->u.pirq = le32_to_cpu(buf[0]);
 				if (context_read_and_validate(&c->context[0], p,
 							      fp))
 					return -1;
 				break;
+			}
 			case OCON_XEN_IOPORT:
 				rc = next_entry(buf, fp, sizeof(uint32_t) * 2);
 				if (rc < 0)
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 3/4 libsepol/cil: reject control characters in ocontext name fields

In reply to Stephen Smalley

The CIL lexer accepts a quoted string containing any byte other than NUL, newline, or the closing quote, and cil_gen_netifcon(), cil_gen_fsuse(), cil_gen_genfscon(), and cil_gen_ibendportcon() copy the resulting string into the corresponding policydb ocontext name field verbatim. policydb_validate() rejects control characters in these four fields, so a policy such as:

    (netifcon "e\x01th0" ...)

is compiled to a binary that libsepol itself refuses to read back, breaking any tool that operates on the loaded policy.

The classic policy compiler cannot emit such names since the relevant lexer tokens (identifier / filesystem / netifname) do not accept control characters. Bring the CIL compiler to parity by rejecting them in these four name fields at build_ast time, so a compiled policy is always accepted by the validator.

The genfscon path and the filecon / typetransition name arguments are intentionally not covered here; those correspond to pathnames and can embed control characters.

---
 libsepol/cil/src/cil_build_ast.c | 37 ++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
libsepol/cil/src/cil_build_ast.c +37 -0
index 4cc9ccf1..eb1537d3 100644
@@ -56,6 +56,22 @@ struct cil_args_build {
 	struct cil_tree_node *boolif;
 };
 
+static int cil_verify_ocon_name(const char *name, const char *what)
+{
+	const unsigned char *s;
+
+	for (s = (const unsigned char *)name; *s; s++) {
+		if (iscntrl(*s)) {
+			cil_log(CIL_ERR,
+				"Invalid character 0x%02x in %s name\n", *s,
+				what);
+			return SEPOL_ERR;
+		}
+	}
+
+	return SEPOL_OK;
+}
+
 static int cil_fill_list(struct cil_tree_node *current, enum cil_flavor flavor,
 			 struct cil_list **list)
 {
@@ -4622,6 +4638,11 @@ int cil_gen_genfscon(struct cil_db *db, struct cil_tree_node *parse_current,
 	genfscon->fs_str = parse_current->next->data;
 	genfscon->path_str = parse_current->next->next->data;
 
+	rc = cil_verify_ocon_name(genfscon->fs_str, "genfscon filesystem");
+	if (rc != SEPOL_OK) {
+		goto exit;
+	}
+
 	if (parse_current->next->next->next->next) {
 		/* (genfscon <FS_STR> <PATH_STR> <FILE_TYPE> ... */
 		char *file_type = parse_current->next->next->next->data;
@@ -4715,6 +4736,12 @@ int cil_gen_netifcon(struct cil_db *db, struct cil_tree_node *parse_current,
 
 	netifcon->interface_str = parse_current->next->data;
 
+	rc = cil_verify_ocon_name(netifcon->interface_str,
+				  "netifcon interface");
+	if (rc != SEPOL_OK) {
+		goto exit;
+	}
+
 	if (parse_current->next->next->cl_head == NULL) {
 		netifcon->if_context_str = parse_current->next->next->data;
 	} else {
@@ -4792,6 +4819,11 @@ int cil_gen_ibendportcon(__attribute__((unused)) struct cil_db *db,
 
 	ibendportcon->dev_name_str = parse_current->next->data;
 
+	rc = cil_verify_ocon_name(ibendportcon->dev_name_str,
+				  "ibendportcon device");
+	if (rc != SEPOL_OK)
+		goto exit;
+
 	rc = cil_fill_integer(parse_current->next->next, &ibendportcon->port,
 			      10);
 	if (rc != SEPOL_OK) {
@@ -5241,6 +5273,11 @@ int cil_gen_fsuse(struct cil_db *db, struct cil_tree_node *parse_current,
 
 	fsuse->fs_str = parse_current->next->next->data;
 
+	rc = cil_verify_ocon_name(fsuse->fs_str, "fsuse filesystem");
+	if (rc != SEPOL_OK) {
+		goto exit;
+	}
+
 	if (parse_current->next->next->next->cl_head == NULL) {
 		fsuse->context_str = parse_current->next->next->next->data;
 	} else {
Signature · 1 line
2.55.0
author maintainer
#3 lore raw

PATCH 4/4 libsepol: validate port range in port_from_record()

In reply to Stephen Smalley

sepol_port_t stores the low/high port as int, but ocontext_t.u.port uses uint16_t. port_from_record() copied the record values into the ocontext without a range check and then compared low > high on the already-truncated 16-bit fields, so a record with low=65537 high=65538 was silently stored as ports 1-2. A low port of 0 or a negative value was likewise accepted.

Apply the same port validation used for portcon ranges in libsepol, checkpolicy, and python/semanage to cover sepol_port_modify() callers and libsemanage local-port merges.

---
 libsepol/src/ports.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
libsepol/src/ports.c +4 -5
index 28e5a44d..5bbeb3c5 100644
@@ -75,13 +75,12 @@ static int port_from_record(sepol_handle_t *handle, const policydb_t *policydb,
 	tmp_port->u.port.protocol = tmp_proto;
 
 	/* Port range */
-	tmp_port->u.port.low_port = low;
-	tmp_port->u.port.high_port = high;
-	if (tmp_port->u.port.low_port > tmp_port->u.port.high_port) {
-		ERR(handle, "low port %d exceeds high port %d",
-		    tmp_port->u.port.low_port, tmp_port->u.port.high_port);
+	if (low < 1 || low > high || high > 0xffff) {
+		ERR(handle, "invalid port range %d-%d", low, high);
 		goto err;
 	}
+	tmp_port->u.port.low_port = low;
+	tmp_port->u.port.high_port = high;
 
 	/* Context */
 	if (context_from_record(handle, policydb, &tmp_con,
Signature · 1 line
2.55.0
author maintainer
#4 lore raw
In reply to Stephen Smalley
Quoted 10 lines · Stephen Smalley

The on-disk portcon record encodes protocol, low_port, and high_port as 32-bit values, but ocontext_t stores them as uint8_t / uint_16_t. ocontext_read_selinux() currently narrows without a range check, so a crafted or corrupt policy with e.g. protocol 0x106 is silently read as IPPROTO_TCP. policydb_validate() then sees only the truncated values and accepts the entry, and checkpolicy -b re-emits it as protocol 6, changing a binary the kernel would have rejected into one that will load with a different meaning.

Quoted 5 lines

The kernel reader already bounds all three fields (U8_MAX / U16_MAX) before the narrowing store, and OCON_IBPKEY / OCON_IBENDPORT do the same in this function. Apply the same pattern to OCON_PORT.

This series has been merged.