From: Alexei Starovoitov <alexei.starovoitov@gmail.com>
To: Bagas Sanjaya <bagasdotme@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
"Steven Rostedt (Google)" <rostedt@goodmis.org>,
Max Froehling <Maximilian.Froehling@gdata.de>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Linux BPF <bpf@vger.kernel.org>,
Linux Memory Management List <linux-mm@kvack.org>
Subject: Re: bpf: bpf_probe_read_user_str() returns 0 for empty strings
Date: Sat, 22 Jul 2023 19:02:29 -0700 [thread overview]
Message-ID: <CAADnVQKJ+SzCEaXxpSKemJo8p0bCOGcoOv1NDsJMsTsMmJmiZQ@mail.gmail.com> (raw)
In-Reply-To: <bba66a5f-3605-e36b-2bf3-f25a48307a46@gmail.com>
On Sat, Jul 22, 2023 at 6:53 PM Bagas Sanjaya <bagasdotme@gmail.com> wrote:
>
> Hi,
>
> I notice a bug report on Bugzilla [1]. Quoting from it:
>
> > Overview:
> >
> > From within eBPF, calling the helper function bpf_probe_read_user_str(void *dst, __u32 size, const void *unsafe_ptr returns 0 when the source string (void *unsafe_ptr) consists of a string containing only a single null-byte.
> >
> > This violates various functions documentations (the helper and various internal kernel functions), which all state:
Sounds like the bugzilla author believes it's a documentation issue.
If so, please encourage the author to send the patch to fix the doc.
> >
> >> On success, the strictly positive length of the output string,
> >> including the trailing NUL character. On error, a negative value.
> >
> > To me, this states that the function should return 1 for char myString[] = ""; However, this is not the case. The function returns 0 instead.
> >
> > For non-empty strings, it works as expected. For example, char myString[] = "abc"; returns 4.
> >
> > Steps to Reproduce:
> > * Write an eBPF program that calls bpf_probe_read_user_str(), using a userspace pointer pointing to an empty string.
> > * Store the result value of that function
> > * Do the same thing, but try out bpf_probe_read_kernel_str(), like this:
> > char empty[] = "";
> > char copy[5];
> > long ret = bpf_probe_read_kernel_str(copy, 5, empty);
> > * Compare the return value of bpf_probe_read_user_str() and bpf_probe_read_kernel_str()
> >
> > Expected Result:
> >
> > Both functions return 1 (because of the single NULL byte).
> >
> > Actual Result:
> >
> > bpf_probe_read_user_str() returns 0, while bpf_probe_read_kernel_str() returns 1.
> >
> > Additional Information:
> >
> > I believe I can see the bug on the current Linux kernel master branch.
> >
> > In the file/function mm/maccess.c::strncpy_from_user_nofault() the helper implementation calls strncpy_from_user(), which returns the length without trailing 0. Hence this function returns 0 for an empty string.
> >
> > However, in line 192 (as of commit fdf0eaf11452d72945af31804e2a1048ee1b574c) there is a check that only increments ret, if it is > 0. This appears to be the logic that adds the trailing null byte. Since the check only does this for a ret > 0, a ret of 0 remains at 0.
> >
> > This is a possible off-by-one error that might cause the behavior.
>
> See Bugzilla for the full thread.
>
> FYI, the culprit line is introduced by commit 3d7081822f7f9e ("uaccess: Add
> non-pagefault user-space read functions"). I Cc: culprit SoB so that they
> can look into this bug.
>
> Thanks.
>
> [1]: https://bugzilla.kernel.org/show_bug.cgi?id=217679
>
> --
> An old man doll... just what I always wanted! - Clara
>
next prev parent reply other threads:[~2023-07-23 2:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-23 1:52 Fwd: " Bagas Sanjaya
2023-07-23 2:02 ` Alexei Starovoitov [this message]
2023-08-28 12:30 ` Fröhling, Maximilian
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=CAADnVQKJ+SzCEaXxpSKemJo8p0bCOGcoOv1NDsJMsTsMmJmiZQ@mail.gmail.com \
--to=alexei.starovoitov@gmail.com \
--cc=Maximilian.Froehling@gdata.de \
--cc=bagasdotme@gmail.com \
--cc=bpf@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhiramat@kernel.org \
--cc=mingo@kernel.org \
--cc=rostedt@goodmis.org \
/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