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 04FDFC4332F for ; Tue, 1 Feb 2022 22:44:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7DCA28D006D; Tue, 1 Feb 2022 17:44:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 78BA66B0174; Tue, 1 Feb 2022 17:44:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6535A8D006D; Tue, 1 Feb 2022 17:44:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 5132E6B0172 for ; Tue, 1 Feb 2022 17:44:51 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0F46C824C442 for ; Tue, 1 Feb 2022 22:44:51 +0000 (UTC) X-FDA: 79095692382.23.0BECDDB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf12.hostedemail.com (Postfix) with ESMTP id 8D6A840007 for ; Tue, 1 Feb 2022 22:44:50 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 78B9460E15; Tue, 1 Feb 2022 22:44:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1198C340EB; Tue, 1 Feb 2022 22:44:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1643755488; bh=dO2ZiNSWR6loVZvexI+PkBxFGJVVZftpKMMgcbecSMU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=an9agKNMXKcOvdlYglAxVxZwJft3R1qKd11ShJ24N53s77YppESIwDKuLEfDGC5Q7 KZbS3UIRpEMEQucPVSbdSP/BPNzCtDgE928WBXcIi7rsjg7pH7q3R70Weh1AeeDkIx 2TBHPQOFv3wko+8aPAWdmoYyL9tx2jL8BgxA5NQg= Date: Tue, 1 Feb 2022 14:44:48 -0800 From: Andrew Morton To: Kees Cook Cc: Magnus =?ISO-8859-1?Q?Gro=DF?= , Alexander Viro , Eric Biederman , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-hardening@vger.kernel.org, Alexey Dobriyan Subject: Re: [PATCH] elf: Relax assumptions about vaddr ordering Message-Id: <20220201144448.8b3b322dc58dc3ec71f72246@linux-foundation.org> In-Reply-To: <202201262230.E16DF58B@keescook> References: <202201260845.FCBC0B5A06@keescook> <202201262230.E16DF58B@keescook> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 8D6A840007 X-Stat-Signature: nqnxkd8w189igh8drcdsgesx9cmrpbxb Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=an9agKNM; dmarc=none; spf=pass (imf12.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org X-Rspam-User: nil X-HE-Tag: 1643755490-105048 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 Wed, 26 Jan 2022 22:31:42 -0800 Kees Cook wrote: > On Wed, Jan 26, 2022 at 08:50:15AM -0800, Kees Cook wrote: > > On Wed, Jan 26, 2022 at 05:25:20PM +0100, Magnus Gro=DF wrote: > > > From ff4dde97e82727727bda711f2367c05663498b24 Mon Sep 17 00:00:00 2001 > > > From: =3D?UTF-8?q?Magnus=3D20Gro=3DC3=3D9F?=3D > > > Date: Wed, 26 Jan 2022 16:35:07 +0100 > > > Subject: [PATCH] elf: Relax assumptions about vaddr ordering > > > MIME-Version: 1.0 > > > Content-Type: text/plain; charset=3DUTF-8 > > > Content-Transfer-Encoding: 8bit > > >=20 > > > Commit 5f501d555653 ("binfmt_elf: reintroduce using > > > MAP_FIXED_NOREPLACE") introduced a regression, where the kernel now > > > assumes that PT_LOAD segments are ordered by vaddr in load_elf_binary= (). > > >=20 > > > Specifically consider an ELF binary with the following PT_LOAD segmen= ts: > > >=20 > > > Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align > > > LOAD 0x000000 0x08000000 0x08000000 0x474585 0x474585 R E 0x1000 > > > LOAD 0x475000 0x08475000 0x08475000 0x090a4 0xc6c10 RW 0x1000 > > > LOAD 0x47f000 0x00010000 0x00010000 0x00000 0x7ff0000 0x1000 > > >=20 > > > Note how the last segment is actually the first segment and vice vers= a. > > >=20 > > > Since total_mapping_size() only computes the difference between the > > > first and the last segment in the order that they appear, it will ret= urn > > > a size of 0 in this case, thus causing load_elf_binary() to fail, whi= ch > > > did not happen before that change. > > >=20 > > > Strictly speaking total_mapping_size() made that assumption already > > > before that patch, but the issue did not appear because the old > > > load_addr_set guards never allowed this call to total_mapping_size(). > > >=20 > > > Instead of fixing this by reverting to the old load_addr_set logic, we > > > fix this by comparing the correct first and last segments in > > > total_mapping_size(). > >=20 > > Ah, nice. Yeah, this is good. > >=20 > > > Signed-off-by: Magnus Gro=DF > >=20 > > Fixes: 5f501d555653 ("binfmt_elf: reintroduce using MAP_FIXED_NOREPLACE= ") > > Cc: stable@vger.kernel.org > > Acked-by: Kees Cook >=20 > Andrew, can you pick this up too? >=20 It conflicts significantly with Alexey's "ELF: fix overflow in total mapping size calculation".