Skip to content

Commit 7118be7

Browse files
Markus BurriBartosz Golaszewski
authored andcommitted
gpio: virtuser: fix potential out-of-bound write
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 <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202505091754.285hHbr2-lkp@intel.com/ Signed-off-by: Markus Burri <markus.burri@mt.com> Link: https://lore.kernel.org/r/20250509150459.115489-1-markus.burri@mt.com Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent 3e38f94 commit 7118be7

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
@@ -401,10 +401,15 @@ static ssize_t gpio_virtuser_direction_do_write(struct file *file,
401401
char buf[32], *trimmed;
402402
int ret, dir, val = 0;
403403

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

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

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

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

631-
buf[strlen(buf) - 1] = '\0';
639+
buf[ret] = '\0';
632640

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

0 commit comments

Comments
 (0)