iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()
authorYang Yingliang <yangyingliang@huawei.com>
Wed, 13 Oct 2021 09:49:22 +0000 (12:49 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sun, 17 Oct 2021 14:24:18 +0000 (15:24 +0100)
commit09776d9374e635b1580b3736c19b95b788fbaa85
treec28b48278b00b3ce45fb7ba6fa3b914cd47b9192
parent9a2ff8009e53296e47de72d5af0bc31cd53274ff
iio: buffer: Fix double-free in iio_buffers_alloc_sysfs_and_mask()

When __iio_buffer_alloc_sysfs_and_mask() failed, 'unwind_idx' should be
set to 'i - 1' to prevent double-free when cleanup resources.

BUG: KASAN: double-free or invalid-free in __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio]
Call Trace:
 kfree+0x117/0x4c0
 __iio_buffer_free_sysfs_and_mask+0x32/0xb0 [industrialio]
 iio_buffers_alloc_sysfs_and_mask+0x60d/0x1570 [industrialio]
 __iio_device_register+0x483/0x1a30 [industrialio]
 ina2xx_probe+0x625/0x980 [ina2xx_adc]

Reported-by: Hulk Robot <hulkci@huawei.com>
Fixes: ee708e6baacd ("iio: buffer: introduce support for attaching more IIO buffers")
Signed-off-by: Yang Yingliang <yangyingliang@huawei.com>
Reviewed-by: Alexandru Ardelean <ardeleanalex@gmail.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20211013094923.2473-2-andriy.shevchenko@linux.intel.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/industrialio-buffer.c