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 <m...@rbox.co>
---
 .../selftests/bpf/prog_tests/sockmap_basic.c       | 111 +++++++++++++++++++++
 1 file changed, 111 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..5c278353a924294a1452f650634cec0539804b1a
 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,114 @@ 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)
+{
+       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);
+
+       for (;;) {
+               int c = *(int *)arg;
+               int zero = 0;
+
+               (void)bpf_map_update_elem(map, &zero, &c, BPF_ANY);
+
+               if (kill(0, SIGUSR1)) {
+                       FAIL_ERRNO("kill");
+                       break;
+               }
+
+               pthread_testcancel();
+       }
+
+       pthread_cleanup_pop(1);
+
+       return NULL;
+}
+
+static void test_sockmap_vsock_connect_signal_race(void)
+{
+       struct sockaddr_vm addr = {
+               .svm_family = AF_VSOCK,
+               .svm_cid = VMADDR_CID_LOCAL,
+               .svm_port = VMADDR_PORT_ANY
+       };
+       struct sockaddr_vm bad_addr;
+       sighandler_t orig_handler;
+       pthread_t thread;
+       socklen_t alen;
+       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;
+
+       alen = sizeof(addr);
+       if (xgetsockname(s, (struct sockaddr *)&addr, &alen) < 0)
+               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)
+                       goto retry;
+
+               if (!connect(c, (struct sockaddr *)&bad_addr, alen) ||
+                   errno != ESOCKTNOSUPPORT)
+                       goto retry;
+
+               if ((recv(c, &(char){0}, 1, MSG_DONTWAIT) < 0) &&
+                   errno == ENODEV) {
+                       FAIL_ERRNO("recv");
+                       tout = 0;
+               }
+retry:
+               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 +1217,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


Reply via email to