Skip to contentSkip to conversation

libselinux: restorecon: add_exclude: validate directory before deref

PATCH In review Rahul Sandhu 3 participants 3 messages May 31 – Jun 2 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: restorecon: add_exclude: validate directory before deref

Currently, the call to strcmp() with directory as an argument results in directory being dereferenced before it is checked to be non-NULL. Hence, move the validation of directory before the call to strcmp().

---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -6
index 69a53503..6a36de67 100644
@@ -204,12 +204,6 @@ static int add_exclude(const char *directory, bool who)
 	size_t len = 0;
 	int i;
 
-	/* Check if already present. */
-	for (i = 0; i < exclude_count; i++) {
-		if (strcmp(directory, exclude_lst[i].directory) == 0)
-			return 0;
-	}
-
 	if (directory == NULL || directory[0] != '/') {
 		selinux_log(SELINUX_ERROR,
 			    "Full path required for exclude: %s.\n",
@@ -218,6 +212,12 @@ static int add_exclude(const char *directory, bool who)
 		return -1;
 	}
 
+	/* Check if already present. */
+	for (i = 0; i < exclude_count; i++) {
+		if (strcmp(directory, exclude_lst[i].directory) == 0)
+			return 0;
+	}
+
 	if (exclude_count >= INT_MAX - 1) {
 		selinux_log(SELINUX_ERROR, "Too many directory excludes: %d.\n", exclude_count);
 		errno = EOVERFLOW;
Signature · 1 line
2.54.0
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Rahul Sandhu

Currently, the call to strcmp() with directory as an argument results in directory being dereferenced before it is checked to be non-NULL. Hence, move the validation of directory before the call to strcmp().

Quoted 38 lines
---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -7
index 69a53503..6a36de67 100644
@@ -204,12 +204,6 @@ static int add_exclude(const char *directory, bool who)
        size_t len = 0;
        int i;
 
-       /* Check if already present. */
-       for (i = 0; i < exclude_count; i++) {
-               if (strcmp(directory, exclude_lst[i].directory) == 0)
-                       return 0;
-       }
-
        if (directory == NULL || directory[0] != '/') {
                selinux_log(SELINUX_ERROR,
                            "Full path required for exclude: %s.\n",
@@ -218,6 +212,12 @@ static int add_exclude(const char *directory, bool who)
                return -1;
        }
 
+       /* Check if already present. */
+       for (i = 0; i < exclude_count; i++) {
+               if (strcmp(directory, exclude_lst[i].directory) == 0)
+                       return 0;
+       }
+
        if (exclude_count >= INT_MAX - 1) {
                selinux_log(SELINUX_ERROR, "Too many directory excludes: %d.\n", exclude_count);
                errno = EOVERFLOW;
--

2.54.0

#2 lore raw
Quoted 11 lines · Stephen Smalley
Quoted 6 lines · Rahul Sandhu

Currently, the call to strcmp() with directory as an argument results in directory being dereferenced before it is checked to be non-NULL. Hence, move the validation of directory before the call to strcmp().

Merged.
Thanks,
Jim

Quoted 39 lines
Quoted 38 lines
---
 libselinux/src/selinux_restorecon.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
libselinux/src/selinux_restorecon.c +6 -7
index 69a53503..6a36de67 100644
@@ -204,12 +204,6 @@ static int add_exclude(const char *directory, bool who)
        size_t len = 0;
        int i;
 
-       /* Check if already present. */
-       for (i = 0; i < exclude_count; i++) {
-               if (strcmp(directory, exclude_lst[i].directory) == 0)
-                       return 0;
-       }
-
        if (directory == NULL || directory[0] != '/') {
                selinux_log(SELINUX_ERROR,
                            "Full path required for exclude: %s.\n",
@@ -218,6 +212,12 @@ static int add_exclude(const char *directory, bool who)
                return -1;
        }
 
+       /* Check if already present. */
+       for (i = 0; i < exclude_count; i++) {
+               if (strcmp(directory, exclude_lst[i].directory) == 0)
+                       return 0;
+       }
+
        if (exclude_count >= INT_MAX - 1) {
                selinux_log(SELINUX_ERROR, "Too many directory excludes: %d.\n", exclude_count);
                errno = EOVERFLOW;
--

2.54.0