mm: Ensure get_unmapped_area() returns higher address than mmap_min_addr
Subsystems (2)
PATCH 1/1 mm: Ensure get_unmapped_area() returns higher address than mmap_min_addr
This patch fixes the problem that get_unmapped_area() can return illegal address and result in failing mmap(2) etc.
In case that the address higher than PAGE_SIZE is set to /proc/sys/vm/mmap_min_addr, the address lower than mmap_min_addr can be returned by get_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).
This is because the current get_unmapped_area() code does not take into account mmap_min_addr.
This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without CAP_SYS_RAWIO, although any illegal parameter is not passed.
2. The bottom-up search path after the top-down search might not work in arch_get_unmapped_area_topdown().
[How to reproduce]
--- test.c -------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/errno.h>
int main(int argc, char *argv[])
{
void *ret = NULL, *last_map;
size_t pagesize = sysconf(_SC_PAGESIZE);
do {
last_map = ret;
ret = mmap(0, pagesize, PROT_NONE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// printf("ret=%p\n", ret);
} while (ret != MAP_FAILED);
if (errno != ENOMEM) {
printf("ERR: unexpected errno: %d (last map=%p)\n",
errno, last_map);
}
return 0;
}
---------------------------------------------------------------
$ gcc -m32 -o test test.c
$ sudo sysctl -w vm.mmap_min_addr=65536
vm.mmap_min_addr = 65536
$ ./test (run as non-priviledge user)
ERR: unexpected errno: 1 (last map=0x10000)--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
mm/mmap.c +5 -5
@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )
@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }
@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )
@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);
Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>
Re: [PATCH] mm: Ensure get_unmapped_area() returns higher addressthan mmap_min_addr
Hi,
Quoted 65 lines · Akira Takeuchi
This patch fixes the problem that
get_unmapped_area()can return illegal address and result in failing mmap(2) etc.In case that the address higher than
PAGE_SIZEis set to/proc/sys/vm/mmap_min_addr, the address lower thanmmap_min_addrcan be returned byget_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).This is because the current
get_unmapped_area()code does not take into accountmmap_min_addr.This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without
CAP_SYS_RAWIO, although any illegal parameter is not passed.2. The bottom-up search path after the top-down search might not work in
arch_get_unmapped_area_topdown().[How to reproduce]
--- test.c ------------------------------------------------- #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/errno.h> int main(int argc, char *argv[]) { void *ret = NULL, *last_map; size_t pagesize = sysconf(_SC_PAGESIZE); do { last_map = ret; ret = mmap(0, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); // printf("ret=%p\n", ret); } while (ret != MAP_FAILED); if (errno != ENOMEM) { printf("ERR: unexpected errno: %d (last map=%p)\n", errno, last_map); } return 0; } --------------------------------------------------------------- $ gcc -m32 -o test test.c $ sudo sysctl -w vm.mmap_min_addr=65536 vm.mmap_min_addr = 65536 $ ./test (run as non-priviledge user) ERR: unexpected errno: 1 (last map=0x10000)Signed-off-by Akira Takeuchi <[email protected]>Signed-off-by Kiyoshi Owada <[email protected]>--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)mm/mmap.c +0
-0
mm/mmap.c arch_get_unmapped_area(struct file *filp, unsigned long addr, · line 1862
Quoted 8 lines
mm/mmap.c +1
-1@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )
I feel that it looks clearer to fix this in round_hint_to_min(), with doing mmap_min_addr check in hint == NULL case. Does it work for you?
Thanks,
Naoya Horiguchi
Quoted 48 lines
mm/mmap.c +4
-4@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>
--
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>
Re: [PATCH] mm: Ensure get_unmapped_area() returns higher addressthan mmap_min_addr
Hi,
Quoted 75 lines · On Wed, 23 Oct 2013 00:26:05 -0400 Naoya Horiguchi <[email protected]>…
Hi,
Quoted 72 lines · Akira Takeuchi
This patch fixes the problem that
get_unmapped_area()can return illegal address and result in failing mmap(2) etc.In case that the address higher than
PAGE_SIZEis set to/proc/sys/vm/mmap_min_addr, the address lower thanmmap_min_addrcan be returned byget_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).This is because the current
get_unmapped_area()code does not take into accountmmap_min_addr.This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without
CAP_SYS_RAWIO, although any illegal parameter is not passed.2. The bottom-up search path after the top-down search might not work in
arch_get_unmapped_area_topdown().[How to reproduce]
--- test.c ------------------------------------------------- #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/errno.h> int main(int argc, char *argv[]) { void *ret = NULL, *last_map; size_t pagesize = sysconf(_SC_PAGESIZE); do { last_map = ret; ret = mmap(0, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); // printf("ret=%p\n", ret); } while (ret != MAP_FAILED); if (errno != ENOMEM) { printf("ERR: unexpected errno: %d (last map=%p)\n", errno, last_map); } return 0; } --------------------------------------------------------------- $ gcc -m32 -o test test.c $ sudo sysctl -w vm.mmap_min_addr=65536 vm.mmap_min_addr = 65536 $ ./test (run as non-priviledge user) ERR: unexpected errno: 1 (last map=0x10000)Signed-off-by Akira Takeuchi <[email protected]>Signed-off-by Kiyoshi Owada <[email protected]>--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)mm/mmap.c +1
-1@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;
Quoted 5 lines
mm/mmap.c arch_get_unmapped_area(struct file *filp, unsigned long addr, · line 1862
Quoted 1 line
mm/mmap.c +0
-0if (flags &MAP_FIXED )I feel that it looks clearer to fix this in
round_hint_to_min(), with doingmmap_min_addrcheck in hint == NULL case. Does it work for you?
The current round_hint_to_min() code checks and adjusts just for the hint address, not for "len". Also, it returns no error.
Do you mean adding "len" to the argument of round_hint_to_min() and making round_hint_to_min() return any error ?
Regards,
Akira Takeuchi
Quoted 51 lines
Thanks,
Naoya HoriguchiQuoted 48 lines
mm/mmap.c +4
-4@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>
Signature · 7 lines
Akira Takeuchi <[email protected]> -- 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>
Naoya Horiguchi
Reviewed-byRe: [PATCH] mm: Ensure get_unmapped_area() returns higheraddressthan mmap_min_addr
Quoted 86 lines · Akira Takeuchi
Hi,
Quoted 75 lines · On Wed, 23 Oct 2013 00:26:05 -0400 Naoya Horiguchi <[email protected]>…
Hi,
Quoted 72 lines · Akira Takeuchi
This patch fixes the problem that
get_unmapped_area()can return illegal address and result in failing mmap(2) etc.In case that the address higher than
PAGE_SIZEis set to/proc/sys/vm/mmap_min_addr, the address lower thanmmap_min_addrcan be returned byget_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).This is because the current
get_unmapped_area()code does not take into accountmmap_min_addr.This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without
CAP_SYS_RAWIO, although any illegal parameter is not passed.2. The bottom-up search path after the top-down search might not work in
arch_get_unmapped_area_topdown().[How to reproduce]
--- test.c ------------------------------------------------- #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/errno.h> int main(int argc, char *argv[]) { void *ret = NULL, *last_map; size_t pagesize = sysconf(_SC_PAGESIZE); do { last_map = ret; ret = mmap(0, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); // printf("ret=%p\n", ret); } while (ret != MAP_FAILED); if (errno != ENOMEM) { printf("ERR: unexpected errno: %d (last map=%p)\n", errno, last_map); } return 0; } --------------------------------------------------------------- $ gcc -m32 -o test test.c $ sudo sysctl -w vm.mmap_min_addr=65536 vm.mmap_min_addr = 65536 $ ./test (run as non-priviledge user) ERR: unexpected errno: 1 (last map=0x10000)Signed-off-by Akira Takeuchi <[email protected]>Signed-off-by Kiyoshi Owada <[email protected]>--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)mm/mmap.c +1
-1@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;Quoted 5 lines
mm/mmap.c arch_get_unmapped_area(struct file *filp, unsigned long addr, · line 1862
Quoted 1 line
mm/mmap.c +0
-0if (flags &MAP_FIXED )I feel that it looks clearer to fix this in
round_hint_to_min(), with doingmmap_min_addrcheck in hint == NULL case. Does it work for you?
Quoted 5 lines
The current
round_hint_to_min()code checks and adjusts just for the hint address, not for "len". Also, it returns no error.Do you mean adding "len" to the argument of
round_hint_to_min()and makinground_hint_to_min()return any error ?
I thought of just removing (hint != NULL) check in round_hint_to_min(), but in my rethinking I found that that affects other code and needs more changes, so your approach is simpler. I drop my suggestion.
The above check is to detect too big request, and in your reproducer len is small (4096), so I'm a bit confused :)
Although it doesn't fix your problem itself, it's correct,
Thanks,
Naoya Horiguchi
Quoted 49 lines
Regards,
Akira TakeuchiQuoted 45 lines
Thanks,
Naoya HoriguchiQuoted 42 lines
mm/mmap.c +4
-4@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);Signature · 5 lines
1.7.0.4 -- Akira Takeuchi <[email protected]>
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
--
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>Signature · 7 lines
Akira Takeuchi <[email protected]> -- 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>
--
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>
Re: [PATCH] mm: Ensure get_unmapped_area() returns higheraddressthan mmap_min_addr
Quoted 93 lines · On Wed, 23 Oct 2013 02:50:49 -0400 Naoya Horiguchi <[email protected]>…
Quoted 86 lines · Akira Takeuchi
Hi,
Quoted 75 lines · On Wed, 23 Oct 2013 00:26:05 -0400 Naoya Horiguchi <[email protected]>…
Hi,
Quoted 72 lines · Akira Takeuchi
This patch fixes the problem that
get_unmapped_area()can return illegal address and result in failing mmap(2) etc.In case that the address higher than
PAGE_SIZEis set to/proc/sys/vm/mmap_min_addr, the address lower thanmmap_min_addrcan be returned byget_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).This is because the current
get_unmapped_area()code does not take into accountmmap_min_addr.This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without
CAP_SYS_RAWIO, although any illegal parameter is not passed.2. The bottom-up search path after the top-down search might not work in
arch_get_unmapped_area_topdown().[How to reproduce]
--- test.c ------------------------------------------------- #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/errno.h> int main(int argc, char *argv[]) { void *ret = NULL, *last_map; size_t pagesize = sysconf(_SC_PAGESIZE); do { last_map = ret; ret = mmap(0, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); // printf("ret=%p\n", ret); } while (ret != MAP_FAILED); if (errno != ENOMEM) { printf("ERR: unexpected errno: %d (last map=%p)\n", errno, last_map); } return 0; } --------------------------------------------------------------- $ gcc -m32 -o test test.c $ sudo sysctl -w vm.mmap_min_addr=65536 vm.mmap_min_addr = 65536 $ ./test (run as non-priviledge user) ERR: unexpected errno: 1 (last map=0x10000)Signed-off-by Akira Takeuchi <[email protected]>Signed-off-by Kiyoshi Owada <[email protected]>--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)mm/mmap.c +1
-1@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;Quoted 5 lines
mm/mmap.c arch_get_unmapped_area(struct file *filp, unsigned long addr, · line 1862
Quoted 1 line
mm/mmap.c +0
-0if (flags &MAP_FIXED )I feel that it looks clearer to fix this in
round_hint_to_min(), with doingmmap_min_addrcheck in hint == NULL case. Does it work for you?Quoted 5 lines
The current
round_hint_to_min()code checks and adjusts just for the hint address, not for "len". Also, it returns no error.Do you mean adding "len" to the argument of
round_hint_to_min()and makinground_hint_to_min()return any error ?
Quoted 7 lines
I thought of just removing (hint != NULL) check in
round_hint_to_min(), but in my rethinking I found that that affects other code and needs more changes, so your approach is simpler. I drop my suggestion.The above check is to detect too big request, and in your reproducer len is small (4096), so I'm a bit confused :)
Although it doesn't fix your problem itself, it's correct,
I'll add a description as a "Note" to my v2 patch to avoid such confusing. The difference between v1 and v2 patch is only adding the "Note". Thank you for reviewing !
Regadrs,
Akira Takeuchi
Quoted 70 lines
Reviewed-by Naoya Horiguchi <[email protected]>Thanks,
Naoya HoriguchiQuoted 65 lines
Regards,
Akira TakeuchiQuoted 51 lines
Thanks,
Naoya HoriguchiQuoted 48 lines
mm/mmap.c +4
-4@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>Signature · 7 lines
Akira Takeuchi <[email protected]> -- 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>
Signature · 7 lines
Akira Takeuchi <[email protected]> -- 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>
PATCH v2 1/1 mm: Ensure get_unmapped_area() returns higher address than mmap_min_addr
In reply to Naoya HoriguchiThis patch fixes the problem that get_unmapped_area() can return illegal address and result in failing mmap(2) etc.
In case that the address higher than PAGE_SIZE is set to /proc/sys/vm/mmap_min_addr, the address lower than mmap_min_addr can be returned by get_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).
This is because the current get_unmapped_area() code does not take into account mmap_min_addr.
This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without CAP_SYS_RAWIO, although any illegal parameter is not passed.
2. The bottom-up search path after the top-down search might not work in arch_get_unmapped_area_topdown().
Note: The first and third chunk of my patch, which changes "len" check, are for more precise check using mmap_min_addr, and not for solving the above problem.
[How to reproduce]
--- test.c -------------------------------------------------
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/errno.h>
int main(int argc, char *argv[])
{
void *ret = NULL, *last_map;
size_t pagesize = sysconf(_SC_PAGESIZE);
do {
last_map = ret;
ret = mmap(0, pagesize, PROT_NONE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// printf("ret=%p\n", ret);
} while (ret != MAP_FAILED);
if (errno != ENOMEM) {
printf("ERR: unexpected errno: %d (last map=%p)\n",
errno, last_map);
}
return 0;
}
---------------------------------------------------------------
$ gcc -m32 -o test test.c
$ sudo sysctl -w vm.mmap_min_addr=65536
vm.mmap_min_addr = 65536
$ ./test (run as non-priviledge user)
ERR: unexpected errno: 1 (last map=0x10000)--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)
mm/mmap.c +5 -5
@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )
@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }
@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )
@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);
Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>
Re: [PATCH v2] mm: Ensure get_unmapped_area() returns higher addressthan mmap_min_addr
Quoted 58 lines · Akira Takeuchi
This patch fixes the problem that
get_unmapped_area()can return illegal address and result in failing mmap(2) etc.In case that the address higher than
PAGE_SIZEis set to/proc/sys/vm/mmap_min_addr, the address lower thanmmap_min_addrcan be returned byget_unmapped_area(), even if you do not pass any virtual address hint (i.e. the second argument).This is because the current
get_unmapped_area()code does not take into accountmmap_min_addr.This leads to two actual problems as follows:
1. mmap(2) can fail with EPERM on the process without
CAP_SYS_RAWIO, although any illegal parameter is not passed.2. The bottom-up search path after the top-down search might not work in
arch_get_unmapped_area_topdown().Note: The first and third chunk of my patch, which changes "len" check, are for more precise check using
mmap_min_addr, and not for solving the above problem.[How to reproduce]
--- test.c ------------------------------------------------- #include <stdio.h> #include <unistd.h> #include <sys/mman.h> #include <sys/errno.h> int main(int argc, char *argv[]) { void *ret = NULL, *last_map; size_t pagesize = sysconf(_SC_PAGESIZE); do { last_map = ret; ret = mmap(0, pagesize, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); // printf("ret=%p\n", ret); } while (ret != MAP_FAILED); if (errno != ENOMEM) { printf("ERR: unexpected errno: %d (last map=%p)\n", errno, last_map); } return 0; } --------------------------------------------------------------- $ gcc -m32 -o test test.c $ sudo sysctl -w vm.mmap_min_addr=65536 vm.mmap_min_addr = 65536 $ ./test (run as non-priviledge user) ERR: unexpected errno: 1 (last map=0x10000)
Quoted 3 lines
Signed-off-by Akira Takeuchi <[email protected]>Signed-off-by Kiyoshi Owada <[email protected]>Reviewed-by Naoya Horiguchi <[email protected]>
BTW, this bug seems to be very old, so I think this patch is worth going into all of currently living stable trees.
Thanks,
Naoya Horiguchi
Quoted 65 lines
--- mm/mmap.c | 10 +++++----- 1 files changed, 5 insertions(+), 5 deletions(-)mm/mmap.c +5
-5@@ -1856,7 +1856,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,struct vm_area_struct *vma;struct vm_unmapped_area_info info; -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1865,7 +1865,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; }@@ -1895,7 +1895,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,struct vm_unmapped_area_info info;/* requested length too big for entire address space */ -if (len >TASK_SIZE ) +if (len >TASK_SIZE - mmap_min_addr)return -ENOMEM ;if (flags &MAP_FIXED )@@ -1905,14 +1905,14 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,if (addr) { addr =PAGE_ALIGN (addr); vma =find_vma (mm, addr); -if (TASK_SIZE - len >= addr && +if (TASK_SIZE - len >= addr && addr >= mmap_min_addr && (!vma || addr + len <= vma->vm_start))return addr; } info.flags =VM_UNMAPPED_AREA_TOPDOWN ; info.length = len; - info.low_limit =PAGE_SIZE ; + info.low_limit =max (PAGE_SIZE , mmap_min_addr); info.high_limit = mm->mmap_base; info.align_mask =0 ; addr =vm_unmapped_area (&info);Signature · 11 lines
1.7.0.4 -- Akira Takeuchi <[email protected]> -- 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>