aboutsummaryrefslogtreecommitdiffstats
path: root/src/fsec-optimize/main.c
diff options
context:
space:
mode:
authorLibravatar netblue30 <netblue30@yahoo.com>2018-01-02 09:08:12 -0500
committerLibravatar netblue30 <netblue30@yahoo.com>2018-01-02 09:08:12 -0500
commite2ff818e3c4414ca19cc8533b1bc3b7afd755758 (patch)
tree6c07abe14e03926d7508b00c4e6ea882e0889d81 /src/fsec-optimize/main.c
parentMerge pull request #1710 from bitfreak25/master (diff)
downloadfirejail-e2ff818e3c4414ca19cc8533b1bc3b7afd755758.tar.gz
firejail-e2ff818e3c4414ca19cc8533b1bc3b7afd755758.tar.zst
firejail-e2ff818e3c4414ca19cc8533b1bc3b7afd755758.zip
optimize default seccomp filters
Diffstat (limited to 'src/fsec-optimize/main.c')
-rw-r--r--src/fsec-optimize/main.c94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/fsec-optimize/main.c b/src/fsec-optimize/main.c
new file mode 100644
index 000000000..2c11b91ef
--- /dev/null
+++ b/src/fsec-optimize/main.c
@@ -0,0 +1,94 @@
1/*
2 * Copyright (C) 2014-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 "fsec_optimize.h"
21
22static void usage(void) {
23 printf("Usage:\n");
24 printf("\tfsec-optimize file - optimize seccomp filter\n");
25}
26
27int main(int argc, char **argv) {
28#if 0
29{
30//system("cat /proc/self/status");
31int i;
32for (i = 0; i < argc; i++)
33 printf("*%s* ", argv[i]);
34printf("\n");
35}
36#endif
37 if (argc != 2) {
38 usage();
39 return 1;
40 }
41
42 if (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0) {
43 usage();
44 return 0;
45 }
46
47 char *fname = argv[1];
48
49 // open input file
50 int fd = open(fname, O_RDONLY);
51 if (fd == -1)
52 goto errexit;
53
54 // calculate the number of entries
55 int size = lseek(fd, 0, SEEK_END);
56 if (size == -1) // todo: check maximum size of seccomp filter (4KB?)
57 goto errexit;
58 unsigned short entries = (unsigned short) size / (unsigned short) sizeof(struct sock_filter);
59
60 // read filter
61 struct sock_filter *filter = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
62 if (filter == MAP_FAILED)
63 goto errexit;
64 close(fd);
65
66 // duplicate the filter memory and unmap the file
67 struct sock_filter *outfilter = duplicate(filter, entries);
68 if (munmap(filter, size) == -1)
69 perror("Error un-mmapping the file");
70
71 // optimize filter
72 entries = optimize(outfilter, entries);
73
74 // write the new file and free memory
75 fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0755);
76 if (fd == -1) {
77 fprintf(stderr, "Error: cannot open output file\n");
78 return 1;
79 }
80 size = write(fd, outfilter, entries * sizeof(struct sock_filter));
81 if (size != entries * sizeof(struct sock_filter)) {
82 fprintf(stderr, "Error: cannot write output file\n");
83 return 1;
84 }
85 close(fd);
86 free(outfilter);
87
88 return 0;
89errexit:
90 close(fd);
91 fprintf(stderr, "Error: cannot read %s\n", fname);
92 exit(1);
93
94}