Skip to contentSkip to conversation

libselinux: free empty scandir(3) result

PATCH In review Christian Göttsche 4 participants 11 messages Apr 29, 2024 – Jun 14, 2024 SELinux security module

lore
Share & tools
lore
#0 lore raw

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
index c557df65..1ede8e2d 100644
@@ -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
#1 lore raw

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.

Fixes 5876aca0 ("libselinux: free data on selabel open failure")
Reported-by Cppcheck
---
 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
index 94a58062..852aeada 100644
@@ -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
index f994eefa..a8decc7a 100644
@@ -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
#2 lore raw

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 Cppcheck
---
 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
index 94a58062..852aeada 100644
@@ -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
index f994eefa..a8decc7a 100644
@@ -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.

#3 lore raw

PATCH 3/3 mcstrans: free constraint in error branch

In reply to Christian Göttsche

From: Christian Göttsche <[email protected]>

Free constraint, like in all other error branches.

Reported-by Cppcheck
---
 mcstrans/src/mcstrans.c | 1 +
 1 file changed, 1 insertion(+)
mcstrans/src/mcstrans.c +1 -0
index fded3235..7667e131 100644
@@ -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
#4 lore raw

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 Cppcheck
---
 mcstrans/src/mcstrans.c | 1 +
 1 file changed, 1 insertion(+)
mcstrans/src/mcstrans.c +1 -1
index fded3235..7667e131 100644
@@ -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
#5 lore raw
In reply to Christian Göttsche
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

---
 libselinux/src/booleans.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libselinux/src/booleans.c +0 -0
index c557df65..1ede8e2d 100644

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

#6 lore raw
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

---
 libselinux/src/booleans.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libselinux/src/booleans.c +0 -0
index c557df65..1ede8e2d 100644
@@ -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 -1
        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?

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

#7 lore raw
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

---
 libselinux/src/booleans.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libselinux/src/booleans.c +0 -0
index c557df65..1ede8e2d 100644
@@ -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 -1
        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 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 if scandir() 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

#8 lore raw
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

---
 libselinux/src/booleans.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
libselinux/src/booleans.c +0 -0
index c557df65..1ede8e2d 100644
@@ -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 -1
        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 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 if scandir() 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

Acked-by on v1 1/3
#9 lore raw
In reply to Christian Göttsche
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

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
index c557df65..1ede8e2d 100644
@@ -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

#10 lore raw
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

For these three patches:

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
index c557df65..1ede8e2d 100644
@@ -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