Skip to content

Commit afe0903

Browse files
Markus Burrigregkh
authored andcommitted
gpio: virtuser: fix potential out-of-bound write
[ Upstream commit 7118be7 ] If the caller wrote more characters, count is truncated to the max available space in "simple_write_to_buffer". Check that the input size does not exceed the buffer size. Write a zero termination afterwards. Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Signed-off-by: Markus Burri <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bartosz Golaszewski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent cef4f57 commit afe0903

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

drivers/gpio/gpio-virtuser.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,15 @@ static ssize_t gpio_virtuser_direction_do_write(struct file *file,
400400
char buf[32], *trimmed;
401401
int ret, dir, val = 0;
402402

403-
ret = simple_write_to_buffer(buf, sizeof(buf), ppos, user_buf, count);
403+
if (count >= sizeof(buf))
404+
return -EINVAL;
405+
406+
ret = simple_write_to_buffer(buf, sizeof(buf) - 1, ppos, user_buf, count);
404407
if (ret < 0)
405408
return ret;
406409

410+
buf[ret] = '\0';
411+
407412
trimmed = strim(buf);
408413

409414
if (strcmp(trimmed, "input") == 0) {
@@ -622,12 +627,15 @@ static ssize_t gpio_virtuser_consumer_write(struct file *file,
622627
char buf[GPIO_VIRTUSER_NAME_BUF_LEN + 2];
623628
int ret;
624629

630+
if (count >= sizeof(buf))
631+
return -EINVAL;
632+
625633
ret = simple_write_to_buffer(buf, GPIO_VIRTUSER_NAME_BUF_LEN, ppos,
626634
user_buf, count);
627635
if (ret < 0)
628636
return ret;
629637

630-
buf[strlen(buf) - 1] = '\0';
638+
buf[ret] = '\0';
631639

632640
ret = gpiod_set_consumer_name(data->ad.desc, buf);
633641
if (ret)

0 commit comments

Comments
 (0)