diff mbox series

drm/tinydrm: fix a loop in debugfs code

Message ID 20190821072456.GJ26957@mwanda (mailing list archive)
State New, archived
Headers show
Series drm/tinydrm: fix a loop in debugfs code | expand

Commit Message

Dan Carpenter Aug. 21, 2019, 7:24 a.m. UTC
This code will likely crash if we try to do a zero byte write.  The code
looks like this:

        /* strip trailing whitespace */
        for (i = count - 1; i > 0; i--)
                if (isspace(buf[i]))
			...

We're writing zero bytes so count = 0.  You would think that "count - 1"
would be negative one, but because "i" is unsigned it is a large
positive numer instead.  The "i > 0" condition is true and the "buf[i]"
access will be out of bounds.

The fix is to make "i" signed and now everything works as expected.  The
upper bound of "count" is capped in __kernel_write() at MAX_RW_COUNT so
we don't have to worry about it being higher than INT_MAX.

Fixes: 02dd95fe3169 ("drm/tinydrm: Add MIPI DBI support")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/gpu/drm/drm_mipi_dbi.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Noralf Trønnes Aug. 26, 2019, 1:01 p.m. UTC | #1
Den 21.08.2019 09.24, skrev Dan Carpenter:
> This code will likely crash if we try to do a zero byte write.  The code
> looks like this:
> 
>         /* strip trailing whitespace */
>         for (i = count - 1; i > 0; i--)
>                 if (isspace(buf[i]))
> 			...
> 
> We're writing zero bytes so count = 0.  You would think that "count - 1"
> would be negative one, but because "i" is unsigned it is a large
> positive numer instead.  The "i > 0" condition is true and the "buf[i]"
> access will be out of bounds.
> 
> The fix is to make "i" signed and now everything works as expected.  The
> upper bound of "count" is capped in __kernel_write() at MAX_RW_COUNT so
> we don't have to worry about it being higher than INT_MAX.
> 
> Fixes: 02dd95fe3169 ("drm/tinydrm: Add MIPI DBI support")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---

Thanks Dan, applied to drm-misc-next with adjusted title:
-drm/tinydrm: fix a loop in debugfs code
+drm/mipi-dbi: fix a loop in debugfs code

Noralf.
diff mbox series

Patch

diff --git a/drivers/gpu/drm/drm_mipi_dbi.c b/drivers/gpu/drm/drm_mipi_dbi.c
index 1961f713aaab..c4ee2709a6f3 100644
--- a/drivers/gpu/drm/drm_mipi_dbi.c
+++ b/drivers/gpu/drm/drm_mipi_dbi.c
@@ -1187,8 +1187,7 @@  static ssize_t mipi_dbi_debugfs_command_write(struct file *file,
 	struct mipi_dbi_dev *dbidev = m->private;
 	u8 val, cmd = 0, parameters[64];
 	char *buf, *pos, *token;
-	unsigned int i;
-	int ret, idx;
+	int i, ret, idx;
 
 	if (!drm_dev_enter(&dbidev->drm, &idx))
 		return -ENODEV;