Re: [PATCH net-next 2/2] udp: convert udp_lib_getsockopt to sockopt_t
From: Stanislav Fomichev
Date: Fri Jun 12 2026 - 22:10:38 EST
On 06/12, Breno Leitao wrote:
> In preparation for converting the proto-layer getsockopt callbacks to the
> sockopt_t interface, switch udp_lib_getsockopt() to take a sockopt_t.
>
> The thin udp_getsockopt()/udpv6_getsockopt() wrappers keep their __user
> signature for now: they build a user-backed sockopt_t with
> sockopt_init_user(), call the helper, and write the returned length back
> to optlen. The helper uses copy_to_iter() instead of copy_to_user().
> No functional change.
>
> Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
> ---
> include/net/udp.h | 2 +-
> net/ipv4/udp.c | 40 ++++++++++++++++++++++++++--------------
> net/ipv6/udp.c | 17 ++++++++++++++---
> 3 files changed, 41 insertions(+), 18 deletions(-)
>
> diff --git a/include/net/udp.h b/include/net/udp.h
> index 8262e2b215b4e..1fee17274745f 100644
> --- a/include/net/udp.h
> +++ b/include/net/udp.h
> @@ -430,7 +430,7 @@ struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
> netdev_features_t features,
> bool is_ipv6);
> int udp_lib_getsockopt(struct sock *sk, int level, int optname,
> - char __user *optval, int __user *optlen);
> + sockopt_t *opt);
> int udp_lib_setsockopt(struct sock *sk, int level, int optname,
> sockptr_t optval, unsigned int optlen,
> int (*push_pending_frames)(struct sock *));
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 70f6cbd4ef73b..0691f74db2c11 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -76,6 +76,7 @@
>
> #include <linux/bpf-cgroup.h>
> #include <linux/uaccess.h>
> +#include <linux/uio.h>
> #include <asm/ioctls.h>
> #include <linux/memblock.h>
> #include <linux/highmem.h>
> @@ -2995,18 +2996,12 @@ static int udp_setsockopt(struct sock *sk, int level, int optname, sockptr_t opt
> }
>
> int udp_lib_getsockopt(struct sock *sk, int level, int optname,
> - char __user *optval, int __user *optlen)
> + sockopt_t *opt)
> {
> struct udp_sock *up = udp_sk(sk);
> int val, len;
>
> - if (get_user(len, optlen))
> - return -EFAULT;
[..]
> - if (len < 0)
> - return -EINVAL;
I see this part now in sockopt_init_user, but you mention that it's a
transitional helper. When we drop it, will we loose this <0 check?
Maybe keep `if ((int)opt->optlen < 0))` here for backwards
compatibility?