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 45CE7C433EF for ; Fri, 28 Jan 2022 08:26:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C12FA6B0071; Fri, 28 Jan 2022 03:26:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BC2EA6B0072; Fri, 28 Jan 2022 03:26:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A63E26B0073; Fri, 28 Jan 2022 03:26:14 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id 936546B0071 for ; Fri, 28 Jan 2022 03:26:14 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4C2DE951B3 for ; Fri, 28 Jan 2022 08:26:14 +0000 (UTC) X-FDA: 79079013468.12.F377A81 Received: from mail-out-2a.itc.rwth-aachen.de (mail-out-2a.itc.rwth-aachen.de [134.130.5.45]) by imf28.hostedemail.com (Postfix) with ESMTP id 69D12C0005 for ; Fri, 28 Jan 2022 08:26:12 +0000 (UTC) X-IPAS-Result: =?us-ascii?q?A2APAACBo/Nh/6QagoZaGwEBAQEBAQEBBQEBARIBAQEDA?= =?us-ascii?q?wEBAUCBRgYBAQELAYFRgixqhEmII4ZLggchnFGBfAsBAQEBAQEBAQEIAT8CB?= =?us-ascii?q?AEBhQUCg18CJTQJDgECBAEBAQEDAgMBAQEBAQEDAQEGAQEBAQEBBQSBHIUvR?= =?us-ascii?q?oZCAQEBAQMjDwE7CxALEQMBAgECAiYCAhAEMwgO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hAzSJflhTBCwfdg= IronPort-HdrOrdr: A9a23:sWQt/qGtukwdgirjpLqFaZHXdLJyesId70hD6qkvc3Fom52j/f xGws5x6fatskdpZJkh8erwW5VoMkmsjaKdgLNhdotKOTOLhILGFvAE0WKP+Vzd8k7Fh6RgPM VbAs5D4bTLZDAU4/oSizPIcerIteP3lJxA8t2uqkuFIzsLV4hQqyNCTiqLGEx/QwdLQbAjEo CH28ZBrz28PVwKc8WSHBA+LqT+juyOsKijTQ8NBhYh5gXLpyiv8qTGHx+R2Qpbey9TwI0l7X POn2XCl+qeWrCAu1HhPl3ontRrcejau5h+7Qu3+4oowwDX+0eVjUJaKvi/VX4O0aWSAR0R4a LxSl8bTr5OAjXqDyyISFLWqnTd+Sdr5Hn4xVCCh3z/5cT/WTIhEsJEwZlUax3D9iMbzadBOY 9wrhakXqBsfGT9deXGlqj1fgAvklDxrWspkOYVgXAaWYwCaKVJpYha+E9OCp8PEC/z9YhiSY BVfYnhzecTdUnfY2HSv2FpztDpVnMvHg2eSkxHvsCOyTBZkH1w0kNdzs0CmXUL8o47VvB/lq 35G7UtkKsLQt4dbKp7CutEScyrCnbVSRaJK26WKUSPLtBzB5rnw6SHnIndJNvaCqDg4KFC5q gpYWkoxlLaIXiedvFm9Kc7gyzwfA== X-IronPort-Anti-Spam-Filtered: true X-IronPort-AV: E=Sophos;i="5.88,323,1635199200"; d="scan'208";a="422091" Received: from rwthex-s4-a.rwth-ad.de ([134.130.26.164]) by mail-in-2a.itc.rwth-aachen.de with ESMTP; 28 Jan 2022 09:26:10 +0100 Received: from localhost (2a02:908:1066:22e0:95a5:b322:26dd:ff8d) by rwthex-s4-a.rwth-ad.de (2a00:8a60:1:e500::26:164) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.15; Fri, 28 Jan 2022 09:26:09 +0100 Date: Fri, 28 Jan 2022 09:26:09 +0100 From: Magnus =?utf-8?B?R3Jvw58=?= To: Kees Cook CC: Andrew Morton , Alexander Viro , Eric Biederman , , , Subject: Re: [PATCH] elf: Relax assumptions about vaddr ordering Message-ID: References: <202201260845.FCBC0B5A06@keescook> <202201262230.E16DF58B@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline In-Reply-To: <202201262230.E16DF58B@keescook> User-Agent: Mutt/2.1.5 (31b18ae9) (2021-12-30) X-Originating-IP: [2a02:908:1066:22e0:95a5:b322:26dd:ff8d] X-ClientProxiedBy: rwthex-w4-b.rwth-ad.de (2a00:8a60:1:e500::26:167) To rwthex-s4-a.rwth-ad.de (2a00:8a60:1:e500::26:164) X-Stat-Signature: zxcogn4fygne7eo9tgdrx9grsa45b9bm X-Rspam-User: nil Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.hostedemail.com: domain of magnus.gross@rwth-aachen.de designates 134.130.5.45 as permitted sender) smtp.mailfrom=magnus.gross@rwth-aachen.de X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 69D12C0005 X-HE-Tag: 1643358372-667254 Content-Transfer-Encoding: quoted-printable 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, Jan 26, 2022 at 10:31:42PM -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=C3=9F wrote: > > > From ff4dde97e82727727bda711f2367c05663498b24 Mon Sep 17 00:00:00 2= 001 > > > 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_bina= ry(). > > >=20 > > > Specifically consider an ELF binary with the following PT_LOAD segm= ents: > > >=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 ve= rsa. > > >=20 > > > Since total_mapping_size() only computes the difference between the > > > first and the last segment in the order that they appear, it will r= eturn > > > a size of 0 in this case, thus causing load_elf_binary() to fail, w= hich > > > 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=C3=9F > >=20 > > Fixes: 5f501d555653 ("binfmt_elf: reintroduce using MAP_FIXED_NOREPLA= CE") > > Cc: stable@vger.kernel.org > > Acked-by: Kees Cook >=20 > Andrew, can you pick this up too? >=20 > -Kees >=20 May I also propose to include this patch in whatever mailing-list corresponds to the 5.16.x bugfix series? It turns out that almost all native Linux games published by the Virtual Programming company have this kind of weird PT_LOAD ordering including the famous Bioshock Infinite, so right now those games are all completely broken since Linux 5.16. P.S.: Someone should probably ask Virtual Programming, what kind of tooling they use to create such convoluted ELF binaries. > >=20 > > -Kees > >=20 > > > --- > > > fs/binfmt_elf.c | 18 ++++++++++++++---- > > > 1 file changed, 14 insertions(+), 4 deletions(-) > > >=20 > > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > > > index f8c7f26f1fbb..0caaad9eddd1 100644 > > > --- a/fs/binfmt_elf.c > > > +++ b/fs/binfmt_elf.c > > > @@ -402,19 +402,29 @@ static unsigned long elf_map(struct file *fil= ep, unsigned long addr, > > > static unsigned long total_mapping_size(const struct elf_phdr *cmd= s, int nr) > > > { > > > int i, first_idx =3D -1, last_idx =3D -1; > > > + unsigned long min_vaddr =3D ULONG_MAX, max_vaddr =3D 0; > > > =20 > > > for (i =3D 0; i < nr; i++) { > > > if (cmds[i].p_type =3D=3D PT_LOAD) { > > > - last_idx =3D i; > > > - if (first_idx =3D=3D -1) > > > + /* > > > + * The PT_LOAD segments are not necessarily ordered > > > + * by vaddr. Make sure that we get the segment with > > > + * minimum vaddr (maximum vaddr respectively) > > > + */ > > > + if (cmds[i].p_vaddr <=3D min_vaddr) { > > > first_idx =3D i; > > > + min_vaddr =3D cmds[i].p_vaddr; > > > + } > > > + if (cmds[i].p_vaddr >=3D max_vaddr) { > > > + last_idx =3D i; > > > + max_vaddr =3D cmds[i].p_vaddr; > > > + } > > > } > > > } > > > if (first_idx =3D=3D -1) > > > return 0; > > > =20 > > > - return cmds[last_idx].p_vaddr + cmds[last_idx].p_memsz - > > > - ELF_PAGESTART(cmds[first_idx].p_vaddr); > > > + return max_vaddr + cmds[last_idx].p_memsz - ELF_PAGESTART(min_vad= dr); > > > } > > > =20 > > > static int elf_read(struct file *file, void *buf, size_t len, loff= _t pos) > > > --=20 > > > 2.34.1 > >=20 > > --=20 > > Kees Cook >=20 > --=20 > Kees Cook