aboutsummaryrefslogtreecommitdiffstats
path: root/src/fbuilder/build_profile.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fbuilder/build_profile.c')
-rw-r--r--src/fbuilder/build_profile.c165
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..3f5fe48ca
--- /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
28static char *cmdlist[] = {
29 "/usr/bin/firejail",
30 "--quiet",
31 "--output=" TRACE_OUTPUT,
32 "--noprofile",
33 "--caps.drop=all",
34 "--nonewprivs",
35 "--trace",
36 "--shell=none",
37 "/usr/bin/strace", // also used as a marker in build_profile()
38 "-c",
39 "-f",
40 "-o" STRACE_OUTPUT,
41};
42
43static void clear_tmp_files(void) {
44 unlink(STRACE_OUTPUT);
45 unlink(TRACE_OUTPUT);
46
47 // run all the rest
48 int i;
49 for (i = 1; i <= 5; i++) {
50 char *newname;
51 if (asprintf(&newname, "%s.%d", TRACE_OUTPUT, i) == -1)
52 errExit("asprintf");
53 unlink(newname);
54 free(newname);
55 }
56
57}
58
59void build_profile(int argc, char **argv, int index) {
60 // next index is the application name
61 if (index >= argc) {
62 fprintf(stderr, "Error: application name missing\n");
63 exit(1);
64 }
65
66 // clean /tmp files
67 clear_tmp_files();
68
69 // detect strace
70 int have_strace = 0;
71 if (access("/usr/bin/strace", X_OK) == 0)
72 have_strace = 1;
73
74 // calculate command length
75 int len = (int) sizeof(cmdlist) / sizeof(char*) + argc - index + 1;
76 if (arg_debug)
77 printf("command len %d + %d + 1\n", (int) (sizeof(cmdlist) / sizeof(char*)), argc - index);
78 char *cmd[len];
79
80 // build command
81 int i = 0;
82 for (i = 0; i < (int) sizeof(cmdlist) / sizeof(char*); i++) {
83 // skip strace if not installed
84 if (have_strace == 0 && strcmp(cmdlist[i], "/usr/bin/strace") == 0)
85 break;
86 cmd[i] = cmdlist[i];
87 }
88
89 int i2 = index;
90 for (; i < (len - 1); i++, i2++)
91 cmd[i] = argv[i2];
92 cmd[i] = NULL;
93
94 if (arg_debug) {
95 for (i = 0; i < len; i++)
96 printf("\t%s\n", cmd[i]);
97 }
98
99 // fork and execute
100 pid_t child = fork();
101 if (child == -1)
102 errExit("fork");
103 if (child == 0) {
104 int rv = execvp(cmd[0], cmd);
105 errExit("execv");
106 }
107
108 // wait for all processes to finish
109 int status;
110 if (waitpid(child, &status, 0) != child)
111 errExit("waitpid");
112
113 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
114 printf("\n\n\n");
115 printf("############################################\n");
116 printf("# %s profile\n", argv[index]);
117 printf("############################################\n");
118 printf("# Persistent global definitions\n");
119 printf("# include /etc/firejail/globals.local\n");
120 printf("\n");
121
122 printf("### basic blacklisting\n");
123 printf("include /etc/firejail/disable-common.inc\n");
124 printf("# include /etc/firejail/disable-devel.inc\n");
125 printf("include /etc/firejail/disable-passwdmgr.inc\n");
126 printf("# include /etc/firejail/disable-programs.inc\n");
127 printf("\n");
128
129 printf("### home directory whitelisting\n");
130 build_home(TRACE_OUTPUT);
131 printf("\n");
132
133 printf("### filesystem\n");
134 build_tmp(TRACE_OUTPUT);
135 build_dev(TRACE_OUTPUT);
136 build_etc(TRACE_OUTPUT);
137 build_var(TRACE_OUTPUT);
138 build_bin(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}