Skip to contentSkip to conversation

bpf: bpf_skc_to_* casting helpers require a NULL check on sk

PATCH Applied Martin KaFai Lau 4 participants 6 messages Sep 15, 2020 – Sep 16, 2020

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

PATCH 1/1 bpf: bpf_skc_to_* casting helpers require a NULL check on sk

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")
---
 net/core/filter.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
net/core/filter.c +7 -7
index 2d62c25e0395..23e8ded0ec97 100644
@@ -9522,7 +9522,7 @@ BPF_CALL_1(bpf_skc_to_tcp6_sock, struct sock *, sk)
 	 * trigger an explicit type generation here.
 	 */
 	BTF_TYPE_EMIT(struct tcp6_sock);
-	if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP &&
 	    sk->sk_family == AF_INET6)
 		return (unsigned long)sk;
 
@@ -9540,7 +9540,7 @@ const struct bpf_func_proto bpf_skc_to_tcp6_sock_proto = {
 
 BPF_CALL_1(bpf_skc_to_tcp_sock, struct sock *, sk)
 {
-	if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP)
 		return (unsigned long)sk;
 
 	return (unsigned long)NULL;
@@ -9558,12 +9558,12 @@ const struct bpf_func_proto bpf_skc_to_tcp_sock_proto = {
 BPF_CALL_1(bpf_skc_to_tcp_timewait_sock, struct sock *, sk)
 {
 #ifdef CONFIG_INET
-	if (sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
+	if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_TIME_WAIT)
 		return (unsigned long)sk;
 #endif
 
 #if IS_BUILTIN(CONFIG_IPV6)
-	if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
+	if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_TIME_WAIT)
 		return (unsigned long)sk;
 #endif
 
@@ -9582,12 +9582,12 @@ const struct bpf_func_proto bpf_skc_to_tcp_timewait_sock_proto = {
 BPF_CALL_1(bpf_skc_to_tcp_request_sock, struct sock *, sk)
 {
 #ifdef CONFIG_INET
-	if (sk->sk_prot == &tcp_prot  && sk->sk_state == TCP_NEW_SYN_RECV)
+	if (sk && sk->sk_prot == &tcp_prot && sk->sk_state == TCP_NEW_SYN_RECV)
 		return (unsigned long)sk;
 #endif
 
 #if IS_BUILTIN(CONFIG_IPV6)
-	if (sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
+	if (sk && sk->sk_prot == &tcpv6_prot && sk->sk_state == TCP_NEW_SYN_RECV)
 		return (unsigned long)sk;
 #endif
 
@@ -9609,7 +9609,7 @@ BPF_CALL_1(bpf_skc_to_udp6_sock, struct sock *, sk)
 	 * trigger an explicit type generation here.
 	 */
 	BTF_TYPE_EMIT(struct udp6_sock);
-	if (sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
+	if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_UDP &&
 	    sk->sk_type == SOCK_DGRAM && sk->sk_family == AF_INET6)
 		return (unsigned long)sk;
 
Signature · 1 line
2.24.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Martin KaFai Lau

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")

Thanks for the fix!

#2 lore raw
Quoted 8 lines · Yonghong Song
Quoted 5 lines · Martin KaFai Lau

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Quoted 8 lines
Quoted 4 lines
Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")

Thanks for the fix!

Thanks for the review. A follow up question.
Is PTR_TO_BTF_ID_OR_NULL still needed?

#3 lore raw
Quoted 14 lines · Martin KaFai Lau
Quoted 13 lines · Yonghong Song
Quoted 9 lines · Martin KaFai Lau

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")
Quoted 5 lines
Quoted 3 lines

Thanks for the fix!

Thanks for the review. A follow up question.
Is PTR_TO_BTF_ID_OR_NULL still needed?

I would still like to preserve PTR_TO_BTF_ID_OR_NULL vs. PTR_TO_BTF_ID difference for ctx arguments. PTR_TO_BTF_ID_OR_NULL means a NULL check is required and PTR_TO_BTF_ID means NULL check is not required.

reviewer Acked-by on v1 1/1
#4 lore raw
In reply to Martin KaFai Lau
Quoted 10 lines · Martin KaFai Lau

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")
#5 lore raw
Quoted 8 lines · Song Liu
Quoted 6 lines · Martin KaFai Lau

The bpf_skc_to_* type casting helpers are available to BPF_PROG_TYPE_TRACING. The traced PTR_TO_BTF_ID may be NULL. For example, the skb->sk may be NULL. Thus, these casting helpers need to check "!sk" also and this patch fixes them.

Quoted 6 lines
Quoted 4 lines
Fixes 0d4fad3e57df ("bpf: Add bpf_skc_to_udp6_sock() helper")
Fixes 478cfbdf5f13 ("bpf: Add bpf_skc_to_{tcp, tcp_timewait, tcp_request}_sock() helpers")
Fixes af7ec1383361 ("bpf: Add bpf_skc_to_tcp6_sock() helper")

Applied. Thanks