Skip to contentSkip to conversation

selftests/bpf: fix v4_to_v6 in sk_lookup

PATCH Applied Stanislav Fomichev 3 participants 5 messages Aug 7, 2020 – Aug 11, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/bpf: fix v4_to_v6 in sk_lookup

I'm getting some garbage in bytes 8 and 9 when doing conversion from sockaddr_in to sockaddr_in6 (leftover from AF_INET?). Let's explicitly clear the higher bytes.

---
 tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +1 -0
index c571584c00f5..9ff0412e1fd3 100644
@@ -309,6 +309,7 @@ static void v4_to_v6(struct sockaddr_storage *ss)
 	v6->sin6_addr.s6_addr[10] = 0xff;
 	v6->sin6_addr.s6_addr[11] = 0xff;
 	memcpy(&v6->sin6_addr.s6_addr[12], &v4.sin_addr.s_addr, 4);
+	memset(&v6->sin6_addr.s6_addr[0], 0, 10);
 }
 
 static int udp_recv_send(int server_fd)
Signature · 1 line
2.28.0.236.gb10cc79966-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Stanislav Fomichev

I'm getting some garbage in bytes 8 and 9 when doing conversion from sockaddr_in to sockaddr_in6 (leftover from AF_INET?). Let's explicitly clear the higher bytes.

---
 tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +0 -0
index c571584c00f5..9ff0412e1fd3 100644

tools/testing/selftests/bpf/prog_tests/sk_lookup.c static void v4_to_v6(struct sockaddr_storage *ss) · line 315

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +1 -0
@@ -309,6 +309,7 @@ static void v4_to_v6(struct sockaddr_storage *ss)
 	v6->sin6_addr.s6_addr[10] = 0xff;
 	v6->sin6_addr.s6_addr[11] = 0xff;
 	memcpy(&v6->sin6_addr.s6_addr[12], &v4.sin_addr.s_addr, 4);
+	memset(&v6->sin6_addr.s6_addr[0], 0, 10);
 }
 
 static int udp_recv_send(int server_fd)

That was badly written. Sorry about that. And thanks for the fix.

I'd even zero out the whole thing:

        memset(v6, 0, sizeof(*v6));

... because right now IPv4 address is left as sin6_flowinfo. I can follow up with that change, unless you'd like to roll a v2.

Fixes 0ab5539f8584 ("selftests/bpf: Tests for BPF_SK_LOOKUP attach point")
#2 lore raw
Quoted 26 lines · Jakub Sitnicki
Quoted 14 lines · Stanislav Fomichev

I'm getting some garbage in bytes 8 and 9 when doing conversion from sockaddr_in to sockaddr_in6 (leftover from AF_INET?). Let's explicitly clear the higher bytes.

---
 tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +0 -0
index c571584c00f5..9ff0412e1fd3 100644

tools/testing/selftests/bpf/prog_tests/sk_lookup.c static void v4_to_v6(struct sockaddr_storage *ss) · line 315

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +1 -0
@@ -309,6 +309,7 @@ static void v4_to_v6(struct sockaddr_storage *ss)
      v6->sin6_addr.s6_addr[10] = 0xff;
      v6->sin6_addr.s6_addr[11] = 0xff;
      memcpy(&v6->sin6_addr.s6_addr[12], &v4.sin_addr.s_addr, 4);
+     memset(&v6->sin6_addr.s6_addr[0], 0, 10);
 }
 
 static int udp_recv_send(int server_fd)

That was badly written. Sorry about that. And thanks for the fix.

Quoted 6 lines

I'd even zero out the whole thing:

        memset(v6, 0, sizeof(*v6));

... because right now IPv4 address is left as sin6_flowinfo. I can follow up with that change, unless you'd like to roll a v2.

Up to you, but I'm not sure zeroing out the whole v6 portion is the best way forward.
IMO, it's a bit confusing when reading the code.
It will work, but only because v4 and v6 address portions don't really overlap :-/
I was thinking about adding new, on the stack sin6, fully initializing it and then doing memcpy into ss.
But I decided that adding memset is probably good enough :-)

#3 lore raw
Quoted 31 lines · Stanislav Fomichev
Quoted 30 lines · Jakub Sitnicki
Quoted 14 lines · Stanislav Fomichev

I'm getting some garbage in bytes 8 and 9 when doing conversion from sockaddr_in to sockaddr_in6 (leftover from AF_INET?). Let's explicitly clear the higher bytes.

---
 tools/testing/selftests/bpf/prog_tests/sk_lookup.c | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +0 -0
index c571584c00f5..9ff0412e1fd3 100644

tools/testing/selftests/bpf/prog_tests/sk_lookup.c static void v4_to_v6(struct sockaddr_storage *ss) · line 315

Quoted 7 lines
tools/testing/selftests/bpf/prog_tests/sk_lookup.c +1 -0
@@ -309,6 +309,7 @@ static void v4_to_v6(struct sockaddr_storage *ss)
      v6->sin6_addr.s6_addr[10] = 0xff;
      v6->sin6_addr.s6_addr[11] = 0xff;
      memcpy(&v6->sin6_addr.s6_addr[12], &v4.sin_addr.s_addr, 4);
+     memset(&v6->sin6_addr.s6_addr[0], 0, 10);
 }
 
 static int udp_recv_send(int server_fd)

That was badly written. Sorry about that. And thanks for the fix.

I'd even zero out the whole thing:

        memset(v6, 0, sizeof(*v6));
Quoted 7 lines
Quoted 2 lines

... because right now IPv4 address is left as sin6_flowinfo. I can follow up with that change, unless you'd like to roll a v2.

Up to you, but I'm not sure zeroing out the whole v6 portion is the best way forward.
IMO, it's a bit confusing when reading the code.
It will work, but only because v4 and v6 address portions don't really overlap :-/

It's not that hacky :-) We copy sockaddr_in bits before overwriting ss:

	struct sockaddr_in v4 = *(struct sockaddr_in *)ss;

It could be easier to read, perhaps by copying just the fields we need:

	struct sockaddr_in *v4 = (struct sockaddr_in *)ss;
	uint32_t addr = v4->sin_addr.saddr;
	in_port_t port = v4->sin_port;
Quoted 3 lines

I was thinking about adding new, on the stack sin6, fully initializing it and then doing memcpy into ss.
But I decided that adding memset is probably good enough :-)

Makes sense. Either way sounds good to me.