Skip to content

Commit b2f15f3

Browse files
drakenclimberpcmoore
authored andcommitted
api: Add support for SCMP_ACT_KILL_PROCESS
This patch adds support for killing the entire process via the SCMP_ACT_KILL_PROCESS action. To maintain backward compatibility, SCMP_ACT_KILL defaults to SCMP_ACT_KILL_THREAD. Support for KILL_PROCESS was added into the Linux kernel in v4.14. This addresses GitHub Issue #96 - RFE: add support for SECCOMP_RET_KILL_PROCESS Signed-off-by: Tom Hromatka <[email protected]> [PM: minor comment tweak in seccomp.h.in] Signed-off-by: Paul Moore <[email protected]>
1 parent 6646e21 commit b2f15f3

File tree

12 files changed

+75
-13
lines changed

12 files changed

+75
-13
lines changed

doc/man/man3/seccomp_init.3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ The thread will be terminated by the kernel with SIGSYS when it calls a syscall
5252
that does not match any of the configured seccomp filter rules. The thread
5353
will not be able to catch the signal.
5454
.TP
55+
.B SCMP_ACT_KILL_PROCESS
56+
The entire process will be terminated by the kernel with SIGSYS when it calls a
57+
syscall that does not match any of the configured seccomp filter rules.
58+
.TP
5559
.B SCMP_ACT_TRAP
5660
The thread will be sent a SIGSYS signal when it calls a syscall that does not
5761
match any of the configured seccomp filter rules. It may catch this and change

doc/man/man3/seccomp_rule_add.3

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ values are as follows:
111111
The thread will be killed by the kernel when it calls a syscall that matches
112112
the filter rule.
113113
.TP
114+
.B SCMP_ACT_KILL_PROCESS
115+
The process will be killed by the kernel when it calls a syscall that matches
116+
the filter rule.
117+
.TP
114118
.B SCMP_ACT_TRAP
115119
The thread will throw a SIGSYS signal when it calls a syscall that matches the
116120
filter rule.

include/seccomp.h.in

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,15 @@ struct scmp_arg_cmp {
244244
/**
245245
* Kill the process
246246
*/
247-
#define SCMP_ACT_KILL 0x00000000U
247+
#define SCMP_ACT_KILL_PROCESS 0x80000000U
248+
/**
249+
* Kill the thread
250+
*/
251+
#define SCMP_ACT_KILL_THREAD 0x00000000U
252+
/**
253+
* Kill the thread, defined for backward compatibility
254+
*/
255+
#define SCMP_ACT_KILL SCMP_ACT_KILL_THREAD
248256
/**
249257
* Throw a SIGSYS signal
250258
*/
@@ -297,6 +305,7 @@ const struct scmp_version *seccomp_version(void);
297305
* uses the seccomp(2) syscall instead of the prctl(2) syscall
298306
* 3 : support for the SCMP_FLTATR_CTL_LOG filter attribute
299307
* support for the SCMP_ACT_LOG action
308+
* support for the SCMP_ACT_KILL_PROCESS action
300309
*
301310
*/
302311
unsigned int seccomp_api_get(void);

src/api.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,21 @@ API int seccomp_api_set(unsigned int level)
135135
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_TSYNC, false);
136136
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_LOG, false);
137137
sys_set_seccomp_action(SCMP_ACT_LOG, false);
138+
sys_set_seccomp_action(SCMP_ACT_KILL_PROCESS, false);
138139
break;
139140
case 2:
140141
sys_set_seccomp_syscall(true);
141142
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_TSYNC, true);
142143
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_LOG, false);
143144
sys_set_seccomp_action(SCMP_ACT_LOG, false);
145+
sys_set_seccomp_action(SCMP_ACT_KILL_PROCESS, false);
144146
break;
145147
case 3:
146148
sys_set_seccomp_syscall(true);
147149
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_TSYNC, true);
148150
sys_set_seccomp_flag(SECCOMP_FILTER_FLAG_LOG, true);
149151
sys_set_seccomp_action(SCMP_ACT_LOG, true);
152+
sys_set_seccomp_action(SCMP_ACT_KILL_PROCESS, true);
150153
break;
151154
default:
152155
return -EINVAL;

src/gen_pfc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "db.h"
3636
#include "gen_pfc.h"
3737
#include "helper.h"
38+
#include "system.h"
3839

3940
struct pfc_sys_list {
4041
struct db_sys_list *sys;
@@ -117,8 +118,11 @@ static void _pfc_arg(FILE *fds,
117118
*/
118119
static void _pfc_action(FILE *fds, uint32_t action)
119120
{
120-
switch (action & 0xffff0000) {
121-
case SCMP_ACT_KILL:
121+
switch (action & SECCOMP_RET_ACTION_FULL) {
122+
case SCMP_ACT_KILL_PROCESS:
123+
fprintf(fds, "action KILL_PROCESS;\n");
124+
break;
125+
case SCMP_ACT_KILL_THREAD:
122126
fprintf(fds, "action KILL;\n");
123127
break;
124128
case SCMP_ACT_TRAP:

src/python/libseccomp.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ cdef extern from "seccomp.h":
6969
SCMP_CMP_MASKED_EQ
7070

7171
cdef enum:
72+
SCMP_ACT_KILL_PROCESS
7273
SCMP_ACT_KILL
7374
SCMP_ACT_TRAP
7475
SCMP_ACT_LOG

src/python/seccomp.pyx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ based filtering interface that should be familiar to, and easily adopted
2929
by application developers.
3030
3131
Filter action values:
32-
KILL - kill the process
32+
KILL_PROCESS - kill the process
33+
KILL - kill the thread
3334
LOG - allow the syscall to be executed after the action has been logged
3435
ALLOW - allow the syscall to execute
3536
TRAP - a SIGSYS signal will be thrown
@@ -94,6 +95,7 @@ def c_str(string):
9495
else:
9596
return bytes(string, "ascii")
9697

98+
KILL_PROCESS = libseccomp.SCMP_ACT_KILL_PROCESS
9799
KILL = libseccomp.SCMP_ACT_KILL
98100
TRAP = libseccomp.SCMP_ACT_TRAP
99101
LOG = libseccomp.SCMP_ACT_LOG
@@ -545,7 +547,8 @@ cdef class SyscallFilter:
545547
""" Add a new rule to filter.
546548
547549
Arguments:
548-
action - the rule action: KILL, TRAP, ERRNO(), TRACE(), LOG, or ALLOW
550+
action - the rule action: KILL_PROCESS, KILL, TRAP, ERRNO(), TRACE(),
551+
LOG, or ALLOW
549552
syscall - the syscall name or number
550553
args - variable number of Arg objects
551554
@@ -627,7 +630,8 @@ cdef class SyscallFilter:
627630
""" Add a new rule to filter.
628631
629632
Arguments:
630-
action - the rule action: KILL, TRAP, ERRNO(), TRACE(), LOG, or ALLOW
633+
action - the rule action: KILL_PROCESS, KILL, TRAP, ERRNO(), TRACE(),
634+
LOG, or ALLOW
631635
syscall - the syscall name or number
632636
args - variable number of Arg objects
633637

src/system.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static int _support_seccomp_syscall = -1;
4343
static int _support_seccomp_flag_tsync = -1;
4444
static int _support_seccomp_flag_log = -1;
4545
static int _support_seccomp_action_log = -1;
46+
static int _support_seccomp_kill_process = -1;
4647

4748
/**
4849
* Check to see if the seccomp() syscall is supported
@@ -123,7 +124,18 @@ void sys_set_seccomp_syscall(bool enable)
123124
*/
124125
int sys_chk_seccomp_action(uint32_t action)
125126
{
126-
if (action == SCMP_ACT_KILL) {
127+
if (action == SCMP_ACT_KILL_PROCESS) {
128+
if (_support_seccomp_kill_process < 0) {
129+
if (sys_chk_seccomp_syscall() == 1 &&
130+
syscall(_nr_seccomp, SECCOMP_GET_ACTION_AVAIL, 0,
131+
&action) == 0)
132+
_support_seccomp_kill_process = 1;
133+
else
134+
_support_seccomp_kill_process = 0;
135+
}
136+
137+
return _support_seccomp_kill_process;
138+
} else if (action == SCMP_ACT_KILL_THREAD) {
127139
return 1;
128140
} else if (action == SCMP_ACT_TRAP) {
129141
return 1;
@@ -162,6 +174,8 @@ void sys_set_seccomp_action(uint32_t action, bool enable)
162174
{
163175
if (action == SCMP_ACT_LOG)
164176
_support_seccomp_action_log = (enable ? 1 : 0);
177+
else if (action == SCMP_ACT_KILL_PROCESS)
178+
_support_seccomp_kill_process = (enable ? 1 : 0);
165179
}
166180

167181
/**

src/system.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ struct db_filter_col;
5555
* The ordering ensures that a min_t() over composed return values always
5656
* selects the least permissive choice.
5757
*/
58-
#define SECCOMP_RET_KILL 0x00000000U /* kill the task immediately */
58+
#define SECCOMP_RET_KILL_PROCESS 0x80000000U /* kill the process immediately */
59+
#define SECCOMP_RET_KILL_THREAD 0x00000000U /* kill the thread immediately */
60+
#define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD /* default to killing the thread */
5961
#define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
6062
#define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
6163
#define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
6264
#define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
6365

6466
/* Masks for the return value sections. */
67+
#define SECCOMP_RET_ACTION_FULL 0xffff0000U
6568
#define SECCOMP_RET_ACTION 0x7fff0000U
6669
#define SECCOMP_RET_DATA 0x0000ffffU
6770

@@ -118,6 +121,13 @@ typedef struct sock_filter bpf_instr_raw;
118121
#define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
119122
#endif
120123

124+
/* SECCOMP_RET_ACTION_FULL was added in kernel v4.14. It may not be
125+
* defined on older kernels
126+
*/
127+
#ifndef SECCOMP_RET_ACTION_FULL
128+
#define SECCOMP_RET_ACTION_FULL 0xffff0000U
129+
#endif
130+
121131
int sys_chk_seccomp_syscall(void);
122132
void sys_set_seccomp_syscall(bool enable);
123133

tools/bpf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ struct sock_filter {
5656
typedef struct sock_filter bpf_instr_raw;
5757

5858
/* seccomp return masks */
59+
#define SECCOMP_RET_ACTION_FULL 0xffff0000U
5960
#define SECCOMP_RET_ACTION 0x7fff0000U
6061
#define SECCOMP_RET_DATA 0x0000ffffU
6162

6263
/* seccomp action values */
63-
#define SECCOMP_RET_KILL 0x00000000U
64+
#define SECCOMP_RET_KILL_PROCESS 0x80000000U
65+
#define SECCOMP_RET_KILL_THREAD 0x00000000U
66+
#define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
6467
#define SECCOMP_RET_TRAP 0x00030000U
6568
#define SECCOMP_RET_ERRNO 0x00050000U
6669
#define SECCOMP_RET_TRACE 0x7ff00000U

0 commit comments

Comments
 (0)