linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH v2 0/2] Randomization of address chosen by mmap.
@ 2018-03-22 16:36 Ilya Smith
  2018-03-22 16:36 ` [RFC PATCH v2 1/2] " Ilya Smith
                   ` (4 more replies)
  0 siblings, 5 replies; 39+ messages in thread
From: Ilya Smith @ 2018-03-22 16:36 UTC (permalink / raw)
  To: rth, ink, mattst88, vgupta, linux, tony.luck, fenghua.yu, jhogan,
	ralf, jejb, deller, benh, paulus, mpe, schwidefsky,
	heiko.carstens, ysato, dalias, davem, tglx, mingo, hpa, x86, nyc,
	viro, arnd, blackzert, gregkh, deepa.kernel, mhocko, hughd,
	kstewart, pombredanne, akpm, steve.capper, punit.agrawal,
	paul.burton, aneesh.kumar, npiggin, keescook, bhsharma, riel,
	nitin.m.gupta, kirill.shutemov, dan.j.williams, jack,
	ross.zwisler, jglisse, willy, aarcange, oleg, linux-alpha,
	linux-kernel, linux-snps-arc, linux-arm-kernel, linux-ia64,
	linux-metag, linux-mips, linux-parisc, linuxppc-dev, linux-s390,
	linux-sh, sparclinux, linux-mm

Current implementation doesn't randomize address returned by mmap.
All the entropy ends with choosing mmap_base_addr at the process
creation. After that mmap build very predictable layout of address
space. It allows to bypass ASLR in many cases. This patch make
randomization of address on any mmap call.

---
v2: Changed the way how gap was chosen. Now we don't get all possible
gaps. Random address generated and used as a tree walking direction.
Tree walked with backtracking till suitable gap will be found.
When the gap was found, address randomly shifted from next vma start.

The vm_unmapped_area_info structure was extended with new field random_shift
what might be used to set arch-depended limit on shift to next vma start.
In case of x86-64 architecture this shift is 256 pages for 32 bit applications
and 0x1000000 pages for 64 bit.

To get the entropy pseudo-random is used. This is because on Intel x86-64
processors instruction RDRAND works very slow if buffer is consumed -
after about 10000 iterations.

This feature could be enabled by setting randomize_va_space with 4.

---
Performance:
After applying this patch single mmap took about 7% longer according to
following test:

    before = rdtsc();
    addr = mmap(0, SIZE, PROT_READ | PROT_WRITE, 
                 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
    after = rdtsc();
    diff = after - before;
    munmap(addr, SIZE)
    ...
    unsigned long long total = 0;
    for(int i = 0; i < count; ++i) {
        total += one_iteration();
    }
    printf("%lld\n", total);

Time is consumed by div instruction in computation of the address.

make kernel:
echo 2 > /proc/sys/kernel/randomize_va_space 
make mrproper && make defconfig && time make 
real    11m9.925s
user    10m17.829s
sys 1m4.969s

echo 4 > /proc/sys/kernel/randomize_va_space 
make mrproper && make defconfig && time make 
real    11m12.806s
user    10m18.305s
sys 1m4.281s


Ilya Smith (2):
  Randomization of address chosen by mmap.
  Architecture defined limit on memory region random shift.

 arch/alpha/kernel/osf_sys.c         |   1 +
 arch/arc/mm/mmap.c                  |   1 +
 arch/arm/mm/mmap.c                  |   2 +
 arch/frv/mm/elf-fdpic.c             |   1 +
 arch/ia64/kernel/sys_ia64.c         |   1 +
 arch/ia64/mm/hugetlbpage.c          |   1 +
 arch/metag/mm/hugetlbpage.c         |   1 +
 arch/mips/mm/mmap.c                 |   1 +
 arch/parisc/kernel/sys_parisc.c     |   2 +
 arch/powerpc/mm/hugetlbpage-radix.c |   1 +
 arch/powerpc/mm/mmap.c              |   2 +
 arch/powerpc/mm/slice.c             |   2 +
 arch/s390/mm/mmap.c                 |   2 +
 arch/sh/mm/mmap.c                   |   2 +
 arch/sparc/kernel/sys_sparc_32.c    |   1 +
 arch/sparc/kernel/sys_sparc_64.c    |   2 +
 arch/sparc/mm/hugetlbpage.c         |   2 +
 arch/tile/mm/hugetlbpage.c          |   2 +
 arch/x86/kernel/sys_x86_64.c        |   4 +
 arch/x86/mm/hugetlbpage.c           |   4 +
 fs/hugetlbfs/inode.c                |   1 +
 include/linux/mm.h                  |  17 ++--
 mm/mmap.c                           | 165 ++++++++++++++++++++++++++++++++++++
 23 files changed, 213 insertions(+), 5 deletions(-)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2018-04-03  0:11 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-22 16:36 [RFC PATCH v2 0/2] Randomization of address chosen by mmap Ilya Smith
2018-03-22 16:36 ` [RFC PATCH v2 1/2] " Ilya Smith
2018-03-22 20:53   ` Andrew Morton
2018-03-23 17:43     ` Ilya Smith
2018-03-22 16:36 ` [RFC PATCH v2 2/2] Architecture defined limit on memory region random shift Ilya Smith
2018-03-22 20:54   ` Andrew Morton
2018-03-23 17:48     ` Ilya Smith
2018-03-23 17:49     ` Ilya Smith
2018-03-22 20:57 ` [RFC PATCH v2 0/2] Randomization of address chosen by mmap Andrew Morton
2018-03-23 17:25   ` Ilya Smith
2018-03-23 12:48 ` Matthew Wilcox
2018-03-23 17:55   ` Ilya Smith
2018-03-26  8:46     ` Michal Hocko
2018-03-26 19:45       ` Ilya Smith
2018-03-27  7:24         ` Michal Hocko
2018-03-27 13:51           ` Ilya Smith
2018-03-27 14:38             ` Michal Hocko
2018-03-28 18:47               ` Ilya Smith
2018-03-27 22:16             ` Theodore Y. Ts'o
2018-03-27 23:58               ` Rich Felker
2018-03-28 18:48               ` Ilya Smith
2018-03-27 22:53             ` Kees Cook
2018-03-27 23:49               ` Matthew Wilcox
2018-03-27 23:57                 ` Kees Cook
2018-03-28  0:00                 ` Rich Felker
2018-03-28 21:07                   ` Luck, Tony
2018-04-03  0:11                     ` Ilya Smith
2018-03-28 21:07                 ` Ilya Smith
2018-03-23 18:00   ` Rich Felker
2018-03-23 19:06     ` Matthew Wilcox
2018-03-23 19:16       ` Rich Felker
2018-03-23 19:29         ` Matthew Wilcox
2018-03-23 19:35           ` Rich Felker
2018-03-28  4:50       ` Rob Landley
2018-03-30  7:55 ` Pavel Machek
2018-03-30  9:07   ` Ilya Smith
2018-03-30  9:57     ` Pavel Machek
2018-03-30 11:10       ` Ilya Smith
2018-03-30 13:33   ` Rich Felker

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).