Skip to content

Commit c81fc59

Browse files
Al Virosashalevin
authored andcommitted
gadgetfs: use-after-free in ->aio_read()
[ Upstream commit f01d35a ] 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> Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
1 parent c7fd186 commit c81fc59

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
@@ -565,7 +565,6 @@ static ssize_t ep_copy_to_user(struct kiocb_priv *priv)
565565
if (total == 0)
566566
break;
567567
}
568-
569568
return len;
570569
}
571570

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

586585
kfree(priv->buf);
586+
kfree(priv->iv);
587587
kfree(priv);
588588
}
589589

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

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

@@ -688,6 +696,7 @@ ep_aio_rwtail(
688696
mutex_unlock(&epdata->lock);
689697

690698
if (unlikely(value)) {
699+
kfree(priv->iv);
691700
kfree(priv);
692701
put_ep(epdata);
693702
} else

0 commit comments

Comments
 (0)