From: Jan Kara <jack@suse.cz>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Jan Kara <jack@suse.cz>,
Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/2] lib: Limit strnlen_user() return value to count + 1
Date: Wed, 3 Jun 2015 15:42:49 +0200 [thread overview]
Message-ID: <20150603134249.GA2270@quack.suse.cz> (raw)
In-Reply-To: <CA+55aFyndqgESxJgsYjXHhu82GBb4Y5ojPvJv4oWL_5iw3xSPw@mail.gmail.com>
On Wed 03-06-15 06:21:45, Linus Torvalds wrote:
> On Wed, Jun 3, 2015 at 2:21 AM, Jan Kara <jack@suse.cz> wrote:
> >
> > The comment is:
>
> Yeah, and the comment right above do_strnlen_user() that you ignored is:
>
> * NOTE! We can sometimes overshoot the user-supplied maximum
> * if it fits in a aligned 'long'. The caller needs to check
> * the return value against "> max".
>
> Which is pretty unambiguous.
>
> The thing is, "retval > max" shiould be considered an error condition.
> Exactly like 0 is, and the caller should check for that.
>
> I do agree that we should change the other comment too, though. I
> think there may have been some cutting-and-pasting when the code was
OK, I'll send a fix.
> > What they roughly did was:
> >
> > char buf[DM_ATTR_NAME_SIZE + 1];
> >
> > len = strnlen_user(from, DM_ATTR_NAME_SIZE);
> > if (!len)
> > return -EFAULT;
> > if (copy_from_user(buf, from, len))
> > return -EFAULT;
> > buf[len - 1] = 0;
>
> Yeah, don't do that.
>
> It's stupid code anyway.
>
> If what you wanted was "strncpy_from_user()", that's what you should have used.
>
> That function actually takes care to be exact, because it obviously
> has a destination buffer that it really cannot overshoot.
>
> So
>
> char buf[DM_ATTR_NAME_SIZE + 1];
>
> if (strncpy_from_user(buf, from, len) < 0)
> return -EFAULT;
> buf[DM_ATTR_NAME_SIZE] = 0;
>
> should actually work.
Yup, that's a good point.
> [ Side note: the generic strncpy_from_user() routine can be
> inefficient on architectures that handle unaligned accesses badly, but
> considering that it's used for copying pathnames from user space, I
> hope such architectures have their own optimized version ]
>
> I actually would like to get rid of "strnlen_user()" users as much as
> humanly possible. It's a fundamentally racy interface, since we don't
> control user memory, and another thread could change the string as it
> is being counted. There are cases where we have to use it (execve
> argument handling is I think the only real case of "yeah, we have no
> alternatives"), so we can't get rid of it entirely, but I basically
> don't believe in trying to make that interface at all easier to use.
Fair enough.
> I'd almost be inclined to unexport it. From a quick look, we don't
> have any module users.
Audit code (kernel/auditsc.c) uses it for arguments of executables so
that looks like a valid use from a module...
Honza
--
Jan Kara <jack@suse.cz>
SUSE Labs, CR
next prev parent reply other threads:[~2015-06-03 13:42 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-02 15:10 [PATCH 1/2] lib: Fix strnlen_user() to not touch memory after specified maximum Jan Kara
2015-06-02 15:10 ` [PATCH 2/2] lib: Limit strnlen_user() return value to count + 1 Jan Kara
2015-06-02 17:08 ` Linus Torvalds
2015-06-03 9:21 ` Jan Kara
2015-06-03 13:21 ` Linus Torvalds
2015-06-03 13:42 ` Jan Kara [this message]
2015-06-02 17:10 ` [PATCH 1/2] lib: Fix strnlen_user() to not touch memory after specified maximum Linus Torvalds
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=20150603134249.GA2270@quack.suse.cz \
--to=jack@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=torvalds@linux-foundation.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
all inboxes | Powered by JetHome®