Skip to content

Commit e510c16

Browse files
lmbborkmann
authored andcommitted
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. Independent of this, we were also thinking about rejecting attr->replace_bpf_fd for the bpf_prog_detach() case given this is not used there generally. However, this field has been ignored in detach for several releases, thus rejecting has a risk of potential breakage. Fixes: e420bed ("bpf: Add fd-based tcx multi-prog infra with link support") Signed-off-by: Lorenz Bauer <[email protected]> Co-developed-by: Daniel Borkmann <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 00e1ee4 commit e510c16

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

kernel/bpf/syscall.c

Lines changed: 14 additions & 5 deletions
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)