Skip to contentSkip to conversation

libsepol: invert only valid range of role bitmap

PATCH In review Christian Göttsche 2 participants 3 messages Jan 25, 2022 – Jan 27, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: invert only valid range of role bitmap

The length of an ebitmap is the current highest allocated (not set) bit and always a multiple of MAPTYPE (= 64). The role ebitmap should only have valid role bits set, even after inverting. The length might be smaller than the maximum number of defined roles leading to non defined role bits set afterwards.
Only invert up to the number of roles defined instead the full ebitmap length, similar to type_set_expand().

This also avoids timeouts on an invalid huge highbit set, since the ebitmap has not been validated yet, on which inverting will take excessive amount of memory and time, found by oss-fuzz (#43709).

---
This patch supersedes "libsepol: reject invalid roles before inverting"
https://patchwork.kernel.org/project/selinux/patch/[email protected]/

---
 libsepol/src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/expand.c +1 -1
index 898e6b87..df8683ef 100644
@@ -2481,7 +2481,7 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_t
 
 	/* if role is to be complimented, invert the entire bitmap here */
 	if (x->flags & ROLE_COMP) {
-		for (i = 0; i < ebitmap_length(r); i++) {
+		for (i = 0; i < p->p_roles.nprim; i++) {
 			if (ebitmap_get_bit(r, i)) {
 				if (ebitmap_set_bit(r, i, 0))
 					return -1;
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Christian Göttsche

The length of an ebitmap is the current highest allocated (not set) bit and always a multiple of MAPTYPE (= 64). The role ebitmap should only have valid role bits set, even after inverting. The length might be smaller than the maximum number of defined roles leading to non defined role bits set afterwards.
Only invert up to the number of roles defined instead the full ebitmap length, similar to type_set_expand().

Quoted 5 lines

This also avoids timeouts on an invalid huge highbit set, since the ebitmap has not been validated yet, on which inverting will take excessive amount of memory and time, found by oss-fuzz (#43709).

Quoted 23 lines

---
This patch supersedes "libsepol: reject invalid roles before inverting"
https://patchwork.kernel.org/project/selinux/patch/[email protected]/

---
 libsepol/src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/expand.c +1 -2
index 898e6b87..df8683ef 100644
@@ -2481,7 +2481,7 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_t
 
        /* if role is to be complimented, invert the entire bitmap here */
        if (x->flags & ROLE_COMP) {
-               for (i = 0; i < ebitmap_length(r); i++) {
+               for (i = 0; i < p->p_roles.nprim; i++) {
                        if (ebitmap_get_bit(r, i)) {
                                if (ebitmap_set_bit(r, i, 0))
                                        return -1;
--

2.34.1

#2 lore raw
Quoted 12 lines · James Carter
Quoted 9 lines · Christian Göttsche

The length of an ebitmap is the current highest allocated (not set) bit and always a multiple of MAPTYPE (= 64). The role ebitmap should only have valid role bits set, even after inverting. The length might be smaller than the maximum number of defined roles leading to non defined role bits set afterwards.
Only invert up to the number of roles defined instead the full ebitmap length, similar to type_set_expand().

Quoted 8 lines
Quoted 5 lines

This also avoids timeouts on an invalid huge highbit set, since the ebitmap has not been validated yet, on which inverting will take excessive amount of memory and time, found by oss-fuzz (#43709).

Merged.
Thanks,
Jim

Quoted 23 lines
Quoted 23 lines

---
This patch supersedes "libsepol: reject invalid roles before inverting"
https://patchwork.kernel.org/project/selinux/patch/[email protected]/

---
 libsepol/src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/expand.c +1 -2
index 898e6b87..df8683ef 100644
@@ -2481,7 +2481,7 @@ int role_set_expand(role_set_t * x, ebitmap_t * r, policydb_t * out, policydb_t
 
        /* if role is to be complimented, invert the entire bitmap here */
        if (x->flags & ROLE_COMP) {
-               for (i = 0; i < ebitmap_length(r); i++) {
+               for (i = 0; i < p->p_roles.nprim; i++) {
                        if (ebitmap_get_bit(r, i)) {
                                if (ebitmap_set_bit(r, i, 0))
                                        return -1;
--

2.34.1