Skip to contentSkip to conversation

Fix bugs in LTP core library

PATCH Applied Andrea Cervesato 4 participants 12 messages Jul 17 – Jul 29 Library code

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index d3c53a1a18d2e4948ebff21d6d66c0ccd1590c6f..b5c3ccdb7be52ce600fbd7d7a83f6081df65d616 100644
@@ -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
#2 lore raw

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
Reviewed-by on v1 1/3
#3 lore raw

Re: [LTP] [PATCH 1/3] lib: Avoid loop_info.lo_name buffer overflow

In reply to Andrea Cervesato
Quoted 16 lines

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 +0 -0
index d3c53a1a18d2e4948ebff21d6d66c0ccd1590c6f..b5c3ccdb7be52ce600fbd7d7a83f6081df65d616 100644

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
#4 lore raw

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 to strcpy(), right? Shouldn't we check for strlen(file) is not longer than sizeof(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
#5 lore raw

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 to strcpy(), right? Shouldn't we check for strlen(file) is not longer than sizeof(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.

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
#6 lore raw

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
#7 lore raw

PATCH 2/3 lib: Close directory fd on ENOSPC in tst_fill_fs

In reply to Andrea Cervesato

From: 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
index c62d48e289965d59ef6c877870c6047bbecb33da..b1e48fdeea6ab00848aab482fd3899fc6f6ce23c 100644
@@ -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
Reviewed-by on v1 2/3
#8 lore raw

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.

---
 lib/tst_fill_fs.c | 1 +
 1 file changed, 1 insertion(+)
lib/tst_fill_fs.c +0 -0
index c62d48e289965d59ef6c877870c6047bbecb33da..b1e48fdeea6ab00848aab482fd3899fc6f6ce23c 100644

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 -0
 			return;
 		}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

PATCH 3/3 lib: Fix wrong field in tst_iovec_alloc

In reply to Andrea Cervesato

From: 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
index ac76a784bb7060ad46815310b56b2a4229bfd395..048b322457c3173df61e0617ad0bc190581c78a7 100644
@@ -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
Reviewed-by on v1 3/3
#10 lore raw

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 -1
 			iovec[i].iov_base = NULL;
-			iovec[i].iov_base = 0;
+			iovec[i].iov_len = 0;

Good catch!

Fixes ee761d81b9 ("lib: Add support for guarded buffers")

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw
In reply to Andrea Cervesato

Merged, 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