read_all: Fix buffer too small for long paths
PATCH v3 1/1 read_all: Fix buffer too small for long paths
queue_pop() copies each path into popped[BUFFER_SIZE], but BUFFER_SIZE was 1024 while visit_dir() builds paths up to PATH_MAX. Any path longer than 1023 bytes aborted the test with TBROK.
Use PATH_MAX for BUFFER_SIZE and MAX_PATH, and drop the extra -1 from the queue_pop() bound so a PATH_MAX - 1 character path fits.
Tested on smaller boards and then on local machines with deeply nested directories under /sys.
--- testcases/kernel/fs/read_all/read_all.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
testcases/kernel/fs/read_all/read_all.c +3 -3
@@ -51,8 +51,8 @@#include "tst_timer.h" #define QUEUE_SIZE 16384 -#define BUFFER_SIZE 1024 -#define MAX_PATH 4096 +#define BUFFER_SIZE PATH_MAX +#define MAX_PATH PATH_MAX#define MAX_DISPLAY 40struct queue {
@@ -131,7 +131,7 @@ static int queue_pop(struct queue *q)while (q->data[i]) { q->popped[j] = q->data[i]; -if (++j >=BUFFER_SIZE -1 ) +if (++j >=BUFFER_SIZE )tst_brk (TBROK ,"Buffer is too small for path" ); i = (i +1 ) %QUEUE_SIZE ;
Signature · 5 lines
2.55.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Michael,
Quoted 1 line · Michael Menasherov via ltp
read_all: Fix buffer too small for long paths
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
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
testcases/kernel/fs/read_all/read_all.c line 56
Quoted 13 lines
testcases/kernel/fs/read_all/read_all.c +2
-2@@ -51,8 +51,8 @@#include "tst_timer.h" #define QUEUE_SIZE 16384 -#define BUFFER_SIZE 1024 -#define MAX_PATH 4096 +#define BUFFER_SIZE PATH_MAX +#define MAX_PATH PATH_MAX#define MAX_DISPLAY 40
This part looks good.
testcases/kernel/fs/read_all/read_all.c static int queue_pop(struct queue *q) · line 137
Quoted 10 lines
testcases/kernel/fs/read_all/read_all.c +1
-1struct queue {@@ -131,7 +131,7 @@ static int queue_pop(struct queue *q)while (q->data[i]) { q->popped[j] = q->data[i]; -if (++j >=BUFFER_SIZE -1 ) +if (++j >=BUFFER_SIZE )tst_brk (TBROK ,"Buffer is too small for path" ); i = (i +1 ) %QUEUE_SIZE ;
However here we do:
q->popped[j] = '\0';
So j must be at most BUFFER_SIZE-1 and with the change above j may end up equal to BUFFER_SIZE and we will land off by one in the array.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Cyril
Thanks for explaining the problem with the code. I took a better look at it. How about checking before the write instead:
while (q->data[i]) {
if (j >= BUFFER_SIZE - 1)
tst_brk(TBROK, "Buffer is too small for path");
q->popped[j++] = q->data[i];
i = (i + 1) % QUEUE_SIZE;
}
q->popped[j] = '\0'; j can never reach BUFFER_SIZE without TBROK, and PATH_MAX - 1 paths still fit. Does this look good to you?
Thanks,
Michael Menasherov.
Quoted 41 lines · Cyril Hrubis
Hi!
Review
Quoted 1 line
(quoted diff) +0
-0
b/testcases/kernel/fs/read_all/read_all.ctestcases/kernel/fs/read_all/read_all.c line 56
Quoted 12 lines
index
5720ffb99..ec9454fc9100644testcases/kernel/fs/read_all/read_all.c +2
-2@@ -51,8 +51,8 @@#include "tst_timer.h" #define QUEUE_SIZE 16384 -#define BUFFER_SIZE 1024 -#define MAX_PATH 4096 +#define BUFFER_SIZE PATH_MAX +#define MAX_PATH PATH_MAX#define MAX_DISPLAY 40This part looks good.
testcases/kernel/fs/read_all/read_all.c static int queue_pop(struct queue *q) · line 137
Quoted 10 lines
testcases/kernel/fs/read_all/read_all.c +1
-1struct queue {@@ -131,7 +131,7 @@ static int queue_pop(struct queue *q)while (q->data[i]) { q->popped[j] = q->data[i]; -if (++j >=BUFFER_SIZE -1 ) +if (++j >=BUFFER_SIZE )tst_brk (TBROK ,"Buffer is too small for path" ); i = (i +1 ) %QUEUE_SIZE ;However here we do:
q->popped[j] = '\0';
So j must be at most BUFFER_SIZE-1 and with the change above j may end up equal to
BUFFER_SIZEand we will land off by one in the array.--
Cyril Hrubis
[email protected]
Signature · 9 lines
Michael Menasherov Software Quality Engineer - Automotive Kernel Red Hat <https://www.redhat.com/> <https://www.redhat.com/> -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Quoted 12 lines
Thanks for explaining the problem with the code. I took a better look at it. How about checking before the write instead:
while (q->data[i]) { if (j >= BUFFER_SIZE - 1) tst_brk(TBROK, "Buffer is too small for path"); q->popped[j++] = q->data[i]; i = (i + 1) % QUEUE_SIZE; } q->popped[j] = '\0';
Quoted 2 lines
j can never reach
BUFFER_SIZEwithout TBROK, andPATH_MAX- 1 paths still fit. Does this look good to you?
Ah, righ, there is tst_brk(), sorry for missing that.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi!
Pushed, thanks.
Signature · 5 lines
Cyril Hrubis [email protected] -- Mailing list info: https://lists.linux.it/listinfo/ltp