Skip to content

Commit deb4de8

Browse files
committed
seccomp: Provide matching filter for introspection
Both the upcoming logging improvements and changes to RET_KILL will need to know which filter a given seccomp return value originated from. In order to delay logic processing of result until after the seccomp loop, this adds a single pointer assignment on matches. This will allow both log and RET_KILL logic to work off the filter rather than doing more expensive tests inside the time-critical run_filters loop. Running tight cycles of getpid() with filters attached shows no measurable difference in speed. Suggested-by: Tyler Hicks <tyhicks@canonical.com> Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Tyler Hicks <tyhicks@canonical.com>
1 parent f3f6e30 commit deb4de8

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

‎kernel/seccomp.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,14 @@ static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
171171
/**
172172
* seccomp_run_filters - evaluates all seccomp filters against @sd
173173
* @sd: optional seccomp data to be passed to filters
174+
* @match: stores struct seccomp_filter that resulted in the return value,
175+
* unless filter returned SECCOMP_RET_ALLOW, in which case it will
176+
* be unchanged.
174177
*
175178
* Returns valid seccomp BPF response codes.
176179
*/
177-
static u32 seccomp_run_filters(const struct seccomp_data *sd)
180+
static u32 seccomp_run_filters(const struct seccomp_data *sd,
181+
struct seccomp_filter **match)
178182
{
179183
struct seccomp_data sd_local;
180184
u32 ret = SECCOMP_RET_ALLOW;
@@ -198,8 +202,10 @@ static u32 seccomp_run_filters(const struct seccomp_data *sd)
198202
for (; f; f = f->prev) {
199203
u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
200204

201-
if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
205+
if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION)) {
202206
ret = cur_ret;
207+
*match = f;
208+
}
203209
}
204210
return ret;
205211
}
@@ -566,6 +572,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
566572
const bool recheck_after_trace)
567573
{
568574
u32 filter_ret, action;
575+
struct seccomp_filter *match = NULL;
569576
int data;
570577

571578
/*
@@ -574,7 +581,7 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
574581
*/
575582
rmb();
576583

577-
filter_ret = seccomp_run_filters(sd);
584+
filter_ret = seccomp_run_filters(sd, &match);
578585
data = filter_ret & SECCOMP_RET_DATA;
579586
action = filter_ret & SECCOMP_RET_ACTION;
580587

@@ -638,6 +645,11 @@ static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
638645
return 0;
639646

640647
case SECCOMP_RET_ALLOW:
648+
/*
649+
* Note that the "match" filter will always be NULL for
650+
* this action since SECCOMP_RET_ALLOW is the starting
651+
* state in seccomp_run_filters().
652+
*/
641653
return 0;
642654

643655
case SECCOMP_RET_KILL:

0 commit comments

Comments
 (0)