Skip to contentSkip to conversation

libsepol: free memory on str_read() failures

PATCH In review Christian Göttsche 2 participants 7 messages Jul 13, 2023 – Aug 4, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol: free memory on str_read() failures

If str_read() fails to read the next entry it has already allocated memory for it.
Free the passed pointer also in the error case.

Reported-by oss-fuzz (issue 60567)
---
 libsepol/src/avtab.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/avtab.c +2 -2
index 99fdaa87..5c76fe12 100644
@@ -857,7 +857,7 @@ static int filename_trans_read_one(avtab_t *a, void *fp)
 	len = le32_to_cpu(*buf);
 	rc = str_read(&name, fp, len);
 	if (rc < 0)
-		return SEPOL_ERR;
+		goto err;
 
 	/* read stype, ttype, tclass and otype */
 	rc = next_entry(buf, fp, sizeof(uint32_t) * 4);
@@ -898,7 +898,7 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp)
 	len = le32_to_cpu(*buf);
 	rc = str_read(&name, fp, len);
 	if (rc < 0)
-		return SEPOL_ERR;
+		goto err;
 
 	/* read ttype, tclass, ndatum */
 	rc = next_entry(buf, fp, sizeof(uint32_t) * 3);
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/5 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)
---
 libsepol/src/avtab.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -7
index 5c76fe12..7b85519b 100644
@@ -564,7 +564,6 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
 	avtab_datum_t datum;
 	avtab_trans_t trans;
 	avtab_extended_perms_t xperms;
-	unsigned set;
 	unsigned int i;
 	int rc;
 
@@ -666,13 +665,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.40.1
#2 lore raw

Re: [PATCH 2/5] libsepol: reject avtab entries with invalid specifier

Quoted 48 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).

Reported-by oss-fuzz (issue 60568)
---
 libsepol/src/avtab.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -8
index 5c76fe12..7b85519b 100644
@@ -564,7 +564,6 @@ int avtab_read_item(struct policy_file *fp, uint32_t vers, avtab_t * a,
        avtab_datum_t datum;
        avtab_trans_t trans;
        avtab_extended_perms_t xperms;
-       unsigned set;
        unsigned int i;
        int rc;
 
@@ -666,13 +665,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;
        }
 
--
Quoted 2 lines

2.40.1

Patches 1, 3, and 4 depended on the prefix/suffix patches and are no longer applicable.
But this one does seem like it is, but, of course, it will not longer apply cleanly.
Jim

#3 lore raw

PATCH 3/5 libsepol: free ebitmap in filename_trans_comp_read_one()

In reply to Christian Göttsche

ebitmap_read() might fail in between, but always calls ebitmap_init(), so ebitmap_destopy() is safe to call.

---
 libsepol/src/avtab.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libsepol/src/avtab.c +3 -1
index 7b85519b..9c7daf8e 100644
@@ -912,7 +912,7 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp)
 	for (i = 0; i < ndatum; i++) {
 		rc = ebitmap_read(&stypes, fp);
 		if (rc < 0)
-			goto err;
+			goto err_ebitmap;
 
 		rc = next_entry(buf, fp, sizeof(uint32_t));
 		if (rc < 0)
@@ -928,6 +928,8 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp)
 			if (rc < 0)
 				goto err_ebitmap;
 		}
+
+		ebitmap_destroy(&stypes);
 	}
 
 	free(name);
Signature · 1 line
2.40.1
#4 lore raw

PATCH 4/5 libsepol: drop superfluous check

In reply to Christian Göttsche

str_read() internally performs a sanity length check.

---
 libsepol/src/policydb.c | 2 --
 1 file changed, 2 deletions(-)
libsepol/src/policydb.c +0 -2
index 552eb77a..e1f8bb06 100644
@@ -3560,8 +3560,6 @@ static int filename_trans_rule_read(policydb_t *p, avrule_t **r,
 			return -1;
 
 		len = le32_to_cpu(buf[0]);
-		if (zero_or_saturated(len))
-			return -1;
 
 		rc = str_read(&cur->object_name, fp, len);
 		if (rc)
Signature · 1 line
2.40.1
#5 lore raw

PATCH 5/5 libsepol: avtab: check read counts for saturation

In reply to Christian Göttsche

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)
---
 libsepol/src/avtab.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
libsepol/src/avtab.c +8 -1
index 9c7daf8e..cb2ca06a 100644
@@ -461,6 +461,8 @@ static int avtab_read_name_trans(policy_file_t *fp, symtab_t *target)
 	if (rc < 0)
 		return rc;
 	nel = le32_to_cpu(buf32[0]);
+	if (is_saturated(nel))
+		return -1;
 
 	rc = symtab_init(target, nel);
 	if (rc < 0)
@@ -736,7 +738,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;
 	}
@@ -909,6 +911,9 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp)
 	key.target_class = le32_to_cpu(buf[1]);
 
 	ndatum = le32_to_cpu(buf[2]);
+	if (is_saturated(ndatum))
+		goto err;
+
 	for (i = 0; i < ndatum; i++) {
 		rc = ebitmap_read(&stypes, fp);
 		if (rc < 0)
@@ -951,6 +956,8 @@ int avtab_filename_trans_read(void *fp, uint32_t vers, avtab_t *a)
 	if (rc < 0)
 		return rc;
 	nel = le32_to_cpu(*buf);
+	if (is_saturated(nel))
+		return -1;
 
 	if (vers < POLICYDB_VERSION_COMP_FTRANS) {
 		for (i = 0; i < nel; i++) {
Signature · 1 line
2.40.1
#6 lore raw

Re: [PATCH 5/5] libsepol: avtab: check read counts for saturation

Quoted 24 lines · Christian Göttsche

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)
---
 libsepol/src/avtab.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
libsepol/src/avtab.c +2 -0
index 9c7daf8e..cb2ca06a 100644
@@ -461,6 +461,8 @@ static int avtab_read_name_trans(policy_file_t *fp, symtab_t *target)
        if (rc < 0)
                return rc;
        nel = le32_to_cpu(buf32[0]);
+       if (is_saturated(nel))
+               return -1;
 
        rc = symtab_init(target, nel);
        if (rc < 0)

libsepol/src/avtab.c int avtab_read(avtab_t * a, struct policy_file *fp, uint32_t vers) · line 744

Quoted 8 lines
libsepol/src/avtab.c +1 -1
@@ -736,7 +738,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;
        }

The other three hunks depended on the prefix/suffix patches, but I think that this hunk might still be applicable.
Jim

Quoted 22 lines
libsepol/src/avtab.c +5 -1
@@ -909,6 +911,9 @@ static int filename_trans_comp_read_one(avtab_t *a, void *fp)
        key.target_class = le32_to_cpu(buf[1]);
 
        ndatum = le32_to_cpu(buf[2]);
+       if (is_saturated(ndatum))
+               goto err;
+
        for (i = 0; i < ndatum; i++) {
                rc = ebitmap_read(&stypes, fp);
                if (rc < 0)
@@ -951,6 +956,8 @@ int avtab_filename_trans_read(void *fp, uint32_t vers, avtab_t *a)
        if (rc < 0)
                return rc;
        nel = le32_to_cpu(*buf);
+       if (is_saturated(nel))
+               return -1;
 
        if (vers < POLICYDB_VERSION_COMP_FTRANS) {
                for (i = 0; i < nel; i++) {
--

2.40.1