Skip to content

Commit edfa9af

Browse files
borkmannMartin KaFai Lau
authored and
Martin KaFai Lau
committed
bpf: Handle bpf_mprog_query with NULL entry
Improve consistency for bpf_mprog_query() API and let the latter also handle a NULL entry as can be the case for tcx. Instead of returning -ENOENT, we copy a count of 0 and revision of 1 to user space, so that this can be fed into a subsequent bpf_mprog_attach() call as expected_revision. A BPF self- test as part of this series has been added to assert this case. Suggested-by: Lorenz Bauer <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Martin KaFai Lau <[email protected]>
1 parent a4fe783 commit edfa9af

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

kernel/bpf/mprog.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,16 @@ int bpf_mprog_query(const union bpf_attr *attr, union bpf_attr __user *uattr,
401401
struct bpf_mprog_cp *cp;
402402
struct bpf_prog *prog;
403403
const u32 flags = 0;
404+
u32 id, count = 0;
405+
u64 revision = 1;
404406
int i, ret = 0;
405-
u32 id, count;
406-
u64 revision;
407407

408408
if (attr->query.query_flags || attr->query.attach_flags)
409409
return -EINVAL;
410-
revision = bpf_mprog_revision(entry);
411-
count = bpf_mprog_total(entry);
410+
if (entry) {
411+
revision = bpf_mprog_revision(entry);
412+
count = bpf_mprog_total(entry);
413+
}
412414
if (copy_to_user(&uattr->query.attach_flags, &flags, sizeof(flags)))
413415
return -EFAULT;
414416
if (copy_to_user(&uattr->query.revision, &revision, sizeof(revision)))

kernel/bpf/tcx.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ int tcx_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
123123
{
124124
bool ingress = attr->query.attach_type == BPF_TCX_INGRESS;
125125
struct net *net = current->nsproxy->net_ns;
126-
struct bpf_mprog_entry *entry;
127126
struct net_device *dev;
128127
int ret;
129128

@@ -133,12 +132,7 @@ int tcx_prog_query(const union bpf_attr *attr, union bpf_attr __user *uattr)
133132
ret = -ENODEV;
134133
goto out;
135134
}
136-
entry = tcx_entry_fetch(dev, ingress);
137-
if (!entry) {
138-
ret = -ENOENT;
139-
goto out;
140-
}
141-
ret = bpf_mprog_query(attr, uattr, entry);
135+
ret = bpf_mprog_query(attr, uattr, tcx_entry_fetch(dev, ingress));
142136
out:
143137
rtnl_unlock();
144138
return ret;

0 commit comments

Comments
 (0)