Linux-man Archive mirror
 help / color / mirror / Atom feed
From: Jiri Olsa <olsajiri@gmail.com>
To: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	Oleg Nesterov <oleg@redhat.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
	linux-api@vger.kernel.org, linux-man@vger.kernel.org,
	x86@kernel.org, bpf@vger.kernel.org,
	Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
	John Fastabend <john.fastabend@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	"Borislav Petkov (AMD)" <bp@alien8.de>,
	Ingo Molnar <mingo@redhat.com>, Andy Lutomirski <luto@kernel.org>,
	"Edgecombe, Rick P" <rick.p.edgecombe@intel.com>,
	Deepak Gupta <debug@rivosinc.com>
Subject: Re: [PATCHv5 bpf-next 7/8] selftests/x86: Add return uprobe shadow stack test
Date: Mon, 13 May 2024 15:28:21 -0600	[thread overview]
Message-ID: <ZkKF9WZVfFgiVSxe@krava> (raw)
In-Reply-To: <20240513184507.215ec89dea4790243d17a52c@kernel.org>

On Mon, May 13, 2024 at 06:45:07PM +0900, Masami Hiramatsu wrote:
> On Tue,  7 May 2024 12:53:20 +0200
> Jiri Olsa <jolsa@kernel.org> wrote:
> 
> > Adding return uprobe test for shadow stack and making sure it's
> > working properly. Borrowed some of the code from bpf selftests.
> 
> Hi Jiri,
> 
> I can not find "SKIP" result in this change. If CONFIG_UPROBES=n,
> this should skip uprobe test.

ah it should be detected by parse_uint_from_file returning ENOENT
or something like that.. will add that

thanks,
jirka

> 
> Thank you,
> 
> > 
> > Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> > ---
> >  .../testing/selftests/x86/test_shadow_stack.c | 142 ++++++++++++++++++
> >  1 file changed, 142 insertions(+)
> > 
> > diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c
> > index 757e6527f67e..1b919baa999b 100644
> > --- a/tools/testing/selftests/x86/test_shadow_stack.c
> > +++ b/tools/testing/selftests/x86/test_shadow_stack.c
> > @@ -34,6 +34,7 @@
> >  #include <sys/ptrace.h>
> >  #include <sys/signal.h>
> >  #include <linux/elf.h>
> > +#include <linux/perf_event.h>
> >  
> >  /*
> >   * Define the ABI defines if needed, so people can run the tests
> > @@ -681,6 +682,141 @@ int test_32bit(void)
> >  	return !segv_triggered;
> >  }
> >  
> > +static int parse_uint_from_file(const char *file, const char *fmt)
> > +{
> > +	int err, ret;
> > +	FILE *f;
> > +
> > +	f = fopen(file, "re");
> > +	if (!f) {
> > +		err = -errno;
> > +		printf("failed to open '%s': %d\n", file, err);
> > +		return err;
> > +	}
> > +	err = fscanf(f, fmt, &ret);
> > +	if (err != 1) {
> > +		err = err == EOF ? -EIO : -errno;
> > +		printf("failed to parse '%s': %d\n", file, err);
> > +		fclose(f);
> > +		return err;
> > +	}
> > +	fclose(f);
> > +	return ret;
> > +}
> > +
> > +static int determine_uprobe_perf_type(void)
> > +{
> > +	const char *file = "/sys/bus/event_source/devices/uprobe/type";
> > +
> > +	return parse_uint_from_file(file, "%d\n");
> > +}
> > +
> > +static int determine_uprobe_retprobe_bit(void)
> > +{
> > +	const char *file = "/sys/bus/event_source/devices/uprobe/format/retprobe";
> > +
> > +	return parse_uint_from_file(file, "config:%d\n");
> > +}
> > +
> > +static ssize_t get_uprobe_offset(const void *addr)
> > +{
> > +	size_t start, end, base;
> > +	char buf[256];
> > +	bool found = false;
> > +	FILE *f;
> > +
> > +	f = fopen("/proc/self/maps", "r");
> > +	if (!f)
> > +		return -errno;
> > +
> > +	while (fscanf(f, "%zx-%zx %s %zx %*[^\n]\n", &start, &end, buf, &base) == 4) {
> > +		if (buf[2] == 'x' && (uintptr_t)addr >= start && (uintptr_t)addr < end) {
> > +			found = true;
> > +			break;
> > +		}
> > +	}
> > +
> > +	fclose(f);
> > +
> > +	if (!found)
> > +		return -ESRCH;
> > +
> > +	return (uintptr_t)addr - start + base;
> > +}
> > +
> > +static __attribute__((noinline)) void uretprobe_trigger(void)
> > +{
> > +	asm volatile ("");
> > +}
> > +
> > +/*
> > + * This test setups return uprobe, which is sensitive to shadow stack
> > + * (crashes without extra fix). After executing the uretprobe we fail
> > + * the test if we receive SIGSEGV, no crash means we're good.
> > + *
> > + * Helper functions above borrowed from bpf selftests.
> > + */
> > +static int test_uretprobe(void)
> > +{
> > +	const size_t attr_sz = sizeof(struct perf_event_attr);
> > +	const char *file = "/proc/self/exe";
> > +	int bit, fd = 0, type, err = 1;
> > +	struct perf_event_attr attr;
> > +	struct sigaction sa = {};
> > +	ssize_t offset;
> > +
> > +	type = determine_uprobe_perf_type();
> > +	if (type < 0)
> > +		return 1;
> > +
> > +	offset = get_uprobe_offset(uretprobe_trigger);
> > +	if (offset < 0)
> > +		return 1;
> > +
> > +	bit = determine_uprobe_retprobe_bit();
> > +	if (bit < 0)
> > +		return 1;
> > +
> > +	sa.sa_sigaction = segv_gp_handler;
> > +	sa.sa_flags = SA_SIGINFO;
> > +	if (sigaction(SIGSEGV, &sa, NULL))
> > +		return 1;
> > +
> > +	/* Setup return uprobe through perf event interface. */
> > +	memset(&attr, 0, attr_sz);
> > +	attr.size = attr_sz;
> > +	attr.type = type;
> > +	attr.config = 1 << bit;
> > +	attr.config1 = (__u64) (unsigned long) file;
> > +	attr.config2 = offset;
> > +
> > +	fd = syscall(__NR_perf_event_open, &attr, 0 /* pid */, -1 /* cpu */,
> > +		     -1 /* group_fd */, PERF_FLAG_FD_CLOEXEC);
> > +	if (fd < 0)
> > +		goto out;
> > +
> > +	if (sigsetjmp(jmp_buffer, 1))
> > +		goto out;
> > +
> > +	ARCH_PRCTL(ARCH_SHSTK_ENABLE, ARCH_SHSTK_SHSTK);
> > +
> > +	/*
> > +	 * This either segfaults and goes through sigsetjmp above
> > +	 * or succeeds and we're good.
> > +	 */
> > +	uretprobe_trigger();
> > +
> > +	printf("[OK]\tUretprobe test\n");
> > +	err = 0;
> > +
> > +out:
> > +	ARCH_PRCTL(ARCH_SHSTK_DISABLE, ARCH_SHSTK_SHSTK);
> > +	signal(SIGSEGV, SIG_DFL);
> > +	if (fd)
> > +		close(fd);
> > +	return err;
> > +}
> > +
> >  void segv_handler_ptrace(int signum, siginfo_t *si, void *uc)
> >  {
> >  	/* The SSP adjustment caused a segfault. */
> > @@ -867,6 +1003,12 @@ int main(int argc, char *argv[])
> >  		goto out;
> >  	}
> >  
> > +	if (test_uretprobe()) {
> > +		ret = 1;
> > +		printf("[FAIL]\turetprobe test\n");
> > +		goto out;
> > +	}
> > +
> >  	return ret;
> >  
> >  out:
> > -- 
> > 2.44.0
> > 
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>

  reply	other threads:[~2024-05-13 21:28 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-07 10:53 [PATCHv5 bpf-next 0/8] uprobe: uretprobe speed up Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 1/8] uprobe: Wire up uretprobe system call Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 2/8] uprobe: Add uretprobe syscall to speed up return probe Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 3/8] selftests/bpf: Add uretprobe syscall test for regs integrity Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 4/8] selftests/bpf: Add uretprobe syscall test for regs changes Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 5/8] selftests/bpf: Add uretprobe syscall call from user space test Jiri Olsa
2024-05-07 16:57   ` Andrii Nakryiko
2024-05-07 10:53 ` [PATCHv5 bpf-next 6/8] x86/shstk: Add return uprobe support Jiri Olsa
2024-05-07 17:35   ` Edgecombe, Rick P
2024-05-09  8:30     ` Jiri Olsa
2024-05-09 16:24       ` Edgecombe, Rick P
2024-05-11 21:09         ` Jiri Olsa
2024-05-13  9:50           ` Masami Hiramatsu
2024-05-13 17:12             ` Edgecombe, Rick P
2024-05-13 21:23               ` Jiri Olsa
2024-05-15  1:10                 ` Edgecombe, Rick P
2024-05-15  1:44                   ` Deepak Gupta
2024-05-15 11:19                     ` Oleg Nesterov
2024-05-15 14:36                       ` Jiri Olsa
2024-05-15 15:18                         ` Edgecombe, Rick P
2024-05-15 15:26                         ` Oleg Nesterov
2024-05-15 15:31                           ` Edgecombe, Rick P
2024-05-15 11:35                 ` Oleg Nesterov
2024-05-15 15:13                   ` Edgecombe, Rick P
2024-05-15 15:42                     ` Oleg Nesterov
2024-05-19 22:18                       ` Jiri Olsa
2024-05-21  1:31                         ` Edgecombe, Rick P
2024-05-21 10:11                           ` Jiri Olsa
2024-05-07 10:53 ` [PATCHv5 bpf-next 7/8] selftests/x86: Add return uprobe shadow stack test Jiri Olsa
2024-05-13  9:45   ` Masami Hiramatsu
2024-05-13 21:28     ` Jiri Olsa [this message]
2024-05-07 10:53 ` [PATCHv5 8/8] man2: Add uretprobe syscall page Jiri Olsa
2024-05-07 11:13   ` Dmitry V. Levin
2024-05-07 11:49     ` Jiri Olsa
2024-05-07 13:44       ` Alejandro Colomar

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=ZkKF9WZVfFgiVSxe@krava \
    --to=olsajiri@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=debug@rivosinc.com \
    --cc=john.fastabend@gmail.com \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-man@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rick.p.edgecombe@intel.com \
    --cc=rostedt@goodmis.org \
    --cc=songliubraving@fb.com \
    --cc=tglx@linutronix.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 read-only IMAP folder(s) and NNTP newsgroup(s).