Skip to content

Commit 0d7592a

Browse files
toddpoynorgregkh
authored andcommitted
scsi: sg: protect against races between mmap() and SG_SET_RESERVED_SIZE
commit 6a8dadcca81fceff9976e8828cceb072873b7bd5 upstream. 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 <toddpoynor@google.com> Acked-by: Douglas Gilbert <dgilbert@interlog.com> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9a4cabf commit 0d7592a

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

drivers/scsi/sg.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12541254
unsigned long req_sz, len, sa;
12551255
Sg_scatter_hold *rsv_schp;
12561256
int k, length;
1257+
int ret = 0;
12571258

12581259
if ((!filp) || (!vma) || (!(sfp = (Sg_fd *) filp->private_data)))
12591260
return -ENXIO;
@@ -1264,8 +1265,11 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12641265
if (vma->vm_pgoff)
12651266
return -EINVAL; /* want no offset */
12661267
rsv_schp = &sfp->reserve;
1267-
if (req_sz > rsv_schp->bufflen)
1268-
return -ENOMEM; /* cannot map more than reserved buffer */
1268+
mutex_lock(&sfp->f_mutex);
1269+
if (req_sz > rsv_schp->bufflen) {
1270+
ret = -ENOMEM; /* cannot map more than reserved buffer */
1271+
goto out;
1272+
}
12691273

12701274
sa = vma->vm_start;
12711275
length = 1 << (PAGE_SHIFT + rsv_schp->page_order);
@@ -1279,7 +1283,9 @@ sg_mmap(struct file *filp, struct vm_area_struct *vma)
12791283
vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
12801284
vma->vm_private_data = sfp;
12811285
vma->vm_ops = &sg_mmap_vm_ops;
1282-
return 0;
1286+
out:
1287+
mutex_unlock(&sfp->f_mutex);
1288+
return ret;
12831289
}
12841290

12851291
static void

0 commit comments

Comments
 (0)