From patchwork Wed May 8 17:31:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13658953 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 52B53C19F4F for ; Wed, 8 May 2024 17:31:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E761D6B009A; Wed, 8 May 2024 13:31:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DD8486B009E; Wed, 8 May 2024 13:31:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A0AD86B009A; Wed, 8 May 2024 13:31:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6D1EF6B009B for ; Wed, 8 May 2024 13:31:55 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 336FEA0C71 for ; Wed, 8 May 2024 17:31:55 +0000 (UTC) X-FDA: 82095921390.07.BA1DC36 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf22.hostedemail.com (Postfix) with ESMTP id 2F53DC0018 for ; Wed, 8 May 2024 17:31:51 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=L0Hnd5hC; spf=pass (imf22.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715189512; 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=ckhcb2/0RYF0Gnhe/jQlCjJECN0VZ08/Fvs6ZKfOACc=; b=aQm1EUlVORl0pTRoxAWiUhw58f0dzdafbyu+LyMIj3iOTJt7+Xg1JeAThXldDL028cx1Sj shxGTQsjLMRlddxY90o89dLoAKG15jrZWeJEKFn8oY6jgk3fJVPbu7sj90iBwGzCW8HNEd aC8IEZEWsqwm9v+QO3scIl9I124TItg= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=L0Hnd5hC; spf=pass (imf22.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.182 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715189512; a=rsa-sha256; cv=none; b=6Kw8xdL1GrESYsdPmgIEve1KmKt1OY7gOIlvJq4aFc+qeuEA1j4Wo+KDhp+znglZN5COZo KKs7AoOrSbx3Vq0rT5TBemvMVZYGDT3E5VqJ4K2MOEXS9hPfGOb8rVx7p8NYuI+Hj3xSL0 EG9c19m/gRyvm2kDphUlXAGHVEvP4dI= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1ee38966529so8246315ad.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=ckhcb2/0RYF0Gnhe/jQlCjJECN0VZ08/Fvs6ZKfOACc=; b=L0Hnd5hCTjGbwH4xwPArEqyz5btXwvS7RsCaz6lXcV+C2zzHta+wrWMhBeiI0FJHsm QlS25wEDEKGlrSFN3YJ1Fs0SbmJ99LcGtgRAHI466UsJUP5E1O8eN0Wn/vPQ24VAACCU Obcb7A7tFCnJo6fJMZbN2cFDtKVC/+2MqhcbY= 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=ckhcb2/0RYF0Gnhe/jQlCjJECN0VZ08/Fvs6ZKfOACc=; b=QDQ9p348e5apxdSfCfbui1tFbd1b+00GSyuNpReR8P/LWdLKheMmA0rnHI2ge+EoAG 07zdFXeuvhaTuWgticbdO/APjCSAqpyvnul8Sc0I69ee9oERGg2lOiaVMZDZykm0WVsZ kAs/bbcRXBmRx7Q6fwiUeTBkjOdHwwaGjTaj0+Sd1TtP8oQXuBEWsJbowGle0As/t+YG UEj9QS7zAhR19kb9QizRh5al6jGuLFqiEYdiu84ZJaM31ImAcmxFbV0/cF9IDD7EKDTw /ewBRY93nK+AttW9erZzw6Vf6gpuVJ0+4iftSl8kXc77sH1Oq842NfzI2j0oGQHat1zK o1vA== X-Forwarded-Encrypted: i=1; AJvYcCWoYrczpM3r0p+JTgTPOLuk+x6soZw53/xK0wF2A0Wo2NlNJfBHHEnb4+XUmeA3LfB58WDKmqb4jCyAQ2OHRbFo5Gk= X-Gm-Message-State: AOJu0YyPwIdRBmT5z7CyWRGrsa44uS0X8AMSWz4KRuLHVCzO4EIDVnaB z0RrOr5za1MQmFHlq7HUoeCFD9vv+JgXclCfDdq8jdXjBEojaIHdqupYl20BuA== X-Google-Smtp-Source: AGHT+IFe9RE0PlweeOYjLwef5REO+i00c2oo7HJiryhYfaXZtd4BvtB2nxrgXXbdGr6zE8h3r5g01g== X-Received: by 2002:a17:902:dacc:b0:1eb:8299:db35 with SMTP id d9443c01a7336-1eefa58cfe5mr4314535ad.32.1715189510956; 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 b3-20020a170902d30300b001e2a479954dsm12129520plc.181.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 , linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Mike Rapoport , Rui Salvaterra , Victor Stinner , Jan Palus , Al Viro , Christian Brauner , Jan Kara , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/3] selftests/exec: Build both static and non-static load_address tests Date: Wed, 8 May 2024 10:31:46 -0700 Message-Id: <20240508173149.677910-1-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=6882; i=keescook@chromium.org; h=from:subject; bh=EVV19YgGbcPQsIHd5yk4GzGYcoILxXyLnNlnQWFy9pw=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmO7cE7X4mhGALtYnO3LLjThfOhTf4zqWRzUonM YIEVDy8ECGJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZju3BAAKCRCJcvTf3G3A JqzhEACugoYdfeBq9hllOhN230YzjegmLLmyhu7D4ZUW4opBdmlFfiuiuoZexZQUxlhuR0aHobi Xktw+k9RzRwGjeUQm6DqEFJ5/ypq1MYaS2e7qfeSwkLfsLSeRGnn2N3Ag6eXL/e/cXhkcQTPo9b Vfd0+o5ctjpzD9SDvJKtjz8hG69soCmtgK6rJyovDQslCzC7aDH33Fvri9GB9lyD1Miafv0AZEA 9H8+/ycD1h+4dKxrn5h5DP7ZMGEhcGDSz8BTtvTibEaIEJNsIVsPkfb0FySoNeRpgIlKOcynYKm W5rADoIiq2Js477XNNWD8awi1czVmZBgBP7vXuSdmgQki7LmPUyd3uBH+xc4wZJX7YqTzuU2p6f 0kCwpXgkwxz5lVtdItPdl4q7RHdCBAungFLFpCqNvhxfnOjp4EeOQcPZWWrBJ4VRM6JnLz6AyBi oaadMihDqXQ3DpElP4hIeED20RpP6RnOoxv9NQPTDNLVryc+aqXTTJyaNVDhjFGTZ3lDDrt2YQJ BJuugxHoIMXKzmxjqagdcHEkkEQc3QT+0a7jsiUyJoFqb18rRzPbTRz3p5f5pMt+fBN/tbQxnfY gKd6t/Mj+QNdLzLoBnE1r39sCfnDiklNPn3+eQskJ8fUaFsewNAWbhyjVVux6wK3OnQrrcooaNB Agj16aAcMuOgCnA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Stat-Signature: oanw4k3xj1hayu1o5baiuitcw5gy9c7m X-Rspam-User: X-Rspamd-Queue-Id: 2F53DC0018 X-Rspamd-Server: rspam05 X-HE-Tag: 1715189511-741271 X-HE-Meta: U2FsdGVkX1+nTqlZVuytgA9vMu+D674EjKxSMb7tQdEzVRxBmRGVi7xd1bs1/OP47raUoOXaUDHH4lxM0RD6kMh1n8rlAlbHhPmspk7RcC5tuaeWK37taHYyygi9/sCJuBIwOYCwypaCtXTr5WGVuqtT7Q1GSVWL0f8HaqBDSlnyxkkBmuyZICupnRnks/zQVxlO1gIU5yjaaNW39Te9oNuixYqXeX9neHz2eZB5dEwTx82u2XeeN4EaTLnziLGBInt6vBmowl/ayjiivmfli/UEgx6fNc3RnjngmEyamMrKMHVhqI+C98TlA7w3m6GeYTLZGkJT1N201Orh8Pa5gVlTzTHH5dooW0TJxvbm9pfKSSk50Yx9UXzQqPhpPcO1lF0CmaB5dqoai8WYYgGfQ5yaqIxqiLXUqyB8mdHfu8b0EaSGWrrmW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DuQS2ocqLip266GFk7ZtwABRdzWbTovdes80YjXyGfIQ2N8HrWPCq6k6eXBZo/3epC75qPwlzWJcMpCl7Imfww3NZWOY028Ctqqq02wsNMAa0ymPC5rllsW87UyBXoxLU9CY4lvW//pDnr5oryWTsRHYOMc2a+o2OzNpI1R+Nl/uvulCqifbztW7eiGHl4yAmWe8SGLqXc+n1gOghHUHE5y5YPGSWua0Ox/hL03DGrkf4q/jXHGOK0H4KjQDG6BqvPsS8gVlItsp/J6mNWTQ5kA7RaS9fiYrAJ5GaeBsrxEtw0lwXoslF5CnLXyZURbG9pOIFIOXA3keFXS7Mthet4q0eoP3IZmQTuCW7NApZjuhm2exkNyFyQ34AlVVy 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: List-Subscribe: List-Unsubscribe: After commit 4d1cd3b2c5c1 ("tools/testing/selftests/exec: fix link error"), the load address alignment tests tried to build statically. This was silently ignored in some cases. However, after attempting to further fix the build by switching to "-static-pie", the test started failing. This appears to be due to non-PT_INTERP ET_DYN execs ("static PIE") not doing alignment correctly, which remains unfixed[1]. See commit aeb7923733d1 ("revert "fs/binfmt_elf: use PT_LOAD p_align values for static PIE"") for more details. Provide rules to build both static and non-static PIE binaries, improve debug reporting, and perform several test steps instead of a single all-or-nothing test. However, do not actually enable static-pie tests; alignment specification is only supported for ET_DYN with PT_INTERP ("regular PIE"). Link: https://bugzilla.kernel.org/show_bug.cgi?id=215275 [1] Signed-off-by: Kees Cook --- Cc: Chris Kennelly Cc: Eric Biederman Cc: Shuah Khan Cc: Muhammad Usama Anjum Cc: John Hubbard Cc: Fangrui Song Cc: Andrew Morton Cc: Yang Yingliang Cc: linux-mm@kvack.org Cc: linux-kselftest@vger.kernel.org --- tools/testing/selftests/exec/Makefile | 19 +++--- tools/testing/selftests/exec/load_address.c | 67 +++++++++++++++++---- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile index fb4472ddffd8..619cff81d796 100644 --- a/tools/testing/selftests/exec/Makefile +++ b/tools/testing/selftests/exec/Makefile @@ -3,8 +3,13 @@ CFLAGS = -Wall CFLAGS += -Wno-nonnull CFLAGS += -D_GNU_SOURCE +ALIGNS := 0x1000 0x200000 0x1000000 +ALIGN_PIES := $(patsubst %,load_address.%,$(ALIGNS)) +ALIGN_STATIC_PIES := $(patsubst %,load_address.static.%,$(ALIGNS)) +ALIGNMENT_TESTS := $(ALIGN_PIES) + TEST_PROGS := binfmt_script.py -TEST_GEN_PROGS := execveat load_address_4096 load_address_2097152 load_address_16777216 non-regular +TEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS) TEST_GEN_FILES := execveat.symlink execveat.denatured script subdir # Makefile is a run-time dependency, since it's accessed by the execveat test TEST_FILES := Makefile @@ -28,9 +33,9 @@ $(OUTPUT)/execveat.symlink: $(OUTPUT)/execveat $(OUTPUT)/execveat.denatured: $(OUTPUT)/execveat cp $< $@ chmod -x $@ -$(OUTPUT)/load_address_4096: load_address.c - $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x1000 -pie -static $< -o $@ -$(OUTPUT)/load_address_2097152: load_address.c - $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x200000 -pie -static $< -o $@ -$(OUTPUT)/load_address_16777216: load_address.c - $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=0x1000000 -pie -static $< -o $@ +$(OUTPUT)/load_address.0x%: load_address.c + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \ + -fPIE -pie $< -o $@ +$(OUTPUT)/load_address.static.0x%: load_address.c + $(CC) $(CFLAGS) $(LDFLAGS) -Wl,-z,max-page-size=$(lastword $(subst ., ,$@)) \ + -fPIE -static-pie $< -o $@ diff --git a/tools/testing/selftests/exec/load_address.c b/tools/testing/selftests/exec/load_address.c index 17e3207d34ae..8257fddba8c8 100644 --- a/tools/testing/selftests/exec/load_address.c +++ b/tools/testing/selftests/exec/load_address.c @@ -5,11 +5,13 @@ #include #include #include +#include #include "../kselftest.h" struct Statistics { unsigned long long load_address; unsigned long long alignment; + bool interp; }; int ExtractStatistics(struct dl_phdr_info *info, size_t size, void *data) @@ -26,11 +28,20 @@ int ExtractStatistics(struct dl_phdr_info *info, size_t size, void *data) stats->alignment = 0; for (i = 0; i < info->dlpi_phnum; i++) { + unsigned long long align; + + if (info->dlpi_phdr[i].p_type == PT_INTERP) { + stats->interp = true; + continue; + } + if (info->dlpi_phdr[i].p_type != PT_LOAD) continue; - if (info->dlpi_phdr[i].p_align > stats->alignment) - stats->alignment = info->dlpi_phdr[i].p_align; + align = info->dlpi_phdr[i].p_align; + + if (align > stats->alignment) + stats->alignment = align; } return 1; // Terminate dl_iterate_phdr. @@ -38,27 +49,57 @@ int ExtractStatistics(struct dl_phdr_info *info, size_t size, void *data) int main(int argc, char **argv) { - struct Statistics extracted; - unsigned long long misalign; + struct Statistics extracted = { }; + unsigned long long misalign, pow2; + bool interp_needed; + char buf[1024]; + FILE *maps; int ret; ksft_print_header(); - ksft_set_plan(1); + ksft_set_plan(4); + + /* Dump maps file for debugging reference. */ + maps = fopen("/proc/self/maps", "r"); + if (!maps) + ksft_exit_fail_msg("FAILED: /proc/self/maps: %s\n", strerror(errno)); + while (fgets(buf, sizeof(buf), maps)) { + ksft_print_msg("%s", buf); + } + fclose(maps); + /* Walk the program headers. */ ret = dl_iterate_phdr(ExtractStatistics, &extracted); if (ret != 1) ksft_exit_fail_msg("FAILED: dl_iterate_phdr\n"); - if (extracted.alignment == 0) - ksft_exit_fail_msg("FAILED: No alignment found\n"); - else if (extracted.alignment & (extracted.alignment - 1)) - ksft_exit_fail_msg("FAILED: Alignment is not a power of 2\n"); + /* Report our findings. */ + ksft_print_msg("load_address=%#llx alignment=%#llx\n", + extracted.load_address, extracted.alignment); + + /* If we're named with ".static." we expect no INTERP. */ + interp_needed = strstr(argv[0], ".static.") == NULL; + + /* Were we built as expected? */ + ksft_test_result(interp_needed == extracted.interp, + "%s INTERP program header %s\n", + interp_needed ? "Wanted" : "Unwanted", + extracted.interp ? "seen" : "missing"); + + /* Did we find an alignment? */ + ksft_test_result(extracted.alignment != 0, + "Alignment%s found\n", extracted.alignment ? "" : " NOT"); + + /* Is the alignment sane? */ + pow2 = extracted.alignment & (extracted.alignment - 1); + ksft_test_result(pow2 == 0, + "Alignment is%s a power of 2: %#llx\n", + pow2 == 0 ? "" : " NOT", extracted.alignment); + /* Is the load address aligned? */ misalign = extracted.load_address & (extracted.alignment - 1); - if (misalign) - ksft_exit_fail_msg("FAILED: alignment = %llu, load_address = %llu\n", - extracted.alignment, extracted.load_address); + ksft_test_result(misalign == 0, "Load Address is %saligned (%#llx)\n", + misalign ? "MIS" : "", misalign); - ksft_test_result_pass("Completed\n"); ksft_finished(); } 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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c5fwG0srO9WPMkUuewy3fjJlORU86rPn+IpcbqXERFhrk/eFyLx1OlFDuN4n23tNdW94ZDwunCHCqAB+7nMytMbbbOAFs+isJi9ZEJgHW7fabIWpBhwRYBQeUSJgTZRDNUaBw1k0B4Npm1K4FzUwzbwcgYeGnfUonAIzgo7lZ39INE7zifEHt5hOHBTYtALMHqsDHqLk52VVdMCp0GEdSh5yJFjjzlrhvZmXGDbPNKQ2gLUsrIGlfdHiW147oMz/7uDBeyPvwbVGMaj5xZah7h8niuwtX/nBAjunKvN6oxjB5PxBlXPsP2ruXmSIqCjLnsXRjc4OXql2QPLaWFVIRTkYnwIAc5WxunjzShAbWw/FaFLL6xNoxMKTd0h6B6GRMOx0EdMjWMVP9dYMg6DUrcpwJ7cwdlkKP6ytVbB8FQomFYP1SuxlyImAAh7LoWQ7juvas/bd/DQczm2326RIvFPjLN6dRum9T8TftdvKjKiU85AjszptPfxtKk9y4Jl27w944= 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: 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, From patchwork Wed May 8 17:31:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13658954 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 B211BC04FFE for ; Wed, 8 May 2024 17:32:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E78746B009E; Wed, 8 May 2024 13:31:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFEEE6B009F; Wed, 8 May 2024 13:31:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2A9E6B00A0; Wed, 8 May 2024 13:31:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8CC776B009E for ; Wed, 8 May 2024 13:31:58 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3D295C11CE for ; Wed, 8 May 2024 17:31:58 +0000 (UTC) X-FDA: 82095921516.28.883D66A Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf30.hostedemail.com (Postfix) with ESMTP id A05EA8002A for ; Wed, 8 May 2024 17:31:53 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=RDKjyBXE; spf=pass (imf30.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.170 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1715189513; 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=4rcMwpi5swukR9fu2rr+tgqRl98owlkzS7lAQiTyYR0=; b=BIG+SsrQpz+hZA7Rxd+XCRATlpnIFmIoHm8lOcOLFVv03sb1tdrhcQyEjL24NGtx+ws5MN o/B+oZsq3dnUwHpB2szGVJEemh4FGrH8O8ZAwuV69MDKSpk3lWKVEg3S+0d7HEE39zJ5nr +CujzrWmcuhhtaXcKhS8faD0IvoJ7qY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=RDKjyBXE; spf=pass (imf30.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.170 as permitted sender) smtp.mailfrom=keescook@chromium.org; dmarc=pass (policy=none) header.from=chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1715189513; a=rsa-sha256; cv=none; b=aRGYHKG28cTuYeiA9hsa04ITZwjvS4CQpAyRchTrA8YkBI55tblcZEOGHRP9lwd/6BodTo NNrZMYSDbcm2QotwP1EQs6ikoHk41tWG7FkdriaCRdS/dbhp0GicIqbDG71+jNTGE1wicu DcLVDlZILWO+gmXX7bS/v3N98Hl5o6w= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1ed835f3c3cso42205105ad.3 for ; Wed, 08 May 2024 10:31:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1715189512; x=1715794312; 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=4rcMwpi5swukR9fu2rr+tgqRl98owlkzS7lAQiTyYR0=; b=RDKjyBXEwgwhownYOMbbMmtEgTx35DDQ28mcJvb9dsIWggWkPnk7r3r54TEBmfH3fz RdsceG6Vhe4nD1+igqj8RKA1p+vOLy66nD4jJHuGLwuNiYf+l+oxl+oGvr+PtFIFAS6K SLlBpGyqGZZ94wMzO69j++mCZ1hvuBOmQLnsk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715189512; x=1715794312; 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=4rcMwpi5swukR9fu2rr+tgqRl98owlkzS7lAQiTyYR0=; b=fQl9yYL68O0FpdCkRSvvvqS0JgMI+0ZoyoysoUP8DKHH8ZiZnCYdGlvwSHp2yy3jSq 0AIox3f3IjKx3I8OS8OHhy6cFoyi+dwyispFgFVOByv0V0agxsHiJ6uRRS5k5FjjvwVU Z4HyKDDzzbTPvPJaGCuOaHc1HRmqCEO0K4fr88JaG9KpEMDEeBJA6cC0ilQVMvpTm9TW iQgo8fqAW+mHjKDZwAGgKmKKAkKLtl8k9IChp0LGAsXbuW0mM+qPIRIjbgCDpYzkmGuq 5BFpjkh7mJmqAamtd68X5rl5++FhJKOxMKJdVs0r2Qpfx16ssF+XW8eKtTE2AoMu8PKI vCDg== X-Forwarded-Encrypted: i=1; AJvYcCXaXRkjk97J45Z7CFtfqc3hqDneLReAJPgKmjew1Lh97MuYD++GJWvLH1zqsctfC6bYgwFaHvxPbr1k7U6mM49BdjU= X-Gm-Message-State: AOJu0YzT5Qu89qzCLinS6Z9LTFCWTS4bPgzhJGqi2a8SH3s4la34E3vX Jobsy47H8PUGyNyGfTL2M1Z6/vQMao7kbq3QgymwWTM9aWPGxqzQO00p/X5EJQ== X-Google-Smtp-Source: AGHT+IFNr+b2pAfQcWXsO87OnrvROJLzoQf+GfNtUyLNTbuK3rS25oOgdGVm7lFzQQasZ1Pfzw4/2A== X-Received: by 2002:a17:902:6506:b0:1e6:766c:6a26 with SMTP id d9443c01a7336-1eeb017cfd1mr35094695ad.12.1715189512601; Wed, 08 May 2024 10:31:52 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id la13-20020a170902fa0d00b001ec412676adsm12094121plb.275.2024.05.08.10.31.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 May 2024 10:31:52 -0700 (PDT) From: Kees Cook To: "H . J . Lu" Cc: Kees Cook , Mike Rapoport , Rui Salvaterra , Victor Stinner , Jan Palus , Alexander Viro , Christian Brauner , Jan Kara , Eric Biederman , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Chris Kennelly , Shuah Khan , Muhammad Usama Anjum , John Hubbard , Fangrui Song , Andrew Morton , Yang Yingliang , Mike Rapoport , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 3/3] binfmt_elf: Honor PT_LOAD alignment for static PIE Date: Wed, 8 May 2024 10:31:48 -0700 Message-Id: <20240508173149.677910-3-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=5293; i=keescook@chromium.org; h=from:subject; bh=eUy8l4dx1nlgASZnAXri0kDxbx6zhnXlB+I6YzLFjTw=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmO7cEJD8sdRg2kTQVgy9QHVOS08welrL7Y9Xmg lC0fTmRrBSJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZju3BAAKCRCJcvTf3G3A JtM8EACdAwapwoIf1FEL7K12lEwr6cSDnpSo/80JcopA9tWkFKXsiep6tWvoxZDOzJ1vc9e6b4V YLAAZBeXNl/xk6VrycAfh+xwdHN2k+KNbWvuJiD34NZowKV8tHkpCLzfyBMxlsEnwkpvxp4gaSj d4IhlEOcmwxk6K5OT+QsSmcjMCjEBIVRIK0goPl4GHq8aOi+sRueJ8TaFrjvw1+8mrbvG2DdFCe Uv8kWkvlo/ILaWPFZ0NusPLLdDhExA73NKe/ve4YZAfk0p6FRzmyWuHOaT7dkEYmU9yuB2JtgFA K2DJetC6ue7f0Q13jS3/JzqgB2vtfz+FNYDwsf5pZqI/0ziFNMCvidOe43tE7Iw0Lmdt2/BgPNL r8gTuz9/tlA2bbDc3e/kRUhuX5nwaHcjR/r8IVSalFQmyqc5IPAk37VHvGDmg6DIbep/8qnwvCF QLG5UBoysuC4MgK+oc4o06z7rfYgL2DuXeo2iJifxHJslw72FqLeVf4SKpDzrWKHhqNWvN5fVlO iCNqiQqRpRch+qbtKbcDu6uw3gg9q5q8qzx+EEXIHGhqVyKXRZKdXgMmXXp/my+9/udZV/cFPHl Axrb6nLzkWUCWdMUTIN4aZHCt5ihat6gagmdxw0UNkomFQUeEHsgPbhNUGZztz5L0i7eTM6zPj0 PxL8C6OI/h070mg== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Stat-Signature: pw85jnr5w61wn78siwowx6u6a3qftadc X-Rspam-User: X-Rspamd-Queue-Id: A05EA8002A X-Rspamd-Server: rspam05 X-HE-Tag: 1715189513-583242 X-HE-Meta: U2FsdGVkX18puOo07yNy0Pr86NUFjGjIVyXHQTgtEMWdppbrHAZhMsqO4SHDXDzQXnB2jFflRBQnBLX1/Mma4P2PALakhPWZMIKuNHpSxN8rS5nZch5ua16xvr2EGZgUwOGQb1WWdAWnlKOHfoTm42kZaqxGDgzV4o8l4uXVfAGfoZ0JgFfh239xj4mBlLjj1Tz1DaKoJ2peaZN/31yoYu5zSDk88Yb78w9d4v19ji7WA/g0w85GxhKrkGxnYc9GsztoMbOGCwGzLfmhxUnKlDP4SFLHaVYYY69CcmEWBhZ1IQGqriiJHQ1XywDOhMyEHEYwb5u+pq0XlFNyh+0QhdfsHQkDYqL9kX6TkY/T4k7ZD8FmRWQhU2ao+Bg+vAYYPB+giQejhuIIhMpo2ob1cKHT9e2T2li7OPdQxWJF+T6y8GDXj9hcyLIlUU1thICdRlpPvbvHMVzk6shn/NuotFZwhiXU19flY09EoQfKcoGdgtaPKusY/wr0jaxbwdvMijQQIwg8f2kfg407moqz8r2Pp4BMUazR5nQReEZ7iLMygs58p0RBI+vOxJ3HRztXiNOP0CjCWOW6YNjm7EzLfHkRHmLvzRrNyJHwWYJ2/8UBUwC4EoXP6rYT9pGK4g5/OnGo6yIcm4z6eqjK9pGemidbTnFlGLypOl66l4bKWwkFZUjbS+fxARhiX1LCv+6o1+z/vULDIowuUwrSvS4RWmDyGNT6iVln0fChHOblEChZI0k5arYwmt/Wf+HGBlJDV6MYLhnVDHa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K4ZDIGUo7OSUQDyb5d3bN7YGSCEcRHL 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: List-Subscribe: List-Unsubscribe: The p_align values in PT_LOAD were ignored for static PIE executables (i.e. ET_DYN without PT_INTERP). This is because there is no way to request a non-fixed mmap region with a specific alignment. ET_DYN with PT_INTERP uses a separate base address (ELF_ET_DYN_BASE) and binfmt_elf performs the ASLR itself, which means it can also apply alignment. For the mmap region, the address selection happens deep within the vm_mmap() implementation (when the requested address is 0). The earlier attempt to implement this: commit 9630f0d60fec ("fs/binfmt_elf: use PT_LOAD p_align values for static PIE") commit 925346c129da ("fs/binfmt_elf: fix PT_LOAD p_align values for loaders") did not take into account the different base address origins, and were eventually reverted: aeb7923733d1 ("revert "fs/binfmt_elf: use PT_LOAD p_align values for static PIE"") In order to get the correct alignment from an mmap base, binfmt_elf must perform a 0-address load first, then tear down the mapping and perform alignment on the resulting address. Since this is slightly more overhead, only do this when it is needed (i.e. the alignment is not the default ELF alignment). This does, however, have the benefit of being able to use MAP_FIXED_NOREPLACE, to avoid potential collisions. With this fixed, enable the static PIE self tests again. Reported-by: H.J. Lu Closes: https://bugzilla.kernel.org/show_bug.cgi?id=215275 Signed-off-by: Kees Cook --- Cc: H.J. Lu Cc: Mike Rapoport Cc: Rui Salvaterra Cc: Victor Stinner Cc: Jan Palus Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: Eric Biederman Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org --- fs/binfmt_elf.c | 42 +++++++++++++++++++++++---- tools/testing/selftests/exec/Makefile | 2 +- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index 56432e019d4e..cbb07a9c02d4 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -1088,10 +1088,13 @@ static int load_elf_binary(struct linux_binprm *bprm) goto out_free_dentry; } + /* Calculate any requested alignment. */ + alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum); + /* * 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 + * binaries: programs (i.e. PIE: ET_DYN with PT_INTERP) + * and loaders (ET_DYN without PT_INTERP, since they * _are_ the ELF interpreter). The loaders must * be loaded away from programs since the program * may otherwise collide with the loader (especially @@ -1111,15 +1114,44 @@ static int load_elf_binary(struct linux_binprm *bprm) * without MAP_FIXED nor MAP_FIXED_NOREPLACE). */ if (interpreter) { + /* On ET_DYN with PT_INTERP, we do the ASLR. */ load_bias = ELF_ET_DYN_BASE; if (current->flags & PF_RANDOMIZE) load_bias += arch_mmap_rnd(); - alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum); + /* Adjust alignment as requested. */ if (alignment) load_bias &= ~(alignment - 1); elf_flags |= MAP_FIXED_NOREPLACE; - } else - load_bias = 0; + } else { + /* + * For ET_DYN without PT_INTERP, we rely on + * the architectures's (potentially ASLR) mmap + * base address (via a load_bias of 0). + * + * When a large alignment is requested, we + * must do the allocation at address "0" right + * now to discover where things will load so + * that we can adjust the resulting alignment. + * In this case (load_bias != 0), we can use + * MAP_FIXED_NOREPLACE to make sure the mapping + * doesn't collide with anything. + */ + if (alignment > ELF_MIN_ALIGN) { + load_bias = elf_load(bprm->file, 0, elf_ppnt, + elf_prot, elf_flags, total_size); + if (BAD_ADDR(load_bias)) { + retval = IS_ERR_VALUE(load_bias) ? + PTR_ERR((void*)load_bias) : -EINVAL; + goto out_free_dentry; + } + vm_munmap(load_bias, total_size); + /* Adjust alignment as requested. */ + if (alignment) + load_bias &= ~(alignment - 1); + elf_flags |= MAP_FIXED_NOREPLACE; + } else + load_bias = 0; + } /* * Since load_bias is used for all subsequent loading diff --git a/tools/testing/selftests/exec/Makefile b/tools/testing/selftests/exec/Makefile index 619cff81d796..ab67d58cfab7 100644 --- a/tools/testing/selftests/exec/Makefile +++ b/tools/testing/selftests/exec/Makefile @@ -6,7 +6,7 @@ CFLAGS += -D_GNU_SOURCE ALIGNS := 0x1000 0x200000 0x1000000 ALIGN_PIES := $(patsubst %,load_address.%,$(ALIGNS)) ALIGN_STATIC_PIES := $(patsubst %,load_address.static.%,$(ALIGNS)) -ALIGNMENT_TESTS := $(ALIGN_PIES) +ALIGNMENT_TESTS := $(ALIGN_PIES) $(ALIGN_STATIC_PIES) TEST_PROGS := binfmt_script.py TEST_GEN_PROGS := execveat non-regular $(ALIGNMENT_TESTS)