libselinux: fix potential NULL reference and memory leak in audit2why
PATCH 1/3 libselinux: fix potential NULL reference and memory leak in audit2why
In audit2why.c add return check for memory allocation. And free every element in the boollist when function fails.
--- libselinux/src/audit2why.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
libselinux/src/audit2why.c +24 -2
@@ -55,7 +55,16 @@ static int load_booleans(const sepol_bool_t * boolean, void *arg __attribute__ ((__unused__))) { boollist[boolcnt] =malloc (sizeof (struct boolean_t )); +if (!boollist[boolcnt]) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +return -1 ; + } boollist[boolcnt]->name =strdup (sepol_bool_get_name (boolean)); +if (!boollist[boolcnt]->name) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +free (boollist[boolcnt]); +return -1 ; + } boollist[boolcnt]->active =sepol_bool_get_value (boolean); boolcnt++;return 0 ;
@@ -149,6 +158,11 @@ static int check_booleans(struct boolean_t **bools)if (fcnt >0 ) { *bools =calloc (sizeof (struct boolean_t ), fcnt +1 ); +if (!*bools) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +free (foundlist); +return 0 ; + }struct boolean_t *b = *bools;for (i =0 ; i < fcnt; i++) {int ctr = foundlist[i];
@@ -278,14 +292,22 @@ static int __policy_init(const char *init_path)return 0 ; err: -if (boollist) -free (boollist); +if (boollist) { +for (i =0 ; i < boolcnt; i++) { +free (boollist[i]->name); +free (boollist[i]); + } +free (boollist); + boollist =NULL ; + boolcnt =0 ; + }if (avc){if (avc->handle)sepol_handle_destroy (avc->handle);if (avc->policydb)sepol_policydb_free (avc->policydb);free (avc); + avc =NULL ; }if (pf)sepol_policy_file_free (pf);
Signature · 1 line
2.27.0
PATCH 2/3 libsepol: fix potential memory leak in common_copy_callback
In common_copy_callback(), destroy new_common->permissions when the function fails.
--- libsepol/src/expand.c | 1 + 1 file changed, 1 insertion(+)
libsepol/src/expand.c +1 -0
@@ -297,6 +297,7 @@ static int common_copy_callback(hashtab_key_t key, hashtab_datum_t datum, (hashtab_datum_t) new_common);if (ret) {ERR (state->handle,"hashtab overflow" ); +symtab_destroy (&new_common->permissions);free (new_common);free (new_id);return -1 ;
Signature · 1 line
2.27.0
Re: [PATCH 2/3] libsepol: fix potential memory leak in common_copy_callback
Quoted 13 lines · Jie Lu
In
common_copy_callback(), destroynew_common->permissionswhen the function fails.Signed-off-by Jie Lu <[email protected]>--- libsepol/src/expand.c | 1 + 1 file changed, 1 insertion(+)libsepol/src/expand.c +0
-0
libsepol/src/expand.c static int common_copy_callback(hashtab_key_t key, hashtab_datum_t datum, · line 303
Quoted 7 lines
libsepol/src/expand.c +1
-0@@ -297,6 +297,7 @@ static int common_copy_callback(hashtab_key_t key, hashtab_datum_t datum, (hashtab_datum_t) new_common);if (ret) {ERR (state->handle,"hashtab overflow" ); +symtab_destroy (&new_common->permissions);free (new_common);free (new_id);return -1 ;
The call to hashtab_map() below this also needs similar cleanup if there is an error. In fact, this whole function really needs common cleanup code with a goto that code upon an error.
Thanks,
Jim
Quoted 3 lines
--
2.27.0
PATCH 3/3 policycoreutils: fix potential NULL reference in load_checks
In reply to Jie LuIn load_checks(), add return check for malloc() to avoid NULL reference.
--- policycoreutils/sestatus/sestatus.c | 4 ++++ 1 file changed, 4 insertions(+)
policycoreutils/sestatus/sestatus.c +4 -0
@@ -140,6 +140,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) pc[*npc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!pc[*npc]) +break ;memcpy (pc[*npc], bufp, buf_len); (*npc)++; bufp =NULL ;
@@ -150,6 +152,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) fc[*nfc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!fc[*nfc]) +break ;memcpy (fc[*nfc], bufp, buf_len); (*nfc)++; bufp =NULL ;
Signature · 1 line
2.27.0
Re: [PATCH 3/3] policycoreutils: fix potential NULL reference in load_checks
Quoted 4 lines · Jie Lu
In
load_checks(), add return check formalloc()to avoid NULL reference.Signed-off-by Jie Lu <[email protected]>
Quoted 29 lines
--- policycoreutils/sestatus/sestatus.c | 4 ++++ 1 file changed, 4 insertions(+)policycoreutils/sestatus/sestatus.c +4
-1@@ -140,6 +140,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) pc[*npc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!pc[*npc]) +break ;memcpy (pc[*npc], bufp, buf_len); (*npc)++; bufp =NULL ;@@ -150,6 +152,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) fc[*nfc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!fc[*nfc]) +break ;memcpy (fc[*nfc], bufp, buf_len); (*nfc)++; bufp =NULL ; --2.27.0
Re: [PATCH 3/3] policycoreutils: fix potential NULL reference in load_checks
Quoted 8 lines · James Carter
Quoted 4 lines · Jie Lu
In
load_checks(), add return check formalloc()to avoid NULL reference.Signed-off-by Jie Lu <[email protected]>Acked-by James Carter <[email protected]>
This patch, but not the other two, has been merged. Thanks,
Jim
Quoted 30 lines
Quoted 30 lines
--- policycoreutils/sestatus/sestatus.c | 4 ++++ 1 file changed, 4 insertions(+)policycoreutils/sestatus/sestatus.c +4
-1@@ -140,6 +140,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) pc[*npc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!pc[*npc]) +break ;memcpy (pc[*npc], bufp, buf_len); (*npc)++; bufp =NULL ;@@ -150,6 +152,8 @@ static void load_checks(char *pc[], int *npc, char *fc[], int *nfc) fc[*nfc] = (char *)malloc ((buf_len) *sizeof (char )); +if (!fc[*nfc]) +break ;memcpy (fc[*nfc], bufp, buf_len); (*nfc)++; bufp =NULL ; --2.27.0
Quoted 47 lines · Jie Lu
In
audit2why.cadd return check for memory allocation. And free every element in the boollist when function fails.Signed-off-by Jie Lu <[email protected]>--- libselinux/src/audit2why.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)libselinux/src/audit2why.c +14
-1@@ -55,7 +55,16 @@ static int load_booleans(const sepol_bool_t * boolean, void *arg __attribute__ ((__unused__))) { boollist[boolcnt] =malloc (sizeof (struct boolean_t )); +if (!boollist[boolcnt]) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +return -1 ; + } boollist[boolcnt]->name =strdup (sepol_bool_get_name (boolean)); +if (!boollist[boolcnt]->name) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +free (boollist[boolcnt]); +return -1 ; + } boollist[boolcnt]->active =sepol_bool_get_value (boolean); boolcnt++;return 0 ;@@ -149,6 +158,11 @@ static int check_booleans(struct boolean_t **bools)if (fcnt >0 ) { *bools =calloc (sizeof (struct boolean_t ), fcnt +1 ); +if (!*bools) { +PyErr_SetString ( PyExc_MemoryError,"Out of memory\n" ); +free (foundlist); +return 0 ; + }struct boolean_t *b = *bools;for (i =0 ; i < fcnt; i++) {int ctr = foundlist[i];@@ -278,14 +292,22 @@ static int __policy_init(const char *init_path)return 0 ; err: -if (boollist)
libselinux/src/audit2why.c static int __policy_init(const char *init_path) · line 303
Quoted 10 lines
libselinux/src/audit2why.c +9
-1-free (boollist); +if (boollist) { +for (i =0 ; i < boolcnt; i++) { +free (boollist[i]->name); +free (boollist[i]); + } +free (boollist); + boollist =NULL ; + boolcnt =0 ; + }
i is not declared and it is indented with spaces rather than tabs.
Thanks,
Jim
Quoted 13 lines
libselinux/src/audit2why.c +1
-1if (avc){if (avc->handle)sepol_handle_destroy (avc->handle);if (avc->policydb)sepol_policydb_free (avc->policydb);free (avc); + avc =NULL ; }if (pf)sepol_policy_file_free (pf); --2.27.0