Re: [PATCH net-next v3 0/4] net: move .getsockopt away from __user buffers

From: Stanislav Fomichev

Date: Wed Apr 08 2026 - 13:07:25 EST


On 04/08, Breno Leitao wrote:
> Currently, the .getsockopt callback requires __user pointers:
>
> int (*getsockopt)(struct socket *sock, int level,
> int optname, char __user *optval, int __user *optlen);
>
> This prevents kernel callers (io_uring, BPF) from using getsockopt on
> levels other than SOL_SOCKET, since they pass kernel pointers.
>
> Following Linus' suggestion [0], this series introduces sockopt_t, a
> type-safe wrapper around iov_iter, and a getsockopt_iter callback that
> works with both user and kernel buffers. AF_PACKET and CAN raw are
> converted as initial users, with selftests covering the trickiest
> conversion patterns.
>
> [0] https://lore.kernel.org/all/CAHk-=whmzrO-BMU=uSVXbuoLi-3tJsO=0kHj1BCPBE3F2kVhTA@xxxxxxxxxxxxxx/
>
> Updates from v2 to v3:
>
> * Use two iov in sockopt_t instead of a single one:
> a) .iter_in that is populated by the caller and will be read-only in
> the protocols callback.
>
> b) .iter_out will be populated by the protocol and it will be sent
> back to the caller.
>
> - This will avoid changing the protocol reset and changing the data
> source at the callback, making the driver callback implementation
> and converstion saner.
>
> * created sockptr_to_sockopt() to convert sockptr to sockopt, making the
> call to getsockopt_iter straight-forward
>
> Link: https://lore.kernel.org/all/CAHk-=whmzrO-BMU=uSVXbuoLi-3tJsO=0kHj1BCPBE3F2kVhTA@xxxxxxxxxxxxxx/ [0]
> ---
> Changes in v3:
> - Create Two iov in sockopt_t instead of a single one (Stanislav Fomichev)
> - Implement the sockptr_to_sockopt() helper (Stanislav Fomichev)
> - Link to v2: https://patch.msgid.link/20260401-getsockopt-v2-0-611df6771aff@xxxxxxxxxx
>
> Changes in v2:
> - Restore optlen even on error path (getsockopt_iter fails)
> - Move af_packet.c and can instead of netlink (given these are the most
> complicate ones).
> - Link to v1: https://patch.msgid.link/20260130-getsockopt-v1-0-9154fcff6f95@xxxxxxxxxx

LGTM! Not sure what's your plan for the selftest? You wanna keep it
outside or maybe repost v4 with it?

Acked-by: Stanislav Fomichev <sdf@xxxxxxxxxxx>

I'm also not sure your unconditional 'copy-optlen-back' will work for every
proto, but I think we can put something into sockopt_t to make it avoid
the copy if needed in the future.