/* Query effective (directly attached + inherited from ancestor cgroups)
  * programs that will be executed for events within a cgroup.
- * attach_flags with this flag are returned only for directly attached programs.
+ * attach_flags with this flag are always returned 0.
  */
 #define BPF_F_QUERY_EFFECTIVE  (1U << 0)
 
                __u32           attach_flags;
                __aligned_u64   prog_ids;
                __u32           prog_cnt;
-               __aligned_u64   prog_attach_flags; /* output: per-program attach_flags */
+               /* output: per-program attach_flags.
+                * not allowed to be set during effective query.
+                */
+               __aligned_u64   prog_attach_flags;
        } query;
 
        struct { /* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */
 
                              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;
        int total_cnt = 0;
        u32 flags;
 
+       if (effective_query && prog_attach_flags)
+               return -EINVAL;
+
        if (type == BPF_LSM_CGROUP) {
-               if (attr->query.prog_cnt && prog_ids && !prog_attach_flags)
+               if (!effective_query && attr->query.prog_cnt &&
+                   prog_ids && !prog_attach_flags)
                        return -EINVAL;
 
                from_atype = CGROUP_LSM_START;
        }
 
        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);
                }
        }
 
+       /* always output uattr->query.attach_flags as 0 during effective query */
+       flags = effective_query ? 0 : flags;
        if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
                return -EFAULT;
        if (copy_to_user(&uattr->query.prog_cnt, &total_cnt, sizeof(total_cnt)))
        }
 
        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);
                                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;
 
 
 /* Query effective (directly attached + inherited from ancestor cgroups)
  * programs that will be executed for events within a cgroup.
- * attach_flags with this flag are returned only for directly attached programs.
+ * attach_flags with this flag are always returned 0.
  */
 #define BPF_F_QUERY_EFFECTIVE  (1U << 0)
 
                __u32           attach_flags;
                __aligned_u64   prog_ids;
                __u32           prog_cnt;
-               __aligned_u64   prog_attach_flags; /* output: per-program attach_flags */
+               /* output: per-program attach_flags.
+                * not allowed to be set during effective query.
+                */
+               __aligned_u64   prog_attach_flags;
        } query;
 
        struct { /* anonymous struct used by BPF_RAW_TRACEPOINT_OPEN command */