bpf: getsockopt hook to get optval without checking kernel retval
PATCH 1/1 bpf: getsockopt hook to get optval without checking kernel retval
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the nf_register_sockopt to register.
--- kernel/bpf/cgroup.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)
kernel/bpf/cgroup.c +13 -22
@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,if (max_optlen <0 )return max_optlen; -if (!retval) { -/* If kernel getsockopt finished successfully, -* copy whatever was returned to the user back -* into our temporary buffer. Set optlen to the -* one that kernel returned as well to let -* BPF programs inspect the value. -*/ - -if (get_user (ctx.optlen, optlen)) { - ret = -EFAULT ; - goto out; - } +if (get_user (ctx.optlen, optlen)) { + ret = -EFAULT ; + goto out; + } -if (ctx.optlen <0 ) { - ret = -EFAULT ; - goto out; - } - orig_optlen = ctx.optlen; +if (ctx.optlen <0 ) { + ret = -EFAULT ; + goto out; + } + orig_optlen = ctx.optlen; -if (copy_from_user (ctx.optval, optval, -min (ctx.optlen, max_optlen)) !=0 ) { - ret = -EFAULT ; - goto out; - } +if (copy_from_user (ctx.optval, optval, +min (ctx.optlen, max_optlen)) !=0 ) { + ret = -EFAULT ; + goto out; }lock_sock (sk);
Signature · 1 line
2.20.1
Martin KaFai Lau
reviewerQuoted 45 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>--- kernel/bpf/cgroup.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +7
-17@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level,if (max_optlen <0 )return max_optlen; -if (!retval) { -/* If kernel getsockopt finished successfully, -* copy whatever was returned to the user back -* into our temporary buffer. Set optlen to the -* one that kernel returned as well to let -* BPF programs inspect the value. -*/ - -if (get_user (ctx.optlen, optlen)) { - ret = -EFAULT ; - goto out; - } +if (get_user (ctx.optlen, optlen)) { + ret = -EFAULT ; + goto out; + } -if (ctx.optlen <0 ) { - ret = -EFAULT ; - goto out; - } - orig_optlen = ctx.optlen; +if (ctx.optlen <0 ) { + ret = -EFAULT ; + goto out;
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, · line 1911
Quoted 10 lines
kernel/bpf/cgroup.c +4
-5+ } + orig_optlen = ctx.optlen; -if (copy_from_user (ctx.optval, optval, -min (ctx.optlen, max_optlen)) !=0 ) { - ret = -EFAULT ; - goto out; - } +if (copy_from_user (ctx.optval, optval, +min (ctx.optlen, max_optlen)) !=0 ) {
What is in optval that is useful to copy from if the kernel didn't handle the optname?
and there is no selftest also.
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT ; + goto out; }lock_sock (sk);
Quoted 47 lines · Martin KaFai Lau
Quoted 46 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>---
kernel/bpf/cgroup.c| 35 +++++++++++++----------------------1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +9
-17@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (max_optlen < 0) return max_optlen; - if (!retval) { - /* If kernel getsockopt finished successfully, - * copy whatever was returned to the user back - * into our temporary buffer. Set optlen to the - * one that kernel returned as well to let - * BPF programs inspect the value. - */ - - if (get_user(ctx.optlen, optlen)) { - ret = -EFAULT; - goto out; - } + if (get_user(ctx.optlen, optlen)) { + ret = -EFAULT; + goto out; + } - if (ctx.optlen < 0) { - ret = -EFAULT; - goto out; - } - orig_optlen = ctx.optlen; + if (ctx.optlen < 0) { + ret = -EFAULT; + goto out; + } + orig_optlen = ctx.optlen;
Quoted 8 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, · line 1911
Quoted 7 lines
kernel/bpf/cgroup.c +2
-5- if (copy_from_user(ctx.optval, optval, - min(ctx.optlen, max_optlen)) != 0) { - ret = -EFAULT; - goto out; - } + if (copy_from_user(ctx.optval, optval, + min(ctx.optlen, max_optlen)) != 0) {What is in optval that is useful to copy from if the kernel didn't handle the optname?
For example, if the user customizes a new optname, it will not be processed if the kernel does not support it. Then the data stored in optval is the data put by the user. If this part can be seen by bpf prog, the user can implement processing logic of the custom optname through bpf prog.
Quoted 3 lines
and there is no selftest also.
Yes, if remove this restriction, everyone thinks it's ok, I'll add it in the next version.
Quoted 5 lines
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT; + goto out; } lock_sock(sk);
Martin KaFai Lau
reviewerQuoted 57 lines · Feng Zhou
Quoted 47 lines · Martin KaFai Lau
Quoted 46 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>---
kernel/bpf/cgroup.c| 35 +++++++++++++----------------------1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +9
-17@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (max_optlen < 0) return max_optlen; - if (!retval) { - /* If kernel getsockopt finished successfully, - * copy whatever was returned to the user back - * into our temporary buffer. Set optlen to the - * one that kernel returned as well to let - * BPF programs inspect the value. - */ - - if (get_user(ctx.optlen, optlen)) { - ret = -EFAULT; - goto out; - } + if (get_user(ctx.optlen, optlen)) { + ret = -EFAULT; + goto out; + } - if (ctx.optlen < 0) { - ret = -EFAULT; - goto out; - } - orig_optlen = ctx.optlen; + if (ctx.optlen < 0) { + ret = -EFAULT; + goto out; + } + orig_optlen = ctx.optlen;Quoted 8 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, · line 1911
Quoted 7 lines
kernel/bpf/cgroup.c +2
-5- if (copy_from_user(ctx.optval, optval, - min(ctx.optlen, max_optlen)) != 0) { - ret = -EFAULT; - goto out; - } + if (copy_from_user(ctx.optval, optval, + min(ctx.optlen, max_optlen)) != 0) {What is in optval that is useful to copy from if the kernel didn't handle the optname?
Quoted 5 lines
For example, if the user customizes a new optname, it will not be processed if the kernel does not support it. Then the data stored in optval is the data put
by the user. If this part can be seen by bpf prog, the user can implement processing logic of the custom optname through bpf prog.
This part does not make sense. It is a (get)sockopt. Why the bpf prog should expect anything useful in the original __user optval? Other than unnecessary copy for other common cases, it looks like a bad api, so consider it a NAK.
Quoted 13 lines
Quoted 4 lines
and there is no selftest also.
Yes, if remove this restriction, everyone thinks it's ok, I'll add it in the next version.
Quoted 5 lines
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT; + goto out; } lock_sock(sk);
Quoted 58 lines · Martin KaFai Lau
Quoted 57 lines · Feng Zhou
Quoted 55 lines · Martin KaFai Lau
Quoted 43 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>---
kernel/bpf/cgroup.c| 35 +++++++++++++----------------------1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +6
-17@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (max_optlen < 0) return max_optlen; - if (!retval) { - /* If kernel getsockopt finished successfully, - * copy whatever was returned to the user back - * into our temporary buffer. Set optlen to the - * one that kernel returned as well to let - * BPF programs inspect the value. - */ - - if (get_user(ctx.optlen, optlen)) { - ret = -EFAULT; - goto out; - } + if (get_user(ctx.optlen, optlen)) { + ret = -EFAULT; + goto out; + } - if (ctx.optlen < 0) { - ret = -EFAULT; - goto out; - } - orig_optlen = ctx.optlen; + if (ctx.optlen < 0) { + ret = -EFAULT;kernel/bpf/cgroup.c int · line 1909
Quoted 10 lines
kernel/bpf/cgroup.c +5
-5+ goto out; + } + orig_optlen = ctx.optlen; - if (copy_from_user(ctx.optval, optval, - min(ctx.optlen, max_optlen)) != 0) { - ret = -EFAULT; - goto out; - } + if (copy_from_user(ctx.optval, optval, + min(ctx.optlen, max_optlen)) != 0) {What is in optval that is useful to copy from if the kernel didn't handle the optname?
Quoted 22 lines
Quoted 5 lines
For example, if the user customizes a new optname, it will not be processed if the kernel does not support it. Then the data stored in optval is the data put
by the user. If this part can be seen by bpf prog, the user can implement processing logic of the custom optname through bpf prog.
This part does not make sense. It is a (get)sockopt. Why the bpf prog should expect anything useful in the original
__useroptval? Other than unnecessary copy for other common cases, it looks like a bad api, so consider it a NAK.Quoted 13 lines
Quoted 4 lines
and there is no selftest also.
Yes, if remove this restriction, everyone thinks it's ok, I'll add it in the next version.
Quoted 5 lines
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT; + goto out; } lock_sock(sk);
According to my understanding, users will have such requirements, customize an optname, which is not available in the kernel. All logic is completed in bpf prog, and bpf prog needs to obtain the user data passed in by the system call, and then return the data required by the user according to this data.
For optname not in the kernel, the error code is #define ENOPROTOOPT 92/* Protocol not available */ Whether to consider the way of judging with error codes, If (! retval | | retval == -ENOPROTOOPT)
Quoted 97 lines · Feng Zhou
Quoted 65 lines · Martin KaFai Lau
Quoted 61 lines · Feng Zhou
Quoted 48 lines · Martin KaFai Lau
Quoted 47 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>--- kernel/bpf/cgroup.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +9
-18@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (max_optlen < 0) return max_optlen; -if (!retval) { -/* If kernel getsockopt finished successfully, -* copy whatever was returned to the user back -* into our temporary buffer. Set optlen to the -* one that kernel returned as well to let -* BPF programs inspect the value. -*/ - -if (get_user (ctx.optlen, optlen)) { - ret = -EFAULT ; - goto out; - } +if (get_user (ctx.optlen, optlen)) { + ret = -EFAULT ; + goto out; + } -if (ctx.optlen <0 ) { - ret = -EFAULT ; - goto out; - } - orig_optlen = ctx.optlen; +if (ctx.optlen <0 ) { + ret = -EFAULT ; + goto out; + } + orig_optlen = ctx.optlen; - if (copy_from_user(ctx.optval, optval,Quoted 8 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, · line 1911
Quoted 6 lines
kernel/bpf/cgroup.c +2
-4- min(ctx.optlen, max_optlen)) != 0) { - ret = -EFAULT ; - goto out; - } +if (copy_from_user (ctx.optval, optval, +min (ctx.optlen, max_optlen)) !=0 ) {What is in optval that is useful to copy from if the kernel didn't handle the optname?
For example, if the user customizes a new optname, it will not be processed if the kernel does not support it. Then the data stored in optval is the data put
Quoted 23 lines
Quoted 2 lines
by the user. If this part can be seen by bpf prog, the user can implement processing logic of the custom optname through bpf prog.
This part does not make sense. It is a (get)sockopt. Why the bpf prog should expect anything useful in the original
__useroptval? Other than unnecessary copy for other common cases, it looks like a bad api, so consider it a NAK.Quoted 14 lines
Quoted 4 lines
and there is no selftest also.
Yes, if remove this restriction, everyone thinks it's ok, I'll add it in the next version.
Quoted 5 lines
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT ; + goto out; }lock_sock (sk);According to my understanding, users will have such requirements, customize an optname, which is not available in the kernel. All logic is completed in bpf prog, and bpf prog needs to obtain the user data passed in by the system call, and then return the data required by the user according to this data.
Quoted 4 lines
For optname not in the kernel, the error code is #define ENOPROTOOPT 92/* Protocol not available */ Whether to consider the way of judging with error codes, If (! retval | | retval ==
-ENOPROTOOPT)
I'm also failing to see what you're trying to do here. You can already implement custom optnames via getsockopt, so what's missing? If you need to pass some data from the userspace to the hook, then setsockopt hook will serve you better.
getsockopt is about reading something from the kernel/bpf; ignoring initial user buffer value is somewhat implied here.
Quoted 103 lines · Stanislav Fomichev
Quoted 97 lines · Feng Zhou
Quoted 65 lines · Martin KaFai Lau
Quoted 61 lines · Feng Zhou
Quoted 48 lines · Martin KaFai Lau
Quoted 47 lines · Feng zhou
From: Feng Zhou <[email protected]>
Remove the judgment on retval and pass bpf ctx by default. The advantage of this is that it is more flexible. Bpf getsockopt can support the new optname without using the module to call the
nf_register_sockoptto register.Signed-off-by Feng Zhou <[email protected]>--- kernel/bpf/cgroup.c | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-)kernel/bpf/cgroup.c +9
-18@@ -1896,30 +1896,21 @@ int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, if (max_optlen < 0) return max_optlen; -if (!retval) { -/* If kernel getsockopt finished successfully, -* copy whatever was returned to the user back -* into our temporary buffer. Set optlen to the -* one that kernel returned as well to let -* BPF programs inspect the value. -*/ - -if (get_user (ctx.optlen, optlen)) { - ret = -EFAULT ; - goto out; - } +if (get_user (ctx.optlen, optlen)) { + ret = -EFAULT ; + goto out; + } -if (ctx.optlen <0 ) { - ret = -EFAULT ; - goto out; - } - orig_optlen = ctx.optlen; +if (ctx.optlen <0 ) { + ret = -EFAULT ; + goto out; + } + orig_optlen = ctx.optlen; - if (copy_from_user(ctx.optval, optval,Quoted 8 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_getsockopt(struct sock *sk, int level, · line 1911
Quoted 6 lines
kernel/bpf/cgroup.c +2
-4- min(ctx.optlen, max_optlen)) != 0) { - ret = -EFAULT ; - goto out; - } +if (copy_from_user (ctx.optval, optval, +min (ctx.optlen, max_optlen)) !=0 ) {What is in optval that is useful to copy from if the kernel didn't handle the optname?
For example, if the user customizes a new optname, it will not be processed if the kernel does not support it. Then the data stored in optval is the data put
Quoted 23 lines
Quoted 2 lines
by the user. If this part can be seen by bpf prog, the user can implement processing logic of the custom optname through bpf prog.
This part does not make sense. It is a (get)sockopt. Why the bpf prog should expect anything useful in the original
__useroptval? Other than unnecessary copy for other common cases, it looks like a bad api, so consider it a NAK.Quoted 14 lines
Quoted 4 lines
and there is no selftest also.
Yes, if remove this restriction, everyone thinks it's ok, I'll add it in the next version.
Quoted 5 lines
Quoted 4 lines
kernel/bpf/cgroup.c +2
-0+ ret = -EFAULT ; + goto out; }lock_sock (sk);According to my understanding, users will have such requirements, customize an optname, which is not available in the kernel. All logic is completed in bpf prog, and bpf prog needs to obtain the user data passed in by the system call, and then return the data required by the user according to this data.
Quoted 4 lines
For optname not in the kernel, the error code is #define ENOPROTOOPT 92/* Protocol not available */ Whether to consider the way of judging with error codes, If (! retval | | retval ==
-ENOPROTOOPT)
Quoted 6 lines
I'm also failing to see what you're trying to do here. You can already implement custom optnames via getsockopt, so what's missing? If you need to pass some data from the userspace to the hook, then setsockopt hook will serve you better.
getsockopt is about reading something from the kernel/bpf; ignoring initial user buffer value is somewhat implied here.
Thanks, you reminded me that can pass data to bpf prog by setsockopt.