Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Matthieu Baerts <matthieu.baerts@tessares.net>
To: stable@vger.kernel.org
Cc: asmadeus@codewreck.org, sashal@kernel.org,
	Andrey Ryabinin <aryabinin@virtuozzo.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Matthieu Baerts <matthieu.baerts@tessares.net>
Subject: [PATCH 3/3] lib/strscpy: Shut up KASAN false-positives in strscpy()
Date: Wed, 24 Jul 2019 22:55:57 +0200	[thread overview]
Message-ID: <20190724205557.30913-4-matthieu.baerts@tessares.net> (raw)
In-Reply-To: <20190724205557.30913-1-matthieu.baerts@tessares.net>

From: Andrey Ryabinin <aryabinin@virtuozzo.com>

commit 1a3241ff10d038ecd096d03380327f2a0b5840a6 upstream.

strscpy() performs the word-at-a-time optimistic reads.  So it may may
access the memory past the end of the object, which is perfectly fine
since strscpy() doesn't use that (past-the-end) data and makes sure the
optimistic read won't cross a page boundary.

Use new read_word_at_a_time() to shut up the KASAN.

Note that this potentially could hide some bugs.  In example bellow,
stscpy() will copy more than we should (1-3 extra uninitialized bytes):

        char dst[8];
        char *src;

        src = kmalloc(5, GFP_KERNEL);
        memset(src, 0xff, 5);
        strscpy(dst, src, 8);

Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Matthieu Baerts <matthieu.baerts@tessares.net>
---
 lib/string.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/string.c b/lib/string.c
index 1530643edf00..33befc6ba3fa 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -203,7 +203,7 @@ ssize_t strscpy(char *dest, const char *src, size_t count)
 	while (max >= sizeof(unsigned long)) {
 		unsigned long c, data;
 
-		c = *(unsigned long *)(src+res);
+		c = read_word_at_a_time(src+res);
 		if (has_zero(c, &data, &constants)) {
 			data = prep_zero_mask(c, data, &constants);
 			data = create_zero_mask(data);
-- 
2.20.1


  parent reply	other threads:[~2019-07-24 20:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-24 20:55 [PATCH 0/3] Backport request for v4.14 to fix KASAN issues Matthieu Baerts
2019-07-24 20:55 ` [PATCH 1/3] compiler.h, kasan: Avoid duplicating __read_once_size_nocheck() Matthieu Baerts
2019-07-24 20:55 ` [PATCH 2/3] compiler.h: Add read_word_at_a_time() function Matthieu Baerts
2019-07-24 20:55 ` Matthieu Baerts [this message]
2019-07-25  4:18 ` [PATCH 0/3] Backport request for v4.14 to fix KASAN issues Sasha Levin
2019-07-25  9:38   ` Matthieu Baerts

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=20190724205557.30913-4-matthieu.baerts@tessares.net \
    --to=matthieu.baerts@tessares.net \
    --cc=aryabinin@virtuozzo.com \
    --cc=asmadeus@codewreck.org \
    --cc=sashal@kernel.org \
    --cc=stable@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