From: Andrii Nakryiko <andrii.nakryiko@gmail.com>
To: Jordan Rome <linux@jordanrome.com>
Cc: bpf@vger.kernel.org, linux-mm@kvack.org,
Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Kernel Team <kernel-team@fb.com>,
Andrew Morton <akpm@linux-foundation.org>,
Shakeel Butt <shakeel.butt@linux.dev>
Subject: Re: [bpf-next v2 1/2] bpf: Add bpf_copy_from_user_task_str kfunc
Date: Fri, 17 Jan 2025 10:26:20 -0800 [thread overview]
Message-ID: <CAEf4BzYaB=tEPrqbfX0AhcE=6YE5Rfjdk2fXtB-ejSFWd9g0Hg@mail.gmail.com> (raw)
In-Reply-To: <CA+QiOd4iq-UTxayaO7dACFdnJC=qM_FZi7=_LRqzvZEa7vgYEg@mail.gmail.com>
On Thu, Jan 16, 2025 at 6:22 PM Jordan Rome <linux@jordanrome.com> wrote:
>
> On Fri, Jan 10, 2025 at 4:50 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Mon, Jan 6, 2025 at 6:12 PM Jordan Rome <linux@jordanrome.com> wrote:
> > >
> > > This new kfunc will be able to copy a string
> > > from another process's/task's address space.
> >
> > nit: this is kernel code, task is unambiguous, so I'd drop the
> > "process" reference here
> >
> > > This is similar to `bpf_copy_from_user_str`
> > > but accepts a `struct task_struct*` argument.
> > >
> > > This required adding an additional function
> > > in memory.c, namely `copy_str_from_process_vm`,
> > > which works similar to `access_process_vm`
> > > but utilizes the `strncpy_from_user` helper
> > > and only supports reading/copying and not writing.
> > >
> > > Signed-off-by: Jordan Rome <linux@jordanrome.com>
> > > ---
> > > include/linux/mm.h | 3 ++
> > > kernel/bpf/helpers.c | 46 ++++++++++++++++++++
> > > mm/memory.c | 101 +++++++++++++++++++++++++++++++++++++++++++
> > > 3 files changed, 150 insertions(+)
> > >
> >
> > please check kernel test bot's complains as well
>
> Maybe I need an entry in nommu.c as well for 'copy_remote_vm_str'?
yep, probably, I see that access_remote_vm has two implementations as well
>
> >
> > > diff --git a/include/linux/mm.h b/include/linux/mm.h
> > > index c39c4945946c..52b304b20630 100644
> > > --- a/include/linux/mm.h
> > > +++ b/include/linux/mm.h
[...]
> > > +/*
> > > + * Copy a string from another process's address space as given in mm.
> > > + * Don't return partial results. If there is any error return -EFAULT.
> >
> > What does "don't return partial results" mean? What happens if we read
> > part of a string and then fail to read the rest?
>
> As per the last sentence, if we fail to read the rest of the string then
> an -EFAULT is returned.
This is confusing because the buffer will contain the partially read
string contents even with -EFAULT. And this "Don't return partial
results" can be interpreted as this API sanitizing the buffer (by
zeroing or something). So I'd drop the "Don't return partial results"
part, as it just creates confusion.
>
> >
> > > + */
[...]
> >
> > > + if (IS_ERR(page)) {
> > > + /*
> > > + * Treat as a total failure for now until we decide how
> > > + * to handle the CONFIG_HAVE_IOREMAP_PROT case and
> > > + * stack expansion.
> > > + */
> > > + err = -EFAULT;
> > > + break;
> > > + }
> > > +
> > > + bytes = len;
> > > + offset = addr & (PAGE_SIZE - 1);
> > > + if (bytes > PAGE_SIZE - offset)
> > > + bytes = PAGE_SIZE - offset;
> > > +
> > > + maddr = kmap_local_page(page);
> > > + retval = strncpy_from_user(buf, (const char __user *)addr, bytes);
> >
> > you are not using maddr... that seems wrong (even if it works due to
> > how kmap_local_page is currently implemented)
>
> How do you think we should handle it then?
Use (maddr + offset_within_the_page) instead of addr itself?
>
> >
> > > + unmap_and_put_page(page, maddr);
> > > +
> > > + if (retval < 0) {
> > > + err = retval;
> > > + break;
> > > + }
> > > +
> > > + len -= retval;
> > > + buf += retval;
> > > + addr += retval;
> > > +
> > > + /* Found the end of the string */
> > > + if (retval < bytes)
> > > + break;
> > > + }
> >
[...]
prev parent reply other threads:[~2025-01-17 18:26 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-07 2:06 Jordan Rome
2025-01-07 2:06 ` [bpf-next v2 2/2] selftests/bpf: Add tests for bpf_copy_from_user_task_str Jordan Rome
2025-01-10 22:01 ` Andrii Nakryiko
2025-01-16 4:57 ` kernel test robot
2025-01-09 1:13 ` [bpf-next v2 1/2] bpf: Add bpf_copy_from_user_task_str kfunc kernel test robot
2025-01-10 0:48 ` kernel test robot
2025-01-10 21:50 ` Andrii Nakryiko
2025-01-17 2:22 ` Jordan Rome
2025-01-17 18:26 ` Andrii Nakryiko [this message]
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='CAEf4BzYaB=tEPrqbfX0AhcE=6YE5Rfjdk2fXtB-ejSFWd9g0Hg@mail.gmail.com' \
--to=andrii.nakryiko@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@fb.com \
--cc=linux-mm@kvack.org \
--cc=linux@jordanrome.com \
--cc=shakeel.butt@linux.dev \
/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