spi: dw: change snprintf to scnprintf for possible overflow
authorSilvio Cesare <silvio.cesare@gmail.com>
Sat, 12 Jan 2019 15:28:44 +0000 (16:28 +0100)
committerMark Brown <broonie@kernel.org>
Tue, 15 Jan 2019 19:02:51 +0000 (19:02 +0000)
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>
Signed-off-by: Willy Tarreau <w@1wt.eu>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi-dw.c

index 2e822a56576acfa04afaf3c51064cdcd7d7d82c5..4c9deb434b3a281ae96f182ff3f9d578b6689382 100644 (file)
@@ -54,41 +54,41 @@ static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
        if (!buf)
                return 0;
 
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "%s registers:\n", dev_name(&dws->master->dev));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "=================================\n");
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
-       len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
+       len += scnprintf(buf + len, SPI_REGS_BUFSIZE - len,
                        "=================================\n");
 
        ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);