Re: [PATCH net v4] bpf, net: Check cgroup_bpf_enabled() only once in do_sock_getsockopt()
From: Tze-nan Wu (吳澤南) <hidden>
Date: 2024-08-22 03:16:16
Also in:
bpf, linux-mediatek, lkml, netdev
On Wed, 2024-08-21 at 14:01 -0700, Alexei Starovoitov wrote:
External email : Please do not click links or open attachments until you have verified the sender or the content. On Wed, Aug 21, 2024 at 2:30 AM Tze-nan Wu [off-list ref] wrote:quoted
The return value from `cgroup_bpf_enabled(CGROUP_GETSOCKOPT)` canchangequoted
between the invocations of `BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN` and `BPF_CGROUP_RUN_PROG_GETSOCKOPT`. If `cgroup_bpf_enabled(CGROUP_GETSOCKOPT)` changes from "false" to "true" between the invocations of`BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN` andquoted
`BPF_CGROUP_RUN_PROG_GETSOCKOPT`, `BPF_CGROUP_RUN_PROG_GETSOCKOPT`willquoted
receive an -EFAULT from`__cgroup_bpf_run_filter_getsockopt(max_optlen=0)`quoted
due to `get_user()` was not reached in`BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN`.quoted
Scenario shown as below: `process A` `process B` ----------- ------------ BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN enableCGROUP_GETSOCKOPTquoted
BPF_CGROUP_RUN_PROG_GETSOCKOPT (-EFAULT) To prevent this, invoke `cgroup_bpf_enabled()` only once and cachethequoted
result in a newly added local variable `enabled`. Both `BPF_CGROUP_*` macros in `do_sock_getsockopt` will then checktheirquoted
condition using the same `enabled` variable as the conditionvariable,quoted
instead of using the return values from `cgroup_bpf_enabled` calledbyquoted
themselves as the condition variable(which could yield differentresults).quoted
This ensures that either both `BPF_CGROUP_*` macros pass theconditionquoted
or neither does. Fixes: 0d01da6afc54 ("bpf: implement getsockopt and setsockopthooks")quoted
Co-developed-by: Yanghui Li <redacted> Signed-off-by: Yanghui Li <redacted> Co-developed-by: Cheng-Jui Wang <redacted> Signed-off-by: Cheng-Jui Wang <redacted> Signed-off-by: Tze-nan Wu <redacted> --- Chagnes from v1 to v2:https://lore.kernel.org/all/20240819082513.27176-1-Tze-nan.Wu@mediatek.com/ (local)quoted
Instead of using cgroup_lock in the fastpath, invokecgroup_bpf_enabledquoted
only once and cache the value in the newly added variable`enabled`.quoted
`BPF_CGROUP_*` macros in do_sock_getsockopt can then both checktheirquoted
condition with the new variable `enable`, ensuring that eitherthey bothquoted
passing the condition or both do not. Chagnes from v2 to v3:https://lore.kernel.org/all/20240819155627.1367-1-Tze-nan.Wu@mediatek.com/ (local)quoted
Hide cgroup_bpf_enabled in the macro, and some modifications toadaptquoted
the coding style. Chagnes from v3 to v4:https://lore.kernel.org/all/20240820092942.16654-1-Tze-nan.Wu@mediatek.com/ (local)quoted
Add bpf tag to subject, and Fixes tag in body. --- include/linux/bpf-cgroup.h | 15 ++++++++------- net/socket.c | 5 +++-- 2 files changed, 11 insertions(+), 9 deletions(-)diff --git a/include/linux/bpf-cgroup.h b/include/linux/bpf-cgroup.hquoted
index fb3c3e7181e6..5afa2ac76aae 100644--- a/include/linux/bpf-cgroup.h +++ b/include/linux/bpf-cgroup.h@@ -390,20 +390,20 @@ static inline boolcgroup_bpf_sock_enabled(struct sock *sk,quoted
__ret;\quoted
}) -#defineBPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen) \quoted
+#define BPF_CGROUP_GETSOCKOPT_MAX_OPTLEN(optlen,enabled) \quoted
({\quoted
int __ret =0; \quoted
- if(cgroup_bpf_enabled(CGROUP_GETSOCKOPT)) \quoted
+ enabled =cgroup_bpf_enabled(CGROUP_GETSOCKOPT); \quoted
+ if (enabled)I suspect the compiler generates slow code after such a patch. pw-bot: cr What is the problem with double cgroup_bpf_enabled() check? yes it might return two different values, so?
Depending on where the -EFAULT occurs, the problem could be different. In our case, the -EFAULT is returned from getsockopt() during a "bootup-critical property setting" flow in Android. As a result, the property setting fails due it get -EFAULT from getsockopt(), causing the device to fail the boot process. Should the userspace caller always anticipate an -EFAULT from getsockopt() if there's another process enables CGROUP_GETSOCKOPT (possibly through the bpf() syscall) at the same time? If that's the case, then I will handle this in userspace. Thanks, --tze-nan