Skip to contentSkip to conversation

net: nvidia: forcedeth: Fix two possible concurrency use-after-free bugs

PATCH In review Jia-Ju Bai 3 participants 10 messages Jan 8, 2019 – Jan 12, 2019

Subsystems (3)
lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 net: nvidia: forcedeth: Fix two possible concurrency use-after-free bugs

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

    line 4134: spin_lock(&np->lock);
    nv_drain_rxtx
      nv_drain_tx
        nv_release_txskb
          line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

To fix these possible bugs, the calls to spin_lock_irqsave() in nv_start_xmit() and nv_start_xmit_optimized() are moved to the front of "prev_tx_ctx->skb = skb;"

---
 drivers/net/ethernet/nvidia/forcedeth.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/nvidia/forcedeth.c +4 -4
index 1d9b0d44ddb6..48fa5a0bd2cb 100644
@@ -2317,6 +2317,8 @@ static netdev_tx_t nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
 	/* set last fragment flag  */
 	prev_tx->flaglen |= cpu_to_le32(tx_flags_extra);
 
+	spin_lock_irqsave(&np->lock, flags);
+
 	/* save skb in this slot's context area */
 	prev_tx_ctx->skb = skb;
 
@@ -2326,8 +2328,6 @@ static netdev_tx_t nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
 		tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
 			 NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
 
-	spin_lock_irqsave(&np->lock, flags);
-
 	/* set tx flags */
 	start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
 
@@ -2475,6 +2475,8 @@ static netdev_tx_t nv_start_xmit_optimized(struct sk_buff *skb,
 	/* set last fragment flag  */
 	prev_tx->flaglen |= cpu_to_le32(NV_TX2_LASTPACKET);
 
+	spin_lock_irqsave(&np->lock, flags);
+
 	/* save skb in this slot's context area */
 	prev_tx_ctx->skb = skb;
 
@@ -2491,8 +2493,6 @@ static netdev_tx_t nv_start_xmit_optimized(struct sk_buff *skb,
 	else
 		start_tx->txvlan = 0;
 
-	spin_lock_irqsave(&np->lock, flags);
-
 	if (np->tx_limit) {
 		/* Limit the number of outstanding tx. Setup all fragments, but
 		 * do not set the VALID bit on the first descriptor. Save a pointer
Signature · 1 line
2.17.0
#1 lore raw
Quoted 18 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

     line 4134: spin_lock(&np->lock);
     nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);
Quoted 3 lines

Thus, two possible concurrency use-after-free bugs may occur.

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

Quoted 49 lines

the calls to spin_lock_irqsave() in
nv_start_xmit() and nv_start_xmit_optimized() are moved to the front of "prev_tx_ctx->skb = skb;"

---
  drivers/net/ethernet/nvidia/forcedeth.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)
drivers/net/ethernet/nvidia/forcedeth.c +4 -4
index 1d9b0d44ddb6..48fa5a0bd2cb 100644
@@ -2317,6 +2317,8 @@ static netdev_tx_t nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
  	/* set last fragment flag  */
  	prev_tx->flaglen |= cpu_to_le32(tx_flags_extra);
 
+	spin_lock_irqsave(&np->lock, flags);
+
  	/* save skb in this slot's context area */
  	prev_tx_ctx->skb = skb;
 
@@ -2326,8 +2328,6 @@ static netdev_tx_t nv_start_xmit(struct sk_buff *skb, struct net_device *dev)
  		tx_flags_extra = skb->ip_summed == CHECKSUM_PARTIAL ?
  			 NV_TX2_CHECKSUM_L3 | NV_TX2_CHECKSUM_L4 : 0;
 
-	spin_lock_irqsave(&np->lock, flags);
-
  	/* set tx flags */
  	start_tx->flaglen |= cpu_to_le32(tx_flags | tx_flags_extra);
 
@@ -2475,6 +2475,8 @@ static netdev_tx_t nv_start_xmit_optimized(struct sk_buff *skb,
  	/* set last fragment flag  */
  	prev_tx->flaglen |= cpu_to_le32(NV_TX2_LASTPACKET);
 
+	spin_lock_irqsave(&np->lock, flags);
+
  	/* save skb in this slot's context area */
  	prev_tx_ctx->skb = skb;
 
@@ -2491,8 +2493,6 @@ static netdev_tx_t nv_start_xmit_optimized(struct sk_buff *skb,
  	else
  		start_tx->txvlan = 0;
 
-	spin_lock_irqsave(&np->lock, flags);
-
  	if (np->tx_limit) {
  		/* Limit the number of outstanding tx. Setup all fragments, but
  		 * do not set the VALID bit on the first descriptor. Save a pointer
author
#2 lore raw
Quoted 20 lines · Zhu Yanjun
Quoted 18 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

     line 4134: spin_lock(&np->lock);
     nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);
Quoted 6 lines
Quoted 3 lines

Thus, two possible concurrency use-after-free bugs may occur.

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Best wishes,
Jia-Ju Bai

#3 lore raw
Quoted 25 lines · Jia-Ju Bai
Quoted 22 lines · Zhu Yanjun
Quoted 20 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
   line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
   line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
   nv_do_nic_poll
     line 4134: spin_lock(&np->lock); nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Before "line 2004: dev_kfree_skb_any(tx_skb->skb); ",

"

                nv_disable_irq(dev); nv_napi_disable(dev); netif_tx_lock_bh(dev); netif_addr_lock(dev); spin_lock(&np->lock);
                /* stop engines */ nv_stop_rxtx(dev);   <---this stop rxtx nv_txrx_reset(dev);
"

In this case, does nv_start_xmit or nv_start_xmit_optimized still work well?

Zhu Yanjun

Quoted 4 lines

Best wishes,
Jia-Ju Bai

author
#4 lore raw
Quoted 47 lines · Yanjun Zhu
Quoted 25 lines · Jia-Ju Bai
Quoted 22 lines · Zhu Yanjun
Quoted 20 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

     line 4134: spin_lock(&np->lock);
     nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Before "line 2004: dev_kfree_skb_any(tx_skb->skb); ",

"

                nv_disable_irq(dev);
                nv_napi_disable(dev);
                netif_tx_lock_bh(dev);
                netif_addr_lock(dev);
                spin_lock(&np->lock);
                /* stop engines */
                nv_stop_rxtx(dev);   <---this stop rxtx
                nv_txrx_reset(dev);
Quoted 4 lines

"

In this case, does nv_start_xmit or nv_start_xmit_optimized still work well?

nv_stop_rxtx() calls nv_stop_tx(dev).

static void nv_stop_tx(struct net_device *dev)

{
    struct fe_priv *np = netdev_priv(dev);
    u8 __iomem *base = get_hwbase(dev);
    u32 tx_ctrl = readl(base + NvRegTransmitterControl);

    if (!np->mac_in_use)
        tx_ctrl &= ~NVREG_XMITCTL_START;
    else
        tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN;
    writel(tx_ctrl, base + NvRegTransmitterControl);
    if (reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
              NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX))
        netdev_info(dev, "%s: TransmitterStatus remained busy\n",
                __func__);

    udelay(NV_TXSTOP_DELAY2);
    if (!np->mac_in_use)
        writel(readl(base + NvRegTransmitPoll) & 
NVREG_TRANSMITPOLL_MAC_ADDR_REV,
               base + NvRegTransmitPoll);
}

nv_stop_tx() seems to only write registers to stop transmitting for hardware.
But it does not wait until nv_start_xmit() and nv_start_xmit_optimized() finish execution.
Maybe netif_stop_queue() should be used here to stop transmitting for network layer, but this function does not seem to wait, either. Do you know any function that can wait until ".ndo_start_xmit" finish execution?

Best wishes,
Jia-Ju Bai

#5 lore raw
Quoted 73 lines · Jia-Ju Bai
Quoted 49 lines · Yanjun Zhu
Quoted 25 lines · Jia-Ju Bai
Quoted 22 lines · Zhu Yanjun
Quoted 20 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
   line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
   line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
   nv_do_nic_poll
     line 4134: spin_lock(&np->lock); nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Before "line 2004: dev_kfree_skb_any(tx_skb->skb); ",

"

                nv_disable_irq(dev); nv_napi_disable(dev); netif_tx_lock_bh(dev); netif_addr_lock(dev); spin_lock(&np->lock);
                /* stop engines */ nv_stop_rxtx(dev);   <---this stop rxtx nv_txrx_reset(dev);
"

Quoted 2 lines

In this case, does nv_start_xmit or nv_start_xmit_optimized still work well?

nv_stop_rxtx() calls nv_stop_tx(dev).

static void nv_stop_tx(struct net_device *dev) {
    struct fe_priv *np = netdev_priv(dev);
    u8 __iomem *base = get_hwbase(dev);
    u32 tx_ctrl = readl(base + NvRegTransmitterControl);

    if (!np->mac_in_use)
        tx_ctrl &= ~NVREG_XMITCTL_START;
    else
        tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN; writel(tx_ctrl, base + NvRegTransmitterControl);
    if (reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0, NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX)) netdev_info(dev, "%s: TransmitterStatus remained busy\n", __func__);

Quoted 8 lines

    udelay(NV_TXSTOP_DELAY2);
    if (!np->mac_in_use)
        writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
}

nv_stop_tx() seems to only write registers to stop transmitting for hardware. But it does not wait until nv_start_xmit() and nv_start_xmit_optimized() finish execution.

There are 3 modes in forcedeth NIC.
In throughput mode (0), every tx & rx packet will generate an interrupt.
In CPU mode (1), interrupts are controlled by a timer.
In dynamic mode (2), the mode toggles between throughput and CPU mode based on network load.

From the source code,

"np->recover_error = 1;" is related with CPU mode.

nv_start_xmit or nv_start_xmit_optimized seems related with ghroughput mode.

In static void nv_do_nic_poll(struct timer_list *t),
when  if (np->recover_error), line 2004: dev_kfree_skb_any(tx_skb->skb); will run.

When "np->recover_error=1", do you think nv_start_xmit or nv_start_xmit_optimized will be called?

Quoted 8 lines

Maybe netif_stop_queue() should be used here to stop transmitting for network layer, but this function does not seem to wait, either. Do you know any function that can wait until ".ndo_start_xmit" finish execution?

Best wishes,
Jia-Ju Bai

author
#6 lore raw
Quoted 91 lines · Yanjun Zhu
Quoted 80 lines · Jia-Ju Bai
Quoted 47 lines · Yanjun Zhu
Quoted 25 lines · Jia-Ju Bai
Quoted 22 lines · Zhu Yanjun
Quoted 20 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

     line 4134: spin_lock(&np->lock);
     nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Before "line 2004: dev_kfree_skb_any(tx_skb->skb); ",

"

                nv_disable_irq(dev);
                nv_napi_disable(dev);
                netif_tx_lock_bh(dev);
                netif_addr_lock(dev);
                spin_lock(&np->lock);
                /* stop engines */
                nv_stop_rxtx(dev);   <---this stop rxtx
                nv_txrx_reset(dev);
Quoted 4 lines

"

In this case, does nv_start_xmit or nv_start_xmit_optimized still work well?

nv_stop_rxtx() calls nv_stop_tx(dev).

static void nv_stop_tx(struct net_device *dev)

{
    struct fe_priv *np = netdev_priv(dev);
    u8 __iomem *base = get_hwbase(dev);
    u32 tx_ctrl = readl(base + NvRegTransmitterControl);

    if (!np->mac_in_use)
        tx_ctrl &= ~NVREG_XMITCTL_START;
    else
        tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN;
    writel(tx_ctrl, base + NvRegTransmitterControl);
    if (reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0,
              NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX))
        netdev_info(dev, "%s: TransmitterStatus remained busy\n",
                __func__);

    udelay(NV_TXSTOP_DELAY2);
    if (!np->mac_in_use)
        writel(readl(base + NvRegTransmitPoll) & 
NVREG_TRANSMITPOLL_MAC_ADDR_REV,
               base + NvRegTransmitPoll);
}
Quoted 2 lines

nv_stop_tx() seems to only write registers to stop transmitting for hardware. But it does not wait until nv_start_xmit() and nv_start_xmit_optimized() finish execution.

There are 3 modes in forcedeth NIC.
In throughput mode (0), every tx & rx packet will generate an interrupt.
In CPU mode (1), interrupts are controlled by a timer.
In dynamic mode (2), the mode toggles between throughput and CPU mode based on network load.

From the source code,

Quoted 8 lines

"np->recover_error = 1;" is related with CPU mode.

nv_start_xmit or nv_start_xmit_optimized seems related with ghroughput mode.

In static void nv_do_nic_poll(struct timer_list *t),
when if (np->recover_error), line 2004: dev_kfree_skb_any(tx_skb->skb); will run.

When "np->recover_error=1", do you think nv_start_xmit or nv_start_xmit_optimized will be called?

Sorry, I do not know about these modes...
But I still think nv_start_xmit() or nv_start_xmit_optimized() can be called here, in no matter which mode :)

Best wishes,
Jia-Ju Bai

#7 lore raw
Quoted 97 lines · Jia-Ju Bai
Quoted 94 lines · Yanjun Zhu
Quoted 73 lines · Jia-Ju Bai
Quoted 49 lines · Yanjun Zhu
Quoted 25 lines · Jia-Ju Bai
Quoted 22 lines · Zhu Yanjun
Quoted 20 lines · Jia-Ju Bai

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
   line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
   line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
   nv_do_nic_poll
     line 4134: spin_lock(&np->lock); nv_drain_rxtx
       nv_drain_tx
         nv_release_txskb
           line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

To fix these possible bugs,

Does this really occur? Can you reproduce this ?

This bug is not found by the real execution.
It is found by a static tool written by myself, and then I check it by manual code review.

Before "line 2004: dev_kfree_skb_any(tx_skb->skb); ",

"

                nv_disable_irq(dev); nv_napi_disable(dev); netif_tx_lock_bh(dev); netif_addr_lock(dev); spin_lock(&np->lock);
                /* stop engines */ nv_stop_rxtx(dev);   <---this stop rxtx nv_txrx_reset(dev);
"

Quoted 2 lines

In this case, does nv_start_xmit or nv_start_xmit_optimized still work well?

nv_stop_rxtx() calls nv_stop_tx(dev).

static void nv_stop_tx(struct net_device *dev) {
    struct fe_priv *np = netdev_priv(dev);
    u8 __iomem *base = get_hwbase(dev);
    u32 tx_ctrl = readl(base + NvRegTransmitterControl);

    if (!np->mac_in_use)
        tx_ctrl &= ~NVREG_XMITCTL_START;
    else
        tx_ctrl |= NVREG_XMITCTL_TX_PATH_EN; writel(tx_ctrl, base + NvRegTransmitterControl);
    if (reg_delay(dev, NvRegTransmitterStatus, NVREG_XMITSTAT_BUSY, 0, NV_TXSTOP_DELAY1, NV_TXSTOP_DELAY1MAX)) netdev_info(dev, "%s: TransmitterStatus remained busy\n", __func__);

Quoted 8 lines

    udelay(NV_TXSTOP_DELAY2);
    if (!np->mac_in_use)
        writel(readl(base + NvRegTransmitPoll) & NVREG_TRANSMITPOLL_MAC_ADDR_REV, base + NvRegTransmitPoll);
}

nv_stop_tx() seems to only write registers to stop transmitting for hardware. But it does not wait until nv_start_xmit() and nv_start_xmit_optimized() finish execution.

There are 3 modes in forcedeth NIC.
In throughput mode (0), every tx & rx packet will generate an interrupt.
In CPU mode (1), interrupts are controlled by a timer.
In dynamic mode (2), the mode toggles between throughput and CPU mode based on network load.

From the source code,

"np->recover_error = 1;" is related with CPU mode.

nv_start_xmit or nv_start_xmit_optimized seems related with ghroughput mode.

Quoted 7 lines
Quoted 4 lines

In static void nv_do_nic_poll(struct timer_list *t),
when  if (np->recover_error), line 2004: dev_kfree_skb_any(tx_skb->skb); will run.

When "np->recover_error=1", do you think nv_start_xmit or nv_start_xmit_optimized will be called?

Sorry, I do not know about these modes...
But I still think nv_start_xmit() or nv_start_xmit_optimized() can be called here, in no matter which mode :)

:-P

If you have forcedeth NIC, you can make tests with it.:-)

Quoted 5 lines

Best wishes,
Jia-Ju Bai

author
#8 lore raw
Quoted 2 lines · Yanjun Zhu

If you have forcedeth NIC, you can make tests with it.:-)

Ah, I would like to, but I do not have the hardware...

Best wishes,
Jia-Ju Bai

maintainer
#9 lore raw
In reply to Jia-Ju Bai

From: Jia-Ju Bai <[email protected]>
Date: Tue, 8 Jan 2019 20:45:18 +0800

Quoted 12 lines

In drivers/net/ethernet/nvidia/forcedeth.c, the functions nv_start_xmit() and nv_start_xmit_optimized() can be concurrently executed with nv_poll_controller().

nv_start_xmit
line 2321: prev_tx_ctx->skb = skb;

nv_start_xmit_optimized
line 2479: prev_tx_ctx->skb = skb;

nv_poll_controller
nv_do_nic_poll

Quoted 7 lines
    line 4134: spin_lock(&np->lock);
    nv_drain_rxtx
      nv_drain_tx
        nv_release_txskb
          line 2004: dev_kfree_skb_any(tx_skb->skb);

Thus, two possible concurrency use-after-free bugs may occur.

I do not think so, the netif_tx_lock_bh() done will prevent the parallel execution.