Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2299195rdh; Tue, 26 Sep 2023 20:37:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEl4mkJKG5U2u/xjjqOcwx/aQZMH1Q8457w1TIA5miMiCftQirCSPc+H7fSQHd0hqEGVa8P X-Received: by 2002:a05:6830:110b:b0:6bc:96c3:b6ce with SMTP id w11-20020a056830110b00b006bc96c3b6cemr907079otq.16.1695785835086; Tue, 26 Sep 2023 20:37:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695785835; cv=none; d=google.com; s=arc-20160816; b=YEaM7Oanzpq1qy9tb49ts49MLm+ZyozE/fK7Jwegpmwg7gMUSPMfGPtw2+fnygJFri IUcZNDmWGMYtFpybp2KcRpBDoEyRB6i7eXfve6o21d4ASeXUIRyoRIuQPVglr6zmCuQW /JoZTdfXIxncBlaMqo7uA9tbweiKX5dOQuZHWtlFNxbv3xBiEeo5QH1B6fCmxe+xczRz jZGOsKriRs+ftUoQM1v9iGiVouPerRAEGthw4TWGCO9+Pl6px699+gJ0D0Dggq+azlOI po4Fr8oOGu/Knnif+vRFaCeS7naQnzN7Y7rZpLVU/YClSG/t/Icq7eAzneljgoz2ndRF fzig== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=MEoyNhEirsDID+iSINOkm/zSxakfDUSa2NzmIBoMrtU=; fh=Xr8vRzEHOZH3wjZL8FfPlZtW2Jg2GYDKjBbRAUGW0KA=; b=JMj1brgCH15nWcpma6P4CrjNv4Rt5SNi116wYIdpZTxTj4Qr3A116LqgSJXsCorLFR K+Crvo19Q/43PIDeY0FrQfcpWoMWXZXVfiSWxTs72waqMJBFeqDyQdLLyqeJJxPvn8+n DG8raM1Ailv1ttrOdKyDM0A0JP5ofQaaJUKjxm9fIoKq2fA7zfS+81ZWvseK4dURHeLK L0juL5i2lhzTzpgZYGItjRa1epaE+w9xf8vIoAzXyhQRjXddEdLq6a+YMm8Lv2aUOvLW TXD0NnrtMjbF2qoIVJX2r0q687FJ5xH83zkCYsRYFZutaCeGbKy6FP71MQ7nVPvh8+Sp P+xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GMGLItkY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id f20-20020a637554000000b00565dd3fbfdfsi14023960pgn.214.2023.09.26.20.37.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 20:37:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=GMGLItkY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 3289080A90F3; Tue, 26 Sep 2023 20:17:19 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232882AbjI0DQ4 (ORCPT + 99 others); Tue, 26 Sep 2023 23:16:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51118 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233081AbjI0DOv (ORCPT ); Tue, 26 Sep 2023 23:14:51 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C991D7293 for ; Tue, 26 Sep 2023 19:34:52 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-68bed2c786eso8033692b3a.0 for ; Tue, 26 Sep 2023 19:34:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1695782092; x=1696386892; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=MEoyNhEirsDID+iSINOkm/zSxakfDUSa2NzmIBoMrtU=; b=GMGLItkY20tnShqsQSE1ZhhboeJoA4hi90EFgQw+P5N6+y00MJxEgvvxs8L44DXShX 336Qt0yAie1pjEVaU79cnyRBt7skxYSzpxnG/nisuELTEKMCPBJWk5pdOsfs+4/AB0xq 5zSFPiqdDv2RHeTYJvUYWap4ai0J42PbGvQno= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695782092; x=1696386892; h=in-reply-to:content-transfer-encoding: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=MEoyNhEirsDID+iSINOkm/zSxakfDUSa2NzmIBoMrtU=; b=Uo0EfpuErnk9EiSG6YjK+oen8J8CiTNp91yJiR+OQ6KrTFJcQ63Jf13EfodX4Q9YoV QAZuaA4PA6QA1BtGi67gao7JLnCHMq2rlDxP0eyFXEFfGe5TBQ6Zyb5pIcl9LDLtpm15 HRWgdpwtFRJsEnZrDz0V1KAWQ34nqlHHFRS1kO5znJtgRk7Li0HJ3LsCx9uJhiolPb8T qGTjkVfieE3FN5I6fQMMRziZfqUBRjYPTT3Oaw8r3v7CiV+UJ+PmiTicifok4Uu9AMfy 6Emj4uwktmvAN9Ma8oxzGoPs2z6K2a+9h96EawSKaMkpBSX2gfa/Ll8/OschwNpbCwmk w0dg== X-Gm-Message-State: AOJu0YyYAkTkA4SxaQt3pPVuxDZ3kz0b9mY4gn9Qo3YdFwbzUFrcFRAJ hwjSmrXPdokwe/AKJAMqCd2Dfl7rmt1orRs9+PI= X-Received: by 2002:a05:6a00:139d:b0:692:780a:de89 with SMTP id t29-20020a056a00139d00b00692780ade89mr882056pfg.33.1695782092187; Tue, 26 Sep 2023 19:34:52 -0700 (PDT) Received: from www.outflux.net (198-0-35-241-static.hfc.comcastbusiness.net. [198.0.35.241]) by smtp.gmail.com with ESMTPSA id z26-20020aa785da000000b006883561b421sm10697260pfn.162.2023.09.26.19.34.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 19:34:51 -0700 (PDT) Date: Tue, 26 Sep 2023 19:34:50 -0700 From: Kees Cook To: "Eric W. Biederman" Cc: Sebastian Ott , Pedro Falcato , Thomas =?iso-8859-1?Q?Wei=DFschuh?= , Alexander Viro , Christian Brauner , Mark Brown , Willy Tarreau , sam@gentoo.org, Rich Felker , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] binfmt_elf: Support segments with 0 filesz and misaligned starts Message-ID: <202309261929.BE87B8B7@keescook> References: <20230914-bss-alloc-v1-1-78de67d2c6dd@weissschuh.net> <36e93c8e-4384-b269-be78-479ccc7817b1@redhat.com> <87zg1bm5xo.fsf@email.froward.int.ebiederm.org> <37d3392c-cf33-20a6-b5c9-8b3fb8142658@redhat.com> <87jzsemmsd.fsf_-_@email.froward.int.ebiederm.org> <84e974d3-ae0d-9eb5-49b2-3348b7dcd336@redhat.com> <202309251001.C050864@keescook> <87v8bxiph5.fsf@email.froward.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87v8bxiph5.fsf@email.froward.int.ebiederm.org> X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 26 Sep 2023 20:17:19 -0700 (PDT) On Mon, Sep 25, 2023 at 10:27:02PM -0500, Eric W. Biederman wrote: > Kees Cook writes: > > > On Mon, Sep 25, 2023 at 05:27:12PM +0200, Sebastian Ott wrote: > >> On Mon, 25 Sep 2023, Eric W. Biederman wrote: > >> > > >> > 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(-) > >> > > >> > Can you please test this one? > > > > Eric thanks for doing this refactoring! This does look similar to the > > earlier attempt: > > https://lore.kernel.org/lkml/20221106021657.1145519-1-pedro.falcato@gmail.com/ > > and it's a bit easier to review. > > I need to context switch away for a while so Kees if you will > I will let you handle the rest of this one. > > > A couple of thoughts running through my head for anyone whose ambitious > might include cleaning up binfmt_elf.c > > The elf_bss variable in load_elf_binary can be removed. > > Work for a follow on patch is using my new elf_load in load_elf_interp > and possibly in load_elf_library. (More code size reduction). > > An outstanding issue is if the first segment has filesz 0, and has a > randomized locations. But that is the same as today. > > There is a whole question does it make sense for the elf loader > to have it's own helper vm_brk_flags in mm/mmap.c or would it > make more sense for binfmt_elf to do what binfmt_elf_fdpic does and > have everything to go through vm_mmap. > > I think replacing vm_brk_flags with vm_mmap would allow fixing the > theoretical issue of filesz 0 and randomizing locations. > > > > In this change I replaced an open coded padzero that did not clear > all of the way to the end of the page, with padzero that does. Yeah, the resulting code is way more readable now. > I also stopped checking the return of padzero as there is at least > one known case where testing for failure is the wrong thing to do. > It looks like binfmt_elf_fdpic may have the proper set of tests > for when error handling can be safely completed. > > I found a couple of commits in the old history > https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git, > that look very interesting in understanding this code. > > commit 39b56d902bf3 ("[PATCH] binfmt_elf: clearing bss may fail") > commit c6e2227e4a3e ("[SPARC64]: Missing user access return value checks in fs/binfmt_elf.c and fs/compat.c") > commit 5bf3be033f50 ("v2.4.10.1 -> v2.4.10.2") > > Looking at commit 39b56d902bf3 ("[PATCH] binfmt_elf: clearing bss may fail"): > > commit 39b56d902bf35241e7cba6cc30b828ed937175ad > > Author: Pavel Machek > > Date: Wed Feb 9 22:40:30 2005 -0800 > > > > [PATCH] binfmt_elf: clearing bss may fail > > > > So we discover that Borland's Kylix application builder emits weird elf > > files which describe a non-writeable bss segment. > > > > So remove the clear_user() check at the place where we zero out the bss. I > > don't _think_ there are any security implications here (plus we've never > > checked that clear_user() return value, so whoops if it is a problem). > > > > Signed-off-by: Pavel Machek > > Signed-off-by: Andrew Morton > > Signed-off-by: Linus Torvalds > > It seems pretty clear that binfmt_elf_fdpic with skipping clear_user > for non-writable segments and otherwise calling clear_user (aka padzero) > and checking it's return code is the right thing to do. > > I just skipped the error checking as that avoids breaking things. > > It looks like Borland's Kylix died in 2005 so it might be safe to > just consider read-only segments with memsz > filesz an error. I really feel like having a read-only BSS is a pathological state that should be detected early? > Looking at commit 5bf3be033f50 ("v2.4.10.1 -> v2.4.10.2") the > binfmt_elf.c bits confirm my guess that the weird structure is because > before that point binfmt_elf.c assumed there would be only a single > segment with memsz > filesz. Which is why the code was structured so > weirdly. Agreed. > Looking a little farther it looks like the binfmt_elf.c was introduced > in Linux v1.0, with essentially the same structure in load_elf_binary as > it has now. Prior to that Linux hard coded support for a.out binaries > in execve. So if someone wants to add a Fixes tag it should be > "Fixes: v1.0" > > Which finally explains to me why the code is so odd. For the most part > the code has only received maintenance for the last 30 years or so. > Strictly 29 years, but 30 has a better ring to it. > > Anyway those are my rambling thoughts that might help someone. > For now I will be happy if we can get my elf_load helper tested > to everyone's satisfaction and merged. I'm probably going to pull most of this email into the commit log for the v2 patch -- there's good history here worth capturing. -- Kees Cook