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 66DBAC4332F for ; Sat, 29 Jan 2022 13:28:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 777A56B009E; Sat, 29 Jan 2022 08:28:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 727626B009F; Sat, 29 Jan 2022 08:28:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5EEB06B00A0; Sat, 29 Jan 2022 08:28:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id 501136B009E for ; Sat, 29 Jan 2022 08:28:31 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id EC82A182371AF for ; Sat, 29 Jan 2022 13:28:30 +0000 (UTC) X-FDA: 79083403980.30.DBD179D Received: from mail-out-3.itc.rwth-aachen.de (mail-out-3.itc.rwth-aachen.de [134.130.5.48]) by imf01.hostedemail.com (Postfix) with ESMTP id 6290440002 for ; Sat, 29 Jan 2022 13:28:29 +0000 (UTC) X-IPAS-Result: =?us-ascii?q?A2AOAAAqQPVh/6QagoZaGgEBAQEBAQEBAQEDAQEBARIBA?= =?us-ascii?q?QEBAgIBAQEBQIFGBQEBAQELAYMlWGoMhD2II4hwA4VMi26LGIF8CwEBAQEBA?= =?us-ascii?q?QEBAQgBNQoCBAEBhQUCg2ACJTQJDgECBAEBAQEDAgMBAQEBAQEDAQEGAQEBA?= =?us-ascii?q?QEBBQSBHIUvOQ2GQgEBAQECASMPATsLEAsRAwECAQICJgIC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FDOmT6hoJnPQSYr6QjNGGGo9AV0YMiifYPA1LTAxexfMIaUXhya4 D0N3dKB8OBLBJ3LmCHlrPgxIYxFLs2taFX06WOD1bF4n9hx0xZPpbxt3Qw= IronPort-HdrOrdr: A9a23:+PWSU6s74xGnDaIUa3POFx4W7skDWdV00zEX/kB9WHVpmgXxrb HWoB19726TtN9xYgBFpTnkAsO9qBznhORICOUqTNWftWrdyQyVxeNZnOjfKlTbckWUltK1s5 0QDpSWYOeQMbEQt7eD3ODXKada/DBFysyVbCXlokuECWxRGsRdBstCZTpz23cZeDV7 X-IronPort-Anti-Spam-Filtered: true X-IronPort-AV: E=Sophos;i="5.88,326,1635199200"; d="scan'208";a="149722743" Received: from rwthex-s4-a.rwth-ad.de ([134.130.26.164]) by mail-in-3.itc.rwth-aachen.de with ESMTP; 29 Jan 2022 14:28:27 +0100 Received: from localhost (92.201.204.180) 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; Sat, 29 Jan 2022 14:28:26 +0100 Date: Sat, 29 Jan 2022 14:28:26 +0100 From: Magnus =?utf-8?B?R3Jvw58=?= To: Kees Cook CC: Andrew Morton , Alexey Dobriyan , Alexander Viro , Eric Biederman , , , Subject: Re: [PATCH] elf: Relax assumptions about vaddr ordering Message-ID: References: <202201260845.FCBC0B5A06@keescook> <202201262230.E16DF58B@keescook> <202201281347.F36AEA5B61@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline In-Reply-To: <202201281347.F36AEA5B61@keescook> User-Agent: Mutt/2.1.5 (31b18ae9) (2021-12-30) X-Originating-IP: [92.201.204.180] X-ClientProxiedBy: rwthex-s1-b.rwth-ad.de (2a00:8a60:1:e500::26:153) To rwthex-s4-a.rwth-ad.de (2a00:8a60:1:e500::26:164) X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 6290440002 X-Stat-Signature: pzjpomu13ojduq1ri4sgg4am1hw49fu5 X-Rspam-User: nil Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf01.hostedemail.com: domain of magnus.gross@rwth-aachen.de designates 134.130.5.48 as permitted sender) smtp.mailfrom=magnus.gross@rwth-aachen.de X-HE-Tag: 1643462909-383230 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 Fri, Jan 28, 2022 at 02:30:12PM -0800 Kees Cook wrote: > On Fri, Jan 28, 2022 at 09:26:09AM +0100, Magnus Gro=C3=9F wrote: > > 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 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 = segments: > > > > >=20 > > > > > Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Ali= gn > > > > > LOAD 0x000000 0x08000000 0x08000000 0x474585 0x474585 R E 0x1= 000 > > > > > LOAD 0x475000 0x08475000 0x08475000 0x090a4 0xc6c10 RW 0x1= 000 > > > > > LOAD 0x47f000 0x00010000 0x00010000 0x00000 0x7ff0000 0x1= 000 > > > > >=20 > > > > > Note how the last segment is actually the first segment and vic= e versa. > > > > >=20 > > > > > Since total_mapping_size() only computes the difference between= the > > > > > first and the last segment in the order that they appear, it wi= ll return > > > > > a size of 0 in this case, thus causing load_elf_binary() to fai= l, which > > > > > did not happen before that change. > > > > >=20 > > > > > Strictly speaking total_mapping_size() made that assumption alr= eady > > > > > before that patch, but the issue did not appear because the old > > > > > load_addr_set guards never allowed this call to total_mapping_s= ize(). > > > > >=20 > > > > > Instead of fixing this by reverting to the old load_addr_set lo= gic, 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_NOR= EPLACE") > > > > Cc: stable@vger.kernel.org > > > > Acked-by: Kees Cook > > >=20 > > > Andrew, can you pick this up too? > > >=20 > > > -Kees > > >=20 > >=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 Virt= ual > > Programming company have this kind of weird PT_LOAD ordering includin= g > > the famous Bioshock Infinite, so right now those games are all > > completely broken since Linux 5.16. > >=20 > > P.S.: Someone should probably ask Virtual Programming, what kind of > > tooling they use to create such convoluted ELF binaries. >=20 > Oh, actually, this was independently fixed: > https://lore.kernel.org/all/YVmd7D0M6G/DcP4O@localhost.localdomain/ >=20 > Alexey, you never answered by question about why we can't use a proper > type and leave the ELF_PAGESTART() macros alone: > https://lore.kernel.org/all/202110071038.B589687@keescook/ Oh sorry, I didn't see that there was already a patch floating around that fixed the issue, otherwise I would have not wasted so much time on debugging this. Oh well, doesn't matter now, I still learned a lot about kernel development and debugging with kgdb, I will probably be able to make some use of that knowledge in the future for another kernel patch. > > P.S.: Someone should probably ask Virtual Programming, what kind of > > tooling they use to create such convoluted ELF binaries. >=20 > Does "strings" provide any hints? :) It seems to be crosstool-ng (https://github.com/crosstool-ng/crosstool-ng): readelf -p .comment bioshock.i386 String dump of section '.comment': [ 0] GCC: (crosstool-NG 1.17.0) 4.6.3 [ 21] GCC: (Ubuntu 4.9.1-16ubuntu6) 4.9.1 [ 45] GCC: (Ubuntu 4.8.2-19ubuntu1) 4.8.2 Not sure though if crosstool-ng outputs these weird ELF binaries in general or if it's just a bug. -- Magnus > >=20 > > > >=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 = *filep, unsigned long addr, > > > > > static unsigned long total_mapping_size(const struct elf_phdr = *cmds, 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= _vaddr); > > > > > } > > > > > =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 >=20 > --=20 > Kees Cook