Skip to content

Commit 71c87fb

Browse files
committed
selftests/seccomp: Record syscall during ptrace entry
In preparation for performing actions during ptrace syscall exit, save the syscall number during ptrace syscall entry. Some architectures do no have the syscall number available during ptrace syscall exit. Suggested-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com> Link: https://lore.kernel.org/linux-kselftest/20200911181012.171027-1-cascardo@canonical.com/ Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Link: https://lore.kernel.org/lkml/20200921074354.6shkt2e5yhzhj3sn@wittgenstein/ Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 4613832 commit 71c87fb

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,12 +1949,19 @@ void tracer_seccomp(struct __test_metadata *_metadata, pid_t tracee,
19491949

19501950
}
19511951

1952+
FIXTURE(TRACE_syscall) {
1953+
struct sock_fprog prog;
1954+
pid_t tracer, mytid, mypid, parent;
1955+
long syscall_nr;
1956+
};
1957+
19521958
void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
19531959
int status, void *args)
19541960
{
1955-
int ret, nr;
1961+
int ret;
19561962
unsigned long msg;
19571963
static bool entry;
1964+
FIXTURE_DATA(TRACE_syscall) *self = args;
19581965

19591966
/*
19601967
* The traditional way to tell PTRACE_SYSCALL entry/exit
@@ -1968,24 +1975,31 @@ void tracer_ptrace(struct __test_metadata *_metadata, pid_t tracee,
19681975
EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
19691976
: PTRACE_EVENTMSG_SYSCALL_EXIT, msg);
19701977

1971-
if (!entry)
1978+
/*
1979+
* Some architectures only support setting return values during
1980+
* syscall exit under ptrace, and on exit the syscall number may
1981+
* no longer be available. Therefore, save the initial sycall
1982+
* number here, so it can be examined during both entry and exit
1983+
* phases.
1984+
*/
1985+
if (entry)
1986+
self->syscall_nr = get_syscall(_metadata, tracee);
1987+
else
19721988
return;
19731989

1974-
nr = get_syscall(_metadata, tracee);
1975-
1976-
if (nr == __NR_getpid)
1990+
switch (self->syscall_nr) {
1991+
case __NR_getpid:
19771992
change_syscall(_metadata, tracee, __NR_getppid, 0);
1978-
if (nr == __NR_gettid)
1993+
break;
1994+
case __NR_gettid:
19791995
change_syscall(_metadata, tracee, -1, 45000);
1980-
if (nr == __NR_openat)
1996+
break;
1997+
case __NR_openat:
19811998
change_syscall(_metadata, tracee, -1, -ESRCH);
1999+
break;
2000+
}
19822001
}
19832002

1984-
FIXTURE(TRACE_syscall) {
1985-
struct sock_fprog prog;
1986-
pid_t tracer, mytid, mypid, parent;
1987-
};
1988-
19892003
FIXTURE_VARIANT(TRACE_syscall) {
19902004
/*
19912005
* All of the SECCOMP_RET_TRACE behaviors can be tested with either
@@ -2044,7 +2058,7 @@ FIXTURE_SETUP(TRACE_syscall)
20442058
self->tracer = setup_trace_fixture(_metadata,
20452059
variant->use_ptrace ? tracer_ptrace
20462060
: tracer_seccomp,
2047-
NULL, variant->use_ptrace);
2061+
self, variant->use_ptrace);
20482062

20492063
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
20502064
ASSERT_EQ(0, ret);

0 commit comments

Comments
 (0)