]> www.infradead.org Git - users/hch/misc.git/commitdiff
tls: Use __sk_dst_get() and dst_dev_rcu() in get_netdev_for_sock().
authorKuniyuki Iwashima <kuniyu@google.com>
Tue, 16 Sep 2025 21:47:23 +0000 (21:47 +0000)
committerJakub Kicinski <kuba@kernel.org>
Thu, 18 Sep 2025 01:10:22 +0000 (18:10 -0700)
get_netdev_for_sock() is called during setsockopt(),
so not under RCU.

Using sk_dst_get(sk)->dev could trigger UAF.

Let's use __sk_dst_get() and dst_dev_rcu().

Note that the only ->ndo_sk_get_lower_dev() user is
bond_sk_get_lower_dev(), which uses RCU.

Fixes: e8f69799810c ("net/tls: Add generic NIC offload infrastructure")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Sabrina Dubroca <sd@queasysnail.net>
Link: https://patch.msgid.link/20250916214758.650211-6-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/tls/tls_device.c

index f672a62a9a52f6ea7a0f5c500acdf9538f08d297..a82fdcf199690fae5dc63cb100fa5e43c04271dd 100644 (file)
@@ -123,17 +123,19 @@ static void tls_device_queue_ctx_destruction(struct tls_context *ctx)
 /* We assume that the socket is already connected */
 static struct net_device *get_netdev_for_sock(struct sock *sk)
 {
-       struct dst_entry *dst = sk_dst_get(sk);
-       struct net_device *netdev = NULL;
+       struct net_device *dev, *lowest_dev = NULL;
+       struct dst_entry *dst;
 
-       if (likely(dst)) {
-               netdev = netdev_sk_get_lowest_dev(dst->dev, sk);
-               dev_hold(netdev);
+       rcu_read_lock();
+       dst = __sk_dst_get(sk);
+       dev = dst ? dst_dev_rcu(dst) : NULL;
+       if (likely(dev)) {
+               lowest_dev = netdev_sk_get_lowest_dev(dev, sk);
+               dev_hold(lowest_dev);
        }
+       rcu_read_unlock();
 
-       dst_release(dst);
-
-       return netdev;
+       return lowest_dev;
 }
 
 static void destroy_record(struct tls_record_info *record)