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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id ED829C433FE for ; Fri, 11 Nov 2022 20:27:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 571E58E000F; Fri, 11 Nov 2022 15:27:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FB658E0008; Fri, 11 Nov 2022 15:27:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3749D8E000F; Fri, 11 Nov 2022 15:27:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 253C48E0008 for ; Fri, 11 Nov 2022 15:27:41 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F119840D24 for ; Fri, 11 Nov 2022 20:27:40 +0000 (UTC) X-FDA: 80122297080.15.663F194 Received: from mail-pg1-f178.google.com (mail-pg1-f178.google.com [209.85.215.178]) by imf23.hostedemail.com (Postfix) with ESMTP id 8DCB8140005 for ; Fri, 11 Nov 2022 20:27:40 +0000 (UTC) Received: by mail-pg1-f178.google.com with SMTP id r18so5208744pgr.12 for ; Fri, 11 Nov 2022 12:27:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=VenKawNdI6vht+/5rGH39TnWAPmNSDWBqV+Ma0YasTE=; b=qXOnXYoTn4MSk0EEKJLu2DIgcOrdHYPGnG+66FDnWf4OaNHtkc86/ZqlshCEMMCMtd OZe+obHgSI5aGVhVhTMCJ/lVvo1UFUcE7L2BLR86uoK6XFXVekh+1NAB8cN7s4jRseTm S1mkUrP+IUQ237XG/8l+Bf/TgR/0E8xXhhCyE9nQhqnehDZo2g6jHd/U7XvyzyWN1J6g aiOjtL7Ho0/UAvsz0U6JuBD+tMmZ48xM91UO5SBuGphPW2kD2mWCDSRCTA0HBQc7FP50 dlkr6wGL28un/msWyOjl1ktz2CYnWNx8nNo2b+qCejCD9dAaGo8TwAOwr9avK/138r1f UvXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=VenKawNdI6vht+/5rGH39TnWAPmNSDWBqV+Ma0YasTE=; b=HtO/bYSw/WeCVodh5KVnZRag7ZZyoqbBaTjG3NQKUzAWNn0I3ScFp31t+liSMZb9uN yYPO0l0LIy4jCVuE+Wg/Z3kiZ/hoBbmKQ94n/IIeIX/7Akx8ukXgEY8XDxFYQNG3ClZL 86naMOds67olZK9Znr2ofaNcpuUX9JTRQT/yoSBOQCw0DiEoVWQM4I1kzRAKx0wmsW1J uF06y3p8O0az8dDaYdwyZgBwuZSCe5aTCbZFqqrr7OHu4l9RbdnUpgkpgzKCwjCPYHaV D21RTqWd+jajbqLF6J8ZQhn2UG1EyTFKlQFsNQqA/Dq2M020toAcu7LYMKIJ2tK07ciz fJyA== X-Gm-Message-State: ANoB5pkJY+hwRv3JLEcFqZWHv3iNEPhcar7bNPqD7nsxmgBcS85Es9KO qSFwTR1/Cwi1u/uhSUu4OuWI4A== X-Google-Smtp-Source: AA0mqf7p+q950DoxxPAxQxTUsalRqJn9q8r0sFdVKzATt8Xe3HgzH38KcItMCpO/f3fIGxvOkGA0xg== X-Received: by 2002:a63:4e53:0:b0:434:aa69:bba2 with SMTP id o19-20020a634e53000000b00434aa69bba2mr2910064pgl.567.1668198459215; Fri, 11 Nov 2022 12:27:39 -0800 (PST) Received: from google.com ([2620:15c:2ce:200:8b77:5448:ea74:27a]) by smtp.gmail.com with ESMTPSA id c2-20020a17090a108200b002137d3da760sm5251776pja.39.2022.11.11.12.27.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 12:27:38 -0800 (PST) Date: Fri, 11 Nov 2022 12:27:34 -0800 From: Fangrui Song To: Kees Cook Cc: Pedro Falcato , Rich Felker , Alexander Viro , Eric Biederman , sam@gentoo.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH v2] binfmt_elf: Allow .bss in any interp PT_LOAD Message-ID: <20221111202734.m5gk6vr4e5zd25lk@google.com> References: <20221111061315.gonna.703-kees@kernel.org> <20221111074234.xm5a6ota7ppdsto5@google.com> <202211111211.93ED8B4B@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <202211111211.93ED8B4B@keescook> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668198460; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VenKawNdI6vht+/5rGH39TnWAPmNSDWBqV+Ma0YasTE=; b=EKB+j7TpYvZNQCqQ193NRYdYyE3L2+CwvzQbq5KbapMEjkMuXzf8aad1tcyyzDiaBhT0Rd SyQJZxH/asMzajVGZTp4DBe2Kbzasjp6UHaxqeKEcWGDS0b+Np1otX5ozNovWjEViPLy9z jzXdW9A1V03LwAcq8i1brjkQy6p+0VI= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qXOnXYoT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of maskray@google.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=maskray@google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668198460; a=rsa-sha256; cv=none; b=PcCXu3ZYGgvRi3WaY3IJj1BRSb9c4QORykdebb6RE+yLFxn+pGa0a4kMpiSFsklO6U+LVm gCmLIQCIAdqnhe4rMrOTt1excK6woEvXMVlh5T4t1NKP8w0uX/Kd/Ac1mGeXjJTfALZVaD IxijJU4Y/0OdlpMnYJZDOz/l25lzen8= X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 8DCB8140005 X-Rspam-User: Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=qXOnXYoT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf23.hostedemail.com: domain of maskray@google.com designates 209.85.215.178 as permitted sender) smtp.mailfrom=maskray@google.com X-Stat-Signature: 6okopg95ah1ymuceo5cgfrc3atgjscw6 X-HE-Tag: 1668198460-651250 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: On 2022-11-11, Kees Cook wrote: >On Thu, Nov 10, 2022 at 11:42:34PM -0800, Fangrui Song wrote: >> (+ sam@gentoo.org from Pedro Falcato's patch) >> >> On 2022-11-10, Kees Cook wrote: >> > Traditionally, only the final PT_LOAD for load_elf_interp() supported >> > having p_memsz > p_filesz. Recently, lld's construction of musl's >> > libc.so on PowerPC64 started having two PT_LOAD program headers with >> > p_memsz > p_filesz. >> > >> > As the least invasive change possible, check for p_memsz > p_filesz for >> > each PT_LOAD in load_elf_interp. >> > >> > Reported-by: Rich Felker >> > Link: https://maskray.me/blog/2022-11-05-lld-musl-powerpc64 >> > Cc: Pedro Falcato >> > Cc: Fangrui Song >> > Cc: Alexander Viro >> > Cc: Eric Biederman >> > Cc: linux-fsdevel@vger.kernel.org >> > Cc: linux-mm@kvack.org >> > Signed-off-by: Kees Cook >> > --- >> > v2: I realized we need to retain the final padding call. >> > v1: https://lore.kernel.org/linux-hardening/20221111055747.never.202-kees@kernel.org/ >> > --- >> > fs/binfmt_elf.c | 18 ++++++++++++++---- >> > 1 file changed, 14 insertions(+), 4 deletions(-) >> > >> > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c >> > index 528e2ac8931f..0a24bbbef1d6 100644 >> > --- a/fs/binfmt_elf.c >> > +++ b/fs/binfmt_elf.c >> > @@ -673,15 +673,25 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, >> > last_bss = k; >> > bss_prot = elf_prot; >> > } >> > + >> > + /* >> > + * Clear any p_memsz > p_filesz area up to the end >> > + * of the page to wipe anything left over from the >> > + * loaded file contents. >> > + */ >> > + if (last_bss > elf_bss && padzero(elf_bss)) >> >> Missing { >> >> But after fixing this, I get a musl ld.so error. >> >> > + error = -EFAULT; >> > + goto out; >> > + } >> > } >> > } >> > >> > /* >> > - * Now fill out the bss section: first pad the last page from >> > - * the file up to the page boundary, and zero it from elf_bss >> > - * up to the end of the page. >> > + * Finally, pad the last page from the file up to the page boundary, >> > + * and zero it from elf_bss up to the end of the page, if this did >> > + * not already happen with the last PT_LOAD. >> > */ >> > - if (padzero(elf_bss)) { >> > + if (last_bss == elf_bss && padzero(elf_bss)) { >> > error = -EFAULT; >> > goto out; >> > } >> > -- >> > 2.34.1 >> > >> >> I added a new section to https://maskray.me/blog/2022-11-05-lld-musl-powerpc64 >> Copying here: >> >> To test that the kernel ELF loader can handle more RW `PT_LOAD` program headers, we can create an executable with more RW `PT_LOAD` program headers with `p_filesz < p_memsz`. >> We can place a read-only section after `.bss` followed by a `SHT_NOBITS` `SHF_ALLOC|SHF_WRITE` section. The read-only section will form a read-only `PT_LOAD` while the RW section will form a RW `PT_LOAD`. >> >> ```text >> #--- a.c >> #include >> #include >> >> extern const char toc[]; >> char nobits0[0] __attribute__((section(".nobits0"))); >> char nobits1[0] __attribute__((section(".nobits1"))); >> >> int main(void) { >> assert(toc[4096-1] == 0); >> for (int i = 0; i < 1024; i++) >> assert(nobits0[i] == 0); >> nobits0[0] = nobits0[1024-1] = 1; >> for (int i = 0; i < 4096; i++) >> assert(nobits1[i] == 0); >> nobits1[0] = nobits1[4096-1] = 1; >> >> puts("hello"); >> } >> >> #--- toc.s >> .section .toc,"aw",@nobits >> .globl toc >> toc: >> .space 4096 >> >> .section .ro0,"a"; .byte 255 >> .section .nobits0,"aw",@nobits; .space 1024 >> .section .ro1,"a"; .byte 255 >> .section .nobits1,"aw",@nobits; .space 4096 >> >> #--- a.lds >> SECTIONS { .ro0 : {} .nobits0 : {} .ro1 : {} .nobits1 : {} } INSERT AFTER .bss; >> ``` >> >> ```sh >> split-file a.txt a >> path/to/musl-gcc -Wl,--dynamic-linker=/lib/libc.so a/a.c a/a.lds -o toy >> ``` >> >> split-file is a utility in llvm-project. > >Where is a.txt? Also, it'd be nice to have this without needing the >musl-gcc. Sorry for the unclear description. I rewrite it. (`char nobits0[0] __attribute__((section(".nobits0")));` is not effective. It's SHT_PROGBITS and makes the output section SHT_PROGBITS. The new example addresses the deficiency.) Create some files. If you have split-file (a [test utility](https://llvm.org/docs/TestingGuide.html#extra-files) from llvm-project), you may place the following content into `a.txt`. ```text #--- a.c #include #include extern const char toc[]; extern char nobits0[], nobits1[]; int main(void) { assert(toc[4096-1] == 0); for (int i = 0; i < 1024; i++) { assert(nobits0[i] == 0); nobits0[i] = 1; } for (int i = 0; i < 8192; i++) { assert(nobits1[i] == 0); nobits1[i] = 1; } puts("hello"); } #--- toc.s .globl toc, nobits0, nobits1 .section .toc,"aw",@nobits; toc: .space 4096 .section .ro0,"a"; .byte 255 .section .nobits0,"aw",@nobits; nobits0: .space 1024 .section .ro1,"a"; .byte 255 .section .nobits1,"aw",@nobits; nobits1: .space 8192 #--- a.lds SECTIONS { .ro0 : {} .nobits0 : {} .ro1 : {} .nobits1 : {} } INSERT AFTER .bss; ``` Then run: ```sh split-file a.txt a path/to/musl-gcc -Wl,--dynamic-linker=/lib/libc.so a/a.c a/a.lds -o toy ``` Note: when a `SHT_NOBITS` section is followed by another section, the `SHT_NOBITS` section behaves as if it occupies the file offset range. This is because ld.lld does not implement a file size optimization. For this simple example, using glibc based gcc works as well (musl provides __assert_fail and puts referenced by the executable): gcc -Wl,--dynamic-linker=/lib/libc.so a/a.c a/a.lds -o toy