Skip to contentSkip to conversation

tls: fix device-down socket lifetime and offload state

PATCH New Rishikesh Jethwani 1 participant 4 messages Nov 26

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

v7 cover tls: fix device-down socket lifetime and offload state

This series fixes a socket use-after-free and incorrect TX/RX driver-state handling in tls_device_down().

Patch 1 sets sk_validate_xmit_skb while holding tls_device_lock, before tls_device_queue_ctx_destruction() can drop the socket's context reference and allow __sk_destruct() to free the socket.

Patch 2 installs tls_validate_xmit_skb_sw() only on hardware-TX sockets, leaving RX-only and software-TX sockets unchanged. Setup publishes tls_validate_xmit_skb() before releasing device_offload_lock, and TLS_TX_DEV_DOWN records the replacement for the TX tls_dev_del() call without relying on tx_conf.

Patch 3 extends the existing TLS_RX_DEV_CLOSED flag to include RX driver state that was never installed. Setup and deletion update the flag under device_offload_lock, allowing tls_device_down() to decide whether to call the RX tls_dev_del() without reading rx_conf before setup publishes it.

Changes in v7:
- Split the device-down classification patch into separate TX (tls_validate_xmit_skb_sw() on RX-only sockets) and RX (TLS_RX_DEV_CLOSED extended to "never installed") fixes.
- Commit messages: name the functions involved instead of "the destructor", "the validator" and "the fallback".
- Drop the added comment in tls_device_down().
- Move the f08d8c1bb97c dependency into the Cc: stable line.

Changes in v6:
- Resolved the conflict with the recent fix.

Changes in v5:
- Reorder: the socket use-after-free fix is now patch 1.

v6: https://lore.kernel.org/netdev/[email protected]/
v5: https://lore.kernel.org/netdev/[email protected]/
v4: https://lore.kernel.org/netdev/[email protected]/

Rishikesh Jethwani (3):
tls: fix socket use-after-free during device-down
tls: restrict device-down TX fallback to hardware TX sockets
tls: use TLS_RX_DEV_CLOSED to skip RX tls_dev_del in device-down

 include/net/tls.h    | 10 ++++++----
 net/tls/tls_device.c | 28 +++++++++++++++++-----------
 net/tls/tls_main.c   |  1 +
 3 files changed, 24 insertions(+), 15 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v7 1/3 tls: fix socket use-after-free during device-down

tls_device_down() takes a reference on each TLS context, then drops tls_device_lock before setting ctx->sk->sk_validate_xmit_skb to tls_validate_xmit_skb_sw(). The socket can be freed in between:
tls_device_sk_destruct() calls tls_device_queue_ctx_destruction(), which drops the socket's context reference but does not free the context because device-down still holds one, and __sk_destruct() then frees the socket.

Set sk_validate_xmit_skb while walking tls_device_list under tls_device_lock. tls_device_queue_ctx_destruction() takes the same lock to drop its reference and remove the context from the list, so the socket cannot be freed before the store completes.

Checking sk_refcnt instead would not work: it can already be zero while queued SKBs still hold sk_wmem_alloc and need the software fallback.

Found by code inspection. An ASan harness running the extracted teardown functions with userspace shims reproduces the UAF before this fix and passes afterward. Tested on Mellanox ConnectX-6 Dx (Crypto Enabled).

Fixes c55dcdd435aa ("net/tls: Fix use-after-free after the TLS device goes down and up")
Cc [email protected] # needs commit f08d8c1bb97c ("net/tls: Fix race in TLS device down flow")
---
 net/tls/tls_device.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
net/tls/tls_device.c +5 -5
index fa7170cd4fd1..3ea764bdc949 100644
@@ -1345,6 +1345,11 @@ static int tls_device_down(struct net_device *netdev)
 		    !refcount_inc_not_zero(&ctx->refcount))
 			continue;
 
+		/* Stop offloaded TX and switch to the fallback.
+		 * tls_is_skb_tx_device_offloaded will return false.
+		 */
+		WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
+
 		list_move(&ctx->list, &list);
 	}
 	spin_unlock_irqrestore(&tls_device_lock, flags);
@@ -1352,11 +1357,6 @@ static int tls_device_down(struct net_device *netdev)
 	list_for_each_entry_safe(ctx, tmp, &list, list)	{
 		bool free_ctx;
 
-		/* Stop offloaded TX and switch to the fallback.
-		 * tls_is_skb_tx_device_offloaded will return false.
-		 */
-		WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
-
 		/* Stop the RX and TX resync.
 		 * tls_dev_resync must not be called after tls_dev_del.
 		 */
Signature · 1 line
2.50.1
#2 lore raw

PATCH v7 2/3 tls: restrict device-down TX fallback to hardware TX sockets

In reply to Rishikesh Jethwani

tls_device_down() sets sk_validate_xmit_skb to tls_validate_xmit_skb_sw() on every offloaded TLS socket on the device, including hardware-RX sockets without TX offload (RX-only, or software TLS TX). tls_validate_xmit_skb_sw() passes the skb to tls_sw_fallback(), which expects a tls_offload_context_tx in priv_ctx_tx, so such a socket can crash when transmitting a payload skb. In the oops below, a payload skb queued on the neighbour entry during the down/up cycle was flushed on ARP reply:

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  tls_sw_fallback+0x11b/0x450
  validate_xmit_skb.isra.0+0x14f/0x420
  __dev_queue_xmit+0x5e8/0xf30

Switch sk_validate_xmit_skb to tls_validate_xmit_skb_sw() only if it is currently tls_validate_xmit_skb(). Record the switch in the new TLS_TX_DEV_DOWN flag and call the TX tls_dev_del only when it is set, instead of checking tx_conf. do_tls_setsockopt_conf() sets tx_conf after tls_set_device_offload() has released device_offload_lock, so tls_device_down() can see a context whose tls_dev_add() succeeded with tx_conf not yet TLS_HW, skip the TX tls_dev_del and leak the driver state.

For the same reason, set sk_validate_xmit_skb in tls_set_device_offload() before releasing device_offload_lock. Otherwise tls_device_down() could run between tls_device_attach() and the store, skip the switch and the TX tls_dev_del, and setup would then set tls_validate_xmit_skb() on a context already detached from the device.

Contexts never return to tls_device_list, so TLS_TX_DEV_DOWN is never cleared.

Tested on Mellanox ConnectX-6 Dx (Crypto Enabled): the crash reproduces without this patch; with it, an RX-only socket survives interface down/up and keeps receiving TLS records.

Fixes c55dcdd435aa ("net/tls: Fix use-after-free after the TLS device goes down and up")
Fixes 4799ac81e52a ("tls: Add rx inline crypto offload")
---
 include/net/tls.h    |  2 ++
 net/tls/tls_device.c | 11 ++++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)
include/net/tls.h +2 -0
index 0b001b8ab1c3..f9540910b2e3 100644
@@ -194,6 +194,8 @@ enum tls_context_flags {
 	 * tls_dev_del call in tls_device_down if it happens simultaneously.
 	 */
 	TLS_RX_DEV_CLOSED = 2,
+	/* tls_device_down() replaced tls_validate_xmit_skb(). */
+	TLS_TX_DEV_DOWN = 3,
 };
 
 struct cipher_context {
net/tls/tls_device.c +8 -3
index 3ea764bdc949..3473299b16de 100644
@@ -1195,13 +1195,13 @@ int tls_set_device_offload(struct sock *sk)
 		goto release_lock;
 
 	tls_device_attach(ctx, sk, netdev);
-	up_read(&device_offload_lock);
 
 	/* following this assignment tls_is_skb_tx_device_offloaded
 	 * will return true and the context might be accessed
 	 * by the netdev's xmit function.
 	 */
 	smp_store_release(&sk->sk_validate_xmit_skb, tls_validate_xmit_skb);
+	up_read(&device_offload_lock);
 	dev_put(netdev);
 
 	return 0;
@@ -1348,7 +1348,12 @@ static int tls_device_down(struct net_device *netdev)
 		/* Stop offloaded TX and switch to the fallback.
 		 * tls_is_skb_tx_device_offloaded will return false.
 		 */
-		WRITE_ONCE(ctx->sk->sk_validate_xmit_skb, tls_validate_xmit_skb_sw);
+		if (READ_ONCE(ctx->sk->sk_validate_xmit_skb) ==
+		    tls_validate_xmit_skb) {
+			WRITE_ONCE(ctx->sk->sk_validate_xmit_skb,
+				   tls_validate_xmit_skb_sw);
+			set_bit(TLS_TX_DEV_DOWN, &ctx->flags);
+		}
 
 		list_move(&ctx->list, &list);
 	}
@@ -1372,7 +1377,7 @@ static int tls_device_down(struct net_device *netdev)
 		synchronize_net();
 
 		/* Release the offload context on the driver side. */
-		if (ctx->tx_conf == TLS_HW)
+		if (test_bit(TLS_TX_DEV_DOWN, &ctx->flags))
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_TX);
 		if (ctx->rx_conf == TLS_HW &&
Signature · 1 line
2.50.1
#3 lore raw

PATCH v7 3/3 tls: use TLS_RX_DEV_CLOSED to skip RX tls_dev_del in device-down

In reply to Rishikesh Jethwani

tls_device_down() calls the RX tls_dev_del only if rx_conf is TLS_HW and TLS_RX_DEV_CLOSED is clear. do_tls_setsockopt_conf() sets rx_conf after tls_set_device_offload_rx() has released device_offload_lock, so tls_device_down() can see a context whose RX tls_dev_add() succeeded with rx_conf not yet TLS_HW, skip the RX tls_dev_del and leak the driver state. rx_conf also shares a byte with tx_conf, which is written without device_offload_lock.

TLS_RX_DEV_CLOSED currently means that tls_device_offload_cleanup_rx() has already released the RX driver state. Extend it to also cover RX state that was never installed: set it in tls_ctx_create(), clear it in tls_set_device_offload_rx() after a successful tls_device_attach(), while still holding device_offload_lock, and set it after every RX tls_dev_del, including in tls_device_down() and when tls_device_offload_cleanup_rx() also drops the netdev. tls_device_down() then checks only this bit and no longer reads rx_conf.

Fixes 4799ac81e52a ("tls: Add rx inline crypto offload")
---
 include/net/tls.h    | 8 ++++----
 net/tls/tls_device.c | 9 +++++----
 net/tls/tls_main.c   | 1 +
 3 files changed, 10 insertions(+), 8 deletions(-)
include/net/tls.h +4 -4
index f9540910b2e3..53768fe11917 100644
@@ -188,10 +188,10 @@ enum tls_context_flags {
 	 * to be atomic.
 	 */
 	TLS_TX_SYNC_SCHED = 1,
-	/* tls_dev_del was called for the RX side, device state was released,
-	 * but tls_ctx->netdev might still be kept, because TX-side driver
-	 * resources might not be released yet. Used to prevent the second
-	 * tls_dev_del call in tls_device_down if it happens simultaneously.
+	/* No RX device state is installed. Set at context creation and after
+	 * RX tls_dev_del, cleared after a successful RX tls_dev_add. Prevents
+	 * deleting absent or already released RX state while ctx->netdev may
+	 * still be kept for TX.
 	 */
 	TLS_RX_DEV_CLOSED = 2,
 	/* tls_device_down() replaced tls_validate_xmit_skb(). */
net/tls/tls_device.c +5 -4
index 3473299b16de..3aac6a45188b 100644
@@ -1282,6 +1282,7 @@ int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
 		goto free_sw_resources;
 
 	tls_device_attach(ctx, sk, netdev);
+	clear_bit(TLS_RX_DEV_CLOSED, &ctx->flags);
 	up_read(&device_offload_lock);
 
 	dev_put(netdev);
@@ -1314,12 +1315,11 @@ 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_CLOSED, &tls_ctx->flags);
 
 	if (tls_ctx->tx_conf != TLS_HW) {
 		dev_put(netdev);
 		rcu_assign_pointer(tls_ctx->netdev, NULL);
-	} else {
-		set_bit(TLS_RX_DEV_CLOSED, &tls_ctx->flags);
 	}
 out:
 	up_read(&device_offload_lock);
@@ -1380,10 +1380,11 @@ static int tls_device_down(struct net_device *netdev)
 		if (test_bit(TLS_TX_DEV_DOWN, &ctx->flags))
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_TX);
-		if (ctx->rx_conf == TLS_HW &&
-		    !test_bit(TLS_RX_DEV_CLOSED, &ctx->flags))
+		if (!test_bit(TLS_RX_DEV_CLOSED, &ctx->flags)) {
 			netdev->tlsdev_ops->tls_dev_del(netdev, ctx,
 							TLS_OFFLOAD_CTX_DIR_RX);
+			set_bit(TLS_RX_DEV_CLOSED, &ctx->flags);
+		}
 
 		dev_put(netdev);
 
net/tls/tls_main.c +1 -0
index fa71c75c2861..54d851df0c58 100644
@@ -935,6 +935,7 @@ struct tls_context *tls_ctx_create(struct sock *sk)
 		return NULL;
 
 	mutex_init(&ctx->tx_lock);
+	set_bit(TLS_RX_DEV_CLOSED, &ctx->flags);
 	ctx->sk_proto = READ_ONCE(sk->sk_prot);
 	ctx->sk = sk;
 	/* Release semantic of rcu_assign_pointer() ensures that
Signature · 1 line
2.50.1