Fix bpf_probe_read_user_str() overcopying
Subsystems (4)
v4 cover Fix bpf_probe_read_user_str() overcopying
6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers") introduced a subtle bug where bpf_probe_read_user_str() would potentially copy a few extra bytes after the NUL terminator.
This issue is particularly nefarious when strings are used as map keys, as seemingly identical strings can occupy multiple entries in a map.
This patchset fixes the issue and introduces a selftest to prevent future regressions.
v3 -> v4:
* directly pass userspace pointer to prog
* test more strings of different length
v2 -> v3:
* set pid filter before attaching prog in selftest
* use long instead of int as bpf_probe_read_user_str() retval
* style changes
v1 -> v2:
* add Fixes: tag
* add selftest
Daniel Xu (2):
lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes
after NUL
lib/strncpy_from_user.c | 9 ++- .../bpf/prog_tests/probe_read_user_str.c | 71 +++++++++++++++++++ .../bpf/progs/test_probe_read_user_str.c | 25 +++++++ 3 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/probe_read_user_str.c create mode 100644 tools/testing/selftests/bpf/progs/test_probe_read_user_str.c
Signature · 1 line
2.28.0
PATCH v4 1/2 lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator
do_strncpy_from_user() may copy some extra bytes after the NUL terminator into the destination buffer. This usually does not matter for normal string operations. However, when BPF programs key BPF maps with strings, this matters a lot.
A BPF program may read strings from user memory by calling the bpf_probe_read_user_str() helper which eventually calls do_strncpy_from_user(). The program can then key a map with the resulting string. BPF map keys are fixed-width and string-agnostic, meaning that map keys are treated as a set of bytes.
The issue is when do_strncpy_from_user() overcopies bytes after the NUL terminator, it can result in seemingly identical strings occupying multiple slots in a BPF map. This behavior is subtle and totally unexpected by the user.
This commit uses the proper word-at-a-time APIs to avoid overcopying.
--- lib/strncpy_from_user.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
lib/strncpy_from_user.c +7 -2
@@ -35,17 +35,22 @@ static inline long do_strncpy_from_user(char *dst, const char __user *src, goto byte_at_a_time;while (max >=sizeof (unsigned long )) { -unsigned long c, data; +unsigned long c, data, mask, *out;/* Fall back to byte-at-a-time if we get a page fault */ unsafe_get_user (c, (unsigned long __user *)(src+res), byte_at_a_time); - *(unsigned long *)(dst+res) = c;if (has_zero (c, &data, &constants)) { data =prep_zero_mask (c, data, &constants); data =create_zero_mask (data); + mask =zero_bytemask (data); + out = (unsigned long *)(dst+res); + *out = (*out & ~mask) | (c & mask);return res +find_zero (data); } + + *(unsigned long *)(dst+res) = c; + res +=sizeof (unsigned long ); max -=sizeof (unsigned long ); }
Signature · 1 line
2.28.0
Re: [PATCH bpf v4 1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator
Quoted 20 lines · Daniel Xu
do_strncpy_from_user()may copy some extra bytes after the NUL terminator into the destination buffer. This usually does not matter for normal string operations. However, when BPF programs key BPF maps with strings, this matters a lot.A BPF program may read strings from user memory by calling the
bpf_probe_read_user_str()helper which eventually callsdo_strncpy_from_user(). The program can then key a map with the resulting string. BPF map keys are fixed-width and string-agnostic, meaning that map keys are treated as a set of bytes.The issue is when
do_strncpy_from_user()overcopies bytes after the NUL terminator, it can result in seemingly identical strings occupying multiple slots in a BPF map. This behavior is subtle and totally unexpected by the user.
Quoted 4 lines
This commit uses the proper word-at-a-time APIs to avoid overcopying.
Fixes 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers")Signed-off-by Daniel Xu <[email protected]>
[...]
Re: [PATCH bpf v4 1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator
In reply to Daniel XuThis series was applied to bpf/bpf.git (refs/heads/master):
Hello:
This series was applied to bpf/bpf.git (refs/heads/master):
Quoted 5 lines · On Thu, 5 Nov 2020 16:06:34 -0800 you wrote:
do_strncpy_from_user()may copy some extra bytes after the NUL terminator into the destination buffer. This usually does not matter for normal string operations. However, when BPF programs key BPF maps with strings, this matters a lot.
Quoted 7 lines
A BPF program may read strings from user memory by calling the
bpf_probe_read_user_str()helper which eventually callsdo_strncpy_from_user(). The program can then key a map with the resulting string. BPF map keys are fixed-width and string-agnostic, meaning that map keys are treated as a set of bytes.[...]
Here is the summary with links:
- [bpf,v4,1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator
https://git.kernel.org/bpf/bpf/c/baca7f1c1c1e - [bpf,v4,2/2] selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes after NUL https://git.kernel.org/bpf/bpf/c/d18b184127de
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
PATCH v4 2/2 selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes after NUL
In reply to Daniel XuPreviously, bpf_probe_read_user_str() could potentially overcopy the trailing bytes after the NUL due to how do_strncpy_from_user() does the copy in long-sized strides. The issue has been fixed in the previous commit.
This commit adds a selftest that ensures we don't regress bpf_probe_read_user_str() again.
--- .../bpf/prog_tests/probe_read_user_str.c | 71 +++++++++++++++++++ .../bpf/progs/test_probe_read_user_str.c | 25 +++++++ 2 files changed, 96 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/probe_read_user_str.c create mode 100644 tools/testing/selftests/bpf/progs/test_probe_read_user_str.c
tools/testing/selftests/bpf/prog_tests/probe_read_user_str.c new file +71 -0
@@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <test_progs.h> +#include "test_probe_read_user_str.skel.h" + +static const char str1[] ="mestring" ; +static const char str2[] ="mestringalittlebigger" ; +static const char str3[] ="mestringblubblubblubblubblub" ; + +static int test_one_str (struct test_probe_read_user_str *skel,const char *str, +size_t len) +{ +int err, duration =0 ; +char buf[256 ]; + +/* Ensure bytes after string are ones */ +memset (buf,1 ,sizeof (buf)); +memcpy (buf, str, len); + +/* Give prog our userspace pointer */ + skel->bss->user_ptr = buf; + +/* Trigger tracepoint */ +usleep (1 ); + +/* Did helper fail? */ +if (CHECK (skel->bss->ret <0 ,"prog_ret" ,"prog returned: %ld\n" , + skel->bss->ret)) +return 1 ; + +/* Check that string was copied correctly */ + err =memcmp (skel->bss->buf, str, len); +if (CHECK (err,"memcmp" ,"prog copied wrong string" )) +return 1 ; + +/* Now check that no extra trailing bytes were copied */ +memset (buf,0 ,sizeof (buf)); + err =memcmp (skel->bss->buf + len, buf,sizeof (buf) - len); +if (CHECK (err,"memcmp" ,"trailing bytes were not stripped" )) +return 1 ; + +return 0 ; +} + +void test_probe_read_user_str (void ) +{ +struct test_probe_read_user_str *skel; +int err, duration =0 ; + + skel =test_probe_read_user_str__open_and_load (); +if (CHECK (!skel,"test_probe_read_user_str__open_and_load" , +"skeleton open and load failed\n" )) +return ; + +/* Give pid to bpf prog so it doesn't read from anyone else */ + skel->bss->pid =getpid (); + + err =test_probe_read_user_str__attach (skel); +if (CHECK (err,"test_probe_read_user_str__attach" , +"skeleton attach failed: %d\n" , err)) + goto out; + +if (test_one_str (skel, str1,sizeof (str1))) + goto out; +if (test_one_str (skel, str2,sizeof (str2))) + goto out; +if (test_one_str (skel, str3,sizeof (str3))) + goto out; + +out: +test_probe_read_user_str__destroy (skel); +}
tools/testing/selftests/bpf/progs/test_probe_read_user_str.c new file +25 -0
@@ -0,0 +1,25 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> + +#include <sys/types.h> + +pid_t pid =0 ; +long ret =0 ; +void *user_ptr =0 ; +char buf[256 ] = {}; + +SEC("tracepoint/syscalls/sys_enter_nanosleep") +int on_write (void *ctx) +{ +if (pid != (bpf_get_current_pid_tgid () >>32 )) +return 0 ; + + ret =bpf_probe_read_user_str (buf,sizeof (buf), user_ptr); + +return 0 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.28.0
Re: [PATCH bpf v4 2/2] selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes after NUL
Quoted 9 lines · Daniel Xu
Previously,
bpf_probe_read_user_str()could potentially overcopy the trailing bytes after the NUL due to howdo_strncpy_from_user()does the copy in long-sized strides. The issue has been fixed in the previous commit.
Quoted 4 lines
This commit adds a selftest that ensures we don't regress
bpf_probe_read_user_str()again.Signed-off-by Daniel Xu <[email protected]>
LGTM!
[...]
Re: [PATCH bpf v4 2/2] selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes after NUL
In reply to Daniel XuQuoted 11 lines · Daniel Xu
Previously,
bpf_probe_read_user_str()could potentially overcopy the trailing bytes after the NUL due to howdo_strncpy_from_user()does the copy in long-sized strides. The issue has been fixed in the previous commit.This commit adds a selftest that ensures we don't regress
bpf_probe_read_user_str()again.Signed-off-by Daniel Xu <[email protected]>---
Quoted 6 lines
.../bpf/prog_tests/probe_read_user_str.c | 71 +++++++++++++++++++ .../bpf/progs/test_probe_read_user_str.c | 25 +++++++ 2 files changed, 96 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/probe_read_user_str.c create mode 100644 tools/testing/selftests/bpf/progs/test_probe_read_user_str.c
[...]