1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Copyright (C) 2014-2016 Firejail Authors
*
* This file is part of firejail project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "firejail.h"
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// spoof /etc/machine_id
void fs_machineid(void) {
union machineid_t {
uint8_t u8[16];
uint32_t u32[4];
} mid;
// if --machine-id flag is active, do nothing
if (arg_machineid)
return;
// init random number generator
srand(time(NULL));
// generate random id
mid.u32[0] = rand();
mid.u32[1] = rand();
mid.u32[2] = rand();
mid.u32[3] = rand();
// UUID version 4 and DCE variant
mid.u8[6] = (mid.u8[6] & 0x0F) | 0x40;
mid.u8[8] = (mid.u8[8] & 0x3F) | 0x80;
// write it in a file
FILE *fp = fopen(RUN_MACHINEID, "w");
if (!fp)
errExit("fopen");
fprintf(fp, "%08x%08x%08x%08x\n", mid.u32[0], mid.u32[1], mid.u32[2], mid.u32[3]);
fclose(fp);
if (set_perms(RUN_MACHINEID, 0, 0, 0444))
errExit("set_perms");
struct stat s;
if (stat("/etc/machine-id", &s) == 0) {
if (arg_debug)
printf("installing a new /etc/machine-id\n");
if (mount(RUN_MACHINEID, "/etc/machine-id", "none", MS_BIND, "mode=444,gid=0"))
errExit("mount");
}
if (stat("/var/lib/dbus/machine-id", &s) == 0) {
if (mount(RUN_MACHINEID, "/var/lib/dbus/machine-id", "none", MS_BIND, "mode=444,gid=0"))
errExit("mount");
}
}
// return 0 if file not found, 1 if found
static int check_dir_or_file(const char *fname) {
assert(fname);
struct stat s;
if (stat(fname, &s) == -1) {
if (arg_debug)
printf("Warning: file %s not found.\n", fname);
return 0;
}
// read access
if (access(fname, R_OK) == -1)
goto errexit;
// dir or regular file
if (S_ISDIR(s.st_mode) || S_ISREG(s.st_mode) || !is_link(fname))
return 1; // normal exit
errexit:
fprintf(stderr, "Error: invalid file type, %s.\n", fname);
exit(1);
}
static void duplicate(const char *fname, const char *private_dir, const char *private_run_dir) {
if (*fname == '~' || *fname == '/' || strstr(fname, "..")) {
fprintf(stderr, "Error: \"%s\" is an invalid filename\n", fname);
exit(1);
}
invalid_filename(fname);
char *src;
if (asprintf(&src, "%s/%s", private_dir, fname) == -1)
errExit("asprintf");
if (check_dir_or_file(src) == 0) {
if (!arg_quiet)
fprintf(stderr, "Warning: skipping %s for private %s\n", fname, private_dir);
free(src);
return;
}
if (arg_debug)
printf("copying %s to private %s\n", src, private_dir);
struct stat s;
if (stat(src, &s) == 0 && S_ISDIR(s.st_mode)) {
// create the directory in RUN_ETC_DIR
char *dirname;
if (asprintf(&dirname, "%s/%s", private_run_dir, fname) == -1)
errExit("asprintf");
create_empty_dir_as_root(dirname, s.st_mode);
sbox_run(SBOX_ROOT| SBOX_SECCOMP, 3, PATH_FCOPY, src, dirname);
free(dirname);
}
else
sbox_run(SBOX_ROOT| SBOX_SECCOMP, 3, PATH_FCOPY, src, private_run_dir);
fs_logger2("clone", src);
free(src);
}
void fs_private_dir_list(const char *private_dir, const char *private_run_dir, const char *private_list) {
assert(private_dir);
assert(private_run_dir);
assert(private_list);
// create /run/firejail/mnt/etc directory
mkdir_attr(private_run_dir, 0755, 0, 0);
fs_logger2("tmpfs", private_dir);
fs_logger_print(); // save the current log
// copy the list of files in the new etc directory
// using a new child process with root privileges
if (*private_list != '\0') {
if (arg_debug)
printf("Copying files in the new %s directory:\n", private_dir);
// copy the list of files in the new home directory
char *dlist = strdup(private_list);
if (!dlist)
errExit("strdup");
char *ptr = strtok(dlist, ",");
duplicate(ptr, private_dir, private_run_dir);
while ((ptr = strtok(NULL, ",")) != NULL)
duplicate(ptr, private_dir, private_run_dir);
free(dlist);
fs_logger_print();
}
if (arg_debug)
printf("Mount-bind %s on top of %s\n", private_run_dir, private_dir);
if (mount(private_run_dir, private_dir, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mount bind");
fs_logger2("mount", private_dir);
}
|