Skip to content

Commit 6caf9ef

Browse files
mmhalborkmann
authored andcommitted
selftests/bpf: Test sockmap redirect for AF_UNIX MSG_OOB
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Tested-by: Jakub Sitnicki <[email protected]> Reviewed-by: Jakub Sitnicki <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0befb34 commit 6caf9ef

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

tools/testing/selftests/bpf/prog_tests/sockmap_listen.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,10 +1399,11 @@ static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
13991399
return;
14001400
}
14011401

1402-
n = xsend(cli1, "a", 1, send_flags);
1403-
if (n == 0)
1402+
/* Last byte is OOB data when send_flags has MSG_OOB bit set */
1403+
n = xsend(cli1, "ab", 2, send_flags);
1404+
if (n >= 0 && n < 2)
14041405
FAIL("%s: incomplete send", log_prefix);
1405-
if (n < 1)
1406+
if (n < 2)
14061407
return;
14071408

14081409
key = SK_PASS;
@@ -1417,6 +1418,25 @@ static void pairs_redir_to_connected(int cli0, int peer0, int cli1, int peer1,
14171418
FAIL_ERRNO("%s: recv_timeout", log_prefix);
14181419
if (n == 0)
14191420
FAIL("%s: incomplete recv", log_prefix);
1421+
1422+
if (send_flags & MSG_OOB) {
1423+
/* Check that we can't read OOB while in sockmap */
1424+
errno = 0;
1425+
n = recv(peer1, &b, 1, MSG_OOB | MSG_DONTWAIT);
1426+
if (n != -1 || errno != EOPNOTSUPP)
1427+
FAIL("%s: recv(MSG_OOB): expected EOPNOTSUPP: retval=%d errno=%d",
1428+
log_prefix, n, errno);
1429+
1430+
/* Remove peer1 from sockmap */
1431+
xbpf_map_delete_elem(sock_mapfd, &(int){ 1 });
1432+
1433+
/* Check that OOB was dropped on redirect */
1434+
errno = 0;
1435+
n = recv(peer1, &b, 1, MSG_OOB | MSG_DONTWAIT);
1436+
if (n != -1 || errno != EINVAL)
1437+
FAIL("%s: recv(MSG_OOB): expected EINVAL: retval=%d errno=%d",
1438+
log_prefix, n, errno);
1439+
}
14201440
}
14211441

14221442
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,
18731893
sock_map, nop_map, verdict_map,
18741894
REDIR_EGRESS, NO_FLAGS);
18751895

1896+
/* MSG_OOB not supported by AF_UNIX SOCK_DGRAM */
1897+
unix_inet_redir_to_connected(family, SOCK_STREAM,
1898+
sock_map, nop_map, verdict_map,
1899+
REDIR_EGRESS, MSG_OOB);
1900+
18761901
skel->bss->test_ingress = true;
18771902
unix_inet_redir_to_connected(family, SOCK_DGRAM,
18781903
sock_map, -1, verdict_map,
@@ -1888,6 +1913,11 @@ static void unix_inet_skb_redir_to_connected(struct test_sockmap_listen *skel,
18881913
sock_map, nop_map, verdict_map,
18891914
REDIR_INGRESS, NO_FLAGS);
18901915

1916+
/* MSG_OOB not supported by AF_UNIX SOCK_DGRAM */
1917+
unix_inet_redir_to_connected(family, SOCK_STREAM,
1918+
sock_map, nop_map, verdict_map,
1919+
REDIR_INGRESS, MSG_OOB);
1920+
18911921
xbpf_prog_detach2(verdict, sock_map, BPF_SK_SKB_VERDICT);
18921922
}
18931923

0 commit comments

Comments
 (0)