Skip to contentSkip to conversation

fsnotify: Pass correct offset to fsnotify_mmap_perm()

PATCH Applied Ryan Roberts 6 participants 13 messages Oct 3, 2025 – Oct 14, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fsnotify: Pass correct offset to fsnotify_mmap_perm()

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Thanks,
Ryan

 mm/util.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/util.c +2 -1
index 6c1d64ed0221..8989d5767528 100644
@@ -566,6 +566,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot,
 	unsigned long flag, unsigned long pgoff)
 {
+	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 	unsigned long ret;
 	struct mm_struct *mm = current->mm;
 	unsigned long populate;
@@ -573,7 +574,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret)
-		ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
+		ret = fsnotify_mmap_perm(file, prot, off, len);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;

--
2.43.0

#1 lore raw
Quoted 6 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Heh. Just submitted fix for the same issue:

https://lore.kernel.org/all/[email protected]/T/#u

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
#2 lore raw
Quoted 6 lines · Kiryl Shutsemau
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 6 lines
Quoted 1 line

Discovered during code review.

Heh. Just submitted fix for the same issue:

https://lore.kernel.org/all/[email protected]/T/#u

Ha... great minds...

I notice that for your version you're just doing "pgoff << PAGE_SHIFT" without casting pgoff.

I'm not sure if that is safe?

pgoff is unsigned long (so 32 bits on 32 bit systems). loff_t is unsigned long long (so always 64 bits). So is it possible that you shift off the end of 32 bits and lose those bits without a cast to loff_t first?

TBH my knowledge of the exact rules is shaky...

Reviewed-by
#3 lore raw
Quoted 19 lines · Ryan Roberts
Quoted 6 lines · Kiryl Shutsemau
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 6 lines
Quoted 1 line

Discovered during code review.

Heh. Just submitted fix for the same issue:

https://lore.kernel.org/all/[email protected]/T/#u

Ha... great minds...

I notice that for your version you're just doing "pgoff << PAGE_SHIFT" without casting pgoff.

Quoted 7 lines

I'm not sure if that is safe?

pgoff is unsigned long (so 32 bits on 32 bit systems). loff_t is unsigned long long (so always 64 bits). So is it possible that you shift off the end of 32 bits and lose those bits without a cast to loff_t first?

TBH my knowledge of the exact rules is shaky...

I think you are right. Missing cast in my patch might be problematic on 32-bit machines.

Signature · 1 line
  Kiryl Shutsemau / Kirill A. Shutemov
Acked-by on v1 1/1
#4 lore raw
In reply to Ryan Roberts
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 8 lines

Discovered during code review.

Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Curious: is there some easy way to write a reproducer? Did you look into that?

LGTM, thanks

Signature · 3 lines
Cheers

David / dhildenb
#5 lore raw
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Quoted 8 lines
Quoted 6 lines
Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Curious: is there some easy way to write a reproducer? Did you look into that?

I didn't; this was just a drive-by discovery.

It looks like there are some fanotify tests in the filesystems selftests; I guess they could be extended to add a regression test?

But FWIW, I think the kernel is just passing the ofset/length info off to user space and isn't acting on it itself. So there is no kernel vulnerability here.

Quoted 5 lines

LGTM, thanks

#6 lore raw
Quoted 18 lines · Ryan Roberts
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Quoted 8 lines
Quoted 6 lines
Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Curious: is there some easy way to write a reproducer? Did you look into that?

Quoted 7 lines

I didn't; this was just a drive-by discovery.

It looks like there are some fanotify tests in the filesystems selftests; I guess they could be extended to add a regression test?

But FWIW, I think the kernel is just passing the ofset/length info off to user space and isn't acting on it itself. So there is no kernel vulnerability here.

Right, I'm rather wondering if this could have been caught earlier and how we could have caught it earlier :)

Signature · 3 lines
Cheers

David / dhildenb
#7 lore raw
Quoted 22 lines · David Hildenbrand
Quoted 20 lines · Ryan Roberts
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Quoted 8 lines
Quoted 6 lines
Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Curious: is there some easy way to write a reproducer? Did you look into that?

I didn't; this was just a drive-by discovery.

Quoted 8 lines
Quoted 5 lines

It looks like there are some fanotify tests in the filesystems selftests; I guess they could be extended to add a regression test?

But FWIW, I think the kernel is just passing the ofset/length info off to user space and isn't acting on it itself. So there is no kernel vulnerability here.

Right, I'm rather wondering if this could have been caught earlier and how we could have caught it earlier :)

Ha! you would have thought we either have no test for it or we test only mmap with offset 0.

But we have LTP test fanotify24 which does mmap with offset page_sz*100 and indeed it prints the info and info says offset 0, only we do not verify the offset info in this test...

Will be fixed.

Thanks Ryan for being alert!
Amir.

#8 lore raw
Quoted 33 lines · Amir Goldstein
Quoted 22 lines · David Hildenbrand
Quoted 20 lines · Ryan Roberts
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Quoted 8 lines
Quoted 6 lines
Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Curious: is there some easy way to write a reproducer? Did you look into that?

I didn't; this was just a drive-by discovery.

Quoted 8 lines
Quoted 5 lines

It looks like there are some fanotify tests in the filesystems selftests; I guess they could be extended to add a regression test?

But FWIW, I think the kernel is just passing the ofset/length info off to user space and isn't acting on it itself. So there is no kernel vulnerability here.

Right, I'm rather wondering if this could have been caught earlier and how we could have caught it earlier :)

Quoted 8 lines

Ha! you would have thought we either have no test for it or we test only mmap with offset 0.

But we have LTP test fanotify24 which does mmap with offset page_sz*100 and indeed it prints the info and info says offset 0, only we do not verify the offset info in this test...

Will be fixed.

Jan,

FYI test enhanced and verified the bug and the fix:
https://github.com/amir73il/ltp/commits/fsnotify-fixes/

Will wait with posting the test until you merge the fix to make sure that the commit id is not changed.

Thanks,
Amir.

#9 lore raw
In reply to Ryan Roberts
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 7 lines

Discovered during code review.

Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Thanks Ryan! I've added the patch to my tree. As a side note, I know the callsite is in mm/ but since this is clearly impacting fsnotify, it would be good to add to CC relevant people (I'm not following linux-mm nor linux-kernel) and discovered this only because of Kiryl's link...

Honza

Quoted 27 lines
 mm/util.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/util.c +2 -2
index 6c1d64ed0221..8989d5767528 100644
@@ -566,6 +566,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot,
 	unsigned long flag, unsigned long pgoff)
 {
+	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 	unsigned long ret;
 	struct mm_struct *mm = current->mm;
 	unsigned long populate;
@@ -573,7 +574,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret)
-		ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
+		ret = fsnotify_mmap_perm(file, prot, off, len);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;
--

2.43.0

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#10 lore raw
Quoted 14 lines · Jan Kara
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 7 lines

Discovered during code review.

Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Quoted 4 lines

Thanks Ryan! I've added the patch to my tree. As a side note, I know the callsite is in mm/ but since this is clearly impacting fsnotify, it would be good to add to CC relevant people (I'm not following linux-mm nor linux-kernel) and discovered this only because of Kiryl's link...

Ahh good point... Sorry I was sleepwalking through the process on Friday afternoon and blindly sent it to the maintainers and reviewers that get_maintainer.pl spat out. It didn't even occur to me that this wasn't an mm thing. :-|

Quoted 30 lines

Honza

Quoted 27 lines
 mm/util.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/util.c +2 -2
index 6c1d64ed0221..8989d5767528 100644
@@ -566,6 +566,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot,
 	unsigned long flag, unsigned long pgoff)
 {
+	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 	unsigned long ret;
 	struct mm_struct *mm = current->mm;
 	unsigned long populate;
@@ -573,7 +574,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret)
-		ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
+		ret = fsnotify_mmap_perm(file, prot, off, len);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;
--

2.43.0

#11 lore raw
Quoted 20 lines · Ryan Roberts
Quoted 14 lines · Jan Kara
Quoted 5 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Quoted 7 lines

Discovered during code review.

Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

---
Applies against today's mm-unstable (aa05a436eca8).

Quoted 4 lines

Thanks Ryan! I've added the patch to my tree. As a side note, I know the callsite is in mm/ but since this is clearly impacting fsnotify, it would be good to add to CC relevant people (I'm not following linux-mm nor linux-kernel) and discovered this only because of Kiryl's link...

Quoted 4 lines

Ahh good point... Sorry I was sleepwalking through the process on Friday afternoon and blindly sent it to the maintainers and reviewers that get_maintainer.pl spat out. It didn't even occur to me that this wasn't an mm thing. :-|

No harm done really. The change is an obvious fix and it would find its way to the kernel sooner or later. As I wrote above, this is just a note for the future to think a bit about patch recipients before hitting send :) It may help to get the patch merged faster.

Honza

Quoted 28 lines
Quoted 27 lines
Quoted 27 lines
 mm/util.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/util.c +2 -2
index 6c1d64ed0221..8989d5767528 100644
@@ -566,6 +566,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot,
 	unsigned long flag, unsigned long pgoff)
 {
+	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 	unsigned long ret;
 	struct mm_struct *mm = current->mm;
 	unsigned long populate;
@@ -573,7 +574,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret)
-		ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
+		ret = fsnotify_mmap_perm(file, prot, off, len);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;
--

2.43.0

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Reviewed-by on v1 1/1
#12 lore raw
In reply to Ryan Roberts
Quoted 7 lines · Ryan Roberts

fsnotify_mmap_perm() requires a byte offset for the file about to be mmap'ed. But it is called from vm_mmap_pgoff(), which has a page offset. Previously the conversion was done incorrectly so let's fix it, being careful not to overflow on 32-bit platforms.

Discovered during code review.

Yikes, that's kind of crazy that has been in place for so long but not picked up...

Quoted 3 lines
Fixes 066e053fe208 ("fsnotify: add pre-content hooks on mmap()")

Thanks for fixing this!

LGTM, so:

Quoted 34 lines

---
Applies against today's mm-unstable (aa05a436eca8).

Thanks,
Ryan

 mm/util.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/util.c +2 -2
index 6c1d64ed0221..8989d5767528 100644
@@ -566,6 +566,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 	unsigned long len, unsigned long prot,
 	unsigned long flag, unsigned long pgoff)
 {
+	loff_t off = (loff_t)pgoff << PAGE_SHIFT;
 	unsigned long ret;
 	struct mm_struct *mm = current->mm;
 	unsigned long populate;
@@ -573,7 +574,7 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr,
 
 	ret = security_mmap_file(file, prot, flag);
 	if (!ret)
-		ret = fsnotify_mmap_perm(file, prot, pgoff >> PAGE_SHIFT, len);
+		ret = fsnotify_mmap_perm(file, prot, off, len);
 	if (!ret) {
 		if (mmap_write_lock_killable(mm))
 			return -EINTR;
--

2.43.0