diff mbox series

iio: buffer: check return value of kstrdup_const()

Message ID 20211013040438.1689277-1-yangyingliang@huawei.com (mailing list archive)
State Accepted
Headers show
Series iio: buffer: check return value of kstrdup_const() | expand

Commit Message

Yang Yingliang Oct. 13, 2021, 4:04 a.m. UTC
Check return value of kstrdup_const() in iio_buffer_wrap_attr(),
or it will cause null-ptr-deref in kernfs_name_hash() when calling
device_add() as follows:

BUG: kernel NULL pointer dereference, address: 0000000000000000
RIP: 0010:strlen+0x0/0x20
Call Trace:
 kernfs_name_hash+0x22/0x110
 kernfs_find_ns+0x11d/0x390
 kernfs_remove_by_name_ns+0x3b/0xb0
 remove_files.isra.1+0x7b/0x190
 internal_create_group+0x7f1/0xbb0
 internal_create_groups+0xa3/0x150
 device_add+0x8f0/0x2020
 cdev_device_add+0xc3/0x160
 __iio_device_register+0x1427/0x1b40 [industrialio]
 __devm_iio_device_register+0x22/0x80 [industrialio]
 adjd_s311_probe+0x195/0x200 [adjd_s311]
 i2c_device_probe+0xa07/0xbb0

Reported-by: Hulk Robot <hulkci@huawei.com>
Fixes: 15097c7a1adc ("iio: buffer: wrap all buffer attributes into iio_dev_attr")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
---
 drivers/iio/industrialio-buffer.c | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Jonathan Cameron Oct. 17, 2021, 1:51 p.m. UTC | #1
On Wed, 13 Oct 2021 12:04:38 +0800
Yang Yingliang <yangyingliang@huawei.com> wrote:

> Check return value of kstrdup_const() in iio_buffer_wrap_attr(),
> or it will cause null-ptr-deref in kernfs_name_hash() when calling
> device_add() as follows:
> 
> BUG: kernel NULL pointer dereference, address: 0000000000000000
> RIP: 0010:strlen+0x0/0x20
> Call Trace:
>  kernfs_name_hash+0x22/0x110
>  kernfs_find_ns+0x11d/0x390
>  kernfs_remove_by_name_ns+0x3b/0xb0
>  remove_files.isra.1+0x7b/0x190
>  internal_create_group+0x7f1/0xbb0
>  internal_create_groups+0xa3/0x150
>  device_add+0x8f0/0x2020
>  cdev_device_add+0xc3/0x160
>  __iio_device_register+0x1427/0x1b40 [industrialio]
>  __devm_iio_device_register+0x22/0x80 [industrialio]
>  adjd_s311_probe+0x195/0x200 [adjd_s311]
>  i2c_device_probe+0xa07/0xbb0
> 
> Reported-by: Hulk Robot <hulkci@huawei.com>
> Fixes: 15097c7a1adc ("iio: buffer: wrap all buffer attributes into iio_dev_attr")
> Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>

Wang Wensheng sent another fix for this patch, but as that was missing the iio_attr free
I have picked up this one.

Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan

> ---
>  drivers/iio/industrialio-buffer.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
> index 5f4bd0b73d03..547a92d469ae 100644
> --- a/drivers/iio/industrialio-buffer.c
> +++ b/drivers/iio/industrialio-buffer.c
> @@ -1312,6 +1312,11 @@ static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
>  	iio_attr->buffer = buffer;
>  	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
>  	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
> +	if (!iio_attr->dev_attr.attr.name) {
> +		kfree(iio_attr);
> +		return NULL;
> +	}
> +
>  	sysfs_attr_init(&iio_attr->dev_attr.attr);
>  
>  	list_add(&iio_attr->l, &buffer->buffer_attr_list);
diff mbox series

Patch

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 5f4bd0b73d03..547a92d469ae 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -1312,6 +1312,11 @@  static struct attribute *iio_buffer_wrap_attr(struct iio_buffer *buffer,
 	iio_attr->buffer = buffer;
 	memcpy(&iio_attr->dev_attr, dattr, sizeof(iio_attr->dev_attr));
 	iio_attr->dev_attr.attr.name = kstrdup_const(attr->name, GFP_KERNEL);
+	if (!iio_attr->dev_attr.attr.name) {
+		kfree(iio_attr);
+		return NULL;
+	}
+
 	sysfs_attr_init(&iio_attr->dev_attr.attr);
 
 	list_add(&iio_attr->l, &buffer->buffer_attr_list);