]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
staging:iio:kfifo_buf: Fix potential buffer overflow in iio_read_first_n_kfifo
authorLars-Peter Clausen <lars@metafoo.de>
Mon, 12 Dec 2011 08:33:14 +0000 (09:33 +0100)
committerGreg Kroah-Hartman <gregkh@suse.de>
Tue, 13 Dec 2011 00:51:03 +0000 (16:51 -0800)
n is the number of bytes to read, not the number of samples. So if there is
enough data available we will write to the userspace buffer beyond its bounds.
Fix this by copying n bytes maximum. Also round n down to the next multiple of
the sample size, so we will only read complete samples. If the buffer is too
small to hold at least one sample return -EINVAL.

Also update the documentation of read_first_n to reflect the fact that 'n' is
supposed to be in bytes and not in samples.

Acked-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
drivers/staging/iio/buffer.h
drivers/staging/iio/kfifo_buf.c

index c69709925ba21160563e166d5ba6861f946b3336..44593b28b36fbb0f86abbe9b3319ed91ce89e7f3 100644 (file)
@@ -21,7 +21,7 @@ struct iio_buffer;
  * @mark_in_use:       reference counting, typically to prevent module removal
  * @unmark_in_use:     reduce reference count when no longer using buffer
  * @store_to:          actually store stuff to the buffer
- * @read_first_n:      try to get a specified number of elements (must exist)
+ * @read_first_n:      try to get a specified number of bytes (must exist)
  * @mark_param_change: notify buffer that some relevant parameter has changed
  *                     Often this means the underlying storage may need to
  *                     change.
index d8867abd0a84f3b85361a1e28d983497583c04de..b69cca5e43ca997d62b70f906e1873d46d08dbc4 100644 (file)
@@ -160,7 +160,11 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
        int ret, copied;
        struct iio_kfifo *kf = iio_to_kfifo(r);
 
-       ret = kfifo_to_user(&kf->kf, buf, r->bytes_per_datum*n, &copied);
+       if (n < r->bytes_per_datum)
+               return -EINVAL;
+
+       n = rounddown(n, r->bytes_per_datum);
+       ret = kfifo_to_user(&kf->kf, buf, n, &copied);
 
        return copied;
 }