selftests/capabilities: Fix possible file leak in copy_fromat_to
PATCH 1/1 selftests/capabilities: Fix possible file leak in copy_fromat_to
The open() function returns -1 on error. openat() and open() initialize 'from' and 'to', and only 'from' validated with 'if' statement. If the initialization of variable 'to' fails, we should better check the value of 'to' and close 'from' to avoid possible file leak.
---
Found this error through static analysis.
--- tools/testing/selftests/capabilities/test_execve.c | 4 ++++ 1 file changed, 4 insertions(+)
tools/testing/selftests/capabilities/test_execve.c +4 -0
@@ -149,6 +149,10 @@ static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)ksft_exit_fail_msg ("open copy source - %s\n" ,strerror (errno));int to =open (toname,O_CREAT |O_WRONLY |O_EXCL ,0700 ); +if (to ==-1 ) { +close (from); +ksft_exit_fail_msg ("open copy destination - %s\n" ,strerror (errno)); + }while (true) {char buf[4096 ];
Signature · 1 line
2.25.1
Quoted 2 lines
…
openat()andopen()initialize
'from' and 'to', and only 'from' validated with 'if' statement.
Why do find such information helpful?
Quoted 2 lines
If the
initialization of variable 'to' fails,
The variable assignment will usually succeed.
A stored return value would eventually indicate a failed function call.
Quoted 2 lines
we should better check the value
of 'to' and close 'from' to avoid possible file leak.
Please improve the change description with an imperative wording.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc5#n94
How do you think about to use a summary phrase like “Complete error handling in copy_fromat_to()”?
…
Quoted 3 lines
---
Found this error through static analysis.
---
* Were any special tools involved?
* Would you like to replace a duplicate marker line by a blank line?
Regards,
Markus
Shuah Khan
maintainerQuoted 9 lines · Ma Ke
The
open()function returns -1 on error.openat()andopen()initialize 'from' and 'to', and only 'from' validated with 'if' statement. If the initialization of variable 'to' fails, we should better check the value of 'to' and close 'from' to avoid possible file leak.Fixes 32ae976ed3b5 ("selftests/capabilities: Add tests for capability evolution")Signed-off-by Ma Ke <[email protected]>---
Found this error through static analysis.
Please add this line to change adding details about the tool you used to find this problem.
tools/testing/selftests/capabilities/test_execve.c static void copy_fromat_to(int fromfd, const char *fromname, const char *toname) · line 152
Quoted 13 lines
--- tools/testing/selftests/capabilities/test_execve.c | 4 ++++ 1 file changed, 4 insertions(+)tools/testing/selftests/capabilities/test_execve.c +1
-0@@ -149,6 +149,10 @@ static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)ksft_exit_fail_msg ("open copy source - %s\n" ,strerror (errno));int to =open (toname,O_CREAT |O_WRONLY |O_EXCL ,0700 ); +if (to ==-1 ) {
Changing this check to < instead of checking for just -1 can catch other error returns.
tools/testing/selftests/capabilities/test_execve.c static void copy_fromat_to(int fromfd, const char *fromname, const char *toname) · line 158
Quoted 6 lines
tools/testing/selftests/capabilities/test_execve.c +3
-0+close (from); +ksft_exit_fail_msg ("open copy destination - %s\n" ,strerror (errno)); + }while (true) {char buf[4096 ];
thanks,
-- Shuah
Shuah Khan
maintainerQuoted 18 lines · Shuah Khan
Quoted 9 lines · Ma Ke
The
open()function returns -1 on error.openat()andopen()initialize 'from' and 'to', and only 'from' validated with 'if' statement. If the initialization of variable 'to' fails, we should better check the value of 'to' and close 'from' to avoid possible file leak.Fixes 32ae976ed3b5 ("selftests/capabilities: Add tests for capability evolution")Signed-off-by Ma Ke <[email protected]>---
Found this error through static analysis.Please add this line to change adding details about the tool you used to find this problem.
Quoted 4 lines
---
tools/testing/selftests/capabilities/test_execve.c| 4 ++++1 file changed, 4 insertions(+)
Quoted 11 lines
tools/testing/selftests/capabilities/test_execve.c static void copy_fromat_to(int fromfd, const char *fromname, const char *toname) · line 151
Quoted 8 lines
tools/testing/selftests/capabilities/test_execve.c +1
-0@@ -149,6 +149,10 @@ static void copy_fromat_to(int fromfd, const char *fromname, const char *toname) ksft_exit_fail_msg("open copy source - %s\n", strerror(errno)); int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700); + if (to == -1) {Changing this check to < instead of checking for just -1 can catch other error returns.
While you are fining this, can you fix this as well:
int from = openat(fromfd, fromname, O_RDONLY);
if (from == -1)
ksft_exit_fail_msg("open copy source - %s\n", strerror(errno));Same change to check for <
thanks,
-- Shuah