Skip to contentSkip to conversation

read_all: Fix buffer too small for long paths

PATCH In review Michael Menasherov via ltp 3 participants 7 messages Sep 8 – Sep 15

lore
Share & tools
lore
#0 lore raw

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.

Fixes 67c95fbbfc3a ("Add read_all file systems test")
---
 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
index 5720ffb99..ec9454fc9 100644
@@ -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
 
 struct 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
#1 lore raw

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
#3 lore raw
In reply to Michael Menasherov via 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
index 5720ffb99..ec9454fc9 100644
@@ -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 -1
 struct 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
#4 lore raw

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.c

testcases/kernel/fs/read_all/read_all.c line 56

Quoted 12 lines

index 5720ffb99..ec9454fc9 100644

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 -1
 struct 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.

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

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_SIZE without TBROK, and PATH_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