* Re: [PATCH mptcp-next 2/2] selftests: mptcp: exercise socket option propagation and behavior
2026-12-03 14:52 ` [PATCH mptcp-next 2/2] selftests: mptcp: exercise socket option propagation and behavior Ruilin Mao
@ 2026-12-03 15:22 ` sashiko-bot
2026-12-05 4:09 ` netdev-bot+sashiko
1 sibling, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-12-03 15:22 UTC (permalink / raw)
To: Ruilin Mao, mptcp
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] selftests/mptcp: rx_timestamp test races with async
net_enable_timestamp()
- [Low] selftests/mptcp: uninitialized buffer read in
check_bound_device()
- [Low] selftests/mptcp: buffer_sizes SO_SNDBUF 8192 doubles to default
tcp_wmem[1]
- [Low] selftests: mptcp: missing netns isolation and EADDRNOTAVAIL skip
--
commit 10945eef75cc34706855384a71c16cd54ded5d2b
Author: Ruilin Mao <zibuyu423@gmail.com>
selftests: mptcp: exercise socket option propagation and behavior
This commit adds a new kselftest harness binary, mptcp_sockopt_test, to
verify listener socket option propagation onto fallback TCP children and
software RX timestamping on MPTCP connections.
Link: https://github.com/multipath-tcp/mptcp_net-next/issues/525
> diff --git a/tools/testing/selftests/net/mptcp/Makefile b/tools/testing/selftests/net/mptcp/Makefile
> index 22ba0da2adb85..a4c2a611dd5ed 100644
> --- a/tools/testing/selftests/net/mptcp/Makefile
> +++ b/tools/testing/selftests/net/mptcp/Makefile
> @@ -19,6 +19,8 @@ TEST_PROGS := \
> userspace_pm.sh \
> # end of TEST_PROGS
>
> +TEST_GEN_PROGS := mptcp_sockopt_test
> +
> TEST_GEN_FILES := \
> mptcp_connect \
> mptcp_diag \
[Severity: Low]
Can running mptcp_sockopt_test directly in the host network namespace cause
spurious failures from host sysctl settings?
With net.ipv4.tcp_syncookies=2 on the host, tcp_conn_request() issues a SYN
cookie and the peer's bare ACK bypasses tcp_check_req() where
rskq_defer_accept drops bare ACKs, waking the listener early and failing the
poll() check in defer_accept. Running the test in an isolated network
namespace (like the other MPTCP selftests, while keeping unprivileged runs
working) avoids host sysctl interference.
Separately, when IPv6 is disabled on lo via net.ipv6.conf.lo.disable_ipv6=1
or ipv6.disable_ipv6=1, socket(AF_INET6, ...) still succeeds in
FIXTURE_SETUP() and bind() in start_listener() fails with EADDRNOTAVAIL
instead of skipping the IPv6 variant.
> diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c b/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c
> new file mode 100644
> index 0000000000000..48ca5522ed0b7
> --- /dev/null
> +++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c
[ ... ]
> +TEST_F(sockopt, rx_timestamp)
> +{
[ ... ]
> + connect_pair(_metadata, &self->server, IPPROTO_MPTCP, &client, &child);
> + ASSERT_EQ(setsockopt(child, SOL_SOCKET, SO_TIMESTAMPING_NEW, &opt, optlen), 0);
> + ASSERT_EQ(write(client, &byte, 1), 1);
> + ASSERT_EQ(recvmsg(child, &msg, 0), 1);
> + ASSERT_EQ(msg.msg_flags & MSG_CTRUNC, 0);
> + cmsg = CMSG_FIRSTHDR(&msg);
> + ASSERT_NE(cmsg, NULL);
[Severity: Medium]
Can rx_timestamp race with asynchronous timestamping activation and fail
flakily?
Under CONFIG_JUMP_LABEL, when no other socket holds RX software timestamping
active, net_enable_timestamp() defers enabling netstamp_needed_key to
netstamp_work instead of enabling it synchronously:
CPU0 (test / softirq) kworker
setsockopt(SO_TIMESTAMPING_NEW)
net_enable_timestamp()
schedule_work(&netstamp_work)
write(client, &byte, 1)
loopback_xmit()
skb_clear_tstamp(skb)
__netif_rx(skb)
net_timestamp_check() <-- netstamp_needed_key still off
recvmsg(child, &msg, 0) <-- no cmsg, ASSERT_NE fails
netstamp_clear()
static_branch_enable(&netstamp_needed_key)
Without a timestamp, __mptcp_recvmsg_mskq() omits the SO_TIMESTAMPING_NEW
control message and ASSERT_NE(cmsg, NULL) fails. Waiting briefly before
sending traffic (as tools/testing/selftests/net/rxtimestamp.c does) or
retrying would avoid the race.
[ ... ]
> +static void check_bound_device(struct __test_metadata *_metadata,
> + struct listener *server)
> +{
> + int client, child;
> + socklen_t len;
> + char name[IFNAMSIZ];
> +
> + start_listener(_metadata, server);
> + connect_pair(_metadata, server, IPPROTO_TCP, &client, &child);
> + len = sizeof(name);
> + ASSERT_EQ(getsockopt(child, SOL_SOCKET, SO_BINDTODEVICE, name, &len), 0);
> + ASSERT_STREQ(name, "lo");
[Severity: Low]
Can ASSERT_STREQ() read uninitialized stack bytes from name[] if the child
socket is not bound to a device?
In net/core/sock.c, sock_getbindtodevice() returns 0 with *optlen set to 0
and leaves the caller's buffer untouched when bound_dev_if == 0. On a
regression where the binding does not propagate, ASSERT_STREQ() passes an
uninitialized, non-NUL-terminated name[] to strcmp() and failure formatting.
Zero-initializing name and checking ASSERT_EQ(len, sizeof("lo")) before
ASSERT_STREQ() prevents that.
[ ... ]
> +TEST_F(sockopt, buffer_sizes)
> +{
> + static const int opts[] = { SO_SNDBUF, SO_RCVBUF };
> + int child, client, size = 8192;
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(opts); i++)
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, opts[i],
> + &size, sizeof(size)), 0);
> + start_listener(_metadata, &self->server);
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + for (i = 0; i < ARRAY_SIZE(opts); i++)
> + ASSERT_EQ(read_int_opt(_metadata, child, SOL_SOCKET, opts[i]),
> + read_int_opt(_metadata, self->server.fd, SOL_SOCKET, opts[i]));
[Severity: Low]
Does setting SO_SNDBUF to 8192 leave sk_sndbuf at its initial default?
sk_setsockopt() doubles the requested value to 16384, which equals the
default sysctl_tcp_wmem[1] (16 * 1024) used for new TCP and MPTCP sockets,
so this test would not catch a regression that fails to copy sk_sndbuf onto
the child. Using a size whose doubled value differs from both default
tcp_wmem[1] and tcp_rmem[1] (for example 12288) avoids matching the default.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179630953224.20.3586549954582163150.mptcp-525-0000@gmail.com?part=2
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH mptcp-next 2/2] selftests: mptcp: exercise socket option propagation and behavior
2026-12-03 14:52 ` [PATCH mptcp-next 2/2] selftests: mptcp: exercise socket option propagation and behavior Ruilin Mao
2026-12-03 15:22 ` sashiko-bot
@ 2026-12-05 4:09 ` netdev-bot+sashiko
1 sibling, 0 replies; 7+ messages in thread
From: netdev-bot+sashiko @ 2026-12-05 4:09 UTC (permalink / raw)
To: zibuyu423; +Cc: mptcp, kuba
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 4
If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).
- [Medium] TEST_F(sockopt, rx_timestamp) in
tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c is racy and can…
- [Low] TEST_F(sockopt, priority_and_incoming_cpu) in
tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c (lines 315-328)…
- [Low] selftests: mptcp: missing netns isolation and EADDRNOTAVAIL skip
- [Low] selftests/mptcp: uninitialized buffer read in
check_bound_device()
- [Low] selftests/mptcp: buffer_sizes SO_SNDBUF 8192 doubles to default
tcp_wmem[1]
--
selftests: mptcp: exercise socket option propagation and behavior
This adds a new kselftest harness program, mptcp_sockopt_test. It checks
that options set on an MPTCP listener reach the TCP socket underneath by
inspecting fallback children. It also covers SO_LINGER, TCP_DEFER_ACCEPT,
IPV6_V6ONLY, integer socket options and RX software timestamps via
SO_TIMESTAMPING_NEW on negotiated MPTCP connections.
Link: https://github.com/multipath-tcp/mptcp_net-next/issues/525
> diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c b/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c
> new file mode 100644
> index 0000000000000..48ca5522ed0b7
> --- /dev/null
> +++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt_test.c
> @@ -0,0 +1,348 @@
[ ... ]
> +TEST_F(sockopt, rx_timestamp)
> +{
> + const struct so_timestamping opt = {
> + .flags = SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE,
> + };
[ ... ]
> + connect_pair(_metadata, &self->server, IPPROTO_MPTCP, &client, &child);
> + ASSERT_EQ(setsockopt(child, SOL_SOCKET, SO_TIMESTAMPING_NEW, &opt, optlen), 0);
> + ASSERT_EQ(write(client, &byte, 1), 1);
> + ASSERT_EQ(recvmsg(child, &msg, 0), 1);
> + ASSERT_EQ(msg.msg_flags & MSG_CTRUNC, 0);
> + cmsg = CMSG_FIRSTHDR(&msg);
> + ASSERT_NE(cmsg, NULL);
[Severity: Medium]
Can this assertion fail now and then even when the kernel is working
correctly?
Take a kernel built with CONFIG_JUMP_LABEL=y where no other socket on the
system uses timestamps. In that case the setsockopt() above does not turn
on netstamp_needed_key right away. net_enable_timestamp() only schedules
the enable for later:
net/core/dev.c:net_enable_timestamp() {
...
atomic_inc(&netstamp_needed_deferred);
schedule_work(&netstamp_work);
...
}
The key is turned on later by netstamp_clear(), which runs on a kworker.
On loopback, the receive side runs inside the write() call itself. Both
places that set the RX software timestamp check the key first:
loopback_xmit()->__netif_rx()->netif_rx_internal()->net_timestamp_check()
ip_local_deliver_finish()->skb_clear_delivery_time()
include/linux/skbuff.h:skb_clear_delivery_time() {
...
if (static_branch_unlikely(&netstamp_needed_key))
skb->tstamp = ktime_get_real();
else
skb->tstamp = 0;
...
}
If the kworker has not finished static_branch_enable() yet, skb->tstamp
is 0. tcp_v4_fill_cb() then leaves has_rxtstamp false, and
__mptcp_recvmsg_mskq() never sets MPTCP_CMSG_TS:
net/mptcp/protocol.c:__mptcp_recvmsg_mskq() {
...
if (MPTCP_SKB_CB(skb)->has_rxtstamp &&
tcp_want_rx_tstamp_cmsg(sk)) {
tcp_update_recv_tstamps(skb, tss);
*cmsg_flags |= MPTCP_CMSG_TS;
...
}
In that case no cmsg is sent, CMSG_FIRSTHDR() returns NULL and the test
fails.
The second iteration seems to have the same problem. Closing the first
child calls net_disable_timestamp(), which schedules a decrement for
later. That decrement can turn the key off again before the next enable.
Could the test avoid depending on when the scheduled key enable finishes,
for example by retrying until a timestamped segment arrives?
[ ... ]
> +TEST_F(sockopt, priority_and_incoming_cpu)
> +{
> + static const int opts[] = { SO_PRIORITY, SO_INCOMING_CPU };
> + unsigned int i;
> +
> + start_listener(_metadata, &self->server);
> + for (i = 0; i < ARRAY_SIZE(opts); i++) {
> + int value = 2;
> +
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, opts[i],
> + &value, sizeof(value)), 0);
> + ASSERT_EQ(read_int_opt(_metadata, self->server.fd, SOL_SOCKET, opts[i]), value);
[Severity: Low]
Does this check exercise any of the MPTCP propagation?
The value is read back from self->server.fd, which is the msk itself.
The generic sk_getsockopt() handles SOL_SOCKET getsockopt on it. The msk
field is written before any subflow loop runs, either by
sock_setsockopt() in mptcp_sol_socket_intval() or by
WRITE_ONCE(sk->sk_incoming_cpu) in mptcp_so_incoming_cpu().
The MPTCP-specific copies are the per-subflow ones in
mptcp_sol_socket_sync_intval():
net/mptcp/sockopt.c:mptcp_sol_socket_sync_intval() {
...
case SO_PRIORITY:
WRITE_ONCE(ssk->sk_priority, val);
break;
...
case SO_INCOMING_CPU:
WRITE_ONCE(ssk->sk_incoming_cpu, val);
break;
...
}
and the copy made at subflow creation in sync_socket_options():
net/mptcp/sockopt.c:sync_socket_options() {
...
ssk->sk_priority = sk->sk_priority;
ssk->sk_bound_dev_if = sk->sk_bound_dev_if;
ssk->sk_incoming_cpu = sk->sk_incoming_cpu;
...
}
This test would still pass if either of these were removed. The commit
message gives the goal as checking "whether listener options reach the
TCP socket underneath".
The other tests in this file check a fallback child. Could SO_PRIORITY be
checked the same way through connect_pair(), since sk_priority is copied
to the child when it is cloned?
[ ... ]
> diff --git a/tools/testing/selftests/net/mptcp/Makefile b/tools/testing/selftests/net/mptcp/Makefile
> --- a/tools/testing/selftests/net/mptcp/Makefile
> +++ b/tools/testing/selftests/net/mptcp/Makefile
> @@ -19,6 +19,8 @@ TEST_PROGS := \
> userspace_pm.sh \
> # end of TEST_PROGS
>
> +TEST_GEN_PROGS := mptcp_sockopt_test
> +
> TEST_GEN_FILES := \
> mptcp_connect \
> mptcp_diag \
[Severity: Low]
Is it intended that this binary runs directly in the caller's network
namespace?
Every other test in this directory runs inside namespaces created by
mptcp_lib_ns_init(), including the C helpers driven by
mptcp_sockopt.sh. Listing mptcp_sockopt_test in TEST_GEN_PROGS means
the runner executes it in the host netns, so host sysctls leak into
the results.
Two cases look like they turn into failures rather than skips:
With net.ipv6.conf.lo.disable_ipv6=1, or ipv6.disable_ipv6=1 on the
command line, inet6_create() still succeeds, so FIXTURE_SETUP() does
not skip the ipv6 variant. But ::1 is not configured on lo, so the
bind() in start_listener() fails with EADDRNOTAVAIL from
__inet6_bind() and the assertion fires. Since ipv6.disable_ipv6=1 is
also inherited by new namespaces through addrconf_init_net(), this
looks like it needs an explicit skip on EADDRNOTAVAIL even with netns
isolation, similar to what udp_splice_checksum.c does.
With net.ipv4.tcp_syncookies=2 on the host, tcp_conn_request() always
answers with a cookie and no request sock is hashed. The bare ACK from
the client then goes through cookie_v4_check() or cookie_v6_check()
and tcp_get_cookie_sock(), and never reaches the rskq_defer_accept
check in tcp_check_req(). The child is queued right away, the listener
becomes readable, and the first poll() in the defer_accept test
returns 1 instead of 0. A fresh namespace starts with
sysctl_tcp_syncookies set to 1 by tcp_sk_init(), which avoids this.
Would it make sense to run this from a small wrapper script that uses
mptcp_lib_ns_init(), or to unshare(CLONE_NEWNET) when permitted? If
so, please keep unprivileged runs working, since the debug test
already expects to be run without CAP_NET_ADMIN.
[ ... ]
> @@ -0,0 +1,348 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#include <arpa/inet.h>
> +#include <linux/net_tstamp.h>
> +#include <linux/errqueue.h>
> +#include <net/if.h>
> +#include <netinet/tcp.h>
> +#include <poll.h>
> +
> +#include "../../kselftest_harness.h"
> +
> +#ifndef IPPROTO_MPTCP
> +#define IPPROTO_MPTCP 262
> +#endif
> +
> +struct listener {
> + int fd;
> + union {
> + struct sockaddr sa;
> + struct sockaddr_in v4;
> + struct sockaddr_in6 v6;
> + } addr;
> + socklen_t addrlen;
> +};
> +
> +FIXTURE(sockopt) {
> + struct listener server;
> +};
> +
> +FIXTURE_VARIANT(sockopt) {
> + int family;
> +};
> +
> +FIXTURE_VARIANT_ADD(sockopt, ipv4) {
> + .family = AF_INET,
> +};
> +
> +FIXTURE_VARIANT_ADD(sockopt, ipv6) {
> + .family = AF_INET6,
> +};
> +
> +FIXTURE_SETUP(sockopt)
> +{
> + self->server.fd = socket(variant->family, SOCK_STREAM, IPPROTO_MPTCP);
> + if (self->server.fd < 0 && (errno == EPROTONOSUPPORT ||
> + errno == ENOPROTOOPT || errno == EAFNOSUPPORT))
> + SKIP(return, "MPTCP unavailable for address family %d", variant->family);
> + ASSERT_GE(self->server.fd, 0);
> +
> + if (variant->family == AF_INET) {
> + self->server.addr.v4.sin_family = AF_INET;
> + self->server.addr.v4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
> + self->server.addrlen = sizeof(self->server.addr.v4);
> + } else {
> + self->server.addr.v6.sin6_family = AF_INET6;
> + self->server.addr.v6.sin6_addr = in6addr_loopback;
> + self->server.addrlen = sizeof(self->server.addr.v6);
> + }
> +}
> +
> +FIXTURE_TEARDOWN(sockopt)
> +{
> + if (self->server.fd >= 0)
> + close(self->server.fd);
> +}
> +
> +static void start_listener(struct __test_metadata *_metadata,
> + struct listener *server)
> +{
> + ASSERT_EQ(bind(server->fd, &server->addr.sa, server->addrlen), 0);
> + ASSERT_EQ(listen(server->fd, 1), 0);
> + ASSERT_EQ(getsockname(server->fd, &server->addr.sa, &server->addrlen), 0);
> +}
> +
> +static void connect_pair(struct __test_metadata *_metadata,
> + struct listener *server, int protocol,
> + int *client, int *child)
> +{
> + int accepted_proto;
> + socklen_t len = sizeof(accepted_proto);
> +
> + *client = socket(server->addr.sa.sa_family, SOCK_STREAM, protocol);
> + ASSERT_GE(*client, 0);
> + ASSERT_EQ(connect(*client, &server->addr.sa, server->addrlen), 0);
> + *child = accept(server->fd, NULL, NULL);
> + ASSERT_GE(*child, 0);
> + ASSERT_EQ(getsockopt(*child, SOL_SOCKET, SO_PROTOCOL, &accepted_proto, &len), 0);
> + ASSERT_EQ(accepted_proto, protocol);
> +}
> +
> +TEST_F(sockopt, linger)
> +{
> + static const struct linger cases[] = {
> + { .l_onoff = 1, .l_linger = 7 },
> + { .l_onoff = 1, .l_linger = 0 },
> + { .l_onoff = 0 },
> + };
> + int child, client;
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(cases); i++) {
> + struct linger got;
> + socklen_t len = sizeof(got);
> + char buf;
> +
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, SO_LINGER,
> + &cases[i], sizeof(cases[i])), 0);
> + if (!i)
> + start_listener(_metadata, &self->server);
> +
> + /* A fallback child inherits its options from the TCP listener. */
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + ASSERT_EQ(getsockopt(child, SOL_SOCKET, SO_LINGER, &got, &len), 0);
> + ASSERT_EQ(len, sizeof(got));
> + ASSERT_EQ(got.l_onoff, cases[i].l_onoff);
> + if (got.l_onoff)
> + ASSERT_EQ(got.l_linger, cases[i].l_linger);
> + close(child);
> +
> + if (cases[i].l_onoff && !cases[i].l_linger) {
> + ASSERT_EQ(read(client, &buf, 1), -1);
> + ASSERT_EQ(errno, ECONNRESET);
> + } else {
> + ASSERT_EQ(read(client, &buf, 1), 0);
> + }
> + close(client);
> + }
> +}
> +
> +TEST_F(sockopt, congestion)
> +{
> + static const char reno[] = "reno";
> + static const char invalid[] = "no-such-tcp-cc";
> + int child, client;
> + char name[32];
> + socklen_t len;
> +
> + /* Exercise an existing TCP subflow and verify its fallback child. */
> + start_listener(_metadata, &self->server);
> + ASSERT_EQ(setsockopt(self->server.fd, IPPROTO_TCP, TCP_CONGESTION,
> + reno, sizeof(reno)), 0);
> + ASSERT_EQ(setsockopt(self->server.fd, IPPROTO_TCP, TCP_CONGESTION,
> + invalid, sizeof(invalid)), -1);
> + ASSERT_EQ(errno, ENOENT);
> +
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + len = sizeof(name);
> + ASSERT_EQ(getsockopt(child, IPPROTO_TCP, TCP_CONGESTION, name, &len), 0);
> + ASSERT_STREQ(name, reno);
> + close(child);
> + close(client);
> +}
> +
> +TEST_F(sockopt, rx_timestamp)
> +{
> + const struct so_timestamping opt = {
> + .flags = SOF_TIMESTAMPING_SOFTWARE | SOF_TIMESTAMPING_RX_SOFTWARE,
> + };
> + int child, client;
> + char byte = 't';
> + unsigned int i;
> +
> + start_listener(_metadata, &self->server);
> + for (i = 0; i < 2; i++) {
> + union {
> + struct cmsghdr align;
> + char buf[CMSG_SPACE(sizeof(struct scm_timestamping64))];
> + } control;
> + struct iovec iov = { .iov_base = &byte, .iov_len = 1 };
> + struct msghdr msg = {
> + .msg_iov = &iov,
> + .msg_iovlen = 1,
> + .msg_control = control.buf,
> + .msg_controllen = sizeof(control.buf),
> + };
> + struct scm_timestamping64 ts;
> + struct cmsghdr *cmsg;
> + socklen_t optlen = i ? sizeof(opt) : sizeof(opt.flags);
> +
> + connect_pair(_metadata, &self->server, IPPROTO_MPTCP, &client, &child);
> + ASSERT_EQ(setsockopt(child, SOL_SOCKET, SO_TIMESTAMPING_NEW, &opt, optlen), 0);
> + ASSERT_EQ(write(client, &byte, 1), 1);
> + ASSERT_EQ(recvmsg(child, &msg, 0), 1);
> + ASSERT_EQ(msg.msg_flags & MSG_CTRUNC, 0);
> + cmsg = CMSG_FIRSTHDR(&msg);
> + ASSERT_NE(cmsg, NULL);
> + ASSERT_EQ(cmsg->cmsg_level, SOL_SOCKET);
> + ASSERT_EQ(cmsg->cmsg_type, SO_TIMESTAMPING_NEW);
> + ASSERT_EQ(cmsg->cmsg_len, CMSG_LEN(sizeof(ts)));
> + memcpy(&ts, CMSG_DATA(cmsg), sizeof(ts));
> + ASSERT_GT(ts.ts[0].tv_sec, 0);
> + ASSERT_GE(ts.ts[0].tv_nsec, 0);
> + ASSERT_LT(ts.ts[0].tv_nsec, 1000000000);
> + close(child);
> + close(client);
> + }
> +}
> +
> +TEST_F(sockopt, defer_accept)
> +{
> + struct pollfd pfd = { .fd = self->server.fd, .events = POLLIN };
> + int client, child, timeout = 10;
> + char byte = 'x';
> +
> + ASSERT_EQ(setsockopt(self->server.fd, IPPROTO_TCP, TCP_DEFER_ACCEPT,
> + &timeout, sizeof(timeout)), 0);
> + start_listener(_metadata, &self->server);
> + client = socket(variant->family, SOCK_STREAM, IPPROTO_TCP);
> + ASSERT_GE(client, 0);
> + ASSERT_EQ(connect(client, &self->server.addr.sa, self->server.addrlen), 0);
> +
> + /* The TCP handshake alone should not wake the MPTCP listener. */
> + ASSERT_EQ(poll(&pfd, 1, 100), 0);
> + ASSERT_EQ(write(client, &byte, 1), 1);
> + ASSERT_EQ(poll(&pfd, 1, 5000), 1);
> + ASSERT_EQ(pfd.revents, POLLIN);
> + child = accept(self->server.fd, NULL, NULL);
> + ASSERT_GE(child, 0);
> + ASSERT_EQ(read(child, &byte, 1), 1);
> + ASSERT_EQ(byte, 'x');
> + close(child);
> + close(client);
> +}
> +
> +static int read_int_opt(struct __test_metadata *_metadata, int fd, int level, int opt)
> +{
> + int val;
> + socklen_t len = sizeof(val);
> +
> + ASSERT_EQ(getsockopt(fd, level, opt, &val, &len), 0);
> + ASSERT_EQ(len, sizeof(val));
> + return val;
> +}
> +
> +TEST_F(sockopt, freebind)
> +{
> + int level = variant->family == AF_INET ? IPPROTO_IP : IPPROTO_IPV6;
> + int opt = variant->family == AF_INET ? IP_FREEBIND : IPV6_FREEBIND;
> + int child, client, on = 1;
> +
> + ASSERT_EQ(setsockopt(self->server.fd, level, opt, &on, sizeof(on)), 0);
> + start_listener(_metadata, &self->server);
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + ASSERT_EQ(read_int_opt(_metadata, child, level, opt), 1);
> + close(child);
> + close(client);
> +}
> +
> +TEST_F(sockopt, v6only)
> +{
> + struct sockaddr_in6 mapped = { .sin6_family = AF_INET6 };
> + int on = 1;
> +
> + if (variant->family != AF_INET6)
> + SKIP(return, "IPv6 only");
> +
> + ASSERT_EQ(inet_pton(AF_INET6, "::ffff:127.0.0.1", &mapped.sin6_addr), 1);
> + ASSERT_EQ(setsockopt(self->server.fd, IPPROTO_IPV6, IPV6_V6ONLY,
> + &on, sizeof(on)), 0);
> + ASSERT_EQ(read_int_opt(_metadata, self->server.fd, IPPROTO_IPV6, IPV6_V6ONLY), 1);
> + ASSERT_EQ(bind(self->server.fd, (struct sockaddr *)&mapped, sizeof(mapped)), -1);
> + ASSERT_EQ(errno, EINVAL);
> +}
> +
> +static void check_bound_device(struct __test_metadata *_metadata,
> + struct listener *server)
> +{
> + int client, child;
> + socklen_t len;
> + char name[IFNAMSIZ];
> +
> + start_listener(_metadata, server);
> + connect_pair(_metadata, server, IPPROTO_TCP, &client, &child);
> + len = sizeof(name);
> + ASSERT_EQ(getsockopt(child, SOL_SOCKET, SO_BINDTODEVICE, name, &len), 0);
> + ASSERT_STREQ(name, "lo");
> + ASSERT_EQ(read_int_opt(_metadata, child, SOL_SOCKET, SO_BINDTOIFINDEX),
> + if_nametoindex("lo"));
> + close(child);
> + close(client);
> +}
> +
> +TEST_F(sockopt, bind_to_ifindex)
> +{
> + int ifindex = if_nametoindex("lo");
> +
> + ASSERT_GT(ifindex, 0);
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, SO_BINDTOIFINDEX,
> + &ifindex, sizeof(ifindex)), 0);
> + check_bound_device(_metadata, &self->server);
> +}
> +
> +TEST_F(sockopt, bind_to_device)
> +{
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, SO_BINDTODEVICE,
> + "lo", sizeof("lo")), 0);
> + check_bound_device(_metadata, &self->server);
> +}
> +
> +TEST_F(sockopt, debug)
> +{
> + int on = 1, client, child;
> +
> + start_listener(_metadata, &self->server);
> + if (setsockopt(self->server.fd, SOL_SOCKET, SO_DEBUG, &on, sizeof(on))) {
> + ASSERT_EQ(errno, EACCES);
> + SKIP(return, "SO_DEBUG needs CAP_NET_ADMIN in the initial user namespace");
> + }
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + ASSERT_EQ(read_int_opt(_metadata, child, SOL_SOCKET, SO_DEBUG), 1);
> + close(child);
> + close(client);
> +}
> +
> +TEST_F(sockopt, priority_and_incoming_cpu)
> +{
> + static const int opts[] = { SO_PRIORITY, SO_INCOMING_CPU };
> + unsigned int i;
> +
> + start_listener(_metadata, &self->server);
> + for (i = 0; i < ARRAY_SIZE(opts); i++) {
> + int value = 2;
> +
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, opts[i],
> + &value, sizeof(value)), 0);
> + ASSERT_EQ(read_int_opt(_metadata, self->server.fd, SOL_SOCKET, opts[i]), value);
> + }
> +}
> +
> +TEST_F(sockopt, buffer_sizes)
> +{
> + static const int opts[] = { SO_SNDBUF, SO_RCVBUF };
> + int child, client, size = 8192;
> + unsigned int i;
> +
> + for (i = 0; i < ARRAY_SIZE(opts); i++)
> + ASSERT_EQ(setsockopt(self->server.fd, SOL_SOCKET, opts[i],
> + &size, sizeof(size)), 0);
> + start_listener(_metadata, &self->server);
> + connect_pair(_metadata, &self->server, IPPROTO_TCP, &client, &child);
> + for (i = 0; i < ARRAY_SIZE(opts); i++)
> + ASSERT_EQ(read_int_opt(_metadata, child, SOL_SOCKET, opts[i]),
> + read_int_opt(_metadata, self->server.fd, SOL_SOCKET, opts[i]));
> + close(child);
> + close(client);
> +}
> +
> +TEST_HARNESS_MAIN
>
[Severity: Low]
Does the SO_SNDBUF half of this test move the value away from its
default?
sk_setsockopt() stores max_t(int, val * 2, SOCK_MIN_SNDBUF) for
SO_SNDBUF, so 8192 becomes 16384. That is the same as the default
sysctl_tcp_wmem[1] set in tcp_init(), which is what a new TCP socket
and a new msk both start with in sk_sndbuf. So the listener and the
child would both report 16384 even if the msk value were never copied
to the subflow.
SOCK_SNDBUF_LOCK propagation is still covered indirectly, since an
unlocked child would have its sndbuf grown by tcp_sndbuf_expand() from
tcp_init_buffer_space(), and SO_RCVBUF is fine because 16384 differs
from the default tcp_rmem[1] of 131072. But a regression that copies
the lock flag without the sk_sndbuf value would go unnoticed.
Could a size be used whose doubled value differs from both defaults,
such as 12288, or could the test check that the listener value
changed from what it read before the setsockopt()?
[Severity: Low]
What happens here if the device binding does not reach the child?
name is not initialised and len is not checked after the
SO_BINDTODEVICE getsockopt(). For an unbound socket,
sock_getbindtodevice() takes the bound_dev_if == 0 branch, reports a
length of 0 and returns success without writing anything to optval.
ASSERT_STREQ() then runs strcmp() on uninitialised stack bytes, and
the failure message prints them with %s, which can read past the end
of name[IFNAMSIZ].
This only matters on the failure path, and the SO_BINDTOIFINDEX check
that follows would still catch an unbound child. But the diagnostic
for exactly the regression this test is meant to catch would be
garbage, and with unlucky stack contents the string check could even
pass.
Could name be zero-initialised, and len checked against sizeof("lo")
before the string comparison?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/179630953224.20.3586549954582163150.mptcp-525-0000%40gmail.com
^ permalink raw reply [flat|nested] 7+ messages in thread