From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Daniel Xu <dxu@dxuuu.xyz>
Cc: bpf <bpf@vger.kernel.org>,
open list <linux-kernel@vger.kernel.org>,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Song Liu <songliubraving@fb.com>,
Kernel Team <kernel-team@fb.com>
Subject: Re: [PATCH bpf v5 1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator
Date: Wed, 11 Nov 2020 15:20:18 -0800 [thread overview]
Message-ID: <CAEf4BzbUVcRTb=CLkh6VPhPvgypOTACcDqScr0PcKHE80+5H4w@mail.gmail.com> (raw)
In-Reply-To: <f5eed57b42cc077d24807fc6f2f7b961d65691e5.1605134506.git.dxu@dxuuu.xyz>
On Wed, Nov 11, 2020 at 2:46 PM Daniel Xu <dxu@dxuuu.xyz> 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.
>
> 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 has strncpy start copying a byte at a time if a NUL is
> spotted.
>
> Fixes: 6ae08ae3dea2 ("bpf: Add probe_read_{user, kernel} and probe_read_{user, kernel}_str helpers")
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
> ---
This looks more immediately correct.
Acked-by: Andrii Nakryiko <andrii@kernel.org>
> lib/strncpy_from_user.c | 9 ++++-----
> 1 file changed, 4 insertions(+), 5 deletions(-)
>
> diff --git a/lib/strncpy_from_user.c b/lib/strncpy_from_user.c
> index e6d5fcc2cdf3..83180742e729 100644
> --- a/lib/strncpy_from_user.c
> +++ b/lib/strncpy_from_user.c
> @@ -40,12 +40,11 @@ static inline long do_strncpy_from_user(char *dst, const char __user *src,
> /* 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);
>
> + if (has_zero(c, &data, &constants))
> + goto 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);
> - return res + find_zero(data);
> - }
> +
> res += sizeof(unsigned long);
> max -= sizeof(unsigned long);
> }
> --
> 2.29.2
>
next prev parent reply other threads:[~2020-11-12 1:51 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-11 22:45 [PATCH bpf v5 0/2] Fix bpf_probe_read_user_str() overcopying Daniel Xu
2020-11-11 22:45 ` [PATCH bpf v5 1/2] lib/strncpy_from_user.c: Don't overcopy bytes after NUL terminator Daniel Xu
2020-11-11 23:20 ` Andrii Nakryiko [this message]
2020-11-13 17:03 ` Alexei Starovoitov
2020-11-13 18:08 ` Linus Torvalds
2020-11-13 19:17 ` Alexei Starovoitov
2020-11-13 19:29 ` Linus Torvalds
2020-11-13 19:46 ` Linus Torvalds
2020-11-13 20:10 ` Linus Torvalds
2020-11-13 20:57 ` Alexei Starovoitov
2020-11-13 21:14 ` Linus Torvalds
2020-11-11 22:45 ` [PATCH bpf v5 2/2] selftest/bpf: Test bpf_probe_read_user_str() strips trailing bytes after NUL Daniel Xu
2020-11-11 23:22 ` [PATCH bpf v5 0/2] Fix bpf_probe_read_user_str() overcopying Andrii Nakryiko
2020-11-12 19:10 ` Daniel Xu
2020-11-12 19:24 ` Andrii Nakryiko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAEf4BzbUVcRTb=CLkh6VPhPvgypOTACcDqScr0PcKHE80+5H4w@mail.gmail.com' \
--to=andrii.nakryiko@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=dxu@dxuuu.xyz \
--cc=kernel-team@fb.com \
--cc=linux-kernel@vger.kernel.org \
--cc=songliubraving@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).