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") Cc: stable@vger.kernel.org Signed-off-by: Rishikesh Jethwani --- include/net/tls.h | 2 ++ net/tls/tls_device.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/include/net/tls.h b/include/net/tls.h index 0b001b8ab1c3..f9540910b2e3 100644 --- a/include/net/tls.h +++ b/include/net/tls.h @@ -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 { diff --git a/net/tls/tls_device.c b/net/tls/tls_device.c index 3ea764bdc949..3473299b16de 100644 --- a/net/tls/tls_device.c +++ b/net/tls/tls_device.c @@ -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 && -- 2.50.1