Skip to contentSkip to conversation

libsepol: reject avtab entries with invalid specifier

PATCH In review Christian Göttsche 2 participants 4 messages Nov 1, 2023 – Nov 7, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 libsepol: reject avtab entries with invalid specifier

Neverallow avtab entries are not supported (normal and extended). Reject them to avoid lookup confusions via avtab_search(), e.g. when searching for a invalid key of AVTAB_TRANSITION|AVTAB_NEVERALLOW and the result of only AVTAB_NEVERALLOW has no transition value.

Simplify the check for the number of specifiers by using the compiler popcount builtin (already used in libsepol).

Reported-by: oss-fuzz (issue 60568), caused at the time by the filetrans
prefix proposal

---
v2:
rebase after revert of filename prefix proposal

---
 libsepol/src/avtab.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -7
index 6ab49c5e..1ef5ee00 100644
@@ -441,7 +441,6 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
 	avtab_key_t key;
 	avtab_datum_t datum;
 	avtab_extended_perms_t xperms;
-	unsigned set;
 	unsigned int i;
 	int rc;
 
@@ -535,13 +534,13 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
 	key.target_class = le16_to_cpu(buf16[items++]);
 	key.specified = le16_to_cpu(buf16[items++]);
 
-	set = 0;
-	for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
-		if (key.specified & spec_order[i])
-			set++;
+	if (key.specified & ~(AVTAB_AV | AVTAB_TYPE | AVTAB_XPERMS | AVTAB_ENABLED)) {
+		ERR(fp->handle, "invalid specifier");
+		return -1;
 	}
-	if (!set || set > 1) {
-		ERR(fp->handle, "more than one specifier");
+
+	if (__builtin_popcount(key.specified & ~AVTAB_ENABLED) != 1) {
+		ERR(fp->handle, "not exactly one specifier");
 		return -1;
 	}
 
Signature · 1 line
2.42.0
#1 lore raw

PATCH v2 2/2 libsepol: avtab: check read counts for saturation

Ensure counts are not set to the maximum value of their type. Also limit their size during fuzzing to prevent OOM reports.

Reported-by: oss-fuzz (issue 60572), caused at the time by the filetrans
prefix proposal

---
v2:
rebase after revert of filename prefix proposal

---
 libsepol/src/avtab.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/avtab.c +1 -1
index 1ef5ee00..7c2328b7 100644
@@ -600,7 +600,7 @@ int avtab_read(avtab_t * a, struct policy_file *fp, uint32_t vers)
 		goto bad;
 	}
 	nel = le32_to_cpu(buf[0]);
-	if (!nel) {
+	if (zero_or_saturated(nel)) {
 		ERR(fp->handle, "table is empty");
 		goto bad;
 	}
Signature · 1 line
2.42.0
Acked-by on v2 1/2
#2 lore raw
In reply to Christian Göttsche
Quoted 6 lines · Christian Göttsche

Neverallow avtab entries are not supported (normal and extended). Reject them to avoid lookup confusions via avtab_search(), e.g. when searching for a invalid key of AVTAB_TRANSITION|AVTAB_NEVERALLOW and the result of only AVTAB_NEVERALLOW has no transition value.

Quoted 6 lines

Simplify the check for the number of specifiers by using the compiler popcount builtin (already used in libsepol).

Reported-by: oss-fuzz (issue 60568), caused at the time by the filetrans
prefix proposal

For these two patches:

Quoted 42 lines

---
v2:
rebase after revert of filename prefix proposal

---
 libsepol/src/avtab.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -8
index 6ab49c5e..1ef5ee00 100644
@@ -441,7 +441,6 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
        avtab_key_t key;
        avtab_datum_t datum;
        avtab_extended_perms_t xperms;
-       unsigned set;
        unsigned int i;
        int rc;
 
@@ -535,13 +534,13 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
        key.target_class = le16_to_cpu(buf16[items++]);
        key.specified = le16_to_cpu(buf16[items++]);
 
-       set = 0;
-       for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
-               if (key.specified & spec_order[i])
-                       set++;
+       if (key.specified & ~(AVTAB_AV | AVTAB_TYPE | AVTAB_XPERMS | AVTAB_ENABLED)) {
+               ERR(fp->handle, "invalid specifier");
+               return -1;
        }
-       if (!set || set > 1) {
-               ERR(fp->handle, "more than one specifier");
+
+       if (__builtin_popcount(key.specified & ~AVTAB_ENABLED) != 1) {
+               ERR(fp->handle, "not exactly one specifier");
                return -1;
        }
 
--

2.42.0

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

Neverallow avtab entries are not supported (normal and extended). Reject them to avoid lookup confusions via avtab_search(), e.g. when searching for a invalid key of AVTAB_TRANSITION|AVTAB_NEVERALLOW and the result of only AVTAB_NEVERALLOW has no transition value.

Simplify the check for the number of specifiers by using the compiler popcount builtin (already used in libsepol).

Quoted 7 lines
Quoted 3 lines

Reported-by: oss-fuzz (issue 60568), caused at the time by the filetrans
prefix proposal

For these two patches:

These two patches have been merged.
Thanks,
Jim

Quoted 42 lines
Quoted 42 lines

---
v2:
rebase after revert of filename prefix proposal

---
 libsepol/src/avtab.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -8
index 6ab49c5e..1ef5ee00 100644
@@ -441,7 +441,6 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
        avtab_key_t key;
        avtab_datum_t datum;
        avtab_extended_perms_t xperms;
-       unsigned set;
        unsigned int i;
        int rc;
 
@@ -535,13 +534,13 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
        key.target_class = le16_to_cpu(buf16[items++]);
        key.specified = le16_to_cpu(buf16[items++]);
 
-       set = 0;
-       for (i = 0; i < ARRAY_SIZE(spec_order); i++) {
-               if (key.specified & spec_order[i])
-                       set++;
+       if (key.specified & ~(AVTAB_AV | AVTAB_TYPE | AVTAB_XPERMS | AVTAB_ENABLED)) {
+               ERR(fp->handle, "invalid specifier");
+               return -1;
        }
-       if (!set || set > 1) {
-               ERR(fp->handle, "more than one specifier");
+
+       if (__builtin_popcount(key.specified & ~AVTAB_ENABLED) != 1) {
+               ERR(fp->handle, "not exactly one specifier");
                return -1;
        }
 
--

2.42.0