diff options
Diffstat (limited to 'src/libpostexecseccomp')
-rw-r--r-- | src/libpostexecseccomp/Makefile.in | 26 | ||||
-rw-r--r-- | src/libpostexecseccomp/libpostexecseccomp.c | 59 | ||||
-rw-r--r-- | src/libpostexecseccomp/libpostexecseccomp.h | 25 |
3 files changed, 110 insertions, 0 deletions
diff --git a/src/libpostexecseccomp/Makefile.in b/src/libpostexecseccomp/Makefile.in new file mode 100644 index 000000000..92803342c --- /dev/null +++ b/src/libpostexecseccomp/Makefile.in | |||
@@ -0,0 +1,26 @@ | |||
1 | CC=@CC@ | ||
2 | PREFIX=@prefix@ | ||
3 | VERSION=@PACKAGE_VERSION@ | ||
4 | NAME=@PACKAGE_NAME@ | ||
5 | HAVE_FATAL_WARNINGS=@HAVE_FATAL_WARNINGS@ | ||
6 | |||
7 | H_FILE_LIST = $(sort $(wildcard *.[h])) | ||
8 | C_FILE_LIST = $(sort $(wildcard *.c)) | ||
9 | OBJS = $(C_FILE_LIST:.c=.o) | ||
10 | BINOBJS = $(foreach file, $(OBJS), $file) | ||
11 | CFLAGS += -ggdb $(HAVE_FATAL_WARNINGS) -O2 -DVERSION='"$(VERSION)"' -fstack-protector-all -D_FORTIFY_SOURCE=2 -fPIC -Wformat -Wformat-security | ||
12 | LDFLAGS += -pie -Wl,-z,relro -Wl,-z,now | ||
13 | |||
14 | all: libpostexecseccomp.so | ||
15 | |||
16 | %.o : %.c $(H_FILE_LIST) | ||
17 | $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@ | ||
18 | |||
19 | libpostexecseccomp.so: $(OBJS) | ||
20 | $(CC) $(LDFLAGS) -shared -fPIC -z relro -o $@ $(OBJS) -ldl | ||
21 | |||
22 | |||
23 | clean:; rm -f $(OBJS) libpostexecseccomp.so | ||
24 | |||
25 | distclean: clean | ||
26 | rm -fr Makefile | ||
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 | } | ||
diff --git a/src/libpostexecseccomp/libpostexecseccomp.h b/src/libpostexecseccomp/libpostexecseccomp.h new file mode 100644 index 000000000..c4aca540a --- /dev/null +++ b/src/libpostexecseccomp/libpostexecseccomp.h | |||
@@ -0,0 +1,25 @@ | |||
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 | #ifndef LIBPOSTEXECSECCOMP_H | ||
21 | #define LIBPOSTEXECSECCOMP_H | ||
22 | |||
23 | #define RUN_SECCOMP_POSTEXEC "/run/firejail/mnt/seccomp.postexec" | ||
24 | |||
25 | #endif | ||