aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio/kfifo_buf.c
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2013-10-15 04:30:00 -0400
committerJonathan Cameron <jic23@kernel.org>2013-10-15 14:19:22 -0400
commit0894d80dfddaeb9f95904ceab623460c1bfdab06 (patch)
tree7b482dcaa4b4db142fcabfe866ebe9a7829e7148 /drivers/iio/kfifo_buf.c
parentf6c23f483937b8be53f313ec31068acdca91a25d (diff)
iio:kfifo: Protect against concurrent access from userspace
It is possible for userspace to concurrently access the buffer from multiple threads or processes. To avoid corruption of the internal state of the buffer we need to add proper locking. It is possible for multiple processes to try to read from the buffer concurrently and it is also possible that one process causes a buffer re-allocation while a different process still access the buffer. Both can be fixed by protecting the calls to kfifo_to_user() and kfifo_alloc() by the same mutex. In iio_read_first_n_kfifo() we also use kfifo_recsize() instead of the buffers bytes_per_datum to avoid a race that can happen if bytes_per_datum has been changed, but the buffer has not been reallocated yet. Note that all access to the buffer from within the kernel is already properly synchronized, so there is no need for extra locking in iio_store_to_kfifo(). Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio/kfifo_buf.c')
-rw-r--r--drivers/iio/kfifo_buf.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/drivers/iio/kfifo_buf.c b/drivers/iio/kfifo_buf.c
index ce51092695ab..c95b61f60919 100644
--- a/drivers/iio/kfifo_buf.c
+++ b/drivers/iio/kfifo_buf.c
@@ -12,6 +12,7 @@
12struct iio_kfifo { 12struct iio_kfifo {
13 struct iio_buffer buffer; 13 struct iio_buffer buffer;
14 struct kfifo kf; 14 struct kfifo kf;
15 struct mutex user_lock;
15 int update_needed; 16 int update_needed;
16}; 17};
17 18
@@ -34,10 +35,12 @@ static int iio_request_update_kfifo(struct iio_buffer *r)
34 35
35 if (!buf->update_needed) 36 if (!buf->update_needed)
36 goto error_ret; 37 goto error_ret;
38 mutex_lock(&buf->user_lock);
37 kfifo_free(&buf->kf); 39 kfifo_free(&buf->kf);
38 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum, 40 ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
39 buf->buffer.length); 41 buf->buffer.length);
40 r->stufftoread = false; 42 r->stufftoread = false;
43 mutex_unlock(&buf->user_lock);
41error_ret: 44error_ret:
42 return ret; 45 return ret;
43} 46}
@@ -114,12 +117,13 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
114 int ret, copied; 117 int ret, copied;
115 struct iio_kfifo *kf = iio_to_kfifo(r); 118 struct iio_kfifo *kf = iio_to_kfifo(r);
116 119
117 if (n < r->bytes_per_datum || r->bytes_per_datum == 0) 120 if (mutex_lock_interruptible(&kf->user_lock))
118 return -EINVAL; 121 return -ERESTARTSYS;
119 122
120 ret = kfifo_to_user(&kf->kf, buf, n, &copied); 123 if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
121 if (ret < 0) 124 ret = -EINVAL;
122 return ret; 125 else
126 ret = kfifo_to_user(&kf->kf, buf, n, &copied);
123 127
124 if (kfifo_is_empty(&kf->kf)) 128 if (kfifo_is_empty(&kf->kf))
125 r->stufftoread = false; 129 r->stufftoread = false;
@@ -127,6 +131,10 @@ static int iio_read_first_n_kfifo(struct iio_buffer *r,
127 if (!kfifo_is_empty(&kf->kf)) 131 if (!kfifo_is_empty(&kf->kf))
128 r->stufftoread = true; 132 r->stufftoread = true;
129 133
134 mutex_unlock(&kf->user_lock);
135 if (ret < 0)
136 return ret;
137
130 return copied; 138 return copied;
131} 139}
132 140
@@ -134,6 +142,7 @@ static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
134{ 142{
135 struct iio_kfifo *kf = iio_to_kfifo(buffer); 143 struct iio_kfifo *kf = iio_to_kfifo(buffer);
136 144
145 mutex_destroy(&kf->user_lock);
137 kfifo_free(&kf->kf); 146 kfifo_free(&kf->kf);
138 kfree(kf); 147 kfree(kf);
139} 148}
@@ -161,6 +170,7 @@ struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
161 kf->buffer.attrs = &iio_kfifo_attribute_group; 170 kf->buffer.attrs = &iio_kfifo_attribute_group;
162 kf->buffer.access = &kfifo_access_funcs; 171 kf->buffer.access = &kfifo_access_funcs;
163 kf->buffer.length = 2; 172 kf->buffer.length = 2;
173 mutex_init(&kf->user_lock);
164 return &kf->buffer; 174 return &kf->buffer;
165} 175}
166EXPORT_SYMBOL(iio_kfifo_allocate); 176EXPORT_SYMBOL(iio_kfifo_allocate);