tls: keep TX and RX hardware offload on the same device

After TX is offloaded on device A, a route change, or a change in the
usable-slave set of an 802.3ad or balance-xor (layer3+4) bond, can make
RX setup program device B. tls_device_attach() sees the existing TLS
destructor and keeps ctx->netdev pointing at A. The RX driver state on B
is then untracked: tls_dev_resync() and tls_dev_del() are invoked on A
for a context it never created, and neither socket close nor
NETDEV_DOWN on B calls tls_dev_del() for B, so the driver context leaks.
The reverse setup order has the same problem.

Check the shared device binding under device_offload_lock before each
tls_dev_add(). Permit the first attachment and a second direction on the
same device. Reject a different device, including when the old binding
was cleared by NETDEV_DOWN: the TLS destructor remains installed and
tls_device_attach() cannot establish a new binding. Setup then falls
back to software for the requested direction.

The RX check precedes allocation and software setup. TX uses its existing
resource unwind path without calling the driver. Document the shared
binding and clarify that fallback is per direction.

This issue was discovered during code review and subsequently
reproduced on hardware using mlx5 and TLS 1.2 AES-GCM-128.

Changing the route between TX and RX setup installed the second
direction on a different device. Driver traces showed successful adds
on both devices, but both deletes at socket close targeted the first
device, leaving an orphaned context on the second. Both setup orders
reproduced the leak, even though all current core TLS context counters
returned to zero. No crash was observed.

With the fix, the same tests correctly fall back to software for the
second direction and leave no orphaned hardware context.

Fixes: 4799ac81e5 ("tls: Add rx inline crypto offload")
Signed-off-by: Rishikesh Jethwani <rjethwani@purestorage.com>
Signed-off-by: Dave Miller <davem@davem-thinkpadp1gen8.ibmlond.csb>
This commit is contained in:
Rishikesh Jethwani 2026-10-20 15:44:59 -06:00 committed by Dave Miller
parent 557d801386
commit 1c8a700752
2 changed files with 34 additions and 6 deletions

View File

@ -71,12 +71,16 @@ During driver initialization device sets the ``NETIF_F_HW_TLS_RX`` and
pointer in the :c:member:`tlsdev_ops` member of the
:c:type:`struct net_device <net_device>`.
When TLS cryptographic connection state is installed on a ``ktls`` socket
(note that it is done twice, once for RX and once for TX direction,
and the two are completely independent), the kernel checks if the underlying
network device is offload-capable and attempts the offload. In case offload
fails the connection is handled entirely in software using the same mechanism
as if the offload was never tried.
TLS cryptographic connection state is installed separately for RX and TX
on a ``ktls`` socket. For each direction, the kernel checks if the underlying
network device is offload-capable and attempts the offload. If offload fails,
that direction is handled in software as if offload had never been tried.
Both hardware directions share one device binding. Once one direction is
offloaded, the other direction must use the same device. If a route change
selects a different device, or ``NETDEV_DOWN`` has cleared the binding,
hardware offload of the other direction is declined and it uses software
instead.
Offload request is performed via the :c:member:`tls_dev_add` callback of
:c:type:`struct tlsdev_ops <tlsdev_ops>`:

View File

@ -1031,6 +1031,20 @@ int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx)
return tls_device_reencrypt(sk, tls_ctx);
}
/* Both directions share ctx->netdev. A second direction must use that
* device; after NETDEV_DOWN clears it, no new direction can be offloaded.
*/
static bool tls_device_can_attach(struct tls_context *ctx, struct sock *sk,
struct net_device *netdev)
{
struct net_device *ctx_netdev;
ctx_netdev = rcu_dereference_protected(ctx->netdev,
lockdep_is_held(&device_offload_lock));
return sk->sk_destruct != tls_device_sk_destruct ||
ctx_netdev == netdev;
}
static void tls_device_attach(struct tls_context *ctx, struct sock *sk,
struct net_device *netdev)
{
@ -1166,6 +1180,11 @@ int tls_set_device_offload(struct sock *sk)
goto release_lock;
}
if (!tls_device_can_attach(ctx, sk, netdev)) {
rc = -EOPNOTSUPP;
goto release_lock;
}
ctx->priv_ctx_tx = offload_ctx;
rc = netdev->tlsdev_ops->tls_dev_add(netdev, sk, TLS_OFFLOAD_CTX_DIR_TX,
&ctx->crypto_send.info,
@ -1236,6 +1255,11 @@ int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
goto release_lock;
}
if (!tls_device_can_attach(ctx, sk, netdev)) {
rc = -EOPNOTSUPP;
goto release_lock;
}
context = kzalloc_obj(*context);
if (!context) {
rc = -ENOMEM;