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 EBAE0C433EF for ; Fri, 28 Jan 2022 22:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 615076B00EC; Fri, 28 Jan 2022 17:30:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C4466B00EF; Fri, 28 Jan 2022 17:30:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 43E826B00F1; Fri, 28 Jan 2022 17:30:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0197.hostedemail.com [216.40.44.197]) by kanga.kvack.org (Postfix) with ESMTP id 2DB346B00EC for ; Fri, 28 Jan 2022 17:30:15 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id E6A0882D6C22 for ; Fri, 28 Jan 2022 22:30:14 +0000 (UTC) X-FDA: 79081140348.06.271DE8B Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by imf23.hostedemail.com (Postfix) with ESMTP id 818D2140005 for ; Fri, 28 Jan 2022 22:30:14 +0000 (UTC) Received: by mail-pg1-f171.google.com with SMTP id s16so6381343pgs.13 for ; Fri, 28 Jan 2022 14:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=UQU/zzOIGiyUM8/XLeItcnOhkfwjpiwMD0bTk1M1h+c=; b=jT2/6eWTnRxYOlLDgGUVm6GoKxvh3ihMwsjZsge72KQr5rO94AiWzyRB9S/F8+nezH 6GNP+mG2ncB+2c9SeTf952+m26365xklIWFgbK0QJs4L9rcSCV3zREErS/hnjU+ESu6o zs9dPkuxEA4/h44VByiEciXRGX+0o6P4CHv1M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to; bh=UQU/zzOIGiyUM8/XLeItcnOhkfwjpiwMD0bTk1M1h+c=; b=6hbkN1LB4qx9qSMiFmvmzqmIkHwE2v2Ss2u1v0RBurp0eUzL6rWDyaw1W1fvYMzfZC 1+CT5qbNz22bJ8Gd1BgrZrwCb1yjjV6EMGjcusNKR9lWagdP6qWB6H8Ov6ESvNBVLNFW TqvWAV4HopOcty6do6N7OXakBiWTdaMJfsyVa3SH5mvj2w2KqGLy4YD3PtXIOw5MSlwQ cGvy0wPcRBaX9Pa6YoG5WOKEOqiar/sPFpu8k1gs7GiyHQuh2eL3v0Phall0BtCghAJA OnfNHaj1qPgjwJGn2pBcIxKS8B8FBF23Oy7Vr9VEkLZrL4WPlr29b9/2EM+ovEKbWsN+ zv2w== X-Gm-Message-State: AOAM530TUrw2mkB0SQw1I7P4AsT3IJqMr/Ol3imYfFl44QZGINE8C77J hnvUm81G1zGIoikD3xEjoBs02w== X-Google-Smtp-Source: ABdhPJyUHOWf/aLPEpgdmUJXWIEYc1ZsCgRh+fo8EE0lsoiW4r9GmsIXssoGzqEE853Xkwa5k4vN8g== X-Received: by 2002:a63:4e58:: with SMTP id o24mr8147584pgl.374.1643409013479; Fri, 28 Jan 2022 14:30:13 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id g18sm3251898pju.7.2022.01.28.14.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 14:30:13 -0800 (PST) Date: Fri, 28 Jan 2022 14:30:12 -0800 From: Kees Cook To: Andrew Morton , Alexey Dobriyan 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 Subject: Re: [PATCH] elf: Relax assumptions about vaddr ordering Message-ID: <202201281347.F36AEA5B61@keescook> References: <202201260845.FCBC0B5A06@keescook> <202201262230.E16DF58B@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 818D2140005 X-Rspam-User: nil Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b="jT2/6eWT"; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf23.hostedemail.com: domain of keescook@chromium.org designates 209.85.215.171 as permitted sender) smtp.mailfrom=keescook@chromium.org X-Stat-Signature: xcnfe345o61ftzoom5k9dn6tzgyeryy8 X-HE-Tag: 1643409014-596754 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 09:26:09AM +0100, Magnus Gro=DF 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=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 n= ow > > > > assumes that PT_LOAD segments are ordered by vaddr in load_elf_bi= nary(). > > > >=20 > > > > Specifically consider an ELF binary with the following PT_LOAD se= gments: > > > >=20 > > > > Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align > > > > LOAD 0x000000 0x08000000 0x08000000 0x474585 0x474585 R E 0x100= 0 > > > > LOAD 0x475000 0x08475000 0x08475000 0x090a4 0xc6c10 RW 0x100= 0 > > > > LOAD 0x47f000 0x00010000 0x00010000 0x00000 0x7ff0000 0x100= 0 > > > >=20 > > > > Note how the last segment is actually the first segment and vice = versa. > > > >=20 > > > > Since total_mapping_size() only computes the difference between t= he > > > > first and the last segment in the order that they appear, it will= return > > > > a size of 0 in this case, thus causing load_elf_binary() to fail,= which > > > > did not happen before that change. > > > >=20 > > > > Strictly speaking total_mapping_size() made that assumption alrea= dy > > > > before that patch, but the issue did not appear because the old > > > > load_addr_set guards never allowed this call to total_mapping_siz= e(). > > > >=20 > > > > Instead of fixing this by reverting to the old load_addr_set logi= c, 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_NOREP= LACE") > > > 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 Virtua= l > 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. >=20 > P.S.: Someone should probably ask Virtual Programming, what kind of > tooling they use to create such convoluted ELF binaries. Oh, actually, this was independently fixed: https://lore.kernel.org/all/YVmd7D0M6G/DcP4O@localhost.localdomain/ 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/ I still don't like the use of "int" in ELF_PAGESTART(), but I agree it shouldn't cause a problem. I just really don't like mixing a signed type with address calculations, from a robustness perspective. Andrew, can you update elf-fix-overflow-in-total-mapping-size-calculation= .patch to include: Fixes: 5f501d555653 ("binfmt_elf: reintroduce using MAP_FIXED_NOREPLACE") Cc: stable@vger.kernel.org Acked-by: Kees Cook Thanks! -Kees >=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 *f= ilep, unsigned long addr, > > > > static unsigned long total_mapping_size(const struct elf_phdr *c= mds, 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_v= addr); > > > > } > > > > =20 > > > > static int elf_read(struct file *file, void *buf, size_t len, lo= ff_t pos) > > > > --=20 > > > > 2.34.1 > > >=20 > > > --=20 > > > Kees Cook > >=20 > > --=20 > > Kees Cook --=20 Kees Cook