2022-03-02 16:19:56

by Zheyu Ma

[permalink] [raw]
Subject: [PATCH] video: fbdev: sm712fb: Fix crash in smtcfb_write()

When the sm712fb driver writes three bytes to the framebuffer, the
driver will crash:

BUG: unable to handle page fault for address: ffffc90001ffffff
RIP: 0010:smtcfb_write+0x454/0x5b0
Call Trace:
vfs_write+0x291/0xd60
? do_sys_openat2+0x27d/0x350
? __fget_light+0x54/0x340
ksys_write+0xce/0x190
do_syscall_64+0x43/0x90
entry_SYSCALL_64_after_hwframe+0x44/0xae

Fix it by removing the open-coded endianness fixup-code.

Signed-off-by: Zheyu Ma <[email protected]>
---
drivers/video/fbdev/sm712fb.c | 21 ++++-----------------
1 file changed, 4 insertions(+), 17 deletions(-)

diff --git a/drivers/video/fbdev/sm712fb.c b/drivers/video/fbdev/sm712fb.c
index 0dbc6bf8268a..e355089ac7d6 100644
--- a/drivers/video/fbdev/sm712fb.c
+++ b/drivers/video/fbdev/sm712fb.c
@@ -1130,7 +1130,7 @@ static ssize_t smtcfb_write(struct fb_info *info, const char __user *buf,
count = total_size - p;
}

- buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
+ buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!buffer)
return -ENOMEM;

@@ -1148,24 +1148,11 @@ static ssize_t smtcfb_write(struct fb_info *info, const char __user *buf,
break;
}

- for (i = c >> 2; i--;) {
- fb_writel(big_swap(*src), dst++);
+ for (i = (c + 3) >> 2; i--;) {
+ fb_writel(big_swap(*src), dst);
+ dst++;
src++;
}
- if (c & 3) {
- u8 *src8 = (u8 *)src;
- u8 __iomem *dst8 = (u8 __iomem *)dst;
-
- for (i = c & 3; i--;) {
- if (i & 1) {
- fb_writeb(*src8++, ++dst8);
- } else {
- fb_writeb(*src8++, --dst8);
- dst8 += 2;
- }
- }
- dst = (u32 __iomem *)dst8;
- }

*ppos += c;
buf += c;
--
2.25.1


2022-03-02 20:32:40

by Helge Deller

[permalink] [raw]
Subject: Re: [PATCH] video: fbdev: sm712fb: Fix crash in smtcfb_write()

On 3/2/22 15:33, Zheyu Ma wrote:
> When the sm712fb driver writes three bytes to the framebuffer, the
> driver will crash:
>
> BUG: unable to handle page fault for address: ffffc90001ffffff
> RIP: 0010:smtcfb_write+0x454/0x5b0
> Call Trace:
> vfs_write+0x291/0xd60
> ? do_sys_openat2+0x27d/0x350
> ? __fget_light+0x54/0x340
> ksys_write+0xce/0x190
> do_syscall_64+0x43/0x90
> entry_SYSCALL_64_after_hwframe+0x44/0xae
>
> Fix it by removing the open-coded endianness fixup-code.
>
> Signed-off-by: Zheyu Ma <[email protected]>

Thanks... it's already in the fbdev git tree and queued up for v5.18...
https://git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev.git/commit/?h=for-next&id=bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8

Helge


> ---
> drivers/video/fbdev/sm712fb.c | 21 ++++-----------------
> 1 file changed, 4 insertions(+), 17 deletions(-)
>
> diff --git a/drivers/video/fbdev/sm712fb.c b/drivers/video/fbdev/sm712fb.c
> index 0dbc6bf8268a..e355089ac7d6 100644
> --- a/drivers/video/fbdev/sm712fb.c
> +++ b/drivers/video/fbdev/sm712fb.c
> @@ -1130,7 +1130,7 @@ static ssize_t smtcfb_write(struct fb_info *info, const char __user *buf,
> count = total_size - p;
> }
>
> - buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
> + buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
> if (!buffer)
> return -ENOMEM;
>
> @@ -1148,24 +1148,11 @@ static ssize_t smtcfb_write(struct fb_info *info, const char __user *buf,
> break;
> }
>
> - for (i = c >> 2; i--;) {
> - fb_writel(big_swap(*src), dst++);
> + for (i = (c + 3) >> 2; i--;) {
> + fb_writel(big_swap(*src), dst);
> + dst++;
> src++;
> }
> - if (c & 3) {
> - u8 *src8 = (u8 *)src;
> - u8 __iomem *dst8 = (u8 __iomem *)dst;
> -
> - for (i = c & 3; i--;) {
> - if (i & 1) {
> - fb_writeb(*src8++, ++dst8);
> - } else {
> - fb_writeb(*src8++, --dst8);
> - dst8 += 2;
> - }
> - }
> - dst = (u32 __iomem *)dst8;
> - }
>
> *ppos += c;
> buf += c;

2022-03-03 01:36:23

by Zheyu Ma

[permalink] [raw]
Subject: Re: [PATCH] video: fbdev: sm712fb: Fix crash in smtcfb_write()

Hi,

On Thu, Mar 3, 2022 at 12:49 AM Helge Deller <[email protected]> wrote:
>
> On 3/2/22 15:33, Zheyu Ma wrote:
> > When the sm712fb driver writes three bytes to the framebuffer, the
> > driver will crash:
> >
> > BUG: unable to handle page fault for address: ffffc90001ffffff
> > RIP: 0010:smtcfb_write+0x454/0x5b0
> > Call Trace:
> > vfs_write+0x291/0xd60
> > ? do_sys_openat2+0x27d/0x350
> > ? __fget_light+0x54/0x340
> > ksys_write+0xce/0x190
> > do_syscall_64+0x43/0x90
> > entry_SYSCALL_64_after_hwframe+0x44/0xae
> >
> > Fix it by removing the open-coded endianness fixup-code.
> >
> > Signed-off-by: Zheyu Ma <[email protected]>
>
> Thanks... it's already in the fbdev git tree and queued up for v5.18...
> https://git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev.git/commit/?h=for-next&id=bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8

This patch fixes the crash in smtcfb_read(), but there is a similar
bug in smtcfb_write(), and I mocked up your patch a wrote a new patch
for it.
So we should fix two bugs with two patches.

Regards,
Zheyu Ma

2022-03-03 12:39:11

by Helge Deller

[permalink] [raw]
Subject: Re: [PATCH] video: fbdev: sm712fb: Fix crash in smtcfb_write()

On 3/3/22 02:34, Zheyu Ma wrote:
> Hi,
>
> On Thu, Mar 3, 2022 at 12:49 AM Helge Deller <[email protected]> wrote:
>>
>> On 3/2/22 15:33, Zheyu Ma wrote:
>>> When the sm712fb driver writes three bytes to the framebuffer, the
>>> driver will crash:
>>>
>>> BUG: unable to handle page fault for address: ffffc90001ffffff
>>> RIP: 0010:smtcfb_write+0x454/0x5b0
>>> Call Trace:
>>> vfs_write+0x291/0xd60
>>> ? do_sys_openat2+0x27d/0x350
>>> ? __fget_light+0x54/0x340
>>> ksys_write+0xce/0x190
>>> do_syscall_64+0x43/0x90
>>> entry_SYSCALL_64_after_hwframe+0x44/0xae
>>>
>>> Fix it by removing the open-coded endianness fixup-code.
>>>
>>> Signed-off-by: Zheyu Ma <[email protected]>
>>
>> Thanks... it's already in the fbdev git tree and queued up for v5.18...
>> https://git.kernel.org/pub/scm/linux/kernel/git/deller/linux-fbdev.git/commit/?h=for-next&id=bd771cf5c4254511cc4abb88f3dab3bd58bdf8e8
>
> This patch fixes the crash in smtcfb_read(), but there is a similar
> bug in smtcfb_write(), and I mocked up your patch a wrote a new patch
> for it.
> So we should fix two bugs with two patches.

Right, I missed the read() code.

I applied your patch now to the fbdev for-next git tree.

Thanks!
Helge


>
> Regards,
> Zheyu Ma