Skip to contentSkip to conversation

ionic: fix port_info lifetime bugs

PATCH Applied Eric Joyner 5 participants 10 messages Oct 15 – Oct 22

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

v2 cover ionic: fix port_info lifetime bugs

Two fixes for the lifetime of the ionic port_info DMA buffer.

Patch 1 fixes a use-after-free. ionic_port_init() allocates port_info only when the pointer is NULL, but its error path frees the buffer unconditionally. On the firmware recovery paths, ionic_lif_handle_fw_up() and ionic_reset_done(), the buffer belongs to an earlier successful init, so a failed PORT_INIT frees it under any ethtool op that is already reading it. The error path now frees only what the same call allocated.

Patch 2 (v1 patch 3) fixes a leak of the same buffer when probe fails after the port has been set up.

---
Changes in v2:
- Nikhil's work replaces v1 patches 1 and 2 with a fix for the root
cause. With the ionic_port_init() error path no longer freeing a
buffer it did not allocate, port_info is never freed or set to NULL
while the netdev is registered, which also covers
ionic_sw_stats_get_values() and ionic_reset_link_down_count(). As the
Sashiko review pointed out, the v1 NULL checks could not stop a reader
that had already loaded the pointer, and ionic_reset_prepare() already
detaches the netdev via ionic_stop_queues_reconfig() before any
port_info or BAR teardown, so the extra detach did not close the window.
- If PORT_INIT fails during recovery, the old buffer is now kept. The
netdev stays detached with IONIC_LIF_F_FW_RESET set until a later
PORT_INIT succeeds, so its stale contents are not reported.
- On freeing without PORT_RESET after a timed-out PORT_INIT: that free
now runs only when the same call allocated the buffer, i.e. at
probe. LIF_INIT is never sent after a failed PORT_INIT on any path,
and Nikhil checked that the firmware only writes port_info once the
LIF is initialized.
- Patch 2: correct the Fixes tag to 0de38d9f1dba, which removed the ionic_port_reset() call from the probe unwind, and reword the
changelog to match. No code change.
- Not addressed here: an ethtool op can still run while the PCI reset
path unmaps the BARs in ionic_clear_pci() and tears down device state
in ionic_dev_teardown(). The ignored PORT_STATE result in
ionic_port_init() is also unchanged.
- Rebase onto current net.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Eric Joyner (1):
ionic: free port_info when probe fails after the port is set up

Nikhil P. Rao (1):
ionic: fix port_info use-after-free on firmware recovery

 drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c |  1 +
 drivers/net/ethernet/pensando/ionic/ionic_main.c    | 13 +++++++++----
 2 files changed, 10 insertions(+), 4 deletions(-)

---
base-commit: 045958acaa75205b31578cb22c4345e28dd2e4bf
change-id: 20260814-ionic-port-info-lifetime-3163779cb298

Best regards,

Signature · 1 line
Eric Joyner <[email protected]>
#1 lore raw

PATCH v2 1/2 ionic: fix port_info use-after-free on firmware recovery

From: "Nikhil P. Rao" <[email protected]>

ionic_port_init() allocates port_info when the pointer is NULL, but its error path frees it without checking whether this call allocated it. Before commit ddc5911b9bd2 ("ionic: call ionic_port_init after fw-upgrade") the function returned early when the buffer already existed, so the two were symmetric.

Since then a failed PORT_INIT on either recovery path - fw_up or reset_done - frees a buffer an earlier init owns, while the netdev is registered and the ethtool ops are reading it.

Free only what this call allocated.

Fixes ddc5911b9bd2 ("ionic: call ionic_port_init after fw-upgrade")
---
 drivers/net/ethernet/pensando/ionic/ionic_main.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_main.c +9 -4
index 10501be9ef95..38b47b959a11 100644
@@ -722,6 +722,7 @@ int ionic_port_init(struct ionic *ionic)
 {
 	struct ionic_identity *ident = &ionic->ident;
 	struct ionic_dev *idev = &ionic->idev;
+	bool allocated = false;
 	size_t sz;
 	int err;
 
@@ -733,6 +734,8 @@ int ionic_port_init(struct ionic *ionic)
 						     GFP_KERNEL);
 		if (!idev->port_info)
 			return -ENOMEM;
+
+		allocated = true;
 	}
 
 	/* If the driver knows about more "extra stats" than the firmware,
@@ -757,10 +760,12 @@ int ionic_port_init(struct ionic *ionic)
 	mutex_unlock(&ionic->dev_cmd_lock);
 	if (err) {
 		dev_err(ionic->dev, "Failed to init port\n");
-		dma_free_coherent(ionic->dev, idev->port_info_sz,
-				  idev->port_info, idev->port_info_pa);
-		idev->port_info = NULL;
-		idev->port_info_pa = 0;
+		if (allocated) {
+			dma_free_coherent(ionic->dev, idev->port_info_sz,
+					  idev->port_info, idev->port_info_pa);
+			idev->port_info = NULL;
+			idev->port_info_pa = 0;
+		}
 	}
 
 	return err;
Signature · 1 line
2.53.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH net v2 1/2] ionic: fix port_info use-after-free on firmware recovery

Quoted 50 lines · Eric Joyner

From: "Nikhil P. Rao" <[email protected]>

ionic_port_init() allocates port_info when the pointer is NULL, but its error path frees it without checking whether this call allocated it. Before commit ddc5911b9bd2 ("ionic: call ionic_port_init after fw-upgrade") the function returned early when the buffer already existed, so the two were symmetric.

Since then a failed PORT_INIT on either recovery path - fw_up or reset_done - frees a buffer an earlier init owns, while the netdev is registered and the ethtool ops are reading it.

Free only what this call allocated.

Fixes ddc5911b9bd2 ("ionic: call ionic_port_init after fw-upgrade")
---
  drivers/net/ethernet/pensando/ionic/ionic_main.c | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)
drivers/net/ethernet/pensando/ionic/ionic_main.c +3 -3
index 10501be9ef95..38b47b959a11 100644
@@ -722,6 +722,7 @@ int ionic_port_init(struct ionic *ionic)
  {
  	struct ionic_identity *ident = &ionic->ident;
  	struct ionic_dev *idev = &ionic->idev;
+	bool allocated = false;
  	size_t sz;
  	int err;
 
@@ -733,6 +734,8 @@ int ionic_port_init(struct ionic *ionic)
  						     GFP_KERNEL);
  		if (!idev->port_info)
  			return -ENOMEM;
+
+		allocated = true;
  	}
 
  	/* If the driver knows about more "extra stats" than the firmware,
@@ -757,10 +760,12 @@ int ionic_port_init(struct ionic *ionic)
  	mutex_unlock(&ionic->dev_cmd_lock);
  	if (err) {
  		dev_err(ionic->dev, "Failed to init port\n");
-		dma_free_coherent(ionic->dev, idev->port_info_sz,
-				  idev->port_info, idev->port_info_pa);
-		idev->port_info = NULL;

drivers/net/ethernet/pensando/ionic/ionic_main.c int ionic_port_init(struct ionic *ionic) · line 771

Quoted 10 lines
drivers/net/ethernet/pensando/ionic/ionic_main.c +6 -1
-		idev->port_info_pa = 0;
+		if (allocated) {
+			dma_free_coherent(ionic->dev, idev->port_info_sz,
+					  idev->port_info, idev->port_info_pa);
+			idev->port_info = NULL;
+			idev->port_info_pa = 0;
+		}
  	}
 
  	return err;
#3 lore raw

PATCH v2 2/2 ionic: free port_info when probe fails after the port is set up

In reply to Eric Joyner

ionic_probe() allocates the port_info DMA buffer via ionic_setup_one() -> ionic_port_init(). If probe then fails anywhere after that, it unwinds through err_out_pci, which never calls ionic_port_reset(); ionic_dev_teardown() and ionic_clear_pci() do not touch port_info, and so the buffer is leaked. The call was lost when the setup code was moved into ionic_setup_one().

Since ionic_remove() already frees the port_info DMA buffer with ionic_port_reset(), call that on the probe error path too. Every path to err_out_pci runs after ionic_setup_one() succeeded, so the BARs are still mapped and the firmware gets a PORT_RESET before the buffer is freed, the same order as in ionic_remove().

Fixes 0de38d9f1dba ("ionic: extract common bits from ionic_probe")
Assisted-by Claude:claude-opus-5
---
 drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c | 1 +
 1 file changed, 1 insertion(+)
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +1 -0
index 05f19489ec5c..68b3bceedd99 100644
@@ -397,6 +397,7 @@ static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 err_out_free_irqs:
 	ionic_bus_free_irq_vectors(ionic);
 err_out_pci:
+	ionic_port_reset(ionic);
 	ionic_dev_teardown(ionic);
 	ionic_clear_pci(ionic);
 	ionic_debugfs_del_dev(ionic);
Signature · 1 line
2.53.0
Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH net v2 2/2] ionic: free port_info when probe fails after the port is set up

Quoted 26 lines · Eric Joyner

ionic_probe() allocates the port_info DMA buffer via ionic_setup_one() -> ionic_port_init(). If probe then fails anywhere after that, it unwinds through err_out_pci, which never calls ionic_port_reset(); ionic_dev_teardown() and ionic_clear_pci() do not touch port_info, and so the buffer is leaked. The call was lost when the setup code was moved into ionic_setup_one().

Since ionic_remove() already frees the port_info DMA buffer with ionic_port_reset(), call that on the probe error path too. Every path to err_out_pci runs after ionic_setup_one() succeeded, so the BARs are still mapped and the firmware gets a PORT_RESET before the buffer is freed, the same order as in ionic_remove().

Fixes 0de38d9f1dba ("ionic: extract common bits from ionic_probe")
Assisted-by Claude:claude-opus-5
---
  drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c | 1 +
  1 file changed, 1 insertion(+)
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +0 -0
index 05f19489ec5c..68b3bceedd99 100644

drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 403

Quoted 7 lines
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +1 -0
@@ -397,6 +397,7 @@ static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  err_out_free_irqs:
  	ionic_bus_free_irq_vectors(ionic);
  err_out_pci:
+	ionic_port_reset(ionic);
  	ionic_dev_teardown(ionic);
  	ionic_clear_pci(ionic);
  	ionic_debugfs_del_dev(ionic);
#5 lore raw

Re: [PATCH net v2 2/2] ionic: free port_info when probe fails after the port is set up

In reply to Eric Joyner
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new ionic_port_reset() call in ionic_probe()'s err_out_pci frees the port_info buffer with dma_free_coherent() even when the…

--

ionic: free port_info when probe fails after the port is set up

This patch adds an ionic_port_reset() call at the err_out_pci label in ionic_probe(). The goal is to free the port_info DMA buffer that ionic_setup_one()->ionic_port_init() allocated when probe fails later. It follows the cleanup order already used in ionic_remove().

drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 400

Quoted 9 lines
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +1 -0
index 05f19489ec5c8..68b3bceedd999 100644
@@ -397,6 +397,7 @@ static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 err_out_free_irqs:
 	ionic_bus_free_irq_vectors(ionic);
 err_out_pci:
+	ionic_port_reset(ionic);

[Severity: Medium]
Is it safe to free port_info here if the PORT_RESET command never completes?

ionic_port_reset() sends the command, but the wait result does not decide whether the free happens:

drivers/net/ethernet/pensando/ionic/ionic_main.c:ionic_port_reset() {
    ...
	if (ionic_is_fw_running(idev)) {
		mutex_lock(&ionic->dev_cmd_lock);
		ionic_dev_cmd_port_reset(idev);
		err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
		mutex_unlock(&ionic->dev_cmd_lock);
	}

	dma_free_coherent(ionic->dev, idev->port_info_sz,
			  idev->port_info, idev->port_info_pa);
    ...
}

__ionic_dev_cmd_wait() can return -ETIMEDOUT while the firmware still reports that it is running:

	if (!done && !time_before(jiffies, max_wait)) {
		ionic_dev_cmd_clean(ionic);
		dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
			 ionic_opcode_to_str(opcode), opcode, max_seconds);
		return -ETIMEDOUT;
	}

ionic_dev_cmd_port_init() passed port_info_pa to the firmware through port_init.info_pa. The comment in ionic_get_link_ksettings() says the NIC updates this buffer by DMA on its own.

Bus mastering, enabled by pci_set_master() in ionic_setup_one(), stays on until pci_disable_device() in ionic_clear_pci(). That runs only after ionic_dev_teardown().

Take a probe failure like an adminq timeout in ionic_lif_init(). The firmware could be running but not answering dev-cmds. If it keeps writing port status into port_info during that window, can it DMA into pages that have already been freed and possibly reused?

Before this patch, the buffer was leaked on this path instead of freed.

The commit message says:

the firmware gets a PORT_RESET before the buffer is freed, the same order as in ionic_remove().

However, the command is only sent here, and its completion is never checked. ionic_remove() uses the same pattern, so this patch carries that convention over to the probe failure path.

Should the free be skipped when the PORT_RESET wait fails? Alternatively, could it be deferred until bus mastering has been disabled?

Quoted 3 lines
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +0 -0
 	ionic_dev_teardown(ionic);
 	ionic_clear_pci(ionic);
 	ionic_debugfs_del_dev(ionic);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261015-ionic-port-info-lifetime-v2-0-57232013fd79%40amd.com
#6 lore raw

Re: [PATCH net v2 2/2] ionic: free port_info when probe fails after the port is set up

Quoted 96 lines · On 10/19/2026 4:20 PM, [email protected] wrote:

[Some people who received this message don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The new ionic_port_reset() call in ionic_probe()'s err_out_pci frees the port_info buffer with dma_free_coherent() even when the…

--

ionic: free port_info when probe fails after the port is set up

This patch adds an ionic_port_reset() call at the err_out_pci label in ionic_probe(). The goal is to free the port_info DMA buffer that ionic_setup_one()->ionic_port_init() allocated when probe fails later. It follows the cleanup order already used in ionic_remove().

drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 400

Quoted 9 lines
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +1 -0
index 05f19489ec5c8..68b3bceedd999 100644
@@ -397,6 +397,7 @@ static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 err_out_free_irqs:
      ionic_bus_free_irq_vectors(ionic);
 err_out_pci:
+     ionic_port_reset(ionic);

[Severity: Medium]
Is it safe to free port_info here if the PORT_RESET command never completes?

ionic_port_reset() sends the command, but the wait result does not decide whether the free happens:

drivers/net/ethernet/pensando/ionic/ionic_main.c:ionic_port_reset() {
    ...
        if (ionic_is_fw_running(idev)) {
                mutex_lock(&ionic->dev_cmd_lock);
                ionic_dev_cmd_port_reset(idev);
                err = ionic_dev_cmd_wait(ionic, DEVCMD_TIMEOUT);
                mutex_unlock(&ionic->dev_cmd_lock);
        }

        dma_free_coherent(ionic->dev, idev->port_info_sz,
                          idev->port_info, idev->port_info_pa);
    ...
}

__ionic_dev_cmd_wait() can return -ETIMEDOUT while the firmware still reports that it is running:

        if (!done && !time_before(jiffies, max_wait)) {
                ionic_dev_cmd_clean(ionic);
                dev_warn(ionic->dev, "DEVCMD %s (%d) timeout after %ld secs\n",
                         ionic_opcode_to_str(opcode), opcode, max_seconds);
                return -ETIMEDOUT;
        }

ionic_dev_cmd_port_init() passed port_info_pa to the firmware through port_init.info_pa. The comment in ionic_get_link_ksettings() says the NIC updates this buffer by DMA on its own.

Bus mastering, enabled by pci_set_master() in ionic_setup_one(), stays on until pci_disable_device() in ionic_clear_pci(). That runs only after ionic_dev_teardown().

Take a probe failure like an adminq timeout in ionic_lif_init(). The firmware could be running but not answering dev-cmds. If it keeps writing port status into port_info during that window, can it DMA into pages that have already been freed and possibly reused?

Before this patch, the buffer was leaked on this path instead of freed.

The commit message says:

the firmware gets a PORT_RESET before the buffer is freed, the same order as in ionic_remove().

However, the command is only sent here, and its completion is never checked. ionic_remove() uses the same pattern, so this patch carries that convention over to the probe failure path.

Should the free be skipped when the PORT_RESET wait fails? Alternatively, could it be deferred until bus mastering has been disabled?

Quoted 6 lines

drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c static int ionic_probe(struct pci_dev *pdev, const struct pci_device_id *ent) · line 403

Quoted 3 lines
drivers/net/ethernet/pensando/ionic/ionic_bus_pci.c +0 -0
      ionic_dev_teardown(ionic);
      ionic_clear_pci(ionic);
      ionic_debugfs_del_dev(ionic);

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261015-ionic-port-info-lifetime-v2-0-57232013fd79%40amd.com

Hi netdev's Sashiko Bot,

This finding is that this invites a change to cleanup handling in ionic that is outside the scope of the patch; this patch does attempt to slightly clean up things by changing a leak to a proper free while tearing down the driver after a failure in probe, but it is valid that the device could still write into the freed memory if that cleanup also has a failure.

I think a proper fix could be patterned after Nikhil's patch in 6443f4f20bda ("pds_core: quiesce DMA before freeing resources"), but that was applied to net-next:
https://lore.kernel.org/all/178096501514.1726280.11716563786761485251.git-patchwork-notify@kernel.org/

- Eric

#7 lore raw
In reply to Eric Joyner
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

#8 lore raw
Quoted 20 lines · On 10/15/2026 4:08 PM, [email protected] wrote:

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

- Whether the issue was actually triggered, or is only theoretical (e.g. found by code inspection). If it was triggered please include the symptoms, like the stack trace or error messages.

- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.

Quoted 8 lines

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account. If the series needs another revision for other reasons, please include the information in the commit messages then.

The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.

Ok:

- This was discovered by a Sashiko review for v1 of a different patchset; we evaluated the discovery internally and it looked like something that should be fixed.

- It's theoretical by finding, but...

- I tested it on an actual DSC3 "Pollara" with a hacked driver and test kernel to cause both issues:
- Had PORT_INIT fail during PCI reset recovery and saw that the port_info
buffer was freed while the netdev was still registered, meaning that the netdev ethtool ops could get a NULL pointer for port_info
- Made probe fail on purpose at ionic_setup_one() to verify that port_info
memory and DMA allocation was being leaked

and then checked that the patched one did not have these issues.

- Eric

#9 lore raw
In reply to Eric Joyner
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 11 lines · On Thu, 15 Oct 2026 16:03:29 -0700 you wrote:

Two fixes for the lifetime of the ionic port_info DMA buffer.

Patch 1 fixes a use-after-free. ionic_port_init() allocates port_info only when the pointer is NULL, but its error path frees the buffer unconditionally. On the firmware recovery paths, ionic_lif_handle_fw_up() and ionic_reset_done(), the buffer belongs to an earlier successful init, so a failed PORT_INIT frees it under any ethtool op that is already reading it. The error path now frees only what the same call allocated.

[...]

Here is the summary with links:
- [net,v2,1/2] ionic: fix port_info use-after-free on firmware recovery

    https://git.kernel.org/netdev/net-next/c/d90d1a768b3b
  - [net,v2,2/2] ionic: free port_info when probe fails after the port is set up
    https://git.kernel.org/netdev/net-next/c/4e15e0a941ce

You are awesome, thank you!

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