Skip to content

Commit

Permalink
Merge branch 'Fix offset when fault occurs in strncpy_from_kernel_nof…
Browse files Browse the repository at this point in the history
…ault()'

Alban Crequy says:

====================

Hi,

This is v2 of the fix & selftest previously sent at:
https://lore.kernel.org/linux-mm/20221108195211.214025-1-flaniel@linux.microsoft.com/

Changes v1 to v2:
- add 'cc:stable', 'Fixes:' and review/ack tags
- update commitmsg and fix my email
- rebase on bpf tree and tag for bpf tree

Thanks!
====================

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
  • Loading branch information
anakryiko committed Nov 11, 2022
2 parents 5704bc7 + 9cd0948 commit c205726
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion mm/maccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ long strncpy_from_kernel_nofault(char *dst, const void *unsafe_addr, long count)
return src - unsafe_addr;
Efault:
pagefault_enable();
dst[-1] = '\0';
dst[0] = '\0';
return -EFAULT;
}

Expand Down
7 changes: 7 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/varlen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 5 additions & 0 deletions tools/testing/selftests/bpf/progs/test_varlen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
{
Expand All @@ -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;
}

Expand Down

0 comments on commit c205726

Please sign in to comment.