mm: fix mapping_seek_hole_data() overflow on last page
Subsystems (3)
PATCH 1/1 mm: fix mapping_seek_hole_data() overflow on last page
In reply to a message not in the archiveA local unprivileged process can create a shmem/tmpfs file with i_size == LLONG_MAX using memfd_create() and fallocate(). If the last page is present in the page cache, lseek(SEEK_HOLE) on that page returns 0x8000000000000000 as a successful offset, which is LLONG_MIN when stored in loff_t.
The same file has readable data at the last byte, but SEEK_DATA from that offset returns ENXIO.
The overflow is in mapping_seek_hole_data():
pos = round_up((u64)pos + 1, seek_size);
For the final page below LLONG_MAX, the next page boundary is 0x8000000000000000, which is then used as a signed file offset.
This violates the lseek() ABI for shmem/tmpfs: a successful seek returns a negative offset, and SEEK_DATA can miss readable data at EOF. Hole-aware copy, backup, and sparse-file scanners may skip the last cached byte range.
Keep mapping_seek_hole_data() inside its documented [start, end) search range by clamping the rounded-up position to end before passing it to folio_seek_hole_data().
--- mm/filemap.c | 2 ++ 1 file changed, 2 insertions(+)
mm/filemap.c +2 -0
@@ -3234,6 +3234,8 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, seek_size =seek_folio_size (&xas, folio); pos =round_up ((u64 )pos +1 , seek_size); +if (pos > end) + pos = end; start =folio_seek_hole_data (&xas, mapping, folio, start, pos, seek_data);if (start < pos)
--
2.25.1
Sorry, my previous email may have been lost due to network filtering issues. Resending the revised patch with the requested changes (tabs preserved, CCs added, signoff added).
Jan Kara
maintainerQuoted 35 lines · Zhen Yan
A local unprivileged process can create a shmem/tmpfs file with
i_size==LLONG_MAXusingmemfd_create()andfallocate(). If the last page is present in the page cache,lseek(SEEK_HOLE)on that page returns0x8000000000000000as a successful offset, which isLLONG_MINwhen stored inloff_t.The same file has readable data at the last byte, but
SEEK_DATAfrom that offset returns ENXIO.The overflow is in
mapping_seek_hole_data():pos = round_up((u64)pos + 1, seek_size);For the final page below
LLONG_MAX, the next page boundary is0x8000000000000000, which is then used as a signed file offset.This violates the
lseek()ABI for shmem/tmpfs: a successful seek returns a negative offset, andSEEK_DATAcan miss readable data at EOF. Hole-aware copy, backup, and sparse-file scanners may skip the last cached byte range.Keep
mapping_seek_hole_data()inside its documented [start, end) search range by clamping the rounded-up position to end before passing it tofolio_seek_hole_data().Signed-off-by Zhen Yan <[email protected]>--- mm/filemap.c | 2 ++ 1 file changed, 2 insertions(+)mm/filemap.c +0
-0
mm/filemap.c loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, · line 3238
Quoted 5 lines
mm/filemap.c +2
-0@@ -3234,6 +3234,8 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, seek_size =seek_folio_size (&xas, folio); pos =round_up ((u64 )pos +1 , seek_size); +if (pos > end) + pos = end;
So I agree with your analysis of the problem but I'm not sure how this is supposed to fix it. If pos was offset of the last folio - that is LLONG_MAX - PAGE_SIZE + 1, then round_up() ends at LLONG_MAX + 1, which readily overflows to LLONG_MIN (pos is loff_t). So pos > end will not be true and nothing changes. Am I missing something? So I think you rather need the condition to check for something like pos < 0. Or maybe use check_add_overflow() to make the overflow checking more explicit.
Honza
Quoted 10 lines
mm/filemap.c +0
-1start =folio_seek_hole_data (&xas, mapping, folio, start, pos, seek_data);if (start < pos) --2.25.1
Sorry, my previous email may have been lost due to network filtering issues. Resending the revised patch with the requested changes (tabs preserved, CCs added, signoff added).
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH v2 1/1 mm: fix mapping_seek_hole_data() overflow on last page
A local unprivileged process can create a shmem/tmpfs file with i_size == LLONG_MAX using memfd_create() and fallocate(). If the last page is present in the page cache, lseek(SEEK_HOLE) on that page returns 0x8000000000000000 as a successful offset, which is LLONG_MIN when stored in loff_t.
The same file has readable data at the last byte, but SEEK_DATA from that offset returns ENXIO.
The overflow is in mapping_seek_hole_data():
pos = round_up((u64)pos + 1, seek_size);
For the final page below LLONG_MAX, the next page boundary is 0x8000000000000000, which is then used as a signed file offset. When assigned to the loff_t pos, this overflows to LLONG_MIN, so a subsequent "pos > end" comparison does not catch it.
Keep mapping_seek_hole_data() inside its documented [start, end) search range: compute round_up() into a u64 variable and compare against (u64)end so the overflow is detected, then clamp pos to end when the rounded-up value goes past the search limit.
--- mm/filemap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
mm/filemap.c +6 -1
@@ -3225,6 +3225,7 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,while ((folio =find_get_entry (&xas, max,XA_PRESENT ))) {loff_t pos = (u64 )xas.xa_index <<PAGE_SHIFT ;size_t seek_size; +u64 next;if (start < pos) {if (!seek_data)
@@ -3233,7 +3234,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, } seek_size =seek_folio_size (&xas, folio); - pos =round_up ((u64 )pos +1 , seek_size); + next =round_up ((u64 )pos +1 , seek_size); +if (next > (u64 )end) + pos = end; +else + pos = next; start =folio_seek_hole_data (&xas, mapping, folio, start, pos, seek_data);if (start < pos)
--
2.25.1
Sorry about the previous patch -- the pos > end check was wrong because round_up() overflows loff_t to LLONG_MIN, making the signed comparison ineffective. Thanks to Jan Kara for pointing this out. This v2 uses a u64 intermediate variable so the overflow is correctly detected.
Quoted 18 lines · Zhen Yan
A local unprivileged process can create a shmem/tmpfs file with
i_size==LLONG_MAXusingmemfd_create()andfallocate(). If the last page is present in the page cache,lseek(SEEK_HOLE)on that page returns0x8000000000000000as a successful offset, which isLLONG_MINwhen stored inloff_t.The same file has readable data at the last byte, but
SEEK_DATAfrom that offset returns ENXIO.The overflow is in
mapping_seek_hole_data():pos = round_up((u64)pos + 1, seek_size);For the final page below
LLONG_MAX, the next page boundary is0x8000000000000000, which is then used as a signed file offset. When assigned to theloff_tpos, this overflows toLLONG_MIN, so a subsequent "pos > end" comparison does not catch it.
Quoted 6 lines
Keep
mapping_seek_hole_data()inside its documented [start, end) search range: computeround_up()into a u64 variable and compare against (u64)end so the overflow is detected, then clamp pos to end when the rounded-up value goes past the search limit.Signed-off-by Zhen Yan <[email protected]>
Thanks for the patch! It looks good. Feel free to add:
One small comment below:
Quoted 3 lines
--- mm/filemap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
The explanation you've added to the bottom of the email really belongs <here>.
Honza
Quoted 34 lines
mm/filemap.c +6
-2@@ -3225,6 +3225,7 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start,while ((folio =find_get_entry (&xas, max,XA_PRESENT ))) {loff_t pos = (u64 )xas.xa_index <<PAGE_SHIFT ;size_t seek_size; +u64 next;if (start < pos) {if (!seek_data)@@ -3233,7 +3234,11 @@ loff_t mapping_seek_hole_data(struct address_space *mapping, loff_t start, } seek_size =seek_folio_size (&xas, folio); - pos =round_up ((u64 )pos +1 , seek_size); + next =round_up ((u64 )pos +1 , seek_size); +if (next > (u64 )end) + pos = end; +else + pos = next; start =folio_seek_hole_data (&xas, mapping, folio, start, pos, seek_data);if (start < pos) --2.25.1
Sorry about the previous patch -- the
pos > endcheck was wrong becauseround_up()overflowsloff_ttoLLONG_MIN, making the signed comparison ineffective. Thanks to Jan Kara for pointing this out. This v2 uses a u64 intermediate variable so the overflow is correctly detected.
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Andrew Morton
maintainerQuoted 19 lines · On Tue, 30 Jun 2026 15:27:25 +0200 Jan Kara <[email protected]> wrote:
Quoted 18 lines · Zhen Yan
A local unprivileged process can create a shmem/tmpfs file with
i_size==LLONG_MAXusingmemfd_create()andfallocate(). If the last page is present in the page cache,lseek(SEEK_HOLE)on that page returns0x8000000000000000as a successful offset, which isLLONG_MINwhen stored inloff_t.The same file has readable data at the last byte, but
SEEK_DATAfrom that offset returns ENXIO.The overflow is in
mapping_seek_hole_data():pos = round_up((u64)pos + 1, seek_size);For the final page below
LLONG_MAX, the next page boundary is0x8000000000000000, which is then used as a signed file offset. When assigned to theloff_tpos, this overflows toLLONG_MIN, so a subsequent "pos > end" comparison does not catch it.
Quoted 9 lines
Quoted 6 lines
Keep
mapping_seek_hole_data()inside its documented [start, end) search range: computeround_up()into a u64 variable and compare against (u64)end so the overflow is detected, then clamp pos to end when the rounded-up value goes past the search limit.Signed-off-by Zhen Yan <[email protected]>Thanks for the patch! It looks good. Feel free to add:
Thanks. I'll queue this for 7.3-rc1 with a cc:stable - no rush, as the issue is presumably very old.
(Do we think the cc:stable is even needed? It's very obscure and the effects aren't very harmful).
AI review might have found a couple of unrelated pre-existing issues in the surrounding code:
https://sashiko.dev/#/patchset/[email protected]
Jan Kara
maintainerQuoted 28 lines · Andrew Morton
Quoted 26 lines · On Tue, 30 Jun 2026 15:27:25 +0200 Jan Kara <[email protected]> wrote:
Quoted 24 lines · Zhen Yan
A local unprivileged process can create a shmem/tmpfs file with
i_size==LLONG_MAXusingmemfd_create()andfallocate(). If the last page is present in the page cache,lseek(SEEK_HOLE)on that page returns0x8000000000000000as a successful offset, which isLLONG_MINwhen stored inloff_t.The same file has readable data at the last byte, but
SEEK_DATAfrom that offset returns ENXIO.The overflow is in
mapping_seek_hole_data():pos = round_up((u64)pos + 1, seek_size);For the final page below
LLONG_MAX, the next page boundary is0x8000000000000000, which is then used as a signed file offset. When assigned to theloff_tpos, this overflows toLLONG_MIN, so a subsequent "pos > end" comparison does not catch it.Keep
mapping_seek_hole_data()inside its documented [start, end) search range: computeround_up()into a u64 variable and compare against (u64)end so the overflow is detected, then clamp pos to end when the rounded-up value goes past the search limit.Signed-off-by Zhen Yan <[email protected]>
Quoted 8 lines
Quoted 2 lines
Thanks for the patch! It looks good. Feel free to add:
Thanks. I'll queue this for 7.3-rc1 with a cc:stable - no rush, as the issue is presumably very old.
(Do we think the cc:stable is even needed? It's very obscure and the effects aren't very harmful).
Frankly, I don't think it's needed. It isn't like people are using LLONG_MIN sized files unless they are trying to exploit some bug :)
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR