linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	stable@vger.kernel.org, Kees Cook <keescook@chromium.org>,
	Ali Saidi <alisaidi@amazon.com>,
	Guenter Roeck <linux@roeck-us.net>,
	Michal Hocko <mhocko@suse.com>,
	Matthew Wilcox <willy@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Jann Horn <jannh@google.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Frank van der Linden <fllinden@amazon.com>
Subject: [PATCH 4.19 77/79] binfmt_elf: move brk out of mmap when doing direct loader exec
Date: Fri, 20 Sep 2019 00:04:02 +0200	[thread overview]
Message-ID: <20190919214814.582627738@linuxfoundation.org> (raw)
In-Reply-To: <20190919214807.612593061@linuxfoundation.org>

From: Kees Cook <keescook@chromium.org>

commit bbdc6076d2e5d07db44e74c11b01a3e27ab90b32 upstream.

Commmit eab09532d400 ("binfmt_elf: use ELF_ET_DYN_BASE only for PIE"),
made changes in the rare case when the ELF loader was directly invoked
(e.g to set a non-inheritable LD_LIBRARY_PATH, testing new versions of
the loader), by moving into the mmap region to avoid both ET_EXEC and
PIE binaries.  This had the effect of also moving the brk region into
mmap, which could lead to the stack and brk being arbitrarily close to
each other.  An unlucky process wouldn't get its requested stack size
and stack allocations could end up scribbling on the heap.

This is illustrated here.  In the case of using the loader directly, brk
(so helpfully identified as "[heap]") is allocated with the _loader_ not
the binary.  For example, with ASLR entirely disabled, you can see this
more clearly:

$ /bin/cat /proc/self/maps
555555554000-55555555c000 r-xp 00000000 ... /bin/cat
55555575b000-55555575c000 r--p 00007000 ... /bin/cat
55555575c000-55555575d000 rw-p 00008000 ... /bin/cat
55555575d000-55555577e000 rw-p 00000000 ... [heap]
...
7ffff7ff7000-7ffff7ffa000 r--p 00000000 ... [vvar]
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 ... [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00027000 ... /lib/x86_64-linux-gnu/ld-2.27.so
7ffff7ffd000-7ffff7ffe000 rw-p 00028000 ... /lib/x86_64-linux-gnu/ld-2.27.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 ...
7ffffffde000-7ffffffff000 rw-p 00000000 ... [stack]

$ /lib/x86_64-linux-gnu/ld-2.27.so /bin/cat /proc/self/maps
...
7ffff7bcc000-7ffff7bd4000 r-xp 00000000 ... /bin/cat
7ffff7bd4000-7ffff7dd3000 ---p 00008000 ... /bin/cat
7ffff7dd3000-7ffff7dd4000 r--p 00007000 ... /bin/cat
7ffff7dd4000-7ffff7dd5000 rw-p 00008000 ... /bin/cat
7ffff7dd5000-7ffff7dfc000 r-xp 00000000 ... /lib/x86_64-linux-gnu/ld-2.27.so
7ffff7fb2000-7ffff7fd6000 rw-p 00000000 ...
7ffff7ff7000-7ffff7ffa000 r--p 00000000 ... [vvar]
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 ... [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00027000 ... /lib/x86_64-linux-gnu/ld-2.27.so
7ffff7ffd000-7ffff7ffe000 rw-p 00028000 ... /lib/x86_64-linux-gnu/ld-2.27.so
7ffff7ffe000-7ffff8020000 rw-p 00000000 ... [heap]
7ffffffde000-7ffffffff000 rw-p 00000000 ... [stack]

The solution is to move brk out of mmap and into ELF_ET_DYN_BASE since
nothing is there in the direct loader case (and ET_EXEC is still far
away at 0x400000).  Anything that ran before should still work (i.e.
the ultimately-launched binary already had the brk very far from its
text, so this should be no different from a COMPAT_BRK standpoint).  The
only risk I see here is that if someone started to suddenly depend on
the entire memory space lower than the mmap region being available when
launching binaries via a direct loader execs which seems highly
unlikely, I'd hope: this would mean a binary would _not_ work when
exec()ed normally.

(Note that this is only done under CONFIG_ARCH_HAS_ELF_RANDOMIZATION
when randomization is turned on.)

Link: http://lkml.kernel.org/r/20190422225727.GA21011@beast
Link: https://lkml.kernel.org/r/CAGXu5jJ5sj3emOT2QPxQkNQk0qbU6zEfu9=Omfhx_p0nCKPSjA@mail.gmail.com
Fixes: eab09532d400 ("binfmt_elf: use ELF_ET_DYN_BASE only for PIE")
Signed-off-by: Kees Cook <keescook@chromium.org>
Reported-by: Ali Saidi <alisaidi@amazon.com>
Cc: Ali Saidi <alisaidi@amazon.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Jann Horn <jannh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Frank van der Linden <fllinden@amazon.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

---
 fs/binfmt_elf.c |   11 +++++++++++
 1 file changed, 11 insertions(+)

--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -1137,6 +1137,17 @@ static int load_elf_binary(struct linux_
 	current->mm->start_stack = bprm->p;
 
 	if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
+		/*
+		 * For architectures with ELF randomization, when executing
+		 * a loader directly (i.e. no interpreter listed in ELF
+		 * headers), move the brk area out of the mmap region
+		 * (since it grows up, and may collide early with the stack
+		 * growing down), and into the unused ELF_ET_DYN_BASE region.
+		 */
+		if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) && !interpreter)
+			current->mm->brk = current->mm->start_brk =
+				ELF_ET_DYN_BASE;
+
 		current->mm->brk = current->mm->start_brk =
 			arch_randomize_brk(current->mm);
 #ifdef compat_brk_randomized



  parent reply	other threads:[~2019-09-19 22:30 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-19 22:02 [PATCH 4.19 00/79] 4.19.75-stable review Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 01/79] netfilter: nf_flow_table: set default timeout after successful insertion Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 02/79] HID: wacom: generic: read HID_DG_CONTACTMAX from any feature report Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 03/79] RDMA/restrack: Release task struct which was hold by CM_ID object Greg Kroah-Hartman
2019-09-21 20:22   ` Pavel Machek
2019-09-22  8:30     ` Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 04/79] Input: elan_i2c - remove Lenovo Legion Y7000 PnpID Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 05/79] powerpc/mm/radix: Use the right page size for vmemmap mapping Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 06/79] USB: usbcore: Fix slab-out-of-bounds bug during device reset Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 07/79] media: tm6000: double free if usb disconnect while streaming Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 08/79] phy: renesas: rcar-gen3-usb2: Disable clearing VBUS in over-current Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 09/79] ip6_gre: fix a dst leak in ip6erspan_tunnel_xmit Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 10/79] udp: correct reuseport selection with connected sockets Greg Kroah-Hartman
2019-09-21 20:32   ` Pavel Machek
2019-09-19 22:02 ` [PATCH 4.19 11/79] xen-netfront: do not assume sk_buff_head list is empty in error handling Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 12/79] net_sched: let qdisc_put() accept NULL pointer Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 13/79] KVM: coalesced_mmio: add bounds checking Greg Kroah-Hartman
2019-09-19 22:02 ` [PATCH 4.19 14/79] firmware: google: check if size is valid when decoding VPD data Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 15/79] serial: sprd: correct the wrong sequence of arguments Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 16/79] tty/serial: atmel: reschedule TX after RX was started Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 17/79] mwifiex: Fix three heap overflow at parsing element in cfg80211_ap_settings Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 18/79] nl80211: Fix possible Spectre-v1 for CQM RSSI thresholds Greg Kroah-Hartman
2019-09-20 11:28   ` Pavel Machek
2019-09-19 22:03 ` [PATCH 4.19 19/79] ieee802154: hwsim: Fix error handle path in hwsim_init_module Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 20/79] ieee802154: hwsim: unregister hw while hwsim_subscribe_all_others fails Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 21/79] ARM: dts: am57xx: Disable voltage switching for SD card Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 22/79] ARM: OMAP2+: Fix missing SYSC_HAS_RESET_STATUS for dra7 epwmss Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 23/79] bus: ti-sysc: Fix using configured sysc mask value Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 24/79] s390/bpf: fix lcgr instruction encoding Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 25/79] ARM: OMAP2+: Fix omap4 errata warning on other SoCs Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 26/79] ARM: dts: dra74x: Fix iodelay configuration for mmc3 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 27/79] ARM: OMAP1: ams-delta-fiq: Fix missing irq_ack Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 28/79] bus: ti-sysc: Simplify cleanup upon failures in sysc_probe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 29/79] s390/bpf: use 32-bit index for tail calls Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 30/79] selftests/bpf: fix "bind{4, 6} deny specific IP & port" on s390 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 31/79] tools: bpftool: close prog FD before exit on showing a single program Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 32/79] fpga: altera-ps-spi: Fix getting of optional confd gpio Greg Kroah-Hartman
2019-09-21 20:46   ` Pavel Machek
2019-09-23  1:33     ` Phil Reid
2019-09-19 22:03 ` [PATCH 4.19 33/79] netfilter: ebtables: Fix argument order to ADD_COUNTER Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 34/79] netfilter: nft_flow_offload: missing netlink attribute policy Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 35/79] netfilter: xt_nfacct: Fix alignment mismatch in xt_nfacct_match_info Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 36/79] NFSv4: Fix return values for nfs4_file_open() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 37/79] NFSv4: Fix return value in nfs_finish_open() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 38/79] NFS: Fix initialisation of I/O result struct in nfs_pgio_rpcsetup Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 39/79] Kconfig: Fix the reference to the IDT77105 Phy driver in the description of ATM_NICSTAR_USE_IDT77105 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 40/79] xdp: unpin xdp umem pages in error path Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 41/79] qed: Add cleanup in qed_slowpath_start() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 42/79] ARM: 8874/1: mm: only adjust sections of valid mm structures Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 43/79] batman-adv: Only read OGM2 tvlv_len after buffer len check Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 44/79] bpf: allow narrow loads of some sk_reuseport_md fields with offset > 0 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 45/79] r8152: Set memory to all 0xFFs on failed reg reads Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 46/79] x86/apic: Fix arch_dynirq_lower_bound() bug for DT enabled machines Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 47/79] netfilter: xt_physdev: Fix spurious error message in physdev_mt_check Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 48/79] netfilter: nf_conntrack_ftp: Fix debug output Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 49/79] NFSv2: Fix eof handling Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 50/79] NFSv2: Fix write regression Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 51/79] kallsyms: Dont let kallsyms_lookup_size_offset() fail on retrieving the first symbol Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 52/79] cifs: set domainName when a domain-key is used in multiuser Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 53/79] cifs: Use kzfree() to zero out the password Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 54/79] usb: host: xhci-tegra: Set DMA mask correctly Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 55/79] ARM: 8901/1: add a criteria for pfn_valid of arm Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 56/79] ibmvnic: Do not process reset during or after device removal Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 57/79] sky2: Disable MSI on yet another ASUS boards (P6Xxxx) Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 58/79] i2c: designware: Synchronize IRQs when unregistering slave client Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 59/79] perf/x86/intel: Restrict period on Nehalem Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 60/79] perf/x86/amd/ibs: Fix sample bias for dispatched micro-ops Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 61/79] amd-xgbe: Fix error path in xgbe_mod_init() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 62/79] tools/power x86_energy_perf_policy: Fix "uninitialized variable" warnings at -O2 Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 63/79] tools/power x86_energy_perf_policy: Fix argument parsing Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 64/79] tools/power turbostat: fix buffer overrun Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 65/79] net: aquantia: fix out of memory condition on rx side Greg Kroah-Hartman
2019-09-20 11:20   ` Pavel Machek
2019-09-19 22:03 ` [PATCH 4.19 66/79] net: seeq: Fix the function used to release some memory in an error handling path Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 67/79] dmaengine: ti: dma-crossbar: Fix a memory leak bug Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 68/79] dmaengine: ti: omap-dma: Add cleanup in omap_dma_probe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 69/79] x86/uaccess: Dont leak the AC flags into __get_user() argument evaluation Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 70/79] x86/hyper-v: Fix overflow bug in fill_gva_list() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 71/79] keys: Fix missing null pointer check in request_key_auth_describe() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 72/79] iommu/amd: Flush old domains in kdump kernel Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 73/79] iommu/amd: Fix race in increase_address_space() Greg Kroah-Hartman
2019-09-19 22:03 ` [PATCH 4.19 74/79] PCI: kirin: Fix section mismatch warning Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 75/79] ovl: fix regression caused by overlapping layers detection Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 76/79] floppy: fix usercopy direction Greg Kroah-Hartman
2019-09-19 22:04 ` Greg Kroah-Hartman [this message]
2019-09-19 22:04 ` [PATCH 4.19 78/79] arm64: kpti: Whitelist Cortex-A CPUs that dont implement the CSV3 field Greg Kroah-Hartman
2019-09-19 22:04 ` [PATCH 4.19 79/79] media: technisat-usb2: break out of loop at end of buffer Greg Kroah-Hartman
2019-09-20  3:39 ` [PATCH 4.19 00/79] 4.19.75-stable review kernelci.org bot
2019-09-20  8:51 ` Naresh Kamboju
2019-09-20 13:48 ` Jon Hunter
2019-09-20 18:37 ` Guenter Roeck
2019-09-20 21:21 ` shuah

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190919214814.582627738@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=akpm@linux-foundation.org \
    --cc=alisaidi@amazon.com \
    --cc=fllinden@amazon.com \
    --cc=jannh@google.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mhocko@suse.com \
    --cc=stable@vger.kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).