diff options
author | netblue30 <netblue30@yahoo.com> | 2017-09-16 08:49:05 -0400 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2017-09-16 08:49:05 -0400 |
commit | 280f37eba89ebc211d0c02848d3d47d086458b25 (patch) | |
tree | 1398c5dfc53c4d286d7b6b528d5a3c1585a67325 /src/fbuilder/build_profile.c | |
parent | Merge pull request #1552 from SpotComms/mf (diff) | |
download | firejail-280f37eba89ebc211d0c02848d3d47d086458b25.tar.gz firejail-280f37eba89ebc211d0c02848d3d47d086458b25.tar.zst firejail-280f37eba89ebc211d0c02848d3d47d086458b25.zip |
--build
Diffstat (limited to 'src/fbuilder/build_profile.c')
-rw-r--r-- | src/fbuilder/build_profile.c | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/src/fbuilder/build_profile.c b/src/fbuilder/build_profile.c new file mode 100644 index 000000000..5fca22648 --- /dev/null +++ b/src/fbuilder/build_profile.c | |||
@@ -0,0 +1,165 @@ | |||
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 | |||
21 | #include "fbuilder.h" | ||
22 | #include <sys/wait.h> | ||
23 | #include <fcntl.h> | ||
24 | |||
25 | #define TRACE_OUTPUT "/tmp/firejail-trace" | ||
26 | #define STRACE_OUTPUT "/tmp/firejail-strace" | ||
27 | |||
28 | static char *cmdlist[] = { | ||
29 | "/usr/bin/firejail", | ||
30 | "--quiet", | ||
31 | "--output=" TRACE_OUTPUT, | ||
32 | "--noprofile", | ||
33 | "--caps.drop=all", | ||
34 | "--nonewprivs", | ||
35 | "--trace", | ||
36 | "/usr/bin/strace", // also used as a marker in build_profile() | ||
37 | "-c", | ||
38 | "-f", | ||
39 | "-o" STRACE_OUTPUT, | ||
40 | }; | ||
41 | |||
42 | static void clear_tmp_files(void) { | ||
43 | unlink(STRACE_OUTPUT); | ||
44 | unlink(TRACE_OUTPUT); | ||
45 | |||
46 | // run all the rest | ||
47 | int i; | ||
48 | for (i = 1; i <= 5; i++) { | ||
49 | char *newname; | ||
50 | if (asprintf(&newname, "%s.%d", TRACE_OUTPUT, i) == -1) | ||
51 | errExit("asprintf"); | ||
52 | unlink(newname); | ||
53 | free(newname); | ||
54 | } | ||
55 | |||
56 | } | ||
57 | |||
58 | void build_profile(int argc, char **argv, int index) { | ||
59 | unlink("/tmp/strace-output"); | ||
60 | |||
61 | // next index is the application name | ||
62 | if (index >= argc) { | ||
63 | fprintf(stderr, "Error: application name missing\n"); | ||
64 | exit(1); | ||
65 | } | ||
66 | |||
67 | // clean /tmp files | ||
68 | clear_tmp_files(); | ||
69 | |||
70 | // detect strace | ||
71 | int have_strace = 0; | ||
72 | if (access("/usr/bin/strace", X_OK) == 0) | ||
73 | have_strace = 1; | ||
74 | |||
75 | // calculate command length | ||
76 | int len = (int) sizeof(cmdlist) / sizeof(char*) + argc - index + 1; | ||
77 | if (arg_debug) | ||
78 | printf("command len %d + %d + 1\n", (int) (sizeof(cmdlist) / sizeof(char*)), argc - index); | ||
79 | char *cmd[len]; | ||
80 | |||
81 | // build command | ||
82 | int i = 0; | ||
83 | for (i = 0; i < (int) sizeof(cmdlist) / sizeof(char*); i++) { | ||
84 | // skip strace if not installed | ||
85 | if (have_strace == 0 && strcmp(cmdlist[i], "/usr/bin/strace") == 0) | ||
86 | break; | ||
87 | cmd[i] = cmdlist[i]; | ||
88 | } | ||
89 | |||
90 | int i2 = index; | ||
91 | for (; i < (len - 1); i++, i2++) | ||
92 | cmd[i] = argv[i2]; | ||
93 | cmd[i] = NULL; | ||
94 | |||
95 | if (arg_debug) { | ||
96 | for (i = 0; i < len; i++) | ||
97 | printf("\t%s\n", cmd[i]); | ||
98 | } | ||
99 | |||
100 | // fork and execute | ||
101 | pid_t child = fork(); | ||
102 | if (child == -1) | ||
103 | errExit("fork"); | ||
104 | if (child == 0) { | ||
105 | int rv = execvp(cmd[0], cmd); | ||
106 | errExit("execv"); | ||
107 | } | ||
108 | |||
109 | // wait for all processes to finish | ||
110 | int status; | ||
111 | if (waitpid(child, &status, 0) != child) | ||
112 | errExit("waitpid"); | ||
113 | |||
114 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | ||
115 | printf("\n\n\n"); | ||
116 | printf("############################################\n"); | ||
117 | printf("# %s profile\n", argv[index]); | ||
118 | printf("############################################\n"); | ||
119 | printf("# Persistent global definitions\n"); | ||
120 | printf("# include /etc/firejail/globals.local\n"); | ||
121 | printf("\n"); | ||
122 | |||
123 | printf("### basic blacklisting\n"); | ||
124 | printf("include /etc/firejail/disable-common.inc\n"); | ||
125 | printf("# include /etc/firejail/disable-devel.inc\n"); | ||
126 | printf("include /etc/firejail/disable-passwdmgr.inc\n"); | ||
127 | printf("# include /etc/firejail/disable-programs.inc\n"); | ||
128 | printf("\n"); | ||
129 | |||
130 | printf("### home directory whitelisting\n"); | ||
131 | build_home(TRACE_OUTPUT); | ||
132 | printf("\n"); | ||
133 | |||
134 | printf("### filesystem\n"); | ||
135 | build_tmp(TRACE_OUTPUT); | ||
136 | build_dev(TRACE_OUTPUT); | ||
137 | build_etc(TRACE_OUTPUT); | ||
138 | build_var(TRACE_OUTPUT); | ||
139 | printf("\n"); | ||
140 | |||
141 | printf("### security filters\n"); | ||
142 | printf("caps.drop all\n"); | ||
143 | printf("nonewprivs\n"); | ||
144 | printf("seccomp\n"); | ||
145 | if (have_strace) | ||
146 | build_seccomp(STRACE_OUTPUT); | ||
147 | else { | ||
148 | printf("# If you install strace on your system, Firejail will also create a\n"); | ||
149 | printf("# whitelisted seccomp filter.\n"); | ||
150 | } | ||
151 | printf("\n"); | ||
152 | |||
153 | printf("### network\n"); | ||
154 | build_protocol(TRACE_OUTPUT); | ||
155 | printf("\n"); | ||
156 | |||
157 | printf("### environment\n"); | ||
158 | printf("shell none\n"); | ||
159 | |||
160 | } | ||
161 | else { | ||
162 | fprintf(stderr, "Error: cannot run the sandbox\n"); | ||
163 | exit(1); | ||
164 | } | ||
165 | } | ||