From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 76C55C4741F for ; Fri, 2 Oct 2020 11:09:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D0800206B8 for ; Fri, 2 Oct 2020 11:09:46 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="XKwOAUfL" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D0800206B8 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 11AD66B005D; Fri, 2 Oct 2020 07:09:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0CBE16B0062; Fri, 2 Oct 2020 07:09:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EFDF8900002; Fri, 2 Oct 2020 07:09:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0098.hostedemail.com [216.40.44.98]) by kanga.kvack.org (Postfix) with ESMTP id BF71B6B005D for ; Fri, 2 Oct 2020 07:09:45 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 4BE658249980 for ; Fri, 2 Oct 2020 11:09:45 +0000 (UTC) X-FDA: 77326715130.30.bear99_2511b2a271a3 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 29599180B3C85 for ; Fri, 2 Oct 2020 11:09:45 +0000 (UTC) X-HE-Tag: bear99_2511b2a271a3 X-Filterd-Recvd-Size: 10338 Received: from mail-lj1-f194.google.com (mail-lj1-f194.google.com [209.85.208.194]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Fri, 2 Oct 2020 11:09:44 +0000 (UTC) Received: by mail-lj1-f194.google.com with SMTP id v23so869303ljd.1 for ; Fri, 02 Oct 2020 04:09:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bzO9r/mm9kn9+8vQnJdM9eXg+PnaAIMWDsQ1sYGBPsQ=; b=XKwOAUfLseMj6x2Y3HCLiEV6YHJcdo3MTKuUAWuJUYQNf5t+iX6Umg8ZAmmCSX7pJu MaOG4WCvPHxchwYPsPNFQxaFNzbU9qHe0qhFbLDr4j/xbpRG3fMb4u3OFeIsbuwg+paH 1oGIgH8UK6VqvyBOjuyBmVW+3fi9Hy9tsiUej7HUXnYb3yKhvsAdnMzwtdpy/nILQQzw LEZyHCwLLcXHtorRA3WXYHXPQoxm8dLF4FyMJJm2OgNyLC5zVeS8WIDrIKm+oRnumHXY tpZ2CNw39d4n8fctnBZUWEhjZXurt93rPviZxOe7BbJevh2uukp+ob2sz7Xh3Vd8ZSkt SFzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=bzO9r/mm9kn9+8vQnJdM9eXg+PnaAIMWDsQ1sYGBPsQ=; b=mmGpNcUtJJXYAqfibKVe1bNNwlFclRtd7GELhtMb3UGw06Kz/wN/ZwsgwVLPXnKl6W WHx0VDivSGd97hTROLpHYJVErCCImR46WF4uWeoR+zr/i+HLcOYCVtAVEedXP0Hxz0Pn s6GEJXFY0SFXZ702r8BUUevCsRr1loQ/UEwFfVicLTvL2YNGW2dLrnAufQC8pytYm82E zwxeGn7Ufd65zplDKeFojJVOtxm2z5cVB9MUbes9xtICXRQdj6dmgwjJmc6x3ZwjsU0b 6aHk2m9HEOFzEcV2pUf3dVo5AqLFnz70bNfHL3hFfixHOT/cYb+BI3XT8bHSfXadt18A 8S5w== X-Gm-Message-State: AOAM531qC7U/lrWixeLhasCq3nWP05EixMjHOgu/9qAvSqevkDSr3dDN vtvVPw9A2vDcPvzhs8pZVOM= X-Google-Smtp-Source: ABdhPJw5fDNozd5g/lD0TKVxOkPdTnVb01sF/xdoUJC4qfmgtMfDktx61pfRVC2gU3JunvKLWoa37Q== X-Received: by 2002:a2e:3002:: with SMTP id w2mr528037ljw.244.1601636982971; Fri, 02 Oct 2020 04:09:42 -0700 (PDT) Received: from localhost.localdomain (88-114-211-119.elisa-laajakaista.fi. [88.114.211.119]) by smtp.gmail.com with ESMTPSA id m1sm232840lfh.99.2020.10.02.04.09.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2020 04:09:42 -0700 (PDT) From: Topi Miettinen To: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Topi Miettinen Subject: [PATCH v2] mm: Optional full ASLR for mmap() and mremap() Date: Fri, 2 Oct 2020 14:09:17 +0300 Message-Id: <20201002110917.4227-1-toiwoton@gmail.com> X-Mailer: git-send-email 2.28.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Writing a new value of 3 to /proc/sys/kernel/randomize_va_space enables full randomization of memory mappings created with mmap(NULL, ...). With 2, the base of the VMA used for such mappings is random, but the mappings are created in predictable places within the VMA and in sequential order. With 3, new VMAs are created to fully randomize the mappings. Also mremap(..., MREMAP_MAYMOVE) will move the mappings even if not necessary. On 32 bit systems this may cause problems due to increased VM fragmentation if the address space gets crowded. In this example, with value of 2, ld.so.cache, libc, an anonymous mmap and locale-archive are located close to each other: $ strace /bin/sync ... openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) =3D 3 fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D189096, ...}) =3D 0 mmap(NULL, 189096, PROT_READ, MAP_PRIVATE, 3, 0) =3D 0x7d9c1e7f2000 ... openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) =3D= 3 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0n\2\0\0\0\0\0"..= ., 832) =3D 832 fstat(3, {st_mode=3DS_IFREG|0755, st_size=3D1839792, ...}) =3D 0 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = =3D 0x7d9c1e7f0000 mmap(NULL, 1852680, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) =3D 0x7d9= c1e62b000 ... openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) =3D= 3 fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D5642592, ...}) =3D 0 mmap(NULL, 5642592, PROT_READ, MAP_PRIVATE, 3, 0) =3D 0x7d9c1e0c9000 With 3, they are located in unrelated addresses: $ echo 3 > /proc/sys/kernel/randomize_va_space $ /bin/sync ... openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) =3D 3 fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D189096, ...}) =3D 0 mmap(NULL, 189096, PROT_READ, MAP_PRIVATE, 3, 0) =3D 0xeda4fbea000 ... openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) =3D= 3 read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0n\2\0\0\0\0\0"..= ., 832) =3D 832 fstat(3, {st_mode=3DS_IFREG|0755, st_size=3D1839792, ...}) =3D 0 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = =3D 0xb8fb9c1d000 mmap(NULL, 1852680, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) =3D 0xaab= d8598000 ... openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) =3D= 3 fstat(3, {st_mode=3DS_IFREG|0644, st_size=3D5642592, ...}) =3D 0 mmap(NULL, 5642592, PROT_READ, MAP_PRIVATE, 3, 0) =3D 0xbe351ab8000 Signed-off-by: Topi Miettinen --- v2: also randomize mremap(..., MREMAP_MAYMOVE) --- Documentation/admin-guide/hw-vuln/spectre.rst | 6 +++--- Documentation/admin-guide/sysctl/kernel.rst | 11 +++++++++++ init/Kconfig | 2 +- mm/mmap.c | 7 ++++++- mm/mremap.c | 15 +++++++++++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Documentation/admin-guide/hw-vuln/spectre.rst b/Documentatio= n/admin-guide/hw-vuln/spectre.rst index e05e581af5cf..9ea250522077 100644 --- a/Documentation/admin-guide/hw-vuln/spectre.rst +++ b/Documentation/admin-guide/hw-vuln/spectre.rst @@ -254,7 +254,7 @@ Spectre variant 2 left by the previous process will also be cleared. =20 User programs should use address space randomization to make attacks - more difficult (Set /proc/sys/kernel/randomize_va_space =3D 1 or 2). + more difficult (Set /proc/sys/kernel/randomize_va_space =3D 1, 2 or 3= ). =20 3. A virtualized guest attacking the host ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -499,8 +499,8 @@ Spectre variant 2 more overhead and run slower. =20 User programs should use address space randomization - (/proc/sys/kernel/randomize_va_space =3D 1 or 2) to make attacks more - difficult. + (/proc/sys/kernel/randomize_va_space =3D 1, 2 or 3) to make attacks + more difficult. =20 3. VM mitigation ^^^^^^^^^^^^^^^^ diff --git a/Documentation/admin-guide/sysctl/kernel.rst b/Documentation/= admin-guide/sysctl/kernel.rst index d4b32cc32bb7..acd0612155d9 100644 --- a/Documentation/admin-guide/sysctl/kernel.rst +++ b/Documentation/admin-guide/sysctl/kernel.rst @@ -1060,6 +1060,17 @@ that support this feature. Systems with ancient and/or broken binaries should be configured with ``CONFIG_COMPAT_BRK`` enabled, which excludes the heap from pro= cess address space randomization. + +3 Additionally enable full randomization of memory mappings created + with mmap(NULL, ...). With 2, the base of the VMA used for such + mappings is random, but the mappings are created in predictable + places within the VMA and in sequential order. With 3, new VMAs + are created to fully randomize the mappings. Also mremap(..., + MREMAP_MAYMOVE) will move the mappings even if not necessary. + + On 32 bit systems this may cause problems due to increased VM + fragmentation if the address space gets crowded. + =3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D =20 =20 diff --git a/init/Kconfig b/init/Kconfig index d6a0b31b13dc..c5ea2e694f6a 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1859,7 +1859,7 @@ config COMPAT_BRK also breaks ancient binaries (including anything libc5 based). This option changes the bootup default to heap randomization disabled, and can be overridden at runtime by setting - /proc/sys/kernel/randomize_va_space to 2. + /proc/sys/kernel/randomize_va_space to 2 or 3. =20 On non-ancient distros (post-2000 ones) N is usually a safe choice. =20 diff --git a/mm/mmap.c b/mm/mmap.c index 40248d84ad5f..489368f43af1 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -47,6 +47,7 @@ #include #include #include +#include =20 #include #include @@ -206,7 +207,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk) #ifdef CONFIG_COMPAT_BRK /* * CONFIG_COMPAT_BRK can still be overridden by setting - * randomize_va_space to 2, which will still cause mm->start_brk + * randomize_va_space to >=3D 2, which will still cause mm->start_brk * to be arbitrarily shifted */ if (current->brk_randomized) @@ -1407,6 +1408,10 @@ unsigned long do_mmap(struct file *file, unsigned = long addr, if (mm->map_count > sysctl_max_map_count) return -ENOMEM; =20 + /* Pick a random address even outside current VMAs? */ + if (!addr && randomize_va_space >=3D 3) + addr =3D arch_mmap_rnd(); + /* Obtain the address to map to. we verify (or select) it and ensure * that it represents a valid section of the address space. */ diff --git a/mm/mremap.c b/mm/mremap.c index 138abbae4f75..c7fd1ab5fb5f 100644 --- a/mm/mremap.c +++ b/mm/mremap.c @@ -24,6 +24,7 @@ #include #include #include +#include =20 #include #include @@ -720,6 +721,20 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigne= d long, old_len, goto out; } =20 + if ((flags & MREMAP_MAYMOVE) && randomize_va_space >=3D 3) { + /* + * Caller is happy with a different address, so let's + * move even if not necessary! + */ + new_addr =3D arch_mmap_rnd(); + + ret =3D mremap_to(addr, old_len, new_addr, new_len, + &locked, flags, &uf, &uf_unmap_early, + &uf_unmap); + goto out; + } + + /* * Always allow a shrinking remap: that just unmaps * the unnecessary pages.. --=20 2.28.0