aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDave Hansen <dave.hansen@linux.intel.com>2014-12-12 19:58:19 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2014-12-13 15:42:52 -0500
commitd3c97900b427b8d5a476fdfe484267f09df418d6 (patch)
tree0169510392ae190dcec4376a7341efe955fa27fd
parent0050ee059f7fc86b1df2527aaa14ed5dc72f9973 (diff)
ipc/shm.c: fix overly aggressive shmdt() when calls span multiple segments
This is a highly-contrived scenario. But, a single shmdt() call can be induced in to unmapping memory from mulitple shm segments. Example code is here: http://www.sr71.net/~dave/intel/shmfun.c The fix is pretty simple: Record the 'struct file' for the first VMA we encounter and then stick to it. Decline to unmap anything not from the same file and thus the same segment. I found this by inspection and the odds of anyone hitting this in practice are pretty darn small. Lightly tested, but it's a pretty small patch. Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Cc: Manfred Spraul <manfred@colorfullife.com> Reviewed-by: Davidlohr Bueso <dave@stgolabs.net> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r--ipc/shm.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/ipc/shm.c b/ipc/shm.c
index 01454796ba3c..e5dc7d246f05 100644
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -1229,6 +1229,7 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1229 int retval = -EINVAL; 1229 int retval = -EINVAL;
1230#ifdef CONFIG_MMU 1230#ifdef CONFIG_MMU
1231 loff_t size = 0; 1231 loff_t size = 0;
1232 struct file *file;
1232 struct vm_area_struct *next; 1233 struct vm_area_struct *next;
1233#endif 1234#endif
1234 1235
@@ -1245,7 +1246,8 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1245 * started at address shmaddr. It records it's size and then unmaps 1246 * started at address shmaddr. It records it's size and then unmaps
1246 * it. 1247 * it.
1247 * - Then it unmaps all shm vmas that started at shmaddr and that 1248 * - Then it unmaps all shm vmas that started at shmaddr and that
1248 * are within the initially determined size. 1249 * are within the initially determined size and that are from the
1250 * same shm segment from which we determined the size.
1249 * Errors from do_munmap are ignored: the function only fails if 1251 * Errors from do_munmap are ignored: the function only fails if
1250 * it's called with invalid parameters or if it's called to unmap 1252 * it's called with invalid parameters or if it's called to unmap
1251 * a part of a vma. Both calls in this function are for full vmas, 1253 * a part of a vma. Both calls in this function are for full vmas,
@@ -1271,8 +1273,14 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1271 if ((vma->vm_ops == &shm_vm_ops) && 1273 if ((vma->vm_ops == &shm_vm_ops) &&
1272 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) { 1274 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) {
1273 1275
1274 1276 /*
1275 size = file_inode(vma->vm_file)->i_size; 1277 * Record the file of the shm segment being
1278 * unmapped. With mremap(), someone could place
1279 * page from another segment but with equal offsets
1280 * in the range we are unmapping.
1281 */
1282 file = vma->vm_file;
1283 size = file_inode(file)->i_size;
1276 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); 1284 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1277 /* 1285 /*
1278 * We discovered the size of the shm segment, so 1286 * We discovered the size of the shm segment, so
@@ -1298,8 +1306,8 @@ SYSCALL_DEFINE1(shmdt, char __user *, shmaddr)
1298 1306
1299 /* finding a matching vma now does not alter retval */ 1307 /* finding a matching vma now does not alter retval */
1300 if ((vma->vm_ops == &shm_vm_ops) && 1308 if ((vma->vm_ops == &shm_vm_ops) &&
1301 (vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) 1309 ((vma->vm_start - addr)/PAGE_SIZE == vma->vm_pgoff) &&
1302 1310 (vma->vm_file == file))
1303 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start); 1311 do_munmap(mm, vma->vm_start, vma->vm_end - vma->vm_start);
1304 vma = next; 1312 vma = next;
1305 } 1313 }