selftests: bpf: Add a test when bpf_probe_read_kernel_str() returns EFAULT

This commit tests previous fix of bpf_probe_read_kernel_str().

The BPF helper bpf_probe_read_kernel_str should return -EFAULT when
given a bad source pointer and the target buffer should only be modified
to make the string NULL terminated.

bpf_probe_read_kernel_str() was previously inserting a NULL before the
beginning of the dst buffer. This test should ensure that the
implementation stays correct for now on.

Without the fix, this test will fail as follows:
  $ cd tools/testing/selftests/bpf
  $ make
  $ sudo ./test_progs --name=varlen
  ...
  test_varlen:FAIL:check got 0 != exp 66

Signed-off-by: Alban Crequy <albancrequy@linux.microsoft.com>
Signed-off-by: Francis Laniel <flaniel@linux.microsoft.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20221110085614.111213-3-albancrequy@linux.microsoft.com

Changes v1 to v2:
- add ack tag
- fix my email
- rebase on bpf tree and tag for bpf tree
This commit is contained in:
Alban Crequy 2022-11-10 09:56:14 +01:00 committed by Andrii Nakryiko
parent 8678ea0685
commit 9cd094829d
2 changed files with 12 additions and 0 deletions

View File

@ -63,6 +63,13 @@ void test_varlen(void)
CHECK_VAL(data->total4, size1 + size2);
CHECK(memcmp(data->payload4, exp_str, size1 + size2), "content_check",
"doesn't match!\n");
CHECK_VAL(bss->ret_bad_read, -EFAULT);
CHECK_VAL(data->payload_bad[0], 0x42);
CHECK_VAL(data->payload_bad[1], 0x42);
CHECK_VAL(data->payload_bad[2], 0);
CHECK_VAL(data->payload_bad[3], 0x42);
CHECK_VAL(data->payload_bad[4], 0x42);
cleanup:
test_varlen__destroy(skel);
}

View File

@ -19,6 +19,7 @@ __u64 payload1_len1 = 0;
__u64 payload1_len2 = 0;
__u64 total1 = 0;
char payload1[MAX_LEN + MAX_LEN] = {};
__u64 ret_bad_read = 0;
/* .data */
int payload2_len1 = -1;
@ -36,6 +37,8 @@ int payload4_len2 = -1;
int total4= -1;
char payload4[MAX_LEN + MAX_LEN] = { 1 };
char payload_bad[5] = { 0x42, 0x42, 0x42, 0x42, 0x42 };
SEC("raw_tp/sys_enter")
int handler64_unsigned(void *regs)
{
@ -61,6 +64,8 @@ int handler64_unsigned(void *regs)
total1 = payload - (void *)payload1;
ret_bad_read = bpf_probe_read_kernel_str(payload_bad + 2, 1, (void *) -1);
return 0;
}