Merge branch 'net-convert-af_netlink-and-af_vsock-to-getsockopt_iter-api'

Breno Leitao says:

====================
net: Convert AF_NETLINK and AF_VSOCK to getsockopt_iter API

Continue the work to convert protocols to the new getsockopt_iter API.

Convert AF_NETLINK and AF_VSOCK getsockopt implementations to the new
sockopt_t/getsockopt_iter API, and add kselftests that verify the size
and errno semantics are preserved across the conversion.

I chose these two socket families because they are probably one of the
most used  protocols,, ensuring that any potential bugs will be
discovered and reported quickly.
====================

Link: https://patch.msgid.link/20260501-getsock_one-v1-0-810ce23ea70e@debian.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Jakub Kicinski
2026-05-04 19:02:32 -07:00
4 changed files with 232 additions and 19 deletions

View File

@@ -39,6 +39,7 @@
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/uio.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/rtnetlink.h>
@@ -1716,18 +1717,18 @@ static int netlink_setsockopt(struct socket *sock, int level, int optname,
}
static int netlink_getsockopt(struct socket *sock, int level, int optname,
char __user *optval, int __user *optlen)
sockopt_t *opt)
{
struct sock *sk = sock->sk;
struct netlink_sock *nlk = nlk_sk(sk);
unsigned int flag;
int len, val;
u32 group;
if (level != SOL_NETLINK)
return -ENOPROTOOPT;
if (get_user(len, optlen))
return -EFAULT;
len = opt->optlen;
if (len < 0)
return -EINVAL;
@@ -1751,14 +1752,14 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
idx = pos / sizeof(unsigned long);
shift = (pos % sizeof(unsigned long)) * 8;
if (put_user((u32)(nlk->groups[idx] >> shift),
(u32 __user *)(optval + pos))) {
group = (u32)(nlk->groups[idx] >> shift);
if (copy_to_iter(&group, sizeof(u32),
&opt->iter_out) != sizeof(u32)) {
err = -EFAULT;
break;
}
}
if (put_user(ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32)), optlen))
err = -EFAULT;
opt->optlen = ALIGN(BITS_TO_BYTES(nlk->ngroups), sizeof(u32));
netlink_unlock_table();
return err;
}
@@ -1784,8 +1785,8 @@ static int netlink_getsockopt(struct socket *sock, int level, int optname,
len = sizeof(int);
val = test_bit(flag, &nlk->flags);
if (put_user(len, optlen) ||
copy_to_user(optval, &val, len))
opt->optlen = len;
if (copy_to_iter(&val, len, &opt->iter_out) != len)
return -EFAULT;
return 0;
@@ -2813,7 +2814,7 @@ static const struct proto_ops netlink_ops = {
.listen = sock_no_listen,
.shutdown = sock_no_shutdown,
.setsockopt = netlink_setsockopt,
.getsockopt = netlink_getsockopt,
.getsockopt_iter = netlink_getsockopt,
.sendmsg = netlink_sendmsg,
.recvmsg = netlink_recvmsg,
.mmap = sock_no_mmap,

View File

@@ -155,6 +155,7 @@
#include <linux/random.h>
#include <linux/skbuff.h>
#include <linux/smp.h>
#include <linux/uio.h>
#include <linux/socket.h>
#include <linux/stddef.h>
#include <linux/sysctl.h>
@@ -2091,8 +2092,7 @@ static int vsock_connectible_setsockopt(struct socket *sock,
static int vsock_connectible_getsockopt(struct socket *sock,
int level, int optname,
char __user *optval,
int __user *optlen)
sockopt_t *opt)
{
struct sock *sk = sock->sk;
struct vsock_sock *vsk = vsock_sk(sk);
@@ -2110,8 +2110,7 @@ static int vsock_connectible_getsockopt(struct socket *sock,
if (level != AF_VSOCK)
return -ENOPROTOOPT;
if (get_user(len, optlen))
return -EFAULT;
len = opt->optlen;
memset(&v, 0, sizeof(v));
@@ -2142,11 +2141,10 @@ static int vsock_connectible_getsockopt(struct socket *sock,
return -EINVAL;
if (len > lv)
len = lv;
if (copy_to_user(optval, &v, len))
if (copy_to_iter(&v, len, &opt->iter_out) != len)
return -EFAULT;
if (put_user(len, optlen))
return -EFAULT;
opt->optlen = len;
return 0;
}
@@ -2631,7 +2629,7 @@ static const struct proto_ops vsock_stream_ops = {
.listen = vsock_listen,
.shutdown = vsock_shutdown,
.setsockopt = vsock_connectible_setsockopt,
.getsockopt = vsock_connectible_getsockopt,
.getsockopt_iter = vsock_connectible_getsockopt,
.sendmsg = vsock_connectible_sendmsg,
.recvmsg = vsock_connectible_recvmsg,
.mmap = sock_no_mmap,
@@ -2653,7 +2651,7 @@ static const struct proto_ops vsock_seqpacket_ops = {
.listen = vsock_listen,
.shutdown = vsock_shutdown,
.setsockopt = vsock_connectible_setsockopt,
.getsockopt = vsock_connectible_getsockopt,
.getsockopt_iter = vsock_connectible_getsockopt,
.sendmsg = vsock_connectible_sendmsg,
.recvmsg = vsock_connectible_recvmsg,
.mmap = sock_no_mmap,

View File

@@ -176,6 +176,7 @@ TEST_GEN_PROGS := \
bind_timewait \
bind_wildcard \
epoll_busy_poll \
getsockopt_iter \
icmp_rfc4884 \
ipv6_fragmentation \
proc_net_pktgen \

View File

@@ -0,0 +1,213 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Quick test for getsockopt{_iter} tests.
*
* Each fixture targets one converted protocol and pins down the
* returned-length / errno semantics across buffer-size variations,
* an unknown optname and a bogus level.
*
* - netlink: NETLINK_PKTINFO covers the flag-style int path; the
* NETLINK_LIST_MEMBERSHIPS cases cover the size-discovery path
* that always reports the required buffer length back via optlen,
* even when the user buffer is too small to receive any group bits.
* - vsock: SO_VM_SOCKETS_BUFFER_SIZE covers the u64 path.
*
* Author: Breno Leitao <leitao@debian.org>
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/vm_sockets.h>
#include <sys/socket.h>
#include "kselftest_harness.h"
#ifndef AF_VSOCK
#define AF_VSOCK 40
#endif
/* ---------- netlink ---------- */
FIXTURE(netlink)
{
int fd;
};
FIXTURE_SETUP(netlink)
{
int group = RTNLGRP_LINK;
self->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (self->fd < 0)
SKIP(return, "AF_NETLINK socket: %s", strerror(errno));
/* Joining a multicast group grows nlk->ngroups so the
* NETLINK_LIST_MEMBERSHIPS path has a non-zero size to report.
*/
if (setsockopt(self->fd, SOL_NETLINK, NETLINK_ADD_MEMBERSHIP,
&group, sizeof(group)) < 0)
SKIP(return, "NETLINK_ADD_MEMBERSHIP: %s", strerror(errno));
}
FIXTURE_TEARDOWN(netlink)
{
if (self->fd >= 0)
close(self->fd);
}
TEST_F(netlink, pktinfo_exact)
{
int val = -1;
socklen_t optlen = sizeof(val);
ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
&val, &optlen));
ASSERT_EQ(sizeof(int), optlen);
ASSERT_TRUE(val == 0 || val == 1);
}
TEST_F(netlink, pktinfo_oversize_clamped)
{
char buf[16] = {};
socklen_t optlen = sizeof(buf);
ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
buf, &optlen));
ASSERT_EQ(sizeof(int), optlen);
}
TEST_F(netlink, pktinfo_undersize)
{
char buf[2] = {};
socklen_t optlen = sizeof(buf);
ASSERT_EQ(-1, getsockopt(self->fd, SOL_NETLINK, NETLINK_PKTINFO,
buf, &optlen));
ASSERT_EQ(EINVAL, errno);
}
TEST_F(netlink, list_memberships_size_discovery)
{
socklen_t optlen = 0;
char dummy;
ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK,
NETLINK_LIST_MEMBERSHIPS,
&dummy, &optlen));
ASSERT_GT(optlen, 0);
ASSERT_EQ(0, optlen % sizeof(__u32));
}
TEST_F(netlink, list_memberships_full_read)
{
__u32 buf[64] = {};
socklen_t optlen = sizeof(buf);
ASSERT_EQ(0, getsockopt(self->fd, SOL_NETLINK,
NETLINK_LIST_MEMBERSHIPS,
buf, &optlen));
ASSERT_GT(optlen, 0);
ASSERT_LE(optlen, sizeof(buf));
ASSERT_EQ(0, optlen % sizeof(__u32));
}
TEST_F(netlink, bad_level)
{
int val;
socklen_t optlen = sizeof(val);
ASSERT_EQ(-1, getsockopt(self->fd, SOL_SOCKET + 1, NETLINK_PKTINFO,
&val, &optlen));
ASSERT_EQ(ENOPROTOOPT, errno);
}
TEST_F(netlink, bad_optname)
{
int val;
socklen_t optlen = sizeof(val);
ASSERT_EQ(-1, getsockopt(self->fd, SOL_NETLINK, 0x7fff,
&val, &optlen));
ASSERT_EQ(ENOPROTOOPT, errno);
}
/* ---------- vsock ---------- */
FIXTURE(vsock)
{
int fd;
};
FIXTURE_SETUP(vsock)
{
self->fd = socket(AF_VSOCK, SOCK_STREAM, 0);
if (self->fd < 0)
SKIP(return, "AF_VSOCK socket: %s", strerror(errno));
}
FIXTURE_TEARDOWN(vsock)
{
if (self->fd >= 0)
close(self->fd);
}
TEST_F(vsock, buffer_size_exact)
{
uint64_t val = 0;
socklen_t optlen = sizeof(val);
ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
SO_VM_SOCKETS_BUFFER_SIZE,
&val, &optlen));
ASSERT_EQ(sizeof(uint64_t), optlen);
ASSERT_GT(val, 0);
}
TEST_F(vsock, buffer_size_oversize_clamped)
{
char buf[16] = {};
socklen_t optlen = sizeof(buf);
ASSERT_EQ(0, getsockopt(self->fd, AF_VSOCK,
SO_VM_SOCKETS_BUFFER_SIZE,
buf, &optlen));
ASSERT_EQ(sizeof(uint64_t), optlen);
}
TEST_F(vsock, buffer_size_undersize)
{
char buf[4] = {};
socklen_t optlen = sizeof(buf);
ASSERT_EQ(-1, getsockopt(self->fd, AF_VSOCK,
SO_VM_SOCKETS_BUFFER_SIZE,
buf, &optlen));
ASSERT_EQ(EINVAL, errno);
}
TEST_F(vsock, bad_level)
{
uint64_t val;
socklen_t optlen = sizeof(val);
ASSERT_EQ(-1, getsockopt(self->fd, SOL_SOCKET + 1,
SO_VM_SOCKETS_BUFFER_SIZE,
&val, &optlen));
ASSERT_EQ(ENOPROTOOPT, errno);
}
TEST_F(vsock, bad_optname)
{
uint64_t val;
socklen_t optlen = sizeof(val);
ASSERT_EQ(-1, getsockopt(self->fd, AF_VSOCK, 0x7fff,
&val, &optlen));
ASSERT_EQ(ENOPROTOOPT, errno);
}
TEST_HARNESS_MAIN