Skip to content

Commit

Permalink
tests: check decoding of SEGV_PKUERR
Browse files Browse the repository at this point in the history
* tests/segv_pkuerr.c: New file.
* tests/gen_tests.in (segv_pkuerr): New test.
* tests/Makefile.am (check_PROGRAMS): Add segv_pkuerr.
* tests/.gitignore: Likewise.
* tests/ptrace.c (main) [HAVE_SIGINFO_T_SI_PKEY]: Check decoding
of SEGV_PKUERR.
  • Loading branch information
ldv-alt committed Feb 3, 2022
1 parent d8bf380 commit d179de6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ seccomp_get_action_avail
seccomp_get_notif_sizes
seccomp_get_notif_sizes-success
secontext.am
segv_pkuerr
select
select-P
semop
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
seccomp-filter-v \
seccomp-strict \
seccomp_get_notif_sizes-success \
segv_pkuerr \
select-P \
set_ptracer_any \
set_sigblock \
Expand Down
1 change: 1 addition & 0 deletions tests/gen_tests.in
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ seccomp-filter-v -v -e trace=seccomp
seccomp_get_action_avail -e trace=seccomp
seccomp_get_notif_sizes -etrace=seccomp -a42
seccomp_get_notif_sizes-success -einject=seccomp:retval=42 -etrace=seccomp -a42
segv_pkuerr -qq --trace=none
select -a36
select-P -a36 -e trace=select -P /dev/full 9>>/dev/full
semop -a32
Expand Down
16 changes: 16 additions & 0 deletions tests/ptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,22 @@ main(void)
XLAT_ARGS(PTRACE_SETSIGINFO), pid, bad_request,
XLAT_ARGS(SIGPROF), sip->si_code, sip->si_errno, errstr);

#ifdef HAVE_SIGINFO_T_SI_PKEY
memset(sip, -1, sizeof(*sip));
sip->si_signo = SIGSEGV;
sip->si_code = SEGV_PKUERR;
sip->si_errno = 0;
sip->si_addr = (void *) (unsigned long) 0xfacefeeddeadbeefULL;
sip->si_pkey = 0xbadc0ded;

do_ptrace(PTRACE_SETSIGINFO, pid, bad_request, (uintptr_t) sip);
printf("ptrace(" XLAT_FMT ", %d, %#lx, {si_signo=" XLAT_FMT_U
", si_code=" XLAT_FMT ", si_addr=%p, si_pkey=%u}) = %s\n",
XLAT_ARGS(PTRACE_SETSIGINFO), pid, bad_request,
XLAT_ARGS(SIGSEGV), XLAT_ARGS(SEGV_PKUERR),
sip->si_addr, sip->si_pkey, errstr);
#endif

#ifdef HAVE_SIGINFO_T_SI_SYSCALL
memset(sip, -1, sizeof(*sip));
sip->si_signo = SIGSYS;
Expand Down
56 changes: 56 additions & 0 deletions tests/segv_pkuerr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Check decoding of SEGV_PKUERR.
*
* Copyright (c) 2022 Dmitry V. Levin <ldv@strace.io>
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "tests.h"
#include <signal.h>

#if defined HAVE_SIGINFO_T_SI_PKEY && defined SEGV_PKUERR

# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/mman.h>

static void
handler(int sig, siginfo_t *info, void *ucontext)
{
if (info->si_code != SEGV_PKUERR)
error_msg_and_skip("SIGSEGV: si_code = %d", info->si_code);

printf("--- SIGSEGV {si_signo=SIGSEGV, si_code=SEGV_PKUERR"
", si_addr=%p, si_pkey=%u} ---\n",
info->si_addr, info->si_pkey);
exit(0);
}

int
main(void) {
int *p = mmap(NULL, get_page_size(), PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (p == MAP_FAILED)
perror_msg_and_fail("mmap");

const struct sigaction act = {
.sa_sigaction = handler,
.sa_flags = SA_SIGINFO | SA_RESETHAND
};
if (sigaction(SIGSEGV, &act, NULL))
perror_msg_and_fail("sigaction");

if (*p)
__asm__ volatile("":::"memory");

error_msg_and_skip("PROT_EXEC page is readable");
}

#else

SKIP_MAIN_UNDEFINED("HAVE_SIGINFO_T_SI_PKEY && SEGV_PKUERR")

#endif

0 comments on commit d179de6

Please sign in to comment.