2021-05-24 15:35:55

by Greg Kroah-Hartman

[permalink] [raw]
Subject: [PATCH 4.4 28/31] video: hgafb: fix potential NULL pointer dereference

From: Igor Matheus Andrade Torrente <[email protected]>

commit dc13cac4862cc68ec74348a80b6942532b7735fa upstream.

The return of ioremap if not checked, and can lead to a NULL to be
assigned to hga_vram. Potentially leading to a NULL pointer
dereference.

The fix adds code to deal with this case in the error label and
changes how the hgafb_probe handles the return of hga_card_detect.

Cc: Ferenc Bakonyi <[email protected]>
Cc: Bartlomiej Zolnierkiewicz <[email protected]>
Cc: stable <[email protected]>
Signed-off-by: Igor Matheus Andrade Torrente <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
---
drivers/video/fbdev/hgafb.c | 21 +++++++++++++--------
1 file changed, 13 insertions(+), 8 deletions(-)

--- a/drivers/video/fbdev/hgafb.c
+++ b/drivers/video/fbdev/hgafb.c
@@ -285,6 +285,8 @@ static int hga_card_detect(void)
hga_vram_len = 0x08000;

hga_vram = ioremap(0xb0000, hga_vram_len);
+ if (!hga_vram)
+ return -ENOMEM;

if (request_region(0x3b0, 12, "hgafb"))
release_io_ports = 1;
@@ -344,13 +346,18 @@ static int hga_card_detect(void)
hga_type_name = "Hercules";
break;
}
- return 1;
+ return 0;
error:
if (release_io_ports)
release_region(0x3b0, 12);
if (release_io_port)
release_region(0x3bf, 1);
- return 0;
+
+ iounmap(hga_vram);
+
+ pr_err("hgafb: HGA card not detected.\n");
+
+ return -EINVAL;
}

/**
@@ -548,13 +555,11 @@ static struct fb_ops hgafb_ops = {
static int hgafb_probe(struct platform_device *pdev)
{
struct fb_info *info;
+ int ret;

- if (! hga_card_detect()) {
- printk(KERN_INFO "hgafb: HGA card not detected.\n");
- if (hga_vram)
- iounmap(hga_vram);
- return -EINVAL;
- }
+ ret = hga_card_detect();
+ if (!ret)
+ return ret;

printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
hga_type_name, hga_vram_len/1024);



2021-05-25 22:31:05

by Pavel Machek

[permalink] [raw]
Subject: Re: [PATCH 4.4 28/31] video: hgafb: fix potential NULL pointer dereference

Hi!

> From: Igor Matheus Andrade Torrente <[email protected]>
>
> commit dc13cac4862cc68ec74348a80b6942532b7735fa upstream.
>
> The return of ioremap if not checked, and can lead to a NULL to be
> assigned to hga_vram. Potentially leading to a NULL pointer
> dereference.
>
> The fix adds code to deal with this case in the error label and
> changes how the hgafb_probe handles the return of hga_card_detect.

This will break hgafb completely, right? And crash system without hga
card as a bonus.

> +++ b/drivers/video/fbdev/hgafb.c
> @@ -285,6 +285,8 @@ static int hga_card_detect(void)
> hga_vram_len = 0x08000;
>
> hga_vram = ioremap(0xb0000, hga_vram_len);
> + if (!hga_vram)
> + return -ENOMEM;
>
> if (request_region(0x3b0, 12, "hgafb"))
> release_io_ports = 1;
> @@ -344,13 +346,18 @@ static int hga_card_detect(void)
> hga_type_name = "Hercules";
> break;
> }
> - return 1;
> + return 0;

Ok, so calling convention is now "0 means detected".


> @@ -548,13 +555,11 @@ static struct fb_ops hgafb_ops = {
> static int hgafb_probe(struct platform_device *pdev)
> {
> struct fb_info *info;
> + int ret;
...
> + ret = hga_card_detect();
> + if (!ret)
> + return ret;
>
> printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
> hga_type_name, hga_vram_len/1024);
>

If the card is detected, 0 is returned, !0 is true, and we abort
detection....

Pavel

Signed-off-by: Pavel Machek <[email protected]>

diff --git a/drivers/video/fbdev/hgafb.c b/drivers/video/fbdev/hgafb.c
index c35f217db53f..d6a95ea49c64 100644
--- a/drivers/video/fbdev/hgafb.c
+++ b/drivers/video/fbdev/hgafb.c
@@ -282,7 +282,7 @@ static int hga_card_detect(void)
void __iomem *p, *q;
unsigned short p_save, q_save;

- hga_vram_len = 0x08000;
+ hga_vram_len = 0x08000;

hga_vram = ioremap(0xb0000, hga_vram_len);
if (!hga_vram)
@@ -558,7 +558,7 @@ static int hgafb_probe(struct platform_device *pdev)
int ret;

ret = hga_card_detect();
- if (!ret)
+ if (ret)
return ret;

printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",


--
DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany


Attachments:
(No filename) (2.27 kB)
signature.asc (201.00 B)
Download all attachments
Subject: Re: [PATCH 4.4 28/31] video: hgafb: fix potential NULL pointer dereference

Hi Pavel,

On 5/25/21 5:47 PM, Pavel Machek wrote:
> Hi!
>
>> From: Igor Matheus Andrade Torrente <[email protected]>
>>
>> commit dc13cac4862cc68ec74348a80b6942532b7735fa upstream.
>>
>> The return of ioremap if not checked, and can lead to a NULL to be
>> assigned to hga_vram. Potentially leading to a NULL pointer
>> dereference.
>>
>> The fix adds code to deal with this case in the error label and
>> changes how the hgafb_probe handles the return of hga_card_detect.
>
> This will break hgafb completely, right? And crash system without hga
> card as a bonus.
>
>> +++ b/drivers/video/fbdev/hgafb.c
>> @@ -285,6 +285,8 @@ static int hga_card_detect(void)
>> hga_vram_len = 0x08000;
>>
>> hga_vram = ioremap(0xb0000, hga_vram_len);
>> + if (!hga_vram)
>> + return -ENOMEM;
>>
>> if (request_region(0x3b0, 12, "hgafb"))
>> release_io_ports = 1;
>> @@ -344,13 +346,18 @@ static int hga_card_detect(void)
>> hga_type_name = "Hercules";
>> break;
>> }
>> - return 1;
>> + return 0;
>
> Ok, so calling convention is now "0 means detected".
>
>
>> @@ -548,13 +555,11 @@ static struct fb_ops hgafb_ops = {
>> static int hgafb_probe(struct platform_device *pdev)
>> {
>> struct fb_info *info;
>> + int ret;
> ...
>> + ret = hga_card_detect();
>> + if (!ret)
>> + return ret;
>>
>> printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
>> hga_type_name, hga_vram_len/1024);
>>
>
> If the card is detected, 0 is returned, !0 is true, and we abort
> detection....

Yes, you are right! There's a patch that fixes it:

https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git/commit/?h=char-misc-linus&id=02625c965239b71869326dd0461615f27307ecb3

As far as I know, this patch should be queue up soon to all stable branches.

Greg should have more details about it.

>
> Pavel
>
> Signed-off-by: Pavel Machek <[email protected]>
>
> diff --git a/drivers/video/fbdev/hgafb.c b/drivers/video/fbdev/hgafb.c
> index c35f217db53f..d6a95ea49c64 100644
> --- a/drivers/video/fbdev/hgafb.c
> +++ b/drivers/video/fbdev/hgafb.c
> @@ -282,7 +282,7 @@ static int hga_card_detect(void)
> void __iomem *p, *q;
> unsigned short p_save, q_save;
>
> - hga_vram_len = 0x08000;
> + hga_vram_len = 0x08000;
>
> hga_vram = ioremap(0xb0000, hga_vram_len);
> if (!hga_vram)
> @@ -558,7 +558,7 @@ static int hgafb_probe(struct platform_device *pdev)
> int ret;
>
> ret = hga_card_detect();
> - if (!ret)
> + if (ret)
> return ret;
>
> printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
>
>

Thanks,
---
Igor M. A. Torrente

2021-05-26 11:26:07

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 4.4 28/31] video: hgafb: fix potential NULL pointer dereference

On Tue, May 25, 2021 at 06:06:15PM -0300, Igor Torrente wrote:
> Hi Pavel,
>
> On 5/25/21 5:47 PM, Pavel Machek wrote:
> > Hi!
> >
> > > From: Igor Matheus Andrade Torrente <[email protected]>
> > >
> > > commit dc13cac4862cc68ec74348a80b6942532b7735fa upstream.
> > >
> > > The return of ioremap if not checked, and can lead to a NULL to be
> > > assigned to hga_vram. Potentially leading to a NULL pointer
> > > dereference.
> > >
> > > The fix adds code to deal with this case in the error label and
> > > changes how the hgafb_probe handles the return of hga_card_detect.
> >
> > This will break hgafb completely, right? And crash system without hga
> > card as a bonus.
> >
> > > +++ b/drivers/video/fbdev/hgafb.c
> > > @@ -285,6 +285,8 @@ static int hga_card_detect(void)
> > > hga_vram_len = 0x08000;
> > > hga_vram = ioremap(0xb0000, hga_vram_len);
> > > + if (!hga_vram)
> > > + return -ENOMEM;
> > > if (request_region(0x3b0, 12, "hgafb"))
> > > release_io_ports = 1;
> > > @@ -344,13 +346,18 @@ static int hga_card_detect(void)
> > > hga_type_name = "Hercules";
> > > break;
> > > }
> > > - return 1;
> > > + return 0;
> >
> > Ok, so calling convention is now "0 means detected".
> >
> >
> > > @@ -548,13 +555,11 @@ static struct fb_ops hgafb_ops = {
> > > static int hgafb_probe(struct platform_device *pdev)
> > > {
> > > struct fb_info *info;
> > > + int ret;
> > ...
> > > + ret = hga_card_detect();
> > > + if (!ret)
> > > + return ret;
> > > printk(KERN_INFO "hgafb: %s with %ldK of memory detected.\n",
> > > hga_type_name, hga_vram_len/1024);
> > >
> >
> > If the card is detected, 0 is returned, !0 is true, and we abort
> > detection....
>
> Yes, you are right! There's a patch that fixes it:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git/commit/?h=char-misc-linus&id=02625c965239b71869326dd0461615f27307ecb3
>
> As far as I know, this patch should be queue up soon to all stable branches.
>
> Greg should have more details about it.

Good catch, I'll go queue that up now, thanks.

greg k-h