On Sun, Feb 28, 2021 at 10:31 AM Lars-Peter Clausen <lars@xxxxxxxxxx> wrote: > > On 2/15/21 11:40 AM, Alexandru Ardelean wrote: > > static ssize_t iio_show_scan_index(struct device *dev, > > struct device_attribute *attr, > > char *buf) > > @@ -1451,11 +1465,13 @@ static void __iio_buffer_free_sysfs_and_mask(struct iio_buffer *buffer) > > iio_free_chan_devattr_list(&buffer->buffer_attr_list); > > } > > > > -int iio_buffer_alloc_sysfs_and_mask(struct iio_dev *indio_dev) > > +int iio_buffers_alloc_sysfs_and_mask(struct iio_dev *indio_dev) > > { > > [...] > > +error_unwind_sysfs_and_mask: > > + for (; unwind_idx >= 0; unwind_idx--) { > > + buffer = iio_dev_opaque->attached_buffers[unwind_idx]; > > + __iio_buffer_free_sysfs_and_mask(buffer); > > + } > > + kfree(iio_dev_opaque->attached_buffers); > > + return ret; > > } > > > > -void iio_buffer_free_sysfs_and_mask(struct iio_dev *indio_dev) > > +void iio_buffers_free_sysfs_and_mask(struct iio_dev *indio_dev) > > { > > [...] > > + for (i = iio_dev_opaque->attached_buffers_cnt - 1; i >= 0; i--) { > > + buffer = iio_dev_opaque->attached_buffers[i]; > > + __iio_buffer_free_sysfs_and_mask(buffer); > > + } > > + > > + kfree(iio_dev_opaque->attached_buffers); > > } > > [...] > > diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c > > index 1d500ea246af..f7f785431106 100644 > > --- a/drivers/iio/industrialio-core.c > > +++ b/drivers/iio/industrialio-core.c > > @@ -1583,7 +1583,7 @@ static void iio_dev_release(struct device *device) > > iio_device_unregister_eventset(indio_dev); > > iio_device_unregister_sysfs(indio_dev); > > > > - iio_buffer_put(indio_dev->buffer); > > + iio_buffers_put(indio_dev); > We do call kfree(iio_dev_opaque->attached_buffers) before we get here. I > think we need to keep the array around, otherwise we end of up with a > use after free. > Good catch. Will send an update here.