fbdev: avoid out-of-bounds read in fb_pad_unaligned_buffer()
Subsystems (2)
PATCH v2 1/1 fbdev: avoid out-of-bounds read in fb_pad_unaligned_buffer()
fb_pad_unaligned_buffer() unconditionally reads and advances the source pointer for the final byte of each row, even when no bits from that byte are actually consumed.
When shift_high >= mod, the remaining bits do not cross a byte boundary, but the code still accesses the next source byte. This can lead to out-of-bounds reads under malformed geometry, as reported by syzbot.
Fix this by only accessing and consuming the final source byte when it contributes bits (shift_high < mod).
This fixes the KASAN slab-out-of-bounds read reported by syzkaller:
https://syzkaller.appspot.com/bug?extid=55e03490a0175b8dd81d
---
v2: address the real issue (shift_high >= mod) condition.
--- drivers/video/fbdev/core/fbmem.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/video/fbdev/core/fbmem.c +9 -6
@@ -100,7 +100,7 @@ EXPORT_SYMBOL(fb_pad_aligned_buffer);void fb_pad_unaligned_buffer (u8 *dst,u32 d_pitch,u8 *src,u32 idx,u32 height,u32 shift_high,u32 shift_low,u32 mod) { -u8 mask = (u8) (0xfff << shift_high), tmp; +u8 mask = (u8) (0xff << shift_high), tmp;int i, j;for (i = height; i--; ) {
@@ -113,15 +113,18 @@ void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, dst[j+1 ] = tmp; src++; } - tmp = dst[idx]; - tmp &= mask; - tmp |= *src >> shift_low; - dst[idx] = tmp; + +/* Only consume another source byte if it contributes bits */ if (shift_high < mod) { + tmp = dst[idx]; + tmp &= mask; + tmp |= *src >> shift_low; + dst[idx] = tmp; tmp = *src << shift_high; dst[idx+1 ] = tmp; + src++; } - src++; + dst += d_pitch; } }
Signature · 1 line
2.43.0
Helge Deller
maintainerQuoted 28 lines · Osama Abdelkader
fb_pad_unaligned_buffer()unconditionally reads and advances the source pointer for the final byte of each row, even when no bits from that byte are actually consumed.When
shift_high>= mod, the remaining bits do not cross a byte boundary, but the code still accesses the next source byte. This can lead to out-of-bounds reads under malformed geometry, as reported by syzbot.Fix this by only accessing and consuming the final source byte when it contributes bits (
shift_high< mod).This fixes the KASAN slab-out-of-bounds read reported by syzkaller:
https://syzkaller.appspot.com/bug?extid=55e03490a0175b8dd81dReported-by [email protected]Signed-off-by Osama Abdelkader <[email protected]>---
v2: address the real issue (shift_high>= mod) condition.--- drivers/video/fbdev/core/fbmem.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)drivers/video/fbdev/core/fbmem.c +0
-0
drivers/video/fbdev/core/fbmem.c EXPORT_SYMBOL(fb_pad_aligned_buffer); · line 103
Quoted 5 lines
drivers/video/fbdev/core/fbmem.c +1
-1@@ -100,7 +100,7 @@ EXPORT_SYMBOL(fb_pad_aligned_buffer);void fb_pad_unaligned_buffer (u8 *dst,u32 d_pitch,u8 *src,u32 idx,u32 height,u32 shift_high,u32 shift_low,u32 mod) { -u8 mask = (u8) (0xfff << shift_high), tmp; +u8 mask = (u8) (0xff << shift_high), tmp;
This part is correct, but shouldn't be part of this patch.
Quoted 13 lines
drivers/video/fbdev/core/fbmem.c +2
-4int i, j;for (i = height; i--; ) {@@ -113,15 +113,18 @@ void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, dst[j+1 ] = tmp; src++; } - tmp = dst[idx]; - tmp &= mask; - tmp |= *src >> shift_low; - dst[idx] = tmp; + +/* Only consume another source byte if it contributes bits */
drivers/video/fbdev/core/fbmem.c void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, · line 124
Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +5
-1if (shift_high < mod) { + tmp = dst[idx]; + tmp &= mask; + tmp |= *src >> shift_low; + dst[idx] = tmp; tmp = *src << shift_high; dst[idx+1 ] = tmp; + src++; } - src++;
Above you moved the src pointer inside the if(), so every line processed may miss a ptr increment. This means the source would need to be different too, but it hasn't changed, as it's still used from bit_putcs_unaligned() which prints a char from the character fonts.
So, I believe this part at least is wrong.
Did you test it?
Helge
Quoted 24 lines · Helge Deller
Quoted 23 lines · Osama Abdelkader
fb_pad_unaligned_buffer()unconditionally reads and advances the source pointer for the final byte of each row, even when no bits from that byte are actually consumed.When
shift_high>= mod, the remaining bits do not cross a byte boundary, but the code still accesses the next source byte. This can lead to out-of-bounds reads under malformed geometry, as reported by syzbot.Fix this by only accessing and consuming the final source byte when it contributes bits (
shift_high< mod).This fixes the KASAN slab-out-of-bounds read reported by syzkaller:
https://syzkaller.appspot.com/bug?extid=55e03490a0175b8dd81dReported-by [email protected]Signed-off-by Osama Abdelkader <[email protected]>---
v2: address the real issue (shift_high>= mod) condition.--- drivers/video/fbdev/core/fbmem.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
Quoted 12 lines
drivers/video/fbdev/core/fbmem.c EXPORT_SYMBOL(fb_pad_aligned_buffer); · line 103
Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +1
-1@@ -100,7 +100,7 @@ EXPORT_SYMBOL(fb_pad_aligned_buffer);void fb_pad_unaligned_buffer (u8 *dst,u32 d_pitch,u8 *src,u32 idx,u32 height,u32 shift_high,u32 shift_low,u32 mod) { -u8 mask = (u8) (0xfff << shift_high), tmp; +u8 mask = (u8) (0xff << shift_high), tmp;This part is correct, but shouldn't be part of this patch.
I just sent a seperate patch for that, and going to remove it in next version of this one.
Quoted 30 lines
Quoted 14 lines
drivers/video/fbdev/core/fbmem.c +2
-4int i, j;for (i = height; i--; ) {@@ -113,15 +113,18 @@ void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, dst[j+1 ] = tmp; src++; } - tmp = dst[idx]; - tmp &= mask; - tmp |= *src >> shift_low; - dst[idx] = tmp; + +/* Only consume another source byte if it contributes bits */ drivers/video/fbdev/core/fbmem.c void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height, · line 124
Quoted 10 lines
drivers/video/fbdev/core/fbmem.c +5
-1if (shift_high < mod) { + tmp = dst[idx]; + tmp &= mask; + tmp |= *src >> shift_low; + dst[idx] = tmp; tmp = *src << shift_high; dst[idx+1 ] = tmp; + src++; } - src++;Above you moved the src pointer inside the
if(), so every line processed may miss a ptr increment. This means the source would need to be different too, but it hasn't changed, as it's still used frombit_putcs_unaligned()which prints a char from the character fonts.
Quoted 3 lines
So, I believe this part at least is wrong.
Did you test it?
I couldn't find syzbot's ReproC, so I did minimal one, I will re-test it and write you.
Quoted 1 line
Helge
BR,
Osama