From patchwork Mon May 20 02:16:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13667994 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 CF142C25B7A for ; Mon, 20 May 2024 02:16:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 666DC6B0089; Sun, 19 May 2024 22:16:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5EE816B008C; Sun, 19 May 2024 22:16:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3CB136B008A; Sun, 19 May 2024 22:16:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1DAA36B0088 for ; Sun, 19 May 2024 22:16:22 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 964E480B56 for ; Mon, 20 May 2024 02:16:21 +0000 (UTC) X-FDA: 82137159762.19.899804E Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by imf03.hostedemail.com (Postfix) with ESMTP id A4A8D2000D for ; Mon, 20 May 2024 02:16:19 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=SJGcvs03; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf03.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.170 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=1716171379; 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=u7+oCeV5s8SnWCifHQ1nG7jot6TA5mZOIRM4l7lIpDY=; b=on3TCr/de9REIdyYwRNFQ5Y7Oiyr7UNczYuBB4hQrsIx5vJ5NBscc9NcedJFAVzVGc4ltJ /z1c5C1qcLc6PIQ87o9B+qyJ1gq9mKdvABdojoQZHPOVfR+OhWOkGIQLawCFuMqkfI0jUa uovEIRbrk0FcChYKPkkqJbxLVaNvGfY= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=SJGcvs03; dmarc=pass (policy=none) header.from=chromium.org; spf=pass (imf03.hostedemail.com: domain of keescook@chromium.org designates 209.85.214.170 as permitted sender) smtp.mailfrom=keescook@chromium.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1716171379; a=rsa-sha256; cv=none; b=tkutxpGJUveF9364gIZdukCyBODAWjWHNnvWmJINxyh4L7vc/E01awQ4kmZTfMK9BbdDJv fnWQBggeZXyQqQxpmDz8EnBUrp/ZUq+fiSmWhnAijavnuklXA4TW8mur1DHXNMooiMZbdv obya/KPHt3eBJutoYChswxKPa0wfnzA= Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1ecd3867556so62917815ad.0 for ; Sun, 19 May 2024 19:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1716171378; x=1716776178; 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=u7+oCeV5s8SnWCifHQ1nG7jot6TA5mZOIRM4l7lIpDY=; b=SJGcvs03wjG5iVNnk8SgGKEAKg5qyZx8ciVqy6hdaMCUgdjokazdWdw2OCMBOqWAco cc0edgzIyudtJgfeuGYT+t/EHZZhH4JL4b8+0CNxQsmhfhshiwauGwg+a3a8OShopB0q 4Q4aXBMlPOIXIgyxECO+q9bYtrL5606FxKXdc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716171378; x=1716776178; 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=u7+oCeV5s8SnWCifHQ1nG7jot6TA5mZOIRM4l7lIpDY=; b=GcabagE89OaZTm6TXk8T7MnjCfkDAdsbEihlWP1KWwgrsntGmp9olUjHvAqMon8mtx 6+0qDUaO9ykrSc64lxr5Cm+Cbh4qtgbHjKW3EQEdFWKiI+4ZNHO7HYXQGYLdeU+cgUX9 JgCSD2zSCOIdT0fS8XULDWEXL7Jyp4oxwa4Y4+/CtXStL1eB+HZipjYvktKUtV4Rzpwj roVbg4N+nepK9J/wssTa5c2QLPN5LXzUJN35+K+caO+JwGlPjVbJjyAIgS5MwMpouOtz /W/KUwxT98FSP44kvLIACvtjEshVofvaYc1XUbS7NccXoD46j7puTRx+MM9Nj1TdSo/Y Fnfw== X-Forwarded-Encrypted: i=1; AJvYcCVBSCD/HNkk0xt7xJmQPpZXVR2d7oE9CqoRzeyXucKmV9NfEBS1fIIryngUKeRqP68uieBrSSRXmV6iz6s45t4lNDo= X-Gm-Message-State: AOJu0YzXiiGFkrYU56GzTvHjAo9PJ6U63mKNPslartFu7ZQZZHoPrmpm NLvZizySuKO3NpanjLihgu/W+lg8nYj7DsngdkarboXW5BjQLgp7lJZPclyE9A== X-Google-Smtp-Source: AGHT+IHRnGNUav1xrKAItbibrjklhMFmAti4JfbJK6woGd6RFU76uzId3Xy5ieOxiN069HAexO85vQ== X-Received: by 2002:a05:6a20:6a04:b0:1af:d40a:7692 with SMTP id adf61e73a8af0-1afde1c4d31mr30237407637.42.1716171378409; Sun, 19 May 2024 19:16:18 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2b67158c3b4sm19014586a91.45.2024.05.19.19.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 19:16:16 -0700 (PDT) From: Kees Cook To: Eric Biederman Cc: Kees Cook , Justin Stitt , Alexander Viro , Christian Brauner , Jan Kara , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 1/2] exec: Add KUnit test for bprm_stack_limits() Date: Sun, 19 May 2024 19:16:11 -0700 Message-Id: <20240520021615.741800-1-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520021337.work.198-kees@kernel.org> References: <20240520021337.work.198-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=8160; i=keescook@chromium.org; h=from:subject; bh=emBZah1nJM7hX2hwqXj1bD4HWPpBnlv8ewVWm+AeRdU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmSrJr1E0juzDEuXc+NCFvv+X5LxxCes4KOV4R8 c2T/ikWuoCJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZkqyawAKCRCJcvTf3G3A JpZMD/90Wembb3QiAeI2/lgYkDLrvjCANGGt0wlkf6F7D0Cv2Xx4H+Yg8dzjNFB2f+Pk5FxgQpa JVXfOYEsq2aYbEHjV5oW19f7lxUXC0J9/2c0zIyX+hu10so69Ff5ZlmUoPMl2Awu7oot1pztAmK Nnu18J6N40YMPeN6U6N6YGHf2kGySvRAS9prEhT5/DOKec47GQkZrZdl80Br26nga/tRCKReBos xbktxpVu0OqDaVgCACoRCMohKw356sYr6C2UOUtKlGuJgRoYZTvgjQLJxkL5NaPg2LOYS5tg7QC qk7/H3L7TEn2jpZqmqMiowISQ+POF2Qz0QmMCmJEkSE8GWQlEZ5sQumg5g4cVOUwTlVxsh3+W24 0C0/q4OK8h8qG9VRQpfxlL1Lq8EWj1EdfQgB0JkstXdeUto2ikkXtMKAT23ms3Ac8BDbyibe9Qu DvIQAFmTZtiXzuO4/chaVOTMnqtarJYvQA1DLNfFQHsLeJYUf5T4cersgMtNhkfLyapJ6WyFC44 g5HKZbmwjIYV3tEoHnFS87eNmn4Xt1BULsQYihMTtI6CeTOdQljVZEoOOLa2kT1tNwHGVQ1j+j/ 4wiHrZky3jrRjFfMVJ7HkZ2eEgkteF46GLaEOWytXTdRDeLf5rS/AyrGEcHA1ThJAwdHwaiPnnv K0/rUIpqBm+XkxA== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: A4A8D2000D X-Stat-Signature: kpxcnf9pnuynjfdni1perriiq6ukju4h X-HE-Tag: 1716171379-947419 X-HE-Meta: U2FsdGVkX1/saRk4xEn8mUlOgeCM6S6xypaSbNiaKbchvXNhWbon9h3+5gOeOPNIn6CemuwxsD5K7D/ehUZk9FBVSQPOy7vdMCpwFnZa3rBtInQlBgBsUNYf1jsCjVAkrK+4LHbPfGtofHTRHPrmBbPn/dHRUJDMZ/AG/OUlgKpzyeH5JY/o4hsipdSmnsxu7i0O8MSvhQ3wblXAq8Y9zv0YU8e03ciQlyS1seNmA1y16ILwTmsCiSnNQJt0szUXDheFklts+uQXSUK+bKNQqxH2JSAG4o2UwwxZT+KM81V9rKaeaAFVimWnv21BsRis+3TLpw16P3Fz/vO/X+gZ+x7B+zNd23r30R8ud7pz+EYzp+9QEmZafTHTRfeGE16Gu6Hv7UCnjGUKIMk++FrK3zi719Ljz8VvW4JPmScS9KW6SRZ6McghjXhy1M/aGz8hpZAxyW1lztEQPTkpmLCemC4j2p6+qgzsx2UaWFAF5iBe0o43dzi8ZDFb1vgA7oO6ECvIggyYLUHrfc16CP1twA66VlzHaBlyktbfjIuRH/DPmG43cj/lZP0yNpC5+/LRESQUMhJstKC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80WAxefXApj2TZQeNAYYLVhhOuhIpcuLD78Gc= 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: Since bprm_stack_limits() operates with very limited side-effects, add it as the first exec.c KUnit test. Add to Kconfig and adjust MAINTAINERS file to include it. Tested on 64-bit UML: $ tools/testing/kunit/kunit.py run exec Signed-off-by: Kees Cook --- Cc: Eric Biederman Cc: Justin Stitt Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org --- MAINTAINERS | 2 + fs/Kconfig.binfmt | 8 ++++ fs/exec.c | 13 ++++++ fs/exec_test.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 fs/exec_test.c diff --git a/MAINTAINERS b/MAINTAINERS index 7c121493f43d..845165dbb756 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8039,7 +8039,9 @@ S: Supported T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/execve F: Documentation/userspace-api/ELF.rst F: fs/*binfmt_*.c +F: fs/Kconfig.binfmt F: fs/exec.c +F: fs/exec_test.c F: include/linux/binfmts.h F: include/linux/elf.h F: include/uapi/linux/binfmts.h diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt index f5693164ca9a..58657f2d9719 100644 --- a/fs/Kconfig.binfmt +++ b/fs/Kconfig.binfmt @@ -176,4 +176,12 @@ config COREDUMP certainly want to say Y here. Not necessary on systems that never need debugging or only ever run flawless code. +config EXEC_KUNIT_TEST + bool "Build execve tests" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + This builds the exec KUnit tests, which tests boundary conditions + of various aspects of the exec internals. + endmenu diff --git a/fs/exec.c b/fs/exec.c index b3c40fbb325f..1d45e1a2d620 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -475,6 +475,15 @@ static int count_strings_kernel(const char *const *argv) return i; } +/* + * Calculate bprm->argmin from: + * - _STK_LIM + * - ARG_MAX + * - bprm->rlim_stack.rlim_cur + * - bprm->argc + * - bprm->envc + * - bprm->p + */ static int bprm_stack_limits(struct linux_binprm *bprm) { unsigned long limit, ptr_size; @@ -2200,3 +2209,7 @@ static int __init init_fs_exec_sysctls(void) fs_initcall(init_fs_exec_sysctls); #endif /* CONFIG_SYSCTL */ + +#ifdef CONFIG_EXEC_KUNIT_TEST +#include "exec_test.c" +#endif diff --git a/fs/exec_test.c b/fs/exec_test.c new file mode 100644 index 000000000000..32a90c6f47e7 --- /dev/null +++ b/fs/exec_test.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include + +struct bprm_stack_limits_result { + struct linux_binprm bprm; + int expected_rc; + unsigned long expected_argmin; +}; + +static const struct bprm_stack_limits_result bprm_stack_limits_results[] = { + /* Giant values produce -E2BIG */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = INT_MAX, .envc = INT_MAX }, .expected_rc = -E2BIG }, + /* + * 0 rlim_stack will get raised to ARG_MAX. With 1 string pointer, + * we should see p - ARG_MAX + sizeof(void *). + */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = 1, .envc = 0 }, .expected_argmin = ULONG_MAX - ARG_MAX + sizeof(void *)}, + /* Validate that argc is always raised to a minimum of 1. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = 0, .envc = 0 }, .expected_argmin = ULONG_MAX - ARG_MAX + sizeof(void *)}, + /* + * 0 rlim_stack will get raised to ARG_MAX. With pointers filling ARG_MAX, + * we should see -E2BIG. (Note argc is always raised to at least 1.) + */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = ARG_MAX / sizeof(void *), .envc = 0 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = ARG_MAX / sizeof(void *) + 1, .envc = 0 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = 0, .envc = ARG_MAX / sizeof(void *) }, .expected_rc = -E2BIG }, + /* And with one less, we see space for exactly 1 pointer. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = (ARG_MAX / sizeof(void *)) - 1, .envc = 0 }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 0, + .argc = 0, .envc = (ARG_MAX / sizeof(void *)) - 2, }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + /* If we raise rlim_stack / 4 to exactly ARG_MAX, nothing changes. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = ARG_MAX / sizeof(void *), .envc = 0 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = ARG_MAX / sizeof(void *) + 1, .envc = 0 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = 0, .envc = ARG_MAX / sizeof(void *) }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = (ARG_MAX / sizeof(void *)) - 1, .envc = 0 }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ARG_MAX * 4, + .argc = 0, .envc = (ARG_MAX / sizeof(void *)) - 2, }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + /* But raising it another pointer * 4 will provide space for 1 more pointer. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = (ARG_MAX + sizeof(void *)) * 4, + .argc = ARG_MAX / sizeof(void *), .envc = 0 }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = (ARG_MAX + sizeof(void *)) * 4, + .argc = 0, .envc = ARG_MAX / sizeof(void *) - 1 }, + .expected_argmin = ULONG_MAX - sizeof(void *) }, + /* Raising rlim_stack / 4 to _STK_LIM / 4 * 3 will see more space. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3), + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3), + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, + /* But raising it any further will see no increase. */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * 3 + sizeof(void *)), + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * (_STK_LIM / 4 * + sizeof(void *)), + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * _STK_LIM, + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = 4 * _STK_LIM, + .argc = 0, .envc = 0 }, + .expected_argmin = ULONG_MAX - (_STK_LIM / 4 * 3) + sizeof(void *) }, +}; + +static void exec_test_bprm_stack_limits(struct kunit *test) +{ + /* Double-check the constants. */ + KUNIT_EXPECT_EQ(test, _STK_LIM, SZ_8M); + KUNIT_EXPECT_EQ(test, ARG_MAX, 32 * SZ_4K); + + for (int i = 0; i < ARRAY_SIZE(bprm_stack_limits_results); i++) { + const struct bprm_stack_limits_result *result = &bprm_stack_limits_results[i]; + struct linux_binprm bprm = result->bprm; + int rc; + + rc = bprm_stack_limits(&bprm); + KUNIT_EXPECT_EQ_MSG(test, rc, result->expected_rc, "on loop %d", i); + KUNIT_EXPECT_EQ_MSG(test, bprm.argmin, result->expected_argmin, "on loop %d", i); + } +} + +static struct kunit_case exec_test_cases[] = { + KUNIT_CASE(exec_test_bprm_stack_limits), + {}, +}; + +static struct kunit_suite exec_test_suite = { + .name = "exec", + .test_cases = exec_test_cases, +}; + +kunit_test_suite(exec_test_suite); From patchwork Mon May 20 02:16:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kees Cook X-Patchwork-Id: 13667990 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 D879CC25B75 for ; Mon, 20 May 2024 02:16:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A24D6B0085; Sun, 19 May 2024 22:16:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 52AC76B0089; Sun, 19 May 2024 22:16:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3D6466B0085; Sun, 19 May 2024 22:16:20 -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 194F16B0085 for ; Sun, 19 May 2024 22:16:20 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CC51AA0B16 for ; Mon, 20 May 2024 02:16:19 +0000 (UTC) X-FDA: 82137159678.24.CF7EE34 Received: from mail-oo1-f43.google.com (mail-oo1-f43.google.com [209.85.161.43]) by imf27.hostedemail.com (Postfix) with ESMTP id D598040018 for ; Mon, 20 May 2024 02:16:17 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=GiM7GkTT; spf=pass (imf27.hostedemail.com: domain of keescook@chromium.org designates 209.85.161.43 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=1716171377; 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=pXk5PQZLOhlOb9f2TikaNKwPS1UYUorWP7IN+mVzocE=; b=eS2AAjX2A6jTIdRGuBys3rl8iRn8q9MRbJFuI/3yN9/dNXO350tgTZB4rNF1nyVvGQ4im4 fGiQz2BEIkGmqLV+l/ZpVONvbiBxO0rhBdJhnXpcCowQAeG7iFwT3+Aa6aXoXrXN8AU6xG jv1rLHmtSJT2a5F+LgdpTtJ/+GmF4Hc= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=chromium.org header.s=google header.b=GiM7GkTT; spf=pass (imf27.hostedemail.com: domain of keescook@chromium.org designates 209.85.161.43 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=1716171377; a=rsa-sha256; cv=none; b=N8U/dCQfn0C8od4VMmOtAJPj6jxKq/D0XWTtOMlVb+yhIyc7H03Wr9YCXftpnlSWs80dDP a2scadXOJ2g7vTJJHc402evSWq2kIpTVQNi2Y4CcFwDevCZOFZvbpDDnkqWxNNMiVrg2Se //DHDEhL7ay3WT8GolIHDOLCXEpZJhE= Received: by mail-oo1-f43.google.com with SMTP id 006d021491bc7-5b2ed25b337so1502425eaf.0 for ; Sun, 19 May 2024 19:16:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1716171377; x=1716776177; 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=pXk5PQZLOhlOb9f2TikaNKwPS1UYUorWP7IN+mVzocE=; b=GiM7GkTTvS7UhQVlkj6u/voQGWw/xXsvd74JLQP49Yn33mAJTcBlD1iVrkDaOlq1HM KReOU2nPD4cO5K8zxc6eXpwrmS6P9my7w5yHNha5pZgjax2erW8gmRFGu4mkbyHsGe/r d+XXfYAwy/UqJ+SIizuJ1erNPorQ8hGgO+ons= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716171377; x=1716776177; 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=pXk5PQZLOhlOb9f2TikaNKwPS1UYUorWP7IN+mVzocE=; b=fp17dsupydGdhyCP5YTdSRa8XQo94hQVwtD4tMbrakKwE/RcncJ+YPLdWuhMqJ+ojC H4gfnMzM4EnV/kLuQ3v32COhzOzSMJ4UXPXvG9LP6dHxKt9k/6v/gbRImjTauhjfE49j xhk7eSlu0Dv86oioz7/iwHGvF7AyJHLmcxjhLKGly16YCQmBCg422J7EbAClO/5bqZdD D7sUCfTLnbWp4w0r4nxb1yZb0cqxEc4pv9VVN04SYJgv4hV9ql+Zk/ZcmS7v9eTWLbOL 3AQJFiGDDQ/oUM+5mp+6GSf8nuz1YOwB2oeIJn8fMvqOlMFNgxmFver7GNQ+6b8ua8O2 Yusg== X-Forwarded-Encrypted: i=1; AJvYcCXJY1JY0FkFKTet0SNGPFwJ5cJ819SJ/1maRx9ub3YI/66NPDhQUqEbQHbWy3iQ38gccZtI2UFLBJYpTbI8bkPQ4/8= X-Gm-Message-State: AOJu0YxuzvyA6Ug9By/HTz26lNvwtXDTOmdHvBxEmY8A8dR5UpO4Q3u1 kwHAxxNe+7qsW7vaCJ44V0Au0JKkyvd/bbXb3ryjR+mHnyFiOd6If3o6U0RIpw== X-Google-Smtp-Source: AGHT+IGCk56SRj37cV6IB9o0SSSdmvK7eIfw1zNZrwdb/ilDu1xfVdX+ZV6pQVOmzEM5QVnWzLp20A== X-Received: by 2002:a05:6870:6b9a:b0:244:c620:7f4b with SMTP id 586e51a60fabf-244c6208e5dmr25152033fac.32.1716171376722; Sun, 19 May 2024 19:16:16 -0700 (PDT) Received: from www.outflux.net ([198.0.35.241]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2ae0d88sm18130632b3a.122.2024.05.19.19.16.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 May 2024 19:16:16 -0700 (PDT) From: Kees Cook To: Eric Biederman Cc: Kees Cook , Justin Stitt , Alexander Viro , Christian Brauner , Jan Kara , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: [PATCH 2/2] exec: Avoid pathological argc, envc, and bprm->p values Date: Sun, 19 May 2024 19:16:12 -0700 Message-Id: <20240520021615.741800-2-keescook@chromium.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240520021337.work.198-kees@kernel.org> References: <20240520021337.work.198-kees@kernel.org> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4282; i=keescook@chromium.org; h=from:subject; bh=m0sCs3UZgNNm707rXtEZ6kEPFezkWis9xLUpPwvfVyU=; b=owEBbQKS/ZANAwAKAYly9N/cbcAmAcsmYgBmSrJsc9KHOfxr0Wciwne6IO1ZYX00AOmo6RSzn ssGypyJUcmJAjMEAAEKAB0WIQSlw/aPIp3WD3I+bhOJcvTf3G3AJgUCZkqybAAKCRCJcvTf3G3A JsKKD/9uBa8Jg3Y7sh77JlX02ZEuM20xIOhSZRe/yX+ttzgjv6KsJ9e3EIzgOYyNTIetZdFOmUb t7Mw60rDwAc5bbFe12eX/hkmt1YSB65lOeZh46s0Pvb0yVyjdgvu7JNmVJav2jDq97QQfVjpbQL 8Lyv6j+6foBWoBRnIwTVPXKlVz1By4gwEVfkh7B1vHQyDU1T0uC3LJ3cI4TtM0Q5kf/OdNrG7Go dkULpZMKwZJXiLDtJxOWAzncvtbcHJpMh2a+kEde/uu48+ASF8pQYWAa7uHHnhmF5ic5z6+Sp0u H+c2g3O+CnK7HKpNC6/kPNQktzYH0YHsn6SZTovtLwkgHJctXZMNx+6u+0i+9HT4konRiJttZUf tJXAxAljiX1a9z2NcyqjkRymJmABRN+tnuT7PU4QwPrzcrzMWPNRQyo9Tkt6Wcnyqyv/OcGcuZI Qx+xQCDv+Pmjn5D3ccDOTJ3Xmp5ZmZzVCw3CJBAM05GyVuKO0dm6b9i/hATOiP8oNQztwOHg671 NES0OBKauNxcL9Ob7iGOT8Jtc+gZkkrdNw/8K6IGViwNkZY9lzH5Lt14+HgRT8jpKdmC5GlEdDL YhbDjJZJ0aJNroXppjbZpDpNNuiHqPewUDpRlFh5orwTPevgk8c9hGJXEn1xDtG/0cpcx1Vesj6 xd0fVBCgpYbeRWQ== X-Developer-Key: i=keescook@chromium.org; a=openpgp; fpr=A5C3F68F229DD60F723E6E138972F4DFDC6DC026 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: D598040018 X-Stat-Signature: nkcj9dtbyx6xxk1itkw4yhcq7zzn11f8 X-Rspam-User: X-HE-Tag: 1716171377-982698 X-HE-Meta: U2FsdGVkX1/dQacRAcjcZfhs8c2EyHqe4PIPj+7zE9siJ3zWliNfbloIp7juDjR+JcIk8bKbpd19SbWVewm/xq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NLkCtxd2FU/AlRvrmUXTJ22Rmr8z6+0VGe07Rtzd50Bp28YgSFB/uwKUJCWxI8KutjjXLMUdYaBV0w+ysqq0f0RRG3tkn3zbEUpf1Tj050PhtFKh6B7hRLppq+FBoc10JIX3aF7IxnwMKx9opZHe33eektwzyJB5QkRMr2uxDFcKzDVcR7yVy8hc0R6MQosxrz5F4EkSvTXmkVIStV8XqFL5tdFeTE5ry2otu3kCL8KQmpYvkkZ6HrPRFnA1l7VSjs4Afm4FAzNl4Oh+IMzMoJ8ohLnyi24JYUGVt59jXpCwnPhQjK/AIUE8jyktn8Vpxxe1B6VGESQ5JTJ/aLGLsy+6/4JUGYdWU8viG6/4tMN96bavykno0Okd/bJB9xCvw2+Laa7YUCHcW5p9BGkAUUkHmNVW+grYrHbXtzwhTIasOXmc3XgTJiEzig/FOIOsT6kEqxZxXKULGjJz6lnYqrvBgmZjOexubci/HAkjB+28Nz9A7S91B04W0aV X-Bogosity: Ham, tests=bogofilter, spamicity=0.000115, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make sure nothing goes wrong with the string counters or the bprm's belief about the stack pointer. Add checks and matching self-tests. For 32-bit validation, this was run under 32-bit UML: $ tools/testing/kunit/kunit.py run --make_options SUBARCH=i386 exec Signed-off-by: Kees Cook --- Cc: Eric Biederman Cc: Justin Stitt Cc: Alexander Viro Cc: Christian Brauner Cc: Jan Kara Cc: linux-fsdevel@vger.kernel.org Cc: linux-mm@kvack.org --- fs/exec.c | 11 ++++++++++- fs/exec_test.c | 26 +++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/fs/exec.c b/fs/exec.c index 1d45e1a2d620..5dcdd115739e 100644 --- a/fs/exec.c +++ b/fs/exec.c @@ -503,6 +503,9 @@ static int bprm_stack_limits(struct linux_binprm *bprm) * of argument strings even with small stacks */ limit = max_t(unsigned long, limit, ARG_MAX); + /* Reject totally pathological counts. */ + if (bprm->argc < 0 || bprm->envc < 0) + return -E2BIG; /* * We must account for the size of all the argv and envp pointers to * the argv and envp strings, since they will also take up space in @@ -516,11 +519,17 @@ static int bprm_stack_limits(struct linux_binprm *bprm) * argc can never be 0, to keep them from walking envp by accident. * See do_execveat_common(). */ - ptr_size = (max(bprm->argc, 1) + bprm->envc) * sizeof(void *); + if (check_add_overflow(max(bprm->argc, 1), bprm->envc, &ptr_size) || + check_mul_overflow(ptr_size, sizeof(void *), &ptr_size)) + return -E2BIG; if (limit <= ptr_size) return -E2BIG; limit -= ptr_size; + /* Avoid a pathological bprm->p. */ + if (bprm->p < limit) + return -E2BIG; + bprm->argmin = bprm->p - limit; return 0; } diff --git a/fs/exec_test.c b/fs/exec_test.c index 32a90c6f47e7..f2d4a80c861d 100644 --- a/fs/exec_test.c +++ b/fs/exec_test.c @@ -8,9 +8,32 @@ struct bprm_stack_limits_result { }; static const struct bprm_stack_limits_result bprm_stack_limits_results[] = { - /* Giant values produce -E2BIG */ + /* Negative argc/envc counts produce -E2BIG */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = INT_MIN, .envc = INT_MIN }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = 5, .envc = -1 }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = -1, .envc = 10 }, .expected_rc = -E2BIG }, + /* The max value of argc or envc is MAX_ARG_STRINGS. */ { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, .argc = INT_MAX, .envc = INT_MAX }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = MAX_ARG_STRINGS, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = 0, .envc = MAX_ARG_STRINGS }, .expected_rc = -E2BIG }, + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = MAX_ARG_STRINGS, .envc = 0 }, .expected_rc = -E2BIG }, + /* + * On 32-bit system these argc and envc counts, while likely impossible + * to represent within the associated TASK_SIZE, could overflow the + * limit calculation, and bypass the ptr_size <= limit check. + */ + { { .p = ULONG_MAX, .rlim_stack.rlim_cur = ULONG_MAX, + .argc = 0x20000001, .envc = 0x20000001 }, .expected_rc = -E2BIG }, + /* Make sure a pathological bprm->p doesn't cause an overflow. */ + { { .p = sizeof(void *), .rlim_stack.rlim_cur = ULONG_MAX, + .argc = 10, .envc = 10 }, .expected_rc = -E2BIG }, /* * 0 rlim_stack will get raised to ARG_MAX. With 1 string pointer, * we should see p - ARG_MAX + sizeof(void *). @@ -88,6 +111,7 @@ static void exec_test_bprm_stack_limits(struct kunit *test) /* Double-check the constants. */ KUNIT_EXPECT_EQ(test, _STK_LIM, SZ_8M); KUNIT_EXPECT_EQ(test, ARG_MAX, 32 * SZ_4K); + KUNIT_EXPECT_EQ(test, MAX_ARG_STRINGS, 0x7FFFFFFF); for (int i = 0; i < ARRAY_SIZE(bprm_stack_limits_results); i++) { const struct bprm_stack_limits_result *result = &bprm_stack_limits_results[i];