Skip to contentSkip to conversation

ptp: ocp: Fix NULL dereference in Adva board SMA sysfs operations

PATCH Applied Sagi Maimon 3 participants 3 messages Apr 29, 2025 – May 1, 2025

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

PATCH v2 1/1 ptp: ocp: Fix NULL dereference in Adva board SMA sysfs operations

From: Sagi Maimon <[email protected]>

On Adva boards, SMA sysfs store/get operations can call __handle_signal_outputs() or __handle_signal_inputs() while the irig and dcf pointers are uninitialized, leading to a NULL pointer dereference in __handle_signal() and causing a kernel crash. Adva boards don't use irig or dcf functionality, so add Adva-specific callbacks ptp_ocp_sma_adva_set_outputs() and ptp_ocp_sma_adva_set_inputs() that avoid invoking irig or dcf input/output routines.

Fixes ef61f5528fca ("ptp: ocp: add Adva timecard support")

---
Addressed comments from Vadim Fedorenko:
- https://www.spinics.net/lists/kernel/msg5659845.html
Changes since v1:
- Remove unused irig and dcf code.
---

---
 drivers/ptp/ptp_ocp.c | 52 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 50 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +50 -2
index faf6e027f89a..2ccdca4f6960 100644
@@ -2578,12 +2578,60 @@ static const struct ocp_sma_op ocp_fb_sma_op = {
 	.set_output	= ptp_ocp_sma_fb_set_output,
 };
 
+static int
+ptp_ocp_sma_adva_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
+{
+	u32 reg, mask, shift;
+	unsigned long flags;
+	u32 __iomem *gpio;
+
+	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
+	shift = sma_nr & 1 ? 0 : 16;
+
+	mask = 0xffff << (16 - shift);
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	reg = ioread32(gpio);
+	reg = (reg & mask) | (val << shift);
+
+	iowrite32(reg, gpio);
+
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}
+
+static int
+ptp_ocp_sma_adva_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
+{
+	u32 reg, mask, shift;
+	unsigned long flags;
+	u32 __iomem *gpio;
+
+	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
+	shift = sma_nr & 1 ? 0 : 16;
+
+	mask = 0xffff << (16 - shift);
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	reg = ioread32(gpio);
+	reg = (reg & mask) | (val << shift);
+
+	iowrite32(reg, gpio);
+
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}
+
 static const struct ocp_sma_op ocp_adva_sma_op = {
 	.tbl		= { ptp_ocp_adva_sma_in, ptp_ocp_adva_sma_out },
 	.init		= ptp_ocp_sma_fb_init,
 	.get		= ptp_ocp_sma_fb_get,
-	.set_inputs	= ptp_ocp_sma_fb_set_inputs,
-	.set_output	= ptp_ocp_sma_fb_set_output,
+	.set_inputs	= ptp_ocp_sma_adva_set_inputs,
+	.set_output	= ptp_ocp_sma_adva_set_output,
 };
 
 static int
Signature · 1 line
2.47.0
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 79 lines · Sagi Maimon

From: Sagi Maimon <[email protected]>

On Adva boards, SMA sysfs store/get operations can call __handle_signal_outputs() or __handle_signal_inputs() while the irig and dcf pointers are uninitialized, leading to a NULL pointer dereference in __handle_signal() and causing a kernel crash. Adva boards don't use irig or dcf functionality, so add Adva-specific callbacks ptp_ocp_sma_adva_set_outputs() and ptp_ocp_sma_adva_set_inputs() that avoid invoking irig or dcf input/output routines.

Fixes ef61f5528fca ("ptp: ocp: add Adva timecard support")

---
Addressed comments from Vadim Fedorenko:
- https://www.spinics.net/lists/kernel/msg5659845.html
Changes since v1:
- Remove unused irig and dcf code.
---

---
  drivers/ptp/ptp_ocp.c | 52 +++++++++++++++++++++++++++++++++++++++++--
  1 file changed, 50 insertions(+), 2 deletions(-)
drivers/ptp/ptp_ocp.c +48 -0
index faf6e027f89a..2ccdca4f6960 100644
@@ -2578,12 +2578,60 @@ static const struct ocp_sma_op ocp_fb_sma_op = {
  	.set_output	= ptp_ocp_sma_fb_set_output,
  };
 
+static int
+ptp_ocp_sma_adva_set_output(struct ptp_ocp *bp, int sma_nr, u32 val)
+{
+	u32 reg, mask, shift;
+	unsigned long flags;
+	u32 __iomem *gpio;
+
+	gpio = sma_nr > 2 ? &bp->sma_map1->gpio2 : &bp->sma_map2->gpio2;
+	shift = sma_nr & 1 ? 0 : 16;
+
+	mask = 0xffff << (16 - shift);
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	reg = ioread32(gpio);
+	reg = (reg & mask) | (val << shift);
+
+	iowrite32(reg, gpio);
+
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}
+
+static int
+ptp_ocp_sma_adva_set_inputs(struct ptp_ocp *bp, int sma_nr, u32 val)
+{
+	u32 reg, mask, shift;
+	unsigned long flags;
+	u32 __iomem *gpio;
+
+	gpio = sma_nr > 2 ? &bp->sma_map2->gpio1 : &bp->sma_map1->gpio1;
+	shift = sma_nr & 1 ? 0 : 16;
+
+	mask = 0xffff << (16 - shift);
+
+	spin_lock_irqsave(&bp->lock, flags);
+
+	reg = ioread32(gpio);
+	reg = (reg & mask) | (val << shift);
+
+	iowrite32(reg, gpio);
+
+	spin_unlock_irqrestore(&bp->lock, flags);
+
+	return 0;
+}
+
  static const struct ocp_sma_op ocp_adva_sma_op = {

drivers/ptp/ptp_ocp.c static const struct ocp_sma_op ocp_fb_sma_op = { · line 2637

Quoted 10 lines
drivers/ptp/ptp_ocp.c +2 -2
  	.tbl		= { ptp_ocp_adva_sma_in, ptp_ocp_adva_sma_out },
  	.init		= ptp_ocp_sma_fb_init,
  	.get		= ptp_ocp_sma_fb_get,
-	.set_inputs	= ptp_ocp_sma_fb_set_inputs,
-	.set_output	= ptp_ocp_sma_fb_set_output,
+	.set_inputs	= ptp_ocp_sma_adva_set_inputs,
+	.set_output	= ptp_ocp_sma_adva_set_output,
  };
 
  static int

LGTM,

#2 lore raw
In reply to Sagi Maimon
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Tue, 29 Apr 2025 10:33:20 +0300 you wrote:

From: Sagi Maimon <[email protected]>

On Adva boards, SMA sysfs store/get operations can call __handle_signal_outputs() or __handle_signal_inputs() while the irig and dcf pointers are uninitialized, leading to a NULL pointer dereference in __handle_signal() and causing a kernel crash. Adva boards don't use irig or dcf functionality, so add Adva-specific callbacks ptp_ocp_sma_adva_set_outputs() and ptp_ocp_sma_adva_set_inputs() that avoid invoking irig or dcf input/output routines.

[...]

Here is the summary with links:
- [v2] ptp: ocp: Fix NULL dereference in Adva board SMA sysfs operations

    https://git.kernel.org/netdev/net/c/e98386d79a23

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html