Skip to contentSkip to conversation

mm/mmap: Fix MAP_FIXED address return on VMA merge

PATCH Applied Liam Howlett 3 participants 5 messages Oct 18, 2022 – Oct 19, 2022

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

PATCH 1/1 mm/mmap: Fix MAP_FIXED address return on VMA merge

mmap should return the start address of newly mapped area when successful. On a successful merge of a VMA, the return address was changed and thus was violating that expectation from userspace.

This is a restoration of functionality provided by 309d08d9b3a3 (mm/mmap.c: fix mmap return value when vma is merged after call_mmap()). For completeness of fixing MAP_FIXED, implement the comments from the previous discussion to never update the address and fail if the address changes. Leaving the error as a WARN_ON() to avoid crashing the kernel.

Cc Liu Zixian <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Matthew Wilcox <[email protected]>
Fixes 4dd1b84140c1 (mm/mmap: use advanced maple tree API for mmap_region())
---
 mm/mmap.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
mm/mmap.c +7 -8
index 42cd2c260898..22010e13f1a1 100644
@@ -2625,14 +2625,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 		if (error)
 			goto unmap_and_free_vma;
 
-		/* Can addr have changed??
-		 *
-		 * Answer: Yes, several device drivers can do it in their
-		 *         f_op->mmap method. -DaveM
+		/*
+		 * Expansion is handled above, merging is handled below.
+		 * Drivers should not alter the address of the VMA.
 		 */
-		WARN_ON_ONCE(addr != vma->vm_start);
-
-		addr = vma->vm_start;
+		if (WARN_ON((addr != vma->vm_start))) {
+			error = -EINVAL;
+			goto close_and_free_vma;
+		}
 		mas_reset(&mas);
 
 		/*
@@ -2654,7 +2654,6 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
 				vm_area_free(vma);
 				vma = merge;
 				/* Update vm_flags to pick up the change. */
-				addr = vma->vm_start;
 				vm_flags = vma->vm_flags;
 				goto unmap_writable;
 			}
Signature · 1 line
2.35.1
#1 lore raw
Quoted 4 lines · Liam Howlett

mmap should return the start address of newly mapped area when successful. On a successful merge of a VMA, the return address was changed and thus was violating that expectation from userspace.

Just wondering, do we have a simple user space reproducer / test?

Do we want to add some more tests for such scenarios?

Quoted 32 lines

This is a restoration of functionality provided by 309d08d9b3a3 (mm/mmap.c: fix mmap return value when vma is merged after call_mmap()). For completeness of fixing MAP_FIXED, implement the comments from the previous discussion to never update the address and fail if the address changes. Leaving the error as a WARN_ON() to avoid crashing the kernel.

Cc Liu Zixian <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Matthew Wilcox <[email protected]>
Fixes 4dd1b84140c1 (mm/mmap: use advanced maple tree API for mmap_region())
---
  mm/mmap.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
mm/mmap.c +1 -4
index 42cd2c260898..22010e13f1a1 100644
@@ -2625,14 +2625,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  		if (error)
  			goto unmap_and_free_vma;
 
-		/* Can addr have changed??
-		 *
-		 * Answer: Yes, several device drivers can do it in their
-		 *         f_op->mmap method. -DaveM
+		/*

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2635

Quoted 10 lines
mm/mmap.c +6 -3
+		 * Expansion is handled above, merging is handled below.
+		 * Drivers should not alter the address of the VMA.
  		 */
-		WARN_ON_ONCE(addr != vma->vm_start);
-
-		addr = vma->vm_start;
+		if (WARN_ON((addr != vma->vm_start))) {
+			error = -EINVAL;
+			goto close_and_free_vma;
+		}

If this is something that user space can trigger, WARN_* is the wrong choice. But what I understand from the comment change is that this must not happen at that point unless there is a real issue.

Why not "if (WARN_ON_ONCE)" ?

Signature · 3 lines
Thanks,

David / dhildenb
#2 lore raw
Quoted 8 lines · David Hildenbrand

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2635

Quoted 4 lines
mm/mmap.c +4 -0
+		if (WARN_ON((addr != vma->vm_start))) {
+			error = -EINVAL;
+			goto close_and_free_vma;
+		}

If this is something that user space can trigger, WARN_* is the wrong choice. But what I understand from the comment change is that this must not happen at that point unless there is a real issue.

It's something that a device driver can trigger. So if userspace calls mmap(MAP_FIXED) and the driver decides to overwrite the vma->vm_start, it'll trigger. I think WARN_ON() is the right choice.

Quoted 1 line

Why not "if (WARN_ON_ONCE)" ?

Because by it's nature it's not going to trigger a bajillion times in quick succession.

#3 lore raw
In reply to David Hildenbrand
Quoted 7 lines · David Hildenbrand
Quoted 4 lines · Liam Howlett

mmap should return the start address of newly mapped area when successful. On a successful merge of a VMA, the return address was changed and thus was violating that expectation from userspace.

Just wondering, do we have a simple user space reproducer / test?

Not today, no.

Quoted 2 lines

Do we want to add some more tests for such scenarios?

Yes, I will code something up for LTP.

Quoted 38 lines
Quoted 38 lines

This is a restoration of functionality provided by 309d08d9b3a3 (mm/mmap.c: fix mmap return value when vma is merged after call_mmap()). For completeness of fixing MAP_FIXED, implement the comments from the previous discussion to never update the address and fail if the address changes. Leaving the error as a WARN_ON() to avoid crashing the kernel.

Cc Liu Zixian <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc Matthew Wilcox <[email protected]>
Fixes 4dd1b84140c1 (mm/mmap: use advanced maple tree API for mmap_region())
---
  mm/mmap.c | 15 +++++++--------
  1 file changed, 7 insertions(+), 8 deletions(-)
mm/mmap.c +3 -7
index 42cd2c260898..22010e13f1a1 100644
@@ -2625,14 +2625,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
  		if (error)
  			goto unmap_and_free_vma;
-		/* Can addr have changed??
-		 *
-		 * Answer: Yes, several device drivers can do it in their
-		 *         f_op->mmap method. -DaveM
+		/*
+		 * Expansion is handled above, merging is handled below.
+		 * Drivers should not alter the address of the VMA.
  		 */
-		WARN_ON_ONCE(addr != vma->vm_start);
-
-		addr = vma->vm_start;
Quoted 8 lines

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2635

Quoted 4 lines
mm/mmap.c +4 -0
+		if (WARN_ON((addr != vma->vm_start))) {
+			error = -EINVAL;
+			goto close_and_free_vma;
+		}

If this is something that user space can trigger, WARN_* is the wrong choice. But what I understand from the comment change is that this must not happen at that point unless there is a real issue.

The VMA start address could be changed in call_mmap() which is a driver call. I guess someone could write a driver to mmap by a users action? I don't think it can be reached other ways. In any case, I'm changing a WARN_ON_ONCE() to a WARN_ON() and undoing the badness instead of marching forwards.

Quoted 2 lines

Why not "if (WARN_ON_ONCE)" ?

I was thinking it was harder to ignore if it happen more frequently? There isn't a driver that does this now, but I'm not picky over which variant to call.

Thanks,
Liam

#4 lore raw
Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
   mm/mmap.c | 15 +++++++--------
   1 file changed, 7 insertions(+), 8 deletions(-)
mm/mmap.c +3 -7
index 42cd2c260898..22010e13f1a1 100644
@@ -2625,14 +2625,14 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
   		if (error)
   			goto unmap_and_free_vma;
-		/* Can addr have changed??
-		 *
-		 * Answer: Yes, several device drivers can do it in their
-		 *         f_op->mmap method. -DaveM
+		/*
+		 * Expansion is handled above, merging is handled below.
+		 * Drivers should not alter the address of the VMA.
   		 */
-		WARN_ON_ONCE(addr != vma->vm_start);
-
-		addr = vma->vm_start;
Quoted 8 lines

mm/mmap.c unsigned long mmap_region(struct file *file, unsigned long addr, · line 2635

Quoted 4 lines
mm/mmap.c +4 -0
+		if (WARN_ON((addr != vma->vm_start))) {
+			error = -EINVAL;
+			goto close_and_free_vma;
+		}

If this is something that user space can trigger, WARN_* is the wrong choice. But what I understand from the comment change is that this must not happen at that point unless there is a real issue.

Quoted 5 lines

The VMA start address could be changed in call_mmap() which is a driver call. I guess someone could write a driver to mmap by a users action? I don't think it can be reached other ways. In any case, I'm changing a WARN_ON_ONCE() to a WARN_ON() and undoing the badness instead of marching forwards.

WARN_ON_ONCE() can also be used in conditionals if that's what you were concerned about, but ...

Quoted 7 lines
Quoted 3 lines

Why not "if (WARN_ON_ONCE)" ?

I was thinking it was harder to ignore if it happen more frequently? There isn't a driver that does this now, but I'm not picky over which variant to call.

.. I think the assumption really is that we won't see (m)any these calls. And if we do, it's a bad bad driver. So WARN_ON() might be just fine.

If this would be easy to trigger by any user space, WARN* would have been the wrong choice, that's why I was asking.

Signature · 3 lines
Thanks,

David / dhildenb