]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/bpf: Test sockmap redirect for AF_UNIX MSG_OOB
authorMichal Luczaj <mhal@rbox.co>
Sat, 13 Jul 2024 19:41:41 +0000 (21:41 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 17 Jul 2024 20:51:55 +0000 (22:51 +0200)
Verify that out-of-band packets are silently dropped before they reach the
redirection logic.

The idea is to test with a 2 byte long send(). Should a MSG_OOB flag be in
use, only the last byte will be treated as out-of-band. Test fails if
verd_mapfd indicates a wrong number of packets processed (e.g. if OOB
wasn't dropped at the source) or if it was possible to recv() MSG_OOB from
the mapped socket, or if any stale OOB data have been left reachable from
the unmapped socket.

Signed-off-by: Michal Luczaj <mhal@rbox.co>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Jakub Sitnicki <jakub@cloudflare.com>
Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Link: https://lore.kernel.org/bpf/20240713200218.2140950-5-mhal@rbox.co
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c

index 3514a344bee6502890b5422c1d90c51e130b9cbb..9ce0e0e0b7da75f257491454986a8c3136fc13ed 100644 (file)
@@ -1399,10 +1399,11 @@ static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
                        return;
        }
 
-       n = xsend(cli1, "a", 1, send_flags);
-       if (n == 0)
+       /* Last byte is OOB data when send_flags has MSG_OOB bit set */
+       n = xsend(cli1, "ab", 2, send_flags);
+       if (n >= 0 && n < 2)
                FAIL("%s: incomplete send", log_prefix);
-       if (n < 1)
+       if (n < 2)
                return;
 
        key = SK_PASS;
@@ -1417,6 +1418,25 @@ static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
                FAIL_ERRNO("%s: recv_timeout", log_prefix);
        if (n == 0)
                FAIL("%s: incomplete recv", log_prefix);
+
+       if (send_flags & MSG_OOB) {
+               /* Check that we can't read OOB while in sockmap */
+               errno = 0;
+               n = recv(peer1, &b, 1, MSG_OOB | MSG_DONTWAIT);
+               if (n != -1 || errno != EOPNOTSUPP)
+                       FAIL("%s: recv(MSG_OOB): expected EOPNOTSUPP: retval=%d errno=%d",
+                            log_prefix, n, errno);
+
+               /* Remove peer1 from sockmap */
+               xbpf_map_delete_elem(sock_mapfd, &(int){ 1 });
+
+               /* Check that OOB was dropped on redirect */
+               errno = 0;
+               n = recv(peer1, &b, 1, MSG_OOB | MSG_DONTWAIT);
+               if (n != -1 || errno != EINVAL)
+                       FAIL("%s: recv(MSG_OOB): expected EINVAL: retval=%d errno=%d",
+                            log_prefix, n, errno);
+       }
 }
 
 static void unix_redir_to_connected(int sotype, int sock_mapfd,
@@ -1873,6 +1893,11 @@ static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
                                     sock_map, nop_map, verdict_map,
                                     REDIR_EGRESS, NO_FLAGS);
 
+       /* MSG_OOB not supported by AF_UNIX SOCK_DGRAM */
+       unix_inet_redir_to_connected(family, SOCK_STREAM,
+                                    sock_map, nop_map, verdict_map,
+                                    REDIR_EGRESS, MSG_OOB);
+
        skel->bss->test_ingress = true;
        unix_inet_redir_to_connected(family, SOCK_DGRAM,
                                     sock_map, -1, verdict_map,
@@ -1888,6 +1913,11 @@ static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
                                     sock_map, nop_map, verdict_map,
                                     REDIR_INGRESS, NO_FLAGS);
 
+       /* MSG_OOB not supported by AF_UNIX SOCK_DGRAM */
+       unix_inet_redir_to_connected(family, SOCK_STREAM,
+                                    sock_map, nop_map, verdict_map,
+                                    REDIR_INGRESS, MSG_OOB);
+
        xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT);
 }