libsepol: free memory on str_read() failures
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.
--- libsepol/src/avtab.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/avtab.c +2 -2
@@ -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
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).
--- libsepol/src/avtab.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
libsepol/src/avtab.c +6 -7
@@ -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
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 onlyAVTAB_NEVERALLOWhas 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)Signed-off-by Christian Göttsche <[email protected]>--- libsepol/src/avtab.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)libsepol/src/avtab.c +6
-8@@ -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
PATCH 3/5 libsepol: free ebitmap in filename_trans_comp_read_one()
In reply to Christian Göttscheebitmap_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
@@ -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
PATCH 4/5 libsepol: drop superfluous check
In reply to Christian Göttschestr_read() internally performs a sanity length check.
--- libsepol/src/policydb.c | 2 -- 1 file changed, 2 deletions(-)
libsepol/src/policydb.c +0 -2
@@ -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
PATCH 5/5 libsepol: avtab: check read counts for saturation
In reply to Christian GöttscheEnsure counts are not set to the maximum value of their type. Also limit their size during fuzzing to prevent OOM reports.
--- libsepol/src/avtab.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
libsepol/src/avtab.c +8 -1
@@ -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
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)Signed-off-by Christian Göttsche <[email protected]>--- libsepol/src/avtab.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)libsepol/src/avtab.c +2
-0@@ -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