Re: [PATCH] selftests/seccomp: fix build on older kernels

From: Kees Cook
Date: Thu Aug 29 2019 - 13:06:12 EST


On Mon, Aug 26, 2019 at 08:43:02AM -0600, Tycho Andersen wrote:
> The seccomp selftest goes to some length to build against older kernel
> headers, viz. all the #ifdefs at the beginning of the file. 201766a20e30
> ("ptrace: add PTRACE_GET_SYSCALL_INFO request") introduces some additional
> macros, but doesn't do the #ifdef dance. Let's add that dance here to
> avoid:
>
> gcc -Wl,-no-as-needed -Wall seccomp_bpf.c -lpthread -o seccomp_bpf
> In file included from seccomp_bpf.c:51:
> seccomp_bpf.c: In function âtracer_ptraceâ:
> seccomp_bpf.c:1787:20: error: âPTRACE_EVENTMSG_SYSCALL_ENTRYâ undeclared (first use in this function); did you mean âPTRACE_EVENT_CLONEâ?
> EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../kselftest_harness.h:608:13: note: in definition of macro â__EXPECTâ
> __typeof__(_expected) __exp = (_expected); \
> ^~~~~~~~~
> seccomp_bpf.c:1787:2: note: in expansion of macro âEXPECT_EQâ
> EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
> ^~~~~~~~~
> seccomp_bpf.c:1787:20: note: each undeclared identifier is reported only once for each function it appears in
> EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../kselftest_harness.h:608:13: note: in definition of macro â__EXPECTâ
> __typeof__(_expected) __exp = (_expected); \
> ^~~~~~~~~
> seccomp_bpf.c:1787:2: note: in expansion of macro âEXPECT_EQâ
> EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
> ^~~~~~~~~
> seccomp_bpf.c:1788:6: error: âPTRACE_EVENTMSG_SYSCALL_EXITâ undeclared (first use in this function); did you mean âPTRACE_EVENT_EXITâ?
> : PTRACE_EVENTMSG_SYSCALL_EXIT, msg);
> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../kselftest_harness.h:608:13: note: in definition of macro â__EXPECTâ
> __typeof__(_expected) __exp = (_expected); \
> ^~~~~~~~~
> seccomp_bpf.c:1787:2: note: in expansion of macro âEXPECT_EQâ
> EXPECT_EQ(entry ? PTRACE_EVENTMSG_SYSCALL_ENTRY
> ^~~~~~~~~
> make: *** [Makefile:12: seccomp_bpf] Error 1
>
> Signed-off-by: Tycho Andersen <tycho@xxxxxxxx>
> Fixes: 201766a20e30 ("ptrace: add PTRACE_GET_SYSCALL_INFO request")

Acked-by: Kees Cook <keescook@xxxxxxxxxxxx>

Alakesh Haloi also sent a fix[1] for this. I prefer Tycho's solution
(one #ifndef and a Fixes line). Shuah, can you please apply this?

Thanks!

-Kees

[1] https://lore.kernel.org/lkml/20190822215823.GA11292@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

> ---
> tools/testing/selftests/seccomp/seccomp_bpf.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/tools/testing/selftests/seccomp/seccomp_bpf.c b/tools/testing/selftests/seccomp/seccomp_bpf.c
> index 6ef7f16c4cf5..7f8b5c8982e3 100644
> --- a/tools/testing/selftests/seccomp/seccomp_bpf.c
> +++ b/tools/testing/selftests/seccomp/seccomp_bpf.c
> @@ -199,6 +199,11 @@ struct seccomp_notif_sizes {
> };
> #endif
>
> +#ifndef PTRACE_EVENTMSG_SYSCALL_ENTRY
> +#define PTRACE_EVENTMSG_SYSCALL_ENTRY 1
> +#define PTRACE_EVENTMSG_SYSCALL_EXIT 2
> +#endif
> +
> #ifndef seccomp
> int seccomp(unsigned int op, unsigned int flags, void *args)
> {
> --
> 2.20.1
>

--
Kees Cook