Received: by 2002:ab2:6991:0:b0:1f7:f6c3:9cb1 with SMTP id v17csp654548lqo; Wed, 8 May 2024 10:32:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVyivKmw712Or8XbBTwrCimWVvucE7y3G33Kejtzxuqn+/HTgOYeqQyJlsumgPDfcbLyl+rYe+HIBC9OI/1pwq3/m1qmRjDvWYxoEfj+Q== X-Google-Smtp-Source: AGHT+IGrlK/bojXYM9qOnyci89DW1uB71Stt6fFiJP92Ur4cApHMehY3skKjUTn7W0myOGr7rAbn X-Received: by 2002:aca:1308:0:b0:3c9:6c89:9a79 with SMTP id 5614622812f47-3c9852b7235mr3522144b6e.21.1715189566537; Wed, 08 May 2024 10:32:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715189566; cv=pass; d=google.com; s=arc-20160816; b=oEo6ZV02jBwNBO+u2HqOn89vog9Hgo4140VKGWvWrqqXb/zjbboPyirZ47CPuoXKKz Yrjlibg9pVyt3q4RbR2zIFaks3ZLOjppWQihmG4yFdGcGCPHtyoIUIGOtaixL+HvbLcD DznPrKYOISUecWG7dMkec07HKgm/0ylmC6+OtlwJO3mDB1Jkd9ucmtkXUiSLYkFdLbuU lMtgXD2ir4zNC0Lz85MNlgRf7F+s8ut+mEJU/O3WtkNK1EMlWaVPVdt7px/uWoaViNIA 4Bg/4yDcYUsHCxo84reYlNJpT43BAQBhJeglm1tcGNjLe/I4txU3u6H2B799pXn6B3T4 9bTQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=H2nNbu4XucHn8QDJu07t5inyAacTBVLvFhCSSSJI5uA=; fh=Sk1l5+lmqv2GRuMNvPzcz37nG090Q+btu21euMSvITM=; b=WaAGkaASuPFifoy8CrdvHMnUcsYVgtpzrPNsLRjLIx+yIV0wzHp4cD9x3RBgrkLASr yAzCX5o3JvY8FVmHNVococX2iPDkjw3VDDn2Ol84Lmp9LhStauUagtOs/CUw4wBAAqwl v/5iWmxs1Q2TT/oW9UAMvOLsA+MdBRfNaaP0OybVmAIkrN5jeMEsmHpzOgISdHcoNy4L Pk1sG/D1BvFI8J8HzVc5EvCCcFb48fRQb6LXvNkeHsj3moZORVfNjzb/UnS1JiTx+YNx 9ZqRGWmwYY5QCHUN6F8e1eHe9x1OY4PIaQzA+hVr73w4zToKkxsGbit1I2mLi64TDEV2 +b9w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MACZdoTV; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-173631-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173631-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id 14-20020a0562140dce00b006a09a76bc39si14116945qvt.557.2024.05.08.10.32.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:32:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-173631-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=MACZdoTV; arc=pass (i=1 spf=pass spfdomain=chromium.org dkim=pass dkdomain=chromium.org dmarc=pass fromdomain=chromium.org); spf=pass (google.com: domain of linux-kernel+bounces-173631-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-173631-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AAEB41C223EC for ; Wed, 8 May 2024 17:32:10 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6804512B154; Wed, 8 May 2024 17:31:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="MACZdoTV" Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58AD212AAE0 for ; Wed, 8 May 2024 17:31:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715189514; cv=none; b=paABcMsXydGcr/zOy29Bz1pWxkrTlcv98ZkV49UyfviUqHUqZV7JwgWXS9CeQmvS1XpnWboH+THeT/CKOUZotLHosQBhISMZn2tBVPXGTsOqdV6N724eouIbmN5oKh+vZDDVgdHKeRXhMfA2T90RgcgacpezFE6sMMR3YG0GMmY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715189514; c=relaxed/simple; bh=Lh9sl0zFqUcR9HG/AVuIcrH791BNMQBsy4Th3x5GAR4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mI13ptR7ZRuuqvUs3t7Vaae/uZsFHnUg0nWTjbqi+tGO1qqmeiJX6dbYGwdgGMlWxtLmEg3I4oXIaTRTN5WoGpQNoUTjyLPVQQO+7zQB3I2d/1/9IL4w35y6IvLQbCfZNCNvGQNnyZ+kv818RkTnPw/OSx/8K611N147XOXD12o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org; spf=pass smtp.mailfrom=chromium.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b=MACZdoTV; arc=none smtp.client-ip=209.85.210.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6f44ed6e82fso31876b3a.3 for ; Wed, 08 May 2024 10:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715189511; x=1715794311; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=H2nNbu4XucHn8QDJu07t5inyAacTBVLvFhCSSSJI5uA=; b=MACZdoTV2HUHmjzFIxVNITkDEIP/reg4vX76Uhvb9lBuevHVple9BEfpKtirjINHfC ozLFMsRvQOAwTeoTC7r7kQvLG3E1WmVmFQb7swbvC5hjfus9Ext9ybjvqwYi7HDw3UO9 LmF4A2l++QQ3D8CU72PDo1PylWW8adWkSEpIM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715189511; x=1715794311; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=H2nNbu4XucHn8QDJu07t5inyAacTBVLvFhCSSSJI5uA=; b=lqG4CYRVMAz5H6TLL/Pqk2ySQEd58sksNzchVeROMn7Fi730DpJlhZvMp7Wxw23PEP FqZTqPYQHF3+dCWYzCBZ8bgH115v04KhiGvmATFoux9F7kvReZ1lJjajz6Rymo1TWMLw sB6CvTbhX2AJeBOZRS0Kp0xpF4SJ1a+rdLjgJNT1uUJhP/fF99PAomVKNEeWeNuOKYbS Qrq6YRE+dcAI+DNKYugq8jkzuAEvFqGB7eG7cJ+6SdQLWSLRP9Rcu5NL9S1fv6mee9QY QE0IGhfm9vNjC6Ramy08tyOM+cj5r2+KuKUJ5N+yga+7SI+HTpD0TF8nllc6wtKe0x2e oX3A== X-Forwarded-Encrypted: i=1; AJvYcCVcYqRg9VuS9G2j3rrk5SNb0Ai3/JVoz1CsWBUq8pDHhEKwqvN3P2u3lag86co+owaixByCIf0r/v27U61MGQ2ocX4cNWymlMK8Bm44 X-Gm-Message-State: AOJu0YzcfFRniWZjvu00K/TJbXfkCnlWm48BPnSLSgeWmFRC1UXolcvN Jbs1dBHWyHLsPZ+PsoIfos56spy8h3NbDUrdPOGtaHdnoOo6+RiH2oJJB0l+BA== X-Received: by 2002:a05:6a00:21d0:b0:6ec:f28b:659f with SMTP id d2e1a72fcca58-6f49c207395mr4060894b3a.3.1715189510677; Wed, 08 May 2024 10:31:50 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id e18-20020a656492000000b006089cf2cde5sm10277706pgv.26.2024.05.08.10.31.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:31:50 -0700 (PDT) From: Kees Cook To: "H . J . Lu" Cc: Kees Cook , Chris Kennelly , Eric Biederman , Shuah Khan , Muhammad Usama Anjum , John Hubbard , Fangrui Song , Andrew Morton , Yang Yingliang , Mike Rapoport , Rui Salvaterra , Victor Stinner , Jan Palus , Al Viro , Christian Brauner , Jan Kara , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/3] binfmt_elf: Calculate total_size earlier Date: Wed, 8 May 2024 10:31:47 -0700 Message-Id: <20240508173149.677910-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240508172848.work.131-kees@kernel.org> References: <20240508172848.work.131-kees@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3041; i=keescook@chromium.org; h=from:subject; bh=Lh9sl0zFqUcR9HG/AVuIcrH791BNMQBsy4Th3x5GAR4=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmO7cEHn0buAr/C675Nd09yTzC5UND2JGNtAzn6 7fAhDpYVyGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZju3BAAKCRCJcvTf3G3A JpCtD/9LYmlihZG7ywOzTinHJzY1kGMdJPl8aorBC0w8BZYWZG2L5735ch2DLxaqLeucHcOdIzd j2P5KQxKEFi3VSlTEJgG/CHFTE/LdNWf8K4HNiSgIPRvftdZy0yfsLhEg6c1Bc5+jOOGN7Yw1Oz 6PcT0O2RfYEkFZSrUDCPgVVNxk1+jVVG6NCQXKg/g1beIDLLZRXDt/CujNa4YHVUpd/PYIZvsBI tyyKrW1A2QVx0+ktTAviklFaU/7UJYQBntwA7dredHuOtT/fqjgzVNTigROk/o3oX6LwbM3R/nS rpbycTLj73Gn4W97wBLGOx8O9yk/tSZARqYHaJgCzAFLUSp6KzKoPZby1r7csVaek1B3X8fP+8+ BkY4XLEZdalGU8cLbxI+PY/Qr9tF2wa2MjCxf53MHCqPc76J9sF8E993yLTDuBhej9jyYD3tQ8f szKoB0M+V2j30jykwU46I56obv/BNssOUb7HZuY3opJ8Mjr05I2VVwIQkqlyrX5+S5i6mvqRIui +CwG4y1jlqtzUR5IZywuhfiJ1adpU8sxNVbg6dRC8xSQ5BOBZyf262o/4gaFHzFjKf5iPULmnZ1 SvfgCjHzQ3eQ8HeL0WrvMnveAWiwczVNHiEsljH0SLouDc9W4fSSSyePhi6mu7GKCpxPtjxXxWH YPVMgoHtsFF9p3A== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 Content-Transfer-Encoding: 8bit In preparation to support PT_LOAD with large p_align values on non-PT_INTERP ET_DYN executables (i.e. "static pie"), we'll need to use the total_size details earlier. Move this separately now to make the next patch more readable. As total_size and load_bias are currently calculated separately, this has no behavioral impact. Signed-off-by: Kees Cook --- fs/binfmt_elf.c | 52 +++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 5397b552fbeb..56432e019d4e 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1061,7 +1061,34 @@ static int load_elf_binary(struct linux_binprm *bprm) * Header for ET_DYN binaries to calculate the * randomization (load_bias) for all the LOAD * Program Headers. + */ + + /* + * Calculate the entire size of the ELF mapping + * (total_size), used for the initial mapping, + * due to load_addr_set which is set to true later + * once the initial mapping is performed. + * + * Note that this is only sensible when the LOAD + * segments are contiguous (or overlapping). If + * used for LOADs that are far apart, this would + * cause the holes between LOADs to be mapped, + * running the risk of having the mapping fail, + * as it would be larger than the ELF file itself. * + * As a result, only ET_DYN does this, since + * some ET_EXEC (e.g. ia64) may have large virtual + * memory holes between LOADs. + * + */ + total_size = total_mapping_size(elf_phdata, + elf_ex->e_phnum); + if (!total_size) { + retval = -EINVAL; + goto out_free_dentry; + } + + /* * There are effectively two types of ET_DYN * binaries: programs (i.e. PIE: ET_DYN with INTERP) * and loaders (ET_DYN without INTERP, since they @@ -1102,31 +1129,6 @@ static int load_elf_binary(struct linux_binprm *bprm) * is then page aligned. */ load_bias = ELF_PAGESTART(load_bias - vaddr); - - /* - * Calculate the entire size of the ELF mapping - * (total_size), used for the initial mapping, - * due to load_addr_set which is set to true later - * once the initial mapping is performed. - * - * Note that this is only sensible when the LOAD - * segments are contiguous (or overlapping). If - * used for LOADs that are far apart, this would - * cause the holes between LOADs to be mapped, - * running the risk of having the mapping fail, - * as it would be larger than the ELF file itself. - * - * As a result, only ET_DYN does this, since - * some ET_EXEC (e.g. ia64) may have large virtual - * memory holes between LOADs. - * - */ - total_size = total_mapping_size(elf_phdata, - elf_ex->e_phnum); - if (!total_size) { - retval = -EINVAL; - goto out_free_dentry; - } } error = elf_load(bprm->file, load_bias + vaddr, elf_ppnt, -- 2.34.1