From: Kees Cook <kees@kernel.org>
To: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Mark Brown <broonie@kernel.org>,
Catalin Marinas <catalin.marinas@arm.com>,
Dave Hansen <dave.hansen@linux.intel.com>,
David Howells <dhowells@redhat.com>,
"Eric W. Biederman" <ebiederm@xmission.com>,
Jann Horn <jannh@google.com>, Jeff Xu <jeffxu@chromium.org>,
Joey Gouly <joey.gouly@arm.com>,
Linus Walleij <linus.walleij@linaro.org>,
Andy Lutomirski <luto@kernel.org>, Marc Zyngier <maz@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Pierre Langlois <pierre.langlois@arm.com>,
Quentin Perret <qperret@google.com>,
"Mike Rapoport (IBM)" <rppt@kernel.org>,
Ryan Roberts <ryan.roberts@arm.com>,
Thomas Gleixner <tglx@linutronix.de>,
Will Deacon <will@kernel.org>,
Matthew Wilcox <willy@infradead.org>,
Qi Zheng <zhengqi.arch@bytedance.com>,
linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org,
x86@kernel.org
Subject: Re: [RFC PATCH 8/8] mm: Add basic tests for kpkeys_hardened_cred
Date: Thu, 6 Feb 2025 20:52:32 -0800 [thread overview]
Message-ID: <202502062024.BCB0DED1D5@keescook> (raw)
In-Reply-To: <20250203102809.1223255-9-kevin.brodsky@arm.com>
On Mon, Feb 03, 2025 at 10:28:09AM +0000, Kevin Brodsky wrote:
> Add basic tests for the kpkeys_hardened_pgtables feature: try to
> perform a direct write to current->{cred,real_cred} and ensure it
> fails.
>
> Signed-off-by: Kevin Brodsky <kevin.brodsky@arm.com>
> ---
> mm/Makefile | 1 +
> mm/kpkeys_hardened_cred_test.c | 42 ++++++++++++++++++++++++++++++++++
Current file naming convention[1] would be to name this as:
mm/tests/kpkeys_hardened_cred_kunit.c
> security/Kconfig.hardening | 11 +++++++++
> 3 files changed, 54 insertions(+)
> create mode 100644 mm/kpkeys_hardened_cred_test.c
>
> diff --git a/mm/Makefile b/mm/Makefile
> index f7263b7f45b8..2024226902d4 100644
> --- a/mm/Makefile
> +++ b/mm/Makefile
> @@ -149,3 +149,4 @@ obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o
> obj-$(CONFIG_PT_RECLAIM) += pt_reclaim.o
> obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES) += kpkeys_hardened_pgtables.o
> obj-$(CONFIG_KPKEYS_HARDENED_PGTABLES_TEST) += kpkeys_hardened_pgtables_test.o
> +obj-$(CONFIG_KPKEYS_HARDENED_CRED_TEST) += kpkeys_hardened_cred_test.o
And for the Kconfig convention says[2] this should be:
CONFIG_KPKEYS_HARDENED_CRED_KUNIT_TEST
> diff --git a/mm/kpkeys_hardened_cred_test.c b/mm/kpkeys_hardened_cred_test.c
> new file mode 100644
> index 000000000000..46048098f99d
> --- /dev/null
> +++ b/mm/kpkeys_hardened_cred_test.c
> @@ -0,0 +1,42 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <kunit/test.h>
> +#include <linux/sched.h>
> +
> +static void write_cred(struct kunit *test)
> +{
> + long zero = 0;
> + int ret;
> +
> + ret = copy_to_kernel_nofault((unsigned long *)current->cred, &zero, sizeof(zero));
> + KUNIT_EXPECT_EQ_MSG(test, ret, -EFAULT,
> + "Write to current->cred wasn't prevented");
> +
> + ret = copy_to_kernel_nofault((unsigned long *)current->real_cred, &zero, sizeof(zero));
> + KUNIT_EXPECT_EQ_MSG(test, ret, -EFAULT,
> + "Write to current->real_cred wasn't prevented");
This is a good negative test. I would include a positive test as well.
i.e. make sure you can run copy_from_kernel_nofault() to read it
successfully. Otherwise you don't know if you're just getting a bad
address -- we want to distinguish between them. (This is more true for
the next suggestion, since current->cred being broken would be much more
obvious.)
While current->cred is good and easy, I would like to see prepare_creds()
exercised too to get a new cred and validate that it is equally directly
readable and directly not writable, and then use the correct accessors
to perform a successful write to the cred, read back the change,
etc. (i.e. validate the expected behavior too.)
> +}
> +
> +static int kpkeys_hardened_cred_suite_init(struct kunit_suite *suite)
> +{
> + if (!arch_kpkeys_enabled()) {
> + pr_err("Cannot run kpkeys_hardened_cred tests: kpkeys are not supported\n");
> + return 1;
> + }
Instead of failing ("return 1") I think this should be a "skip" (it is
expected to not work if there is no support) in each test instead:
if (!arch_kpkeys_enabled())
kunit_skip(test, "kpkeys are not supported\n");
I'm very happy to see tests! :)
-Kees
[1] https://docs.kernel.org/dev-tools/kunit/style.html#test-file-and-module-names
[2] https://docs.kernel.org/dev-tools/kunit/style.html#test-kconfig-entries
--
Kees Cook
next prev parent reply other threads:[~2025-02-07 4:52 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-03 10:28 [RFC PATCH 0/8] pkeys-based cred hardening Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 1/8] arm64: kpkeys: Avoid unnecessary writes to POR_EL1 Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 2/8] mm: kpkeys: Introduce unrestricted level Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 3/8] slab: Introduce SLAB_SET_PKEY Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 4/8] rcu: Allow processing kpkeys-protected data Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 5/8] mm: kpkeys: Introduce cred pkey/level Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 6/8] cred: Protect live struct cred with kpkeys Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 7/8] fs: Protect creds installed by override_creds() Kevin Brodsky
2025-02-03 10:28 ` [RFC PATCH 8/8] mm: Add basic tests for kpkeys_hardened_cred Kevin Brodsky
2025-02-07 4:52 ` Kees Cook [this message]
2025-02-11 8:58 ` Kevin Brodsky
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=202502062024.BCB0DED1D5@keescook \
--to=kees@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=broonie@kernel.org \
--cc=catalin.marinas@arm.com \
--cc=dave.hansen@linux.intel.com \
--cc=dhowells@redhat.com \
--cc=ebiederm@xmission.com \
--cc=jannh@google.com \
--cc=jeffxu@chromium.org \
--cc=joey.gouly@arm.com \
--cc=kevin.brodsky@arm.com \
--cc=linus.walleij@linaro.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=luto@kernel.org \
--cc=maz@kernel.org \
--cc=peterz@infradead.org \
--cc=pierre.langlois@arm.com \
--cc=qperret@google.com \
--cc=rppt@kernel.org \
--cc=ryan.roberts@arm.com \
--cc=tglx@linutronix.de \
--cc=will@kernel.org \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox