Skip to content

Commit b5a36b1

Browse files
lmbAlexei Starovoitov
authored andcommitted
bpf: respect size hint to BPF_PROG_TEST_RUN if present
Use data_size_out as a size hint when copying test output to user space. ENOSPC is returned if the output buffer is too small. Callers which so far did not set data_size_out are not affected. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent d59dd69 commit b5a36b1

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
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
}

0 commit comments

Comments
 (0)