Skip to contentSkip to conversation

potential sockmap memleak and proc stats fix

PATCH Superseded John Fastabend 3 participants 8 messages Jul 6, 2021 – Jul 12, 2021

Subsystems (6)
Networking Network drivers BPF TCP +2 more subsystems

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

John Fastabend

author maintainer
#0 lore raw

v3 cover potential sockmap memleak and proc stats fix

While investigating a memleak in sockmap I found these two issues. Patch 1 found doing code review, I wasn't able to get KASAN to trigger a memleak here, but should be necessary. Patch 2 fixes proc stats so when we use sockstats for debugging we get correct values.

The fix for observered memleak will come after these, but requires some more discussion and potentially patch revert so I'll try to get the set here going now.

John Fastabend (2):
bpf, sockmap: fix potential memory leak on unlikely error case bpf, sockmap: sk_prot needs inuse_idx set for proc stats

 net/core/skmsg.c    | 10 ++++++----
 net/core/sock_map.c | 11 ++++++++++-
 2 files changed, 16 insertions(+), 5 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v3 1/2 bpf, sockmap: fix potential memory leak on unlikely error case

If skb_linearize is needed and fails we could leak a msg on the error handling. To fix ensure we kfree the msg block before returning error. Found during code review.

Fixes 4363023d2668e ("bpf, sockmap: Avoid failures from skb_to_sgvec when skb has frag_list")
---
 net/core/skmsg.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
net/core/skmsg.c +6 -4
index 9b6160a191f8..1a9059e5b3b3 100644
@@ -508,10 +508,8 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
 	if (skb_linearize(skb))
 		return -EAGAIN;
 	num_sge = skb_to_sgvec(skb, msg->sg.data, 0, skb->len);
-	if (unlikely(num_sge < 0)) {
-		kfree(msg);
+	if (unlikely(num_sge < 0))
 		return num_sge;
-	}
 
 	copied = skb->len;
 	msg->sg.start = 0;
@@ -530,6 +528,7 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
 {
 	struct sock *sk = psock->sk;
 	struct sk_msg *msg;
+	int err;
 
 	/* If we are receiving on the same sock skb->sk is already assigned,
 	 * skip memory accounting and owner transition seeing it already set
@@ -548,7 +547,10 @@ static int sk_psock_skb_ingress(struct sk_psock *psock, struct sk_buff *skb)
 	 * into user buffers.
 	 */
 	skb_set_owner_r(skb, sk);
-	return sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
+	err = sk_psock_skb_ingress_enqueue(skb, psock, sk, msg);
+	if (err < 0)
+		kfree(msg);
+	return err;
 }
 
 /* Puts an skb on the ingress queue of the socket already assigned to the
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH bpf v3 1/2] bpf, sockmap: fix potential memory leak on unlikely error case

Quoted 4 lines · John Fastabend

If skb_linearize is needed and fails we could leak a msg on the error handling. To fix ensure we kfree the msg block before returning error. Found during code review.

sk_psock_skb_ingress_self() also needs the same fix, right? Other than this, it looks good to me.

Thanks.

author maintainer
#3 lore raw

Re: [PATCH bpf v3 1/2] bpf, sockmap: fix potential memory leak on unlikely error case

Quoted 7 lines · Cong Wang
Quoted 4 lines · John Fastabend

If skb_linearize is needed and fails we could leak a msg on the error handling. To fix ensure we kfree the msg block before returning error. Found during code review.

sk_psock_skb_ingress_self() also needs the same fix, right?

Yep.

Quoted 1 line

Other than this, it looks good to me.

I'll do another spin to get the other one as well. Mind as well fix both cases at once.

Quoted 2 lines

Thanks.

author maintainer
#4 lore raw

PATCH v3 2/2 bpf, sockmap: sk_prot needs inuse_idx set for proc stats

In reply to John Fastabend

Proc socket stats use sk_prot->inuse_idx value to record inuse sock stats. We currently do not set this correctly from sockmap side. The result is reading sock stats '/proc/net/sockstat' gives incorrect values. The socket counter is incremented correctly, but because we don't set the counter correctly when we replace sk_prot we may omit the decrement.

Fixes 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface")
---
 net/core/sock_map.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
net/core/sock_map.c +10 -1
index 60decd6420ca..27bdf768aa8c 100644
@@ -185,10 +185,19 @@ static void sock_map_unref(struct sock *sk, void *link_raw)
 
 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
 {
+	int err;
+#ifdef CONFIG_PROC_FS
+	int idx = sk->sk_prot->inuse_idx;
+#endif
 	if (!sk->sk_prot->psock_update_sk_prot)
 		return -EINVAL;
 	psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
-	return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+	err = sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+#ifdef CONFIG_PROC_FS
+	if (!err)
+		sk->sk_prot->inuse_idx = idx;
+#endif
+	return err;
 }
 
 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)
Signature · 1 line
2.25.1
#5 lore raw

Re: [PATCH bpf v3 2/2] bpf, sockmap: sk_prot needs inuse_idx set for proc stats

Quoted 18 lines · John Fastabend

Proc socket stats use sk_prot->inuse_idx value to record inuse sock stats. We currently do not set this correctly from sockmap side. The result is reading sock stats '/proc/net/sockstat' gives incorrect values. The socket counter is incremented correctly, but because we don't set the counter correctly when we replace sk_prot we may omit the decrement.

Fixes 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface")
---
 net/core/sock_map.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
net/core/sock_map.c +0 -0
index 60decd6420ca..27bdf768aa8c 100644

net/core/sock_map.c static void sock_map_unref(struct sock *sk, void *link_raw) · line 191

Quoted 7 lines
net/core/sock_map.c +4 -0
@@ -185,10 +185,19 @@ static void sock_map_unref(struct sock *sk, void *link_raw)
 
 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
 {
+       int err;
+#ifdef CONFIG_PROC_FS
+       int idx = sk->sk_prot->inuse_idx;
+#endif

A nit: Reverse XMAS tree declaration style is preferred for networking subsystem.

Thanks.

maintainer
#6 lore raw

Re: [PATCH bpf v3 2/2] bpf, sockmap: sk_prot needs inuse_idx set for proc stats

In reply to John Fastabend
Quoted 27 lines · John Fastabend

Proc socket stats use sk_prot->inuse_idx value to record inuse sock stats. We currently do not set this correctly from sockmap side. The result is reading sock stats '/proc/net/sockstat' gives incorrect values. The socket counter is incremented correctly, but because we don't set the counter correctly when we replace sk_prot we may omit the decrement.

Fixes 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface")
---
 net/core/sock_map.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
net/core/sock_map.c +4 -0
index 60decd6420ca..27bdf768aa8c 100644
@@ -185,10 +185,19 @@ static void sock_map_unref(struct sock *sk, void *link_raw)
 
 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
 {
+	int err;
+#ifdef CONFIG_PROC_FS
+	int idx = sk->sk_prot->inuse_idx;
+#endif
 	if (!sk->sk_prot->psock_update_sk_prot)
 		return -EINVAL;
 	psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;

net/core/sock_map.c static void sock_map_unref(struct sock *sk, void *link_raw) · line 203

Quoted 10 lines
net/core/sock_map.c +6 -1
-	return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+	err = sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+#ifdef CONFIG_PROC_FS
+	if (!err)
+		sk->sk_prot->inuse_idx = idx;
+#endif
+	return err;
 }
 
 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)

We could initialize inuse_idx just once in {tcp,udp}_bpf_rebuild_protos, if we changed {tcp,udp}_bpf_v4_build_proto to be a late_initcall, so that it runs after inet_init when {tcp,udp}_prot and udp_prot are already registered and have inuse_idx assigned.

author maintainer
#7 lore raw

Re: [PATCH bpf v3 2/2] bpf, sockmap: sk_prot needs inuse_idx set for proc stats

Quoted 37 lines · Jakub Sitnicki
Quoted 36 lines · John Fastabend

Proc socket stats use sk_prot->inuse_idx value to record inuse sock stats. We currently do not set this correctly from sockmap side. The result is reading sock stats '/proc/net/sockstat' gives incorrect values. The socket counter is incremented correctly, but because we don't set the counter correctly when we replace sk_prot we may omit the decrement.

Fixes 604326b41a6fb ("bpf, sockmap: convert to generic sk_msg interface")
---
 net/core/sock_map.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
net/core/sock_map.c +10 -1
index 60decd6420ca..27bdf768aa8c 100644
@@ -185,10 +185,19 @@ static void sock_map_unref(struct sock *sk, void *link_raw)
 
 static int sock_map_init_proto(struct sock *sk, struct sk_psock *psock)
 {
+	int err;
+#ifdef CONFIG_PROC_FS
+	int idx = sk->sk_prot->inuse_idx;
+#endif
 	if (!sk->sk_prot->psock_update_sk_prot)
 		return -EINVAL;
 	psock->psock_update_sk_prot = sk->sk_prot->psock_update_sk_prot;
-	return sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+	err = sk->sk_prot->psock_update_sk_prot(sk, psock, false);
+#ifdef CONFIG_PROC_FS
+	if (!err)
+		sk->sk_prot->inuse_idx = idx;
+#endif
+	return err;
 }
 
Quoted 6 lines

net/core/sock_map.c static void sock_map_unref(struct sock *sk, void *link_raw) · line 203

Quoted 1 line
net/core/sock_map.c +0 -0
 static struct sk_psock *sock_map_psock_get_checked(struct sock *sk)

We could initialize inuse_idx just once in {tcp,udp}_bpf_rebuild_protos, if we changed {tcp,udp}_bpf_v4_build_proto to be a late_initcall, so that it runs after inet_init when {tcp,udp}_prot and udp_prot are already registered and have inuse_idx assigned.

OK does seem slightly nicer. Then I guess the diff is just,

net/ipv4/tcp_bpf.c +1 -1
index f26916a62f25..d3e9386b493e 100644
@@ -503,7 +503,7 @@ static int __init tcp_bpf_v4_build_proto(void)
        tcp_bpf_rebuild_protos(tcp_bpf_prots[TCP_BPF_IPV4], &tcp_prot);
        return 0;
 }
-core_initcall(tcp_bpf_v4_build_proto);
+late_initcall(tcp_bpf_v4_build_proto);
 
 static int tcp_bpf_assert_proto_ops(struct proto *ops)
 {