Skip to content

Commit 6a8dadc

Browse files
toddpoynormartinkpetersen
authored andcommitted
scsi: sg: protect against races between mmap() and SG_SET_RESERVED_SIZE
Take f_mutex around mmap() processing to protect against races with the SG_SET_RESERVED_SIZE ioctl. Ensure the reserve buffer length remains consistent during the mapping operation, and set the "mmap called" flag to prevent further changes to the reserved buffer size as an atomic operation with the mapping. [mkp: fixed whitespace] Signed-off-by: Todd Poynor <[email protected]> Acked-by: Douglas Gilbert <[email protected]> Signed-off-by: Martin K. Petersen <[email protected]>
1 parent 8d26f49 commit 6a8dadc

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

drivers/scsi/sg.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,6 +1262,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12621262
unsigned long req_sz, len, sa;
12631263
Sg_scatter_hold *rsv_schp;
12641264
int k, length;
1265+
int ret = 0;
12651266

12661267
if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
12671268
return -ENXIO;
@@ -1272,8 +1273,11 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12721273
if (vma->vm_pgoff)
12731274
return -EINVAL; /* want no offset */
12741275
rsv_schp = &sfp->reserve;
1275-
if (req_sz > rsv_schp->bufflen)
1276-
return -ENOMEM; /* cannot map more than reserved buffer */
1276+
mutex_lock(&sfp->f_mutex);
1277+
if (req_sz > rsv_schp->bufflen) {
1278+
ret = -ENOMEM; /* cannot map more than reserved buffer */
1279+
goto out;
1280+
}
12771281

12781282
sa = vma->vm_start;
12791283
length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
@@ -1287,7 +1291,9 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12871291
vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
12881292
vma->vm_private_data = sfp;
12891293
vma->vm_ops = &sg_mmap_vm_ops;
1290-
return 0;
1294+
out:
1295+
mutex_unlock(&sfp->f_mutex);
1296+
return ret;
12911297
}
12921298

12931299
static void

0 commit comments

Comments
 (0)