Skip to contentSkip to conversation

mm: fix mapping_seek_hole_data() overflow on last page

PATCH Superseded Zhen Yan 3 participants 6 messages Jun 30 – Jul 2 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Zhen Yan

author
#1 lore raw

PATCH 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.

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
index 5af62e6abca5..22a1534ec8b3 100644
@@ -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).

maintainer
#2 lore raw
Quoted 35 lines · Zhen Yan

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.

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 +0 -0
index 5af62e6abca5..22a1534ec8b3 100644

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 -1
 		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).

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#3 lore raw

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
index 5af62e6abca5..b78fd88f2638 100644
@@ -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.

maintainer Reviewed-by on v2 1/1
#4 lore raw
Quoted 18 lines · Zhen Yan

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.

Quoted 6 lines

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.

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
index 5af62e6abca5..b78fd88f2638 100644
@@ -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.

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#5 lore raw
Quoted 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_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.

Quoted 9 lines
Quoted 6 lines

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.

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]
maintainer
#6 lore raw
Quoted 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_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.

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