From patchwork Wed May 8 17:31:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13658951 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 3AF9DC19F4F for ; Wed, 8 May 2024 17:31:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BAC36B0099; Wed, 8 May 2024 13:31:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 943756B009A; Wed, 8 May 2024 13:31:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7BC456B009B; Wed, 8 May 2024 13:31:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5AB8C6B0099 for ; Wed, 8 May 2024 13:31:54 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C544F1411C8 for ; Wed, 8 May 2024 17:31:53 +0000 (UTC) X-FDA: 82095921306.24.6040A61 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) by imf16.hostedemail.com (Postfix) with ESMTP id BDCE4180017 for ; Wed, 8 May 2024 17:31:51 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bqCwTY3f; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf16.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.182 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715189511; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H2nNbu4XucHn8QDJu07t5inyAacTBVLvFhCSSSJI5uA=; b=pXOblB9NWc6/2tvVhH+lU9is9OsojdScHyYicBluzHA/Vy35oZ2Wr07JNLVaashOEZpGdi qgxmV7PicgkPKRUFIzGr3W4SPIvcwMkeuf4RwxGsRt3ATUAtkenw/zh73h9xufZTXCaDr1 gXRGoDAfzciJGGW2V59Odk9KRZXCYnc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715189511; a=rsa-sha256; cv=none; b=MLBMJV8bYSuFDomCO3X9+WBS2rrruhd2ha/kJ9hdjfmJDc0aQWdK9XnMe8RvjU/rAOdCGP 2ZqmCLSm7cNCcPsU8w6TlbQ04jwm3rqht7xrNPprgD0Iu6qDg3bh4l64nUOJiyYY4HxxV8 f6iVMCz8pYB1rOiYu+uZGxCKKzFp17k= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=bqCwTY3f; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf16.hostedemail.com: domain of keescook@chromium.org designates 209.85.210.182 as permitted sender) smtp.mailfrom=keescook@chromium.org Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-6f467fb2e66so39098b3a.1 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=kvack.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=bqCwTY3fx1rljvDxaeeOwbgDfO9Smq3f/xjnv64jGnFOy+fJuPD3aC1v/0jj9ZaQV/ x/GZX6U4qbnj1BFP1pAV/3l4/4EFu68QfiPhNDTeZcYrcPxIbMfR+LBzZSjgbhcuM8o1 6xp4NYiMME7u1vuRETHgGEzgEdYSJJ/9C20kY= 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=wbmnQSUz9koLzOKJQOADLCFDhggRo9jWLAoUwf8lhPX+7qSx33X1iZVPBF+G3orZ/H ZrhGdVwi/9HFGs0j+XOY5VwuQiRV+g8SWQM4kMZP+7qTVhTiIRoICSyMogVeDTOTjfmM kFrgZln6h/ALnfP0stK/yOufaEJzF6RC7WpyXOyR5QbCZHLLlqj/8HlRrNwzCE4QteYD Zf+e589cNPa3AbaY5wtoOfTW3+2HC5RXQczlDDBkIAy81593MxMHgaZe5UEk39BksWNs B11KatVXLAa2QKtJVA473v62qYL3J1LYbjy5uo1ZzBU88gHR64aOCh4APpvljsQL3YaK gFgg== X-Forwarded-Encrypted: i=1; AJvYcCVaIoC99DXCJPiCI/vCbFWm/QAg+Xd+DGEmZEGlMoD+QXzS2TwG922Qft67sLv36Fctxe+l5uTAmSWQiG4tMKxjEOw= X-Gm-Message-State: AOJu0Yxl9aH/DoWi5qVz6qXrDtlaYJuUUN4EBSn3wALNznOhuvyEYCHj iOfUlCix01Tv3vfeN5vVdEtdIe0Br9ez08AqrX+C3ViCIZZzUQ8RX4OFgzaFLA== X-Google-Smtp-Source: AGHT+IHhC2MOnPhfcqxPojENxgcZ8XwUYRxVGdudDH8TMRtD2+Tt4ejx6OP4QShOQ0NN1c/LqX7HRQ== 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> 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 X-Rspamd-Queue-Id: BDCE4180017 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: hyfqigc1w1o3etuxe5zgceue4xni8s1y X-HE-Tag: 1715189511-969493 X-HE-Meta: U2FsdGVkX1/6KvrA+JYPKiE2eJaWFcgvKNPTBpSQN30XBW2Bity0ayEOj9DvS4E0B5pKIFFgvd+mzMyO9Ycc6YTW9t+32dCRTL43b72wJPrd6tffjkhRfkrkbSP4ulyKeYCfjeq7AKpjJS4JYwMj6e1k796EkUytTxdvHQUWRypEI+8uqzB1i6YBtcOR0ZhYdcXxEWmvojhZnYjkmJuTDE2q254d8x9YJn+0TBI38tU07zHyTuWZX+BMuS32Hrv6q7OavvvgfngzC7LPG9n9thTCOC+px3SaSbwgNWQogPVLCOsCUZHfzvD91OY0eVpWBSgbPbup1K6p60bWsKCiNcbMRHztMPWGjb7jifjiTaUz+SW/AsZ3Fo9bQnS/WEMkT1EtwsgOrC0y/lOHBnNyUvW4gOYDf5wKhJboGZUUf6v/i70GAqgF+dFk3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ity: 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: List-Subscribe: List-Unsubscribe: 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,