Skip to content

Commit f01d35a

Browse files
author
Al Viro
committed
gadgetfs: use-after-free in ->aio_read()
AIO_PREAD requests call ->aio_read() with iovec on caller's stack, so if we are going to access it asynchronously, we'd better get ourselves a copy - the one on kernel stack of aio_run_iocb() won't be there anymore. function/f_fs.c take care of doing that, legacy/inode.c doesn't... Cc: stable@vger.kernel.org Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
1 parent 70e60d9 commit f01d35a

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

‎drivers/usb/gadget/legacy/inode.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
566566
if (total == 0)
567567
break;
568568
}
569-
570569
return len;
571570
}
572571

@@ -585,6 +584,7 @@ static void ep_user_copy_worker(struct work_struct *work)
585584
aio_complete(iocb, ret, ret);
586585

587586
kfree(priv->buf);
587+
kfree(priv->iv);
588588
kfree(priv);
589589
}
590590

@@ -605,6 +605,7 @@ static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
605605
*/
606606
if (priv->iv == NULL || unlikely(req->actual == 0)) {
607607
kfree(req->buf);
608+
kfree(priv->iv);
608609
kfree(priv);
609610
iocb->private = NULL;
610611
/* aio_complete() reports bytes-transferred _and_ faults */
@@ -640,7 +641,7 @@ ep_aio_rwtail(
640641
struct usb_request *req;
641642
ssize_t value;
642643

643-
priv = kmalloc(sizeof *priv, GFP_KERNEL);
644+
priv = kzalloc(sizeof *priv, GFP_KERNEL);
644645
if (!priv) {
645646
value = -ENOMEM;
646647
fail:
@@ -649,7 +650,14 @@ ep_aio_rwtail(
649650
}
650651
iocb->private = priv;
651652
priv->iocb = iocb;
652-
priv->iv = iv;
653+
if (iv) {
654+
priv->iv = kmemdup(iv, nr_segs * sizeof(struct iovec),
655+
GFP_KERNEL);
656+
if (!priv->iv) {
657+
kfree(priv);
658+
goto fail;
659+
}
660+
}
653661
priv->nr_segs = nr_segs;
654662
INIT_WORK(&priv->work, ep_user_copy_worker);
655663

@@ -689,6 +697,7 @@ ep_aio_rwtail(
689697
mutex_unlock(&epdata->lock);
690698

691699
if (unlikely(value)) {
700+
kfree(priv->iv);
692701
kfree(priv);
693702
put_ep(epdata);
694703
} else

0 commit comments

Comments
 (0)