Skip to contentSkip to conversation

macsec: avoid use-after-free in macsec_handle_frame()

PATCH Applied Eric Dumazet 4 participants 6 messages Oct 7, 2020 – Oct 8, 2020

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

PATCH 1/1 macsec: avoid use-after-free in macsec_handle_frame()

From: Eric Dumazet <[email protected]>

De-referencing skb after call to gro_cells_receive() is not allowed. We need to fetch skb->len earlier.

Fixes 5491e7c6b1a9 ("macsec: enable GRO and RPS on macsec devices")
Cc Paolo Abeni <[email protected]>
---
 drivers/net/macsec.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/net/macsec.c +3 -1
index 9159846b8b9388644bcf8f136231726d8cf297f2..787ac2c8e74ebef65534291aacb6dc5d9b839b27 100644
@@ -1077,6 +1077,7 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
 	struct macsec_rx_sa *rx_sa;
 	struct macsec_rxh_data *rxd;
 	struct macsec_dev *macsec;
+	unsigned int len;
 	sci_t sci;
 	u32 hdr_pn;
 	bool cbit;
@@ -1232,9 +1233,10 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
 	macsec_rxsc_put(rx_sc);
 
 	skb_orphan(skb);
+	len = skb->len;
 	ret = gro_cells_receive(&macsec->gro_cells, skb);
 	if (ret == NET_RX_SUCCESS)
-		count_rx(dev, skb->len);
+		count_rx(dev, len);
 	else
 		macsec->secy.netdev->stats.rx_dropped++;
 
Signature · 1 line
2.28.0.806.g8561365e88-goog
maintainer
#1 lore raw

Hi,

drivers/net/macsec.c static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb) · line 1241

Quoted 11 lines · Eric Dumazet
drivers/net/macsec.c +2 -1
@@ -1232,9 +1233,10 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
 	macsec_rxsc_put(rx_sc);
 
 	skb_orphan(skb);
+	len = skb->len;
 	ret = gro_cells_receive(&macsec->gro_cells, skb);
 	if (ret == NET_RX_SUCCESS)
-		count_rx(dev, skb->len);
+		count_rx(dev, len);
 	else
 		macsec->secy.netdev->stats.rx_dropped++;

I'm sorry I'm low on coffee, but I can't see the race?!? here we are in a BH section, and the above code dereference the skb only if it's has been enqueued into the gro_cells napi. It could be dequeued/dropped only after we leave this section ?!?

Thanks,

Paolo

#2 lore raw
Quoted 7 lines · Paolo Abeni

Hi,

Quoted 3 lines · Eric Dumazet
drivers/net/macsec.c +0 -0
@@ -1232,9 +1233,10 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
      macsec_rxsc_put(rx_sc);
 
Quoted 13 lines

drivers/net/macsec.c static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb) · line 1241

Quoted 8 lines
drivers/net/macsec.c +2 -1
      skb_orphan(skb);
+     len = skb->len;
      ret = gro_cells_receive(&macsec->gro_cells, skb);
      if (ret == NET_RX_SUCCESS)
-             count_rx(dev, skb->len);
+             count_rx(dev, len);
      else
              macsec->secy.netdev->stats.rx_dropped++;

I'm sorry I'm low on coffee, but I can't see the race?!? here we are in a BH section, and the above code dereference the skb only if it's has been enqueued into the gro_cells napi. It could be dequeued/dropped only after we leave this section ?!?

We should think of this as an alias for napi_gro_receive(), and not make any assumptions.
Semantically the skb has been given to another layer.

netif_rx() can absolutely queue the skb to another cpu backlog (RPS, RFS...), and the other cpu might have consumed the skb right away.

We can avoid these subtle bugs by respecting simple rules ;)

Quoted 5 lines

Thanks,

Paolo

maintainer Acked-by
#3 lore raw
Quoted 21 lines · Eric Dumazet
Quoted 6 lines · Paolo Abeni

Hi,

Quoted 3 lines · Eric Dumazet
drivers/net/macsec.c +0 -0
@@ -1232,9 +1233,10 @@ static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb)
      macsec_rxsc_put(rx_sc);
 
Quoted 13 lines

drivers/net/macsec.c static rx_handler_result_t macsec_handle_frame(struct sk_buff **pskb) · line 1241

Quoted 8 lines
drivers/net/macsec.c +2 -1
      skb_orphan(skb);
+     len = skb->len;
      ret = gro_cells_receive(&macsec->gro_cells, skb);
      if (ret == NET_RX_SUCCESS)
-             count_rx(dev, skb->len);
+             count_rx(dev, len);
      else
              macsec->secy.netdev->stats.rx_dropped++;

I'm sorry I'm low on coffee, but I can't see the race?!? here we are in a BH section, and the above code dereference the skb only if it's has been enqueued into the gro_cells napi. It could be dequeued/dropped only after we leave this section ?!?

Quoted 5 lines

We should think of this as an alias for napi_gro_receive(), and not make any assumptions.
Semantically the skb has been given to another layer. netif_rx() can absolutely queue the skb to another cpu backlog (RPS, RFS...), and the other cpu might have consumed the skb right away.

Ah! I completely missed that code path in gro_cells_receive()! Thank you for pointing that out!

#5 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 10 lines · On Wed, 7 Oct 2020 01:42:46 -0700 you wrote:

From: Eric Dumazet <[email protected]>

De-referencing skb after call to gro_cells_receive() is not allowed. We need to fetch skb->len earlier.

Fixes 5491e7c6b1a9 ("macsec: enable GRO and RPS on macsec devices")
Cc Paolo Abeni <[email protected]>

[...]

Here is the summary with links:
- [net] macsec: avoid use-after-free in macsec_handle_frame()

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

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html