]> granicus.if.org Git - strace/blob - tests/seccomp_get_action_avail.c
strace: terminate itself if interrupted by a signal
[strace] / tests / seccomp_get_action_avail.c
1 /*
2  * Check decoding of seccomp SECCOMP_GET_ACTION_AVAIL.
3  *
4  * Copyright (c) 2017 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "tests.h"
11 #include <asm/unistd.h>
12
13 #ifdef __NR_seccomp
14
15 # include <stdio.h>
16 # include <stdint.h>
17 # include <unistd.h>
18
19 # ifdef HAVE_LINUX_SECCOMP_H
20 #  include <linux/seccomp.h>
21 # endif
22
23 # ifndef SECCOMP_GET_ACTION_AVAIL
24 #  define SECCOMP_GET_ACTION_AVAIL 2
25 # endif
26
27 static const char *errstr;
28
29 static long
30 k_seccomp(const kernel_ulong_t op, const kernel_ulong_t flags,
31           const kernel_ulong_t args)
32 {
33         const long rc = syscall(__NR_seccomp, op, flags, args);
34         errstr = sprintrc(rc);
35         return rc;
36 }
37
38 int
39 main(void)
40 {
41         TAIL_ALLOC_OBJECT_CONST_PTR(uint32_t, act);
42         kernel_ulong_t op = (kernel_ulong_t) 0xfacefeed00000000ULL
43                                 | SECCOMP_GET_ACTION_AVAIL;
44         kernel_ulong_t flags = (kernel_ulong_t) 0xdeadbeef00000000ULL;
45         unsigned int i;
46
47         struct {
48                 uint32_t val;
49                 const char *str;
50         } actions [] = {
51                 { 0, "SECCOMP_RET_KILL_THREAD" },
52 # ifdef SECCOMP_RET_KILL_PROCESS
53                 { ARG_STR(SECCOMP_RET_KILL_PROCESS) },
54 # endif
55 # ifdef SECCOMP_RET_TRAP
56                 { ARG_STR(SECCOMP_RET_TRAP) },
57 # endif
58 # ifdef SECCOMP_RET_ERRNO
59                 { ARG_STR(SECCOMP_RET_ERRNO) },
60 # endif
61 # ifdef SECCOMP_RET_TRACE
62                 { ARG_STR(SECCOMP_RET_TRACE) },
63 # endif
64 # ifdef SECCOMP_RET_LOG
65                 { ARG_STR(SECCOMP_RET_LOG) },
66 # endif
67 # ifdef SECCOMP_RET_ALLOW
68                 { ARG_STR(SECCOMP_RET_ALLOW) },
69 # endif
70                 { 0xffffffff, "0xffffffff /* SECCOMP_RET_??? */" }
71         };
72
73         for (i = 0; i < ARRAY_SIZE(actions); ++i) {
74                 *act = actions[i].val;
75                 k_seccomp(op, flags, (uintptr_t) act);
76                 printf("seccomp(SECCOMP_GET_ACTION_AVAIL, 0, [%s]) = %s\n",
77                        actions[i].str, errstr);
78         }
79
80         *act = actions[0].val;
81
82         k_seccomp(op, flags, (uintptr_t) (act + 1));
83         printf("seccomp(SECCOMP_GET_ACTION_AVAIL, 0, %p) = %s\n",
84                act + 1, errstr);
85
86         if (F8ILL_KULONG_SUPPORTED) {
87                 k_seccomp(op, flags, f8ill_ptr_to_kulong(act));
88                 printf("seccomp(SECCOMP_GET_ACTION_AVAIL, 0, %#jx) = %s\n",
89                        (uintmax_t) f8ill_ptr_to_kulong(act), errstr);
90         }
91
92         flags |= 0xcafef00d;
93         k_seccomp(op, flags, 0);
94         printf("seccomp(SECCOMP_GET_ACTION_AVAIL, %u, NULL) = %s\n",
95                (unsigned int) flags, errstr);
96
97         puts("+++ exited with 0 +++");
98         return 0;
99 }
100
101 #else
102
103 SKIP_MAIN_UNDEFINED("__NR_seccomp")
104
105 #endif