hexdump: truncate output in case of overflow
PATCH 1/1 hexdump: truncate output in case of overflow
There is a classical off-by-one error in case when we try to place, for example, 1+1 bytes as hex in the buffer of size 6. The expected result is to get an output truncated, but in the reality we get 6 bytes filed followed by terminating NUL.
Change the logic how we fill the output in case of byte dumping into limited space. This will follow the snprintf() behaviour by truncating output even on half bytes.
---
Waiting for Aaro's Tested-by: tag, that's why RFT. Meanwhile I will update test-hexdump to cover all corner case in overflow.
Linus, it would be nice to promote the fix when we get Aaro's confirmation.
lib/hexdump.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
lib/hexdump.c +5 -1
@@ -169,11 +169,15 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, } }else {for (j =0 ; j < len; j++) { -if (linebuflen < lx +3 ) +if (linebuflen < lx +2 ) goto overflow2; ch = ptr[j]; linebuf[lx++] =hex_asc_hi (ch); +if (linebuflen < lx +2 ) + goto overflow2; linebuf[lx++] =hex_asc_lo (ch); +if (linebuflen < lx +2 ) + goto overflow2; linebuf[lx++] =' ' ; }if (j)
Signature · 1 line
2.6.1
Hi,
Quoted 5 lines · Andy Shevchenko
There is a classical off-by-one error in case when we try to place, for example, 1+1 bytes as hex in the buffer of size 6. The expected result is to get an output truncated, but in the reality we get 6 bytes filed followed by terminating NUL.
Quoted 7 lines
Change the logic how we fill the output in case of byte dumping into limited space. This will follow the
snprintf()behaviour by truncating output even on half bytes.Fixes 114fc1afb2de (hexdump: make it return number of bytes placed in buffer)Reported-by Aaro Koskinen <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
Thanks, this fixes the crash with kmemleak for me:
A.
Quoted 33 lines
---
Waiting for Aaro's Tested-by: tag, that's why RFT. Meanwhile I will update test-hexdump to cover all corner case in overflow.Linus, it would be nice to promote the fix when we get Aaro's confirmation.
lib/hexdump.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)lib/hexdump.c +5
-1@@ -169,11 +169,15 @@ int hex_dump_to_buffer(const void *buf, size_t len, int rowsize, int groupsize, } }else {for (j =0 ; j < len; j++) { -if (linebuflen < lx +3 ) +if (linebuflen < lx +2 ) goto overflow2; ch = ptr[j]; linebuf[lx++] =hex_asc_hi (ch); +if (linebuflen < lx +2 ) + goto overflow2; linebuf[lx++] =hex_asc_lo (ch); +if (linebuflen < lx +2 ) + goto overflow2; linebuf[lx++] =' ' ; }if (j)Signature · 1 line
2.6.1