Fix bugs in LTP core library
cover Fix bugs in LTP core library
These bugs were found by LLM long time ago, after asking to check for errors in memory management and general usage. An in-depth analysis was not done, because these were mostly cases where LTP might produce false positives or crash the application. Keep it simple.
---
Andrea Cervesato (3):
lib: Avoid loop_info.lo_name buffer overflow
lib: Close directory fd on ENOSPC in tst_fill_fs
lib: Fix wrong field in tst_iovec_alloc
lib/tst_buffers.c | 2 +- lib/tst_device.c | 2 +- lib/tst_fill_fs.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-)
---
base-commit: a67fe5f7b20492ff6ee7f4b1776a7579d4b3859b
change-id: 20260717-fix_core_library-c9fef66bd8df
Best regards,
Signature · 5 lines
Andrea Cervesato <[email protected]> -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/3 lib: Avoid loop_info.lo_name buffer overflow
From: Andrea Cervesato <[email protected]>
The backing file path may be longer than the fixed-size lo_name field, so an unbounded strcpy() can overflow the loop_info structure. Copy at most the field size and rely on the preceding memset() for termination.
--- lib/tst_device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_device.c +1 -1
@@ -182,7 +182,7 @@ int tst_attach_device(const char *dev, const char *file)* LOOP_SET_FD and LOOP_SET_STATUS. */ memset (&loopinfo,0 ,sizeof (loopinfo)); -strcpy (loopinfo.lo_name, file); +strncpy (loopinfo.lo_name, file,sizeof (loopinfo.lo_name) -1 );if (ioctl (dev_fd,LOOP_SET_STATUS , &loopinfo)) {close (dev_fd);
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] lib: Avoid loop_info.lo_name buffer overflow
Hi Andrea,
Quoted 1 line · Andrea Cervesato
lib: Avoid loop_info.lo_name buffer overflow
Verdict - Reviewed
---
Note:
The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] lib: Avoid loop_info.lo_name buffer overflow
In reply to Andrea CervesatoQuoted 16 lines
From: Andrea Cervesato <[email protected]>
The backing file path may be longer than the fixed-size
lo_namefield, so an unboundedstrcpy()can overflow theloop_infostructure. Copy at most the field size and rely on the precedingmemset()for termination.Signed-off-by Andrea Cervesato <[email protected]>--- lib/tst_device.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/tst_device.c +0
-0
lib/tst_device.c int tst_attach_device(const char *dev, const char *file) · line 185
Quoted 5 lines
lib/tst_device.c +1
-1@@ -182,7 +182,7 @@ int tst_attach_device(const char *dev, const char *file)* LOOP_SET_FD and LOOP_SET_STATUS. */ memset (&loopinfo,0 ,sizeof (loopinfo)); -strcpy (loopinfo.lo_name, file); +strncpy (loopinfo.lo_name, file,sizeof (loopinfo.lo_name) -1 );
I suppose strncpy() is recommended as safer alternative to strcpy(), right? Shouldn't we check for strlen(file) is not longer than sizeof(loopinfo.lo_name) -1 while we are at it?
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] lib: Avoid loop_info.lo_name buffer overflow
Hi Petr,
Quoted 3 lines
I suppose
strncpy()is recommended as safer alternative tostrcpy(), right? Shouldn't we check for strlen(file) is not longer thansizeof(loopinfo.lo_name)-1 while we are at it?
we don't need to check for strlen(file) because strncpy() copy min(strlen(loopinfo.lo_name), strlen(file)) chars at max.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] lib: Avoid loop_info.lo_name buffer overflow
Quoted 8 lines
Hi Petr,
Quoted 3 lines
I suppose
strncpy()is recommended as safer alternative tostrcpy(), right? Shouldn't we check for strlen(file) is not longer thansizeof(loopinfo.lo_name)-1 while we are at it?we don't need to check for strlen(file) because
strncpy()copymin(strlen(loopinfo.lo_name), strlen(file))chars at max.
I mean if file is longer, then only part will be copied and following ioctl() will fail, right? Therefore warning about too long name would help.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 1/3] lib: Avoid loop_info.lo_name buffer overflow
Quoted 2 lines
I mean if file is longer, then only part will be copied and following
ioctl()will fail, right? Therefore warning about too long name would help.
sure, we can add that
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/3 lib: Close directory fd on ENOSPC in tst_fill_fs
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
The directory descriptor opened at the top of the function was leaked when openat() failed with ENOSPC and the function returned early. Close it before returning.
--- lib/tst_fill_fs.c | 1 + 1 file changed, 1 insertion(+)
lib/tst_fill_fs.c +1 -0
@@ -91,6 +91,7 @@ static void fill_flat_vec(const char *path, int verbose)if (fd ==-1 ) {if (errno ==ENOSPC ) {tst_res (TINFO |TERRNO ,"openat()" ); +SAFE_CLOSE (dir);return ; } tst_brk(TBROK | TERRNO, "openat(%d, %d, 0600) failed for path %s",
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/3] lib: Close directory fd on ENOSPC in tst_fill_fs
Hi Andrea,
Quoted 14 lines
The directory descriptor opened at the top of the function was leaked when
openat()failed with ENOSPC and the function returned early. Close it before returning.Signed-off-by Andrea Cervesato <[email protected]>--- lib/tst_fill_fs.c | 1 + 1 file changed, 1 insertion(+)lib/tst_fill_fs.c +0
-0
lib/tst_fill_fs.c static void fill_flat_vec(const char *path, int verbose) · line 94
Quoted 4 lines
lib/tst_fill_fs.c +1
-0@@ -91,6 +91,7 @@ static void fill_flat_vec(const char *path, int verbose)if (fd ==-1 ) {if (errno ==ENOSPC ) {tst_res (TINFO |TERRNO ,"openat()" ); +SAFE_CLOSE (dir);
Good catch!
Kind regards,
Petr
Quoted 2 lines
lib/tst_fill_fs.c +0
-0return ; }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/3 lib: Fix wrong field in tst_iovec_alloc
In reply to Andrea CervesatoFrom: Andrea Cervesato <[email protected]>
The zero-size branch assigned iov_base twice and left iov_len unset, clearly a copy-paste typo. Set iov_len to 0 as intended.
--- lib/tst_buffers.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/tst_buffers.c +1 -1
@@ -123,7 +123,7 @@ struct iovec *tst_iovec_alloc(int sizes[]) iovec[i].iov_len = sizes[i]; }else { iovec[i].iov_base =NULL ; - iovec[i].iov_base =0 ; + iovec[i].iov_len =0 ; } }
Signature · 5 lines
2.51.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 3/3] lib: Fix wrong field in tst_iovec_alloc
Hi Andrea,
...
lib/tst_buffers.c struct iovec *tst_iovec_alloc(int sizes[]) · line 126
Quoted 3 lines
lib/tst_buffers.c +1
-1iovec[i].iov_base =NULL ; - iovec[i].iov_base =0 ; + iovec[i].iov_len =0 ;
Good catch!
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Andrea Cervesato via ltp
AppliedMerged, Thanks!
With suggested @Petr feedback on tst_attach_device:
int tst_attach_device(const char *dev, const char *file) {
...
if (strlen(file) >= LO_NAME_SIZE) {
tst_brkm(TBROK, NULL, "Device name can't be longer than %u chars",
LO_NAME_SIZE);
} ...
}
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp