Skip to content

Fix unsafe BPF_PROG_TEST_RUN interface #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion net/bpf/test_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ 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;

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;
Expand Down
4 changes: 3 additions & 1 deletion tools/lib/bpf/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
attr.test.data_in = ptr_to_u64(data);
attr.test.data_out = ptr_to_u64(data_out);
attr.test.data_size_in = size;
if (data_out)
attr.test.data_size_out = *size_out;
attr.test.repeat = repeat;

ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
if (size_out)
if (data_out)
*size_out = attr.test.data_size_out;
if (retval)
*retval = attr.test.retval;
Expand Down
41 changes: 40 additions & 1 deletion tools/testing/selftests/bpf/test_progs.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,40 @@ static void test_pkt_access(void)
bpf_object__close(obj);
}

static void test_output_size_hint(void)
{
const char *file = "./test_pkt_access.o";
struct bpf_object *obj;
__u32 retval, size, duration;
int err, prog_fd;
char buf[10];

err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd);
if (err) {
error_cnt++;
return;
}

memset(buf, 0, sizeof(buf));

size = 5;
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
CHECK(err || retval, "run",
"err %d errno %d retval %d\n",
err, errno, retval);

CHECK(size != sizeof(pkt_v4), "out_size",
"incorrect output size, want %lu have %u\n",
sizeof(pkt_v4), size);

CHECK(buf[5] != 0, "overflow",
"prog_test_run ignored size hint\n");

bpf_object__close(obj);
}


static void test_xdp(void)
{
struct vip key4 = {.protocol = 6, .family = AF_INET};
Expand All @@ -142,6 +176,7 @@ static void test_xdp(void)
bpf_map_update_elem(map_fd, &key4, &value4, 0);
bpf_map_update_elem(map_fd, &key6, &value6, 0);

size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);

Expand All @@ -150,6 +185,7 @@ static void test_xdp(void)
"err %d errno %d retval %d size %d\n",
err, errno, retval, size);

size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, 1, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || errno || retval != XDP_TX || size != 114 ||
Expand Down Expand Up @@ -214,13 +250,15 @@ static void test_l4lb(void)
goto out;
bpf_map_update_elem(map_fd, &real_num, &real_def, 0);

size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v4, sizeof(pkt_v4),
buf, &size, &retval, &duration);
CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 54 ||
*magic != MAGIC_VAL, "ipv4",
"err %d errno %d retval %d size %d magic %x\n",
err, errno, retval, size, *magic);


size = sizeof(buf);
err = bpf_prog_test_run(prog_fd, NUM_ITER, &pkt_v6, sizeof(pkt_v6),
buf, &size, &retval, &duration);
CHECK(err || errno || retval != 7/*TC_ACT_REDIRECT*/ || size != 74 ||
Expand Down Expand Up @@ -502,6 +540,7 @@ int main(void)
setrlimit(RLIMIT_MEMLOCK, &rinf);

test_pkt_access();
test_output_size_hint();
test_xdp();
test_l4lb();
test_tcp_estats();
Expand Down