selftests/capabilities: Fix possible file leak in copy_fromat_to
PATCH v2 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. Improve the checking of 'from' additionally.
---
Changes in v2:
- modified the patch according to suggestions;
- found by customized static analysis tool.
--- tools/testing/selftests/capabilities/test_execve.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
tools/testing/selftests/capabilities/test_execve.c +5 -1
@@ -145,10 +145,14 @@ static void chdir_to_tmpfs(void)static void copy_fromat_to (int fromfd,const char *fromname,const char *toname) {int from =openat (fromfd, fromname,O_RDONLY ); -if (from ==-1 ) +if (from <0 )ksft_exit_fail_msg ("open copy source - %s\n" ,strerror (errno));int to =open (toname,O_CREAT |O_WRONLY |O_EXCL ,0700 ); +if (to <0 ) { +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 you 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 3 lines
we should better check the value
of 'to' and close 'from' to avoid possible file leak. Improve the checking of 'from' additionally.
Please split desired changes into separate update steps.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc5#n168
How do you think about to use a summary phrase like “Complete error handling in copy_fromat_to()”?
…
Quoted 5 lines
---
Changes in v2:
- modified the patch according to suggestions;
- found by customized static analysis tool.
---
* Would you like to replace a duplicate marker line by a blank line?
* I would appreciate further information about the applied tool.
Regards,
Markus
Shuah Khan
maintainerQuoted 6 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. Improve the checking of 'from' additionally.
Quoted 6 lines
Fixes 32ae976ed3b5 ("selftests/capabilities: Add tests for capability evolution")Signed-off-by Ma Ke <[email protected]>---
Changes in v2:
- modified the patch according to suggestions;
- found by customized static analysis tool.
Care to give more details on this customized static analysis tool?
Quoted 14 lines
--- tools/testing/selftests/capabilities/test_execve.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)tools/testing/selftests/capabilities/test_execve.c +1
-1@@ -145,10 +145,14 @@ static void chdir_to_tmpfs(void)static void copy_fromat_to (int fromfd,const char *fromname,const char *toname) {int from =openat (fromfd, fromname,O_RDONLY ); -if (from ==-1 ) +if (from <0 )
tools/testing/selftests/capabilities/test_execve.c static void chdir_to_tmpfs(void) · line 158
Quoted 10 lines
tools/testing/selftests/capabilities/test_execve.c +4
-0ksft_exit_fail_msg ("open copy source - %s\n" ,strerror (errno));int to =open (toname,O_CREAT |O_WRONLY |O_EXCL ,0700 ); +if (to <0 ) { +close (from); +ksft_exit_fail_msg ("open copy destination - %s\n" ,strerror (errno)); + }while (true) {char buf[4096 ];
thanks,
-- Shuah