Commit dbd9755a authored by Eric Dumazet's avatar Eric Dumazet Committed by Geliang Tang
Browse files

inet: lockless getsockopt(IP_OPTIONS)

mainline inclusion
from mainline-v6.7-rc1
commit a4725d0d893599253a4bb283fdabdd4a66d9451d
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I9VYQ9
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=a4725d0d893599253a4bb283fdabdd4a66d9451d



--------------------------------

inet->inet_opt being RCU protected, we can use RCU instead
of locking the socket.

Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Reviewed-by: default avatarDavid Ahern <dsahern@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Reviewed-by: default avatarJackie Liu <liuyun01@kylinos.cn>
Signed-off-by: default avatarGeliang Tang <tanggeliang@kylinos.cn>
parent 79a0ead7
Loading
Loading
Loading
Loading
+10 −10
Original line number Diff line number Diff line
@@ -1600,27 +1600,20 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
	case IP_TOS:
		val = READ_ONCE(inet->tos);
		goto copyval;
	}

	if (needs_rtnl)
		rtnl_lock();
	sockopt_lock_sock(sk);

	switch (optname) {
	case IP_OPTIONS:
	{
		unsigned char optbuf[sizeof(struct ip_options)+40];
		struct ip_options *opt = (struct ip_options *)optbuf;
		struct ip_options_rcu *inet_opt;

		inet_opt = rcu_dereference_protected(inet->inet_opt,
						     lockdep_sock_is_held(sk));
		rcu_read_lock();
		inet_opt = rcu_dereference(inet->inet_opt);
		opt->optlen = 0;
		if (inet_opt)
			memcpy(optbuf, &inet_opt->opt,
			       sizeof(struct ip_options) +
			       inet_opt->opt.optlen);
		sockopt_release_sock(sk);
		rcu_read_unlock();

		if (opt->optlen == 0) {
			len = 0;
@@ -1636,6 +1629,13 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
			return -EFAULT;
		return 0;
	}
	}

	if (needs_rtnl)
		rtnl_lock();
	sockopt_lock_sock(sk);

	switch (optname) {
	case IP_MTU:
	{
		struct dst_entry *dst;