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>
This commit is contained in:
commit
2a95471c33
@ -374,8 +374,11 @@ union bpf_attr {
|
||||
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
|
||||
__u32 prog_fd;
|
||||
__u32 retval;
|
||||
__u32 data_size_in;
|
||||
__u32 data_size_out;
|
||||
__u32 data_size_in; /* input: len of data_in */
|
||||
__u32 data_size_out; /* input/output: len of data_out
|
||||
* returns ENOSPC if data_out
|
||||
* is too small.
|
||||
*/
|
||||
__aligned_u64 data_in;
|
||||
__aligned_u64 data_out;
|
||||
__u32 repeat;
|
||||
|
@ -74,8 +74,18 @@ static int bpf_test_finish(const union bpf_attr *kattr,
|
||||
{
|
||||
void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
|
||||
int err = -EFAULT;
|
||||
u32 copy_size = size;
|
||||
|
||||
if (data_out && copy_to_user(data_out, data, size))
|
||||
/* Clamp copy if the user has provided a size hint, but copy the full
|
||||
* buffer if not to retain old behaviour.
|
||||
*/
|
||||
if (kattr->test.data_size_out &&
|
||||
copy_size > kattr->test.data_size_out) {
|
||||
copy_size = kattr->test.data_size_out;
|
||||
err = -ENOSPC;
|
||||
}
|
||||
|
||||
if (data_out && copy_to_user(data_out, data, copy_size))
|
||||
goto out;
|
||||
if (copy_to_user(&uattr->test.data_size_out, &size, sizeof(size)))
|
||||
goto out;
|
||||
@ -83,7 +93,8 @@ static int bpf_test_finish(const union bpf_attr *kattr,
|
||||
goto out;
|
||||
if (copy_to_user(&uattr->test.duration, &duration, sizeof(duration)))
|
||||
goto out;
|
||||
err = 0;
|
||||
if (err != -ENOSPC)
|
||||
err = 0;
|
||||
out:
|
||||
return err;
|
||||
}
|
||||
|
@ -374,8 +374,11 @@ union bpf_attr {
|
||||
struct { /* anonymous struct used by BPF_PROG_TEST_RUN command */
|
||||
__u32 prog_fd;
|
||||
__u32 retval;
|
||||
__u32 data_size_in;
|
||||
__u32 data_size_out;
|
||||
__u32 data_size_in; /* input: len of data_in */
|
||||
__u32 data_size_out; /* input/output: len of data_out
|
||||
* returns ENOSPC if data_out
|
||||
* is too small.
|
||||
*/
|
||||
__aligned_u64 data_in;
|
||||
__aligned_u64 data_out;
|
||||
__u32 repeat;
|
||||
|
@ -463,6 +463,29 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
|
||||
{
|
||||
union bpf_attr attr;
|
||||
int ret;
|
||||
|
||||
if (!test_attr->data_out && test_attr->data_size_out > 0)
|
||||
return -EINVAL;
|
||||
|
||||
bzero(&attr, sizeof(attr));
|
||||
attr.test.prog_fd = test_attr->prog_fd;
|
||||
attr.test.data_in = ptr_to_u64(test_attr->data_in);
|
||||
attr.test.data_out = ptr_to_u64(test_attr->data_out);
|
||||
attr.test.data_size_in = test_attr->data_size_in;
|
||||
attr.test.data_size_out = test_attr->data_size_out;
|
||||
attr.test.repeat = test_attr->repeat;
|
||||
|
||||
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
|
||||
test_attr->data_size_out = attr.test.data_size_out;
|
||||
test_attr->retval = attr.test.retval;
|
||||
test_attr->duration = attr.test.duration;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
|
||||
{
|
||||
union bpf_attr attr;
|
||||
|
@ -118,6 +118,25 @@ LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
|
||||
LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
|
||||
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
|
||||
enum bpf_attach_type type);
|
||||
|
||||
struct bpf_prog_test_run_attr {
|
||||
int prog_fd;
|
||||
int repeat;
|
||||
const void *data_in;
|
||||
__u32 data_size_in;
|
||||
void *data_out; /* optional */
|
||||
__u32 data_size_out; /* in: max length of data_out
|
||||
* out: length of data_out */
|
||||
__u32 retval; /* out: return code of the BPF program */
|
||||
__u32 duration; /* out: average per repetition in ns */
|
||||
};
|
||||
|
||||
LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
|
||||
|
||||
/*
|
||||
* bpf_prog_test_run does not check that data_out is large enough. Consider
|
||||
* using bpf_prog_test_run_xattr instead.
|
||||
*/
|
||||
LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
|
||||
__u32 size, void *data_out, __u32 *size_out,
|
||||
__u32 *retval, __u32 *duration);
|
||||
|
@ -65,6 +65,7 @@ LIBBPF_0.0.1 {
|
||||
bpf_prog_load_xattr;
|
||||
bpf_prog_query;
|
||||
bpf_prog_test_run;
|
||||
bpf_prog_test_run_xattr;
|
||||
bpf_program__fd;
|
||||
bpf_program__is_kprobe;
|
||||
bpf_program__is_perf_event;
|
||||
|
@ -70,7 +70,7 @@ static struct {
|
||||
.tcp.urg_ptr = 123,
|
||||
};
|
||||
|
||||
#define CHECK(condition, tag, format...) ({ \
|
||||
#define _CHECK(condition, tag, duration, format...) ({ \
|
||||
int __ret = !!(condition); \
|
||||
if (__ret) { \
|
||||
error_cnt++; \
|
||||
@ -83,6 +83,11 @@ static struct {
|
||||
__ret; \
|
||||
})
|
||||
|
||||
#define CHECK(condition, tag, format...) \
|
||||
_CHECK(condition, tag, duration, format)
|
||||
#define CHECK_ATTR(condition, tag, format...) \
|
||||
_CHECK(condition, tag, tattr.duration, format)
|
||||
|
||||
static int bpf_find_map(const char *test, struct bpf_object *obj,
|
||||
const char *name)
|
||||
{
|
||||
@ -124,6 +129,53 @@ static void test_pkt_access(void)
|
||||
bpf_object__close(obj);
|
||||
}
|
||||
|
||||
static void test_prog_run_xattr(void)
|
||||
{
|
||||
const char *file = "./test_pkt_access.o";
|
||||
struct bpf_object *obj;
|
||||
char buf[10];
|
||||
int err;
|
||||
struct bpf_prog_test_run_attr tattr = {
|
||||
.repeat = 1,
|
||||
.data_in = &pkt_v4,
|
||||
.data_size_in = sizeof(pkt_v4),
|
||||
.data_out = buf,
|
||||
.data_size_out = 5,
|
||||
};
|
||||
|
||||
err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj,
|
||||
&tattr.prog_fd);
|
||||
if (CHECK_ATTR(err, "load", "err %d errno %d\n", err, errno))
|
||||
return;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
err = bpf_prog_test_run_xattr(&tattr);
|
||||
CHECK_ATTR(err != -1 || errno != ENOSPC || tattr.retval, "run",
|
||||
"err %d errno %d retval %d\n", err, errno, tattr.retval);
|
||||
|
||||
CHECK_ATTR(tattr.data_size_out != sizeof(pkt_v4), "data_size_out",
|
||||
"incorrect output size, want %lu have %u\n",
|
||||
sizeof(pkt_v4), tattr.data_size_out);
|
||||
|
||||
CHECK_ATTR(buf[5] != 0, "overflow",
|
||||
"BPF_PROG_TEST_RUN ignored size hint\n");
|
||||
|
||||
tattr.data_out = NULL;
|
||||
tattr.data_size_out = 0;
|
||||
errno = 0;
|
||||
|
||||
err = bpf_prog_test_run_xattr(&tattr);
|
||||
CHECK_ATTR(err || errno || tattr.retval, "run_no_output",
|
||||
"err %d errno %d retval %d\n", err, errno, tattr.retval);
|
||||
|
||||
tattr.data_size_out = 1;
|
||||
err = bpf_prog_test_run_xattr(&tattr);
|
||||
CHECK_ATTR(err != -EINVAL, "run_wrong_size_out", "err %d\n", err);
|
||||
|
||||
bpf_object__close(obj);
|
||||
}
|
||||
|
||||
static void test_xdp(void)
|
||||
{
|
||||
struct vip key4 = {.protocol = 6, .family = AF_INET};
|
||||
@ -1837,6 +1889,7 @@ int main(void)
|
||||
jit_enabled = is_jit_enabled();
|
||||
|
||||
test_pkt_access();
|
||||
test_prog_run_xattr();
|
||||
test_xdp();
|
||||
test_xdp_adjust_tail();
|
||||
test_l4lb_all();
|
||||
|
Loading…
Reference in New Issue
Block a user