Skip to contentSkip to conversation

net/tls: Protect from calling tls_dev_del for TLS RX twice

PATCH Superseded Saeed Mahameed 4 participants 9 messages Nov 17, 2020 – Nov 25, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net/tls: Protect from calling tls_dev_del for TLS RX twice

From: Maxim Mikityanskiy <[email protected]>

tls_device_offload_cleanup_rx doesn't clear tls_ctx->netdev after calling tls_dev_del if TLX TX offload is also enabled. Clearing tls_ctx->netdev gets postponed until tls_device_gc_task. It leaves a time frame when tls_device_down may get called and call tls_dev_del for RX one extra time, confusing the driver, which may lead to a crash.

This patch corrects this racy behavior by adding a flag to prevent tls_device_down from calling tls_dev_del the second time.

Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")

---
For -stable: 5.3

 include/net/tls.h    | 1 +
 net/tls/tls_device.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/tls.h +1 -0
index baf1e99d8193..a0deddfde412 100644
@@ -199,6 +199,7 @@ enum tls_context_flags {
 	 * to be atomic.
 	 */
 	TLS_TX_SYNC_SCHED = 1,
+	TLS_RX_DEV_RELEASED = 2,
 };
 
 struct cipher_context {
net/tls/tls_device.c +2 -1
index cec86229a6a0..b2261caac6be 100644
@@ -1241,6 +1241,7 @@ void tls_device_offload_cleanup_rx(struct sock *sk)
 
 	netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
 					TLS_OFFLOAD_CTX_DIR_RX);
+	set_bit(TLS_RX_DEV_RELEASED, &tls_ctx->flags);
 
 	if (tls_ctx->tx_conf != TLS_HW) {
 		dev_put(netdev);
@@ -1274,7 +1275,7 @@ static int tls_device_down(struct net_device *netdev)
 		if (ctx->tx_conf == TLS_HW)
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_TX);
-		if (ctx->rx_conf == TLS_HW)
+		if (ctx->rx_conf == TLS_HW && !test_bit(TLS_RX_DEV_RELEASED, &ctx->flags))
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_RX);
 		WRITE_ONCE(ctx->netdev, NULL);
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better consistency and to add resiliency against the drivers that may pass SKBs with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups, including a call to the destructor. This way the code of GRO_MERGED_FREE becomes similar to kfree_skb_partial.

Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD case also in napi_frags_finish()")
Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")

---
For -stable: 5.4

 net/core/dev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
net/core/dev.c +1 -2
index 82dc6b48e45f..85dcc7f19902 100644
@@ -6048,8 +6048,7 @@ EXPORT_SYMBOL(gro_find_complete_by_type);
 
 static void napi_skb_free_stolen_head(struct sk_buff *skb)
 {
-	skb_dst_drop(skb);
-	skb_ext_put(skb);
+	skb_release_head_state(skb);
 	kmem_cache_free(skbuff_head_cache, skb);
 }
 
Signature · 1 line
2.26.2
maintainer
#2 lore raw

Re: [PATCH net 2/2] net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

Quoted 10 lines · On Tue, 17 Nov 2020 12:33:55 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better consistency and to add resiliency against the drivers that may pass SKBs with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups, including a call to the destructor. This way the code of GRO_MERGED_FREE becomes similar to kfree_skb_partial.

Quoted 5 lines
Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD case also in napi_frags_finish()")
Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")

CC Eric for GRO expertise.

Makes sense to me, but do you still need "net/mlx5e: Fix refcount leak on kTLS RX resync" even with this applied?

Quoted 14 lines
net/core/dev.c +1 -2
index 82dc6b48e45f..85dcc7f19902 100644
@@ -6048,8 +6048,7 @@ EXPORT_SYMBOL(gro_find_complete_by_type);
 
 static void napi_skb_free_stolen_head(struct sk_buff *skb)
 {
-	skb_dst_drop(skb);
-	skb_ext_put(skb);
+	skb_release_head_state(skb);
 	kmem_cache_free(skbuff_head_cache, skb);
 }
 
#3 lore raw

Re: [PATCH net 2/2] net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 17 Nov 2020 12:33:55 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better consistency and to add resiliency against the drivers that may pass SKBs with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups, including a call to the destructor. This way the code of GRO_MERGED_FREE becomes similar to kfree_skb_partial.

Quoted 7 lines
Quoted 5 lines
Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD case also in napi_frags_finish()")
Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")

CC Eric for GRO expertise.

Thanks for CCing me.

Since when drivers can pass funny skbs with destructors ???

Can we please stop adding more cycles to _already_ expensive GRO ?

Quoted 19 lines

Makes sense to me, but do you still need "net/mlx5e: Fix refcount leak on kTLS RX resync" even with this applied?

Quoted 14 lines
net/core/dev.c +1 -2
index 82dc6b48e45f..85dcc7f19902 100644
@@ -6048,8 +6048,7 @@ EXPORT_SYMBOL(gro_find_complete_by_type);
 
 static void napi_skb_free_stolen_head(struct sk_buff *skb)
 {
-     skb_dst_drop(skb);
-     skb_ext_put(skb);
+     skb_release_head_state(skb);
      kmem_cache_free(skbuff_head_cache, skb);
 }
 
maintainer
#4 lore raw

Re: [PATCH net 2/2] net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

Quoted 19 lines · On Wed, 18 Nov 2020 21:02:29 +0100 Eric Dumazet wrote:
Quoted 18 lines · Jakub Kicinski
Quoted 15 lines · On Tue, 17 Nov 2020 12:33:55 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better consistency and to add resiliency against the drivers that may pass SKBs with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups, including a call to the destructor. This way the code of GRO_MERGED_FREE becomes similar to kfree_skb_partial.

Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD case also in napi_frags_finish()")
Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")
Quoted 7 lines
Quoted 1 line

CC Eric for GRO expertise.

Thanks for CCing me.

Since when drivers can pass funny skbs with destructors ???

Can we please stop adding more cycles to _already_ expensive GRO ?

I don't think they do that today much (save for the ktls optimization in mlx5 Maxim is fixing separately). But I believe the idea of early demux in XDP had been floated in the past.

If we don't want that to happen we should document it (stating the obvious).

#5 lore raw

Re: [PATCH net 2/2] net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

Quoted 27 lines · Jakub Kicinski
Quoted 25 lines · On Wed, 18 Nov 2020 21:02:29 +0100 Eric Dumazet wrote:
Quoted 12 lines · Jakub Kicinski
Quoted 10 lines · On Tue, 17 Nov 2020 12:33:55 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better consistency and to add resiliency against the drivers that may pass SKBs with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups, including a call to the destructor. This way the code of GRO_MERGED_FREE becomes similar to kfree_skb_partial.

Quoted 7 lines
Quoted 5 lines
Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD case also in napi_frags_finish()")
Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")

CC Eric for GRO expertise.

Thanks for CCing me.

Since when drivers can pass funny skbs with destructors ???

Quoted 8 lines
Quoted 1 line

Can we please stop adding more cycles to _already_ expensive GRO ?

I don't think they do that today much (save for the ktls optimization in mlx5 Maxim is fixing separately). But I believe the idea of early demux in XDP had been floated in the past.

If we don't want that to happen we should document it (stating the obvious).

This is a patch targeting the net tree, with Fixes: tag pretending this is an old bug.

How can we possibly merge two skbs if they have destructors ?

We do not make sure it is even possible.

Many destructors track skb->truesize against a socket wmem_alloc or rmem_alloc, this stuff can not possibly work, unless stronger checks in GRO, since GRO changes skb->truesize
without checking skb->destructor.

If skb has a destructor, just bypass GRO completely, this is the only thing we can do.
This would be quite unfortunate to add such a check "just because someone tries to fool us"

net/core/dev.c +0 -0
index 4bfdcd6b20e8836e2884c51c6ce349ed54130bfa..76f0a627b6a1ee02339a724ecb6e4dbade80501b

100644

net/core/dev.c +0 -0
@@ -5920,7 +5920,7 @@ static enum gro_result dev_gro_receive(struct

napi_struct *napi, struct sk_buff

        int same_flow;
        int grow;

- if (netif_elide_gro(skb->dev))
+ if (netif_elide_gro(skb->dev) || skb->destructor)

                goto normal;

        gro_head = gro_list_prepare(napi, skb);
maintainer
#7 lore raw
In reply to Saeed Mahameed
Quoted 26 lines · On Tue, 17 Nov 2020 12:33:54 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

tls_device_offload_cleanup_rx doesn't clear tls_ctx->netdev after calling tls_dev_del if TLX TX offload is also enabled. Clearing tls_ctx->netdev gets postponed until tls_device_gc_task. It leaves a time frame when tls_device_down may get called and call tls_dev_del for RX one extra time, confusing the driver, which may lead to a crash.

This patch corrects this racy behavior by adding a flag to prevent tls_device_down from calling tls_dev_del the second time.

Fixes e8f69799810c ("net/tls: Add generic NIC offload infrastructure")

---
For -stable: 5.3

 include/net/tls.h    | 1 +
 net/tls/tls_device.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/tls.h +0 -0
index baf1e99d8193..a0deddfde412 100644

include/net/tls.h enum tls_context_flags { · line 201

Quoted 3 lines
include/net/tls.h +0 -0
@@ -199,6 +199,7 @@ enum tls_context_flags {
 	 * to be atomic.
 	 */
 	TLS_TX_SYNC_SCHED = 1,

Please add a comment here explaining that this bit is set when device state is partially released, and ctx->netdev cannot be cleared but RX side was already removed.

net/tls/tls_device.c void tls_device_offload_cleanup_rx(struct sock *sk) · line 1244

Quoted 13 lines
include/net/tls.h +1 -0
+	TLS_RX_DEV_RELEASED = 2,
 };
 
 struct cipher_context {
net/tls/tls_device.c +1 -0
index cec86229a6a0..b2261caac6be 100644
@@ -1241,6 +1241,7 @@ void tls_device_offload_cleanup_rx(struct sock *sk)
 
 	netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
 					TLS_OFFLOAD_CTX_DIR_RX);
+	set_bit(TLS_RX_DEV_RELEASED, &tls_ctx->flags);

Would the semantics of the bit be clearer if we only set the bit in an else branch below and renamed it TLS_RX_DEV_CLOSED?

Otherwise it could be confusing to the reader that his bit is only set here but not in tls_device_down().

Quoted 11 lines
net/tls/tls_device.c +1 -1
 	if (tls_ctx->tx_conf != TLS_HW) {
 		dev_put(netdev);
@@ -1274,7 +1275,7 @@ static int tls_device_down(struct net_device *netdev)
 		if (ctx->tx_conf == TLS_HW)
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_TX);
-		if (ctx->rx_conf == TLS_HW)
+		if (ctx->rx_conf == TLS_HW && !test_bit(TLS_RX_DEV_RELEASED, &ctx->flags))
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_RX);
 		WRITE_ONCE(ctx->netdev, NULL);
#6 lore raw

Re: [PATCH net 2/2] net: Call skb destructor on NAPI_GRO_FREE_STOLEN_HEAD

In reply to Eric Dumazet
Quoted 61 lines · Eric Dumazet
Quoted 36 lines · Jakub Kicinski
Quoted 26 lines · On Wed, 18 Nov 2020 21:02:29 +0100 Eric Dumazet wrote:
Quoted 24 lines · Jakub Kicinski
Quoted 22 lines · On Tue, 17 Nov 2020 12:33:55 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

All GRO flows except one call skb->destructor, however, GRO_MERGED_FREE
doesn't do it in case of NAPI_GRO_FREE_STOLEN_HEAD. For better
consistency and to add resiliency against the drivers that may pass SKBs
with a destructor, this patch changes napi_skb_free_stolen_head to use skb_release_head_state, which should perform all the needed cleanups,
including a call to the destructor. This way the code of GRO_MERGED_FREE
becomes similar to kfree_skb_partial.

Fixes e44699d2c280 ("net: handle NAPI_GRO_FREE_STOLEN_HEAD

case also in napi_frags_finish()")

Fixes d7e8883cfcf4 ("net: make GRO aware of skb->head_frag")
Quoted 8 lines
Quoted 1 line

CC Eric for GRO expertise.

Thanks for CCing me.

Since when drivers can pass funny skbs with destructors ???

Can we please stop adding more cycles to _already_ expensive GRO ?

Quoted 8 lines

I don't think they do that today much (save for the ktls optimization
in mlx5 Maxim is fixing separately). But I believe the idea of early
demux in XDP had been floated in the past.

If we don't want that to happen we should document it (stating the obvious).

This is a patch targeting the net tree, with Fixes: tag pretending this is an old bug.

How can we possibly merge two skbs if they have destructors ?

We do not make sure it is even possible.

Many destructors track skb->truesize against a socket wmem_alloc or rmem_alloc,
this stuff can not possibly work, unless stronger checks in GRO, since
GRO changes skb->truesize
without checking skb->destructor.

Quoted 5 lines

If skb has a destructor, just bypass GRO completely, this is the only thing we can do.
This would be quite unfortunate to add such a check "just because someone tries to fool us"

Thanks Eric !!
We don't actually need this patch, as the kTLS SKBs are handled locally in the drivers, I think we don't need to add any extra check in the datapath and just enforce the policy somehow with debug macros maybe WARN_ONE_ONCE()

I will drop this patch, but the XDP folks who are going to implement XDP early demux should take care of this themselves.

Quoted 17 lines
net/core/dev.c +0 -0

index 4bfdcd6b20e8836e2884c51c6ce349ed54130bfa..76f0a627b6a1ee02339a724ecb6 e4dbade80501b
100644

net/core/dev.c +0 -0
@@ -5920,7 +5920,7 @@ static enum gro_result dev_gro_receive(struct

napi_struct *napi, struct sk_buff

        int same_flow;
        int grow;
net/core/dev.c +1 -1
-       if (netif_elide_gro(skb->dev))
+       if (netif_elide_gro(skb->dev) || skb->destructor)
                goto normal;
 
        gro_head = gro_list_prepare(napi, skb);
#8 lore raw
In reply to Jakub Kicinski
Quoted 57 lines · Jakub Kicinski
Quoted 29 lines · On Tue, 17 Nov 2020 12:33:54 -0800 Saeed Mahameed wrote:

From: Maxim Mikityanskiy <[email protected]>

tls_device_offload_cleanup_rx doesn't clear tls_ctx->netdev after calling tls_dev_del if TLX TX offload is also enabled. Clearing tls_ctx->netdev gets postponed until tls_device_gc_task. It leaves a
time frame when tls_device_down may get called and call tls_dev_del for
RX one extra time, confusing the driver, which may lead to a crash.

This patch corrects this racy behavior by adding a flag to prevent tls_device_down from calling tls_dev_del the second time.

Fixes e8f69799810c ("net/tls: Add generic NIC offload

infrastructure")

---
For -stable: 5.3

 include/net/tls.h    | 1 +
 net/tls/tls_device.c | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
include/net/tls.h +0 -0
index baf1e99d8193..a0deddfde412 100644

include/net/tls.h enum tls_context_flags { · line 201

Quoted 3 lines
include/net/tls.h +0 -0
@@ -199,6 +199,7 @@ enum tls_context_flags {
 	 * to be atomic.
 	 */
 	TLS_TX_SYNC_SCHED = 1,

Please add a comment here explaining that this bit is set when device state is partially released, and ctx->netdev cannot be cleared but RX side was already removed.

Quoted 9 lines
include/net/tls.h +1 -0
+	TLS_RX_DEV_RELEASED = 2,
 };
 
 struct cipher_context {
net/tls/tls_device.c +0 -0
index cec86229a6a0..b2261caac6be 100644

net/tls/tls_device.c void tls_device_offload_cleanup_rx(struct · line 1245

Quoted 5 lines
net/tls/tls_device.c +1 -0
@@ -1241,6 +1241,7 @@ void tls_device_offload_cleanup_rx(struct
 sock *sk)
 
 	netdev->tlsdev_ops->tls_dev_del(netdev, tls_ctx,
 					TLS_OFFLOAD_CTX_DIR_RX);
+	set_bit(TLS_RX_DEV_RELEASED, &tls_ctx->flags);

Would the semantics of the bit be clearer if we only set the bit in an
else branch below and renamed it TLS_RX_DEV_CLOSED?

Quoted 4 lines

Otherwise it could be confusing to the reader that his bit is only set
here but not in tls_device_down().

Thanks Jakub, Maxim handled both comments, I will send V2 and drop the other patch !