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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* Copyright (C) 2014-2021 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 "fids.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <dirent.h>
#include <glob.h>
#define MAXBUF 4096
static int dir_level = 1;
static int include_level = 0;
int arg_init = 0;
int arg_check = 0;
char *arg_homedir = NULL;
char *arg_dbfile = NULL;
int f_scanned = 0;
int f_modified = 0;
int f_new = 0;
int f_removed = 0;
int f_permissions = 0;
static inline int is_dir(const char *fname) {
assert(fname);
struct stat s;
if (stat(fname, &s) == 0) {
if (S_ISDIR(s.st_mode))
return 1;
}
return 0;
}
static inline int is_link(const char *fname) {
assert(fname);
char c;
ssize_t rv = readlink(fname, &c, 1);
return (rv != -1);
}
// mode is an array of 10 chars or more
static inline void file_mode(const char *fname, char *mode) {
assert(fname);
assert(mode);
struct stat s;
if (stat(fname, &s)) {
*mode = '\0';
return;
}
sprintf(mode, (s.st_mode & S_IRUSR) ? "r" : "-");
sprintf(mode + 1, (s.st_mode & S_IWUSR) ? "w" : "-");
sprintf(mode + 2, (s.st_mode & S_IXUSR) ? "x" : "-");
sprintf(mode + 3, (s.st_mode & S_IRGRP) ? "r" : "-");
sprintf(mode + 4, (s.st_mode & S_IWGRP) ? "w" : "-");
sprintf(mode + 5, (s.st_mode & S_IXGRP) ? "x" : "-");
sprintf(mode + 6, (s.st_mode & S_IROTH) ? "r" : "-");
sprintf(mode + 7, (s.st_mode & S_IWOTH) ? "w" : "-");
sprintf(mode + 8, (s.st_mode & S_IXOTH) ? "x" : "-");
}
static void file_checksum(const char *fname) {
assert(fname);
int fd = open(fname, O_RDONLY);
if (fd == -1)
return;
off_t size = lseek(fd, 0, SEEK_END);
if (size < 0) {
close(fd);
return;
}
char *content = "empty";
int mmapped = 0;
if (size == 0) {
// empty files don't mmap - use "empty" string as the file content
size = 6; // strlen("empty") + 1
}
else {
content = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
mmapped = 1;
}
unsigned char checksum[KEY_SIZE / 8];
blake2b(checksum, sizeof(checksum), content, size);
if (mmapped)
munmap(content, size);
// calculate blake2 checksum
char str_checksum[(KEY_SIZE / 8) * 2 + 1];
int long unsigned i;
char *ptr = str_checksum;
for (i = 0; i < sizeof(checksum); i++, ptr += 2)
sprintf(ptr, "%02x", (unsigned char ) checksum[i]);
// build permissions string
char mode[10];
file_mode(fname, mode);
if (arg_init)
printf("%s\t%s\t%s\n", mode, str_checksum, fname);
else if (arg_check)
db_check(fname, str_checksum, mode);
else
assert(0);
f_scanned++;
if (f_scanned % 500 == 0)
fprintf(stderr, "%d ", f_scanned);
fflush(0);
}
void list_directory(const char *fname) {
assert(fname);
if (dir_level > MAX_DIR_LEVEL) {
fprintf(stderr, "Warning fids: maximum depth level exceeded for %s\n", fname);
return;
}
if (db_exclude_check(fname))
return;
if (is_link(fname))
return;
if (!is_dir(fname)) {
file_checksum(fname);
return;
}
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(fname)))
return;
dir_level++;
while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
char *path;
if (asprintf(&path, "%s/%s", fname, entry->d_name) == -1)
errExit("asprintf");
list_directory(path);
free(path);
}
closedir(dir);
dir_level--;
}
void globbing(const char *fname) {
assert(fname);
// filter top directory
if (strcmp(fname, "/") == 0)
return;
glob_t globbuf;
int globerr = glob(fname, GLOB_NOCHECK | GLOB_NOSORT | GLOB_PERIOD, NULL, &globbuf);
if (globerr) {
fprintf(stderr, "Error fids: failed to glob pattern %s\n", fname);
exit(1);
}
long unsigned i;
for (i = 0; i < globbuf.gl_pathc; i++) {
char *path = globbuf.gl_pathv[i];
assert(path);
list_directory(path);
}
globfree(&globbuf);
}
static void process_config(const char *fname) {
assert(fname);
if (++include_level >= MAX_INCLUDE_LEVEL) {
fprintf(stderr, "Error ids: maximum include level for config files exceeded\n");
exit(1);
}
fprintf(stderr, "Opening config file %s\n", fname);
int fd = open(fname, O_RDONLY|O_CLOEXEC);
if (fd < 0) {
if (include_level == 1) {
fprintf(stderr, "Error ids: cannot open config file %s\n", fname);
exit(1);
}
return;
}
// make sure the file is owned by root
struct stat s;
if (fstat(fd, &s)) {
fprintf(stderr, "Error ids: cannot stat config file %s\n", fname);
exit(1);
}
if (s.st_uid || s.st_gid) {
fprintf(stderr, "Error ids: config file not owned by root\n");
exit(1);
}
fprintf(stderr, "Loading config file %s\n", fname);
FILE *fp = fdopen(fd, "r");
if (!fp) {
fprintf(stderr, "Error fids: cannot open config file %s\n", fname);
exit(1);
}
char buf[MAXBUF];
int line = 0;
while (fgets(buf, MAXBUF, fp)) {
line++;
// trim \n
char *ptr = strchr(buf, '\n');
if (ptr)
*ptr = '\0';
// comments
ptr = strchr(buf, '#');
if (ptr)
*ptr = '\0';
// empty space
ptr = buf;
while (*ptr == ' ' || *ptr == '\t')
ptr++;
char *start = ptr;
// empty line
if (*start == '\0')
continue;
// trailing spaces
ptr = start + strlen(start);
ptr--;
while (*ptr == ' ' || *ptr == '\t')
*ptr-- = '\0';
// replace ${HOME}
if (strncmp(start, "include", 7) == 0) {
ptr = start + 7;
if ((*ptr != ' ' && *ptr != '\t') || *ptr == '\0') {
fprintf(stderr, "Error fids: invalid line %d in %s\n", line, fname);
exit(1);
}
while (*ptr == ' ' || *ptr == '\t')
ptr++;
if (*ptr == '/')
process_config(ptr);
else {
// assume the file is in /etc/firejail
char *tmp;
if (asprintf(&tmp, "/etc/firejail/%s", ptr) == -1)
errExit("asprintf");
process_config(tmp);
free(tmp);
}
}
else if (*start == '!') {
// exclude file or dir
start++;
if (strncmp(start, "${HOME}", 7))
db_exclude_add(start);
else {
char *fname;
if (asprintf(&fname, "%s%s", arg_homedir, start + 7) == -1)
errExit("asprintf");
db_exclude_add(fname);
free(fname);
}
}
else if (strncmp(start, "${HOME}", 7))
globbing(start);
else {
char *fname;
if (asprintf(&fname, "%s%s", arg_homedir, start + 7) == -1)
errExit("asprintf");
globbing(fname);
free(fname);
}
}
fclose(fp);
include_level--;
}
void usage(void) {
printf("Usage: fids [--help|-h|-?] --init|--check homedir\n");
}
int main(int argc, char **argv) {
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 ||
strcmp(argv[i], "-?") == 0 ||
strcmp(argv[i], "--help") == 0) {
usage();
return 0;
}
else if (strcmp(argv[i], "--init") == 0)
arg_init = 1;
else if (strcmp(argv[i], "--check") == 0)
arg_check = 1;
else if (strncmp(argv[i], "--", 2) == 0) {
fprintf(stderr, "Error fids: invalid argument %s\n", argv[i]);
exit(1);
}
}
if (argc != 3) {
fprintf(stderr, "Error fids: invalid number of arguments\n");
exit(1);
}
arg_homedir = argv[2];
int op = arg_check + arg_init;
if (op == 0 || op == 2) {
fprintf(stderr, "Error fids: use either --init or --check\n");
exit(1);
}
if (arg_init) {
process_config(SYSCONFDIR"/ids.config");
fprintf(stderr, "\n%d files scanned\n", f_scanned);
fprintf(stderr, "IDS database initialized\n");
}
else if (arg_check) {
if (db_init()) {
fprintf(stderr, "Error: IDS database not initialized, please run \"firejail --ids-init\"\n");
exit(1);
}
process_config(SYSCONFDIR"/ids.config");
fprintf(stderr, "\n%d files scanned: modified %d, permissions %d, new %d, removed %d\n",
f_scanned, f_modified, f_permissions, f_new, f_removed);
db_missing();
}
else
assert(0);
return 0;
}
|