From: Eryu Guan <eguan@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: Eryu Guan <eguan@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
Chris Metcalf <cmetcalf@ezchip.com>,
Kees Cook <keescook@chromium.org>
Subject: [PATCH] lib/string: avoid reading beyond src buffer in strscpy
Date: Thu, 7 Dec 2017 19:33:24 +0800 [thread overview]
Message-ID: <20171207113324.24388-1-eguan@redhat.com> (raw)
strscpy() tries to copy sizeof(unsigned long) bytes a time from src
to dest when possible, and stops the loop when 'max' is less than
sizeof(unsigned long). But it doesn't check if (src+res) goes beyond
src buffer and does out-of-bound access to the underlying memory.
KASAN reported global-out-of-bound bug when reading seccomp
actions_logged file in procfs:
cat /proc/sys/kernel/seccomp/actions_logged
Because seccomp_names_from_actions_logged() is copying short strings
(less than sizeof(unsigned long)) to buffer 'names'. e.g.
ret = strscpy(names, " ", size);
Fixed by capping the 'max' value according to the src buffer size,
to make sure we won't go beyond src buffer.
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Chris Metcalf <cmetcalf@ezchip.com>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: Eryu Guan <eguan@redhat.com>
---
lib/string.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/lib/string.c b/lib/string.c
index 64a9e33f1daa..13a0147eea00 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -179,6 +179,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
{
const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
size_t max = count;
+ size_t src_sz = strlen(src) + 1;
long res = 0;
if (count == 0)
@@ -200,6 +201,10 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
max = 0;
#endif
+ /* avoid reading beyond src buffer */
+ if (max > src_sz)
+ max = src_sz;
+
while (max >= sizeof(unsigned long)) {
unsigned long c, data;
--
2.14.3
next reply other threads:[~2017-12-07 11:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-07 11:33 Eryu Guan [this message]
2017-12-07 18:26 ` Kees Cook
2017-12-08 15:29 ` Andrey Ryabinin
2017-12-08 15:29 ` Dmitry Vyukov
2017-12-08 20:54 ` Kees Cook
2017-12-11 16:44 ` Andrey Ryabinin
2017-12-12 10:19 ` David Laight
2017-12-12 16:06 ` Andrey Ryabinin
2017-12-12 22:42 ` 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=20171207113324.24388-1-eguan@redhat.com \
--to=eguan@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=cmetcalf@ezchip.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.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
Powered by JetHome