Skip to contentSkip to conversation

mm/mremap: Fail map duplication attempts for private mappings

PATCH Superseded Mike Kravetz 4 participants 13 messages Jul 13, 2017 – Jul 24, 2017

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

PATCH 1/1 mm/mremap: Fail map duplication attempts for private mappings

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

---
 mm/mremap.c | 7 +++++++
 1 file changed, 7 insertions(+)
mm/mremap.c +7 -0
index cd8a1b1..076f506 100644
@@ -383,6 +383,13 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 7 lines
2.7.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 1/1
#1 lore raw

[+CC linux-api]

Quoted 8 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Quoted 4 lines

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

Quoted 23 lines
---
 mm/mremap.c | 7 +++++++
 1 file changed, 7 insertions(+)
mm/mremap.c +7 -0
index cd8a1b1..076f506 100644
@@ -383,6 +383,13 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
#2 lore raw
Quoted 11 lines · Vlastimil Babka

[+CC linux-api]

Quoted 8 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Quoted 7 lines
Quoted 4 lines

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

In another e-mail thread, Andrea makes the case that mremap(old_size == 0) of private file backed mappings could possibly be used for something useful. For example to create a private COW mapping. Of course, a better way to do this would be simply using the fd to create a private mapping.

If returning EINVAL for all private mappings is too general, the following patch adds a check to only return EINVAL for private anon mappings.

mm/mremap: Fail map duplication attempts for private anon mappings

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private anonymous mappings. If duplication is attempted for a private anon mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where an attempt is made to duplicate a private anon mapping.

---
 mm/mremap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/mremap.c +8 -0
index cd8a1b1..586ea3d 100644
@@ -383,6 +383,14 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private anon mappings.
+	 */
+	if (!old_len && vma_is_anonymous(vma) &&
+	    !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 7 lines
2.7.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw
Quoted 49 lines · Mike Kravetz
Quoted 11 lines · Vlastimil Babka

[+CC linux-api]

Quoted 8 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Quoted 6 lines
Quoted 4 lines

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

In another e-mail thread, Andrea makes the case that mremap(old_size == 0) of private file backed mappings could possibly be used for something useful. For example to create a private COW mapping. Of course, a better way to do this would be simply using the fd to create a private mapping.

If returning EINVAL for all private mappings is too general, the following patch adds a check to only return EINVAL for private anon mappings.

mm/mremap: Fail map duplication attempts for private anon mappings

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private anonymous mappings. If duplication is attempted for a private anon mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where an attempt is made to duplicate a private anon mapping.

---
 mm/mremap.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/mremap.c +0 -0
index cd8a1b1..586ea3d 100644

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 392

Quoted 10 lines
mm/mremap.c +7 -0
@@ -383,6 +383,14 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private anon mappings.
+	 */
+	if (!old_len && vma_is_anonymous(vma) &&
+	    !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);

Sounds better compared to rejecting everything private.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
In reply to Mike Kravetz
Quoted 15 lines · Mike Kravetz
Quoted 14 lines · Vlastimil Babka

[+CC linux-api]

Quoted 11 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

In another e-mail thread, Andrea makes the case that mremap(old_size == 0) of private file backed mappings could possibly be used for something useful. For example to create a private COW mapping.

What does this mean exactly? I do not see it would force CoW so again the new mapping could fail with the basic invariant that the content of the new mapping should match the old one (e.g. old mapping already CoWed some pages the new mapping would still contain the origin content unless I am missing something).

[...]

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 392

Quoted 7 lines
mm/mremap.c +7 -0
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private anon mappings.
+	 */
+	if (!old_len && vma_is_anonymous(vma) &&
+	    !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);

Why is vma_is_anonymous() without VM_*SHARE* check insufficient?

Signature · 2 lines
Michal Hocko
SUSE Labs
#5 lore raw
Quoted 25 lines · Michal Hocko
Quoted 15 lines · Mike Kravetz
Quoted 14 lines · Vlastimil Babka

[+CC linux-api]

Quoted 11 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

In another e-mail thread, Andrea makes the case that mremap(old_size == 0) of private file backed mappings could possibly be used for something useful. For example to create a private COW mapping.

Quoted 5 lines

What does this mean exactly? I do not see it would force CoW so again the new mapping could fail with the basic invariant that the content of the new mapping should match the old one (e.g. old mapping already CoWed some pages the new mapping would still contain the origin content unless I am missing something).

I do not think you are missing anything. You are correct in saying that the new mapping would be COW of the original file contents. It is NOT based on any private pages of the old private mapping. Sorry, my wording above was not quite clear.

As previously discussed, the more straight forward to way to accomplish the same thing would be a simple call to mmap with the fd.

After thinking about this some more, perhaps the original patch to return EINVAL for all private mappings makes more sense. Even in the case of a file backed private mapping, the new mapping will be based on the file and not the old mapping. The purpose of mremap is to create a new mapping based on the old mapping. So, this is not strictly in line with the purpose of mremap.

Actually, the more I think about this, the more I wish there was some way to deprecate and eventually eliminate the old_size == 0 behavior.

Quoted 10 lines

[...]

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 392

Quoted 7 lines
mm/mremap.c +7 -0
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private anon mappings.
+	 */
+	if (!old_len && vma_is_anonymous(vma) &&
+	    !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);

Why is vma_is_anonymous() without VM_*SHARE* check insufficient?

Are you asking,
why is if (!old_len && vma_is_anonymous(vma)) insufficient?

If so, you are correct that the additional check for VM_*SHARE* is not necessary. Shared mappings are technically not anonymous as they must contain a common backing object.

The !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE) check was there in the first patch to catch all private mappings. When adding vma_is_anonymous(vma), I missed the fact that it was redundant. But, based on your comments above I think the first patch is more correct.

Signature · 7 lines
Mike Kravetz

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw
Quoted 40 lines · Mike Kravetz
Quoted 25 lines · Michal Hocko
Quoted 15 lines · Mike Kravetz
Quoted 14 lines · Vlastimil Babka

[+CC linux-api]

Quoted 11 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

In another e-mail thread, Andrea makes the case that mremap(old_size == 0) of private file backed mappings could possibly be used for something useful. For example to create a private COW mapping.

Quoted 5 lines

What does this mean exactly? I do not see it would force CoW so again the new mapping could fail with the basic invariant that the content of the new mapping should match the old one (e.g. old mapping already CoWed some pages the new mapping would still contain the origin content unless I am missing something).

I do not think you are missing anything. You are correct in saying that the new mapping would be COW of the original file contents. It is NOT based on any private pages of the old private mapping. Sorry, my wording above was not quite clear.

As previously discussed, the more straight forward to way to accomplish the same thing would be a simple call to mmap with the fd.

Quoted 6 lines

After thinking about this some more, perhaps the original patch to return EINVAL for all private mappings makes more sense. Even in the case of a file backed private mapping, the new mapping will be based on the file and not the old mapping. The purpose of mremap is to create a new mapping based on the old mapping. So, this is not strictly in line with the purpose of mremap.

Yes that is exactly my point. One would expect that the new mapping has the same content as the previous mapping at the time when it was created and the copy will be "atomic" (wrt. page faults). Otherwise you could simply implement it in the userspace.

That being said, I do not think we should try to pretend this is a correct behavior and the !old_len should be supported only for the shared mappings which have at least reasonable semantic.

Quoted 14 lines

Actually, the more I think about this, the more I wish there was some way to deprecate and eventually eliminate the old_size == 0 behavior.

Quoted 10 lines

[...]

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 392

Quoted 7 lines
mm/mremap.c +7 -0
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private anon mappings.
+	 */
+	if (!old_len && vma_is_anonymous(vma) &&
+	    !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);

Why is vma_is_anonymous() without VM_*SHARE* check insufficient?

Quoted 2 lines

Are you asking,
why is if (!old_len && vma_is_anonymous(vma)) insufficient?

yes

Quoted 3 lines

If so, you are correct that the additional check for VM_*SHARE* is not necessary. Shared mappings are technically not anonymous as they must contain a common backing object.

that is my understanding as well. But maybe there are some weird mappings which do not have vm_ops and populate the whole range inside the mmap callback. I remember we had a CVE for those but forgot all details of course. Failing on those doesn't seem like a tragedy to me and maybe it is even correct.

Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw
In reply to Vlastimil Babka
Quoted 11 lines · Vlastimil Babka

[+CC linux-api]

Quoted 8 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Quoted 6 lines
Quoted 4 lines

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

After considering Michal's concerns with follow on patch, it appears this patch provides the most desired behavior. Any other concerns or issues with this patch?

If this moves forward, I will create man page updates to describe the mremap(old_size == 0) behavior.

Signature · 1 line
Mike Kravetz
Quoted 13 lines
Quoted 13 lines
---
 mm/mremap.c | 7 +++++++
 1 file changed, 7 insertions(+)
mm/mremap.c +0 -0
index cd8a1b1..076f506 100644
@@ -383,6 +383,13 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
Quoted 12 lines

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 395

Quoted 11 lines
mm/mremap.c +7 -0
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw
Quoted 15 lines · Mike Kravetz
Quoted 14 lines · Vlastimil Babka

[+CC linux-api]

Quoted 11 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

After considering Michal's concerns with follow on patch, it appears this patch provides the most desired behavior. Any other concerns or issues with this patch?

Maybe we should add a pr_warn_once to make users aware that this is no longer supported.

Quoted 22 lines

If this moves forward, I will create man page updates to describe the mremap(old_size == 0) behavior.

Signature · 1 line
Mike Kravetz
Quoted 13 lines
Quoted 13 lines
---
 mm/mremap.c | 7 +++++++
 1 file changed, 7 insertions(+)
mm/mremap.c +0 -0
index cd8a1b1..076f506 100644
@@ -383,6 +383,13 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
Quoted 3 lines

mm/mremap.c static struct vm_area_struct *vma_to_resize(unsigned long addr, · line 388

Quoted 3 lines
mm/mremap.c +3 -0
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.

Do not allow this for private mappings because we have never really duplicated the range for those so the new VMA is a fresh one unrelated to the original one which breaks mremap semantic. While we can do that there doesn't seem to be any existing usecase currently.

Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
mm/mremap.c +4 -0
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE)))
+		return ERR_PTR(-EINVAL);
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#9 lore raw

PATCH v2 1/1 mm/mremap: Fail map duplication attempts for private mappings

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings. If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping. Also, print a warning message (once) if such an attempt is made.

---
 mm/mremap.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/mremap.c +9 -0
index cd8a1b1..949f6a7 100644
@@ -383,6 +383,15 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
+		pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
+		return ERR_PTR(-EINVAL);
+	}
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 7 lines
2.7.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v2 1/1
#10 lore raw
Quoted 3 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings.

sorry for the nit picking but this is not true strictly speaking. It makes some sense, arguably (e.g. take an atomic snapshot of the mapping). It doesn't make any sense with the _current_ implementation.

Quoted 11 lines

If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping. Also, print a warning message (once) if such an attempt is made.

I do not insist on the comment update suggested
http://lkml.kernel.org/r/[email protected]
but I would appreciate it...

Other than that looks reasonably to me

Quoted 27 lines
---
 mm/mremap.c | 9 +++++++++
 1 file changed, 9 insertions(+)
mm/mremap.c +9 -0
index cd8a1b1..949f6a7 100644
@@ -383,6 +383,15 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len  is a special case where a mapping is 'duplicated'.
+	 * Do not allow this for private mappings.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
+		pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
+		return ERR_PTR(-EINVAL);
+	}
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 1 line
2.7.5
Signature · 8 lines
Michal Hocko
SUSE Labs

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#11 lore raw
Quoted 21 lines · Michal Hocko
Quoted 3 lines · Mike Kravetz

mremap will create a 'duplicate' mapping if old_size == 0 is specified. Such duplicate mappings make no sense for private mappings.

sorry for the nit picking but this is not true strictly speaking. It makes some sense, arguably (e.g. take an atomic snapshot of the mapping). It doesn't make any sense with the _current_ implementation.

Quoted 11 lines

If duplication is attempted for a private mapping, mremap creates a separate private mapping unrelated to the original mapping and makes no modifications to the original. This is contrary to the purpose of mremap which should return a mapping which is in some way related to the original.

Therefore, return EINVAL in the case where if an attempt is made to duplicate a private mapping. Also, print a warning message (once) if such an attempt is made.

Quoted 7 lines

I do not insist on the comment update suggested
http://lkml.kernel.org/r/[email protected]
but I would appreciate it...

Other than that looks reasonably to me

My apologies. I overlooked your comment about the comment when creating the patch. Below is the patch with commit message and comment updated.

Quoted 1 line

From 5c4a1602bd6a942544ed011dc0a72fd258e874b2 Mon Sep 17 00:00:00 2001

From: Mike Kravetz <[email protected]>
Date: Wed, 12 Jul 2017 13:52:47 -0700
Subject: [PATCH] mm/mremap: Fail map duplication attempts for private mappings

mremap will attempt to create a 'duplicate' mapping if old_size == 0 is specified. In the case of private mappings, mremap will actually create a fresh separate private mapping unrelated to the original. This does not fit with the design semantics of mremap as the intention is to create a new mapping based on the original.

Therefore, return EINVAL in the case where an attempt is made to duplicate a private mapping. Also, print a warning message (once) if such an attempt is made.

---
 mm/mremap.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
mm/mremap.c +13 -0
index cd8a1b1..75b167d 100644
@@ -383,6 +383,19 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len is a special case where an attempt is made to 'duplicate'
+	 * a mapping.  This makes no sense for private mappings as it will
+	 * instead create a fresh/new mapping unrelated to the original.  This
+	 * is contrary to the basic idea of mremap which creates new mappings
+	 * based on the original.  There are no known use cases for this
+	 * behavior.  As a result, fail such attempts.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
+		pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
+		return ERR_PTR(-EINVAL);
+	}
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 7 lines
2.7.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by
#12 lore raw

On Fri 21-07-17 14:18:31, Mike Kravetz wrote:
[...]

Quoted 12 lines
Quoted 1 line

From 5c4a1602bd6a942544ed011dc0a72fd258e874b2 Mon Sep 17 00:00:00 2001

From: Mike Kravetz <[email protected]>
Date: Wed, 12 Jul 2017 13:52:47 -0700
Subject: [PATCH] mm/mremap: Fail map duplication attempts for private mappings

mremap will attempt to create a 'duplicate' mapping if old_size == 0 is specified. In the case of private mappings, mremap will actually create a fresh separate private mapping unrelated to the original. This does not fit with the design semantics of mremap as the intention is to create a new mapping based on the original.

Quoted 5 lines

Therefore, return EINVAL in the case where an attempt is made to duplicate a private mapping. Also, print a warning message (once) if such an attempt is made.

Thanks!

Quoted 30 lines
---
 mm/mremap.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
mm/mremap.c +13 -0
index cd8a1b1..75b167d 100644
@@ -383,6 +383,19 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
 	if (!vma || vma->vm_start > addr)
 		return ERR_PTR(-EFAULT);
 
+	/*
+	 * !old_len is a special case where an attempt is made to 'duplicate'
+	 * a mapping.  This makes no sense for private mappings as it will
+	 * instead create a fresh/new mapping unrelated to the original.  This
+	 * is contrary to the basic idea of mremap which creates new mappings
+	 * based on the original.  There are no known use cases for this
+	 * behavior.  As a result, fail such attempts.
+	 */
+	if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
+		pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap.  This is not supported.\n", current->comm, current->pid);
+		return ERR_PTR(-EINVAL);
+	}
+
 	if (is_vm_hugetlb_page(vma))
 		return ERR_PTR(-EINVAL);
 
Signature · 1 line
2.7.5
Signature · 2 lines
Michal Hocko
SUSE Labs