Skip to contentSkip to conversation

bpf: don't leak memory in bpf getsockopt when optlen == 0

PATCH Applied Stanislav Fomichev 3 participants 3 messages Jan 12, 2021

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

PATCH v2 1/1 bpf: don't leak memory in bpf getsockopt when optlen == 0

optlen == 0 indicates that the kernel should ignore BPF buffer and use the original one from the user. We, however, forget to free the temporary buffer that we've allocated for BPF.

Fixes d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE")
---
 kernel/bpf/cgroup.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/cgroup.c +3 -2
index 6ec088a96302..96555a8a2c54 100644
@@ -1391,12 +1391,13 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,
 		if (ctx.optlen != 0) {
 			*optlen = ctx.optlen;
 			*kernel_optval = ctx.optval;
+			/* export and don't free sockopt buf */
+			return 0;
 		}
 	}
 
 out:
-	if (ret)
-		sockopt_free_buf(&ctx);
+	sockopt_free_buf(&ctx);
 	return ret;
 }
 
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Acked-by on v2 1/1
#1 lore raw
Quoted 3 lines · Stanislav Fomichev

optlen == 0 indicates that the kernel should ignore BPF buffer and use the original one from the user. We, however, forget to free the temporary buffer that we've allocated for BPF.

#2 lore raw
In reply to Stanislav Fomichev
This patch was applied to bpf/bpf.git (refs/heads/master):

Hello:

This patch was applied to bpf/bpf.git (refs/heads/master):

Quoted 9 lines · On Tue, 12 Jan 2021 08:28:29 -0800 you wrote:

optlen == 0 indicates that the kernel should ignore BPF buffer and use the original one from the user. We, however, forget to free the temporary buffer that we've allocated for BPF.

Fixes d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE")

[...]

Here is the summary with links:
- [bpf,v2] bpf: don't leak memory in bpf getsockopt when optlen == 0

    https://git.kernel.org/bpf/bpf/c/4be34f3d0731

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html