[PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling

Michal Luczaj posted 5 patches 5 months, 1 week ago
[PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling
Posted by Michal Luczaj 5 months, 1 week ago
In some test cases, OOB packets might have been left unread. Flush them out
and introduce additional checks.

Signed-off-by: Michal Luczaj <mhal@rbox.co>
---
 tools/testing/selftests/bpf/prog_tests/sockmap_redir.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
index c1bf1076e8152b7d83c3e07e2dce746b5a39cf7e..4997e72c14345b274367f3f2f4115c39d1ae48c9 100644
--- a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
+++ b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
@@ -184,6 +184,19 @@ static void handle_unsupported(int sd_send, int sd_peer, int sd_in, int sd_out,
 			FAIL_ERRNO("unsupported: packet missing, retval=%zd", n);
 	}
 
+	/* af_unix send("ab", MSG_OOB) spits out 2 packets, but only the latter
+	 * ("b") is designated OOB. If the peer is in a sockmap, the OOB packet
+	 * will be silently dropped. Otherwise OOB stays in the queue and should
+	 * be taken care of.
+	 */
+	if ((send_flags & MSG_OOB) && !pass && !drop) {
+		errno = 0;
+		n = recv_timeout(sd_peer, &recv_buf, 1, MSG_OOB, IO_TIMEOUT_SEC);
+		/* Ignore unsupported sk_msg error */
+		if (n != 1 && errno != EOPNOTSUPP)
+			FAIL_ERRNO("recv(OOB): retval=%zd", n);
+	}
+
 	/* Ensure queues are empty */
 	fail_recv("bpf.recv(sd_send)", sd_send, 0);
 	if (sd_in != sd_send)
@@ -192,6 +205,9 @@ static void handle_unsupported(int sd_send, int sd_peer, int sd_in, int sd_out,
 	fail_recv("bpf.recv(sd_out)", sd_out, 0);
 	if (sd_recv != sd_out)
 		fail_recv("bpf.recv(sd_recv)", sd_recv, 0);
+
+	fail_recv("recv(sd_peer, OOB)", sd_peer, MSG_OOB);
+	fail_recv("recv(sd_out, OOB)", sd_out, MSG_OOB);
 }
 
 static void test_send_redir_recv(int sd_send, int send_flags, int sd_peer,

-- 
2.50.1
Re: [PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling
Posted by Jakub Sitnicki 4 months, 3 weeks ago
Sorry for the super-long time-to-feedback.

On Fri, Sep 05, 2025 at 01:11 PM +02, Michal Luczaj wrote:
> In some test cases, OOB packets might have been left unread. Flush them out
> and introduce additional checks.
>
> Signed-off-by: Michal Luczaj <mhal@rbox.co>
> ---
>  tools/testing/selftests/bpf/prog_tests/sockmap_redir.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> index c1bf1076e8152b7d83c3e07e2dce746b5a39cf7e..4997e72c14345b274367f3f2f4115c39d1ae48c9 100644
> --- a/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> +++ b/tools/testing/selftests/bpf/prog_tests/sockmap_redir.c
> @@ -184,6 +184,19 @@ static void handle_unsupported(int sd_send, int sd_peer, int sd_in, int sd_out,
>  			FAIL_ERRNO("unsupported: packet missing, retval=%zd", n);
>  	}
>  
> +	/* af_unix send("ab", MSG_OOB) spits out 2 packets, but only the latter
> +	 * ("b") is designated OOB. If the peer is in a sockmap, the OOB packet
> +	 * will be silently dropped. Otherwise OOB stays in the queue and should
> +	 * be taken care of.
> +	 */
> +	if ((send_flags & MSG_OOB) && !pass && !drop) {

Nit: There's a similar check a few lines before that:

	if (pass == 0 && drop == 0 && (status & UNSUPPORTED_RACY_VERD)) {

For readability it might make sense to introduce a helper flag:

        bool no_verdict = !pass && !drop; /* prog didn't run */

> +		errno = 0;
> +		n = recv_timeout(sd_peer, &recv_buf, 1, MSG_OOB, IO_TIMEOUT_SEC);
> +		/* Ignore unsupported sk_msg error */
> +		if (n != 1 && errno != EOPNOTSUPP)
> +			FAIL_ERRNO("recv(OOB): retval=%zd", n);
> +	}
> +
>  	/* Ensure queues are empty */
>  	fail_recv("bpf.recv(sd_send)", sd_send, 0);
>  	if (sd_in != sd_send)
> @@ -192,6 +205,9 @@ static void handle_unsupported(int sd_send, int sd_peer, int sd_in, int sd_out,
>  	fail_recv("bpf.recv(sd_out)", sd_out, 0);
>  	if (sd_recv != sd_out)
>  		fail_recv("bpf.recv(sd_recv)", sd_recv, 0);
> +
> +	fail_recv("recv(sd_peer, OOB)", sd_peer, MSG_OOB);
> +	fail_recv("recv(sd_out, OOB)", sd_out, MSG_OOB);
>  }
>  
>  static void test_send_redir_recv(int sd_send, int send_flags, int sd_peer,

Reviewed-by: Jakub Sitnicki <jakub@cloudflare.com>
Re: [PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling
Posted by Michal Luczaj 5 months, 1 week ago
On 9/5/25 13:11, Michal Luczaj wrote:
> In some test cases, OOB packets might have been left unread. Flush them out
> and introduce additional checks.
> 
> Signed-off-by: Michal Luczaj <mhal@rbox.co>

Sorry, this should also have:

Fixes: f0709263a07e ("selftests/bpf: Add selftest for sockmap/hashmap
redirection")
Re: [PATCH bpf-next 2/5] selftests/bpf: sockmap_redir: Fix OOB handling
Posted by Jakub Sitnicki 4 months, 3 weeks ago
On Fri, Sep 05, 2025 at 01:19 PM +02, Michal Luczaj wrote:
> On 9/5/25 13:11, Michal Luczaj wrote:
>> In some test cases, OOB packets might have been left unread. Flush them out
>> and introduce additional checks.
>> 
>> Signed-off-by: Michal Luczaj <mhal@rbox.co>
>
> Sorry, this should also have:
>
> Fixes: f0709263a07e ("selftests/bpf: Add selftest for sockmap/hashmap
> redirection")

I'm not sure if we're backporting selftest changes to stable.