* [PATCH bpf-next v3 0/2] Fix wrong cgroup attach flags being assigned to effective progs
@ 2022-09-14 16:17 Pu Lehui
2022-09-14 16:17 ` [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query Pu Lehui
2022-09-14 16:17 ` [PATCH bpf-next v3 2/2] bpftool: Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
0 siblings, 2 replies; 6+ messages in thread
From: Pu Lehui @ 2022-09-14 16:17 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Pu Lehui
From: Pu Lehui <pulehui@huawei.com>
When root-cgroup attach multi progs and sub-cgroup attach a
override prog, bpftool will display incorrectly for the attach
flags of the sub-cgroup’s effective progs:
$ bpftool cgroup tree /sys/fs/cgroup effective
CgroupPath
ID AttachType AttachFlags Name
/sys/fs/cgroup
6 cgroup_sysctl multi sysctl_tcp_mem
13 cgroup_sysctl multi sysctl_tcp_mem
/sys/fs/cgroup/cg1
20 cgroup_sysctl override sysctl_tcp_mem
6 cgroup_sysctl override sysctl_tcp_mem <- wrong
13 cgroup_sysctl override sysctl_tcp_mem <- wrong
/sys/fs/cgroup/cg1/cg2
20 cgroup_sysctl sysctl_tcp_mem
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
For cg1, obviously, the attach flags of prog6 and prog13 can not be
OVERRIDE. And for query with EFFECTIVE flags, exporting attach flags
does not make sense, we can remove this logic. After these patches,
the above situation will show as bellow:
# bpftool cgroup tree /sys/fs/cgroup effective
CgroupPath
ID AttachType Name
/sys/fs/cgroup
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
/sys/fs/cgroup/cg1
20 cgroup_sysctl sysctl_tcp_mem
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
/sys/fs/cgroup/cg1/cg2
20 cgroup_sysctl sysctl_tcp_mem
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
v3:
- Don't show attach flags when effective query. (John, sdf, martin)
v2:
https://lore.kernel.org/bpf/20220908145304.3436139-1-pulehui@huaweicloud.com
- Limit prog_cnt to avoid overflow. (John)
- Add more detail message.
v1:
https://lore.kernel.org/bpf/20220820120234.2121044-1-pulehui@huawei.com
Pu Lehui (2):
bpf, cgroup: Don't populate prog_attach_flags array when effective
query
bpftool: Fix wrong cgroup attach flags being assigned to effective
progs
kernel/bpf/cgroup.c | 23 +++++++++-------
tools/bpf/bpftool/cgroup.c | 54 +++++++++++++++++++++++---------------
2 files changed, 46 insertions(+), 31 deletions(-)
--
2.25.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query
2022-09-14 16:17 [PATCH bpf-next v3 0/2] Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
@ 2022-09-14 16:17 ` Pu Lehui
2022-09-17 0:03 ` Martin KaFai Lau
2022-09-14 16:17 ` [PATCH bpf-next v3 2/2] bpftool: Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
1 sibling, 1 reply; 6+ messages in thread
From: Pu Lehui @ 2022-09-14 16:17 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Pu Lehui
From: Pu Lehui <pulehui@huawei.com>
Attach flags is only valid for attached progs of this layer cgroup,
but not for effective progs. For querying with EFFECTIVE flags,
exporting attach flags does not make sense. so we don't need to
populate prog_attach_flags array when effective query.
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
kernel/bpf/cgroup.c | 23 +++++++++++++----------
1 file changed, 13 insertions(+), 10 deletions(-)
diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 00c7f864900e..45b1bc47f84e 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -1020,6 +1020,7 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
union bpf_attr __user *uattr)
{
__u32 __user *prog_attach_flags = u64_to_user_ptr(attr->query.prog_attach_flags);
+ bool effective_query = attr->query.query_flags & BPF_F_QUERY_EFFECTIVE;
__u32 __user *prog_ids = u64_to_user_ptr(attr->query.prog_ids);
enum bpf_attach_type type = attr->query.attach_type;
enum cgroup_bpf_attach_type from_atype, to_atype;
@@ -1030,7 +1031,8 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
u32 flags;
if (type == BPF_LSM_CGROUP) {
- if (attr->query.prog_cnt && prog_ids && !prog_attach_flags)
+ if (attr->query.prog_cnt && prog_ids &&
+ !effective_query && !prog_attach_flags)
return -EINVAL;
from_atype = CGROUP_LSM_START;
@@ -1045,7 +1047,7 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
}
for (atype = from_atype; atype <= to_atype; atype++) {
- if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
+ if (effective_query) {
effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
total_cnt += bpf_prog_array_length(effective);
@@ -1068,7 +1070,7 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
}
for (atype = from_atype; atype <= to_atype && total_cnt; atype++) {
- if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
+ if (effective_query) {
effective = rcu_dereference_protected(cgrp->bpf.effective[atype],
lockdep_is_held(&cgroup_mutex));
cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
@@ -1090,15 +1092,16 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
if (++i == cnt)
break;
}
- }
- if (prog_attach_flags) {
- flags = cgrp->bpf.flags[atype];
+ if (prog_attach_flags) {
+ flags = cgrp->bpf.flags[atype];
- for (i = 0; i < cnt; i++)
- if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
- return -EFAULT;
- prog_attach_flags += cnt;
+ for (i = 0; i < cnt; i++)
+ if (copy_to_user(prog_attach_flags + i,
+ &flags, sizeof(flags)))
+ return -EFAULT;
+ prog_attach_flags += cnt;
+ }
}
prog_ids += cnt;
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH bpf-next v3 2/2] bpftool: Fix wrong cgroup attach flags being assigned to effective progs
2022-09-14 16:17 [PATCH bpf-next v3 0/2] Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
2022-09-14 16:17 ` [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query Pu Lehui
@ 2022-09-14 16:17 ` Pu Lehui
1 sibling, 0 replies; 6+ messages in thread
From: Pu Lehui @ 2022-09-14 16:17 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Martin KaFai Lau, Song Liu, Yonghong Song,
John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Pu Lehui
From: Pu Lehui <pulehui@huawei.com>
When root-cgroup attach multi progs and sub-cgroup attach a override prog,
bpftool will display incorrectly for the attach flags of the sub-cgroup’s
effective progs:
$ bpftool cgroup tree /sys/fs/cgroup effective
CgroupPath
ID AttachType AttachFlags Name
/sys/fs/cgroup
6 cgroup_sysctl multi sysctl_tcp_mem
13 cgroup_sysctl multi sysctl_tcp_mem
/sys/fs/cgroup/cg1
20 cgroup_sysctl override sysctl_tcp_mem
6 cgroup_sysctl override sysctl_tcp_mem <- wrong
13 cgroup_sysctl override sysctl_tcp_mem <- wrong
/sys/fs/cgroup/cg1/cg2
20 cgroup_sysctl sysctl_tcp_mem
6 cgroup_sysctl sysctl_tcp_mem
13 cgroup_sysctl sysctl_tcp_mem
Attach flags is only valid for attached progs of this layer cgroup,
but not for effective progs. For querying with EFFECTIVE flags,
exporting attach flags does not make sense. Since prog_attach_flags
array not being populated when effective query, so we can just remove
the corresponding attach flags logic.
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
tools/bpf/bpftool/cgroup.c | 54 +++++++++++++++++++++++---------------
1 file changed, 33 insertions(+), 21 deletions(-)
diff --git a/tools/bpf/bpftool/cgroup.c b/tools/bpf/bpftool/cgroup.c
index cced668fb2a3..24cdb0b41f89 100644
--- a/tools/bpf/bpftool/cgroup.c
+++ b/tools/bpf/bpftool/cgroup.c
@@ -136,8 +136,8 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
jsonw_string_field(json_wtr, "attach_type", attach_type_str);
else
jsonw_uint_field(json_wtr, "attach_type", attach_type);
- jsonw_string_field(json_wtr, "attach_flags",
- attach_flags_str);
+ if (!(query_flags & BPF_F_QUERY_EFFECTIVE))
+ jsonw_string_field(json_wtr, "attach_flags", attach_flags_str);
jsonw_string_field(json_wtr, "name", prog_name);
if (attach_btf_name)
jsonw_string_field(json_wtr, "attach_btf_name", attach_btf_name);
@@ -150,7 +150,10 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
printf("%-15s", attach_type_str);
else
printf("type %-10u", attach_type);
- printf(" %-15s %-15s", attach_flags_str, prog_name);
+ if (query_flags & BPF_F_QUERY_EFFECTIVE)
+ printf(" %-15s", prog_name);
+ else
+ printf(" %-15s %-15s", attach_flags_str, prog_name);
if (attach_btf_name)
printf(" %-15s", attach_btf_name);
else if (info.attach_btf_id)
@@ -200,7 +203,7 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
{
LIBBPF_OPTS(bpf_prog_query_opts, p);
__u32 prog_attach_flags[1024] = {0};
- const char *attach_flags_str;
+ const char *attach_flags_str = NULL;
__u32 prog_ids[1024] = {0};
char buf[32];
__u32 iter;
@@ -219,23 +222,25 @@ static int show_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type,
return 0;
for (iter = 0; iter < p.prog_cnt; iter++) {
- __u32 attach_flags;
-
- attach_flags = prog_attach_flags[iter] ?: p.attach_flags;
-
- switch (attach_flags) {
- case BPF_F_ALLOW_MULTI:
- attach_flags_str = "multi";
- break;
- case BPF_F_ALLOW_OVERRIDE:
- attach_flags_str = "override";
- break;
- case 0:
- attach_flags_str = "";
- break;
- default:
- snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
- attach_flags_str = buf;
+ if (!(query_flags & BPF_F_QUERY_EFFECTIVE)) {
+ __u32 attach_flags;
+
+ attach_flags = prog_attach_flags[iter] ?: p.attach_flags;
+
+ switch (attach_flags) {
+ case BPF_F_ALLOW_MULTI:
+ attach_flags_str = "multi";
+ break;
+ case BPF_F_ALLOW_OVERRIDE:
+ attach_flags_str = "override";
+ break;
+ case 0:
+ attach_flags_str = "";
+ break;
+ default:
+ snprintf(buf, sizeof(buf), "unknown(%x)", attach_flags);
+ attach_flags_str = buf;
+ }
}
show_bpf_prog(prog_ids[iter], type,
@@ -292,6 +297,8 @@ static int do_show(int argc, char **argv)
if (json_output)
jsonw_start_array(json_wtr);
+ else if (query_flags & BPF_F_QUERY_EFFECTIVE)
+ printf("%-8s %-15s %-15s\n", "ID", "AttachType", "Name");
else
printf("%-8s %-15s %-15s %-15s\n", "ID", "AttachType",
"AttachFlags", "Name");
@@ -436,6 +443,11 @@ static int do_show_tree(int argc, char **argv)
if (json_output)
jsonw_start_array(json_wtr);
+ else if (query_flags & BPF_F_QUERY_EFFECTIVE)
+ printf("%s\n"
+ "%-8s %-15s %-15s\n",
+ "CgroupPath",
+ "ID", "AttachType", "Name");
else
printf("%s\n"
"%-8s %-15s %-15s %-15s\n",
--
2.25.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query
2022-09-14 16:17 ` [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query Pu Lehui
@ 2022-09-17 0:03 ` Martin KaFai Lau
2022-09-19 13:32 ` Pu Lehui
0 siblings, 1 reply; 6+ messages in thread
From: Martin KaFai Lau @ 2022-09-17 0:03 UTC (permalink / raw)
To: Pu Lehui, Stanislav Fomichev
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Hao Luo, Jiri Olsa, Pu Lehui, bpf, linux-kernel
On 9/14/22 9:17 AM, Pu Lehui wrote:
> From: Pu Lehui <pulehui@huawei.com>
>
> Attach flags is only valid for attached progs of this layer cgroup,
> but not for effective progs. For querying with EFFECTIVE flags,
> exporting attach flags does not make sense. so we don't need to
> populate prog_attach_flags array when effective query.
prog_attach_flags has been added to 6.0 which is in rc5. It is still
doable (and cleaner) to reject prog_attach_flags when it is an
effective_query. This should be done regardless of 'type ==
BPF_LSM_CGROUP' or not. Something like:
if (effective_query && prog_attach_flags)
return -EINVAL;
Otherwise, the whole prog_attach_flags needs to be set to 0 during
effective_query. Please target the change to the bpf tree instead of
bpf-next such that this uapi bit can be fixed before 6.0.
Also, the effective_query issue is not limited to the prog_attach_flags?
For the older uattr->query.attach_flags, it should be set to 0 also when
it is an effective_query, right?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query
2022-09-17 0:03 ` Martin KaFai Lau
@ 2022-09-19 13:32 ` Pu Lehui
2022-09-19 17:28 ` Martin KaFai Lau
0 siblings, 1 reply; 6+ messages in thread
From: Pu Lehui @ 2022-09-19 13:32 UTC (permalink / raw)
To: Martin KaFai Lau, Stanislav Fomichev
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Hao Luo, Jiri Olsa, bpf, linux-kernel, Pu Lehui,
Pu Lehui
On 2022/9/17 8:03, Martin KaFai Lau wrote:
> On 9/14/22 9:17 AM, Pu Lehui wrote:
>> From: Pu Lehui <pulehui@huawei.com>
>>
>> Attach flags is only valid for attached progs of this layer cgroup,
>> but not for effective progs. For querying with EFFECTIVE flags,
>> exporting attach flags does not make sense. so we don't need to
>> populate prog_attach_flags array when effective query.
>
> prog_attach_flags has been added to 6.0 which is in rc5. It is still
> doable (and cleaner) to reject prog_attach_flags when it is an
> effective_query. This should be done regardless of 'type ==
> BPF_LSM_CGROUP' or not. Something like:
>
> if (effective_query && prog_attach_flags)
> return -EINVAL;
>
> Otherwise, the whole prog_attach_flags needs to be set to 0 during
> effective_query. Please target the change to the bpf tree instead of
> bpf-next such that this uapi bit can be fixed before 6.0.
>
Okay, will handle in next version.
> Also, the effective_query issue is not limited to the prog_attach_flags?
> For the older uattr->query.attach_flags, it should be set to 0 also when
> it is an effective_query, right?
For output uattr->query.attach_flags, we certainly don't need to copy it
to userspace when effective query. Since we do not utilize
uattr->query.attach_flags in the cgroup query function, should we need
to take it as input and reject when it is non-zero in effective query?
Something like:
if (effective_query && (prog_attach_flags || attr->query.attach_flags))
For both output and input scenarios, we are faced with the problem that
there is a ambiguity in attach_flags being 0. When we do not copy to the
userspace, libbpf will set it to 0 by default, and 0 can mean NONE flag
attach, or no attach prog. The same is true for input scenarios.
So should we need to define NONE attach flag and redefine the others?
Such as follow:
#define BPF_F_ALLOW_NONE (1U << 0)
#define BPF_F_ALLOW_OVERRIDE (1U << 1)
#define BPF_F_ALLOW_MULTI (1U << 2)
#define BPF_F_REPLACE (1U << 3)
And then attach flags being 0 certainly means no attach any prog.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query
2022-09-19 13:32 ` Pu Lehui
@ 2022-09-19 17:28 ` Martin KaFai Lau
0 siblings, 0 replies; 6+ messages in thread
From: Martin KaFai Lau @ 2022-09-19 17:28 UTC (permalink / raw)
To: Pu Lehui, Stanislav Fomichev
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Quentin Monnet, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Hao Luo, Jiri Olsa, bpf, linux-kernel, Pu Lehui
On 9/19/22 6:32 AM, Pu Lehui wrote:
>
>
> On 2022/9/17 8:03, Martin KaFai Lau wrote:
>> On 9/14/22 9:17 AM, Pu Lehui wrote:
>>> From: Pu Lehui <pulehui@huawei.com>
>>>
>>> Attach flags is only valid for attached progs of this layer cgroup,
>>> but not for effective progs. For querying with EFFECTIVE flags,
>>> exporting attach flags does not make sense. so we don't need to
>>> populate prog_attach_flags array when effective query.
>>
>> prog_attach_flags has been added to 6.0 which is in rc5. It is still
>> doable (and cleaner) to reject prog_attach_flags when it is an
>> effective_query. This should be done regardless of 'type ==
>> BPF_LSM_CGROUP' or not. Something like:
>>
>> if (effective_query && prog_attach_flags)
>> return -EINVAL;
>>
>> Otherwise, the whole prog_attach_flags needs to be set to 0 during
>> effective_query. Please target the change to the bpf tree instead of
>> bpf-next such that this uapi bit can be fixed before 6.0.
>>
>
> Okay, will handle in next version.
Thanks. It will also be useful to comment the uapi's bpf.h and mention
prog_attach_flags should not be set during effective_query.
>
>> Also, the effective_query issue is not limited to the
>> prog_attach_flags? For the older uattr->query.attach_flags, it should
>> be set to 0 also when it is an effective_query, right?
>
> For output uattr->query.attach_flags, we certainly don't need to copy it
> to userspace when effective query. Since we do not utilize
> uattr->query.attach_flags in the cgroup query function, should we need
> to take it as input and reject when it is non-zero in effective query?
> Something like:
> if (effective_query && (prog_attach_flags || attr->query.attach_flags))
No. I don't think the zero attr->query.attach_flags can be enforced
now. It is used as an output value only and its input value has never
been checked. Although the bpftool always sets it to 0 before the
query, checking zero now does not gain much while there is a slight
chance of breaking other users.
Only need to set/output uattr->query.attach_flags as 0 during
effective_query.
>
> For both output and input scenarios, we are faced with the problem that
> there is a ambiguity in attach_flags being 0. When we do not copy to the
> userspace, libbpf will set it to 0 by default, and 0 can mean NONE flag
> attach, or no attach prog. The same is true for input scenarios.
>
> So should we need to define NONE attach flag and redefine the others?
> Such as follow:
> #define BPF_F_ALLOW_NONE (1U << 0)
I would not change the uapi for this. 0 implicitly means no flags or
none. Regardless, this change does not belong to the bpf tree where
this fix will be landing.
> #define BPF_F_ALLOW_OVERRIDE (1U << 1)
> #define BPF_F_ALLOW_MULTI (1U << 2)
> #define BPF_F_REPLACE (1U << 3)
>
> And then attach flags being 0 certainly means no attach any prog.
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-09-19 17:28 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-14 16:17 [PATCH bpf-next v3 0/2] Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
2022-09-14 16:17 ` [PATCH bpf-next v3 1/2] bpf, cgroup: Don't populate prog_attach_flags array when effective query Pu Lehui
2022-09-17 0:03 ` Martin KaFai Lau
2022-09-19 13:32 ` Pu Lehui
2022-09-19 17:28 ` Martin KaFai Lau
2022-09-14 16:17 ` [PATCH bpf-next v3 2/2] bpftool: Fix wrong cgroup attach flags being assigned to effective progs Pu Lehui
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.