Re: [PATCH v7] gpio: virtuser: new virtual driver

From: Dan Carpenter
Date: Wed May 29 2024 - 10:33:13 EST


On Mon, May 27, 2024 at 04:40:54PM +0200, Bartosz Golaszewski wrote:
> +static ssize_t
> +gpio_virtuser_sysfs_consumer_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t len)
> +{
> + struct gpio_virtuser_line_data *data = to_gpio_virtuser_data(attr);
> + int ret;
> +
> + if (strlen(buf) > GPIO_CONSUMER_NAME_MAX_LEN)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is off by one. strlen() doesn't count the NUL terminator.

> + return -EINVAL;
> +
> + guard(mutex)(&data->consumer_lock);
> +
> + ret = gpiod_set_consumer_name(data->desc, buf);
> + if (ret)
> + return ret;
> +
> + sprintf(data->consumer, buf);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It results in a buffer overflow here.

> +
> + return len;
> +}

regards,
dan carpenter