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 37F0FC433FE for ; Fri, 28 Jan 2022 20:04:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB0696B00D6; Fri, 28 Jan 2022 15:04:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A38BE6B00FC; Fri, 28 Jan 2022 15:04:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B2AA6B00FE; Fri, 28 Jan 2022 15:04:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id 755A36B00D6 for ; Fri, 28 Jan 2022 15:04:26 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 2E4D496F0F for ; Fri, 28 Jan 2022 20:04:26 +0000 (UTC) X-FDA: 79080772932.15.B6BD95B Received: from mail-pg1-f172.google.com (mail-pg1-f172.google.com [209.85.215.172]) by imf14.hostedemail.com (Postfix) with ESMTP id B7A6C10000F for ; Fri, 28 Jan 2022 20:04:25 +0000 (UTC) Received: by mail-pg1-f172.google.com with SMTP id s16so6025398pgs.13 for ; Fri, 28 Jan 2022 12:04:25 -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=Ow6eT8rr/7d9xpi/r2pDaVO8JLSWc0gIq0t6DC0fz0U=; b=CFNLBB0HiS0aAgD12+s0IyhU2V9a9fHe6nueloJjET0XtN/Ydnat2VpccyE9jrb98d E0ccBDqoGU9NNrFwtNYXGqYV9WIZNIkSpMTqJX38g9O+67siB5WRBQFQvjK2pKw8A1I6 ExWacafu/haB8iGYULV7HeWoe0SOFnXBk59Hk= 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=Ow6eT8rr/7d9xpi/r2pDaVO8JLSWc0gIq0t6DC0fz0U=; b=nFQ7MDS5U4uyCCF/1/gii2M+x4mLzE4b+t6Bfhjr1REeBKkWFP99TJNYeTEdrMKjL+ Ru2I7EAwihlt0gxbsB2WXPEYZhRwHohK89cr6O8PHkF5Eyrdge6nPCeGg1u1hfC4+EDu 4y+tNIXF3t9RFWzKE9nC4TtoEodSrBOgBeyhCXvgA11/VZze9OS+bVXtKcmeFp0dE8MQ 2Mjku0uLCHLfoB2fgpTa1BxxBJvOkaY+KtLPndStqi0+weRwMl7FeBLFN1c+u2NyWJ4c mHSRePxaXn/Pf89k0U5ys1NbtqryF+rddQlz5+tcDV3/8HixZRhhj/JlOa/XgAPujexo nfNQ== X-Gm-Message-State: AOAM530Xlcq2BAn7BMORfkkRZDkGGFCycUxg56wsBhoeA8Yzh+B27fvt OHMepm+z1uC1HptIwJH6S8KWrYEE1cpQZw== X-Google-Smtp-Source: ABdhPJwoQipIAMdr1PZ2Swd9KmfITLuqOTCVITAwBcqCDdESvWM2724Lmp0TZ+9ryG0UTXwuVir+Ug== X-Received: by 2002:a05:6a00:1709:: with SMTP id h9mr9467158pfc.23.1643400264496; Fri, 28 Jan 2022 12:04:24 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id x17sm9564865pfu.135.2022.01.28.12.04.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 12:04:24 -0800 (PST) Date: Fri, 28 Jan 2022 12:04:23 -0800 From: Kees Cook To: Magnus =?iso-8859-1?Q?Gro=DF?= Cc: Andrew Morton , 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: <202201281202.C494BAB574@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: rspam09 X-Rspamd-Queue-Id: B7A6C10000F X-Stat-Signature: gxmmtr6jqaepk691duhegm486bdkrhko X-Rspam-User: nil Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=CFNLBB0H; spf=pass (imf14.hostedemail.com: domain of keescook@chromium.org designates 209.85.215.172 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org X-HE-Tag: 1643400265-347059 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. Thanks for additional rationale! I included the field that would be expected to have this picked up for the 5.16 stable tree (the "Fixes: ..." and "Cc: stable@vger.kernel.org") so once it lands in Linus's tree, it'll get picked up for the v5.16.z series too. > P.S.: Someone should probably ask Virtual Programming, what kind of > tooling they use to create such convoluted ELF binaries. Does "strings" provide any hints? :) --=20 Kees Cook