diff options
Diffstat (limited to 'src/firejail/fs_trace.c')
-rw-r--r-- | src/firejail/fs_trace.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/firejail/fs_trace.c b/src/firejail/fs_trace.c new file mode 100644 index 000000000..1c7ef5cbe --- /dev/null +++ b/src/firejail/fs_trace.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014, 2015 netblue30 (netblue30@yahoo.com) | ||
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 | #include <linux/limits.h> | ||
24 | #include <glob.h> | ||
25 | #include <dirent.h> | ||
26 | #include <fcntl.h> | ||
27 | #include <pwd.h> | ||
28 | |||
29 | void fs_trace_preload(void) { | ||
30 | struct stat s; | ||
31 | |||
32 | // create an empty /etc/ld.so.preload | ||
33 | if (stat("/etc/ld.so.preload", &s)) { | ||
34 | if (arg_debug) | ||
35 | printf("Creating an empty /etc/ld.so.preload file\n"); | ||
36 | /* coverity[toctou] */ | ||
37 | FILE *fp = fopen("/etc/ld.so.preload", "w"); | ||
38 | if (!fp) | ||
39 | errExit("fopen"); | ||
40 | fclose(fp); | ||
41 | if (chown("/etc/ld.so.preload", 0, 0) < 0) | ||
42 | errExit("chown"); | ||
43 | if (chmod("/etc/ld.so.preload", S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) | ||
44 | errExit("chmod"); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | void fs_trace(void) { | ||
49 | // create /tmp/firejail/mnt directory | ||
50 | fs_build_mnt_dir(); | ||
51 | |||
52 | // create the new ld.so.preload file and mount-bind it | ||
53 | if (arg_debug) | ||
54 | printf("Create the new ld.so.preload file\n"); | ||
55 | char *preload; | ||
56 | if (asprintf(&preload, "%s/ld.so.preload", MNT_DIR) == -1) | ||
57 | errExit("asprintf"); | ||
58 | FILE *fp = fopen(preload, "w"); | ||
59 | if (!fp) | ||
60 | errExit("fopen"); | ||
61 | fprintf(fp, "%s/lib/firejail/libtrace.so\n", PREFIX); | ||
62 | fclose(fp); | ||
63 | if (chown(preload, 0, 0) < 0) | ||
64 | errExit("chown"); | ||
65 | if (chmod(preload, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH ) < 0) | ||
66 | errExit("chmod"); | ||
67 | |||
68 | // mount the new preload file | ||
69 | if (arg_debug) | ||
70 | printf("Mount the new ld.so.preload file\n"); | ||
71 | if (mount(preload, "/etc/ld.so.preload", NULL, MS_BIND|MS_REC, NULL) < 0) | ||
72 | errExit("mount bind ls.so.preload"); | ||
73 | } | ||
74 | |||
75 | |||
76 | \ No newline at end of file | ||