linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/8] lkdtm: change snprintf to scnprintf for possible overflow
@ 2019-01-12 15:28 Willy Tarreau
  2019-01-12 15:28 ` [PATCH 2/8] libertas: " Willy Tarreau
                   ` (8 more replies)
  0 siblings, 9 replies; 36+ messages in thread
From: Willy Tarreau @ 2019-01-12 15:28 UTC (permalink / raw)
  To: Silvio Cesare
  Cc: linux-kernel, Dan Carpenter, Kees Cook, Will Deacon, Greg KH

From: Silvio Cesare <silvio.cesare@gmail.com>

Change snprintf to scnprintf. There are generally two cases where using
snprintf causes problems.

1) Uses of size += snprintf(buf, SIZE - size, fmt, ...)
In this case, if snprintf would have written more characters than what the
buffer size (SIZE) is, then size will end up larger than SIZE. In later
uses of snprintf, SIZE - size will result in a negative number, leading
to problems. Note that size might already be too large by using
size = snprintf before the code reaches a case of size += snprintf.

2) If size is ultimately used as a length parameter for a copy back to user
space, then it will potentially allow for a buffer overflow and information
disclosure when size is greater than SIZE. When the size is used to index
the buffer directly, we can have memory corruption. This also means when
size = snprintf... is used, it may also cause problems since size may become
large.  Copying to userspace is mitigated by the HARDENED_USERCOPY kernel
configuration.

The solution to these issues is to use scnprintf which returns the number of
characters actually written to the buffer, so the size variable will never
exceed SIZE.

Signed-off-by: Silvio Cesare <silvio.cesare@gmail.com>
Cc: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Kees Cook <keescook@chromium.org>
Cc: Will Deacon <will.deacon@arm.com>
Cc: Greg KH <greg@kroah.com>
Signed-off-by: Willy Tarreau <w@1wt.eu>

---
 drivers/misc/lkdtm/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/misc/lkdtm/core.c b/drivers/misc/lkdtm/core.c
index 2837dc77478e..610aa3bfe630 100644
--- a/drivers/misc/lkdtm/core.c
+++ b/drivers/misc/lkdtm/core.c
@@ -347,9 +347,9 @@ static ssize_t lkdtm_debugfs_read(struct file *f, char __user *user_buf,
 	if (buf == NULL)
 		return -ENOMEM;
 
-	n = snprintf(buf, PAGE_SIZE, "Available crash types:\n");
+	n = scnprintf(buf, PAGE_SIZE, "Available crash types:\n");
 	for (i = 0; i < ARRAY_SIZE(crashtypes); i++) {
-		n += snprintf(buf + n, PAGE_SIZE - n, "%s\n",
+		n += scnprintf(buf + n, PAGE_SIZE - n, "%s\n",
 			      crashtypes[i].name);
 	}
 	buf[n] = '\0';
-- 
2.19.2


^ permalink raw reply related	[flat|nested] 36+ messages in thread

end of thread, other threads:[~2019-03-21  0:41 UTC | newest]

Thread overview: 36+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-12 15:28 [PATCH 1/8] lkdtm: change snprintf to scnprintf for possible overflow Willy Tarreau
2019-01-12 15:28 ` [PATCH 2/8] libertas: " Willy Tarreau
2019-01-15  1:09   ` Kees Cook
2019-01-15  5:55   ` Kalle Valo
2019-01-15 20:35     ` Willy Tarreau
2019-01-16 16:40       ` Kalle Valo
2019-01-16 17:02         ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 3/8] ocfs2: " Willy Tarreau
2019-01-15  1:14   ` Kees Cook
2019-01-12 15:28 ` [PATCH 4/8] ASoC: " Willy Tarreau
2019-01-15  1:13   ` Kees Cook
2019-01-15  1:25   ` Nicolin Chen
2019-01-15  3:18     ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 5/8] scsi: lpfc: " Willy Tarreau
2019-01-15  1:15   ` Kees Cook
2019-01-15 22:41     ` James Smart
2019-03-20 17:39       ` Greg KH
2019-03-20 20:27         ` James Smart
2019-03-21  0:41         ` James Smart
2019-01-12 15:28 ` [PATCH 6/8] ASoC: intel: skylake: " Willy Tarreau
2019-01-15  1:12   ` Kees Cook
2019-01-16 18:41   ` Kees Cook
2019-01-16 19:35     ` Pierre-Louis Bossart
2019-01-16 19:51       ` Kees Cook
2019-01-12 15:28 ` [PATCH 7/8] ASoC: dapm: " Willy Tarreau
2019-01-14 14:56   ` Mark Brown
2019-01-15  3:16     ` Willy Tarreau
2019-01-15 15:44       ` Mark Brown
2019-01-15 15:55         ` Willy Tarreau
2019-01-12 15:28 ` [PATCH 8/8] spi: dw: " Willy Tarreau
2019-01-15  1:09   ` Kees Cook
2019-01-15  1:02 ` [PATCH 1/8] lkdtm: " Kees Cook
2019-01-15  1:07   ` Kees Cook
2019-01-15  3:12   ` Willy Tarreau
2019-01-15 20:47 ` Kees Cook
2019-01-18 13:06   ` Greg KH

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).