[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <2025042509-french-washbowl-5cde@gregkh>
Date: Fri, 25 Apr 2025 15:52:16 +0200
From: Greg Kroah-Hartman <gregkh@...uxfoundation.org>
To: Boqun Feng <boqun.feng@...il.com>
Cc: Alice Ryhl <aliceryhl@...gle.com>, Miguel Ojeda <ojeda@...nel.org>,
Andrew Morton <akpm@...ux-foundation.org>,
Alexander Viro <viro@...iv.linux.org.uk>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...nel.org>,
Trevor Gross <tmgross@...ch.edu>,
Danilo Krummrich <dakr@...nel.org>, rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH] uaccess: rust: add strncpy_from_user
On Fri, Apr 25, 2025 at 06:39:25AM -0700, Boqun Feng wrote:
> On Fri, Apr 25, 2025 at 09:43:30AM +0000, Alice Ryhl wrote:
> > On Thu, Apr 24, 2025 at 08:57:13AM -0700, Boqun Feng wrote:
> > > On Thu, Apr 24, 2025 at 03:17:48PM +0000, Alice Ryhl wrote:
> > > > This is needed for ioctls that operate on a user-provided string.
> > > >
> > > > It is somewhat unfortunate that strncpy_from_user does not nul-terminate
> > > > the string when the end of `buf` is reached. This implies that we can't
> > > > return a &CStr from the function, since the buffer may not always be
> > > > nul-terminated.
> > > >
> > > > That said, we could add more convenient helpers on top that add a NUL
> > > > byte in that case.
> > > >
> > > > This method isn't defined on UserSliceReader because it complicates the
> > > > semantics. The UserSliceReader type also has its own maximum length, so
> > > > we would have to limit the read by that length too.
> > > >
> > > > Signed-off-by: Alice Ryhl <aliceryhl@...gle.com>
> > > > ---
> > > > rust/kernel/uaccess.rs | 27 +++++++++++++++++++++++++++
> > > > 1 file changed, 27 insertions(+)
> > > >
> > > > diff --git a/rust/kernel/uaccess.rs b/rust/kernel/uaccess.rs
> > > > index 80a9782b1c6e98ed6eae308ade8551afa7adc188..1bd82045e81ea887008e30241bd6de27f096b639 100644
> > > > --- a/rust/kernel/uaccess.rs
> > > > +++ b/rust/kernel/uaccess.rs
> > > > @@ -369,3 +369,30 @@ pub fn write<T: AsBytes>(&mut self, value: &T) -> Result {
> > > > Ok(())
> > > > }
> > > > }
> > > > +
> > > > +/// Reads a nul-terminated string into `buf` and returns the length.
> > > > +///
> > > > +/// Fails with [`EFAULT`] if the read happens on a bad address. If the end of `buf` is reached,
> > > > +/// then the buffer will not be nul-terminated.
> > > > +#[inline]
> > > > +pub fn strncpy_from_user(ptr: UserPtr, buf: &mut [u8]) -> Result<usize> {
> > >
> > > Sorry maybe there is an email I'm missing, but could you provide more
> > > context of the usage?
> > >
> > > First the function name is a bit weird, because the 'n' in "strncpy"
> > > means the parameters should have an 'n' (i.e. length) in it, but there
> > > is none in the Rust version.
> >
> > There is a length! It's the length of `buf`. It's pretty normal that C
> > methods with a pointer and length become a Rust method with a slice.
> >
>
> That's exactly the point, no need to reuse a name from C if we have
> something better.
Up to point, us kernel developers are used to the C names, so keep it
close if at all possible, ESPECIALLY for just links/wrappers of C
functions like this one is.
> > The distinction between strcpy and strncpy in my eyes is that strcpy
> > reads until you find a NUL byte, whereas strncpy reads until you find a
> > NUL byte *or* you read a user-specified number of bytes. This method is
> > in the latter category.
> >
>
> Then copy_from_user_until_nul()? Or cstrcpy_from_user()? We should have
> a bit consistent naming on Rust side regardless how C names things IMO.
You need to specify a max length, otherwise that's just going to confuse
us all. strncpy_from_user() is the function we are used to using for
copying up to N number of bytes from userspace where a 0 termination
stops the copy if N isn't reached. So I vote highly for the original
name here please.
thanks,
greg k-h
Powered by blists - more mailing lists