Skip to content

Commit ba62d61

Browse files
lmbMartin KaFai Lau
authored and
Martin KaFai Lau
committed
bpf: Refuse unused attributes in bpf_prog_{attach,detach}
The recently added tcx attachment extended the BPF UAPI for attaching and detaching by a couple of fields. Those fields are currently only supported for tcx, other types like cgroups and flow dissector silently ignore the new fields except for the new flags. This is problematic once we extend bpf_mprog to older attachment types, since it's hard to figure out whether the syscall really was successful if the kernel silently ignores non-zero values. Explicitly reject non-zero fields relevant to bpf_mprog for attachment types which don't use the latter yet. Fixes: e420bed ("bpf: Add fd-based tcx multi-prog infra with link support") Signed-off-by: Lorenz Bauer <lmb@isovalent.com> Co-developed-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20231006220655.1653-3-daniel@iogearbox.net Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent edfa9af commit ba62d61

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

kernel/bpf/syscall.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -3796,7 +3796,6 @@ static int bpf_prog_attach(const union bpf_attr *attr)
37963796
{
37973797
enum bpf_prog_type ptype;
37983798
struct bpf_prog *prog;
3799-
u32 mask;
38003799
int ret;
38013800

38023801
if (CHECK_ATTR(BPF_PROG_ATTACH))
@@ -3805,10 +3804,16 @@ static int bpf_prog_attach(const union bpf_attr *attr)
38053804
ptype = attach_type_to_prog_type(attr->attach_type);
38063805
if (ptype == BPF_PROG_TYPE_UNSPEC)
38073806
return -EINVAL;
3808-
mask = bpf_mprog_supported(ptype) ?
3809-
BPF_F_ATTACH_MASK_MPROG : BPF_F_ATTACH_MASK_BASE;
3810-
if (attr->attach_flags & ~mask)
3811-
return -EINVAL;
3807+
if (bpf_mprog_supported(ptype)) {
3808+
if (attr->attach_flags & ~BPF_F_ATTACH_MASK_MPROG)
3809+
return -EINVAL;
3810+
} else {
3811+
if (attr->attach_flags & ~BPF_F_ATTACH_MASK_BASE)
3812+
return -EINVAL;
3813+
if (attr->relative_fd ||
3814+
attr->expected_revision)
3815+
return -EINVAL;
3816+
}
38123817

38133818
prog = bpf_prog_get_type(attr->attach_bpf_fd, ptype);
38143819
if (IS_ERR(prog))
@@ -3878,6 +3883,10 @@ static int bpf_prog_detach(const union bpf_attr *attr)
38783883
if (IS_ERR(prog))
38793884
return PTR_ERR(prog);
38803885
}
3886+
} else if (attr->attach_flags ||
3887+
attr->relative_fd ||
3888+
attr->expected_revision) {
3889+
return -EINVAL;
38813890
}
38823891

38833892
switch (ptype) {

0 commit comments

Comments
 (0)