aboutsummaryrefslogtreecommitdiffstats
path: root/src/firejail/preproc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/firejail/preproc.c')
-rw-r--r--src/firejail/preproc.c110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/firejail/preproc.c b/src/firejail/preproc.c
new file mode 100644
index 000000000..6784ff5ac
--- /dev/null
+++ b/src/firejail/preproc.c
@@ -0,0 +1,110 @@
1/*
2 * Copyright (C) 2014-2016 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 "firejail.h"
21#include <sys/mount.h>
22#include <sys/stat.h>
23
24static int tmpfs_mounted = 0;
25
26// build /run/firejail directory
27void preproc_build_firejail_dir(void) {
28 struct stat s;
29
30 // CentOS 6 doesn't have /run directory
31 if (stat(RUN_FIREJAIL_BASEDIR, &s)) {
32 create_empty_dir_as_root(RUN_FIREJAIL_BASEDIR, 0755);
33 }
34
35 if (stat(RUN_FIREJAIL_DIR, &s)) {
36 create_empty_dir_as_root(RUN_FIREJAIL_DIR, 0755);
37 }
38
39 if (stat(RUN_FIREJAIL_NETWORK_DIR, &s)) {
40 create_empty_dir_as_root(RUN_FIREJAIL_NETWORK_DIR, 0755);
41 }
42
43 if (stat(RUN_FIREJAIL_BANDWIDTH_DIR, &s)) {
44 create_empty_dir_as_root(RUN_FIREJAIL_BANDWIDTH_DIR, 0755);
45 }
46
47 if (stat(RUN_FIREJAIL_NAME_DIR, &s)) {
48 create_empty_dir_as_root(RUN_FIREJAIL_NAME_DIR, 0755);
49 }
50
51 if (stat(RUN_FIREJAIL_X11_DIR, &s)) {
52 create_empty_dir_as_root(RUN_FIREJAIL_X11_DIR, 0755);
53 }
54
55 if (stat(RUN_FIREJAIL_APPIMAGE_DIR, &s)) {
56 create_empty_dir_as_root(RUN_FIREJAIL_APPIMAGE_DIR, 0755);
57 }
58
59 create_empty_file_as_root(RUN_RO_FILE, S_IRUSR);
60 create_empty_dir_as_root(RUN_RO_DIR, S_IRUSR);
61}
62
63// build /run/firejail/mnt directory
64void preproc_mount_mnt_dir(void) {
65 struct stat s;
66
67 // mount tmpfs on top of /run/firejail/mnt
68 if (!tmpfs_mounted) {
69 if (arg_debug)
70 printf("Mounting tmpfs on %s directory\n", RUN_MNT_DIR);
71 if (mount("tmpfs", RUN_MNT_DIR, "tmpfs", MS_NOSUID | MS_STRICTATIME | MS_REC, "mode=755,gid=0") < 0)
72 errExit("mounting /run/firejail/mnt");
73 tmpfs_mounted = 1;
74 fs_logger2("tmpfs", RUN_MNT_DIR);
75 }
76}
77
78// grab a copy of cp command
79void preproc_build_cp_command(void) {
80 struct stat s;
81 preproc_mount_mnt_dir();
82 if (stat(RUN_CP_COMMAND, &s)) {
83 char* fname = realpath("/bin/cp", NULL);
84 if (fname == NULL) {
85 fprintf(stderr, "Error: /bin/cp not found\n");
86 exit(1);
87 }
88 if (stat(fname, &s)) {
89 fprintf(stderr, "Error: /bin/cp not found\n");
90 exit(1);
91 }
92 if (is_link(fname)) {
93 fprintf(stderr, "Error: invalid /bin/cp file\n");
94 exit(1);
95 }
96 int rv = copy_file(fname, RUN_CP_COMMAND, 0, 0, 0755);
97 if (rv) {
98 fprintf(stderr, "Error: cannot access /bin/cp\n");
99 exit(1);
100 }
101 ASSERT_PERMS(RUN_CP_COMMAND, 0, 0, 0755);
102
103 free(fname);
104 }
105}
106
107// delete the temporary cp command
108void preproc_delete_cp_command(void) {
109 unlink(RUN_CP_COMMAND);
110}