diff options
Diffstat (limited to 'src/libpostexecseccomp/libpostexecseccomp.c')
-rw-r--r-- | src/libpostexecseccomp/libpostexecseccomp.c | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/libpostexecseccomp/libpostexecseccomp.c b/src/libpostexecseccomp/libpostexecseccomp.c new file mode 100644 index 000000000..801f968a6 --- /dev/null +++ b/src/libpostexecseccomp/libpostexecseccomp.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | #include "libpostexecseccomp.h" | ||
21 | #include <fcntl.h> | ||
22 | #include <linux/audit.h> | ||
23 | #include <linux/bpf.h> | ||
24 | #include <linux/filter.h> | ||
25 | #include <linux/seccomp.h> | ||
26 | #include <sys/mman.h> | ||
27 | #include <sys/prctl.h> | ||
28 | #include <sys/ptrace.h> | ||
29 | #include <sys/stat.h> | ||
30 | #include <sys/types.h> | ||
31 | #include <unistd.h> | ||
32 | |||
33 | __attribute__((constructor)) | ||
34 | static void load_seccomp(void) { | ||
35 | int fd = open(RUN_SECCOMP_POSTEXEC, O_RDONLY); | ||
36 | if (fd == -1) | ||
37 | return; | ||
38 | |||
39 | int size = lseek(fd, 0, SEEK_END); | ||
40 | unsigned short entries = (unsigned short) size / (unsigned short) sizeof(struct sock_filter); | ||
41 | struct sock_filter *filter = MAP_FAILED; | ||
42 | if (size != 0) | ||
43 | filter = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); | ||
44 | |||
45 | close(fd); | ||
46 | |||
47 | if (size == 0 || filter == MAP_FAILED) | ||
48 | return; | ||
49 | |||
50 | // install filter | ||
51 | struct sock_fprog prog = { | ||
52 | .len = entries, | ||
53 | .filter = filter, | ||
54 | }; | ||
55 | |||
56 | prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); | ||
57 | prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog); | ||
58 | munmap(filter, size); | ||
59 | } | ||