linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>
To: "dietmar.eggemann@arm.com" <dietmar.eggemann@arm.com>,
	"broonie@kernel.org" <broonie@kernel.org>,
	"Szabolcs.Nagy@arm.com" <Szabolcs.Nagy@arm.com>,
	"brauner@kernel.org" <brauner@kernel.org>,
	"dave.hansen@linux.intel.com" <dave.hansen@linux.intel.com>,
	"debug@rivosinc.com" <debug@rivosinc.com>,
	"mgorman@suse.de" <mgorman@suse.de>,
	"vincent.guittot@linaro.org" <vincent.guittot@linaro.org>,
	"fweimer@redhat.com" <fweimer@redhat.com>,
	"mingo@redhat.com" <mingo@redhat.com>,
	"rostedt@goodmis.org" <rostedt@goodmis.org>,
	"hjl.tools@gmail.com" <hjl.tools@gmail.com>,
	"tglx@linutronix.de" <tglx@linutronix.de>,
	"vschneid@redhat.com" <vschneid@redhat.com>,
	"shuah@kernel.org" <shuah@kernel.org>,
	"hpa@zytor.com" <hpa@zytor.com>,
	"peterz@infradead.org" <peterz@infradead.org>,
	"bp@alien8.de" <bp@alien8.de>,
	"bsegall@google.com" <bsegall@google.com>,
	"x86@kernel.org" <x86@kernel.org>,
	"juri.lelli@redhat.com" <juri.lelli@redhat.com>
Cc: "yury.khrustalev@arm.com" <yury.khrustalev@arm.com>,
	"linux-kselftest@vger.kernel.org"
	<linux-kselftest@vger.kernel.org>,
	"linux-api@vger.kernel.org" <linux-api@vger.kernel.org>,
	"jannh@google.com" <jannh@google.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"catalin.marinas@arm.com" <catalin.marinas@arm.com>,
	"will@kernel.org" <will@kernel.org>,
	"wilco.dijkstra@arm.com" <wilco.dijkstra@arm.com>,
	"kees@kernel.org" <kees@kernel.org>
Subject: Re: [PATCH RFT v15 4/8] fork: Add shadow stack support to clone3()
Date: Mon, 14 Apr 2025 18:28:14 +0000	[thread overview]
Message-ID: <b15bf40b530f74d3339a313e7f5f5cb09205d348.camel@intel.com> (raw)
In-Reply-To: <20250408-clone3-shadow-stack-v15-4-3fa245c6e3be@kernel.org>

On Tue, 2025-04-08 at 19:49 +0100, Mark Brown wrote:
> +int arch_shstk_validate_clone(struct task_struct *t,
> +			      struct vm_area_struct *vma,
> +			      struct page *page,
> +			      struct kernel_clone_args *args)
> +{
> +	/*
> +	 * SSP is aligned, so reserved bits and mode bit are a zero, just mark
> +	 * the token 64-bit.
> +	 */
> +	void *maddr = kmap_local_page(page);
> +	int offset;
> +	unsigned long addr, ssp;
> +	u64 expected;
> +
> +	if (!features_enabled(ARCH_SHSTK_SHSTK))
> +		return 0;
> +
> +	ssp = args->shadow_stack_pointer;
> +	addr = ssp - SS_FRAME_SIZE;
> +	expected = ssp | BIT(0);
> +	offset = offset_in_page(addr);
> +
> +	if (!cmpxchg_to_user_page(vma, page, addr, (unsigned long *)(maddr + offset),
> +				  expected, 0))
> +		return -EINVAL;
> +	set_page_dirty_lock(page);
> +
> +	return 0;
> +}
> +

First of all, sorry for not contributing on this since v9. I've had an unusual
enormous project conflict (TDX) combined with my test HW dieing.

I tested v15 on x86 and saw a couple problems:
1. I think kmap_local_page() is supposed to be paired kunmap_local(). But shstk
is not supported on highmem systems, so let's just use page_address().
2. Some off by one (frame) errors that cause the clone3 test to fail on x86.

Both fixed in the diff below, but in debugging the off-by-one errors I've
realized this implementation wastes a shadow stack frame.

On x86 when that token is consumed normally it would have:

  SSP = token_addr + 8

I always assumed the HW token consumption behavior was to try to save a frame on
the shadow stack. Once the token is consumed it is useless. So might as well
reuse the frame for the next push. But the clone3 behavior is different than the
normal token consumption logic. Instead it will have SSP *at* the token, which
will then have the next call push and leave the zero frame as wasted space.

Do we want this? On arm there is SHADOW_STACK_SET_MARKER, which leaves a marker
token. But on clone3 it will also leave behind a zero frame from the CMPXCHGed
token. So if you use SHADOW_STACK_SET_MARKER you get two marker tokens. And on
x86 you will get one one for clone3 but not others, until x86 implements
SHADOW_STACK_SET_MARKER. At which point x86 has to diverge from arm (bad) or
also have the double marker frame.

The below fixes the x86 functionally, but what do you think of the wasted frame?
One fix would be to change shadow_stack_pointer to shadow_stack_token, and then
have each arch consume it in the normal HW way, leaving the new thread with:

   SSP = clone_args->shadow_stack_token + 8

diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c
index 056e2c9ec305..2b0f84ae4367 100644
--- a/arch/x86/kernel/shstk.c
+++ b/arch/x86/kernel/shstk.c
@@ -200,20 +200,19 @@ int arch_shstk_validate_clone(struct task_struct *t,
         * SSP is aligned, so reserved bits and mode bit are a zero, just mark
         * the token 64-bit.
         */
-       void *maddr = kmap_local_page(page);
+       void *maddr = page_address(page);
        int offset;
-       unsigned long addr, ssp;
+       unsigned long ssp;
        u64 expected;
 
        if (!features_enabled(ARCH_SHSTK_SHSTK))
                return 0;
 
        ssp = args->shadow_stack_pointer;
-       addr = ssp - SS_FRAME_SIZE;
-       expected = ssp | BIT(0);
-       offset = offset_in_page(addr);
+       expected = (ssp + SS_FRAME_SIZE) | BIT(0);
+       offset = offset_in_page(ssp);
 
-       if (!cmpxchg_to_user_page(vma, page, addr, (unsigned long *)(maddr +
offset),
+       if (!cmpxchg_to_user_page(vma, page, ssp, (unsigned long *)(maddr +
offset),
                                  expected, 0))
                return -EINVAL;
        set_page_dirty_lock(page);




  reply	other threads:[~2025-04-14 18:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-08 18:49 [PATCH RFT v15 0/8] fork: Support shadow stacks in clone3() Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 1/8] arm64/gcs: Return a success value from gcs_alloc_thread_stack() Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 2/8] Documentation: userspace-api: Add shadow stack API documentation Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 3/8] selftests: Provide helper header for shadow stack testing Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 4/8] fork: Add shadow stack support to clone3() Mark Brown
2025-04-14 18:28   ` Edgecombe, Rick P [this message]
2025-04-15 12:56     ` Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 5/8] selftests/clone3: Remove redundant flushes of output streams Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 6/8] selftests/clone3: Factor more of main loop into test_clone3() Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 7/8] selftests/clone3: Allow tests to flag if -E2BIG is a valid error code Mark Brown
2025-04-08 18:49 ` [PATCH RFT v15 8/8] selftests/clone3: Test shadow stack support Mark Brown

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=b15bf40b530f74d3339a313e7f5f5cb09205d348.camel@intel.com \
    --to=rick.p.edgecombe@intel.com \
    --cc=Szabolcs.Nagy@arm.com \
    --cc=bp@alien8.de \
    --cc=brauner@kernel.org \
    --cc=broonie@kernel.org \
    --cc=bsegall@google.com \
    --cc=catalin.marinas@arm.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=debug@rivosinc.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=fweimer@redhat.com \
    --cc=hjl.tools@gmail.com \
    --cc=hpa@zytor.com \
    --cc=jannh@google.com \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    --cc=wilco.dijkstra@arm.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    --cc=yury.khrustalev@arm.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;
as well as URLs for NNTP newsgroup(s).