aboutsummaryrefslogtreecommitdiffstats
path: root/src/faudit/pid.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/faudit/pid.c')
-rw-r--r--src/faudit/pid.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/faudit/pid.c b/src/faudit/pid.c
new file mode 100644
index 000000000..a0fb1d921
--- /dev/null
+++ b/src/faudit/pid.c
@@ -0,0 +1,101 @@
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 "faudit.h"
21
22void pid_test(void) {
23 char *kern_proc[] = {
24 "kthreadd",
25 "ksoftirqd",
26 "kworker",
27 "rcu_sched",
28 "rcu_bh",
29 NULL // NULL terminated list
30 };
31 int i;
32
33 // look at the first 10 processes
34 int not_visible = 1;
35 for (i = 1; i <= 10; i++) {
36 struct stat s;
37 char *fname;
38 if (asprintf(&fname, "/proc/%d/comm", i) == -1)
39 errExit("asprintf");
40 if (stat(fname, &s) == -1) {
41 free(fname);
42 continue;
43 }
44
45 // open file
46 /* coverity[toctou] */
47 FILE *fp = fopen(fname, "r");
48 if (!fp) {
49// fprintf(stderr, "Warning: cannot open %s\n", fname);
50 free(fname);
51 continue;
52 }
53
54 // read file
55 char buf[100];
56 if (fgets(buf, 10, fp) == NULL) {
57// fprintf(stderr, "Warning: cannot read %s\n", fname);
58 fclose(fp);
59 free(fname);
60 continue;
61 }
62 not_visible = 0;
63
64 // clean /n
65 char *ptr;
66 if ((ptr = strchr(buf, '\n')) != NULL)
67 *ptr = '\0';
68
69 // check process name against the kernel list
70 int j = 0;
71 while (kern_proc[j] != NULL) {
72 if (strncmp(buf, kern_proc[j], strlen(kern_proc[j])) == 0) {
73 fclose(fp);
74 free(fname);
75 printf("BAD: Process %d is not running in a PID namespace. ", getpid());
76 printf("Are you sure you're running in a sandbox?\n");
77 return;
78 }
79 j++;
80 }
81
82 fclose(fp);
83 free(fname);
84 }
85
86 pid_t pid = getpid();
87 if (not_visible && pid > 100)
88 printf("BAD: Process %d is not running in a PID namespace.\n", pid);
89 else
90 printf("GOOD: process %d is running in a PID namespace.\n", pid);
91
92 // try to guess the type of container/sandbox
93 char *str = getenv("container");
94 if (str)
95 printf("INFO: container/sandbox %s.\n", str);
96 else {
97 str = getenv("SNAP");
98 if (str)
99 printf("INFO: this is a snap package\n");
100 }
101}