Skip to contentSkip to conversation

libsepol: Fix expand_role_attributes_in_attributes()

PATCH In review James Carter 1 participant 2 messages Dec 17, 2025 – Jan 8 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: Fix expand_role_attributes_in_attributes()

Fix potential NULL dereference when there are gaps in the roles in the role_val_to_struct array.

Remove debugging messages and provide a better error message.

Checking "!done" is clearer than checking "reps < p->p_roles.nprim" for determining that there was a problem expanding role attributes.

---
 libsepol/src/expand.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
libsepol/src/expand.c +9 -10
index 04b5e5e4..74f03c87 100644
@@ -875,7 +875,7 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
 	ebitmap_init(&attrs);
 	for (i=0; i < p->p_roles.nprim; i++) {
 		rd = p->role_val_to_struct[i];
-		if (rd->flavor == ROLE_ATTRIB) {
+		if (rd && rd->flavor == ROLE_ATTRIB) {
 			ebitmap_set_bit(&attrs, i, 1);
 		}
 	}
@@ -887,9 +887,6 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
 			rd = p->role_val_to_struct[i];
 			if (ebitmap_match_any(&rd->roles, &attrs)) {
 				done = 0;
-				if (ebitmap_get_bit(&rd->roles, i)) {
-					ERR(handle, "Before: attr has own bit set: %d\n", i);
-				}
 				ebitmap_init(&roles);
 				ebitmap_for_each_positive_bit(&rd->roles, nj, j) {
 					if (ebitmap_get_bit(&attrs, j)) {
@@ -901,19 +898,21 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
 				ebitmap_union(&rd->roles, &roles);
 				ebitmap_destroy(&roles);
 				if (ebitmap_get_bit(&rd->roles, i)) {
-					ERR(handle, "After: attr has own bit set: %d\n", i);
-					done = 1; /* Just end early */
+					ERR(handle, "Found loop in role attributes involving: %s", p->p_role_val_to_name[i]);
+					ebitmap_destroy(&attrs);
+					return -1;
 				}
 			}
 		}
 	}
 
-	if (reps >= p->p_roles.nprim) {
-		ERR(handle, "Had to bail: reps = %u\n", reps);
-	}
-
 	ebitmap_destroy(&attrs);
 
+	if (!done) {
+		ERR(handle, "Failed to expand role attributes");
+		return -1;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.50.0
#1 lore raw
Quoted 10 lines · James Carter

Fix potential NULL dereference when there are gaps in the roles in the role_val_to_struct array.

Remove debugging messages and provide a better error message.

Checking "!done" is clearer than checking "reps < p->p_roles.nprim" for determining that there was a problem expanding role attributes.

This patch has been merged.
Jim

Quoted 58 lines
---
 libsepol/src/expand.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
libsepol/src/expand.c +9 -11
index 04b5e5e4..74f03c87 100644
@@ -875,7 +875,7 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
        ebitmap_init(&attrs);
        for (i=0; i < p->p_roles.nprim; i++) {
                rd = p->role_val_to_struct[i];
-               if (rd->flavor == ROLE_ATTRIB) {
+               if (rd && rd->flavor == ROLE_ATTRIB) {
                        ebitmap_set_bit(&attrs, i, 1);
                }
        }
@@ -887,9 +887,6 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
                        rd = p->role_val_to_struct[i];
                        if (ebitmap_match_any(&rd->roles, &attrs)) {
                                done = 0;
-                               if (ebitmap_get_bit(&rd->roles, i)) {
-                                       ERR(handle, "Before: attr has own bit set: %d\n", i);
-                               }
                                ebitmap_init(&roles);
                                ebitmap_for_each_positive_bit(&rd->roles, nj, j) {
                                        if (ebitmap_get_bit(&attrs, j)) {
@@ -901,19 +898,21 @@ static int expand_role_attributes_in_attributes(sepol_handle_t *handle, policydb
                                ebitmap_union(&rd->roles, &roles);
                                ebitmap_destroy(&roles);
                                if (ebitmap_get_bit(&rd->roles, i)) {
-                                       ERR(handle, "After: attr has own bit set: %d\n", i);
-                                       done = 1; /* Just end early */
+                                       ERR(handle, "Found loop in role attributes involving: %s", p->p_role_val_to_name[i]);
+                                       ebitmap_destroy(&attrs);
+                                       return -1;
                                }
                        }
                }
        }
 
-       if (reps >= p->p_roles.nprim) {
-               ERR(handle, "Had to bail: reps = %u\n", reps);
-       }
-
        ebitmap_destroy(&attrs);
 
+       if (!done) {
+               ERR(handle, "Failed to expand role attributes");
+               return -1;
+       }
+
        return 0;
 }
 
--

2.50.0