Skip to contentSkip to conversation

media: nxp: isi: Check whether pad is non-NULL before access

PATCH In review Marek Vasut 4 participants 11 messages Dec 1, 2023 – Jan 13, 2024

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

PATCH 1/1 media: nxp: isi: Check whether pad is non-NULL before access

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
 	}
 
 	pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-	sd = media_entity_to_v4l2_subdev(pad->entity);
+	if (!pad) {
+		dev_dbg(xbar->isi->dev,
+			"no pad connected to crossbar input %u\n",
+			sink_pad);
+		return ERR_PTR(-EPIPE);
+	}
 
+	sd = media_entity_to_v4l2_subdev(pad->entity);
 	if (!sd) {
 		dev_dbg(xbar->isi->dev,
 			"no entity connected to crossbar input %u\n",
Signature · 1 line
2.42.0
reviewer Reviewed-by on v1 1/1
#1 lore raw

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar, · line 164

Quoted 4 lines · Marek Vasut
        pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
-       sd = media_entity_to_v4l2_subdev(pad->entity);
+       if (!pad) {
+               dev_dbg(xbar->isi->dev,

Maybe dev_err() here instead?

Anyway:

#2 lore raw
Quoted 8 lines · Fabio Estevam

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar, · line 164

Quoted 4 lines · Marek Vasut
         pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
-       sd = media_entity_to_v4l2_subdev(pad->entity);
+       if (!pad) {
+               dev_dbg(xbar->isi->dev,

Maybe dev_err() here instead?

That dev_dbg() is aligned with the prints in the rest of the function and it's not like kernel should do dev_err() into kernel log every time userspace does something wrong.

Quoted 3 lines

Anyway:

Thanks !

maintainer
#3 lore raw
Quoted 8 lines · Marek Vasut
Quoted 7 lines · Fabio Estevam
Quoted 4 lines · Marek Vasut
         pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
-       sd = media_entity_to_v4l2_subdev(pad->entity);
+       if (!pad) {
+               dev_dbg(xbar->isi->dev,
Quoted 5 lines
Quoted 1 line

Maybe dev_err() here instead?

That dev_dbg() is aligned with the prints in the rest of the function and it's not like kernel should do dev_err() into kernel log every time userspace does something wrong.

We usually use dev_dbg() for errors that can be easily triggered from userspace, to avoid giving unpriviledged processes an easy way to flood the kernel log.

Quoted 5 lines
Quoted 3 lines

Anyway:

Thanks !

Signature · 3 lines
Regards,

Laurent Pinchart
Reviewed-by
#4 lore raw

Quoting Laurent Pinchart (2023-12-03 17:00:57)

Quoted 11 lines · Marek Vasut
Quoted 10 lines · Fabio Estevam
Quoted 8 lines · Marek Vasut

drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar, · line 164

Quoted 5 lines
         pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +2 -1
-       sd = media_entity_to_v4l2_subdev(pad->entity);
+       if (!pad) {
+               dev_dbg(xbar->isi->dev,

Maybe dev_err() here instead?

Quoted 7 lines
Quoted 3 lines

That dev_dbg() is aligned with the prints in the rest of the function and it's not like kernel should do dev_err() into kernel log every time userspace does something wrong.

We usually use dev_dbg() for errors that can be easily triggered from userspace, to avoid giving unpriviledged processes an easy way to flood the kernel log.

When this call returns it will also report a dev_err "Failed to enable pipe":

[   56.806321] mxc-isi 32e00000.isi: no pad connecxted to crossbar input 0
[   56.813655] mxc-isi 32e00000.isi: Failed to enable pipe 0

Personally - I'd make this print dev_err so the /reason/ we failed to enable the pipe is also stated.

But I can confirm this is a bug and fairly easily trigged by attempting to capture from the ISI through the libcamera simple pipeline handler, which (I believe) does not do any route handling at present.

Which ever log level:

And this needs to go to stable, as this caused a null-pointer-deref for me on a v6.6 tree.

--
Kieran

Quoted 16 lines
Quoted 6 lines
Quoted 4 lines

Anyway:

Thanks !

Signature · 8 lines
Regards,

Laurent Pinchart

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#5 lore raw
In reply to Marek Vasut

Hi Marek,

Thank you for the patch.

Quoted 3 lines · Marek Vasut

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Could you share more information about how to misconfigure the routing ?

Quoted 36 lines
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
 	}
 
 	pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-	sd = media_entity_to_v4l2_subdev(pad->entity);
+	if (!pad) {
+		dev_dbg(xbar->isi->dev,
+			"no pad connected to crossbar input %u\n",
+			sink_pad);
+		return ERR_PTR(-EPIPE);
+	}
 
+	sd = media_entity_to_v4l2_subdev(pad->entity);
 	if (!sd) {
 		dev_dbg(xbar->isi->dev,
 			"no entity connected to crossbar input %u\n",
Signature · 3 lines
Regards,

Laurent Pinchart
#6 lore raw

Quoting Laurent Pinchart (2023-12-03 16:59:59)

Quoted 10 lines

Hi Marek,

Thank you for the patch.

Quoted 3 lines · Marek Vasut

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Could you share more information about how to misconfigure the routing ?

You simply do 'nothing'.

--
Kieran

Quoted 42 lines
Quoted 37 lines
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
      }
 
      pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-     sd = media_entity_to_v4l2_subdev(pad->entity);
+     if (!pad) {
+             dev_dbg(xbar->isi->dev,
+                     "no pad connected to crossbar input %u\n",
+                     sink_pad);
+             return ERR_PTR(-EPIPE);
+     }
 
+     sd = media_entity_to_v4l2_subdev(pad->entity);
      if (!sd) {
              dev_dbg(xbar->isi->dev,
                      "no entity connected to crossbar input %u\n",
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer
#7 lore raw
Quoted 5 lines · Kieran Bingham

Quoting Laurent Pinchart (2023-12-03 16:59:59)

Quoted 4 lines

Hi Marek,

Thank you for the patch.

Quoted 8 lines
Quoted 6 lines · Marek Vasut
Quoted 3 lines

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Could you share more information about how to misconfigure the routing ?

You simply do 'nothing'.

The default configuration should be working one. I think that should then be fixed too (in a separate patch).

Quoted 41 lines
Quoted 41 lines
Quoted 36 lines
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
      }
 
      pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-     sd = media_entity_to_v4l2_subdev(pad->entity);
+     if (!pad) {
+             dev_dbg(xbar->isi->dev,
+                     "no pad connected to crossbar input %u\n",
+                     sink_pad);
+             return ERR_PTR(-EPIPE);
+     }
 
+     sd = media_entity_to_v4l2_subdev(pad->entity);
      if (!sd) {
              dev_dbg(xbar->isi->dev,
                      "no entity connected to crossbar input %u\n",
Signature · 3 lines
Regards,

Laurent Pinchart
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer Reviewed-by
#8 lore raw
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Kieran Bingham

Quoting Laurent Pinchart (2023-12-03 16:59:59)

Quoted 9 lines

Hi Marek,

Thank you for the patch.

Quoted 3 lines · Marek Vasut

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Could you share more information about how to misconfigure the routing ?

You simply do 'nothing'.

The default configuration should be working one. I think that should then be fixed too (in a separate patch).

I managed to reproduce the issue (I had to heavily hack libcamera, by default it would reject incorrect configurations before triggering the kernel bug). The default configuration of the crossbar switch is fine, this patch is the right fix.

I'd like to expand the commit message with a clearer description of the erronous configuration. Marek, are you fine with the following commit message ?

--------
When translating source to sink streams in the crossbar subdev, the driver tries to locate the remote subdev connected to the sink pad. The remote pad may be NULL, if userspace tries to enable a stream that ends at an unconnected crossbar sink. When that occurs, the driver dereferences the NULL pad, leading to a crash.

Prevent the crash by checking if the pad is NULL before using it, and return an error if it is.
--------

If so I'll update it locally, no need for a new version.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
      }
 
      pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-     sd = media_entity_to_v4l2_subdev(pad->entity);
+     if (!pad) {
+             dev_dbg(xbar->isi->dev,
+                     "no pad connected to crossbar input %u\n",
+                     sink_pad);
+             return ERR_PTR(-EPIPE);
+     }
 
+     sd = media_entity_to_v4l2_subdev(pad->entity);
      if (!sd) {
              dev_dbg(xbar->isi->dev,
                      "no entity connected to crossbar input %u\n",
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer
#9 lore raw
Quoted 16 lines · Laurent Pinchart
Quoted 15 lines · Laurent Pinchart
Quoted 13 lines · Kieran Bingham

Quoting Laurent Pinchart (2023-12-03 16:59:59)

Quoted 10 lines

Hi Marek,

Thank you for the patch.

Quoted 3 lines · Marek Vasut

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Could you share more information about how to misconfigure the routing ?

You simply do 'nothing'.

Quoted 7 lines
Quoted 2 lines

The default configuration should be working one. I think that should then be fixed too (in a separate patch).

I managed to reproduce the issue (I had to heavily hack libcamera, by default it would reject incorrect configurations before triggering the kernel bug). The default configuration of the crossbar switch is fine, this patch is the right fix.

Actually, I think this should be fixed by catching the pipeline misconfiguration at validation time, not when enabling streams. It would make this patch redundant, but I think we can still merge it for additional safety, especially as it may take more time to implement the validation time check.

Quoted 55 lines

I'd like to expand the commit message with a clearer description of the erronous configuration. Marek, are you fine with the following commit message ?

--------
When translating source to sink streams in the crossbar subdev, the driver tries to locate the remote subdev connected to the sink pad. The remote pad may be NULL, if userspace tries to enable a stream that ends at an unconnected crossbar sink. When that occurs, the driver dereferences the NULL pad, leading to a crash.

Prevent the crash by checking if the pad is NULL before using it, and return an error if it is.
--------

If so I'll update it locally, no need for a new version.

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
Fixes cf21f328fcaf ("media: nxp: Add i.MX8 ISI driver")

---
Cc: Fabio Estevam <[email protected]>
Cc: Laurent Pinchart <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: NXP Linux Team <[email protected]>
Cc: Pengutronix Kernel Team <[email protected]>
Cc: Sascha Hauer <[email protected]>
Cc: Shawn Guo <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-crossbar.c +7 -1
index 792f031e032ae..44354931cf8a1 100644
@@ -160,8 +160,14 @@ mxc_isi_crossbar_xlate_streams(struct mxc_isi_crossbar *xbar,
      }
 
      pad = media_pad_remote_pad_first(&xbar->pads[sink_pad]);
-     sd = media_entity_to_v4l2_subdev(pad->entity);
+     if (!pad) {
+             dev_dbg(xbar->isi->dev,
+                     "no pad connected to crossbar input %u\n",
+                     sink_pad);
+             return ERR_PTR(-EPIPE);
+     }
 
+     sd = media_entity_to_v4l2_subdev(pad->entity);
      if (!sd) {
              dev_dbg(xbar->isi->dev,
                      "no entity connected to crossbar input %u\n",
Signature · 3 lines
Regards,

Laurent Pinchart
#10 lore raw
In reply to Laurent Pinchart
Quoted 35 lines · Laurent Pinchart
Quoted 11 lines · Laurent Pinchart
Quoted 10 lines · Kieran Bingham

Quoting Laurent Pinchart (2023-12-03 16:59:59)

Quoted 9 lines

Hi Marek,

Thank you for the patch.

Quoted 3 lines · Marek Vasut

The pad can be NULL if media controller routing is not set up correctly. Check whether the pad is NULL before using it, otherwise it is possible to achieve NULL pointer dereference.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Could you share more information about how to misconfigure the routing ?

You simply do 'nothing'.

The default configuration should be working one. I think that should then be fixed too (in a separate patch).

I managed to reproduce the issue (I had to heavily hack libcamera, by default it would reject incorrect configurations before triggering the kernel bug). The default configuration of the crossbar switch is fine, this patch is the right fix.

I'd like to expand the commit message with a clearer description of the erronous configuration. Marek, are you fine with the following commit message ?

--------
When translating source to sink streams in the crossbar subdev, the driver tries to locate the remote subdev connected to the sink pad. The remote pad may be NULL, if userspace tries to enable a stream that ends at an unconnected crossbar sink. When that occurs, the driver dereferences the NULL pad, leading to a crash.

Quoted 7 lines

Prevent the crash by checking if the pad is NULL before using it, and return an error if it is.
--------

If so I'll update it locally, no need for a new version.

Sure, go for it.