Skip to contentSkip to conversation

spi: xilinx_spi: Fix potential null pointer access

PATCH In review Jiajie Chen 2 participants 3 messages Feb 21, 2023 – Feb 27, 2023

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

PATCH 1/1 spi: xilinx_spi: Fix potential null pointer access

It was incorrectly using an old priv->regs pointer, and may lead to null pointer access.

---

 drivers/spi/xilinx_spi.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/spi/xilinx_spi.c +1 -3
index 4e9115dafe..e759b66000 100644
@@ -112,9 +112,7 @@ struct xilinx_spi_priv {
 static int xilinx_spi_probe(struct udevice *bus)
 {
 	struct xilinx_spi_priv *priv = dev_get_priv(bus);
-	struct xilinx_spi_regs *regs = priv->regs;
-
-	priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus);
+	struct xilinx_spi_regs *regs = priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus);
 
 	priv->fifo_depth = dev_read_u32_default(bus, "fifo-size", 0);
 
Signature · 1 line
2.30.2
maintainer
#1 lore raw
Quoted 2 lines · Jiajie Chen

It was incorrectly using an old priv->regs pointer, and may lead to null pointer access.

I would describe it a little bit differently to describe what it happening.

priv structure is initiated by DM core to zeros that's why regs property is pointing to 0 address + spi offsets. That's why likely spi resets never happened.

Quoted 12 lines

---

  drivers/spi/xilinx_spi.c | 4 +---
  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/spi/xilinx_spi.c +0 -0
index 4e9115dafe..e759b66000 100644

drivers/spi/xilinx_spi.c struct xilinx_spi_priv { · line 115

Quoted 7 lines
drivers/spi/xilinx_spi.c +1 -3
@@ -112,9 +112,7 @@ struct xilinx_spi_priv {
  static int xilinx_spi_probe(struct udevice *bus)
  {
  	struct xilinx_spi_priv *priv = dev_get_priv(bus);
-	struct xilinx_spi_regs *regs = priv->regs;
-
-	priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus);
+	struct xilinx_spi_regs *regs = priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus);

The patch is good but I pretty much don't like this long line. Can you please do it on 2 lines instead?

It would be easier to read.

Thanks,
Michal

#2 lore raw

Comments below.

Quoted 10 lines · Michal Simek
Quoted 2 lines · Jiajie Chen

It was incorrectly using an old priv->regs pointer, and may lead to null pointer access.

I would describe it a little bit differently to describe what it happening.

priv structure is initiated by DM core to zeros that's why regs property is pointing to 0 address + spi offsets. That's why likely spi resets never happened.

Thanks, I will post a v2 patch.

Quoted 17 lines
Quoted 17 lines

---

  drivers/spi/xilinx_spi.c | 4 +---

  1 file changed, 1 insertion(+), 3 deletions(-)
drivers/spi/xilinx_spi.c +0 -1
index 4e9115dafe..e759b66000 100644
@@ -112,9 +112,7 @@ struct xilinx_spi_priv {
   static int xilinx_spi_probe(struct udevice *bus)
   {
       struct xilinx_spi_priv *priv = dev_get_priv(bus);
-    struct xilinx_spi_regs *regs = priv->regs;
Quoted 8 lines

drivers/spi/xilinx_spi.c struct xilinx_spi_priv {

Quoted 4 lines
drivers/spi/xilinx_spi.c +1 -2
-
-    priv->regs = (struct xilinx_spi_regs *)dev_read_addr(bus);
+    struct xilinx_spi_regs *regs = priv->regs = (struct 
 xilinx_spi_regs *)dev_read_addr(bus);

The patch is good but I pretty much don't like this long line. Can you please do it on 2 lines instead?

Sure.

Quoted 5 lines

It would be easier to read.

Thanks,
Michal