Racing signal-interrupted connect() and sockmap update may result in an
unconnected (and missing vsock transport) socket in a sockmap.
Test spends 2 seconds attempting to reach WARN_ON_ONCE().
connect
/ state = SS_CONNECTED /
sock_map_update_elem
if signal_pending
state = SS_UNCONNECTED
connect
transport = NULL
vsock_bpf_recvmsg
WARN_ON_ONCE(!vsk->transport)
Signed-off-by: Michal Luczaj <mhal@rbox.co>
---
.../selftests/bpf/prog_tests/sockmap_basic.c | 99 ++++++++++++++++++++++
1 file changed, 99 insertions(+)
diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
index 1e3e4392dcca0e1722c1982ecc649a80c27443b2..2f8bba27866354848f1e30b5473cedb6a85244ff 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
@@ -3,6 +3,7 @@
#include <error.h>
#include <netinet/tcp.h>
#include <sys/epoll.h>
+#include <linux/time64.h>
#include "test_progs.h"
#include "test_skmsg_load_helpers.skel.h"
@@ -1042,6 +1043,102 @@ static void test_sockmap_vsock_unconnected(void)
xclose(map);
}
+#define CONNECT_SIGNAL_RACE_TIMEOUT 2 /* seconds */
+
+static void sig_handler(int signum)
+{
+ /* nop */
+}
+
+static void connect_signal_racer_cleanup(void *map)
+{
+ xclose(*(int *)map);
+}
+
+static void *connect_signal_racer(void *arg)
+{
+ pid_t pid;
+ int map;
+
+ map = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL, sizeof(int),
+ sizeof(int), 1, NULL);
+ if (!ASSERT_OK_FD(map, "bpf_map_create"))
+ return NULL;
+
+ pthread_cleanup_push(connect_signal_racer_cleanup, &map);
+ pid = getpid();
+
+ for (;;) {
+ int c = *(int *)arg;
+ int zero = 0;
+
+ (void)bpf_map_update_elem(map, &zero, &c, BPF_ANY);
+
+ if (kill(pid, SIGUSR1)) {
+ FAIL_ERRNO("kill");
+ break;
+ }
+
+ if ((recv(c, NULL, 0, MSG_DONTWAIT) < 0) && errno == ENODEV) {
+ FAIL_ERRNO("recv");
+ break;
+ }
+ }
+
+ pthread_cleanup_pop(1);
+
+ return NULL;
+}
+
+static void test_sockmap_vsock_connect_signal_race(void)
+{
+ struct sockaddr_vm addr, bad_addr;
+ socklen_t alen = sizeof(addr);
+ sighandler_t orig_handler;
+ pthread_t thread;
+ int s, c, p;
+ __u64 tout;
+
+ orig_handler = signal(SIGUSR1, sig_handler);
+ if (!ASSERT_NEQ(orig_handler, SIG_ERR, "signal handler setup"))
+ return;
+
+ s = socket_loopback(AF_VSOCK, SOCK_SEQPACKET | SOCK_NONBLOCK);
+ if (s < 0)
+ goto restore;
+
+ if (xgetsockname(s, (struct sockaddr *)&addr, &alen))
+ goto close;
+
+ bad_addr = addr;
+ bad_addr.svm_cid = 0x42424242; /* non-existing */
+
+ if (xpthread_create(&thread, 0, connect_signal_racer, &c))
+ goto close;
+
+ tout = get_time_ns() + CONNECT_SIGNAL_RACE_TIMEOUT * NSEC_PER_SEC;
+ do {
+ c = xsocket(AF_VSOCK, SOCK_SEQPACKET, 0);
+ if (c < 0)
+ break;
+
+ if (connect(c, (struct sockaddr *)&addr, alen) && errno == EINTR)
+ (void)connect(c, (struct sockaddr *)&bad_addr, alen);
+
+ xclose(c);
+ p = accept(s, NULL, NULL);
+ if (p >= 0)
+ xclose(p);
+ } while (get_time_ns() < tout);
+
+ ASSERT_OK(pthread_cancel(thread), "pthread_cancel");
+ xpthread_join(thread, NULL);
+close:
+ xclose(s);
+restore:
+ ASSERT_NEQ(signal(SIGUSR1, orig_handler), SIG_ERR, "handler restore");
+}
+
void test_sockmap_basic(void)
{
if (test__start_subtest("sockmap create_update_free"))
@@ -1108,4 +1205,6 @@ void test_sockmap_basic(void)
test_sockmap_skb_verdict_vsock_poll();
if (test__start_subtest("sockmap vsock unconnected"))
test_sockmap_vsock_unconnected();
+ if (test__start_subtest("sockmap vsock connect signal race"))
+ test_sockmap_vsock_connect_signal_race();
}
--
2.48.1
On Mon, Mar 17, 2025 at 10:52:24AM +0100, Michal Luczaj wrote:
>Racing signal-interrupted connect() and sockmap update may result in an
>unconnected (and missing vsock transport) socket in a sockmap.
>
>Test spends 2 seconds attempting to reach WARN_ON_ONCE().
>
>connect
> / state = SS_CONNECTED /
> sock_map_update_elem
> if signal_pending
> state = SS_UNCONNECTED
>
>connect
> transport = NULL
> vsock_bpf_recvmsg
> WARN_ON_ONCE(!vsk->transport)
>
>Signed-off-by: Michal Luczaj <mhal@rbox.co>
>---
> .../selftests/bpf/prog_tests/sockmap_basic.c | 99 ++++++++++++++++++++++
> 1 file changed, 99 insertions(+)
LGTM for the vsock part!
Acked-by: Stefano Garzarella <sgarzare@redhat.com>
>
>diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
>index 1e3e4392dcca0e1722c1982ecc649a80c27443b2..2f8bba27866354848f1e30b5473cedb6a85244ff 100644
>--- a/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
>+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
>@@ -3,6 +3,7 @@
> #include <error.h>
> #include <netinet/tcp.h>
> #include <sys/epoll.h>
>+#include <linux/time64.h>
>
> #include "test_progs.h"
> #include "test_skmsg_load_helpers.skel.h"
>@@ -1042,6 +1043,102 @@ static void test_sockmap_vsock_unconnected(void)
> xclose(map);
> }
>
>+#define CONNECT_SIGNAL_RACE_TIMEOUT 2 /* seconds */
>+
>+static void sig_handler(int signum)
>+{
>+ /* nop */
>+}
>+
>+static void connect_signal_racer_cleanup(void *map)
>+{
>+ xclose(*(int *)map);
>+}
>+
>+static void *connect_signal_racer(void *arg)
>+{
>+ pid_t pid;
>+ int map;
>+
>+ map = bpf_map_create(BPF_MAP_TYPE_SOCKMAP, NULL, sizeof(int),
>+ sizeof(int), 1, NULL);
>+ if (!ASSERT_OK_FD(map, "bpf_map_create"))
>+ return NULL;
>+
>+ pthread_cleanup_push(connect_signal_racer_cleanup, &map);
>+ pid = getpid();
>+
>+ for (;;) {
>+ int c = *(int *)arg;
>+ int zero = 0;
>+
>+ (void)bpf_map_update_elem(map, &zero, &c, BPF_ANY);
>+
>+ if (kill(pid, SIGUSR1)) {
>+ FAIL_ERRNO("kill");
>+ break;
>+ }
>+
>+ if ((recv(c, NULL, 0, MSG_DONTWAIT) < 0) && errno == ENODEV) {
>+ FAIL_ERRNO("recv");
>+ break;
>+ }
>+ }
>+
>+ pthread_cleanup_pop(1);
>+
>+ return NULL;
>+}
>+
>+static void test_sockmap_vsock_connect_signal_race(void)
>+{
>+ struct sockaddr_vm addr, bad_addr;
>+ socklen_t alen = sizeof(addr);
>+ sighandler_t orig_handler;
>+ pthread_t thread;
>+ int s, c, p;
>+ __u64 tout;
>+
>+ orig_handler = signal(SIGUSR1, sig_handler);
>+ if (!ASSERT_NEQ(orig_handler, SIG_ERR, "signal handler setup"))
>+ return;
>+
>+ s = socket_loopback(AF_VSOCK, SOCK_SEQPACKET | SOCK_NONBLOCK);
>+ if (s < 0)
>+ goto restore;
>+
>+ if (xgetsockname(s, (struct sockaddr *)&addr, &alen))
>+ goto close;
>+
>+ bad_addr = addr;
>+ bad_addr.svm_cid = 0x42424242; /* non-existing */
>+
>+ if (xpthread_create(&thread, 0, connect_signal_racer, &c))
>+ goto close;
>+
>+ tout = get_time_ns() + CONNECT_SIGNAL_RACE_TIMEOUT * NSEC_PER_SEC;
>+ do {
>+ c = xsocket(AF_VSOCK, SOCK_SEQPACKET, 0);
>+ if (c < 0)
>+ break;
>+
>+ if (connect(c, (struct sockaddr *)&addr, alen) && errno == EINTR)
>+ (void)connect(c, (struct sockaddr *)&bad_addr, alen);
>+
>+ xclose(c);
>+ p = accept(s, NULL, NULL);
>+ if (p >= 0)
>+ xclose(p);
>+ } while (get_time_ns() < tout);
>+
>+ ASSERT_OK(pthread_cancel(thread), "pthread_cancel");
>+ xpthread_join(thread, NULL);
>+close:
>+ xclose(s);
>+restore:
>+ ASSERT_NEQ(signal(SIGUSR1, orig_handler), SIG_ERR, "handler restore");
>+}
>+
> void test_sockmap_basic(void)
> {
> if (test__start_subtest("sockmap create_update_free"))
>@@ -1108,4 +1205,6 @@ void test_sockmap_basic(void)
> test_sockmap_skb_verdict_vsock_poll();
> if (test__start_subtest("sockmap vsock unconnected"))
> test_sockmap_vsock_unconnected();
>+ if (test__start_subtest("sockmap vsock connect signal race"))
>+ test_sockmap_vsock_connect_signal_race();
> }
>
>--
>2.48.1
>
© 2016 - 2025 Red Hat, Inc.