Skip to content

Commit 956c57d

Browse files
q2vengregkh
authored andcommitted
smc: Use __sk_dst_get() and dst_dev_rcu() in in smc_clc_prfx_set().
[ Upstream commit 935d783 ] smc_clc_prfx_set() is called during connect() and not under RCU nor RTNL. Using sk_dst_get(sk)->dev could trigger UAF. Let's use __sk_dst_get() and dev_dst_rcu() under rcu_read_lock() after kernel_getsockname(). Note that the returned value of smc_clc_prfx_set() is not used in the caller. While at it, we change the 1st arg of smc_clc_prfx_set[46]_rcu() not to touch dst there. Fixes: a046d57 ("smc: CLC handshake (incl. preparation steps)") Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://patch.msgid.link/20250916214758.650211-3-kuniyu@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c0fcd72 commit 956c57d

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

net/smc/smc_clc.c

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -509,10 +509,10 @@ static bool smc_clc_msg_hdr_valid(struct smc_clc_msg_hdr *clcm, bool check_trl)
509509
}
510510

511511
/* find ipv4 addr on device and get the prefix len, fill CLC proposal msg */
512-
static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
512+
static int smc_clc_prfx_set4_rcu(struct net_device *dev, __be32 ipv4,
513513
struct smc_clc_msg_proposal_prefix *prop)
514514
{
515-
struct in_device *in_dev = __in_dev_get_rcu(dst->dev);
515+
struct in_device *in_dev = __in_dev_get_rcu(dev);
516516
const struct in_ifaddr *ifa;
517517

518518
if (!in_dev)
@@ -530,12 +530,12 @@ static int smc_clc_prfx_set4_rcu(struct dst_entry *dst, __be32 ipv4,
530530
}
531531

532532
/* fill CLC proposal msg with ipv6 prefixes from device */
533-
static int smc_clc_prfx_set6_rcu(struct dst_entry *dst,
533+
static int smc_clc_prfx_set6_rcu(struct net_device *dev,
534534
struct smc_clc_msg_proposal_prefix *prop,
535535
struct smc_clc_ipv6_prefix *ipv6_prfx)
536536
{
537537
#if IS_ENABLED(CONFIG_IPV6)
538-
struct inet6_dev *in6_dev = __in6_dev_get(dst->dev);
538+
struct inet6_dev *in6_dev = __in6_dev_get(dev);
539539
struct inet6_ifaddr *ifa;
540540
int cnt = 0;
541541

@@ -564,41 +564,44 @@ static int smc_clc_prfx_set(struct socket *clcsock,
564564
struct smc_clc_msg_proposal_prefix *prop,
565565
struct smc_clc_ipv6_prefix *ipv6_prfx)
566566
{
567-
struct dst_entry *dst = sk_dst_get(clcsock->sk);
568567
struct sockaddr_storage addrs;
569568
struct sockaddr_in6 *addr6;
570569
struct sockaddr_in *addr;
570+
struct net_device *dev;
571+
struct dst_entry *dst;
571572
int rc = -ENOENT;
572573

573-
if (!dst) {
574-
rc = -ENOTCONN;
575-
goto out;
576-
}
577-
if (!dst->dev) {
578-
rc = -ENODEV;
579-
goto out_rel;
580-
}
581574
/* get address to which the internal TCP socket is bound */
582575
if (kernel_getsockname(clcsock, (struct sockaddr *)&addrs) < 0)
583-
goto out_rel;
576+
goto out;
577+
584578
/* analyze IP specific data of net_device belonging to TCP socket */
585579
addr6 = (struct sockaddr_in6 *)&addrs;
580+
586581
rcu_read_lock();
582+
583+
dst = __sk_dst_get(clcsock->sk);
584+
dev = dst ? dst_dev_rcu(dst) : NULL;
585+
if (!dev) {
586+
rc = -ENODEV;
587+
goto out_unlock;
588+
}
589+
587590
if (addrs.ss_family == PF_INET) {
588591
/* IPv4 */
589592
addr = (struct sockaddr_in *)&addrs;
590-
rc = smc_clc_prfx_set4_rcu(dst, addr->sin_addr.s_addr, prop);
593+
rc = smc_clc_prfx_set4_rcu(dev, addr->sin_addr.s_addr, prop);
591594
} else if (ipv6_addr_v4mapped(&addr6->sin6_addr)) {
592595
/* mapped IPv4 address - peer is IPv4 only */
593-
rc = smc_clc_prfx_set4_rcu(dst, addr6->sin6_addr.s6_addr32[3],
596+
rc = smc_clc_prfx_set4_rcu(dev, addr6->sin6_addr.s6_addr32[3],
594597
prop);
595598
} else {
596599
/* IPv6 */
597-
rc = smc_clc_prfx_set6_rcu(dst, prop, ipv6_prfx);
600+
rc = smc_clc_prfx_set6_rcu(dev, prop, ipv6_prfx);
598601
}
602+
603+
out_unlock:
599604
rcu_read_unlock();
600-
out_rel:
601-
dst_release(dst);
602605
out:
603606
return rc;
604607
}

0 commit comments

Comments
 (0)