aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTodd Poynor <toddpoynor@google.com>2014-02-04 19:08:37 -0500
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>2014-02-07 12:03:16 -0500
commit077f6db9731673753ca41a5c3acbb5ead142658a (patch)
tree4846028c95f948bd89be3437e9005af5e2c668e3
parent5cf045f54d31894ec59ee741e01fa258be2ba0fb (diff)
staging: ashmem: Avoid deadlock between read and mmap calls
Avoid holding ashmem_mutex across code that can page fault. Page faults grab the mmap_sem for the process, which are also held by mmap calls prior to calling ashmem_mmap, which locks ashmem_mutex. The reversed order of locking between the two can deadlock. The calls that can page fault are read() and the ASHMEM_SET_NAME and ASHMEM_GET_NAME ioctls. Move the code that accesses userspace pages outside the ashmem_mutex. Cc: Colin Cross <ccross@android.com> Cc: Android Kernel Team <kernel-team@android.com> Signed-off-by: Todd Poynor <toddpoynor@google.com> [jstultz: minor commit message tweaks] Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r--drivers/staging/android/ashmem.c45
1 files changed, 26 insertions, 19 deletions
diff --git a/drivers/staging/android/ashmem.c b/drivers/staging/android/ashmem.c
index 23948f167012..713a97226787 100644
--- a/drivers/staging/android/ashmem.c
+++ b/drivers/staging/android/ashmem.c
@@ -295,21 +295,29 @@ static ssize_t ashmem_read(struct file *file, char __user *buf,
295 295
296 /* If size is not set, or set to 0, always return EOF. */ 296 /* If size is not set, or set to 0, always return EOF. */
297 if (asma->size == 0) 297 if (asma->size == 0)
298 goto out; 298 goto out_unlock;
299 299
300 if (!asma->file) { 300 if (!asma->file) {
301 ret = -EBADF; 301 ret = -EBADF;
302 goto out; 302 goto out_unlock;
303 } 303 }
304 304
305 ret = asma->file->f_op->read(asma->file, buf, len, pos); 305 mutex_unlock(&ashmem_mutex);
306 if (ret < 0)
307 goto out;
308 306
309 /** Update backing file pos, since f_ops->read() doesn't */ 307 /*
310 asma->file->f_pos = *pos; 308 * asma and asma->file are used outside the lock here. We assume
309 * once asma->file is set it will never be changed, and will not
310 * be destroyed until all references to the file are dropped and
311 * ashmem_release is called.
312 */
313 ret = asma->file->f_op->read(asma->file, buf, len, pos);
314 if (ret >= 0) {
315 /** Update backing file pos, since f_ops->read() doesn't */
316 asma->file->f_pos = *pos;
317 }
318 return ret;
311 319
312out: 320out_unlock:
313 mutex_unlock(&ashmem_mutex); 321 mutex_unlock(&ashmem_mutex);
314 return ret; 322 return ret;
315} 323}
@@ -498,6 +506,7 @@ out:
498 506
499static int set_name(struct ashmem_area *asma, void __user *name) 507static int set_name(struct ashmem_area *asma, void __user *name)
500{ 508{
509 int len;
501 int ret = 0; 510 int ret = 0;
502 char local_name[ASHMEM_NAME_LEN]; 511 char local_name[ASHMEM_NAME_LEN];
503 512
@@ -510,21 +519,19 @@ static int set_name(struct ashmem_area *asma, void __user *name)
510 * variable that does not need protection and later copy the local 519 * variable that does not need protection and later copy the local
511 * variable to the structure member with lock held. 520 * variable to the structure member with lock held.
512 */ 521 */
513 if (copy_from_user(local_name, name, ASHMEM_NAME_LEN)) 522 len = strncpy_from_user(local_name, name, ASHMEM_NAME_LEN);
514 return -EFAULT; 523 if (len < 0)
515 524 return len;
525 if (len == ASHMEM_NAME_LEN)
526 local_name[ASHMEM_NAME_LEN - 1] = '\0';
516 mutex_lock(&ashmem_mutex); 527 mutex_lock(&ashmem_mutex);
517 /* cannot change an existing mapping's name */ 528 /* cannot change an existing mapping's name */
518 if (unlikely(asma->file)) { 529 if (unlikely(asma->file))
519 ret = -EINVAL; 530 ret = -EINVAL;
520 goto out; 531 else
521 } 532 strcpy(asma->name + ASHMEM_NAME_PREFIX_LEN, local_name);
522 memcpy(asma->name + ASHMEM_NAME_PREFIX_LEN,
523 local_name, ASHMEM_NAME_LEN);
524 asma->name[ASHMEM_FULL_NAME_LEN-1] = '\0';
525out:
526 mutex_unlock(&ashmem_mutex);
527 533
534 mutex_unlock(&ashmem_mutex);
528 return ret; 535 return ret;
529} 536}
530 537