Skip to contentSkip to conversation

media: amphion: fix potential NULL deref in vpu_core_parse_dt()

PATCH In review Chenyuan Yang 4 participants 6 messages Apr 11, 2025 – Apr 23, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: amphion: fix potential NULL deref in vpu_core_parse_dt()

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")
---
 drivers/media/platform/amphion/vpu_core.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/platform/amphion/vpu_core.c +11 -0
index 8df85c14ab3f..26568987586d 100644
@@ -586,7 +586,18 @@ static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np)
 	}
 
 	core->fw.virt = memremap(core->fw.phys, core->fw.length, MEMREMAP_WC);
+	if (!core->fw.virt) {
+		dev_err(core->dev, "failed to remap fw region\n");
+		of_node_put(node);
+		return -ENOMEM;
+	}
 	core->rpc.virt = memremap(core->rpc.phys, core->rpc.length, MEMREMAP_WC);
+	if (!core->rpc.virt) {
+		dev_err(core->dev, "failed to remap rpc region\n");
+		memunmap(core->fw.virt);
+		of_node_put(node);
+		return -ENOMEM;
+	}
 	memset(core->rpc.virt, 0, core->rpc.length);
 
 	ret = vpu_iface_check_memory_region(core, core->rpc.phys, core->rpc.length);
Signature · 1 line
2.34.1
#1 lore raw

Hi,

Le vendredi 11 avril 2025 à 13:43 -0500, Chenyuan Yang a écrit :

Quoted 5 lines

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

Its hard to believe that its a coincidence that someone else sent a patch for this. A colleague, the same person ?

I do prefer this version though, the commits message is better, the code is nicer. If its you, adding a [PATCH v2], or just adding a comment that its a better version would be nice.

Quoted 20 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")

---
 drivers/media/platform/amphion/vpu_core.c | 11 +++++++++++

 1 file changed, 11 insertions(+)
drivers/media/platform/amphion/vpu_core.c +0 -0
index 8df85c14ab3f..26568987586d 100644
@@ -586,7 +586,18 @@ static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np)
  	}


  core->fw.virt = memremap(core->fw.phys, core->fw.length, MEMREMAP_WC);

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 591

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+	if (!core->fw.virt) {
+		dev_err(core->dev, "failed to remap fw region\n");
+		of_node_put(node);

nit: node and res are no longer used passed line 579. Meaning you could unref the node earlier, and remove the repeated of_node_put(node) call in the error conditions.

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +2 -0
+		return -ENOMEM;
+	}

  core->rpc.virt = memremap(core->rpc.phys, core->rpc.length, MEMREMAP_WC);

I really enjoy blank lines after closing scope, even though its not a strict coding standard.

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 597

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+	if (!core->rpc.virt) {
+		dev_err(core->dev, "failed to remap rpc region\n");
+		memunmap(core->fw.virt);

Its interesting that you thought of cleaning that up here, since its not being cleanup in the error case of "if (ret != VPU_CORE_MEMORY_UNCACHED)". And its also not being cleanup if the probe fails later for other reasons. Perhaps your chance to add more fixes to this driver.

Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+		of_node_put(node);
+		return -ENOMEM;
+	}

  memset(core->rpc.virt, 0, core->rpc.length);

Same, I like blank lines (but you are free to ignore me if Ming does not care).

Quoted 2 lines


  ret = vpu_iface_check_memory_region(core, core->rpc.phys, core->rpc.length);

regards,
Nicolas

#2 lore raw

Le vendredi 11 avril 2025 à 17:20 -0400, Nicolas Dufresne a écrit :

Quoted 9 lines

Hi,

Le vendredi 11 avril 2025 à 13:43 -0500, Chenyuan Yang a écrit :

Quoted 5 lines

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

Quoted 6 lines

Its hard to believe that its a coincidence that someone else sent a patch for this. A colleague, the same person ?

I do prefer this version though, the commits message is better, the code is nicer. If its you, adding a [PATCH v2], or just adding a comment that its a better version would be nice.

To Ming Qian, this is the type of patch that I expect an Acked-by from the maintainer.

Meanwhile, to Chenyuan, you should followup when requested. Marking this patch as change requested, looking forward a v2.

Nicolas

Quoted 59 lines
Quoted 21 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")

---
 drivers/media/platform/amphion/vpu_core.c | 11 +++++++++++

 1 file changed, 11 insertions(+)
drivers/media/platform/amphion/vpu_core.c +0 -0
index 8df85c14ab3f..26568987586d 100644
@@ -586,7 +586,18 @@ static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np)
  	}


  core->fw.virt = memremap(core->fw.phys, core->fw.length, MEMREMAP_WC);

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 591

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+	if (!core->fw.virt) {
+		dev_err(core->dev, "failed to remap fw region\n");
+		of_node_put(node);

nit: node and res are no longer used passed line 579. Meaning you could unref the node earlier, and remove the repeated of_node_put(node) call in the error conditions.

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +2 -0
+		return -ENOMEM;
+	}

  core->rpc.virt = memremap(core->rpc.phys, core->rpc.length, MEMREMAP_WC);

I really enjoy blank lines after closing scope, even though its not a strict coding standard.

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 597

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+	if (!core->rpc.virt) {
+		dev_err(core->dev, "failed to remap rpc region\n");
+		memunmap(core->fw.virt);

Its interesting that you thought of cleaning that up here, since its not being cleanup in the error case of "if (ret != VPU_CORE_MEMORY_UNCACHED)".  And its also not being cleanup if the probe fails later for other reasons. Perhaps your chance to add more fixes to this driver.

Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+		of_node_put(node);
+		return -ENOMEM;
+	}

  memset(core->rpc.virt, 0, core->rpc.length);

Same, I like blank lines (but you are free to ignore me if Ming does not care).

Quoted 2 lines


  ret = vpu_iface_check_memory_region(core, core->rpc.phys, core->rpc.length);

regards,
Nicolas

maintainer Reviewed-by
#3 lore raw
Quoted 34 lines

-----Original Message-----
From: Nicolas Dufresne <[email protected]>
Sent: Wednesday, April 23, 2025 5:14 AM
To: Chenyuan Yang <[email protected]>; Ming Qian <[email protected]>; Eagle Zhou <[email protected]>; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]
Subject: [EXT] Re: [PATCH] media: amphion: fix potential NULL deref in vpu_core_parse_dt()

Caution: This is an external email. Please take care when clicking links or opening attachments. When in doubt, report the message using the 'Report this email' button

Quoted 9 lines · Nicolas Dufresne

Hi,

Quoted 5 lines · Chenyuan Yang

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

Quoted 6 lines

Its hard to believe that its a coincidence that someone else sent a patch for this. A colleague, the same person ?

I do prefer this version though, the commits message is better, the code is nicer. If its you, adding a [PATCH v2], or just adding a comment that its a better version would be nice.

Quoted 17 lines

To Ming Qian, this is the type of patch that I expect an Acked-by from the maintainer.

Meanwhile, to Chenyuan, you should followup when requested. Marking this patch as change requested, looking forward a v2.

Nicolas

Quoted 9 lines
Quoted 9 lines

This is similar to the commit 966d47e1f27c
("efi: fix potential NULL deref in efi_mem_reserve_persistent").

This is found by our static analysis tool KNighter.

Fixes 9f599f351e86 ("media: amphion: add vpu core driver")
Quoted 55 lines
Quoted 10 lines

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core

Quoted 10 lines
---
 drivers/media/platform/amphion/vpu_core.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/media/platform/amphion/vpu_core.c +0 -0
index 8df85c14ab3f..26568987586d 100644
@@ -586,7 +586,18 @@ static int vpu_core_parse_dt(struct vpu_core

*core, struct device_node *np)

Quoted 44 lines

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 591

Quoted 7 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
    }
 
    core->fw.virt = memremap(core->fw.phys, core->fw.length,
 MEMREMAP_WC);
+   if (!core->fw.virt) {
+           dev_err(core->dev, "failed to remap fw region\n");
+           of_node_put(node);

nit: node and res are no longer used passed line 579. Meaning you could unref the node earlier, and remove the repeated of_node_put(node) call in the error conditions.

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np)

Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c +2 -0
+           return -ENOMEM;
+   }
    core->rpc.virt = memremap(core->rpc.phys, core->rpc.length,
 MEMREMAP_WC);

I really enjoy blank lines after closing scope, even though its not a strict coding standard.

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 597

Quoted 3 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+   if (!core->rpc.virt) {
+           dev_err(core->dev, "failed to remap rpc region\n");
+           memunmap(core->fw.virt);

Its interesting that you thought of cleaning that up here, since its not being cleanup in the error case of "if (ret != VPU_CORE_MEMORY_UNCACHED)". And its also not being cleanup if the probe fails later for other reasons. Perhaps your chance to add more fixes to this driver.

drivers/media/platform/amphion/vpu_core.c static int vpu_core_parse_dt(struct vpu_core *core, struct device_node *np) · line 601

Quoted 4 lines
drivers/media/platform/amphion/vpu_core.c +3 -0
+           of_node_put(node);
+           return -ENOMEM;
+   }
    memset(core->rpc.virt, 0, core->rpc.length);

Same, I like blank lines (but you are free to ignore me if Ming does not care).

Quoted 3 lines
    ret = vpu_iface_check_memory_region(core, core->rpc.phys,

core->rpc.length);

regards,
Nicolas

#4 lore raw
In reply to Chenyuan Yang
Quoted 5 lines

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

* Do you propose to complete the error handling?

* Can any other summary phrase variant become more desirable accordingly?

* Please avoid duplicate source code (also for corresponding exception handling).

See also:
[PATCH] media: amphion: fix potential NULL deref in vpu_core_parse_dt
https://lore.kernel.org/all/[email protected]/

Regards,
Markus

#5 lore raw

Hi Markus,

Le samedi 12 avril 2025 à 17:15 +0200, Markus Elfring a écrit :

Quoted 9 lines
Quoted 5 lines

The result of memremap() may be NULL on failure, leading to a null dereference in the subsequent memset(). Add explicit checks after each memremap() call: if the firmware region fails to map, return immediately; if the RPC region fails, unmap the firmware window before returning.

* Do you propose to complete the error handling?

* Can any other summary phrase variant become more desirable accordingly?

That could equally be a machine replying. I'm happy to get help with reviews, but his isn't useful. It simply confuses the least experienced submitters.

Quoted 2 lines

* Please avoid duplicate source code (also for corresponding exception handling).

This type of comment only make sense inline, there is no true duplication either.

Quoted 5 lines

See also:
[PATCH] media: amphion: fix potential NULL deref in vpu_core_parse_dt
https://lore.kernel.org/all/[email protected]/

I already stated I prefer this version.

Nicolas