linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Edgecombe, Rick P" <rick.p.edgecombe@intel.com>
To: "jolsa@kernel.org" <jolsa@kernel.org>,
	"peterz@infradead.org" <peterz@infradead.org>,
	"mhiramat@kernel.org" <mhiramat@kernel.org>,
	"andrii@kernel.org" <andrii@kernel.org>,
	"oleg@redhat.com" <oleg@redhat.com>
Cc: "songliubraving@fb.com" <songliubraving@fb.com>,
	"alx@kernel.org" <alx@kernel.org>,
	"alan.maguire@oracle.com" <alan.maguire@oracle.com>,
	"David.Laight@ACULAB.COM" <David.Laight@ACULAB.COM>,
	"john.fastabend@gmail.com" <john.fastabend@gmail.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"mingo@kernel.org" <mingo@kernel.org>,
	"rostedt@goodmis.org" <rostedt@goodmis.org>,
	"yhs@fb.com" <yhs@fb.com>,
	"eyal.birger@gmail.com" <eyal.birger@gmail.com>,
	"kees@kernel.org" <kees@kernel.org>,
	"linux-trace-kernel@vger.kernel.org"
	<linux-trace-kernel@vger.kernel.org>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	"thomas@t-8ch.de" <thomas@t-8ch.de>,
	"haoluo@google.com" <haoluo@google.com>,
	"x86@kernel.org" <x86@kernel.org>
Subject: Re: [PATCH 4/6] uprobes/x86: Fix uprobe syscall vs shadow stack
Date: Thu, 21 Aug 2025 18:26:13 +0000	[thread overview]
Message-ID: <09238f699d47d92ef93a7621e28e7b1c0c2b7114.camel@intel.com> (raw)
In-Reply-To: <20250821123657.055790090@infradead.org>

On Thu, 2025-08-21 at 14:28 +0200, Peter Zijlstra wrote:
> The uprobe syscall stores and strips the trampoline stack frame from
> the user context, to make it appear similar to an exception at the
> original instruction. It then restores the trampoline stack when it
> can exit using sysexit.
> 
> Make sure to match the regular stack manipulation with shadow stack
> operations such that regular and shadow stack don't get out of sync
> and causes trouble.
> 
> This enables using the optimization when shadow stack is in use.
> 
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  arch/x86/include/asm/shstk.h |    4 ++++
>  arch/x86/kernel/shstk.c      |   40 ++++++++++++++++++++++++++++++++++++++++
>  arch/x86/kernel/uprobes.c    |   17 ++++++++---------
>  3 files changed, 52 insertions(+), 9 deletions(-)
> 
> --- a/arch/x86/include/asm/shstk.h
> +++ b/arch/x86/include/asm/shstk.h
> @@ -23,6 +23,8 @@ int setup_signal_shadow_stack(struct ksi
>  int restore_signal_shadow_stack(void);
>  int shstk_update_last_frame(unsigned long val);
>  bool shstk_is_enabled(void);
> +int shstk_pop(u64 *val);
> +int shstk_push(u64 val);
>  #else
>  static inline long shstk_prctl(struct task_struct *task, int option,
>  			       unsigned long arg2) { return -EINVAL; }
> @@ -35,6 +37,8 @@ static inline int setup_signal_shadow_st
>  static inline int restore_signal_shadow_stack(void) { return 0; }
>  static inline int shstk_update_last_frame(unsigned long val) { return 0; }
>  static inline bool shstk_is_enabled(void) { return false; }
> +static inline int shstk_pop(u64 *val) { return -ENOTSUPP; }
> +static inline int shstk_push(u64 val) { return -ENOTSUPP; }
>  #endif /* CONFIG_X86_USER_SHADOW_STACK */
>  
>  #endif /* __ASSEMBLER__ */
> --- a/arch/x86/kernel/shstk.c
> +++ b/arch/x86/kernel/shstk.c
> @@ -246,6 +246,46 @@ static unsigned long get_user_shstk_addr
>  	return ssp;
>  }
>  
> +int shstk_pop(u64 *val)
> +{
> +	int ret = 0;
> +	u64 ssp;
> +
> +	if (!features_enabled(ARCH_SHSTK_SHSTK))
> +		return -ENOTSUPP;
> +
> +	fpregs_lock_and_load();
> +
> +	rdmsrq(MSR_IA32_PL3_SSP, ssp);
> +	if (val && get_user(*val, (__user u64 *)ssp))

It makes it so shstk_pop() can incssp without pushing anything to the shadow
stack, but nothing uses this.

Also, since there is no read_user_shstk_64() it should probably check that the
VMA is actually shadow stack, like how it does in shstk_pop_sigframe(). What
this actually would expose, I'm not sure. It might be ok. There would just be a
fault later during shstk_push(args.retaddr) I guess.

Hmm, I guess no strong objections, but I'm still not sure it's worth supporting
the optimization.


> +		ret = -EFAULT;
> +	else
> +		wrmsrq(MSR_IA32_PL3_SSP, ssp + SS_FRAME_SIZE);
> +	fpregs_unlock();
> +
> +	return ret;
> +}
> +
> +int shstk_push(u64 val)
> +{
> +	u64 ssp;
> +	int ret;
> +
> +	if (!features_enabled(ARCH_SHSTK_SHSTK))
> +		return -ENOTSUPP;
> +
> +	fpregs_lock_and_load();
> +
> +	rdmsrq(MSR_IA32_PL3_SSP, ssp);
> +	ssp -= SS_FRAME_SIZE;
> +	ret = write_user_shstk_64((__user void *)ssp, val);
> +	if (!ret)
> +		wrmsrq(MSR_IA32_PL3_SSP, ssp);
> +	fpregs_unlock();
> +
> +	return ret;
> +}
> +
>  #define SHSTK_DATA_BIT BIT(63)
>  
>  static int put_shstk_data(u64 __user *addr, u64 data)
> --- a/arch/x86/kernel/uprobes.c
> +++ b/arch/x86/kernel/uprobes.c
> @@ -804,7 +804,7 @@ SYSCALL_DEFINE0(uprobe)
>  {
>  	struct pt_regs *regs = task_pt_regs(current);
>  	struct uprobe_syscall_args args;
> -	unsigned long ip, sp;
> +	unsigned long ip, sp, sret;
>  	int err;
>  
>  	/* Allow execution only from uprobe trampolines. */
> @@ -831,6 +831,10 @@ SYSCALL_DEFINE0(uprobe)
>  
>  	sp = regs->sp;
>  
> +	err = shstk_pop((u64 *)&sret);
> +	if (err == -EFAULT || (!err && sret != args.retaddr))
> +		goto sigill;
> +
>  	handle_syscall_uprobe(regs, regs->ip);
>  
>  	/*
> @@ -855,6 +859,9 @@ SYSCALL_DEFINE0(uprobe)
>  	if (args.retaddr - 5 != regs->ip)
>  		args.retaddr = regs->ip;
>  
> +	if (shstk_push(args.retaddr) == -EFAULT)
> +		goto sigill;
> +
>  	regs->ip = ip;
>  
>  	err = copy_to_user((void __user *)regs->sp, &args, sizeof(args));
> @@ -1124,14 +1131,6 @@ void arch_uprobe_optimize(struct arch_up
>  	struct mm_struct *mm = current->mm;
>  	uprobe_opcode_t insn[5];
>  
> -	/*
> -	 * Do not optimize if shadow stack is enabled, the return address hijack
> -	 * code in arch_uretprobe_hijack_return_addr updates wrong frame when
> -	 * the entry uprobe is optimized and the shadow stack crashes the app.
> -	 */
> -	if (shstk_is_enabled())
> -		return;
> -
>  	if (!should_optimize(auprobe))
>  		return;
>  
> 
> 


  reply	other threads:[~2025-08-21 18:26 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-21 12:28 [PATCH 0/6] uprobes/x86: Cleanups and fixes Peter Zijlstra
2025-08-21 12:28 ` [PATCH 1/6] uprobes/x86: Add struct uretprobe_syscall_args Peter Zijlstra
2025-08-21 12:28 ` [PATCH 2/6] uprobes/x86: Optimize is_optimize() Peter Zijlstra
2025-08-26  5:51   ` David Laight
2025-08-26  8:18     ` Peter Zijlstra
2025-08-27 19:32       ` David Laight
2025-08-26  8:25     ` Jiri Olsa
2025-08-26  8:33       ` Jiri Olsa
2025-08-21 12:28 ` [PATCH 3/6] uprobes/x86: Accept more NOP forms Peter Zijlstra
2025-08-21 12:28 ` [PATCH 4/6] uprobes/x86: Fix uprobe syscall vs shadow stack Peter Zijlstra
2025-08-21 18:26   ` Edgecombe, Rick P [this message]
2025-08-21 12:28 ` [PATCH 5/6] uprobes/x86: Make asm style consistent Peter Zijlstra
2025-08-21 12:28 ` [PATCH 6/6] uprobes/x86: Add SLS mitigation to the trampolines Peter Zijlstra
2025-08-21 14:18 ` [PATCH 0/6] uprobes/x86: Cleanups and fixes Jiri Olsa
2025-08-21 18:27   ` Edgecombe, Rick P
2025-08-21 19:52     ` Jiri Olsa
2025-08-21 19:57       ` Edgecombe, Rick P
2025-08-22  8:42   ` Jiri Olsa
2025-08-22 18:05     ` Andrii Nakryiko
2025-08-22 15:51 ` Oleg Nesterov

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=09238f699d47d92ef93a7621e28e7b1c0c2b7114.camel@intel.com \
    --to=rick.p.edgecombe@intel.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=alan.maguire@oracle.com \
    --cc=alx@kernel.org \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=eyal.birger@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=thomas@t-8ch.de \
    --cc=x86@kernel.org \
    --cc=yhs@fb.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).