Skip to contentSkip to conversation

checkpolicy: fix xperm complement at range boundaries

PATCH In review Stephen Smalley 1 participant 2 messages Jun 22 – Jun 23 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 checkpolicy: fix xperm complement at range boundaries

From: netliomax25-code <[email protected]>

1. avrule_omit_xperms() builds the complement of an omitted ("~") extended permission range set and advanced the next allowed range start with r->range.high + 1 kept in a uint16_t.
2. when an omitted range ended at 0xffff the increment wrapped to 0, so a spurious [0,0xffff] range was appended and the omitted value was granted; an omit range starting at 0 with no successor left the trailing range high unset, producing a malformed low > high range.
3. reworked it to walk the sorted, merged ranges with a uint32_t cursor and emit each gap, so the 0 and 0xffff boundaries are handled.

Added a roundtrip regression in tests/policy_allonce.conf covering ~0xffff and ~0x0.

---
 checkpolicy/policy_define.c                   | 63 +++++++++++--------
 checkpolicy/tests/policy_allonce.conf         |  2 +
 .../tests/policy_allonce.expected.conf        |  4 ++
 .../tests/policy_allonce.expected_opt.conf    |  4 ++
 4 files changed, 47 insertions(+), 26 deletions(-)
checkpolicy/policy_define.c +37 -26
index 63bf5245..d7576dd4 100644
@@ -2092,48 +2092,59 @@ error:
 /* flip to included ranges */
 static int avrule_omit_xperms(struct av_xperm_range_list **rangehead)
 {
-	struct av_xperm_range_list *rnew, *r, *newhead, *r2;
+	struct av_xperm_range_list *rnew, *r, *newhead = NULL, *tail = NULL;
+	uint32_t next_low = 0;
 
-	rnew = calloc(1, sizeof(struct av_xperm_range_list));
-	if (!rnew)
-		goto error;
-
-	newhead = rnew;
-
-	r = *rangehead;
-	r2 = newhead;
-
-	if (r->range.low == 0) {
-		r2->range.low = r->range.high + 1;
-		r = r->next;
-	} else {
-		r2->range.low = 0;
+	/*
+	 * The input ranges are sorted and merged. Emit the gaps between them
+	 * as the complement. next_low is the first value not yet covered by an
+	 * emitted (allowed) range; promote it to uint32_t so that high + 1 does
+	 * not wrap when a range ends at 0xffff.
+	 */
+	for (r = *rangehead; r != NULL; r = r->next) {
+		if (r->range.low > next_low) {
+			rnew = calloc(1, sizeof(struct av_xperm_range_list));
+			if (!rnew)
+				goto error;
+			rnew->range.low = next_low;
+			rnew->range.high = r->range.low - 1;
+			if (tail)
+				tail->next = rnew;
+			else
+				newhead = rnew;
+			tail = rnew;
+		}
+		next_low = (uint32_t)r->range.high + 1;
 	}
 
-	while (r) {
-		r2->range.high = r->range.low - 1;
+	if (next_low <= UINT16_MAX) {
 		rnew = calloc(1, sizeof(struct av_xperm_range_list));
 		if (!rnew)
 			goto error;
-		r2->next = rnew;
-		r2 = r2->next;
-
-		r2->range.low = r->range.high + 1;
-		if (!r->next)
-			r2->range.high = 0xffff;
-		r = r->next;
+		rnew->range.low = next_low;
+		rnew->range.high = UINT16_MAX;
+		if (tail)
+			tail->next = rnew;
+		else
+			newhead = rnew;
+		tail = rnew;
 	}
 
 	r = *rangehead;
 	while (r != NULL) {
-		r2 = r;
+		rnew = r;
 		r = r->next;
-		free(r2);
+		free(rnew);
 	}
 	*rangehead = newhead;
 	return 0;
 
 error:
+	while (newhead != NULL) {
+		rnew = newhead;
+		newhead = newhead->next;
+		free(rnew);
+	}
 	yyerror("out of memory");
 	return -1;
 }
checkpolicy/tests/policy_allonce.conf +2 -0
index 4115dea2..263237fa 100644
@@ -43,6 +43,8 @@ dontaudit TYPE1 { TYPE2 TYPE3 } : CLASS3 { PERM1 CPERM1 };
 neverallow TYPE1 TYPE2 : { CLASS2 CLASS3 } { CPERM1 };
 allowxperm TYPE1 TYPE2 : CLASS1 ioctl { 0x456-0x5678 };
 allowxperm TYPE2 TYPE1 : CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4 : CLASS1 ioctl ~0xffff;
+allowxperm TYPE4 TYPE3 : CLASS1 ioctl ~0x0;
 auditallowxperm TYPE1 TYPE2 : CLASS1 ioctl 0x2;
 dontauditxperm TYPE1 TYPE2 : CLASS1 ioctl 0x3;
 neverallowxperm TYPE1 TYPE2 : CLASS1 ioctl 0x4;
checkpolicy/tests/policy_allonce.expected.conf +4 -0
index a88d8785..c1100992 100644
@@ -44,6 +44,10 @@ allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x456-0x4ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x500-0x55ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x5600-0x5678 };
 allowxperm TYPE2 TYPE1:CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0x0-0xfeff };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0xff00-0xfffe };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x1-0xff };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x100-0xffff };
 auditallowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x2 };
 dontauditxperm TYPE1 TYPE2:CLASS1 ioctl { 0x3 };
 type_transition TYPE1 TYPE2:CLASS1 TYPE3;
checkpolicy/tests/policy_allonce.expected_opt.conf +4 -0
index 3d21c310..2510c91a 100644
@@ -44,6 +44,10 @@ allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x456-0x4ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x500-0x55ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x5600-0x5678 };
 allowxperm TYPE2 TYPE1:CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0x0-0xfeff };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0xff00-0xfffe };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x1-0xff };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x100-0xffff };
 auditallowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x2 };
 dontauditxperm TYPE1 TYPE2:CLASS1 ioctl { 0x3 };
 type_transition TYPE1 TYPE2:CLASS1 TYPE3;
Signature · 1 line
2.54.0
author maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 13 lines · Stephen Smalley

From: netliomax25-code <[email protected]>

1. avrule_omit_xperms() builds the complement of an omitted ("~") extended permission range set and advanced the next allowed range start with r->range.high + 1 kept in a uint16_t.
2. when an omitted range ended at 0xffff the increment wrapped to 0, so a spurious [0,0xffff] range was appended and the omitted value was granted; an omit range starting at 0 with no successor left the trailing range high unset, producing a malformed low > high range.
3. reworked it to walk the sorted, merged ranges with a uint32_t cursor and emit each gap, so the 0 and 0xffff boundaries are handled.

Quoted 5 lines

Added a roundtrip regression in tests/policy_allonce.conf covering ~0xffff and ~0x0.

Quoted 143 lines
---
 checkpolicy/policy_define.c                   | 63 +++++++++++--------
 checkpolicy/tests/policy_allonce.conf         |  2 +
 .../tests/policy_allonce.expected.conf        |  4 ++
 .../tests/policy_allonce.expected_opt.conf    |  4 ++
 4 files changed, 47 insertions(+), 26 deletions(-)
checkpolicy/policy_define.c +37 -26
index 63bf5245..d7576dd4 100644
@@ -2092,48 +2092,59 @@ error:
 /* flip to included ranges */
 static int avrule_omit_xperms(struct av_xperm_range_list **rangehead)
 {
-       struct av_xperm_range_list *rnew, *r, *newhead, *r2;
+       struct av_xperm_range_list *rnew, *r, *newhead = NULL, *tail = NULL;
+       uint32_t next_low = 0;
 
-       rnew = calloc(1, sizeof(struct av_xperm_range_list));
-       if (!rnew)
-               goto error;
-
-       newhead = rnew;
-
-       r = *rangehead;
-       r2 = newhead;
-
-       if (r->range.low == 0) {
-               r2->range.low = r->range.high + 1;
-               r = r->next;
-       } else {
-               r2->range.low = 0;
+       /*
+        * The input ranges are sorted and merged. Emit the gaps between them
+        * as the complement. next_low is the first value not yet covered by an
+        * emitted (allowed) range; promote it to uint32_t so that high + 1 does
+        * not wrap when a range ends at 0xffff.
+        */
+       for (r = *rangehead; r != NULL; r = r->next) {
+               if (r->range.low > next_low) {
+                       rnew = calloc(1, sizeof(struct av_xperm_range_list));
+                       if (!rnew)
+                               goto error;
+                       rnew->range.low = next_low;
+                       rnew->range.high = r->range.low - 1;
+                       if (tail)
+                               tail->next = rnew;
+                       else
+                               newhead = rnew;
+                       tail = rnew;
+               }
+               next_low = (uint32_t)r->range.high + 1;
        }
 
-       while (r) {
-               r2->range.high = r->range.low - 1;
+       if (next_low <= UINT16_MAX) {
                rnew = calloc(1, sizeof(struct av_xperm_range_list));
                if (!rnew)
                        goto error;
-               r2->next = rnew;
-               r2 = r2->next;
-
-               r2->range.low = r->range.high + 1;
-               if (!r->next)
-                       r2->range.high = 0xffff;
-               r = r->next;
+               rnew->range.low = next_low;
+               rnew->range.high = UINT16_MAX;
+               if (tail)
+                       tail->next = rnew;
+               else
+                       newhead = rnew;
+               tail = rnew;
        }
 
        r = *rangehead;
        while (r != NULL) {
-               r2 = r;
+               rnew = r;
                r = r->next;
-               free(r2);
+               free(rnew);
        }
        *rangehead = newhead;
        return 0;
 
 error:
+       while (newhead != NULL) {
+               rnew = newhead;
+               newhead = newhead->next;
+               free(rnew);
+       }
        yyerror("out of memory");
        return -1;
 }
checkpolicy/tests/policy_allonce.conf +2 -0
index 4115dea2..263237fa 100644
@@ -43,6 +43,8 @@ dontaudit TYPE1 { TYPE2 TYPE3 } : CLASS3 { PERM1 CPERM1 };
 neverallow TYPE1 TYPE2 : { CLASS2 CLASS3 } { CPERM1 };
 allowxperm TYPE1 TYPE2 : CLASS1 ioctl { 0x456-0x5678 };
 allowxperm TYPE2 TYPE1 : CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4 : CLASS1 ioctl ~0xffff;
+allowxperm TYPE4 TYPE3 : CLASS1 ioctl ~0x0;
 auditallowxperm TYPE1 TYPE2 : CLASS1 ioctl 0x2;
 dontauditxperm TYPE1 TYPE2 : CLASS1 ioctl 0x3;
 neverallowxperm TYPE1 TYPE2 : CLASS1 ioctl 0x4;
checkpolicy/tests/policy_allonce.expected.conf +4 -0
index a88d8785..c1100992 100644
@@ -44,6 +44,10 @@ allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x456-0x4ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x500-0x55ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x5600-0x5678 };
 allowxperm TYPE2 TYPE1:CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0x0-0xfeff };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0xff00-0xfffe };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x1-0xff };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x100-0xffff };
 auditallowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x2 };
 dontauditxperm TYPE1 TYPE2:CLASS1 ioctl { 0x3 };
 type_transition TYPE1 TYPE2:CLASS1 TYPE3;
checkpolicy/tests/policy_allonce.expected_opt.conf +4 -1
index 3d21c310..2510c91a 100644
@@ -44,6 +44,10 @@ allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x456-0x4ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x500-0x55ff };
 allowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x5600-0x5678 };
 allowxperm TYPE2 TYPE1:CLASS4 nlmsg { 0x1 0x12 };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0x0-0xfeff };
+allowxperm TYPE3 TYPE4:CLASS1 ioctl { 0xff00-0xfffe };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x1-0xff };
+allowxperm TYPE4 TYPE3:CLASS1 ioctl { 0x100-0xffff };
 auditallowxperm TYPE1 TYPE2:CLASS1 ioctl { 0x2 };
 dontauditxperm TYPE1 TYPE2:CLASS1 ioctl { 0x3 };
 type_transition TYPE1 TYPE2:CLASS1 TYPE3;
--

2.54.0