Skip to content

Commit 47ffefd

Browse files
ebiggersgregkh
authored andcommitted
aio: fix use-after-free due to missing POLLFREE handling
commit 50252e4 upstream. signalfd_poll() and binder_poll() are special in that they use a waitqueue whose lifetime is the current task, rather than the struct file as is normally the case. This is okay for blocking polls, since a blocking poll occurs within one task; however, non-blocking polls require another solution. This solution is for the queue to be cleared before it is freed, by sending a POLLFREE notification to all waiters. Unfortunately, only eventpoll handles POLLFREE. A second type of non-blocking poll, aio poll, was added in kernel v4.18, and it doesn't handle POLLFREE. This allows a use-after-free to occur if a signalfd or binder fd is polled with aio poll, and the waitqueue gets freed. Fix this by making aio poll handle POLLFREE. A patch by Ramji Jiyani <ramjiyani@google.com> (https://lore.kernel.org/r/20211027011834.2497484-1-ramjiyani@google.com) tried to do this by making aio_poll_wake() always complete the request inline if POLLFREE is seen. However, that solution had two bugs. First, it introduced a deadlock, as it unconditionally locked the aio context while holding the waitqueue lock, which inverts the normal locking order. Second, it didn't consider that POLLFREE notifications are missed while the request has been temporarily de-queued. The second problem was solved by my previous patch. This patch then properly fixes the use-after-free by handling POLLFREE in a deadlock-free way. It does this by taking advantage of the fact that freeing of the waitqueue is RCU-delayed, similar to what eventpoll does. Fixes: 2c14fa8 ("aio: implement IOCB_CMD_POLL") Cc: <stable@vger.kernel.org> # v4.18+ Link: https://lore.kernel.org/r/20211209010455.42744-6-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent e4d1974 commit 47ffefd

2 files changed

Lines changed: 107 additions & 32 deletions

File tree

‎fs/aio.c‎

Lines changed: 106 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,6 +1622,51 @@ static void aio_poll_put_work(struct work_struct *work)
16221622
iocb_put(iocb);
16231623
}
16241624

1625+
/*
1626+
* Safely lock the waitqueue which the request is on, synchronizing with the
1627+
* case where the ->poll() provider decides to free its waitqueue early.
1628+
*
1629+
* Returns true on success, meaning that req->head->lock was locked, req->wait
1630+
* is on req->head, and an RCU read lock was taken. Returns false if the
1631+
* request was already removed from its waitqueue (which might no longer exist).
1632+
*/
1633+
static bool poll_iocb_lock_wq(struct poll_iocb *req)
1634+
{
1635+
wait_queue_head_t *head;
1636+
1637+
/*
1638+
* While we hold the waitqueue lock and the waitqueue is nonempty,
1639+
* wake_up_pollfree() will wait for us. However, taking the waitqueue
1640+
* lock in the first place can race with the waitqueue being freed.
1641+
*
1642+
* We solve this as eventpoll does: by taking advantage of the fact that
1643+
* all users of wake_up_pollfree() will RCU-delay the actual free. If
1644+
* we enter rcu_read_lock() and see that the pointer to the queue is
1645+
* non-NULL, we can then lock it without the memory being freed out from
1646+
* under us, then check whether the request is still on the queue.
1647+
*
1648+
* Keep holding rcu_read_lock() as long as we hold the queue lock, in
1649+
* case the caller deletes the entry from the queue, leaving it empty.
1650+
* In that case, only RCU prevents the queue memory from being freed.
1651+
*/
1652+
rcu_read_lock();
1653+
head = smp_load_acquire(&req->head);
1654+
if (head) {
1655+
spin_lock(&head->lock);
1656+
if (!list_empty(&req->wait.entry))
1657+
return true;
1658+
spin_unlock(&head->lock);
1659+
}
1660+
rcu_read_unlock();
1661+
return false;
1662+
}
1663+
1664+
static void poll_iocb_unlock_wq(struct poll_iocb *req)
1665+
{
1666+
spin_unlock(&req->head->lock);
1667+
rcu_read_unlock();
1668+
}
1669+
16251670
static void aio_poll_complete_work(struct work_struct *work)
16261671
{
16271672
struct poll_iocb *req = container_of(work, struct poll_iocb, work);
@@ -1641,24 +1686,25 @@ static void aio_poll_complete_work(struct work_struct *work)
16411686
* avoid further branches in the fast path.
16421687
*/
16431688
spin_lock_irq(&ctx->ctx_lock);
1644-
spin_lock(&req->head->lock);
1645-
if (!mask && !READ_ONCE(req->cancelled)) {
1646-
/*
1647-
* The request isn't actually ready to be completed yet.
1648-
* Reschedule completion if another wakeup came in.
1649-
*/
1650-
if (req->work_need_resched) {
1651-
schedule_work(&req->work);
1652-
req->work_need_resched = false;
1653-
} else {
1654-
req->work_scheduled = false;
1689+
if (poll_iocb_lock_wq(req)) {
1690+
if (!mask && !READ_ONCE(req->cancelled)) {
1691+
/*
1692+
* The request isn't actually ready to be completed yet.
1693+
* Reschedule completion if another wakeup came in.
1694+
*/
1695+
if (req->work_need_resched) {
1696+
schedule_work(&req->work);
1697+
req->work_need_resched = false;
1698+
} else {
1699+
req->work_scheduled = false;
1700+
}
1701+
poll_iocb_unlock_wq(req);
1702+
spin_unlock_irq(&ctx->ctx_lock);
1703+
return;
16551704
}
1656-
spin_unlock(&req->head->lock);
1657-
spin_unlock_irq(&ctx->ctx_lock);
1658-
return;
1659-
}
1660-
list_del_init(&req->wait.entry);
1661-
spin_unlock(&req->head->lock);
1705+
list_del_init(&req->wait.entry);
1706+
poll_iocb_unlock_wq(req);
1707+
} /* else, POLLFREE has freed the waitqueue, so we must complete */
16621708
list_del_init(&iocb->ki_list);
16631709
iocb->ki_res.res = mangle_poll(mask);
16641710
spin_unlock_irq(&ctx->ctx_lock);
@@ -1672,13 +1718,14 @@ static int aio_poll_cancel(struct kiocb *iocb)
16721718
struct aio_kiocb *aiocb = container_of(iocb, struct aio_kiocb, rw);
16731719
struct poll_iocb *req = &aiocb->poll;
16741720

1675-
spin_lock(&req->head->lock);
1676-
WRITE_ONCE(req->cancelled, true);
1677-
if (!req->work_scheduled) {
1678-
schedule_work(&aiocb->poll.work);
1679-
req->work_scheduled = true;
1680-
}
1681-
spin_unlock(&req->head->lock);
1721+
if (poll_iocb_lock_wq(req)) {
1722+
WRITE_ONCE(req->cancelled, true);
1723+
if (!req->work_scheduled) {
1724+
schedule_work(&aiocb->poll.work);
1725+
req->work_scheduled = true;
1726+
}
1727+
poll_iocb_unlock_wq(req);
1728+
} /* else, the request was force-cancelled by POLLFREE already */
16821729

16831730
return 0;
16841731
}
@@ -1730,21 +1777,45 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync,
17301777
*
17311778
* Don't remove the request from the waitqueue here, as it might
17321779
* not actually be complete yet (we won't know until vfs_poll()
1733-
* is called), and we must not miss any wakeups.
1780+
* is called), and we must not miss any wakeups. POLLFREE is an
1781+
* exception to this; see below.
17341782
*/
17351783
if (req->work_scheduled) {
17361784
req->work_need_resched = true;
17371785
} else {
17381786
schedule_work(&req->work);
17391787
req->work_scheduled = true;
17401788
}
1789+
1790+
/*
1791+
* If the waitqueue is being freed early but we can't complete
1792+
* the request inline, we have to tear down the request as best
1793+
* we can. That means immediately removing the request from its
1794+
* waitqueue and preventing all further accesses to the
1795+
* waitqueue via the request. We also need to schedule the
1796+
* completion work (done above). Also mark the request as
1797+
* cancelled, to potentially skip an unneeded call to ->poll().
1798+
*/
1799+
if (mask & POLLFREE) {
1800+
WRITE_ONCE(req->cancelled, true);
1801+
list_del_init(&req->wait.entry);
1802+
1803+
/*
1804+
* Careful: this *must* be the last step, since as soon
1805+
* as req->head is NULL'ed out, the request can be
1806+
* completed and freed, since aio_poll_complete_work()
1807+
* will no longer need to take the waitqueue lock.
1808+
*/
1809+
smp_store_release(&req->head, NULL);
1810+
}
17411811
}
17421812
return 1;
17431813
}
17441814

17451815
struct aio_poll_table {
17461816
struct poll_table_struct pt;
17471817
struct aio_kiocb *iocb;
1818+
bool queued;
17481819
int error;
17491820
};
17501821

@@ -1755,11 +1826,12 @@ aio_poll_queue_proc(struct file *file, struct wait_queue_head *head,
17551826
struct aio_poll_table *pt = container_of(p, struct aio_poll_table, pt);
17561827

17571828
/* multiple wait queues per file are not supported */
1758-
if (unlikely(pt->iocb->poll.head)) {
1829+
if (unlikely(pt->queued)) {
17591830
pt->error = -EINVAL;
17601831
return;
17611832
}
17621833

1834+
pt->queued = true;
17631835
pt->error = 0;
17641836
pt->iocb->poll.head = head;
17651837
add_wait_queue(head, &pt->iocb->poll.wait);
@@ -1791,6 +1863,7 @@ static int aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
17911863
apt.pt._qproc = aio_poll_queue_proc;
17921864
apt.pt._key = req->events;
17931865
apt.iocb = aiocb;
1866+
apt.queued = false;
17941867
apt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */
17951868

17961869
/* initialized the list so that we can do list_empty checks */
@@ -1799,9 +1872,10 @@ static int aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
17991872

18001873
mask = vfs_poll(req->file, &apt.pt) & req->events;
18011874
spin_lock_irq(&ctx->ctx_lock);
1802-
if (likely(req->head)) {
1803-
spin_lock(&req->head->lock);
1804-
if (list_empty(&req->wait.entry) || req->work_scheduled) {
1875+
if (likely(apt.queued)) {
1876+
bool on_queue = poll_iocb_lock_wq(req);
1877+
1878+
if (!on_queue || req->work_scheduled) {
18051879
/*
18061880
* aio_poll_wake() already either scheduled the async
18071881
* completion work, or completed the request inline.
@@ -1817,15 +1891,16 @@ static int aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb)
18171891
} else if (cancel) {
18181892
/* Cancel if possible (may be too late though). */
18191893
WRITE_ONCE(req->cancelled, true);
1820-
} else if (!list_empty(&req->wait.entry)) {
1894+
} else if (on_queue) {
18211895
/*
18221896
* Actually waiting for an event, so add the request to
18231897
* active_reqs so that it can be cancelled if needed.
18241898
*/
18251899
list_add_tail(&aiocb->ki_list, &ctx->active_reqs);
18261900
aiocb->ki_cancel = aio_poll_cancel;
18271901
}
1828-
spin_unlock(&req->head->lock);
1902+
if (on_queue)
1903+
poll_iocb_unlock_wq(req);
18291904
}
18301905
if (mask) { /* no async, we'd stolen it */
18311906
aiocb->ki_res.res = mangle_poll(mask);

‎include/uapi/asm-generic/poll.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#define POLLRDHUP 0x2000
3030
#endif
3131

32-
#define POLLFREE (__force __poll_t)0x4000 /* currently only for epoll */
32+
#define POLLFREE (__force __poll_t)0x4000
3333

3434
#define POLL_BUSY_LOOP (__force __poll_t)0x8000
3535

0 commit comments

Comments
 (0)