Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2659239rwb; Fri, 11 Nov 2022 12:41:30 -0800 (PST) X-Google-Smtp-Source: AA0mqf4DP6dXAo6bdMhAgOcLvLDpochhD2BMfE44tfB3x0kKRc1KRUU1GnERNetEnjd3HVzlzqMC X-Received: by 2002:a65:67d9:0:b0:440:6e64:fb02 with SMTP id b25-20020a6567d9000000b004406e64fb02mr3159147pgs.84.1668199290082; Fri, 11 Nov 2022 12:41:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668199290; cv=none; d=google.com; s=arc-20160816; b=CW2mPSac3AaKk0BxwscrKfbLxINYheOS0ykQ/1AamqvDh+OiMhQvv3odR0HRhcPFtm aoGwUUgOkTxmwq1BzIL0AuY4CBiQ11pmGKBP4cxzekChDzkt6n9jDSlLqFBM3qSowAYW zv/U2ut63QL2/gJlGtGYJveIJKywybsADqaB0lN56C/wlJcN9CHBrX/pN1FY+XKQfS3w CQa1+nzVY8BYDeUgqvZcrXwxUZbomxqOOBMU5GE7Icuq4AR7V7QEWmfySy7naUnMyDrc Kz1JV0IlYYgQ9zxn5WU6XuuRWTsb1AzBGVWTvTZEFG5aTNtmp+rjzSVBnNwFLZRJmCGK rFgg== 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=CzfuirqUexa4OrUP6aM1ldAHfjduol2fhAGQPIMhaNg=; b=wbG1Lb+lHqVY0pyoGEmXKwQQGFXXCaQma3YufMX1lgJnMB8NRIINxRd9MXainsBiH7 cFeGDEBKnGiQw9CyuTyUVOcIBKmKcFGoPFm8usyLE7mpxFkZR2oSNbY72xF5o6Z0LA75 LipcIqinav+Lu/ZDCY43g9lmtUU57ZEWNqlM3z9IODq65wPmxEMuwMRSVVwexo2F1mqH 6yjpGIhdUtz2VgTRdBlsP6eE1F3fnpm5mmh1G03pdBF/MN9YR87e9uC6YXbAll1Gnthn gXSCySmOTIS+zqqk3inWa9ujxqqtjObHB/TGrUUBO40G3pfF+vnUH4uHuv1jq6eYV9Gu D9qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=eiMN91hn; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o14-20020a63e34e000000b0046edb8cdbf7si3058613pgj.795.2022.11.11.12.41.18; Fri, 11 Nov 2022 12:41:30 -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=@chromium.org header.s=google header.b=eiMN91hn; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234461AbiKKUOU (ORCPT + 90 others); Fri, 11 Nov 2022 15:14:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234449AbiKKUOT (ORCPT ); Fri, 11 Nov 2022 15:14:19 -0500 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C1FB5B860 for ; Fri, 11 Nov 2022 12:14:18 -0800 (PST) Received: by mail-pg1-x535.google.com with SMTP id f63so5211411pgc.2 for ; Fri, 11 Nov 2022 12:14:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; 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=CzfuirqUexa4OrUP6aM1ldAHfjduol2fhAGQPIMhaNg=; b=eiMN91hnPXZQzawWYNQtt2JbgJX4zONo3XXA2vHKrwUDAZM35OEnfRYlvN5dnWnHK3 pjLnnizKKu4ocjX9ODiE3DPWno1mHG1fttmdmWfyUh8rGHcRgmCdXnDpnak17egxCSpg lGr2Lz2VD1IMD9rcdGZMZuwgOUP314MaWeyXo= 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=CzfuirqUexa4OrUP6aM1ldAHfjduol2fhAGQPIMhaNg=; b=3h2ed5IyePcbNKmr2e9hzeErYf5mlrL9WV3hKkAdP+kNZU61Q1SImmfaCUuVELIoV8 WcwkAHRgiBMoIT25ir2mmZlEfYlRMeMYP0gbXbcmQONkKgN/+cDw2krZlH9Ez1l8l69Z juwdfapVYOPdObUxmRk1qbygB57l5eoZk/W0upS9aDSucoZWnGFEV6ci+5UIdaBQ37X4 YEZrxdF4M+DXZgDy3sLIfSpKLwUNfFi0Ia+qzYWqsV6YO2CyNhyEaMsZMVy1dpPwUH+Z H2T7DDQtGJFCi0dSLeWXoiVbyPFJtmL7aEW8B+ukVlhu4nbMy6JRnRMY8untIdydV//l W+9Q== X-Gm-Message-State: ANoB5pkllaXCNLkMQkamB93gzTEfviJktOGl9OX2UJQaHjVXDh+BY/4D WGzP9bjVR7BOTZRMC0+O8JsG6g== X-Received: by 2002:aa7:99c5:0:b0:562:3add:37e1 with SMTP id v5-20020aa799c5000000b005623add37e1mr4169347pfi.80.1668197600735; Fri, 11 Nov 2022 12:13:20 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c188-20020a624ec5000000b0056be7ac5261sm1951559pfb.163.2022.11.11.12.13.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Nov 2022 12:13:20 -0800 (PST) Date: Fri, 11 Nov 2022 12:13:19 -0800 From: Kees Cook To: Fangrui Song 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: <202211111211.93ED8B4B@keescook> References: <20221111061315.gonna.703-kees@kernel.org> <20221111074234.xm5a6ota7ppdsto5@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221111074234.xm5a6ota7ppdsto5@google.com> X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,URIBL_BLACK 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 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. -- Kees Cook