libselinux: free empty scandir(3) result
PATCH 1/3 libselinux: free empty scandir(3) result
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
libselinux/src/booleans.c +5 -1
@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) { +free (namelist); errno =ENOENT ;return -1 ; }
Signature · 1 line
2.43.0
PATCH 2/3 libselinux: avoid pointer dereference before check
From: Christian Göttsche <[email protected]>
Since commit 5876aca0 ("libselinux: free data on selabel open failure") the close handler of label backends must support partial initialized state, e.g. ->data being NULL. Thus checks for NULL were added, but in two cases the pointers in question were already dereferenced before.
Reorder the dereference after the NULL-checks.
--- libselinux/src/label_media.c | 4 +++- libselinux/src/label_x.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
libselinux/src/label_media.c +3 -1
@@ -164,12 +164,14 @@ finish:static void close (struct selabel_handle *rec) {struct saved_data *data = (struct saved_data *)rec->data; -struct spec *spec, *spec_arr = data->spec_arr; +struct spec *spec, *spec_arr;unsigned int i;if (!data)return ; + spec_arr = data->spec_arr; +for (i =0 ; i < data->nspec; i++) { spec = &spec_arr[i];free (spec->key);
libselinux/src/label_x.c +3 -1
@@ -191,12 +191,14 @@ finish:static void close (struct selabel_handle *rec) {struct saved_data *data = (struct saved_data *)rec->data; -struct spec *spec, *spec_arr = data->spec_arr; +struct spec *spec, *spec_arr;unsigned int i;if (!data)return ; + spec_arr = data->spec_arr; +for (i =0 ; i < data->nspec; i++) { spec = &spec_arr[i];free (spec->key);
Signature · 1 line
2.43.0
Re: [PATCH 2/3] libselinux: avoid pointer dereference before check
Quoted 59 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Since commit
5876aca0("libselinux: free data on selabel open failure") the close handler of label backends must support partial initialized state, e.g. ->data being NULL. Thus checks for NULL were added, but in two cases the pointers in question were already dereferenced before.Reorder the dereference after the NULL-checks.
Fixes 5876aca0 ("libselinux: free data on selabel open failure")Reported-by CppcheckSigned-off-by Christian Göttsche <[email protected]>--- libselinux/src/label_media.c | 4 +++- libselinux/src/label_x.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)libselinux/src/label_media.c +3
-1@@ -164,12 +164,14 @@ finish:static void close (struct selabel_handle *rec) {struct saved_data *data = (struct saved_data *)rec->data; -struct spec *spec, *spec_arr = data->spec_arr; +struct spec *spec, *spec_arr;unsigned int i;if (!data)return ; + spec_arr = data->spec_arr; +for (i =0 ; i < data->nspec; i++) { spec = &spec_arr[i];free (spec->key);libselinux/src/label_x.c +3
-2@@ -191,12 +191,14 @@ finish:static void close (struct selabel_handle *rec) {struct saved_data *data = (struct saved_data *)rec->data; -struct spec *spec, *spec_arr = data->spec_arr; +struct spec *spec, *spec_arr;unsigned int i;if (!data)return ; + spec_arr = data->spec_arr; +for (i =0 ; i < data->nspec; i++) { spec = &spec_arr[i];free (spec->key); --
Quoted 3 lines
2.43.0
LGTM, ack.
PATCH 3/3 mcstrans: free constraint in error branch
In reply to Christian GöttscheFrom: Christian Göttsche <[email protected]>
Free constraint, like in all other error branches.
--- mcstrans/src/mcstrans.c | 1 + 1 file changed, 1 insertion(+)
mcstrans/src/mcstrans.c +1 -0
@@ -477,6 +477,7 @@ add_constraint(char op, char *raw, char *tok) { }if (asprintf (&constraint->text,"%s%c%s" , raw, op, tok) <0 ) {log_error ("asprintf failed %s" ,strerror (errno)); +free (constraint);return -1 ; } constraint->op = op;
Signature · 1 line
2.43.0
Re: [PATCH 3/3] mcstrans: free constraint in error branch
Quoted 24 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Free constraint, like in all other error branches.
Reported-by CppcheckSigned-off-by Christian Göttsche <[email protected]>--- mcstrans/src/mcstrans.c | 1 + 1 file changed, 1 insertion(+)mcstrans/src/mcstrans.c +1
-1@@ -477,6 +477,7 @@ add_constraint(char op, char *raw, char *tok) { }if (asprintf (&constraint->text,"%s%c%s" , raw, op, tok) <0 ) {log_error ("asprintf failed %s" ,strerror (errno)); +free (constraint);return -1 ; } constraint->op = op; --
Quoted 2 lines
2.43.0
Ack, but this code could be cleaned up to use a common error label. I think that would clean this code up a lot.
Quoted 1 line
Quoted 20 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +0
-0
libselinux/src/booleans.c int security_get_boolean_names(char ***names, int *len) · line 59
Quoted 8 lines
libselinux/src/booleans.c +4
-1@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) {
Changing this will allow scandir to fail and it continue, what's the point?
Quoted 8 lines
libselinux/src/booleans.c +1
-1+free (namelist); errno =ENOENT ;return -1 ; } --2.43.0
Quoted 24 lines · William Roberts
Quoted 21 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +0
-0@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)
Quoted 9 lines
libselinux/src/booleans.c int security_get_boolean_names(char ***names, int *len) · line 59
Quoted 7 lines
libselinux/src/booleans.c +4
-1snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) {Changing this will allow scandir to fail and it continue, what's the point?
What do you mean by "continue"?
The function will still return -1 with errno set if scandir(3) returns <= 0, like it does currently.
But currently if scandir() returns 0, we currently leak the pointer to the empty array.
Quoted 9 lines
Quoted 9 lines
libselinux/src/booleans.c +1
-1+free (namelist); errno =ENOENT ;return -1 ; } --2.43.0
Quoted 36 lines · Christian Göttsche
Quoted 24 lines · William Roberts
Quoted 21 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +0
-0@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)Quoted 9 lines
libselinux/src/booleans.c int security_get_boolean_names(char ***names, int *len) · line 59
Quoted 7 lines
libselinux/src/booleans.c +4
-1snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) {Changing this will allow scandir to fail and it continue, what's the point?
Quoted 5 lines
What do you mean by "continue"?
The function will still return -1 with errno set if scandir(3) returns <= 0, like it does currently.
But currently ifscandir()returns 0, we currently leak the pointer to the empty array.
I completely misread that, my apologies. I just mocked scandir for all those paths
with the leak sanitizer enabled, lgtm.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
libselinux/src/booleans.c +1
-1+free (namelist); errno =ENOENT ;return -1 ; } --2.43.0
Quoted 45 lines · William Roberts
Quoted 36 lines · Christian Göttsche
Quoted 24 lines · William Roberts
Quoted 21 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +0
-0@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)Quoted 9 lines
libselinux/src/booleans.c int security_get_boolean_names(char ***names, int *len) · line 59
Quoted 7 lines
libselinux/src/booleans.c +4
-1snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) {Changing this will allow scandir to fail and it continue, what's the point?
Quoted 5 lines
What do you mean by "continue"?
The function will still return -1 with errno set if scandir(3) returns <= 0, like it does currently.
But currently ifscandir()returns 0, we currently leak the pointer to the empty array.
Quoted 4 lines
I completely misread that, my apologies. I just mocked scandir for all those paths
with the leak sanitizer enabled, lgtm.
I assume that is an ack for this patch as well as the other two?
Thanks,
Jim
Quoted 11 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
libselinux/src/booleans.c +1
-1+free (namelist); errno =ENOENT ;return -1 ; } --2.43.0
Quoted 11 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>
For these three patches:
Quoted 25 lines
--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +5
-2@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) { +free (namelist); errno =ENOENT ;return -1 ; } --2.43.0
Quoted 9 lines · James Carter
Quoted 6 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
In case scandir(3) finds no entries still free the returned result to avoid leaking it.
Quoted 9 lines
Quoted 5 lines
Also do not override errno in case of a failure.
Reported.by: Cppcheck
Signed-off-by Christian Göttsche <[email protected]>For these three patches:
Acked-by James Carter <[email protected]>
These three patches have been merged.
Thanks,
Jim
Quoted 25 lines
Quoted 25 lines
--- libselinux/src/booleans.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)libselinux/src/booleans.c +5
-2@@ -53,7 +53,11 @@ int security_get_boolean_names(char ***names, int *len)snprintf (path,sizeof path,"%s%s" , selinux_mnt,SELINUX_BOOL_DIR ); *len =scandir (path, &namelist, &filename_select, alphasort); -if (*len <=0 ) { +if (*len <0 ) { +return -1 ; + } +if (*len ==0 ) { +free (namelist); errno =ENOENT ;return -1 ; } --2.43.0