diff mbox series

[bpf-next,1/2] bpf, cgroup: Fix attach flags being assigned to effective progs

Message ID 20220820120234.2121044-2-pulehui@huawei.com (mailing list archive)
State Changes Requested
Delegated to: BPF
Headers show
Series Fix cgroup attach flags being assigned to effective progs | expand

Checks

Context Check Description
netdev/tree_selection success Clearly marked for bpf-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix success Link
netdev/cover_letter success Series has a cover letter
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 354 this patch: 354
netdev/cc_maintainers warning 3 maintainers not CCed: song@kernel.org martin.lau@linux.dev haoluo@google.com
netdev/build_clang success Errors and warnings before: 17 this patch: 17
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 354 this patch: 354
netdev/checkpatch warning WARNING: line length of 82 exceeds 80 columns WARNING: line length of 91 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 12 this patch: 12
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-3 success Logs for Kernel LATEST on z15 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for llvm-toolchain
bpf/vmtest-bpf-next-VM_Test-5 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-1 success Logs for Kernel LATEST on ubuntu-latest with gcc
bpf/vmtest-bpf-next-VM_Test-2 success Logs for Kernel LATEST on ubuntu-latest with llvm-16

Commit Message

Pu Lehui Aug. 20, 2022, 12:02 p.m. UTC
Attach flags is only valid for attached progs of this layer cgroup,
but not for effective progs. We know that the attached progs is at
the beginning of the effective progs array, so we can just populate
the elements in front of the prog_attach_flags array.

Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
 kernel/bpf/cgroup.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

John Fastabend Aug. 23, 2022, 9:22 p.m. UTC | #1
Pu Lehui wrote:
> Attach flags is only valid for attached progs of this layer cgroup,
> but not for effective progs. We know that the attached progs is at
> the beginning of the effective progs array, so we can just populate
> the elements in front of the prog_attach_flags array.
> 
> Signed-off-by: Pu Lehui <pulehui@huawei.com>

Trying to parse above, could you add a bit more detail on why this is
problem so readers don't need to track it down.

> ---
>  kernel/bpf/cgroup.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
> index 59b7eb60d5b4..9adf72e99907 100644
> --- a/kernel/bpf/cgroup.c
> +++ b/kernel/bpf/cgroup.c
> @@ -1091,11 +1091,14 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
>  		}
>  

Because we are looking at it let me try to understand. There are two
paths that set cnt relative bits here,

  if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
      ...     
      cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);                                       
      ...     
  } else {
     ...
     progs = &cgrp->bpf.progs[atype];
     cnt = min_t(int, prog_list_length(progs), total_cnt);
     ...
  }

And the docs claim

 *              **BPF_F_QUERY_EFFECTIVE**
 *                      Only return information regarding programs which are
 *                      currently effective at the specified *target_fd*.

so in the EFFECTIVE case should we be reporting flags at all if the
commit message says "attach flags is only valid for attached progs
of this layer cgroup, but not for effective progs."

And then in the else branch the change is what you have in the diff anyways correct?

>  		if (prog_attach_flags) {
> +			int progs_cnt = prog_list_length(&cgrp->bpf.progs[atype]);
>  			flags = cgrp->bpf.flags[atype];
>  
> -			for (i = 0; i < cnt; i++)

Do we need to min with total_cnt here so we don't walk off a short user list?

> +			/* attach flags only for attached progs, but not effective progs */
> +			for (i = 0; i < progs_cnt; i++)
>  				if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
>  					return -EFAULT;
> +
>  			prog_attach_flags += cnt;
>  		}
>  
> -- 
> 2.25.1
>
Pu Lehui Sept. 8, 2022, 3:07 a.m. UTC | #2
On 2022/8/24 5:22, John Fastabend wrote:
> Pu Lehui wrote:
>> Attach flags is only valid for attached progs of this layer cgroup,
>> but not for effective progs. We know that the attached progs is at
>> the beginning of the effective progs array, so we can just populate
>> the elements in front of the prog_attach_flags array.
>>
>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> 
> Trying to parse above, could you add a bit more detail on why this is
> problem so readers don't need to track it down.
> 
>> ---
>>   kernel/bpf/cgroup.c | 5 ++++-
>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
>> index 59b7eb60d5b4..9adf72e99907 100644
>> --- a/kernel/bpf/cgroup.c
>> +++ b/kernel/bpf/cgroup.c
>> @@ -1091,11 +1091,14 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
>>   		}
>>   
> 
> Because we are looking at it let me try to understand. There are two
> paths that set cnt relative bits here,
> 

Hi John,

Thanks for your review. The reason is that:
For the following cgroup tree:
    root
     |
    cg1
     |
    cg2

I attached prog1 and prog2 to root cgroup with MULTI attach type, 
attached prog3 to cg1 with OVERRIDE attach type, and then used bpftool
to show this cgroup tree with effective query flag:

$ bpftool cgroup tree /sys/fs/cgroup effective
CgroupPath
ID       AttachType      AttachFlags     Name
/sys/fs/cgroup
1        sysctl          multi           sysctl_tcp_mem
2        sysctl          multi           sysctl_tcp_mem
/sys/fs/cgroup/cg1
3        sysctl          override        sysctl_tcp_mem
1        sysctl          override        sysctl_tcp_mem <- wrong
2        sysctl          override        sysctl_tcp_mem <- wrong
/sys/fs/cgroup/cg1/cg2
3        sysctl                          sysctl_tcp_mem
1        sysctl                          sysctl_tcp_mem
2        sysctl                          sysctl_tcp_mem

For cg1, obviously, the attach flags of prog1 and prog2 can not be 
OVERRIDE, and the attach flags of prog1 and prog2 is meaningless for 
cg1. We only need to care the attach flags of prog which attached to 
cg1, other progs attach flags should be omit.

>    if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
>        ...
>        cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
>        ...
>    } else {
>       ...
>       progs = &cgrp->bpf.progs[atype];
>       cnt = min_t(int, prog_list_length(progs), total_cnt);
>       ...
>    }
> 
> And the docs claim
> 
>   *              **BPF_F_QUERY_EFFECTIVE**
>   *                      Only return information regarding programs which are
>   *                      currently effective at the specified *target_fd*.
> 
> so in the EFFECTIVE case should we be reporting flags at all if the
> commit message says "attach flags is only valid for attached progs
> of this layer cgroup, but not for effective progs."
> 
> And then in the else branch the change is what you have in the diff anyways correct?
> 
>>   		if (prog_attach_flags) {
>> +			int progs_cnt = prog_list_length(&cgrp->bpf.progs[atype]);
>>   			flags = cgrp->bpf.flags[atype];
>>   
>> -			for (i = 0; i < cnt; i++)
> 
> Do we need to min with total_cnt here so we don't walk off a short user list?
> 

We should limit it, will fix it in v2. For query without effective flag, 
progs_cnt will equal to cnt, and for effective flag situation, progs_cnt 
only calculate prog count which attached to this cgroup layer.

>> +			/* attach flags only for attached progs, but not effective progs */
>> +			for (i = 0; i < progs_cnt; i++)
>>   				if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
>>   					return -EFAULT;
>> +
>>   			prog_attach_flags += cnt;
>>   		}
>>   
>> -- 
>> 2.25.1
>>
> .
>
Pu Lehui Sept. 8, 2022, 3:23 a.m. UTC | #3
On 2022/9/8 11:07, Pu Lehui wrote:
> 
> 
> On 2022/8/24 5:22, John Fastabend wrote:
>> Pu Lehui wrote:
>>> Attach flags is only valid for attached progs of this layer cgroup,
>>> but not for effective progs. We know that the attached progs is at
>>> the beginning of the effective progs array, so we can just populate
>>> the elements in front of the prog_attach_flags array.
>>>
>>> Signed-off-by: Pu Lehui <pulehui@huawei.com>
>>
>> Trying to parse above, could you add a bit more detail on why this is
>> problem so readers don't need to track it down.
>>
>>> ---
>>>   kernel/bpf/cgroup.c | 5 ++++-
>>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
>>> index 59b7eb60d5b4..9adf72e99907 100644
>>> --- a/kernel/bpf/cgroup.c
>>> +++ b/kernel/bpf/cgroup.c
>>> @@ -1091,11 +1091,14 @@ static int __cgroup_bpf_query(struct cgroup 
>>> *cgrp, const union bpf_attr *attr,
>>>           }
>>
>> Because we are looking at it let me try to understand. There are two
>> paths that set cnt relative bits here,
>>
> 
> Hi John,
> 
> Thanks for your review. The reason is that:
> For the following cgroup tree:
>     root
>      |
>     cg1
>      |
>     cg2
> 
> I attached prog1 and prog2 to root cgroup with MULTI attach type, 
sorry, typo, attach type -> attach flag
> attached prog3 to cg1 with OVERRIDE attach type, and then used bpftool
> to show this cgroup tree with effective query flag:
> 
> $ bpftool cgroup tree /sys/fs/cgroup effective
> CgroupPath
> ID       AttachType      AttachFlags     Name
> /sys/fs/cgroup
> 1        sysctl          multi           sysctl_tcp_mem
> 2        sysctl          multi           sysctl_tcp_mem
> /sys/fs/cgroup/cg1
> 3        sysctl          override        sysctl_tcp_mem
> 1        sysctl          override        sysctl_tcp_mem <- wrong
> 2        sysctl          override        sysctl_tcp_mem <- wrong
> /sys/fs/cgroup/cg1/cg2
> 3        sysctl                          sysctl_tcp_mem
> 1        sysctl                          sysctl_tcp_mem
> 2        sysctl                          sysctl_tcp_mem
> 
> For cg1, obviously, the attach flags of prog1 and prog2 can not be 
> OVERRIDE, and the attach flags of prog1 and prog2 is meaningless for 
> cg1. We only need to care the attach flags of prog which attached to 
> cg1, other progs attach flags should be omit.
> 
>>    if (attr->query.query_flags & BPF_F_QUERY_EFFECTIVE) {
>>        ...
>>        cnt = min_t(int, bpf_prog_array_length(effective), total_cnt);
>>        ...
>>    } else {
>>       ...
>>       progs = &cgrp->bpf.progs[atype];
>>       cnt = min_t(int, prog_list_length(progs), total_cnt);
>>       ...
>>    }
>>
>> And the docs claim
>>
>>   *              **BPF_F_QUERY_EFFECTIVE**
>>   *                      Only return information regarding programs 
>> which are
>>   *                      currently effective at the specified 
>> *target_fd*.
>>
>> so in the EFFECTIVE case should we be reporting flags at all if the
>> commit message says "attach flags is only valid for attached progs
>> of this layer cgroup, but not for effective progs."
>>
>> And then in the else branch the change is what you have in the diff 
>> anyways correct?
>>
>>>           if (prog_attach_flags) {
>>> +            int progs_cnt = prog_list_length(&cgrp->bpf.progs[atype]);
>>>               flags = cgrp->bpf.flags[atype];
>>> -            for (i = 0; i < cnt; i++)
>>
>> Do we need to min with total_cnt here so we don't walk off a short 
>> user list?
>>
> 
> We should limit it, will fix it in v2. For query without effective flag, 
> progs_cnt will equal to cnt, and for effective flag situation, progs_cnt 
> only calculate prog count which attached to this cgroup layer.
> 
>>> +            /* attach flags only for attached progs, but not 
>>> effective progs */
>>> +            for (i = 0; i < progs_cnt; i++)
>>>                   if (copy_to_user(prog_attach_flags + i, &flags, 
>>> sizeof(flags)))
>>>                       return -EFAULT;
>>> +
>>>               prog_attach_flags += cnt;
>>>           }
>>> -- 
>>> 2.25.1
>>>
>> .
>>
> .
diff mbox series

Patch

diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 59b7eb60d5b4..9adf72e99907 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -1091,11 +1091,14 @@  static int __cgroup_bpf_query(struct cgroup *cgrp, const union bpf_attr *attr,
 		}
 
 		if (prog_attach_flags) {
+			int progs_cnt = prog_list_length(&cgrp->bpf.progs[atype]);
 			flags = cgrp->bpf.flags[atype];
 
-			for (i = 0; i < cnt; i++)
+			/* attach flags only for attached progs, but not effective progs */
+			for (i = 0; i < progs_cnt; i++)
 				if (copy_to_user(prog_attach_flags + i, &flags, sizeof(flags)))
 					return -EFAULT;
+
 			prog_attach_flags += cnt;
 		}