iio:buffer: make length types match kfifo types
PATCH 1/2 iio:buffer: make length types match kfifo types
Currently, we use int for buffer length and bytes_per_datum. However, kfifo uses unsigned int for length and size_t for element size. We need to make sure these matches or we will have bugs related to overflow (in the range between INT_MAX and UINT_MAX for length, for example).
In addition, set_bytes_per_datum uses size_t while bytes_per_datum is an int, which would cause bugs for large values of bytes_per_datum.
Change buffer length to use unsigned int and bytes_per_datum to use size_t.
--- drivers/iio/buffer/industrialio-buffer-dma.c | 2 +- drivers/iio/buffer/kfifo_buf.c | 4 ++-- include/linux/iio/buffer_impl.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)
drivers/iio/buffer/industrialio-buffer-dma.c +1 -1
@@ -587,7 +587,7 @@ EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);* Should be used as the set_length callback for iio_buffer_access_ops * struct for DMA buffers. */ -int iio_dma_buffer_set_length (struct iio_buffer *buffer,int length) +int iio_dma_buffer_set_length (struct iio_buffer *buffer,unsigned int length) {/* Avoid an invalid state */ if (length <2 )
drivers/iio/buffer/kfifo_buf.c +2 -2
@@ -22,7 +22,7 @@ struct iio_kfifo {#define iio_to_kfifo (r) container_of(r, struct iio_kfifo, buffer)static inline int __iio_allocate_kfifo (struct iio_kfifo *buf, -int bytes_per_datum,int length) +size_t bytes_per_datum,unsigned int length) {if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ;
@@ -67,7 +67,7 @@ static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)return 0 ; } -static int iio_set_length_kfifo (struct iio_buffer *r,int length) +static int iio_set_length_kfifo (struct iio_buffer *r,unsigned int length) {/* Avoid an invalid state */ if (length <2 )
include/linux/iio/buffer_impl.h +3 -3
@@ -53,7 +53,7 @@ struct iio_buffer_access_funcs {int (*request_update)(struct iio_buffer *buffer);int (*set_bytes_per_datum)(struct iio_buffer *buffer,size_t bpd); -int (*set_length)(struct iio_buffer *buffer,int length); +int (*set_length)(struct iio_buffer *buffer,unsigned int length);int (*enable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);int (*disable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);
@@ -72,10 +72,10 @@ struct iio_buffer_access_funcs {*/ struct iio_buffer {/** @length: Number of datums in buffer. */ -int length; +unsigned int length;/** @bytes_per_datum: Size of individual datum including timestamp. */ -int bytes_per_datum; +size_t bytes_per_datum;/** * @access: Buffer access functions associated with the
Signature · 1 line
2.11.0
PATCH 2/2 iio:kfifo_buf: check for uint overflow
Currently, the following causes a kernel OOPS in memcpy:
echo 1073741825 > buffer/length
echo 1 > buffer/enable
Note that using 1073741824 instead of 1073741825 causes "write error:
Cannot allocate memory" but no OOPS.
This is because 1073741824 == 2^30 and 1073741825 == 2^30+1. Since kfifo rounds up to the nearest power of 2, it will actually call kmalloc with roundup_pow_of_two(length) * bytes_per_datum.
Using length == 1073741825 and bytes_per_datum == 2, we get:
kmalloc(roundup_pow_of_two(1073741825) * 2
or kmalloc(2147483648 * 2)
or kmalloc(4294967296)
or kmalloc(UINT_MAX + 1)
so this overflows to 0, causing kmalloc to return ZERO_SIZE_PTR and subsequent memcpy to fail once the device is enabled.
Fix this by checking for overflow prior to allocating a kfifo. With this check added, the above code returns -EINVAL when enabling the buffer, rather than causing an OOPS.
--- drivers/iio/buffer/kfifo_buf.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/iio/buffer/kfifo_buf.c +7 -0
@@ -27,6 +27,13 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ; +/* +* Make sure we don't overflow an unsigned int after kfifo rounds up to +* the next power of 2. +*/ +if (roundup_pow_of_two (length) >UINT_MAX / bytes_per_datum) +return -EINVAL ; +return __kfifo_alloc ((struct __kfifo *)&buf->kf, length, bytes_per_datum,GFP_KERNEL ); }
Signature · 1 line
2.11.0
Re: [PATCH 2/2] iio:kfifo_buf: check for uint overflow
Quoted 19 lines · On Mon, 26 Mar 2018 14:27:52 -0700 Martin Kelly <[email protected]> wrote:
Currently, the following causes a kernel OOPS in memcpy:
echo 1073741825 > buffer/length
echo 1 > buffer/enableNote that using 1073741824 instead of 1073741825 causes "write error:
Cannot allocate memory" but no OOPS.This is because 1073741824 == 2^30 and 1073741825 == 2^30+1. Since kfifo rounds up to the nearest power of 2, it will actually call kmalloc with
roundup_pow_of_two(length)*bytes_per_datum.Using length == 1073741825 and
bytes_per_datum== 2, we get:kmalloc(roundup_pow_of_two(1073741825) * 2
orkmalloc(2147483648 * 2)
or kmalloc(4294967296)
orkmalloc(UINT_MAX + 1)
Quoted 8 lines
so this overflows to 0, causing kmalloc to return
ZERO_SIZE_PTRand subsequent memcpy to fail once the device is enabled.Fix this by checking for overflow prior to allocating a kfifo. With this check added, the above code returns
-EINVALwhen enabling the buffer, rather than causing an OOPS.Signed-off-by Martin Kelly <[email protected]>
Applied to the fixes-togreg branch of iio.git and marked for stable.
I thought about this for a few mins. A 4 gig allocation on a 32bit machine is going to fail anyway for obvious reasons, but good to protect against overflow if someone were to write this value.
Particularly good description btw!
Thanks,
Jonathan
Quoted 22 lines
--- drivers/iio/buffer/kfifo_buf.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/iio/buffer/kfifo_buf.c +7
-0@@ -27,6 +27,13 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ; +/* +* Make sure we don't overflow an unsigned int after kfifo rounds up to +* the next power of 2. +*/ +if (roundup_pow_of_two (length) >UINT_MAX / bytes_per_datum) +return -EINVAL ; +return __kfifo_alloc ((struct __kfifo *)&buf->kf, length, bytes_per_datum,GFP_KERNEL ); }
Re: [PATCH 2/2] iio:kfifo_buf: check for uint overflow
Quoted 29 lines · Jonathan Cameron
Quoted 26 lines · On Mon, 26 Mar 2018 14:27:52 -0700 Martin Kelly <[email protected]> wrote:
Currently, the following causes a kernel OOPS in memcpy:
echo 1073741825 > buffer/length
echo 1 > buffer/enableNote that using 1073741824 instead of 1073741825 causes "write error:
Cannot allocate memory" but no OOPS.This is because 1073741824 == 2^30 and 1073741825 == 2^30+1. Since kfifo rounds up to the nearest power of 2, it will actually call kmalloc with
roundup_pow_of_two(length)*bytes_per_datum.Using length == 1073741825 and
bytes_per_datum== 2, we get:kmalloc(roundup_pow_of_two(1073741825) * 2
orkmalloc(2147483648 * 2)
or kmalloc(4294967296)
orkmalloc(UINT_MAX + 1)so this overflows to 0, causing kmalloc to return
ZERO_SIZE_PTRand subsequent memcpy to fail once the device is enabled.Fix this by checking for overflow prior to allocating a kfifo. With this check added, the above code returns
-EINVALwhen enabling the buffer, rather than causing an OOPS.
Quoted 7 lines
Quoted 1 line
Signed-off-by Martin Kelly <[email protected]>Applied to the fixes-togreg branch of iio.git and marked for stable.
I thought about this for a few mins. A 4 gig allocation on a 32bit machine is going to fail anyway for obvious reasons, but good to protect against overflow if someone were to write this value.
Yep, I found this by accident when my userspace had a bug, and then I just got curious about what was happening. Such clean, isolated bugs like this are rare and fun to find :).
Quoted 30 lines
Particularly good description btw!
Thanks,
Jonathan
Quoted 22 lines
--- drivers/iio/buffer/kfifo_buf.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/iio/buffer/kfifo_buf.c +7
-0@@ -27,6 +27,13 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ; +/* +* Make sure we don't overflow an unsigned int after kfifo rounds up to +* the next power of 2. +*/ +if (roundup_pow_of_two (length) >UINT_MAX / bytes_per_datum) +return -EINVAL ; +return __kfifo_alloc ((struct __kfifo *)&buf->kf, length, bytes_per_datum,GFP_KERNEL ); }
Jonathan Cameron
maintainerQuoted 5 lines · On Mon, 26 Mar 2018 14:27:51 -0700 Martin Kelly <[email protected]> wrote:
Currently, we use int for buffer length and
bytes_per_datum. However, kfifo uses unsigned int for length andsize_tfor element size. We need to make sure these matches or we will have bugs related to overflow (in the range betweenINT_MAXandUINT_MAXfor length, for example).
Quoted 7 lines
In addition,
set_bytes_per_datumusessize_twhilebytes_per_datumis an int, which would cause bugs for large values ofbytes_per_datum.Change buffer length to use unsigned int and
bytes_per_datumto usesize_t.Signed-off-by Martin Kelly <[email protected]>
Good bit of cleanup.
I'm fine to take these but I'm not going to rush them in as fixes for the obvious reason that it's pretty insane to have a buffer of anywhere near the sizes where this is going to go wrong.
Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with them.
Thanks,
Jonathan
Quoted 67 lines
--- drivers/iio/buffer/industrialio-buffer-dma.c | 2 +- drivers/iio/buffer/kfifo_buf.c | 4 ++-- include/linux/iio/buffer_impl.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)drivers/iio/buffer/industrialio-buffer-dma.c +1
-1@@ -587,7 +587,7 @@ EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);* Should be used as the set_length callback for iio_buffer_access_ops * struct for DMA buffers. */ -int iio_dma_buffer_set_length (struct iio_buffer *buffer,int length) +int iio_dma_buffer_set_length (struct iio_buffer *buffer,unsigned int length) {/* Avoid an invalid state */ if (length <2 )drivers/iio/buffer/kfifo_buf.c +2
-2@@ -22,7 +22,7 @@ struct iio_kfifo {#define iio_to_kfifo (r) container_of(r, struct iio_kfifo, buffer)static inline int __iio_allocate_kfifo (struct iio_kfifo *buf, -int bytes_per_datum,int length) +size_t bytes_per_datum,unsigned int length) {if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ;@@ -67,7 +67,7 @@ static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)return 0 ; } -static int iio_set_length_kfifo (struct iio_buffer *r,int length) +static int iio_set_length_kfifo (struct iio_buffer *r,unsigned int length) {/* Avoid an invalid state */ if (length <2 )include/linux/iio/buffer_impl.h +3
-3@@ -53,7 +53,7 @@ struct iio_buffer_access_funcs {int (*request_update)(struct iio_buffer *buffer);int (*set_bytes_per_datum)(struct iio_buffer *buffer,size_t bpd); -int (*set_length)(struct iio_buffer *buffer,int length); +int (*set_length)(struct iio_buffer *buffer,unsigned int length);int (*enable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);int (*disable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);@@ -72,10 +72,10 @@ struct iio_buffer_access_funcs {*/ struct iio_buffer {/** @length: Number of datums in buffer. */ -int length; +unsigned int length;/** @bytes_per_datum: Size of individual datum including timestamp. */ -int bytes_per_datum; +size_t bytes_per_datum;/** * @access: Buffer access functions associated with the
Quoted 15 lines · On Fri, 30 Mar 2018 11:10:42 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 5 lines · On Mon, 26 Mar 2018 14:27:51 -0700 Martin Kelly <[email protected]> wrote:
Currently, we use int for buffer length and
bytes_per_datum. However, kfifo uses unsigned int for length andsize_tfor element size. We need to make sure these matches or we will have bugs related to overflow (in the range betweenINT_MAXandUINT_MAXfor length, for example).Quoted 7 lines
In addition,
set_bytes_per_datumusessize_twhilebytes_per_datumis an int, which would cause bugs for large values ofbytes_per_datum.Change buffer length to use unsigned int and
bytes_per_datumto usesize_t.Signed-off-by Martin Kelly <[email protected]>
Quoted 8 lines
Good bit of cleanup.
I'm fine to take these but I'm not going to rush them in as fixes for the obvious reason that it's pretty insane to have a buffer of anywhere near the sizes where this is going to go wrong.
Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with them.
Actually I changed my mind on this after reading the second patch. Both added to the fixes-togreg branch of iio.git and marked for stable. Still bonkers sized buffers, but the results are nasty enough to make it sensible to deal with it.
So still not that urgent (which is handy given where we are in the cycle) but nice to clean up.
Jonathan
Quoted 77 lines
Thanks,
Jonathan
Quoted 57 lines
--- drivers/iio/buffer/industrialio-buffer-dma.c | 2 +- drivers/iio/buffer/kfifo_buf.c | 4 ++-- include/linux/iio/buffer_impl.h | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-)drivers/iio/buffer/industrialio-buffer-dma.c +1
-1@@ -587,7 +587,7 @@ EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);* Should be used as the set_length callback for iio_buffer_access_ops * struct for DMA buffers. */ -int iio_dma_buffer_set_length (struct iio_buffer *buffer,int length) +int iio_dma_buffer_set_length (struct iio_buffer *buffer,unsigned int length) {/* Avoid an invalid state */ if (length <2 )drivers/iio/buffer/kfifo_buf.c +2
-2@@ -22,7 +22,7 @@ struct iio_kfifo {#define iio_to_kfifo (r) container_of(r, struct iio_kfifo, buffer)static inline int __iio_allocate_kfifo (struct iio_kfifo *buf, -int bytes_per_datum,int length) +size_t bytes_per_datum,unsigned int length) {if ((length ==0 ) || (bytes_per_datum ==0 ))return -EINVAL ;@@ -67,7 +67,7 @@ static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)return 0 ; } -static int iio_set_length_kfifo (struct iio_buffer *r,int length) +static int iio_set_length_kfifo (struct iio_buffer *r,unsigned int length) {/* Avoid an invalid state */ if (length <2 )include/linux/iio/buffer_impl.h +1
-1@@ -53,7 +53,7 @@ struct iio_buffer_access_funcs {int (*request_update)(struct iio_buffer *buffer);int (*set_bytes_per_datum)(struct iio_buffer *buffer,size_t bpd); -int (*set_length)(struct iio_buffer *buffer,int length); +int (*set_length)(struct iio_buffer *buffer,unsigned int length);int (*enable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);int (*disable)(struct iio_buffer *buffer,struct iio_dev *indio_dev);@@ -72,10 +72,10 @@ struct iio_buffer_access_funcs {*/ struct iio_buffer {include/linux/iio/buffer_impl.h struct iio_buffer_access_funcs { · line 81
Quoted 10 lines
include/linux/iio/buffer_impl.h +2
-2/** @length: Number of datums in buffer. */ -int length; +unsigned int length;/** @bytes_per_datum: Size of individual datum including timestamp. */ -int bytes_per_datum; +size_t bytes_per_datum;/** * @access: Buffer access functions associated with the --
To unsubscribe from this list: send the line "unsubscribe linux-iio" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html