iio: hw_consumer: Use struct_size() helper in kzalloc()

Make use of the struct_size() helper instead of an open-coded version,
in order to avoid any potential type mistakes or integer overflows that,
in the worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/iio/buffer/industrialio-hw-consumer.c:63:23: warning: using sizeof on a flexible structure when using CF='-Wflexible-array-sizeof'

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20220120225243.GA37225@embeddedor
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
This commit is contained in:
Gustavo A. R. Silva 2022-01-20 16:52:43 -06:00 committed by Jonathan Cameron
parent c39010ea6b
commit 2d255ec510
1 changed files with 2 additions and 2 deletions

View File

@ -52,7 +52,6 @@ static const struct iio_buffer_access_funcs iio_hw_buf_access = {
static struct hw_consumer_buffer *iio_hw_consumer_get_buffer( static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
struct iio_hw_consumer *hwc, struct iio_dev *indio_dev) struct iio_hw_consumer *hwc, struct iio_dev *indio_dev)
{ {
size_t mask_size = BITS_TO_LONGS(indio_dev->masklength) * sizeof(long);
struct hw_consumer_buffer *buf; struct hw_consumer_buffer *buf;
list_for_each_entry(buf, &hwc->buffers, head) { list_for_each_entry(buf, &hwc->buffers, head) {
@ -60,7 +59,8 @@ static struct hw_consumer_buffer *iio_hw_consumer_get_buffer(
return buf; return buf;
} }
buf = kzalloc(sizeof(*buf) + mask_size, GFP_KERNEL); buf = kzalloc(struct_size(buf, scan_mask, BITS_TO_LONGS(indio_dev->masklength)),
GFP_KERNEL);
if (!buf) if (!buf)
return NULL; return NULL;