diff options
author | 2015-08-08 19:12:30 -0400 | |
---|---|---|
committer | 2015-08-08 19:12:30 -0400 | |
commit | 1379851360349d6617ad32944a25ee5e2bb74fc2 (patch) | |
tree | f69b48e90708bfa3c2723d5a27ed3e024c827b43 /src/firejail/shutdown.c | |
parent | delete files (diff) | |
download | firejail-1379851360349d6617ad32944a25ee5e2bb74fc2.tar.gz firejail-1379851360349d6617ad32944a25ee5e2bb74fc2.tar.zst firejail-1379851360349d6617ad32944a25ee5e2bb74fc2.zip |
Baseline firejail 0.9.28
Diffstat (limited to 'src/firejail/shutdown.c')
-rw-r--r-- | src/firejail/shutdown.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/firejail/shutdown.c b/src/firejail/shutdown.c new file mode 100644 index 000000000..b666996df --- /dev/null +++ b/src/firejail/shutdown.c | |||
@@ -0,0 +1,98 @@ | |||
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/stat.h> | ||
22 | #include <sys/wait.h> | ||
23 | #include <fcntl.h> | ||
24 | #include <sys/prctl.h> | ||
25 | |||
26 | void shut_name(const char *name) { | ||
27 | if (!name || strlen(name) == 0) { | ||
28 | fprintf(stderr, "Error: invalid sandbox name\n"); | ||
29 | exit(1); | ||
30 | } | ||
31 | |||
32 | pid_t pid; | ||
33 | if (name2pid(name, &pid)) { | ||
34 | fprintf(stderr, "Error: cannot find sandbox %s\n", name); | ||
35 | exit(1); | ||
36 | } | ||
37 | |||
38 | shut(pid); | ||
39 | } | ||
40 | |||
41 | void shut(pid_t pid) { | ||
42 | pid_t parent = pid; | ||
43 | // if the pid is that of a firejail process, use the pid of a child process inside the sandbox | ||
44 | char *comm = pid_proc_comm(pid); | ||
45 | if (comm) { | ||
46 | // remove \n | ||
47 | char *ptr = strchr(comm, '\n'); | ||
48 | if (ptr) | ||
49 | *ptr = '\0'; | ||
50 | if (strcmp(comm, "firejail") == 0) { | ||
51 | pid_t child; | ||
52 | if (find_child(pid, &child) == 0) { | ||
53 | pid = child; | ||
54 | printf("Switching to pid %u, the first child process inside the sandbox\n", (unsigned) pid); | ||
55 | } | ||
56 | } | ||
57 | free(comm); | ||
58 | } | ||
59 | |||
60 | // check privileges for non-root users | ||
61 | uid_t uid = getuid(); | ||
62 | if (uid != 0) { | ||
63 | struct stat s; | ||
64 | char *dir; | ||
65 | if (asprintf(&dir, "/proc/%u/ns", pid) == -1) | ||
66 | errExit("asprintf"); | ||
67 | if (stat(dir, &s) < 0) | ||
68 | errExit("stat"); | ||
69 | if (s.st_uid != uid) { | ||
70 | fprintf(stderr, "Error: permission is denied to shutdown a sandbox created by a different user.\n"); | ||
71 | exit(1); | ||
72 | } | ||
73 | } | ||
74 | |||
75 | printf("Sending SIGTERM to %u\n", pid); | ||
76 | kill(pid, SIGTERM); | ||
77 | sleep(2); | ||
78 | |||
79 | // if the process is still running, terminate it using SIGKILL | ||
80 | // try to open stat file | ||
81 | char *file; | ||
82 | if (asprintf(&file, "/proc/%u/status", pid) == -1) { | ||
83 | perror("asprintf"); | ||
84 | exit(1); | ||
85 | } | ||
86 | FILE *fp = fopen(file, "r"); | ||
87 | if (!fp) | ||
88 | return; | ||
89 | fclose(fp); | ||
90 | |||
91 | // kill the process and also the parent | ||
92 | printf("Sending SIGKILL to %u\n", pid); | ||
93 | kill(pid, SIGKILL); | ||
94 | if (parent != pid) { | ||
95 | printf("Sending SIGKILL to %u\n", parent); | ||
96 | kill(parent, SIGKILL); | ||
97 | } | ||
98 | } | ||