Skip to content

Commit e4d8a29

Browse files
Mikulas Patockatorvalds
authored andcommitted
hex2bin: fix access beyond string end
If we pass too short string to "hex2bin" (and the string size without the terminating NUL character is even), "hex2bin" reads one byte after the terminating NUL character. This patch fixes it. Note that hex_to_bin returns -1 on error and hex2bin return -EINVAL on error - so we can't just return the variable "hi" or "lo" on error. This inconsistency may be fixed in the next merge window, but for the purpose of fixing this bug, we just preserve the existing behavior and return -1 and -EINVAL. Signed-off-by: Mikulas Patocka <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Fixes: b780498 ("lib: add error checking to hex2bin") Cc: [email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent e5be157 commit e4d8a29

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/hexdump.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@ EXPORT_SYMBOL(hex_to_bin);
6363
int hex2bin(u8 *dst, const char *src, size_t count)
6464
{
6565
while (count--) {
66-
int hi = hex_to_bin(*src++);
67-
int lo = hex_to_bin(*src++);
66+
int hi, lo;
6867

69-
if ((hi < 0) || (lo < 0))
68+
hi = hex_to_bin(*src++);
69+
if (unlikely(hi < 0))
70+
return -EINVAL;
71+
lo = hex_to_bin(*src++);
72+
if (unlikely(lo < 0))
7073
return -EINVAL;
7174

7275
*dst++ = (hi << 4) | lo;

0 commit comments

Comments
 (0)