Skip to contentSkip to conversation

libselinux/setexecfilecon: Remove useless rc check

PATCH In review Vit Mojzis 2 participants 4 messages Oct 25, 2024 – Oct 30, 2024 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/2 libselinux/setexecfilecon: Remove useless rc check

Fixes:
Error: IDENTICAL_BRANCHES (CWE-398):
libselinux-3.6/src/setexecfilecon.c:45: implicit_else: The code from the above if-then branch is identical to the code after the if statement.
libselinux-3.6/src/setexecfilecon.c:43: identical_branches: The same code is executed when the condition "rc < 0" is true or false, because the code in the if-then branch and after the if statement is identical. Should the if statement be removed?

 \#   41|
 \#   42|           rc = setexeccon(newcon);
 \#   43|->         if (rc < 0)
 \#   44|                   goto out;
 \#   45|         out:
---
 libselinux/src/setexecfilecon.c | 2 --
 1 file changed, 2 deletions(-)
libselinux/src/setexecfilecon.c +0 -2
index 2c6505a9..4b31e775 100644
@@ -40,8 +40,6 @@ int setexecfilecon(const char *filename, const char *fallback_type)
 	}
 
 	rc = setexeccon(newcon);
-	if (rc < 0)
-		goto out;
       out:
 
 	if (rc < 0 && security_getenforce() == 0)
Signature · 1 line
2.47.0
author
#1 lore raw

PATCH 2/2 libselinux/matchpathcon: RESOURCE_LEAK: Variable "con"

Fixes:
Error: RESOURCE_LEAK (CWE-772):
libselinux-3.6/src/matchpathcon.c:519: alloc_arg: "lgetfilecon_raw" allocates memory that is stored into "con". [Note: The source code implementation of the function has been overridden by a user model.] libselinux-3.6/src/matchpathcon.c:528: leaked_storage: Variable "con" going out of scope leaks the storage it points to.

 \#  526|
 \#  527|           if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
 \#  528|->                         return -1;
 \#  529|
 \#  530|           if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
---
 libselinux/src/matchpathcon.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
libselinux/src/matchpathcon.c +3 -1
index e44734c3..967520e4 100644
@@ -524,8 +524,10 @@ int selinux_file_context_verify(const char *path, mode_t mode)
 			return 0;
 	}
 	
-	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0))
+	if (!hnd && (matchpathcon_init_prefix(NULL, NULL) < 0)){
+			freecon(con);
 			return -1;
+	}
 
 	if (selabel_lookup_raw(hnd, &fcontext, path, mode) != 0) {
 		if (errno != ENOENT)
Signature · 1 line
2.47.0
Acked-by on v1 1/2
#2 lore raw
In reply to Vit Mojzis
Quoted 5 lines · Vit Mojzis

Fixes:
Error: IDENTICAL_BRANCHES (CWE-398):
libselinux-3.6/src/setexecfilecon.c:45: implicit_else: The code from the above if-then branch is identical to the code after the if statement.
libselinux-3.6/src/setexecfilecon.c:43: identical_branches: The same code is executed when the condition "rc < 0" is true or false, because the code in the if-then branch and after the if statement is identical. Should the if statement be removed?

Quoted 7 lines
 \#   41|
 \#   42|           rc = setexeccon(newcon);
 \#   43|->         if (rc < 0)
 \#   44|                   goto out;
 \#   45|         out:

For these two patches:

Quoted 21 lines
---
 libselinux/src/setexecfilecon.c | 2 --
 1 file changed, 2 deletions(-)
libselinux/src/setexecfilecon.c +0 -3
index 2c6505a9..4b31e775 100644
@@ -40,8 +40,6 @@ int setexecfilecon(const char *filename, const char *fallback_type)
        }
 
        rc = setexeccon(newcon);
-       if (rc < 0)
-               goto out;
       out:
 
        if (rc < 0 && security_getenforce() == 0)
--

2.47.0

#3 lore raw
Quoted 13 lines · James Carter
Quoted 11 lines · Vit Mojzis

Fixes:
Error: IDENTICAL_BRANCHES (CWE-398):
libselinux-3.6/src/setexecfilecon.c:45: implicit_else: The code from the above if-then branch is identical to the code after the if statement.
libselinux-3.6/src/setexecfilecon.c:43: identical_branches: The same code is executed when the condition "rc < 0" is true or false, because the code in the if-then branch and after the if statement is identical. Should the if statement be removed?

 \#   41|
 \#   42|           rc = setexeccon(newcon);
 \#   43|->         if (rc < 0)
 \#   44|                   goto out;
 \#   45|         out:
Quoted 5 lines
Quoted 1 line

For these two patches:

These two patches have been merged.
Thanks,
Jim

Quoted 21 lines
Quoted 21 lines
---
 libselinux/src/setexecfilecon.c | 2 --
 1 file changed, 2 deletions(-)
libselinux/src/setexecfilecon.c +0 -3
index 2c6505a9..4b31e775 100644
@@ -40,8 +40,6 @@ int setexecfilecon(const char *filename, const char *fallback_type)
        }
 
        rc = setexeccon(newcon);
-       if (rc < 0)
-               goto out;
       out:
 
        if (rc < 0 && security_getenforce() == 0)
--

2.47.0