CVE-2017-15126: Use After Free
A flaw was found in the Linux kernels handling of fork failure when dealing with event messages in the userfaultfd code. Failure to fork correctly can lead to a situation where a fork event will be removed from an already freed list of events with userfaultfdctxput(). The userfault events are part of the slab cache, and the secondary free could only free other event list objects as the free mechanism uses the underlying kmemcache implementation making generic use-after-free style attacks less effective.
While technically a use after free style flaw, at this time Red Hat Product security believes this flaw to be difficult to exploit but this may change in the future.
From the commit:
When reading the event from the uffd, we put it on a temporary forkevent list to detect if we can still access it after releasing and retaking the eventwqh.lock.
If fork aborts and removes the event from the forkevent all is fine as long as we're still in the userfault read context and forkevent head is still alive.
We've [forgotten] to put the event allocated in the fork kernel stack, back from forkevent list-head to the eventwqh head, before returning from userfaultfdctxread, because the forkevent head lifetime is limited to the userfaultfdctxread stack lifetime.
Forgetting to move the event back to its eventwqh place then results in removewaitqueue(&ctx->eventwqh, &ewq->wq); in userfaultfdeventwaitcompletion to remove it from a head that has been already freed from the reader stack.
This could only happen if resolveuserfaultfork failed (for example if there are no file descriptors available to allocate the fork uffd). If it succeeded it was put back correctly.
Furthermore, after finduserfaultevt receives a fork event, the forked userfault context in forknctx and uwq->msg.arg.reserved.reserved1 can be released by the fork thread as soon as the eventwqh.lock is released. Taking a reference on the forknctx before dropping the lock prevents an use after free in resolveuserfaultfork().
If the fork side aborted and it already released everything, we still try to succeed resolveuserfaultfork(), if possible. -- end ---
References:
https://marc.info/?t=150351212000001&r=1&w=2
An upstream patch:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=384632e67e0829deb8015ee6ad916b180049d252
Other sources
A use-after-free flaw was found in fs/userfaultfd.c in the Linux kernel before 4.13.6. The issue is related to the handling of fork failure when dealing with event messages. Failure to fork correctly can lead to a situation where a fork event will be removed from an already freed list of events with userfaultfdctxput().
— MITRE
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
Linux kernelto a version that resolves this vulnerability.Fixed in 4.13.6
Event History
Frequently Asked Questions
What conditions are needed to trigger this flaw?
The issue involves a fork failure while processing userfaultfd event messages. Exploitation requires reaching the affected userfaultfd fork-event handling path and causing the fork to abort or fail.
How practical is exploitation?
Red Hat Product Security assessed the flaw as difficult to exploit at the time of disclosure. The secondary free is constrained to other event-list objects from the relevant slab cache, which makes generic use-after-free attack techniques less effective; this assessment could change with new research.
What happens when the vulnerable path is triggered?
A fork event can be removed from an event list after it has already been freed through userfaultfd_ctx_put(). This creates a use-after-free-style condition involving userfault event objects managed through the slab cache.