From patchwork Tue Sep 26 14:42:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Eric W. Biederman" X-Patchwork-Id: 13399293 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04A70E7E644 for ; Tue, 26 Sep 2023 14:43:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69CAA8D0031; Tue, 26 Sep 2023 10:43:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64D528D0002; Tue, 26 Sep 2023 10:43:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 514348D0031; Tue, 26 Sep 2023 10:43:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4270D8D0002 for ; Tue, 26 Sep 2023 10:43:14 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 09991C09BB for ; Tue, 26 Sep 2023 14:43:14 +0000 (UTC) X-FDA: 81279016308.10.A891B24 Received: from out02.mta.xmission.com (out02.mta.xmission.com [166.70.13.232]) by imf26.hostedemail.com (Postfix) with ESMTP id 9A66214000A for ; Tue, 26 Sep 2023 14:43:10 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf26.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.232 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695739391; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nkr1BtLtAC1rq7f3vv8w5ryDfQjJNEeSrIocqkwh9Ts=; b=1lizYvGESw+YFqET4YTWzVeUBsa3t9794yYU6xtTvztflRiw6DKXS0pz9EarpBbDGec2wh OiSigSlbD9ey+H7pc0PeZGveYiAS7AV94cAAK3sJOsBeA/EWtNGNPozhMMjeXnHq0QjsF1 +KE10wVsRumhmcxgvnk6RU7WuT9wmsQ= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=xmission.com; spf=pass (imf26.hostedemail.com: domain of ebiederm@xmission.com designates 166.70.13.232 as permitted sender) smtp.mailfrom=ebiederm@xmission.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695739391; a=rsa-sha256; cv=none; b=FJO4dJfIQmOW4Ovs7kkvpufMCC4Y0ZUa8Epq3IesYltOhl3iNcAiiuI76FNbbKX/fqeEOR wCjdQFHi5RGRoC15DRoU9z2DL+vmGlotMUhrgVpHr+kXFWACCAwg6NcBqd+m9x3FicjnAe NpXMA1vROQiGHsbVBK019XwHDtuVlHc= Received: from in02.mta.xmission.com ([166.70.13.52]:34354) by out02.mta.xmission.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1ql9HI-001UMK-Fa; Tue, 26 Sep 2023 08:43:08 -0600 Received: from ip68-227-168-167.om.om.cox.net ([68.227.168.167]:53274 helo=email.froward.int.ebiederm.org.xmission.com) by in02.mta.xmission.com with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1ql9HH-00AmvZ-6o; Tue, 26 Sep 2023 08:43:08 -0600 From: "Eric W. Biederman" To: Dan Carpenter Cc: oe-kbuild@lists.linux.dev, Sebastian Ott , lkp@intel.com, oe-kbuild-all@lists.linux.dev, Thomas =?utf-8?q?Wei=C3=9Fs?= =?utf-8?q?chuh?= , Alexander Viro , Christian Brauner , Kees Cook , Mark Brown , Willy Tarreau , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <60c7fdeb-da32-4d21-9e34-368050224d6c@kadam.mountain> Date: Tue, 26 Sep 2023 09:42:44 -0500 In-Reply-To: <60c7fdeb-da32-4d21-9e34-368050224d6c@kadam.mountain> (Dan Carpenter's message of "Tue, 26 Sep 2023 16:49:57 +0300") Message-ID: <87sf71f123.fsf@email.froward.int.ebiederm.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-XM-SPF: eid=1ql9HH-00AmvZ-6o;;;mid=<87sf71f123.fsf@email.froward.int.ebiederm.org>;;;hst=in02.mta.xmission.com;;;ip=68.227.168.167;;;frm=ebiederm@xmission.com;;;spf=pass X-XM-AID: U2FsdGVkX1/Hj+tqfYmK0zrPxO+Sjh4Gz9DbfrD3sFA= X-SA-Exim-Connect-IP: 68.227.168.167 X-SA-Exim-Mail-From: ebiederm@xmission.com Subject: [PATCH v2] binfmt_elf: Support segments with 0 filesz and misaligned starts X-SA-Exim-Version: 4.2.1 (built Sat, 08 Feb 2020 21:53:50 +0000) X-SA-Exim-Scanned: Yes (on in02.mta.xmission.com) X-Rspam-User: X-Stat-Signature: 7jp6iy8naipfsuh5yuszygf9661wtbn3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 9A66214000A X-HE-Tag: 1695739390-982011 X-HE-Meta: U2FsdGVkX1/UFsbJsnqZcgbFZ46EHeJADHAKDNU79OMwV57jvwBQVRK0CMmuJOki7UB9IWNqxFgvC/TY7VPhRvYpU736RV3vKJQjq/A6aTM42+LTbZ7sxlZUY4vDZ+Jp+H3thvLMdJ3ZPPURSpegGDFT6f1ZVXVx+0mwtjt+Z9r+0za7LUx7U+wZZg1icmX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xizFqVM4GMC0H2BjRl1h8EUTMRn+7LoNh6FmmcPqGXBNF+X3wpYZ9d02eqF7lcic2rJJtGHLjBoC9KC7Vr6LySlBRYVRZZgQpwUXIBrAO2RhYUke+PwnFZX 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: Implement a helper elf_load that wraps elf_map and performs all of the necessary work to ensure that when "memsz > filesz" the bytes described by "memsz > filesz" are zeroed. Link: https://lkml.kernel.org/r/20230914-bss-alloc-v1-1-78de67d2c6dd@weissschuh.net Reported-by: Sebastian Ott Reported-by: Thomas Weißschuh Signed-off-by: "Eric W. Biederman" --- fs/binfmt_elf.c | 111 +++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 63 deletions(-) Dan Carpenter writes: > smatch warnings: > fs/binfmt_elf.c:431 elf_load() error: uninitialized symbol 'map_addr'. That was embarrassing. I have added an initialization of map_error in the p_filesz == 0 case. I am a bit surprised this worked without map_error being set in testing, but it looks like any "value < TASK_SIZE" would work. So I guess odds are about 50/50 that any random stack value would not cause load_elf_binary to fail. Thanks for pointing that out. Eric diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 7b3d2d491407..2a615f476e44 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -110,25 +110,6 @@ static struct linux_binfmt elf_format = { #define BAD_ADDR(x) (unlikely((unsigned long)(x) >= TASK_SIZE)) -static int set_brk(unsigned long start, unsigned long end, int prot) -{ - start = ELF_PAGEALIGN(start); - end = ELF_PAGEALIGN(end); - if (end > start) { - /* - * Map the last of the bss segment. - * If the header is requesting these pages to be - * executable, honour that (ppc32 needs this). - */ - int error = vm_brk_flags(start, end - start, - prot & PROT_EXEC ? VM_EXEC : 0); - if (error) - return error; - } - current->mm->start_brk = current->mm->brk = end; - return 0; -} - /* We need to explicitly zero any fractional pages after the data section (i.e. bss). This would contain the junk from the file that should not @@ -406,6 +387,51 @@ static unsigned long elf_map(struct file *filep, unsigned long addr, return(map_addr); } +static unsigned long elf_load(struct file *filep, unsigned long addr, + const struct elf_phdr *eppnt, int prot, int type, + unsigned long total_size) +{ + unsigned long zero_start, zero_end; + unsigned long map_addr; + + if (eppnt->p_filesz) { + map_addr = elf_map(filep, addr, eppnt, prot, type, total_size); + if (BAD_ADDR(map_addr)) + return map_addr; + if (eppnt->p_memsz > eppnt->p_filesz) { + zero_start = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) + + eppnt->p_filesz; + zero_end = map_addr + ELF_PAGEOFFSET(eppnt->p_vaddr) + + eppnt->p_memsz; + + /* Zero the end of the last mapped page */ + padzero(zero_start); + } + } else { + map_addr = zero_start = ELF_PAGESTART(addr); + zero_end = zero_start + ELF_PAGEOFFSET(eppnt->p_vaddr) + + eppnt->p_memsz; + } + if (eppnt->p_memsz > eppnt->p_filesz) { + /* + * Map the last of the segment. + * If the header is requesting these pages to be + * executable, honour that (ppc32 needs this). + */ + int error; + + zero_start = ELF_PAGEALIGN(zero_start); + zero_end = ELF_PAGEALIGN(zero_end); + + error = vm_brk_flags(zero_start, zero_end - zero_start, + prot & PROT_EXEC ? VM_EXEC : 0); + if (error) + map_addr = error; + } + return map_addr; +} + + static unsigned long total_mapping_size(const struct elf_phdr *phdr, int nr) { elf_addr_t min_addr = -1; @@ -829,7 +855,6 @@ static int load_elf_binary(struct linux_binprm *bprm) struct elf_phdr *elf_ppnt, *elf_phdata, *interp_elf_phdata = NULL; struct elf_phdr *elf_property_phdata = NULL; unsigned long elf_bss, elf_brk; - int bss_prot = 0; int retval, i; unsigned long elf_entry; unsigned long e_entry; @@ -1040,33 +1065,6 @@ static int load_elf_binary(struct linux_binprm *bprm) if (elf_ppnt->p_type != PT_LOAD) continue; - if (unlikely (elf_brk > elf_bss)) { - unsigned long nbyte; - - /* There was a PT_LOAD segment with p_memsz > p_filesz - before this one. Map anonymous pages, if needed, - and clear the area. */ - retval = set_brk(elf_bss + load_bias, - elf_brk + load_bias, - bss_prot); - if (retval) - goto out_free_dentry; - nbyte = ELF_PAGEOFFSET(elf_bss); - if (nbyte) { - nbyte = ELF_MIN_ALIGN - nbyte; - if (nbyte > elf_brk - elf_bss) - nbyte = elf_brk - elf_bss; - if (clear_user((void __user *)elf_bss + - load_bias, nbyte)) { - /* - * This bss-zeroing can fail if the ELF - * file specifies odd protections. So - * we don't check the return value - */ - } - } - } - elf_prot = make_prot(elf_ppnt->p_flags, &arch_state, !!interpreter, false); @@ -1162,7 +1160,7 @@ static int load_elf_binary(struct linux_binprm *bprm) } } - error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt, + error = elf_load(bprm->file, load_bias + vaddr, elf_ppnt, elf_prot, elf_flags, total_size); if (BAD_ADDR(error)) { retval = IS_ERR_VALUE(error) ? @@ -1217,10 +1215,8 @@ static int load_elf_binary(struct linux_binprm *bprm) if (end_data < k) end_data = k; k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz; - if (k > elf_brk) { - bss_prot = elf_prot; + if (k > elf_brk) elf_brk = k; - } } e_entry = elf_ex->e_entry + load_bias; @@ -1232,18 +1228,7 @@ static int load_elf_binary(struct linux_binprm *bprm) start_data += load_bias; end_data += load_bias; - /* Calling set_brk effectively mmaps the pages that we need - * for the bss and break sections. We must do this before - * mapping in the interpreter, to make sure it doesn't wind - * up getting placed where the bss needs to go. - */ - retval = set_brk(elf_bss, elf_brk, bss_prot); - if (retval) - goto out_free_dentry; - if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) { - retval = -EFAULT; /* Nobody gets to see this, but.. */ - goto out_free_dentry; - } + current->mm->start_brk = current->mm->brk = ELF_PAGEALIGN(elf_brk); if (interpreter) { elf_entry = load_elf_interp(interp_elf_ex,