Re: security problem with seccomp-filter

From: Kees Cook
Date: Mon Apr 13 2015 - 13:30:43 EST


On Sun, Apr 12, 2015 at 2:33 PM, Felix von Leitner
<felix-linuxkernel@xxxxxxx> wrote:
>> What you're describing should work correctly (it's part of the
>> regression test suite we use). So, given that, I'd love to get to the
>> bottom of what you're seeing. Do you have a URL to your code? What
>> architecture are you running on?
>
> Well, I must be doing something wrong then.
> I extracted a test case from my program.
> I put it on http://ptrace.fefe.de/seccompfail.c
>
> It installs three seccomp filters, the last one containing this:
>
> DISALLOW_SYSCALL(prctl),
>
> with
>
> #define DISALLOW_SYSCALL(name) \
> BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, __NR_##name, 0, 1), \
> BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
>
> It is my understanding that that should then kill the process if the
> prctl syscall is called again.
>
> I test this by attempting to install the very same seccomp filter again,
> which calls prctl, but the process is not killed.
>
> What am I doing wrong?

Your filters don't load a syscall number, so the comparisons aren't
validating anything.

For example, from the working filter:

struct sock_filter filter[] = {
...
/* load the syscall number */
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
...
ALLOW_SYSCALL(close),
...
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_KILL)
};

Where as yours from seccomp_denyfile and seccomp_denysocket don't load
the syscall number:

struct sock_filter filter[] = {
DISALLOW_SYSCALL(open),
...
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
};

When you add the load as the first statement in both filters:

struct sock_filter filter[] = {
BPF_STMT(BPF_LD+BPF_W+BPF_ABS, offsetof(struct seccomp_data, nr)),
DISALLOW_SYSCALL(open),
...
BPF_STMT(BPF_RET+BPF_K, SECCOMP_RET_ALLOW),
};

things work as expected:

$ ./seccompfail
Bad system call

I wonder if the validator could be improved to disallow comparisons
when the accumulator hasn't been written to. That might have helped
you find this mistake more quickly (i.e. getting EINVAL from your
filter install attempts).

-Kees

--
Kees Cook
Chrome OS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/