aboutsummaryrefslogtreecommitdiffstats
path: root/src/jailcheck/virtual.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/jailcheck/virtual.c')
-rw-r--r--src/jailcheck/virtual.c125
1 files changed, 125 insertions, 0 deletions
diff --git a/src/jailcheck/virtual.c b/src/jailcheck/virtual.c
new file mode 100644
index 000000000..09092f9ce
--- /dev/null
+++ b/src/jailcheck/virtual.c
@@ -0,0 +1,125 @@
1/*
2 * Copyright (C) 2014-2021 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 "jailcheck.h"
21#include <dirent.h>
22#include <sys/wait.h>
23
24
25#define MAX_TEST_FILES 16
26static char *dirs[MAX_TEST_FILES];
27static char *files[MAX_TEST_FILES];
28static int files_cnt = 0;
29
30void virtual_setup(const char *directory) {
31 // I am root!
32 assert(directory);
33 assert(*directory == '/');
34 assert(files_cnt < MAX_TEST_FILES);
35
36 // try to open the dir as root
37 DIR *dir = opendir(directory);
38 if (!dir) {
39 fprintf(stderr, "Warning: directory %s not found, skipping\n", directory);
40 return;
41 }
42 closedir(dir);
43
44 // create a test file
45 char *test_file;
46 if (asprintf(&test_file, "%s/jailcheck-private-%d", directory, getpid()) == -1)
47 errExit("asprintf");
48
49 FILE *fp = fopen(test_file, "w");
50 if (!fp) {
51 printf("Warning: I cannot create test file in directory %s, skipping...\n", directory);
52 return;
53 }
54 fprintf(fp, "this file was created by firetest utility, you can safely delete it\n");
55 fclose(fp);
56 if (strcmp(directory, user_home_dir) == 0) {
57 int rv = chown(test_file, user_uid, user_gid);
58 if (rv)
59 errExit("chown");
60 }
61
62 char *dname = strdup(directory);
63 if (!dname)
64 errExit("strdup");
65 dirs[files_cnt] = dname;
66 files[files_cnt] = test_file;
67 files_cnt++;
68}
69
70void virtual_destroy(void) {
71 // remove test files
72 int i;
73
74 for (i = 0; i < files_cnt; i++) {
75 int rv = unlink(files[i]);
76 (void) rv;
77 }
78 files_cnt = 0;
79}
80
81void virtual_test(void) {
82 // I am root in sandbox mount namespace
83 assert(user_uid);
84 int i;
85
86 int cnt = 0;
87 cnt += printf(" Virtual dirs: "); fflush(0);
88
89 for (i = 0; i < files_cnt; i++) {
90 assert(files[i]);
91
92 // I am root!
93 pid_t child = fork();
94 if (child == -1)
95 errExit("fork");
96
97 if (child == 0) { // child
98 // drop privileges
99 if (setgid(user_gid) != 0)
100 errExit("setgid");
101 if (setuid(user_uid) != 0)
102 errExit("setuid");
103
104 // try to open the file for reading
105 FILE *fp = fopen(files[i], "r");
106 if (fp)
107 fclose(fp);
108 else {
109 if (cnt == 0)
110 cnt += printf("\n ");
111 cnt += printf("%s, ", dirs[i]);
112 if (cnt > 60)
113 cnt = 0;
114 }
115 fflush(0);
116 exit(cnt);
117 }
118
119 // wait for the child to finish
120 int status;
121 wait(&status);
122 cnt = WEXITSTATUS(status);
123 }
124 printf("\n");
125}