From: Kees Cook <keescook@chromium.org>
To: Ajay Garg <ajaygargnsit@gmail.com>
Cc: Greg KH <gregkh@linuxfoundation.org>,
andy@kernel.org, akpm@linux-foundation.org, adobriyan@gmail.com,
Nick Desaulniers <ndesaulniers@google.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
linux-hardening@vger.kernel.org
Subject: Re: RFC for a new string-copy function, using mixtures of strlcpy and strscpy
Date: Mon, 8 Nov 2021 10:05:25 -0800 [thread overview]
Message-ID: <202111080954.B97F7B4C@keescook> (raw)
In-Reply-To: <CAHP4M8W2H4V=qgAeVp76GwfVUBzR3erZxJiuhm6jnyMo+gvknQ@mail.gmail.com>
On Mon, Nov 08, 2021 at 06:15:54PM +0530, Ajay Garg wrote:
> Hi Greg,
>
> Thanks for your time.
>
> >
> > Wait, why? We have recently added new string copy functions to resolve
> > the type of issues you have pointed out. The kernel is not yet
> > converted to use them, so why add yet-another-function that also is not
> > used?
>
> Greg, would request your couple of minutes more, in suggesting a
> concrete way forward, by working through an example as below.
>
>
> In the file fs/kernfs/dir.c, there is a statement
>
> return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
>
> Here, there is no information of how long kn->name might be, so there
> is a definite chance of overflow happening. Thus, accordingly, strlcpy
> is used, to bound copying of upto buflen bytes. Of course, buf
> (destination-buffer) is safe from any overflow now.
>
> However, iffff strlen(kn->name) is greater than (buflen - 1), then
> strlcpy would return a different value than the number of bytes
> actually copied. Since there is no check, this (wrong) return value
> will be propagated to the clients down the stack.
The propagation issues are the real problem here, IMO. strlcpy returns
strlen(src), which is bound to cause problems if the result in used to
adjust string index, and strscpy returns -E2BIG on overflow, which can
cause different problems if the result in used to adjust string indexes.
strlcpy has the added problem of potentially performing OOB reads when
src is unterminated, so it needs to be entirely removed from the kernel
in favor of strscpy[1]. But yes, as you say, it isn't a drop-in
replacement, but that's because it shouldn't be -- the return values
need to be checked in both cases.
> Now, in the current situation, following is the remedy :
>
> ########################################
> int ret = strlcpy(buf, kn->parent ? kn->name : "/", buflen);
> if(ret >= buflen)
> ret = buflen - 1;
>
> return ret;
> ########################################
We do have other functions that "hide" the overflow (e.g. scnprintf),
but those are usually more common in large string constructions, or
chains of copies. When doing a single string copy, I think it's
important that the caller decide explicitly what to do in the overflow
case.
For the specific fs/kerfs/dir.c case, I don't see any problems --
nothing uses the result (cgroup_name() is the only caller of
kernfs_name() that I see).
--
Kees Cook
next prev parent reply other threads:[~2021-11-08 18:05 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-08 3:16 RFC for a new string-copy function, using mixtures of strlcpy and strscpy Ajay Garg
[not found] ` <CAHp75VcVZ6dDDm-k=Njo-jDq81bL4BTwrtkkAnm24b23qWKB_g@mail.gmail.com>
2021-11-08 7:42 ` Ajay Garg
2021-11-08 8:33 ` Andy Shevchenko
2021-11-08 8:46 ` Ajay Garg
2021-11-08 12:22 ` Greg KH
2021-11-08 12:45 ` Ajay Garg
2021-11-08 13:01 ` Greg KH
2021-11-08 13:19 ` Ajay Garg
2021-11-08 18:05 ` Kees Cook [this message]
2021-11-08 19:20 ` Ajay Garg
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=202111080954.B97F7B4C@keescook \
--to=keescook@chromium.org \
--cc=adobriyan@gmail.com \
--cc=ajaygargnsit@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andy@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ndesaulniers@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.