tls: fix socket use-after-free during device-down
Subsystems (4)
PATCH v6 1/2 tls: fix socket use-after-free during device-down
tls_device_down() pins each TLS context, then drops tls_device_lock before switching ctx->sk's TX validator. The socket destructor can run between these steps: the context survives, but __sk_destruct() frees the socket.
Switch the validator while selecting contexts from tls_device_list under tls_device_lock. The destructor uses the same lock for its context put and list removal, so ctx->sk remains allocated through the switch.
Checking sk_refcnt would miss sockets whose queued SKBs still hold sk_wmem_alloc and require fallback after sk_refcnt reaches zero.
Requires commit f08d8c1bb97c ("net/tls: Fix race in TLS device down flow"), which moved the destructor's context put under tls_device_lock. Include this prerequisite in older backports.
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).
--- net/tls/tls_device.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-)
net/tls/tls_device.c +8 -5
@@ -1345,6 +1345,14 @@ 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. +* +* The context reference pins ctx; tls_device_lock keeps ctx->sk +* allocated until the validator has been switched. +*/ +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 +1360,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
PATCH v6 2/2 tls: restrict device-down TX fallback to hardware TX sockets
tls_device_down() switches every offloaded TLS context associated with the device to the TX software fallback, including hardware-RX sockets without TX offload (RX-only, or software TLS TX). The fallback expects a hardware TX context 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
Device-down teardown cannot key on tx_conf/rx_conf: setup publishes them after releasing device_offload_lock, and they share a byte written without that lock. Device-down can therefore observe configuration fields that do not yet reflect a successful tls_dev_add() and skip the required tls_dev_del(), leaking driver state. Key it on installed driver state instead.
TX: switch the validator only if it is tls_validate_xmit_skb(), record the switch in TLS_TX_DEV_DOWN, and call TX tls_dev_del only when that bit is set. tls_set_device_offload() now publishes the validator under device_offload_lock; otherwise device-down could run between tls_device_attach() and the store, skip tls_dev_del (leaking driver state), and setup would then install the hardware validator on a torn-down attachment. Contexts never return to the active list, so the bit needs no reset.
RX: TLS_RX_DEV_CLOSED now means "no RX driver state installed". Set it at context creation and after every RX tls_dev_del, and clear it after a successful RX tls_dev_add under device_offload_lock. This avoids deleting absent RX state and reading tx_conf/rx_conf while their shared byte is updated.
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.
c55dcdd435aa made the fallback switch unconditional; 4799ac81e52a keyed teardown on tx_conf/rx_conf.
--- include/net/tls.h | 10 ++++++---- net/tls/tls_device.c | 20 +++++++++++++------- net/tls/tls_main.c | 1 + 3 files changed, 20 insertions(+), 11 deletions(-)
include/net/tls.h +6 -4
@@ -188,12 +188,14 @@ 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 , +/* Records the TX fallback switch for device-down's cleanup loop. */ + TLS_TX_DEV_DOWN = 3, }; struct cipher_context {
net/tls/tls_device.c +13 -7
@@ -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 ;
@@ -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);
@@ -1351,7 +1351,12 @@ static int tls_device_down(struct net_device *netdev)* The context reference pins ctx; tls_device_lock keeps ctx->sk * allocated until the validator has been switched. */ -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); }
@@ -1375,13 +1380,14 @@ 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 && - !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
@@ -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