bpf: don't leak memory in bpf getsockopt when optlen == 0
Subsystems (3)
PATCH 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.
--- kernel/bpf/cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/cgroup.c +1 -1
@@ -1395,7 +1395,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, } out: -if (ret) +if (*kernel_optval ==NULL )sockopt_free_buf (&ctx);return ret; }
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
Quoted 16 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.
Reported-by Martin KaFai Lau <[email protected]>Fixes d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE")Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/cgroup.c +0
-0
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1398
Quoted 5 lines
kernel/bpf/cgroup.c +1
-1@@ -1395,7 +1395,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, } out: -if (ret) +if (*kernel_optval ==NULL )
It seems fragile to depend on the caller to init *kernel_optval to NULL.
How about something like:
kernel/bpf/cgroup.c +8 -6
@@ -1358,7 +1358,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (copy_from_user (ctx.optval, optval,min (*optlen, max_optlen)) !=0 ) { ret = -EFAULT ; - goto out; + goto err_out; }lock_sock (sk);
@@ -1368,7 +1368,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (!ret) { ret = -EPERM ; - goto out; + goto err_out; }if (ctx.optlen ==-1 ) {
@@ -1379,7 +1379,6 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, ret = -EFAULT ; }else {/* optlen within bounds, run kernel handler */ - ret =0 ;/* export any potential modifications */ *level = ctx.level;
@@ -1391,12 +1390,15 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (ctx.optlen !=0 ) { *optlen = ctx.optlen; *kernel_optval = ctx.optval; + }else { +sockopt_free_buf (&ctx); } + +return 0 ; } -out: -if (ret) -sockopt_free_buf (&ctx); +err_out: +sockopt_free_buf (&ctx);return ret; }
Quoted 20 lines · Martin KaFai Lau
Quoted 18 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.
Reported-by Martin KaFai Lau <[email protected]>Fixes d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE")Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/cgroup.c +0
-0@@ -1395,7 +1395,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, }
Quoted 4 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1398
Quoted 3 lines
kernel/bpf/cgroup.c +1
-1out: -if (ret) +if (*kernel_optval ==NULL )It seems fragile to depend on the caller to init
*kernel_optvalto NULL.
We can manually reset it to NULL when we enter __cgroup_bpf_run_filter_setsockopt,
I didn't bother since there is only one existing caller.
But you patch also LGTM, I don't really have a preference.
Quoted 50 lines
How about something like:
kernel/bpf/cgroup.c +8
-6@@ -1358,7 +1358,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (copy_from_user (ctx.optval, optval,min (*optlen, max_optlen)) !=0 ) { ret = -EFAULT ; - goto out; + goto err_out; }lock_sock (sk);@@ -1368,7 +1368,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (!ret) { ret = -EPERM ; - goto out; + goto err_out; }if (ctx.optlen ==-1 ) {@@ -1379,7 +1379,6 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, ret = -EFAULT ; }else {/* optlen within bounds, run kernel handler */ - ret =0 ;/* export any potential modifications */ *level = ctx.level;@@ -1391,12 +1390,15 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (ctx.optlen !=0 ) { *optlen = ctx.optlen; *kernel_optval = ctx.optval; + }else { +sockopt_free_buf (&ctx); } + +return 0 ; } -out: -if (ret) -sockopt_free_buf (&ctx); +err_out: +sockopt_free_buf (&ctx);return ret; }
Quoted 21 lines · Stanislav Fomichev
Quoted 20 lines · Martin KaFai Lau
Quoted 18 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.
Reported-by Martin KaFai Lau <[email protected]>Fixes d8fe449a9c51 ("bpf: Don't return EINVAL from {get,set}sockopt when optlen > PAGE_SIZE")Signed-off-by Stanislav Fomichev <[email protected]>--- kernel/bpf/cgroup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/cgroup.c +0
-0@@ -1395,7 +1395,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, }
Quoted 6 lines
Quoted 4 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, · line 1398
Quoted 3 lines
kernel/bpf/cgroup.c +1
-1out: -if (ret) +if (*kernel_optval ==NULL )It seems fragile to depend on the caller to init
*kernel_optvalto NULL.We can manually reset it to NULL when we enter
__cgroup_bpf_run_filter_setsockopt,
It feels weird to reset the caller value at the beginning while this is not intended to be an _init() like function, so I avoided it.
but yeah, I am fine on this way also and won't oppose it strongly ;)
Quoted 54 lines
I didn't bother since there is only one existing caller.
But you patch also LGTM, I don't really have a preference.
Quoted 50 lines
How about something like:
kernel/bpf/cgroup.c +8
-6@@ -1358,7 +1358,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (copy_from_user (ctx.optval, optval,min (*optlen, max_optlen)) !=0 ) { ret = -EFAULT ; - goto out; + goto err_out; }lock_sock (sk);@@ -1368,7 +1368,7 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (!ret) { ret = -EPERM ; - goto out; + goto err_out; }if (ctx.optlen ==-1 ) {@@ -1379,7 +1379,6 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level, ret = -EFAULT ; }else {/* optlen within bounds, run kernel handler */ - ret =0 ;/* export any potential modifications */ *level = ctx.level;@@ -1391,12 +1390,15 @@ int __cgroup_bpf_run_filter_setsockopt(struct sock *sk, int *level,if (ctx.optlen !=0 ) { *optlen = ctx.optlen; *kernel_optval = ctx.optval; + }else { +sockopt_free_buf (&ctx); } + +return 0 ; } -out: -if (ret) -sockopt_free_buf (&ctx); +err_out: +sockopt_free_buf (&ctx);return ret; }