Skip to contentSkip to conversation

net: ena: PHC: Fix potential use-after-free in get_timestamp

PATCH Applied Arthur Kiyanovski 3 participants 3 messages May 8 – May 10

Subsystems (4)
lore
Share & tools
lore

Arthur Kiyanovski

author maintainer
#0 lore raw

PATCH 1/1 net: ena: PHC: Fix potential use-after-free in get_timestamp

Move the phc->active check and resp pointer assignment to after acquiring the spinlock. Previously, phc->active was checked without holding the lock, and resp was cached from ena_dev->phc.virt_addr before the lock was acquired.

If ena_com_phc_destroy() runs between the lockless active check and the lock acquisition, it sets active=false, releases the lock, frees the DMA memory, and sets virt_addr=NULL. The get_timestamp path would then read a NULL virt_addr and dereference it.

With both the active check and the pointer read under the lock, destroy cannot free the memory while get_timestamp is using it.

Fixes e0ea34158ee8 ("net: ena: Add PHC support in the ENA driver")
---
 drivers/net/ethernet/amazon/ena/ena_com.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/ethernet/amazon/ena/ena_com.c +5 -2
index e67b592..8c86789 100644
@@ -1782,20 +1782,23 @@ void ena_com_phc_destroy(struct ena_com_dev *ena_dev)
 
 int ena_com_phc_get_timestamp(struct ena_com_dev *ena_dev, u64 *timestamp)
 {
-	volatile struct ena_admin_phc_resp *resp = ena_dev->phc.virt_addr;
 	const ktime_t zero_system_time = ktime_set(0, 0);
 	struct ena_com_phc_info *phc = &ena_dev->phc;
+	volatile struct ena_admin_phc_resp *resp;
 	ktime_t expire_time;
 	ktime_t block_time;
 	unsigned long flags = 0;
 	int ret = 0;
 
+	spin_lock_irqsave(&phc->lock, flags);
+
 	if (!phc->active) {
+		spin_unlock_irqrestore(&phc->lock, flags);
 		netdev_err(ena_dev->net_device, "PHC feature is not active in the device\n");
 		return -EOPNOTSUPP;
 	}
 
-	spin_lock_irqsave(&phc->lock, flags);
+	resp = ena_dev->phc.virt_addr;
 
 	/* Check if PHC is in blocked state */
 	if (unlikely(ktime_compare(phc->system_time, zero_system_time))) {
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 40 lines · Arthur Kiyanovski

Move the phc->active check and resp pointer assignment to after acquiring the spinlock. Previously, phc->active was checked without holding the lock, and resp was cached from ena_dev->phc.virt_addr before the lock was acquired.

If ena_com_phc_destroy() runs between the lockless active check and the lock acquisition, it sets active=false, releases the lock, frees the DMA memory, and sets virt_addr=NULL. The get_timestamp path would then read a NULL virt_addr and dereference it.

With both the active check and the pointer read under the lock, destroy cannot free the memory while get_timestamp is using it.

Fixes e0ea34158ee8 ("net: ena: Add PHC support in the ENA driver")
---
  drivers/net/ethernet/amazon/ena/ena_com.c | 7 +++++--
  1 file changed, 5 insertions(+), 2 deletions(-)
drivers/net/ethernet/amazon/ena/ena_com.c +3 -1
index e67b592..8c86789 100644
@@ -1782,20 +1782,23 @@ void ena_com_phc_destroy(struct ena_com_dev *ena_dev)
 
  int ena_com_phc_get_timestamp(struct ena_com_dev *ena_dev, u64 *timestamp)
  {
-	volatile struct ena_admin_phc_resp *resp = ena_dev->phc.virt_addr;
  	const ktime_t zero_system_time = ktime_set(0, 0);
  	struct ena_com_phc_info *phc = &ena_dev->phc;
+	volatile struct ena_admin_phc_resp *resp;
  	ktime_t expire_time;
  	ktime_t block_time;
  	unsigned long flags = 0;
  	int ret = 0;
 
+	spin_lock_irqsave(&phc->lock, flags);
+
  	if (!phc->active) {

drivers/net/ethernet/amazon/ena/ena_com.c void ena_com_phc_destroy(struct ena_com_dev *ena_dev) · line 1804

Quoted 10 lines
drivers/net/ethernet/amazon/ena/ena_com.c +2 -1
+		spin_unlock_irqrestore(&phc->lock, flags);
  		netdev_err(ena_dev->net_device, "PHC feature is not active in the device\n");
  		return -EOPNOTSUPP;
  	}
 
-	spin_lock_irqsave(&phc->lock, flags);
+	resp = ena_dev->phc.virt_addr;
 
  	/* Check if PHC is in blocked state */
  	if (unlikely(ktime_compare(phc->system_time, zero_system_time))) {
#2 lore raw
In reply to Arthur Kiyanovski
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 Fri, 8 May 2026 06:21:21 +0000 you wrote:

Move the phc->active check and resp pointer assignment to after acquiring the spinlock. Previously, phc->active was checked without holding the lock, and resp was cached from ena_dev->phc.virt_addr before the lock was acquired.

If ena_com_phc_destroy() runs between the lockless active check and the lock acquisition, it sets active=false, releases the lock, frees the DMA memory, and sets virt_addr=NULL. The get_timestamp path would then read a NULL virt_addr and dereference it.

[...]

Here is the summary with links:
- [net] net: ena: PHC: Fix potential use-after-free in get_timestamp

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

You are awesome, thank you!

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