Skip to content

Commit 2a95471

Browse files
author
Alexei Starovoitov
committed
Merge branch 'prog_test_run-improvement'
Lorenz Bauer says: ==================== Right now, there is no safe way to use BPF_PROG_TEST_RUN with data_out. This is because bpf_test_finish copies the output buffer to user space without checking its size. This can lead to the kernel overwriting data in user space after the buffer if xdp_adjust_head and friends are in play. Thanks to everyone for their advice and patience with this patch set! Changes in v5: * Fix up libbpf.map Changes in v4: * Document bpf_prog_test_run and bpf_prog_test_run_xattr * Use struct bpf_prog_test_run_attr for return values Changes in v3: * Introduce bpf_prog_test_run_xattr instead of modifying the existing function Changes in v2: * Make the syscall return ENOSPC if data_size_out is too small * Make bpf_prog_test_run return EINVAL if size_out is missing * Document the new behaviour of data_size_out ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents d59dd69 + df47fc3 commit 2a95471

File tree

7 files changed

+120
-7
lines changed

7 files changed

+120
-7
lines changed

include/uapi/linux/bpf.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,11 @@ union bpf_attr {
374374
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
375375
__u32 prog_fd;
376376
__u32 retval;
377-
__u32 data_size_in;
378-
__u32 data_size_out;
377+
__u32 data_size_in; /* input: len of data_in */
378+
__u32 data_size_out; /* input/output: len of data_out
379+
* returns ENOSPC if data_out
380+
* is too small.
381+
*/
379382
__aligned_u64 data_in;
380383
__aligned_u64 data_out;
381384
__u32 repeat;

net/bpf/test_run.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,27 @@ static int bpf_test_finish(const union bpf_attr *kattr,
7474
{
7575
void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
7676
int err = -EFAULT;
77+
u32 copy_size = size;
78+
79+
/* Clamp copy if the user has provided a size hint, but copy the full
80+
* buffer if not to retain old behaviour.
81+
*/
82+
if (kattr->test.data_size_out &&
83+
copy_size > kattr->test.data_size_out) {
84+
copy_size = kattr->test.data_size_out;
85+
err = -ENOSPC;
86+
}
7787

78-
if (data_out && copy_to_user(data_out, data, size))
88+
if (data_out && copy_to_user(data_out, data, copy_size))
7989
goto out;
8090
if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
8191
goto out;
8292
if (copy_to_user(&uattr->test.retval, &retval, sizeof(retval)))
8393
goto out;
8494
if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
8595
goto out;
86-
err = 0;
96+
if (err != -ENOSPC)
97+
err = 0;
8798
out:
8899
return err;
89100
}

tools/include/uapi/linux/bpf.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,11 @@ union bpf_attr {
374374
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
375375
__u32 prog_fd;
376376
__u32 retval;
377-
__u32 data_size_in;
378-
__u32 data_size_out;
377+
__u32 data_size_in; /* input: len of data_in */
378+
__u32 data_size_out; /* input/output: len of data_out
379+
* returns ENOSPC if data_out
380+
* is too small.
381+
*/
379382
__aligned_u64 data_in;
380383
__aligned_u64 data_out;
381384
__u32 repeat;

tools/lib/bpf/bpf.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,29 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
463463
return ret;
464464
}
465465

466+
int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
467+
{
468+
union bpf_attr attr;
469+
int ret;
470+
471+
if (!test_attr->data_out && test_attr->data_size_out > 0)
472+
return -EINVAL;
473+
474+
bzero(&attr, sizeof(attr));
475+
attr.test.prog_fd = test_attr->prog_fd;
476+
attr.test.data_in = ptr_to_u64(test_attr->data_in);
477+
attr.test.data_out = ptr_to_u64(test_attr->data_out);
478+
attr.test.data_size_in = test_attr->data_size_in;
479+
attr.test.data_size_out = test_attr->data_size_out;
480+
attr.test.repeat = test_attr->repeat;
481+
482+
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
483+
test_attr->data_size_out = attr.test.data_size_out;
484+
test_attr->retval = attr.test.retval;
485+
test_attr->duration = attr.test.duration;
486+
return ret;
487+
}
488+
466489
int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
467490
{
468491
union bpf_attr attr;

tools/lib/bpf/bpf.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
118118
LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
119119
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
120120
enum bpf_attach_type type);
121+
122+
struct bpf_prog_test_run_attr {
123+
int prog_fd;
124+
int repeat;
125+
const void *data_in;
126+
__u32 data_size_in;
127+
void *data_out; /* optional */
128+
__u32 data_size_out; /* in: max length of data_out
129+
* out: length of data_out */
130+
__u32 retval; /* out: return code of the BPF program */
131+
__u32 duration; /* out: average per repetition in ns */
132+
};
133+
134+
LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
135+
136+
/*
137+
* bpf_prog_test_run does not check that data_out is large enough. Consider
138+
* using bpf_prog_test_run_xattr instead.
139+
*/
121140
LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
122141
__u32 size, void *data_out, __u32 *size_out,
123142
__u32 *retval, __u32 *duration);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ LIBBPF_0.0.1 {
6565
bpf_prog_load_xattr;
6666
bpf_prog_query;
6767
bpf_prog_test_run;
68+
bpf_prog_test_run_xattr;
6869
bpf_program__fd;
6970
bpf_program__is_kprobe;
7071
bpf_program__is_perf_event;

tools/testing/selftests/bpf/test_progs.c

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static struct {
7070
.tcp.urg_ptr = 123,
7171
};
7272

73-
#define CHECK(condition, tag, format...) ({ \
73+
#define _CHECK(condition, tag, duration, format...) ({ \
7474
int __ret = !!(condition); \
7575
if (__ret) { \
7676
error_cnt++; \
@@ -83,6 +83,11 @@ static struct {
8383
__ret; \
8484
})
8585

86+
#define CHECK(condition, tag, format...) \
87+
_CHECK(condition, tag, duration, format)
88+
#define CHECK_ATTR(condition, tag, format...) \
89+
_CHECK(condition, tag, tattr.duration, format)
90+
8691
static int bpf_find_map(const char *test, struct bpf_object *obj,
8792
const char *name)
8893
{
@@ -124,6 +129,53 @@ static void test_pkt_access(void)
124129
bpf_object__close(obj);
125130
}
126131

132+
static void test_prog_run_xattr(void)
133+
{
134+
const char *file = "./test_pkt_access.o";
135+
struct bpf_object *obj;
136+
char buf[10];
137+
int err;
138+
struct bpf_prog_test_run_attr tattr = {
139+
.repeat = 1,
140+
.data_in = &pkt_v4,
141+
.data_size_in = sizeof(pkt_v4),
142+
.data_out = buf,
143+
.data_size_out = 5,
144+
};
145+
146+
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj,
147+
&tattr.prog_fd);
148+
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
149+
return;
150+
151+
memset(buf, 0, sizeof(buf));
152+
153+
err = bpf_prog_test_run_xattr(&tattr);
154+
CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run",
155+
"err %d errno %d retval %d\n", err, errno, tattr.retval);
156+
157+
CHECK_ATTR(tattr.data_size_out != sizeof(pkt_v4), "data_size_out",
158+
"incorrect output size, want %lu have %u\n",
159+
sizeof(pkt_v4), tattr.data_size_out);
160+
161+
CHECK_ATTR(buf[5] != 0, "overflow",
162+
"BPF_PROG_TEST_RUN ignored size hint\n");
163+
164+
tattr.data_out = NULL;
165+
tattr.data_size_out = 0;
166+
errno = 0;
167+
168+
err = bpf_prog_test_run_xattr(&tattr);
169+
CHECK_ATTR(err || errno || tattr.retval, "run_no_output",
170+
"err %d errno %d retval %d\n", err, errno, tattr.retval);
171+
172+
tattr.data_size_out = 1;
173+
err = bpf_prog_test_run_xattr(&tattr);
174+
CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err);
175+
176+
bpf_object__close(obj);
177+
}
178+
127179
static void test_xdp(void)
128180
{
129181
struct vip key4 = {.protocol = 6, .family = AF_INET};
@@ -1837,6 +1889,7 @@ int main(void)
18371889
jit_enabled = is_jit_enabled();
18381890

18391891
test_pkt_access();
1892+
test_prog_run_xattr();
18401893
test_xdp();
18411894
test_xdp_adjust_tail();
18421895
test_l4lb_all();

0 commit comments

Comments
 (0)