diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/firejail/firejail.h | 3 | ||||
-rw-r--r-- | src/firejail/main.c | 4 | ||||
-rw-r--r-- | src/firejail/run_symlink.c | 91 |
3 files changed, 98 insertions, 0 deletions
diff --git a/src/firejail/firejail.h b/src/firejail/firejail.h index 1de38c43a..ba8a9dc48 100644 --- a/src/firejail/firejail.h +++ b/src/firejail/firejail.h | |||
@@ -500,5 +500,8 @@ void fs_logger_change_owner(void); | |||
500 | void fs_logger_print_log_name(const char *name); | 500 | void fs_logger_print_log_name(const char *name); |
501 | void fs_logger_print_log(pid_t pid); | 501 | void fs_logger_print_log(pid_t pid); |
502 | 502 | ||
503 | // run_symlink.c | ||
504 | void run_symlink(int argc, char **argv); | ||
505 | |||
503 | #endif | 506 | #endif |
504 | 507 | ||
diff --git a/src/firejail/main.c b/src/firejail/main.c index 58d735010..43d2f0fa2 100644 --- a/src/firejail/main.c +++ b/src/firejail/main.c | |||
@@ -459,6 +459,10 @@ int main(int argc, char **argv) { | |||
459 | int highest_errno = errno_highest_nr(); | 459 | int highest_errno = errno_highest_nr(); |
460 | #endif | 460 | #endif |
461 | 461 | ||
462 | // check argv[0] symlink wrapper | ||
463 | run_symlink(argc, argv); | ||
464 | |||
465 | |||
462 | // check if we already have a sandbox running | 466 | // check if we already have a sandbox running |
463 | int rv = check_kernel_procs(); | 467 | int rv = check_kernel_procs(); |
464 | if (rv == 0) { | 468 | if (rv == 0) { |
diff --git a/src/firejail/run_symlink.c b/src/firejail/run_symlink.c new file mode 100644 index 000000000..5f8d131ae --- /dev/null +++ b/src/firejail/run_symlink.c | |||
@@ -0,0 +1,91 @@ | |||
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/types.h> | ||
22 | #include <sys/stat.h> | ||
23 | #include <unistd.h> | ||
24 | |||
25 | void run_symlink(int argc, char **argv) { | ||
26 | char *program = strrchr(argv[0], '/'); | ||
27 | if (program) | ||
28 | program += 1; | ||
29 | else | ||
30 | program = argv[0]; | ||
31 | if (strcmp(program, "firejail") == 0) | ||
32 | return; | ||
33 | |||
34 | // find the real program | ||
35 | // probably the first entry returend by "which -a" is a symlink - use the second entry! | ||
36 | char *p = getenv("PATH"); | ||
37 | if (!p) { | ||
38 | fprintf(stderr, "Error: PATH environment variable not set\n"); | ||
39 | exit(1); | ||
40 | } | ||
41 | |||
42 | char *path = strdup(p); | ||
43 | if (!path) | ||
44 | errExit("strdup"); | ||
45 | |||
46 | // look in path for our program | ||
47 | char *tok = strtok(path, ":"); | ||
48 | int found = 0; | ||
49 | while (tok) { | ||
50 | char *name; | ||
51 | if (asprintf(&name, "%s/%s", tok, program) == -1) | ||
52 | errExit("asprintf"); | ||
53 | |||
54 | struct stat s; | ||
55 | if (stat(name, &s) == 0) { | ||
56 | if (!is_link(name)) { | ||
57 | program = strdup(name); | ||
58 | found = 1; | ||
59 | break; | ||
60 | } | ||
61 | } | ||
62 | |||
63 | free(name); | ||
64 | tok = strtok(NULL, ":"); | ||
65 | } | ||
66 | if (!found) { | ||
67 | fprintf(stderr, "Error: cannot find the program in the path\n"); | ||
68 | exit(1); | ||
69 | } | ||
70 | |||
71 | |||
72 | // start the argv[0] program in a new sandbox | ||
73 | char *firejail; | ||
74 | if (asprintf(&firejail, "%s/bin/firejail", PREFIX) == -1) | ||
75 | errExit("asprintf"); | ||
76 | |||
77 | printf("Redirecting symlink to %s\n", firejail, program); | ||
78 | |||
79 | // run command | ||
80 | char *a[3 + argc]; | ||
81 | a[0] = firejail; | ||
82 | a[1] = program; | ||
83 | int i; | ||
84 | for (i = 0; i < (argc - 1); i++) | ||
85 | a[i + 2] = argv[i + 1]; | ||
86 | a[i + 2] = NULL; | ||
87 | execvp(a[0], a); | ||
88 | |||
89 | perror("execvp"); | ||
90 | exit(1); | ||
91 | } | ||