io_uring/rsrc: fix RLIMIT_MEMLOCK bypass via compound page accounting
Subsystems (2)
PATCH 1/1 io_uring/rsrc: fix RLIMIT_MEMLOCK bypass via compound page accounting
When multiple registered buffers share the same compound page, only the first buffer accounts for the memory via io_buffer_account_pin(). The subsequent buffers skip accounting since headpage_already_acct() returns true.
When the first buffer is unregistered, the accounting is decremented, but the compound page remains pinned by the remaining buffers. This creates a state where pinned memory is not properly accounted against RLIMIT_MEMLOCK.
On systems with HugeTLB pages pre-allocated, an unprivileged user can exploit this to pin memory beyond RLIMIT_MEMLOCK by cycling buffer registrations. The bypass amount is proportional to the number of available huge pages, potentially allowing gigabytes of memory to be pinned while the kernel accounting shows near-zero.
Fix this by recalculating the actual pages to unaccount when unmapping a buffer. For regular pages, always unaccount. For compound pages, only unaccount if no other registered buffer references the same compound page. This ensures the accounting persists until the last buffer referencing the compound page is released.
--- io_uring/rsrc.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-)
io_uring/rsrc.c +67 -2
@@ -139,15 +139,80 @@ static void io_free_imu(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu)kvfree (imu); } +/* +* Calculate pages to unaccount when unmapping a buffer. Regular pages are +* always counted. Compound pages are only counted if no other registered +* buffer references them, ensuring accounting persists until the last user. +*/ +static unsigned long io_buffer_calc_unaccount (struct io_ring_ctx *ctx, +struct io_mapped_ubuf *imu) +{ +struct page *last_hpage =NULL ; +unsigned long acct =0 ; +unsigned int i; + +for (i =0 ; i < imu->nr_bvecs; i++) { +struct page *page = imu->bvec[i].bv_page; +struct page *hpage; +unsigned int j; + +if (!PageCompound (page)) { + acct++; +continue ; + } + + hpage =compound_head (page); +if (hpage == last_hpage) +continue ; + last_hpage = hpage; + +/* Check if we already processed this hpage earlier in this buffer */ +for (j =0 ; j < i; j++) { +if (PageCompound (imu->bvec[j].bv_page) && +compound_head (imu->bvec[j].bv_page) == hpage) + goto next_hpage; + } + +/* Only unaccount if no other buffer references this page */ +for (j =0 ; j < ctx->buf_table.nr; j++) { +struct io_rsrc_node *node = ctx->buf_table.nodes[j]; +struct io_mapped_ubuf *other; +unsigned int k; + +if (!node) +continue ; + other = node->buf; +if (other == imu) +continue ; + +for (k =0 ; k < other->nr_bvecs; k++) { +struct page *op = other->bvec[k].bv_page; + +if (!PageCompound (op)) +continue ; +if (compound_head (op) == hpage) + goto next_hpage; + } + } + acct +=page_size (hpage) >>PAGE_SHIFT ; +next_hpage: + ; + } +return acct; +} +static void io_buffer_unmap (struct io_ring_ctx *ctx,struct io_mapped_ubuf *imu) { +unsigned long acct; +if (unlikely (refcount_read (&imu->refs) >1 )) {if (!refcount_dec_and_test (&imu->refs))return ; } -if (imu->acct_pages) -io_unaccount_mem (ctx->user, ctx->mm_account, imu->acct_pages); + acct =io_buffer_calc_unaccount (ctx, imu); +if (acct) +io_unaccount_mem (ctx->user, ctx->mm_account, acct); imu->release (imu->priv);io_free_imu (ctx, imu); }
Signature · 1 line
2.34.1
Hi Jens, Pavel, and all,
Just a gentle follow-up on this patch below.
Please let me know if there are any concerns or if changes are needed.
Thanks for your time.
Best regards,
Yuhao Jiang
Quoted 121 lines · Yuhao Jiang
When multiple registered buffers share the same compound page, only the first buffer accounts for the memory via
io_buffer_account_pin(). The subsequent buffers skip accounting sinceheadpage_already_acct()returns true.When the first buffer is unregistered, the accounting is decremented, but the compound page remains pinned by the remaining buffers. This creates a state where pinned memory is not properly accounted against
RLIMIT_MEMLOCK.On systems with HugeTLB pages pre-allocated, an unprivileged user can exploit this to pin memory beyond
RLIMIT_MEMLOCKby cycling buffer registrations. The bypass amount is proportional to the number of available huge pages, potentially allowing gigabytes of memory to be pinned while the kernel accounting shows near-zero.Fix this by recalculating the actual pages to unaccount when unmapping a buffer. For regular pages, always unaccount. For compound pages, only unaccount if no other registered buffer references the same compound page. This ensures the accounting persists until the last buffer referencing the compound page is released.
Reported-by Yuhao Jiang <[email protected]>Fixes 57bebf807e2a ("io_uring/rsrc: optimise registered huge pages")Signed-off-by Yuhao Jiang <[email protected]>--- io_uring/rsrc.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-)io_uring/rsrc.c +67
-3@@ -139,15 +139,80 @@ static void io_free_imu(struct io_ring_ctx *ctx, struct io_mapped_ubuf *imu)kvfree (imu); } +/* +* Calculate pages to unaccount when unmapping a buffer. Regular pages are +* always counted. Compound pages are only counted if no other registered +* buffer references them, ensuring accounting persists until the last user. +*/ +static unsigned long io_buffer_calc_unaccount (struct io_ring_ctx *ctx, +struct io_mapped_ubuf *imu) +{ +struct page *last_hpage =NULL ; +unsigned long acct =0 ; +unsigned int i; + +for (i =0 ; i < imu->nr_bvecs; i++) { +struct page *page = imu->bvec[i].bv_page; +struct page *hpage; +unsigned int j; + +if (!PageCompound (page)) { + acct++; +continue ; + } + + hpage =compound_head (page); +if (hpage == last_hpage) +continue ; + last_hpage = hpage; + +/* Check if we already processed this hpage earlier in this buffer */ +for (j =0 ; j < i; j++) { +if (PageCompound (imu->bvec[j].bv_page) && +compound_head (imu->bvec[j].bv_page) == hpage) + goto next_hpage; + } + +/* Only unaccount if no other buffer references this page */ +for (j =0 ; j < ctx->buf_table.nr; j++) { +struct io_rsrc_node *node = ctx->buf_table.nodes[j]; +struct io_mapped_ubuf *other; +unsigned int k; + +if (!node) +continue ; + other = node->buf; +if (other == imu) +continue ; + +for (k =0 ; k < other->nr_bvecs; k++) { +struct page *op = other->bvec[k].bv_page; + +if (!PageCompound (op)) +continue ; +if (compound_head (op) == hpage) + goto next_hpage; + } + } + acct +=page_size (hpage) >>PAGE_SHIFT ; +next_hpage: + ; + } +return acct; +} +static void io_buffer_unmap (struct io_ring_ctx *ctx,struct io_mapped_ubuf *imu) { +unsigned long acct; +if (unlikely (refcount_read (&imu->refs) >1 )) {if (!refcount_dec_and_test (&imu->refs))return ; } -if (imu->acct_pages) -io_unaccount_mem (ctx->user, ctx->mm_account, imu->acct_pages); + acct =io_buffer_calc_unaccount (ctx, imu); +if (acct) +io_unaccount_mem (ctx->user, ctx->mm_account, acct); imu->release (imu->priv);io_free_imu (ctx, imu); } --2.34.1
Quoted 4 lines · Yuhao Jiang
Hi Jens, Pavel, and all,
Just a gentle follow-up on this patch below.
Please let me know if there are any concerns or if changes are needed.
I'm pretty this will break with buffer sharing / cloning. I'd be tempted to remove all this cross buffer accounting logic and overestimate it, the current accounting is not sane. Otherwise, it'll likely need some proxy object shared b/w buffers or some other overly overcomplicated solution.
Quoted 31 lines
Thanks for your time.
Best regards,
Yuhao JiangQuoted 25 lines · Yuhao Jiang
When multiple registered buffers share the same compound page, only the first buffer accounts for the memory via
io_buffer_account_pin(). The subsequent buffers skip accounting sinceheadpage_already_acct()returns true.When the first buffer is unregistered, the accounting is decremented, but the compound page remains pinned by the remaining buffers. This creates a state where pinned memory is not properly accounted against
RLIMIT_MEMLOCK.On systems with HugeTLB pages pre-allocated, an unprivileged user can exploit this to pin memory beyond
RLIMIT_MEMLOCKby cycling buffer registrations. The bypass amount is proportional to the number of available huge pages, potentially allowing gigabytes of memory to be pinned while the kernel accounting shows near-zero.Fix this by recalculating the actual pages to unaccount when unmapping a buffer. For regular pages, always unaccount. For compound pages, only unaccount if no other registered buffer references the same compound page. This ensures the accounting persists until the last buffer referencing the compound page is released.
Reported-by Yuhao Jiang <[email protected]>Fixes 57bebf807e2a ("io_uring/rsrc: optimise registered huge pages")
That's not the right commit, the accounting is ancient, should get blamed somewhere around first commits that added registered buffers.
Signature · 1 line
Pavel Begunkov
Quoted 11 lines · Pavel Begunkov
Quoted 4 lines · Yuhao Jiang
Hi Jens, Pavel, and all,
Just a gentle follow-up on this patch below.
Please let me know if there are any concerns or if changes are needed.I'm pretty this will break with buffer sharing / cloning. I'd be tempted to remove all this cross buffer accounting logic and overestimate it, the current accounting is not sane. Otherwise, it'll likely need some proxy object shared b/w buffers or some other overly overcomplicated solution
Another way would be to double account cloned buffers and then have your patch, which combines overaccounting with the ugliness of full buffer table walks.
Quoted 24 lines
Quoted 24 lines · Yuhao Jiang
Quoted 23 lines
When multiple registered buffers share the same compound page, only the first buffer accounts for the memory via
io_buffer_account_pin(). The subsequent buffers skip accounting sinceheadpage_already_acct()returns true.When the first buffer is unregistered, the accounting is decremented, but the compound page remains pinned by the remaining buffers. This creates a state where pinned memory is not properly accounted against
RLIMIT_MEMLOCK.On systems with HugeTLB pages pre-allocated, an unprivileged user can exploit this to pin memory beyond
RLIMIT_MEMLOCKby cycling buffer registrations. The bypass amount is proportional to the number of available huge pages, potentially allowing gigabytes of memory to be pinned while the kernel accounting shows near-zero.Fix this by recalculating the actual pages to unaccount when unmapping a buffer. For regular pages, always unaccount. For compound pages, only unaccount if no other registered buffer references the same compound page. This ensures the accounting persists until the last buffer referencing the compound page is released.
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Reported-by Yuhao Jiang <[email protected]>Fixes 57bebf807e2a ("io_uring/rsrc: optimise registered huge pages")That's not the right commit, the accounting is ancient, should get blamed somewhere around first commits that added registered buffers.
Turns it came just a bit later:
commit de2939388be564836b06f0f06b3787bdedaed822
Author: Jens Axboe <[email protected]> Date: Thu Sep 17 16:19:16 2020 -0600
io_uring: improve registered buffer accounting for huge pages
Signature · 1 line
Pavel Begunkov
Quoted 51 lines · Pavel Begunkov
Quoted 11 lines · Pavel Begunkov
Quoted 4 lines · Yuhao Jiang
Hi Jens, Pavel, and all,
Just a gentle follow-up on this patch below.
Please let me know if there are any concerns or if changes are needed.I'm pretty this will break with buffer sharing / cloning. I'd be tempted to remove all this cross buffer accounting logic and overestimate it, the current accounting is not sane. Otherwise, it'll likely need some proxy object shared b/w buffers or some other overly overcomplicated solution
Another way would be to double account cloned buffers and then have your patch, which combines overaccounting with the ugliness of full buffer table walks.
Quoted 24 lines
Quoted 24 lines · Yuhao Jiang
Quoted 23 lines
When multiple registered buffers share the same compound page, only the first buffer accounts for the memory via
io_buffer_account_pin(). The subsequent buffers skip accounting sinceheadpage_already_acct()returns true.When the first buffer is unregistered, the accounting is decremented, but the compound page remains pinned by the remaining buffers. This creates a state where pinned memory is not properly accounted against
RLIMIT_MEMLOCK.On systems with HugeTLB pages pre-allocated, an unprivileged user can exploit this to pin memory beyond
RLIMIT_MEMLOCKby cycling buffer registrations. The bypass amount is proportional to the number of available huge pages, potentially allowing gigabytes of memory to be pinned while the kernel accounting shows near-zero.Fix this by recalculating the actual pages to unaccount when unmapping a buffer. For regular pages, always unaccount. For compound pages, only unaccount if no other registered buffer references the same compound page. This ensures the accounting persists until the last buffer referencing the compound page is released.
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Reported-by Yuhao Jiang <[email protected]>Fixes 57bebf807e2a ("io_uring/rsrc: optimise registered huge pages")That's not the right commit, the accounting is ancient, should get blamed somewhere around first commits that added registered buffers.
Turns it came just a bit later:
Quoted 9 lines
commit
de2939388be564836b06f0f06b3787bdedaed822Author: Jens Axboe <[email protected]> Date: Thu Sep 17 16:19:16 2020 -0600
io_uring: improve registered buffer accounting for huge pages--
Pavel Begunkov
Thanks for the review. I see the issues with buffer sharing/cloning and the accounting concerns you pointed out. I'll rework this accordingly and send a v2, and also fix the Fixes tag.
Best regards,
Yuhao Jiang