Skip to contentSkip to conversation

video: imsttfb: Fix use after free bug in imsttfb_probe due to lack of error-handling of init_imstt

PATCH Applied Zheng Wang 4 participants 17 messages Apr 27, 2023 – Jun 7, 2023

Subsystems (3)
lore
Share & tools
lore

Zheng Wang

author
#0 lore raw

PATCH v2 1/1 video: imsttfb: Fix use after free bug in imsttfb_probe due to lack of error-handling of init_imstt

A use-after-free bug may occur if init_imstt invokes framebuffer_release and free the info ptr. The caller, imsttfb_probe didn't notice that and still keep the ptr as private data in pdev.

If we remove the driver which will call imsttfb_remove to make cleanup, UAF happens.

Fix it by return error code if bad case happens in init_imstt.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
v2:
- add return error code in another location.

---
 drivers/video/fbdev/imsttfb.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/video/fbdev/imsttfb.c +8 -7
index bea45647184e..975dd682fae4 100644
@@ -1347,7 +1347,7 @@ static const struct fb_ops imsttfb_ops = {
 	.fb_ioctl 	= imsttfb_ioctl,
 };
 
-static void init_imstt(struct fb_info *info)
+static int init_imstt(struct fb_info *info)
 {
 	struct imstt_par *par = info->par;
 	__u32 i, tmp, *ip, *end;
@@ -1420,7 +1420,7 @@ static void init_imstt(struct fb_info *info)
 	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
 		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
 		framebuffer_release(info);
-		return;
+		return -ENODEV;
 	}
 
 	sprintf(info->fix.id, "IMS TT (%s)", par->ramdac == IBM ? "IBM" : "TVP");
@@ -1456,12 +1456,13 @@ static void init_imstt(struct fb_info *info)
 
 	if (register_framebuffer(info) < 0) {
 		framebuffer_release(info);
-		return;
+		return -ENODEV;
 	}
 
 	tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8;
 	fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n",
 		info->fix.id, info->fix.smem_len >> 20, tmp);
+	return 0;
 }
 
 static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
@@ -1529,10 +1530,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (!par->cmap_regs)
 		goto error;
 	info->pseudo_palette = par->palette;
-	init_imstt(info);
-
-	pci_set_drvdata(pdev, info);
-	return 0;
+	ret = init_imstt(info);
+	if (!ret)
+		pci_set_drvdata(pdev, info);
+	return ret;
 
 error:
 	if (par->dc_regs)
Signature · 1 line
2.25.1
maintainer Applied
#1 lore raw
Quoted 11 lines · Zheng Wang

A use-after-free bug may occur if init_imstt invokes framebuffer_release and free the info ptr. The caller, imsttfb_probe didn't notice that and still keep the ptr as private data in pdev.

If we remove the driver which will call imsttfb_remove to make cleanup, UAF happens.

Fix it by return error code if bad case happens in init_imstt.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

applied.
Thanks!
Helge

Quoted 59 lines

---
v2:
- add return error code in another location.

---
  drivers/video/fbdev/imsttfb.c | 15 ++++++++-------
  1 file changed, 8 insertions(+), 7 deletions(-)
drivers/video/fbdev/imsttfb.c +8 -7
index bea45647184e..975dd682fae4 100644
@@ -1347,7 +1347,7 @@ static const struct fb_ops imsttfb_ops = {
  	.fb_ioctl 	= imsttfb_ioctl,
  };
 
-static void init_imstt(struct fb_info *info)
+static int init_imstt(struct fb_info *info)
  {
  	struct imstt_par *par = info->par;
  	__u32 i, tmp, *ip, *end;
@@ -1420,7 +1420,7 @@ static void init_imstt(struct fb_info *info)
  	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
  		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
  		framebuffer_release(info);
-		return;
+		return -ENODEV;
  	}
 
  	sprintf(info->fix.id, "IMS TT (%s)", par->ramdac == IBM ? "IBM" : "TVP");
@@ -1456,12 +1456,13 @@ static void init_imstt(struct fb_info *info)
 
  	if (register_framebuffer(info) < 0) {
  		framebuffer_release(info);
-		return;
+		return -ENODEV;
  	}
 
  	tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8;
  	fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n",
  		info->fix.id, info->fix.smem_len >> 20, tmp);
+	return 0;
  }
 
  static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
@@ -1529,10 +1530,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  	if (!par->cmap_regs)
  		goto error;
  	info->pseudo_palette = par->palette;
-	init_imstt(info);
-
-	pci_set_drvdata(pdev, info);
-	return 0;
+	ret = init_imstt(info);
+	if (!ret)
+		pci_set_drvdata(pdev, info);
+	return ret;
 
  error:
  	if (par->dc_regs)
#2 lore raw
In reply to Zheng Wang

Hello.

Quoted 6 lines · Zheng Wang

static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)

(quoted diff) +0 -1
@@ -1529,10 +1530,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	if (!par->cmap_regs)
 		goto error;
 	info->pseudo_palette = par->palette;
-	init_imstt(info);

Review static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 1539

Quoted 10 lines
(quoted diff) +4 -3
-
-	pci_set_drvdata(pdev, info);
-	return 0;
+	ret = init_imstt(info);
+	if (!ret)
+		pci_set_drvdata(pdev, info);
+	return ret;
 
 error:
 	if (par->dc_regs)

This part caught my eye -- shouldn't the -ENODEV from init_imstt go through the standard error with proper cleanup? (It seems like a leak from my 30000 ft view, i.e. not sure about imsttfb_{probe,remove} pairing.)

Shouldn't there be something like the diff below on top of the existing code?

Regards,
Michal

drivers/video/fbdev/imsttfb.c +4 -4
index 975dd682fae4..a116ac8ca020 100644
@@ -1419,7 +1419,6 @@ static int init_imstt(struct fb_info *info)
 	if ((info->var.xres * info->var.yres) * (info->var.bits_per_pixel >> 3) > info->fix.smem_len
 	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
 		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
-		framebuffer_release(info);
 		return -ENODEV;
 	}
 
@@ -1455,7 +1454,6 @@ static int init_imstt(struct fb_info *info)
 	fb_alloc_cmap(&info->cmap, 0, 0);
 
 	if (register_framebuffer(info) < 0) {
-		framebuffer_release(info);
 		return -ENODEV;
 	}
 
@@ -1531,8 +1529,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 		goto error;
 	info->pseudo_palette = par->palette;
 	ret = init_imstt(info);
-	if (!ret)
-		pci_set_drvdata(pdev, info);
+	if (ret)
+		goto error;
+
+	pci_set_drvdata(pdev, info);
 	return ret;
 
 error:
maintainer
#3 lore raw

Hi Michal,

Quoted 15 lines · Michal Koutný
Quoted 14 lines · Zheng Wang

static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)

(quoted diff) +4 -4
@@ -1529,10 +1530,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  	if (!par->cmap_regs)
  		goto error;
  	info->pseudo_palette = par->palette;
-	init_imstt(info);
-
-	pci_set_drvdata(pdev, info);
-	return 0;
+	ret = init_imstt(info);
+	if (!ret)
+		pci_set_drvdata(pdev, info);
+	return ret;
 
Quoted 7 lines

Review static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 1539

Quoted 2 lines
(quoted diff) +0 -0
  error:
  	if (par->dc_regs)

This part caught my eye -- shouldn't the -ENODEV from init_imstt go through the standard error with proper cleanup? (It seems like a leak from my 30000 ft view, i.e. not sure about imsttfb_{probe,remove} pairing.)

Yes, you seem to be right.

Quoted 1 line

Shouldn't there be something like the diff below on top of the existing code?

Yes, but ....

Quoted 16 lines

Regards,
Michal

drivers/video/fbdev/imsttfb.c +0 -1
index 975dd682fae4..a116ac8ca020 100644
@@ -1419,7 +1419,6 @@ static int init_imstt(struct fb_info *info)
  	if ((info->var.xres * info->var.yres) * (info->var.bits_per_pixel >> 3) > info->fix.smem_len
  	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
  		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
-		framebuffer_release(info);
  		return -ENODEV;
  	}
 

drivers/video/fbdev/imsttfb.c static int init_imstt(struct fb_info *info) · line 1458

Quoted 4 lines
drivers/video/fbdev/imsttfb.c +0 -1
@@ -1455,7 +1454,6 @@ static int init_imstt(struct fb_info *info)
  	fb_alloc_cmap(&info->cmap, 0, 0);
 
  	if (register_framebuffer(info) < 0) {
-		framebuffer_release(info);

That's ^^^ ok, but I think a
fb_dealloc_cmap()
is missing here ...

... and in the error path of imsttfb_probe() ....

Quoted 6 lines
drivers/video/fbdev/imsttfb.c +0 -0
  		return -ENODEV;
  	}
 
@@ -1531,8 +1529,10 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  		goto error;
  	info->pseudo_palette = par->palette;

drivers/video/fbdev/imsttfb.c static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 1538

Quoted 10 lines
drivers/video/fbdev/imsttfb.c +4 -2
  	ret = init_imstt(info);
-	if (!ret)
-		pci_set_drvdata(pdev, info);
+	if (ret)
+		goto error;
+
+	pci_set_drvdata(pdev, info);
  	return ret;
 
  error:

Would you mind sending a proper patch?

Thanks for noticing!
Helge

#4 lore raw

cover fbdev: imsttfb: Adjustments for two function implementations

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 19:07:19 +0200

Some update suggestions were taken into account
from static source code analysis.

Markus Elfring (4):
Fix error handling in init_imstt()
Fix exception handling in imsttfb_probe()
Move a variable assignment for an error code in imsttfb_probe() Improve a size determination in imsttfb_probe()

 drivers/video/fbdev/imsttfb.c | 56 ++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 20 deletions(-)

--
2.40.1

#5 lore raw

PATCH 1/4 fbdev: imsttfb: Fix error handling in init_imstt()

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 14:32:39 +0200

The return value was overlooked from a call of
the function “fb_alloc_cmap”.

* Thus use a corresponding error check.

* Add two jump targets so that a bit of exception handling can be better reused at the end of this function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes c75f5a550610 ("fbdev: imsttfb: Fix use after free bug in imsttfb_probe")
---
 drivers/video/fbdev/imsttfb.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/video/fbdev/imsttfb.c +13 -5
index 975dd682fae4..d3532def4707 100644
@@ -1351,6 +1351,7 @@ static int init_imstt(struct fb_info *info)
 {
 	struct imstt_par *par = info->par;
 	__u32 i, tmp, *ip, *end;
+	int ret;
 
 	tmp = read_reg_le32(par->dc_regs, PRC);
 	if (par->ramdac == IBM)
@@ -1419,8 +1420,7 @@ static int init_imstt(struct fb_info *info)
 	if ((info->var.xres * info->var.yres) * (info->var.bits_per_pixel >> 3) > info->fix.smem_len
 	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
 		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
-		framebuffer_release(info);
-		return -ENODEV;
+		goto e_nodev;
 	}
 
 	sprintf(info->fix.id, "IMS TT (%s)", par->ramdac == IBM ? "IBM" : "TVP");
@@ -1452,17 +1452,25 @@ static int init_imstt(struct fb_info *info)
 	              FBINFO_HWACCEL_FILLRECT |
 	              FBINFO_HWACCEL_YPAN;
 
-	fb_alloc_cmap(&info->cmap, 0, 0);
+	ret = fb_alloc_cmap(&info->cmap, 0, 0);
+	if (ret)
+		goto release_framebuffer;
 
 	if (register_framebuffer(info) < 0) {
-		framebuffer_release(info);
-		return -ENODEV;
+		fb_dealloc_cmap(&info->cmap);
+		goto e_nodev;
 	}
 
 	tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8;
 	fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n",
 		info->fix.id, info->fix.smem_len >> 20, tmp);
 	return 0;
+
+e_nodev:
+	ret = -ENODEV;
+release_framebuffer:
+	framebuffer_release(info);
+	return ret;
 }
 
 static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)

--
2.40.1

maintainer
#6 lore raw

Re: [PATCH 1/4] fbdev: imsttfb: Fix error handling in init_imstt()

Quoted 56 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 14:32:39 +0200

The return value was overlooked from a call of
the function “fb_alloc_cmap”.

* Thus use a corresponding error check.

* Add two jump targets so that a bit of exception handling can be better reused at the end of this function.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Fixes c75f5a550610 ("fbdev: imsttfb: Fix use after free bug in imsttfb_probe")
---
  drivers/video/fbdev/imsttfb.c | 18 +++++++++++++-----
  1 file changed, 13 insertions(+), 5 deletions(-)
drivers/video/fbdev/imsttfb.c +6 -5
index 975dd682fae4..d3532def4707 100644
@@ -1351,6 +1351,7 @@ static int init_imstt(struct fb_info *info)
  {
  	struct imstt_par *par = info->par;
  	__u32 i, tmp, *ip, *end;
+	int ret;
 
  	tmp = read_reg_le32(par->dc_regs, PRC);
  	if (par->ramdac == IBM)
@@ -1419,8 +1420,7 @@ static int init_imstt(struct fb_info *info)
  	if ((info->var.xres * info->var.yres) * (info->var.bits_per_pixel >> 3) > info->fix.smem_len
  	    || !(compute_imstt_regvals(par, info->var.xres, info->var.yres))) {
  		printk("imsttfb: %ux%ux%u not supported\n", info->var.xres, info->var.yres, info->var.bits_per_pixel);
-		framebuffer_release(info);
-		return -ENODEV;
+		goto e_nodev;
  	}
 
  	sprintf(info->fix.id, "IMS TT (%s)", par->ramdac == IBM ? "IBM" : "TVP");
@@ -1452,17 +1452,25 @@ static int init_imstt(struct fb_info *info)
  	              FBINFO_HWACCEL_FILLRECT |
  	              FBINFO_HWACCEL_YPAN;
 
-	fb_alloc_cmap(&info->cmap, 0, 0);
+	ret = fb_alloc_cmap(&info->cmap, 0, 0);
+	if (ret)
+		goto release_framebuffer;
 
  	if (register_framebuffer(info) < 0) {
-		framebuffer_release(info);
-		return -ENODEV;
+		fb_dealloc_cmap(&info->cmap);

drivers/video/fbdev/imsttfb.c static int init_imstt(struct fb_info *info) · line 1470

Quoted 10 lines
drivers/video/fbdev/imsttfb.c +4 -0
+		goto e_nodev;
  	}
 
  	tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8;
  	fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n",
  		info->fix.id, info->fix.smem_len >> 20, tmp);
  	return 0;
+
+e_nodev:
+	ret = -ENODEV;

I think the return value isn't checked at all, so you could simply return below "-ENODEV" for all cases (instead of "return ret"). Then you don't need the e_nodev label and can simplify the flow.

Helge

Quoted 9 lines
drivers/video/fbdev/imsttfb.c +3 -1
+release_framebuffer:
+	framebuffer_release(info);
+	return ret;
  }
 
  static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
--

2.40.1

#7 lore raw

Re: [cocci] [PATCH 1/4] fbdev: imsttfb: Fix error handling in init_imstt()

Quoted 7 lines
Quoted 7 lines

The return value was overlooked from a call of
the function “fb_alloc_cmap”.

* Thus use a corresponding error check.

* Add two jump targets so that a bit of exception handling can be better reused at the end of this function.

…

Quoted 1 line
Quoted 1 line

+++ b/drivers/video/fbdev/imsttfb.c

…

Quoted 16 lines
Quoted 16 lines
(quoted diff) +0 -0
@@ -1452,17 +1452,25 @@ static int init_imstt(struct fb_info *info)

                    FBINFO_HWACCEL_FILLRECT |
                    FBINFO_HWACCEL_YPAN;

-    fb_alloc_cmap(&info->cmap, 0, 0);
+    ret = fb_alloc_cmap(&info->cmap, 0, 0);
+    if (ret)
+        goto release_framebuffer;

      if (register_framebuffer(info) < 0) {
-        framebuffer_release(info);
-        return -ENODEV;
+        fb_dealloc_cmap(&info->cmap);
+        goto e_nodev;
      }

Quoted 11 lines

drivers/video/fbdev/imsttfb.c static int init_imstt(struct fb_info *info) · line 1470

Quoted 7 lines

      tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8; fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n", info->fix.id, info->fix.smem_len >> 20, tmp); return 0;

drivers/video/fbdev/imsttfb.c +3 -0
+
+e_nodev:
+    ret = -ENODEV;

I think the return value isn't checked at all, so you could simply return below "-ENODEV" for all cases (instead of "return ret"). Then you don't need the e_nodev label and can simplify the flow.

Can it be helpful to distinguish involved error codes better?

How should return value ignorance be avoided further?

Regards,
Markus

maintainer
#8 lore raw

Re: [PATCH 1/4] fbdev: imsttfb: Fix error handling in init_imstt()

Quoted 38 lines · Markus Elfring
Quoted 7 lines
Quoted 7 lines

The return value was overlooked from a call of
the function “fb_alloc_cmap”.

* Thus use a corresponding error check.

* Add two jump targets so that a bit of exception handling can be better reused at the end of this function.

…

Quoted 1 line
Quoted 1 line

+++ b/drivers/video/fbdev/imsttfb.c

…

Quoted 16 lines
Quoted 16 lines
drivers/video/fbdev/imsttfb.c +5 -3
@@ -1452,17 +1452,25 @@ static int init_imstt(struct fb_info *info)
                     FBINFO_HWACCEL_FILLRECT |
                     FBINFO_HWACCEL_YPAN;
 
-    fb_alloc_cmap(&info->cmap, 0, 0);
+    ret = fb_alloc_cmap(&info->cmap, 0, 0);
+    if (ret)
+        goto release_framebuffer;
 
       if (register_framebuffer(info) < 0) {
-        framebuffer_release(info);
-        return -ENODEV;
+        fb_dealloc_cmap(&info->cmap);
+        goto e_nodev;
       }
 
Quoted 11 lines

drivers/video/fbdev/imsttfb.c static int init_imstt(struct fb_info *info) · line 1470

Quoted 7 lines
drivers/video/fbdev/imsttfb.c +3 -0
       tmp = (read_reg_le32(par->dc_regs, SSTATUS) & 0x0f00) >> 8;
       fb_info(info, "%s frame buffer; %uMB vram; chip version %u\n",
           info->fix.id, info->fix.smem_len >> 20, tmp);
       return 0;
+
+e_nodev:
+    ret = -ENODEV;

I think the return value isn't checked at all, so you could simply return below "-ENODEV" for all cases (instead of "return ret"). Then you don't need the e_nodev label and can simplify the flow.

Quoted 1 line

Can it be helpful to distinguish involved error codes better?

No.

Helge

#9 lore raw

Re: [cocci] [1/4] fbdev: imsttfb: Fix error handling in init_imstt()

Quoted 3 lines
Quoted 1 line

Can it be helpful to distinguish involved error codes better?

No.

I find such a feedback surprising.

May the error code be preserved from a failed call of the function “fb_alloc_cmap”?

Regards,
Markus

#10 lore raw

Re: [cocci] [1/4] fbdev: imsttfb: Fix error handling in init_imstt()

In reply to Helge Deller
Quoted 3 lines

I think the return value isn't checked at all, so you could simply return below "-ENODEV" for all cases (instead of "return ret"). Then you don't need the e_nodev label and can simplify the flow.

I noticed that the software evolution was continued with an other change variant.

fbdev: imsttfb: Release framebuffer and dealloc cmap on error path
https://lore.kernel.org/linux-fbdev/[email protected]/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/video/fbdev/imsttfb.c?h=v6.4-rc5&id=5cf9a090a39c97f4506b7b53739d469b1c05a7e9

Regards,
Markus

#11 lore raw

PATCH 2/4 fbdev: imsttfb: Fix exception handling in imsttfb_probe()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 15:40:43 +0200

The exception handling was incomplete.

The label “error” was used to jump to another pointer check despite of the detail in the implementation of the function “imsttfb_probe” that it was determined already that the corresponding variable contained a null pointer.

* Thus use more appropriate labels instead.

* Delete two redundant checks.

---
 drivers/video/fbdev/imsttfb.c | 34 +++++++++++++++++++++-------------
 1 file changed, 21 insertions(+), 13 deletions(-)
drivers/video/fbdev/imsttfb.c +21 -13
index d3532def4707..6490f544f8eb 100644
@@ -1503,8 +1503,8 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
 	if (!request_mem_region(addr, size, "imsttfb")) {
 		printk(KERN_ERR "imsttfb: Can't reserve memory region\n");
-		framebuffer_release(info);
-		return -ENODEV;
+		ret = -ENODEV;
+		goto release_framebuffer;
 	}
 
 	switch (pdev->device) {
@@ -1521,34 +1521,42 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 			printk(KERN_INFO "imsttfb: Device 0x%x unknown, "
 					 "contact maintainer.\n", pdev->device);
 			ret = -ENODEV;
-			goto error;
+			goto release_region;
 	}
 
 	info->fix.smem_start = addr;
 	info->screen_base = (__u8 *)ioremap(addr, par->ramdac == IBM ?
 					    0x400000 : 0x800000);
 	if (!info->screen_base)
-		goto error;
+		goto release_region;
+
 	info->fix.mmio_start = addr + 0x800000;
 	par->dc_regs = ioremap(addr + 0x800000, 0x1000);
 	if (!par->dc_regs)
-		goto error;
+		goto unmap_io_screen_base;
+
 	par->cmap_regs_phys = addr + 0x840000;
 	par->cmap_regs = (__u8 *)ioremap(addr + 0x840000, 0x1000);
 	if (!par->cmap_regs)
-		goto error;
+		goto unmap_io_dc_regs;
+
 	info->pseudo_palette = par->palette;
 	ret = init_imstt(info);
-	if (!ret)
-		pci_set_drvdata(pdev, info);
+	if (ret)
+		goto unmap_io_cmap_regs;
+
+	pci_set_drvdata(pdev, info);
 	return ret;
 
-error:
-	if (par->dc_regs)
-		iounmap(par->dc_regs);
-	if (info->screen_base)
-		iounmap(info->screen_base);
+unmap_io_cmap_regs:
+	iounmap(par->cmap_regs);
+unmap_io_dc_regs:
+	iounmap(par->dc_regs);
+unmap_io_screen_base:
+	iounmap(info->screen_base);
+release_region:
 	release_mem_region(addr, size);
+release_framebuffer:
 	framebuffer_release(info);
 	return ret;
 }

--
2.40.1

#12 lore raw

Re: [cocci] [2/4] fbdev: imsttfb: Fix exception handling in imsttfb_probe()

Quoted 10 lines

The exception handling was incomplete.

The label “error” was used to jump to another pointer check despite of the detail in the implementation of the function “imsttfb_probe” that it was determined already that the corresponding variable contained a null pointer.

* Thus use more appropriate labels instead.

* Delete two redundant checks.

I noticed that the software evolution was continued with an other change variant.

fbdev: imsttfb: Fix error path of imsttfb_probe()
https://lore.kernel.org/linux-fbdev/[email protected]/
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/drivers/video/fbdev/imsttfb.c?h=v6.4-rc5&id=518ecb6a209f6ff678aeadf9f2bf870c0982ca85

Regards,
Markus

#13 lore raw

PATCH 3/4 fbdev: imsttfb: Move a variable assignment for an error code in imsttfb_probe()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 18:30:26 +0200

The value “-ENOMEM” was assigned to the variable “ret” at the beginning.
Move this statement directly before the first ioremap() call.

---
 drivers/video/fbdev/imsttfb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/video/fbdev/imsttfb.c +1 -1
index 6490f544f8eb..90c72428e8d7 100644
@@ -1484,7 +1484,6 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	ret = aperture_remove_conflicting_pci_devices(pdev, "imsttfb");
 	if (ret)
 		return ret;
-	ret = -ENOMEM;
 
 	dp = pci_device_to_OF_node(pdev);
 	if(dp)
@@ -1525,6 +1524,7 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	}
 
 	info->fix.smem_start = addr;
+	ret = -ENOMEM;
 	info->screen_base = (__u8 *)ioremap(addr, par->ramdac == IBM ?
 					    0x400000 : 0x800000);
 	if (!info->screen_base)

--
2.40.1

maintainer
#14 lore raw

Re: [PATCH 3/4] fbdev: imsttfb: Move a variable assignment for an error code in imsttfb_probe()

Quoted 6 lines · Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 18:30:26 +0200

The value “-ENOMEM” was assigned to the variable “ret” at the beginning.
Move this statement directly before the first ioremap() call.

Please do not move such variables without real need.
It makes backporting (of this and maybe follow-up patches) much more complicated and the compiler will optimize it anyway.

Thanks!
Helge

Quoted 29 lines
---
  drivers/video/fbdev/imsttfb.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/video/fbdev/imsttfb.c +1 -2
index 6490f544f8eb..90c72428e8d7 100644
@@ -1484,7 +1484,6 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  	ret = aperture_remove_conflicting_pci_devices(pdev, "imsttfb");
  	if (ret)
  		return ret;
-	ret = -ENOMEM;
 
  	dp = pci_device_to_OF_node(pdev);
  	if(dp)
@@ -1525,6 +1524,7 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  	}
 
  	info->fix.smem_start = addr;
+	ret = -ENOMEM;
  	info->screen_base = (__u8 *)ioremap(addr, par->ramdac == IBM ?
  					    0x400000 : 0x800000);
  	if (!info->screen_base)
--

2.40.1

#15 lore raw

Re: [cocci] [PATCH 3/4] fbdev: imsttfb: Move a variable assignment for an error code in imsttfb_probe()

Quoted 5 lines
Quoted 3 lines

The value “-ENOMEM” was assigned to the variable “ret” at the beginning.
Move this statement directly before the first ioremap() call.

Please do not move such variables without real need.

Is there a need to explain desirable effects better?

Quoted 1 line

It makes backporting (of this and maybe follow-up patches) much more complicated

I propose to reconsider such development concerns a bit more.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/log/drivers/video/fbdev/imsttfb.c?h=v6.4-rc3

Quoted 1 line

and the compiler will optimize it anyway.

How much do expectations fit to supported and documented software optimisations?

Regards,
Markus

#16 lore raw

PATCH 4/4 fbdev: imsttfb: Improve a size determination in imsttfb_probe()

In reply to Markus Elfring

From: Markus Elfring <[email protected]>
Date: Tue, 23 May 2023 18:50:53 +0200

Replace the specification of a data structure by a pointer dereference as the parameter for the operator "sizeof" to make the corresponding size determination a bit safer according to the Linux coding style convention.

---
 drivers/video/fbdev/imsttfb.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/video/fbdev/imsttfb.c +1 -1
index 90c72428e8d7..346e0166c711 100644
@@ -1491,7 +1491,7 @@ static int imsttfb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 	else if (IS_ENABLED(CONFIG_OF))
 		printk(KERN_ERR "imsttfb: no OF node for pci device\n");
 
-	info = framebuffer_alloc(sizeof(struct imstt_par), &pdev->dev);
+	info = framebuffer_alloc(sizeof(*par), &pdev->dev);
 	if (!info)
 		return -ENOMEM;
 

--
2.40.1