Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp1797787rwb; Thu, 10 Nov 2022 23:58:42 -0800 (PST) X-Google-Smtp-Source: AA0mqf6hG2sck1vs137egiSR6Ne9hsjla9ogl9bs7zgaqK7r/bHtmxDUq5Kv6PwpL1BLaRB2p9XP X-Received: by 2002:a17:906:16d8:b0:7ae:23c:3cb4 with SMTP id t24-20020a17090616d800b007ae023c3cb4mr890918ejd.599.1668153522328; Thu, 10 Nov 2022 23:58:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668153522; cv=none; d=google.com; s=arc-20160816; b=M2FRRr/yEgE1Q5IuH5UdIho6lYX3B2WkWlK2N/KJsXfEtNlrkuhSV+u7H24J+I683y zbf4RlLNk1b8BwrlBOwh9plvM2jEkJ6jgQAUlZ1WuBLasWjJvgzZPRkwzJzj9qu8yzsS M7RE6RMkhB5boN1+3WJc1j746+E6XbA0c1ACPw0gMNmTJlmLRtE+j7UF6YwXm3D0DC84 +tE+1buebd9Rfc3PkE9GXER/afzJirsIha8hjYthD2n5qq7JzYbdYsZDCXM3UY/QTWeu N9exmP7lZdKYZXDx0a8WpYgPcLgg9T4YFY38y81fTJ5MaLPF5BKyjivBFpbVPkXrLTAl VAFQ== 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=DPlfHwSzNGfVHYFsMHE3q3XKtrEblxpPMeiSWifb33g=; b=bl89WA3lHQQreWor+YpkaZI30E2qOXpowZ5O5ZnYIKyULn1VkibRHorHWc1Ax6odf8 +zuD6dHgVWJTfzDykQkOtSNNVHk3YfcRTV7HUlyDmzLWh2VqxRmMGC8ayMMAnCbSWd3O ZUdqEGVaOSBm4QgheVuRwaMld2VMLHE0hG58tYPhAKlqK0yoKBL0ratw8VrgMTUA/1DD P9xVX4TycRowEQTB9qFSOqddXHmgU8SJn2U3On0L0/RFN4cmmLKq4P/uMzl1jrO21NA6 oYdU2yvaYUeyuGcojcuo8PScYBkB2x6YfHakg83q95b8iw+C3ZYZlrUk0CFnJ6g7dxov ZFhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="M/tZlkQu"; 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 s2-20020a170906168200b0078e29796733si1058768ejd.653.2022.11.10.23.58.19; Thu, 10 Nov 2022 23:58:42 -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="M/tZlkQu"; 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 S233065AbiKKHmo (ORCPT + 93 others); Fri, 11 Nov 2022 02:42:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233051AbiKKHml (ORCPT ); Fri, 11 Nov 2022 02:42:41 -0500 Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 759FE742FD for ; Thu, 10 Nov 2022 23:42:40 -0800 (PST) Received: by mail-pj1-x102e.google.com with SMTP id m6-20020a17090a5a4600b00212f8dffec9so4083917pji.0 for ; Thu, 10 Nov 2022 23:42: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=DPlfHwSzNGfVHYFsMHE3q3XKtrEblxpPMeiSWifb33g=; b=M/tZlkQud5ba+2P9ogVpn++cTNC01PLRW0GjC9txFQf7MuZhwIJCk/ePeLQDSH1jqB ZN1h9AYoJriAQ8AZXELGghXus+BQgjW1r1wI5XaSaaSqgV9Av10rBZ6rbqL/RmYT/RlB HiN7umoR3s7s4zWOnO8f3vI9SKPm6HA56FVSLVY7RLb5udjWmAZ2hTbpZsGqgJQR7l9w ShqHDZca4tc1Hx8SLQPJnemY+17GR2xmWW0EZFAmtWXAHxTQzdnME9rwzZg2rxoDS+Oq 62X4GrGE08o8N+1x9ud7dP4+jAT1WQkrcKqk21higRwdUfbv4m4ikDW8iO0TPw/BipH3 HZ5g== 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=DPlfHwSzNGfVHYFsMHE3q3XKtrEblxpPMeiSWifb33g=; b=K1KlHPxUooDVlpnp9F35PKTk3Rqhs7spnLbRPNRZHyPXnhAHkWmy36UZ7CX/IIqGBR fQ1hFb69IIPIxLSJgR6U9XNs7YMbaIzNOlIee+sU3pz9KcA1T6JnPCXpi2H9Zup5/t5p 4ASI7qLzlMCQW4ZU+O3LDEgs8XK0IDZvt4dQqcluC20YT56WCXBfzY2r0QGWnKzk+y8q 3eolbmmWKxpNt9jJUjqq7oN6/XQvSupAaIRd12rIy0RuXOmbgmC7h/sQQUcLFE+w6Id9 x/yoSIJjRxmERvQ9/+RNs2GzhKXoq1vJ4obBm7wuDSvnMPAbhMCQfcIwyf8kiUsthn/r NQIw== X-Gm-Message-State: ANoB5pnPfXobT1Lr3DDBOuWt5Xeb8/Qzyrk3GhG7iLxsQRmwVs6nU6qB p7Bqrzl7XZ6YCntkLPelzH+Lm9JotPynSw== X-Received: by 2002:a17:902:e013:b0:186:9b23:a112 with SMTP id o19-20020a170902e01300b001869b23a112mr1531311plo.15.1668152559788; Thu, 10 Nov 2022 23:42:39 -0800 (PST) Received: from google.com ([2620:15c:2ce:200:8b77:5448:ea74:27a]) by smtp.gmail.com with ESMTPSA id z35-20020a631923000000b0043a1c0a0ab1sm799275pgl.83.2022.11.10.23.42.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Nov 2022 23:42:38 -0800 (PST) Date: Thu, 10 Nov 2022 23:42: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: <20221111074234.xm5a6ota7ppdsto5@google.com> References: <20221111061315.gonna.703-kees@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20221111061315.gonna.703-kees@kernel.org> 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 (+ 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.