All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH net-next 0/2] Fix race between datagram socket address change and rehash
@ 2024-12-04 22:12 Stefano Brivio
  2024-12-04 22:12 ` [PATCH net-next 1/2] datagram: Rehash sockets only if local address changed for their family Stefano Brivio
  2024-12-04 22:12 ` [PATCH net-next 2/2] datagram, udp: Set local address and rehash socket atomically against lookup Stefano Brivio
  0 siblings, 2 replies; 18+ messages in thread
From: Stefano Brivio @ 2024-12-04 22:12 UTC (permalink / raw)
  To: Willem de Bruijn
  Cc: Eric Dumazet, netdev, Kuniyuki Iwashima, Mike Manning,
	David Gibson, Paul Holzinger, Philo Lu, Cambda Zhu, Fred Chen,
	Yubing Qiu

Patch 2/2 fixes a race condition in the lookup of datagram sockets
between address change (triggered by connect()) and rehashing. The
issue occurs regardless of the type of hash, that is, it happens with
updates to the secondary hash as well as to the newly introduced
four-tuple hash.

Patch 1/2 is a small optimisation to simplify 2/2.

This is essentially a rebase onto current net-next of the RFC I
originally posted against 'net', before the 6.13 merge window,
at:

  https://lore.kernel.org/netdev/20241114215414.3357873-1-sbrivio@redhat.com/

with a couple of minor changes described in the single patch messages.

The rebase is not trivial as four-tuple hashes were introduced
meanwhile, with commits 1b29a730ef8b ("ipv6/udp: Add 4-tuple hash for
connected socket"), 78c91ae2c6de ("ipv4/udp: Add 4-tuple hash for
connected socket"), and their dependencies, but the race condition,
described in detail in the commit message for 1/2, is exactly the same
as before.

Stefano Brivio (2):
  datagram: Rehash sockets only if local address changed for their
    family
  datagram, udp: Set local address and rehash socket atomically against
    lookup

 include/net/inet_hashtables.h | 13 ++++++
 include/net/sock.h            |  2 +-
 include/net/udp.h             |  3 +-
 net/core/sock.c               | 12 ++++-
 net/ipv4/datagram.c           |  7 +--
 net/ipv4/inet_hashtables.c    | 13 ------
 net/ipv4/udp.c                | 84 +++++++++++++++++++++++------------
 net/ipv4/udp_impl.h           |  2 +-
 net/ipv4/udplite.c            |  2 +-
 net/ipv6/datagram.c           | 30 +++++++++----
 net/ipv6/udp.c                | 31 +++++++------
 net/ipv6/udp_impl.h           |  2 +-
 net/ipv6/udplite.c            |  2 +-
 13 files changed, 130 insertions(+), 73 deletions(-)

-- 
2.40.1


^ permalink raw reply	[flat|nested] 18+ messages in thread
* Re: [PATCH net-next 2/2] datagram, udp: Set local address and rehash socket atomically against lookup
@ 2024-12-10 18:34 kernel test robot
  0 siblings, 0 replies; 18+ messages in thread
From: kernel test robot @ 2024-12-10 18:34 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp, Dan Carpenter

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20241204221254.3537932-3-sbrivio@redhat.com>
References: <20241204221254.3537932-3-sbrivio@redhat.com>
TO: Stefano Brivio <sbrivio@redhat.com>
TO: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
CC: Eric Dumazet <edumazet@google.com>
CC: netdev@vger.kernel.org
CC: Kuniyuki Iwashima <kuniyu@amazon.com>
CC: Mike Manning <mvrmanning@gmail.com>
CC: David Gibson <david@gibson.dropbear.id.au>
CC: Paul Holzinger <pholzing@redhat.com>
CC: Philo Lu <lulie@linux.alibaba.com>
CC: Cambda Zhu <cambda@linux.alibaba.com>
CC: Fred Chen <fred.cc@alibaba-inc.com>
CC: Yubing Qiu <yubing.qiuyubing@alibaba-inc.com>

Hi Stefano,

kernel test robot noticed the following build warnings:

[auto build test WARNING on net-next/main]

url:    https://github.com/intel-lab-lkp/linux/commits/Stefano-Brivio/datagram-Rehash-sockets-only-if-local-address-changed-for-their-family/20241205-062005
base:   net-next/main
patch link:    https://lore.kernel.org/r/20241204221254.3537932-3-sbrivio%40redhat.com
patch subject: [PATCH net-next 2/2] datagram, udp: Set local address and rehash socket atomically against lookup
:::::: branch date: 6 days ago
:::::: commit date: 6 days ago
config: sparc64-randconfig-r072-20241210 (https://download.01.org/0day-ci/archive/20241211/202412110238.YqquWDMw-lkp@intel.com/config)
compiler: sparc64-linux-gcc (GCC) 14.2.0

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Reported-by: Dan Carpenter <error27@gmail.com>
| Closes: https://lore.kernel.org/r/202412110238.YqquWDMw-lkp@intel.com/

New smatch warnings:
net/ipv4/udp.c:2248 udp_lib_set_rcv_saddr() error: uninitialized symbol 'hslot'.

Old smatch warnings:
net/ipv4/udp.c:2488 __udp4_lib_mcast_deliver() warn: potential spectre issue 'udptable->hash2' [r]
net/ipv4/udp.c:2728 __udp4_lib_mcast_demux_lookup() warn: potential spectre issue 'udptable->hash' [r]

vim +/hslot +2248 net/ipv4/udp.c

645ca708f936b2f Eric Dumazet      2008-10-29  2181  
ab240d39ed91dae Stefano Brivio    2024-12-04  2182  /**
ab240d39ed91dae Stefano Brivio    2024-12-04  2183   * udp_lib_set_rcv_saddr() - Set local address and rehash socket atomically
ab240d39ed91dae Stefano Brivio    2024-12-04  2184   * @sk:		Socket changing local address
ab240d39ed91dae Stefano Brivio    2024-12-04  2185   * @addr:	New address, __be32 * or struct in6_addr * depending on family
ab240d39ed91dae Stefano Brivio    2024-12-04  2186   * @hash:	New secondary hash (local port and address) for socket
ab240d39ed91dae Stefano Brivio    2024-12-04  2187   * @hash4:	New 4-tuple hash (local/remote port and address) for socket
ab240d39ed91dae Stefano Brivio    2024-12-04  2188   *
ab240d39ed91dae Stefano Brivio    2024-12-04  2189   * Set local address for socket and rehash it while holding a spinlock on the
ab240d39ed91dae Stefano Brivio    2024-12-04  2190   * primary hash chain (port only). This needs to be atomic to avoid that a
ab240d39ed91dae Stefano Brivio    2024-12-04  2191   * concurrent lookup misses a socket while it's being connected or disconnected.
719f835853a92f6 Eric Dumazet      2010-09-08  2192   */
ab240d39ed91dae Stefano Brivio    2024-12-04  2193  void udp_lib_set_rcv_saddr(struct sock *sk, void *addr, u16 hash, u16 hash4)
719f835853a92f6 Eric Dumazet      2010-09-08  2194  {
ab240d39ed91dae Stefano Brivio    2024-12-04  2195  	struct udp_hslot *hslot;
ab240d39ed91dae Stefano Brivio    2024-12-04  2196  
719f835853a92f6 Eric Dumazet      2010-09-08  2197  	if (sk_hashed(sk)) {
67fb43308f4b354 Kuniyuki Iwashima 2022-11-14  2198  		struct udp_table *udptable = udp_get_table_prot(sk);
ab240d39ed91dae Stefano Brivio    2024-12-04  2199  		struct udp_hslot *hslot2, *nhslot2;
719f835853a92f6 Eric Dumazet      2010-09-08  2200  
719f835853a92f6 Eric Dumazet      2010-09-08  2201  		hslot2 = udp_hashslot2(udptable, udp_sk(sk)->udp_portaddr_hash);
ab240d39ed91dae Stefano Brivio    2024-12-04  2202  		nhslot2 = udp_hashslot2(udptable, hash);
e32ea7e747271a0 Craig Gallek      2016-01-04  2203  
719f835853a92f6 Eric Dumazet      2010-09-08  2204  		hslot = udp_hashslot(udptable, sock_net(sk),
719f835853a92f6 Eric Dumazet      2010-09-08  2205  				     udp_sk(sk)->udp_port_hash);
ab240d39ed91dae Stefano Brivio    2024-12-04  2206  
719f835853a92f6 Eric Dumazet      2010-09-08  2207  		spin_lock_bh(&hslot->lock);
ab240d39ed91dae Stefano Brivio    2024-12-04  2208  
ab240d39ed91dae Stefano Brivio    2024-12-04  2209  		udp_sk(sk)->udp_portaddr_hash = hash;
ab240d39ed91dae Stefano Brivio    2024-12-04  2210  
ab240d39ed91dae Stefano Brivio    2024-12-04  2211  		if (hslot2 != nhslot2 ||
ab240d39ed91dae Stefano Brivio    2024-12-04  2212  		    rcu_access_pointer(sk->sk_reuseport_cb)) {
e32ea7e747271a0 Craig Gallek      2016-01-04  2213  			if (rcu_access_pointer(sk->sk_reuseport_cb))
e32ea7e747271a0 Craig Gallek      2016-01-04  2214  				reuseport_detach_sock(sk);
719f835853a92f6 Eric Dumazet      2010-09-08  2215  
e32ea7e747271a0 Craig Gallek      2016-01-04  2216  			if (hslot2 != nhslot2) {
719f835853a92f6 Eric Dumazet      2010-09-08  2217  				spin_lock(&hslot2->lock);
ca065d0cf80fa54 Eric Dumazet      2016-04-01  2218  				hlist_del_init_rcu(&udp_sk(sk)->udp_portaddr_node);
719f835853a92f6 Eric Dumazet      2010-09-08  2219  				hslot2->count--;
719f835853a92f6 Eric Dumazet      2010-09-08  2220  				spin_unlock(&hslot2->lock);
719f835853a92f6 Eric Dumazet      2010-09-08  2221  
719f835853a92f6 Eric Dumazet      2010-09-08  2222  				spin_lock(&nhslot2->lock);
ca065d0cf80fa54 Eric Dumazet      2016-04-01  2223  				hlist_add_head_rcu(&udp_sk(sk)->udp_portaddr_node,
719f835853a92f6 Eric Dumazet      2010-09-08  2224  							 &nhslot2->head);
719f835853a92f6 Eric Dumazet      2010-09-08  2225  				nhslot2->count++;
719f835853a92f6 Eric Dumazet      2010-09-08  2226  				spin_unlock(&nhslot2->lock);
e32ea7e747271a0 Craig Gallek      2016-01-04  2227  			}
719f835853a92f6 Eric Dumazet      2010-09-08  2228  
78c91ae2c6deb5d Philo Lu          2024-11-14  2229  			if (udp_hashed4(sk)) {
ab240d39ed91dae Stefano Brivio    2024-12-04  2230  				udp_rehash4(udptable, sk, hash4);
78c91ae2c6deb5d Philo Lu          2024-11-14  2231  
78c91ae2c6deb5d Philo Lu          2024-11-14  2232  				if (hslot2 != nhslot2) {
78c91ae2c6deb5d Philo Lu          2024-11-14  2233  					spin_lock(&hslot2->lock);
78c91ae2c6deb5d Philo Lu          2024-11-14  2234  					udp_hash4_dec(hslot2);
78c91ae2c6deb5d Philo Lu          2024-11-14  2235  					spin_unlock(&hslot2->lock);
78c91ae2c6deb5d Philo Lu          2024-11-14  2236  
78c91ae2c6deb5d Philo Lu          2024-11-14  2237  					spin_lock(&nhslot2->lock);
78c91ae2c6deb5d Philo Lu          2024-11-14  2238  					udp_hash4_inc(nhslot2);
78c91ae2c6deb5d Philo Lu          2024-11-14  2239  					spin_unlock(&nhslot2->lock);
78c91ae2c6deb5d Philo Lu          2024-11-14  2240  				}
78c91ae2c6deb5d Philo Lu          2024-11-14  2241  			}
719f835853a92f6 Eric Dumazet      2010-09-08  2242  		}
719f835853a92f6 Eric Dumazet      2010-09-08  2243  	}
ab240d39ed91dae Stefano Brivio    2024-12-04  2244  
ab240d39ed91dae Stefano Brivio    2024-12-04  2245  	inet_update_saddr(sk, addr, sk->sk_family);
ab240d39ed91dae Stefano Brivio    2024-12-04  2246  
ab240d39ed91dae Stefano Brivio    2024-12-04  2247  	if (sk_hashed(sk))
ab240d39ed91dae Stefano Brivio    2024-12-04 @2248  		spin_unlock_bh(&hslot->lock);
719f835853a92f6 Eric Dumazet      2010-09-08  2249  }
ab240d39ed91dae Stefano Brivio    2024-12-04  2250  EXPORT_SYMBOL(udp_lib_set_rcv_saddr);
719f835853a92f6 Eric Dumazet      2010-09-08  2251  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2024-12-18 16:21 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-04 22:12 [PATCH net-next 0/2] Fix race between datagram socket address change and rehash Stefano Brivio
2024-12-04 22:12 ` [PATCH net-next 1/2] datagram: Rehash sockets only if local address changed for their family Stefano Brivio
2024-12-04 22:12 ` [PATCH net-next 2/2] datagram, udp: Set local address and rehash socket atomically against lookup Stefano Brivio
2024-12-05  9:30   ` Paolo Abeni
2024-12-05 15:58     ` Stefano Brivio
2024-12-05 16:53       ` Paolo Abeni
2024-12-06 10:50         ` Stefano Brivio
2024-12-06 12:36           ` Paolo Abeni
2024-12-06 13:35             ` Stefano Brivio
2024-12-06 15:10               ` Paolo Abeni
2024-12-18 16:21               ` Stefano Brivio
2024-12-05 16:35   ` Eric Dumazet
2024-12-05 22:32     ` David Gibson
2024-12-05 22:52       ` Eric Dumazet
2024-12-06  2:16         ` David Gibson
2024-12-06  9:04           ` Eric Dumazet
2024-12-09  2:20             ` David Gibson
  -- strict thread matches above, loose matches on Subject: below --
2024-12-10 18:34 kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.