Extend direct cgroup program queries to merge
(pl->flags & BPF_F_PREORDER) with the cgroup-wide flags when copying
prog_attach_flags to user space. Only BPF_F_PREORDER is extracted from
pl->flags to ensure transient positioning flags (such as BPF_F_BEFORE,
BPF_F_AFTER, BPF_F_ID, or BPF_F_REPLACE) are not leaked to user space.

This can introduce new prog_attach_flags[] values with BPF_F_PREORDER
set, for example 0x40 (BPF_F_PREORDER), 0x41
(BPF_F_ALLOW_OVERRIDE | BPF_F_PREORDER), or 0x42
(BPF_F_ALLOW_MULTI | BPF_F_PREORDER), depending on the cgroup-wide
attachment mode.

Older bpftool versions treat such combinations as unknown flags;
bpftool support is updated later in this series.

Signed-off-by: Hui Su <[email protected]>
---

Notes (bpf-preorder-v3-20260922-check):
    Testing:
    - PASS: ./test_progs -t cgroup_mprog_opts (x86_64 QEMU/KVM).
    - Result reused after a content-equivalent rebase; the patch diff is 
unchanged.

 kernel/bpf/cgroup.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/kernel/bpf/cgroup.c b/kernel/bpf/cgroup.c
index 1cb5e6a6ffc1..2bbe77de89f0 100644
--- a/kernel/bpf/cgroup.c
+++ b/kernel/bpf/cgroup.c
@@ -1515,19 +1515,19 @@ static int __cgroup_bpf_query(struct cgroup *cgrp, 
const union bpf_attr *attr,
                                id = prog_list_id(pl);
                                if (copy_to_user(prog_ids + i, &id, sizeof(id)))
                                        return -EFAULT;
+                               if (prog_attach_flags) {
+                                       flags = cgrp->bpf.flags[atype] |
+                                               (pl->flags & BPF_F_PREORDER);
+                                       if (copy_to_user(prog_attach_flags + i,
+                                                        &flags, sizeof(flags)))
+                                               return -EFAULT;
+                               }
                                if (++i == cnt)
                                        break;
                        }
 
-                       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;
+                       if (prog_attach_flags)
                                prog_attach_flags += cnt;
-                       }
                }
 
                prog_ids += cnt;
-- 
2.55.0


Reply via email to