Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2685108rwb; Fri, 11 Nov 2022 13:07:48 -0800 (PST) X-Google-Smtp-Source: AA0mqf6SMdr2CeiPonlHlaA/djUfcUJufueg/AStzN3H98SbAgJA9mPDN/kIebzYUZmPFear3+Cs X-Received: by 2002:a17:90a:6ba6:b0:213:2173:f431 with SMTP id w35-20020a17090a6ba600b002132173f431mr3678847pjj.201.1668200868071; Fri, 11 Nov 2022 13:07:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668200868; cv=none; d=google.com; s=arc-20160816; b=PN7QXALfkq16fekzIxKiizcZa6DmIjrUgYono/phT4ilPJKzHLMlAEEEN8a32dUcSi q7czINT7lc2S6i163q207UZ5oaL4vvMEgXA7NSp1uUFOFXwUxpFLsR+WIAMs2J/u/n9p eMDUCv5K8lWdmUZG1N72DRIY73jyxjN9IgJSJYy4A2j3qhLP6i/DrNNAGtVS2C1nQYXK FBViNMuTRTLngd0QIDsW5qsGFpzTh3hUmxsb+w32okekmymQk/93AF57xx+dIk7+gUSv mbsWxupCx2YuEEtAKIEyZYBOt9oAhYH1etimsiRN1LdrWd/x9Y3Jd+olNAjdJecJmo0v Bp3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=VenKawNdI6vht+/5rGH39TnWAPmNSDWBqV+Ma0YasTE=; b=jcrfHszRvLveoCOBTQC3dxVIPS1NgkRQwXg5drZkcdRzej9CB6Fo9nz5QAxGNSjeTb yLB5NdrE7J+6OtHQbZhskQ1hPvjn27uJqdsRyU9AbS/j4H23KfOk+OlJqLAjrbTGM+7u j1lHEIlJwv9tsagTw2cpfgv4Hkt6VadXZY6WhbZQloLNNBks1XUcZbxyvztryas3U0Oc E2MF2iI7LUnjgqMiWJDUbMuZPF2IFlmDMPdopnIu+Xx3rTy2VWt/hEr3bB1P1OSy5KVN 9wQWT1RGVm0YMA9Pkmfjlx/7pkecsU38Ye/E8loooHhSFIK9I1Fatf7dxFLyH7RtUaw6 Rdtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qXOnXYoT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q5-20020a170902a3c500b0016c474690cfsi3206239plb.318.2022.11.11.13.07.36; Fri, 11 Nov 2022 13:07:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=qXOnXYoT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234479AbiKKU1m (ORCPT + 91 others); Fri, 11 Nov 2022 15:27:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233172AbiKKU1l (ORCPT ); Fri, 11 Nov 2022 15:27:41 -0500 Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4A61748D1 for ; Fri, 11 Nov 2022 12:27:39 -0800 (PST) Received: by mail-pg1-x530.google.com with SMTP id 130so5229842pgc.5 for ; Fri, 11 Nov 2022 12:27:39 -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=nEMMjIfpuFXY82yAWbL8dazKcZqXGqre9MfoRaTSYu9rXr0r7/2ReAo7Zm7JKwyNWN ZWM4K7X9IRXFv5pEZDQW7zXDJGV/gIvgCGpl4C55QvH+Lvi8hPTnmKp+NtVOLkKIw6Jl 953bsKZMLRzMA1kcl6UFKTh41icXaiKj9dFGfdFbIoligQXWFKNBRbDxhdwm6qXYaoJ+ 9LgHF34GNxBJuLSwwetdvaQVHQ+NQPE3AejkXhshJFTFWwueYh1RyiW67fn0BJZnr/o7 bkk2UMqOv5YadmVvU4hPVwschKAuEw3VZBF4f31L8eHg+Kpj6wjzAtnQ+jK6/fOQdQJV myWA== X-Gm-Message-State: ANoB5pl8vGO0bOVCPrY8M797GhCJ2skHHEygj0FZWYUHsiw8hCyAcoTX jXubdIMG8RUzNhLovj7qlTdxrA== 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> X-Spam-Status: No, score=-15.9 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, URIBL_BLACK,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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