[PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer

David Laight posted 8 patches 11 months ago
[PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer
Posted by David Laight 11 months ago
While the output generated by test_hexdump_prepare_test() shouldn't
be longer than the size of the buffer passed, for safety verify that
the buffer is long enough.
If too short fill the buffer with an error message - output on
test failure.

Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
 lib/test_hexdump.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
index 743ea5c78f9e..ed6f0b0a1bb3 100644
--- a/lib/test_hexdump.c
+++ b/lib/test_hexdump.c
@@ -39,6 +39,14 @@ static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
 		groupsize = 1;
 	byteswap = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 0 : groupsize - 1;
 
+	/* Check test passed a big enough output buffer */
+	if (ascii)
+		i = rowsize * 2 + rowsize / groupsize + 1 + len + 1;
+	else
+		i = len * 2 + len / groupsize - 1 + 1;
+	if (i > testlen)
+		return scnprintf(test, testlen, "buffer too short %zu < %zu", testlen, i);
+
 	/* hex dump */
 	p = test;
 	for (i = 0, j = 0; i < len; i++) {
-- 
2.39.5
Re: [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer
Posted by Andy Shevchenko 11 months ago
On Sat, Mar 08, 2025 at 09:34:48AM +0000, David Laight wrote:
> While the output generated by test_hexdump_prepare_test() shouldn't
> be longer than the size of the buffer passed, for safety verify that
> the buffer is long enough.
> If too short fill the buffer with an error message - output on
> test failure.

Isn't the function should behave snprintf() alike?
I think this patch is simply wrong because it's based on a wrong assumption.

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer
Posted by David Laight 11 months ago
On Mon, 10 Mar 2025 11:01:29 +0200
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> On Sat, Mar 08, 2025 at 09:34:48AM +0000, David Laight wrote:
> > While the output generated by test_hexdump_prepare_test() shouldn't
> > be longer than the size of the buffer passed, for safety verify that
> > the buffer is long enough.
> > If too short fill the buffer with an error message - output on
> > test failure.  
> 
> Isn't the function should behave snprintf() alike?
> I think this patch is simply wrong because it's based on a wrong assumption.
> 

The normal hex_dump_to_buffer() is snprintf() like.

But, as the tests are written, test_hexdump_prepare_test() is always
passed a fixed size buffer that should be big enough.
The test control code then truncates the output to the required length
and fills the rest of the buffer with '#' before the compare.

The 'testlen' (output buffer length) parameter is actually ignored.
So if someone adds a test that would request output longer than the
buffer (actually requires the 'rowsize' limit be relaxed) then the
test code overruns the on-stack buffer.
While the control code could be rewritten that would be more changes.
So it seemed best just to force the test to fail and make it obvious why.

	David