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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
|
/*
* 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 <linux/limits.h>
#include <fnmatch.h>
#include <glob.h>
#include <dirent.h>
#include <fcntl.h>
#include <errno.h>
static void create_empty_dir(void) {
struct stat s;
if (stat(RUN_RO_DIR, &s)) {
/* coverity[toctou] */
int rv = mkdir(RUN_RO_DIR, S_IRUSR | S_IXUSR);
if (rv == -1)
errExit("mkdir");
if (chown(RUN_RO_DIR, 0, 0) < 0)
errExit("chown");
}
}
static void create_empty_file(void) {
struct stat s;
if (stat(RUN_RO_FILE, &s)) {
/* coverity[toctou] */
FILE *fp = fopen(RUN_RO_FILE, "w");
if (!fp)
errExit("fopen");
fclose(fp);
if (chown(RUN_RO_FILE, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_RO_FILE, S_IRUSR) < 0)
errExit("chown");
}
}
// build /run/firejail directory
void fs_build_firejail_dir(void) {
struct stat s;
// CentOS 6 doesn't have /run directory
if (stat(RUN_FIREJAIL_BASEDIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_BASEDIR);
/* coverity[toctou] */
int rv = mkdir(RUN_FIREJAIL_BASEDIR, 0755);
if (rv == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_BASEDIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_BASEDIR, 0755) < 0)
errExit("chmod");
}
else { // check /tmp/firejail directory belongs to root end exit if doesn't!
if (s.st_uid != 0 || s.st_gid != 0) {
fprintf(stderr, "Error: non-root %s directory, exiting...\n", RUN_FIREJAIL_DIR);
exit(1);
}
}
if (stat(RUN_FIREJAIL_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_DIR);
/* coverity[toctou] */
int rv = mkdir(RUN_FIREJAIL_DIR, 0755);
if (rv == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_DIR, 0755) < 0)
errExit("chmod");
}
if (stat(RUN_FIREJAIL_NETWORK_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_NETWORK_DIR);
if (mkdir(RUN_FIREJAIL_NETWORK_DIR, 0755) == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_NETWORK_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_NETWORK_DIR, 0755) < 0)
errExit("chmod");
}
if (stat(RUN_FIREJAIL_BANDWIDTH_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_BANDWIDTH_DIR);
if (mkdir(RUN_FIREJAIL_BANDWIDTH_DIR, 0755) == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_BANDWIDTH_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_BANDWIDTH_DIR, 0755) < 0)
errExit("chmod");
}
if (stat(RUN_FIREJAIL_NAME_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_NAME_DIR);
if (mkdir(RUN_FIREJAIL_NAME_DIR, 0755) == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_NAME_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_NAME_DIR, 0755) < 0)
errExit("chmod");
}
if (stat(RUN_FIREJAIL_X11_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_FIREJAIL_X11_DIR);
if (mkdir(RUN_FIREJAIL_X11_DIR, 0755) == -1)
errExit("mkdir");
if (chown(RUN_FIREJAIL_X11_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_FIREJAIL_X11_DIR, 0755) < 0)
errExit("chmod");
}
create_empty_dir();
create_empty_file();
}
// build /tmp/firejail/mnt directory
static int tmpfs_mounted = 0;
#ifdef HAVE_CHROOT
static void fs_build_remount_mnt_dir(void) {
tmpfs_mounted = 0;
fs_build_mnt_dir();
}
#endif
void fs_build_mnt_dir(void) {
struct stat s;
fs_build_firejail_dir();
// create /run/firejail/mnt directory
if (stat(RUN_MNT_DIR, &s)) {
if (arg_debug)
printf("Creating %s directory\n", RUN_MNT_DIR);
/* coverity[toctou] */
int rv = mkdir(RUN_MNT_DIR, 0755);
if (rv == -1)
errExit("mkdir");
if (chown(RUN_MNT_DIR, 0, 0) < 0)
errExit("chown");
if (chmod(RUN_MNT_DIR, 0755) < 0)
errExit("chmod");
}
// ... and mount tmpfs on top of it
if (!tmpfs_mounted) {
// mount tmpfs on top of /run/firejail/mnt
if (arg_debug)
printf("Mounting tmpfs on %s directory\n", RUN_MNT_DIR);
if (mount("tmpfs", RUN_MNT_DIR, "tmpfs", MS_NOSUID | MS_STRICTATIME | MS_REC, "mode=755,gid=0") < 0)
errExit("mounting /tmp/firejail/mnt");
tmpfs_mounted = 1;
fs_logger2("tmpfs", RUN_MNT_DIR);
}
}
// grab a copy of cp command
void fs_build_cp_command(void) {
struct stat s;
fs_build_mnt_dir();
if (stat(RUN_CP_COMMAND, &s)) {
char* fname = realpath("/bin/cp", NULL);
if (fname == NULL) {
fprintf(stderr, "Error: /bin/cp not found\n");
exit(1);
}
if (stat(fname, &s)) {
fprintf(stderr, "Error: /bin/cp not found\n");
exit(1);
}
if (is_link(fname)) {
fprintf(stderr, "Error: invalid /bin/cp file\n");
exit(1);
}
int rv = copy_file(fname, RUN_CP_COMMAND);
if (rv) {
fprintf(stderr, "Error: cannot access /bin/cp\n");
exit(1);
}
/* coverity[toctou] */
if (chown(RUN_CP_COMMAND, 0, 0))
errExit("chown");
if (chmod(RUN_CP_COMMAND, 0755))
errExit("chmod");
free(fname);
}
}
// delete the temporary cp command
void fs_delete_cp_command(void) {
unlink(RUN_CP_COMMAND);
}
//***********************************************
// process profile file
//***********************************************
typedef enum {
BLACKLIST_FILE,
BLACKLIST_NOLOG,
MOUNT_READONLY,
MOUNT_TMPFS,
OPERATION_MAX
} OPERATION;
typedef enum {
UNSUCCESSFUL,
SUCCESSFUL
} LAST_DISABLE_OPERATION;
LAST_DISABLE_OPERATION last_disable = UNSUCCESSFUL;
static void disable_file(OPERATION op, const char *filename) {
assert(filename);
assert(op <OPERATION_MAX);
last_disable = UNSUCCESSFUL;
// rebuild /run/firejail directory in case tmpfs was mounted on top of /run
fs_build_firejail_dir();
// Resolve all symlinks
char* fname = realpath(filename, NULL);
if (fname == NULL && errno != EACCES) {
if (arg_debug)
printf("Warning (realpath): %s is an invalid file, skipping...\n", filename);
return;
}
if (fname == NULL && errno == EACCES) {
if (arg_debug)
printf("Debug: no access to file %s, forcing mount\n", filename);
// realpath and stat funtions will fail on FUSE filesystems
// they don't seem to like a uid of 0
// force mounting
int rv = mount(RUN_RO_DIR, filename, "none", MS_BIND, "mode=400,gid=0");
if (rv == 0)
last_disable = SUCCESSFUL;
else {
rv = mount(RUN_RO_FILE, filename, "none", MS_BIND, "mode=400,gid=0");
if (rv == 0)
last_disable = SUCCESSFUL;
}
if (last_disable == SUCCESSFUL) {
if (arg_debug)
printf("Disable %s\n", filename);
if (op == BLACKLIST_FILE)
fs_logger2("blacklist", filename);
else
fs_logger2("blacklist-nolog", filename);
}
else {
if (arg_debug)
printf("Warning (blacklisting): %s is an invalid file, skipping...\n", filename);
}
return;
}
// if the file is not present, do nothing
struct stat s;
if (fname == NULL)
return;
if (stat(fname, &s) == -1) {
if (arg_debug)
printf("Warning: %s does not exist, skipping...\n", fname);
free(fname);
return;
}
// modify the file
if (op == BLACKLIST_FILE || op == BLACKLIST_NOLOG) {
// some distros put all executables under /usr/bin and make /bin a symbolic link
if ((strcmp(fname, "/bin") == 0 || strcmp(fname, "/usr/bin") == 0) &&
is_link(filename) &&
S_ISDIR(s.st_mode))
fprintf(stderr, "Warning: %s directory link was not blacklisted\n", filename);
else {
if (arg_debug)
printf("Disable %s\n", fname);
else if (arg_debug_blacklists) {
printf("Disable %s", fname);
if (op == BLACKLIST_FILE)
printf("\n");
else
printf(" - no logging\n");
}
if (S_ISDIR(s.st_mode)) {
if (mount(RUN_RO_DIR, fname, "none", MS_BIND, "mode=400,gid=0") < 0)
errExit("disable file");
}
else {
if (mount(RUN_RO_FILE, fname, "none", MS_BIND, "mode=400,gid=0") < 0)
errExit("disable file");
}
last_disable = SUCCESSFUL;
if (op == BLACKLIST_FILE)
fs_logger2("blacklist", fname);
else
fs_logger2("blacklist-nolog", fname);
}
}
else if (op == MOUNT_READONLY) {
if (arg_debug)
printf("Mounting read-only %s\n", fname);
fs_rdonly(fname);
// todo: last_disable = SUCCESSFUL;
}
else if (op == MOUNT_TMPFS) {
if (S_ISDIR(s.st_mode)) {
if (arg_debug)
printf("Mounting tmpfs on %s\n", fname);
// preserve owner and mode for the directory
if (mount("tmpfs", fname, "tmpfs", MS_NOSUID | MS_NODEV | MS_STRICTATIME | MS_REC, 0) < 0)
errExit("mounting tmpfs");
/* coverity[toctou] */
if (chown(fname, s.st_uid, s.st_gid) == -1)
errExit("mounting tmpfs chmod");
last_disable = SUCCESSFUL;
fs_logger2("tmpfs", fname);
}
else
printf("Warning: %s is not a directory; cannot mount a tmpfs on top of it.\n", fname);
}
else
assert(0);
free(fname);
}
// Treat pattern as a shell glob pattern and blacklist matching files
static void globbing(OPERATION op, const char *pattern, const char *noblacklist[], size_t noblacklist_len) {
assert(pattern);
glob_t globbuf;
// Profiles contain blacklists for files that might not exist on a user's machine.
// GLOB_NOCHECK makes that okay.
int globerr = glob(pattern, GLOB_NOCHECK | GLOB_NOSORT, NULL, &globbuf);
if (globerr) {
fprintf(stderr, "Error: failed to glob pattern %s\n", pattern);
exit(1);
}
size_t i, j;
for (i = 0; i < globbuf.gl_pathc; i++) {
char *path = globbuf.gl_pathv[i];
assert(path);
// /home/me/.* can glob to /home/me/.. which would blacklist /home/
const char *base = gnu_basename(path);
if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0)
continue;
// noblacklist is expected to be short in normal cases, so stupid and correct brute force is okay
bool okay_to_blacklist = true;
for (j = 0; j < noblacklist_len; j++) {
int result = fnmatch(noblacklist[j], path, FNM_PATHNAME);
if (result == FNM_NOMATCH)
continue;
else if (result == 0) {
okay_to_blacklist = false;
break;
}
else {
fprintf(stderr, "Error: failed to compare path %s with pattern %s\n", path, noblacklist[j]);
exit(1);
}
}
if (okay_to_blacklist)
disable_file(op, path);
else if (arg_debug)
printf("Not blacklist %s\n", path);
}
globfree(&globbuf);
}
// blacklist files or directoies by mounting empty files on top of them
void fs_blacklist(void) {
char *homedir = cfg.homedir;
assert(homedir);
ProfileEntry *entry = cfg.profile;
if (!entry)
return;
size_t noblacklist_c = 0;
size_t noblacklist_m = 32;
char **noblacklist = calloc(noblacklist_m, sizeof(*noblacklist));
if (noblacklist == NULL)
errExit("failed allocating memory for noblacklist entries");
while (entry) {
OPERATION op = OPERATION_MAX;
char *ptr;
// whitelist commands handled by fs_whitelist()
if (strncmp(entry->data, "whitelist ", 10) == 0 || *entry->data == '\0') {
entry = entry->next;
continue;
}
// process bind command
if (strncmp(entry->data, "bind ", 5) == 0) {
char *dname1 = entry->data + 5;
char *dname2 = split_comma(dname1);
if (dname2 == NULL) {
fprintf(stderr, "Error: second directory missing in bind command\n");
entry = entry->next;
continue;
}
struct stat s;
if (stat(dname1, &s) == -1) {
fprintf(stderr, "Error: cannot find directories for bind command\n");
entry = entry->next;
continue;
}
if (stat(dname2, &s) == -1) {
fprintf(stderr, "Error: cannot find directories for bind command\n");
entry = entry->next;
continue;
}
// mount --bind olddir newdir
if (arg_debug)
printf("Mount-bind %s on top of %s\n", dname1, dname2);
// preserve dname2 mode and ownership
if (mount(dname1, dname2, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mount bind");
/* coverity[toctou] */
if (chown(dname2, s.st_uid, s.st_gid) == -1)
errExit("mount-bind chown");
/* coverity[toctou] */
if (chmod(dname2, s.st_mode) == -1)
errExit("mount-bind chmod");
entry = entry->next;
continue;
}
// Process noblacklist command
if (strncmp(entry->data, "noblacklist ", 12) == 0) {
if (noblacklist_c >= noblacklist_m) {
noblacklist_m *= 2;
noblacklist = realloc(noblacklist, sizeof(*noblacklist) * noblacklist_m);
if (noblacklist == NULL)
errExit("failed increasing memory for noblacklist entries");
}
else
noblacklist[noblacklist_c++] = expand_home(entry->data + 12, homedir);
entry = entry->next;
continue;
}
// process blacklist command
if (strncmp(entry->data, "blacklist ", 10) == 0) {
ptr = entry->data + 10;
op = BLACKLIST_FILE;
}
else if (strncmp(entry->data, "blacklist-nolog ", 16) == 0) {
ptr = entry->data + 16;
op = BLACKLIST_NOLOG;
}
else if (strncmp(entry->data, "read-only ", 10) == 0) {
ptr = entry->data + 10;
op = MOUNT_READONLY;
}
else if (strncmp(entry->data, "tmpfs ", 6) == 0) {
ptr = entry->data + 6;
op = MOUNT_TMPFS;
}
else {
fprintf(stderr, "Error: invalid profile line %s\n", entry->data);
entry = entry->next;
continue;
}
// replace home macro in blacklist array
char *new_name = expand_home(ptr, homedir);
ptr = new_name;
// expand path macro - look for the file in /bin, /usr/bin, /sbin and /usr/sbin directories
if (ptr) {
if (strncmp(ptr, "${PATH}", 7) == 0) {
char *fname = ptr + 7;
size_t fname_len = strlen(fname);
char **paths = build_paths(); //{"/bin", "/sbin", "/usr/bin", "/usr/sbin", NULL};
int i = 0;
while (paths[i] != NULL) {
char *path = paths[i];
i++;
char newname[strlen(path) + fname_len + 1];
sprintf(newname, "%s%s", path, fname);
globbing(op, newname, (const char**)noblacklist, noblacklist_c);
}
}
else
globbing(op, ptr, (const char**)noblacklist, noblacklist_c);
}
if (new_name)
free(new_name);
entry = entry->next;
}
size_t i;
for (i = 0; i < noblacklist_c; i++) free(noblacklist[i]);
free(noblacklist);
}
//***********************************************
// mount namespace
//***********************************************
// remount a directory read-only
void fs_rdonly(const char *dir) {
assert(dir);
// check directory exists
struct stat s;
int rv = stat(dir, &s);
if (rv == 0) {
// mount --bind /bin /bin
if (mount(dir, dir, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mount read-only");
// mount --bind -o remount,ro /bin
if (mount(NULL, dir, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_REC, NULL) < 0)
errExit("mount read-only");
fs_logger2("read-only", dir);
}
}
void fs_rdonly_noexit(const char *dir) {
assert(dir);
// check directory exists
struct stat s;
int rv = stat(dir, &s);
if (rv == 0) {
int merr = 0;
// mount --bind /bin /bin
if (mount(dir, dir, NULL, MS_BIND|MS_REC, NULL) < 0)
merr = 1;
// mount --bind -o remount,ro /bin
if (mount(NULL, dir, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_REC, NULL) < 0)
merr = 1;
if (merr)
fprintf(stderr, "Warning: cannot mount %s read-only\n", dir);
else
fs_logger2("read-only", dir);
}
}
// mount /proc and /sys directories
void fs_proc_sys_dev_boot(void) {
struct stat s;
if (arg_debug)
printf("Remounting /proc and /proc/sys filesystems\n");
if (mount("proc", "/proc", "proc", MS_NOSUID | MS_NOEXEC | MS_NODEV | MS_REC, NULL) < 0)
errExit("mounting /proc");
fs_logger("remount /proc");
// remount /proc/sys readonly
if (mount("/proc/sys", "/proc/sys", NULL, MS_BIND | MS_REC, NULL) < 0)
errExit("mounting /proc/sys");
if (mount(NULL, "/proc/sys", NULL, MS_BIND | MS_REMOUNT | MS_RDONLY | MS_REC, NULL) < 0)
errExit("mounting /proc/sys");
fs_logger("read-only /proc/sys");
/* Mount a version of /sys that describes the network namespace */
if (arg_debug)
printf("Remounting /sys directory\n");
if (umount2("/sys", MNT_DETACH) < 0)
fprintf(stderr, "Warning: failed to unmount /sys\n");
else {
if (mount("sysfs", "/sys", "sysfs", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REC, NULL) < 0)
fprintf(stderr, "Warning: failed to mount /sys\n");
else
fs_logger("remount /sys");
}
if (stat("/sys/firmware", &s) == 0) {
disable_file(BLACKLIST_FILE, "/sys/firmware");
}
if (stat("/sys/hypervisor", &s) == 0) {
disable_file(BLACKLIST_FILE, "/sys/hypervisor");
}
if (stat("/sys/fs", &s) == 0) {
disable_file(BLACKLIST_FILE, "/sys/fs");
}
if (stat("/sys/module", &s) == 0) {
disable_file(BLACKLIST_FILE, "/sys/module");
}
if (stat("/sys/power", &s) == 0) {
disable_file(BLACKLIST_FILE, "/sys/power");
}
// if (mount("sysfs", "/sys", "sysfs", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REC, NULL) < 0)
// errExit("mounting /sys");
// Disable SysRq
// a linux box can be shut down easily using the following commands (as root):
// # echo 1 > /proc/sys/kernel/sysrq
// #echo b > /proc/sysrq-trigger
// for more information see https://www.kernel.org/doc/Documentation/sysrq.txt
if (arg_debug)
printf("Disable /proc/sysrq-trigger\n");
fs_rdonly_noexit("/proc/sysrq-trigger");
// disable hotplug and uevent_helper
if (arg_debug)
printf("Disable /proc/sys/kernel/hotplug\n");
fs_rdonly_noexit("/proc/sys/kernel/hotplug");
if (arg_debug)
printf("Disable /sys/kernel/uevent_helper\n");
fs_rdonly_noexit("/sys/kernel/uevent_helper");
// read-only /proc/irq and /proc/bus
if (arg_debug)
printf("Disable /proc/irq\n");
fs_rdonly_noexit("/proc/irq");
if (arg_debug)
printf("Disable /proc/bus\n");
fs_rdonly_noexit("/proc/bus");
// disable /proc/kcore
disable_file(BLACKLIST_FILE, "/proc/kcore");
// disable /proc/kallsyms
disable_file(BLACKLIST_FILE, "/proc/kallsyms");
// disable /boot
if (stat("/boot", &s) == 0) {
if (arg_debug)
printf("Disable /boot directory\n");
disable_file(BLACKLIST_FILE, "/boot");
}
// disable /selinux
if (stat("/selinux", &s) == 0) {
if (arg_debug)
printf("Disable /selinux directory\n");
disable_file(BLACKLIST_FILE, "/selinux");
}
// disable /dev/port
if (stat("/dev/port", &s) == 0) {
disable_file(BLACKLIST_FILE, "/dev/port");
}
if (getuid() != 0) {
// disable /dev/kmsg
if (stat("/dev/kmsg", &s) == 0) {
disable_file(BLACKLIST_FILE, "/dev/kmsg");
}
// disable /proc/kmsg
if (stat("/proc/kmsg", &s) == 0) {
disable_file(BLACKLIST_FILE, "/proc/kmsg");
}
}
}
// disable firejail configuration in /etc/firejail and in ~/.config/firejail
static void disable_firejail_config(void) {
struct stat s;
if (stat("/etc/firejail", &s) == 0)
disable_file(BLACKLIST_FILE, "/etc/firejail");
char *fname;
if (asprintf(&fname, "%s/.config/firejail", cfg.homedir) == -1)
errExit("asprintf");
if (stat(fname, &s) == 0)
disable_file(BLACKLIST_FILE, fname);
if (stat("/usr/local/etc/firejail", &s) == 0)
disable_file(BLACKLIST_FILE, "/usr/local/etc/firejail");
if (strcmp(PREFIX, "/usr/local")) {
if (asprintf(&fname, "%s/etc/firejail", PREFIX) == -1)
errExit("asprintf");
if (stat(fname, &s) == 0)
disable_file(BLACKLIST_FILE, fname);
}
free(fname);
}
// build a basic read-only filesystem
void fs_basic_fs(void) {
if (arg_debug)
printf("Mounting read-only /bin, /sbin, /lib, /lib32, /lib64, /usr, /etc, /var\n");
fs_rdonly("/bin");
fs_rdonly("/sbin");
fs_rdonly("/lib");
fs_rdonly("/lib64");
fs_rdonly("/lib32");
fs_rdonly("/usr");
fs_rdonly("/etc");
fs_rdonly("/var");
// update /var directory in order to support multiple sandboxes running on the same root directory
if (!arg_private_dev)
fs_dev_shm();
fs_var_lock();
fs_var_tmp();
fs_var_log();
fs_var_lib();
fs_var_cache();
fs_var_utmp();
// don't leak user information
restrict_users();
disable_firejail_config();
}
// mount overlayfs on top of / directory
// mounting an overlay and chrooting into it:
//
// Old Ubuntu kernel
// # cd ~
// # mkdir -p overlay/root
// # mkdir -p overlay/diff
// # mount -t overlayfs -o lowerdir=/,upperdir=/root/overlay/diff overlayfs /root/overlay/root
// # chroot /root/overlay/root
// to shutdown, first exit the chroot and then unmount the overlay
// # exit
// # umount /root/overlay/root
//
// Kernels 3.18+
// # cd ~
// # mkdir -p overlay/root
// # mkdir -p overlay/diff
// # mkdir -p overlay/work
// # mount -t overlay -o lowerdir=/,upperdir=/root/overlay/diff,workdir=/root/overlay/work overlay /root/overlay/root
// # cat /etc/mtab | grep overlay
// /root/overlay /root/overlay/root overlay rw,relatime,lowerdir=/,upperdir=/root/overlay/diff,workdir=/root/overlay/work 0 0
// # chroot /root/overlay/root
// to shutdown, first exit the chroot and then unmount the overlay
// # exit
// # umount /root/overlay/root
// to do: fix the code below; also, it might work without /dev; impose seccomp/caps filters when not root
#include <sys/utsname.h>
void fs_overlayfs(void) {
// check kernel version
struct utsname u;
int rv = uname(&u);
if (rv != 0)
errExit("uname");
int major;
int minor;
if (2 != sscanf(u.release, "%d.%d", &major, &minor)) {
fprintf(stderr, "Error: cannot extract Linux kernel version: %s\n", u.version);
exit(1);
}
if (arg_debug)
printf("Linux kernel version %d.%d\n", major, minor);
int oldkernel = 0;
if (major < 3) {
fprintf(stderr, "Error: minimum kernel version required 3.x\n");
exit(1);
}
if (major == 3 && minor < 18)
oldkernel = 1;
// build overlay directories
fs_build_mnt_dir();
char *oroot;
if(asprintf(&oroot, "%s/oroot", RUN_MNT_DIR) == -1)
errExit("asprintf");
if (mkdir(oroot, 0755))
errExit("mkdir");
if (chown(oroot, 0, 0) < 0)
errExit("chown");
if (chmod(oroot, 0755) < 0)
errExit("chmod");
char *basedir = RUN_MNT_DIR;
if (arg_overlay_keep) {
// set base for working and diff directories
basedir = cfg.overlay_dir;
if (mkdir(basedir, 0755) != 0) {
fprintf(stderr, "Error: cannot create overlay directory\n");
exit(1);
}
}
char *odiff;
if(asprintf(&odiff, "%s/odiff", basedir) == -1)
errExit("asprintf");
if (mkdir(odiff, 0755))
errExit("mkdir");
if (chown(odiff, 0, 0) < 0)
errExit("chown");
if (chmod(odiff, 0755) < 0)
errExit("chmod");
char *owork;
if(asprintf(&owork, "%s/owork", basedir) == -1)
errExit("asprintf");
if (mkdir(owork, 0755))
errExit("mkdir");
if (chown(owork, 0, 0) < 0)
errExit("chown");
if (chmod(owork, 0755) < 0)
errExit("chmod");
// mount overlayfs
if (arg_debug)
printf("Mounting OverlayFS\n");
char *option;
if (oldkernel) { // old Ubuntu/OpenSUSE kernels
if (arg_overlay_keep) {
fprintf(stderr, "Error: option --overlay= not available for kernels older than 3.18\n");
exit(1);
}
if (asprintf(&option, "lowerdir=/,upperdir=%s", odiff) == -1)
errExit("asprintf");
if (mount("overlayfs", oroot, "overlayfs", MS_MGC_VAL, option) < 0)
errExit("mounting overlayfs");
}
else { // kernel 3.18 or newer
if (asprintf(&option, "lowerdir=/,upperdir=%s,workdir=%s", odiff, owork) == -1)
errExit("asprintf");
if (mount("overlay", oroot, "overlay", MS_MGC_VAL, option) < 0)
errExit("mounting overlayfs");
//***************************
// issue #263 start code
// My setup has a separate mount point for /home. When the overlay is mounted,
// the overlay does not contain the original /home contents.
// I added code to create a second overlay for /home if the overlay home dir is empty and this seems to work
// @dshmgh, Jan 2016
{
char *overlayhome;
struct stat s;
char *hroot;
char *hdiff;
char *hwork;
// dons add debug
if (arg_debug) printf ("DEBUG: chroot dirs are oroot %s odiff %s owork %s\n",oroot,odiff,owork);
// BEFORE NEXT, WE NEED TO TEST IF /home has any contents or do we need to mount it?
// must create var for oroot/cfg.homedir
if (asprintf(&overlayhome,"%s%s",oroot,cfg.homedir) == -1)
errExit("asprintf");
if (arg_debug) printf ("DEBUG: overlayhome var holds ##%s##\n",overlayhome);
// if no homedir in overlay -- create another overlay for /home
if (stat(overlayhome, &s) == -1) {
if(asprintf(&hroot, "%s/oroot/home", RUN_MNT_DIR) == -1)
errExit("asprintf");
if(asprintf(&hdiff, "%s/hdiff", basedir) == -1)
errExit("asprintf");
if (mkdir(hdiff, S_IRWXU | S_IRWXG | S_IRWXO))
errExit("mkdir");
if (chown(hdiff, 0, 0) < 0)
errExit("chown");
if (chmod(hdiff, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
errExit("chmod");
if(asprintf(&hwork, "%s/hwork", basedir) == -1)
errExit("asprintf");
if (mkdir(hwork, S_IRWXU | S_IRWXG | S_IRWXO))
errExit("mkdir");
if (chown(hwork, 0, 0) < 0)
errExit("chown");
if (chmod(hwork, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0)
errExit("chmod");
// no homedir in overlay so now mount another overlay for /home
if (asprintf(&option, "lowerdir=/home,upperdir=%s,workdir=%s", hdiff, hwork) == -1)
errExit("asprintf");
if (mount("overlay", hroot, "overlay", MS_MGC_VAL, option) < 0)
errExit("mounting overlayfs for mounted home directory");
printf("OverlayFS for /home configured in %s directory\n", basedir);
} // stat(overlayhome)
free(overlayhome);
}
// issue #263 end code
//***************************
}
printf("OverlayFS configured in %s directory\n", basedir);
// mount-bind dev directory
if (arg_debug)
printf("Mounting /dev\n");
char *dev;
if (asprintf(&dev, "%s/dev", oroot) == -1)
errExit("asprintf");
if (mount("/dev", dev, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting /dev");
fs_logger("whitelist /dev");
// mount-bind run directory
if (arg_debug)
printf("Mounting /run\n");
char *run;
if (asprintf(&run, "%s/run", oroot) == -1)
errExit("asprintf");
if (mount("/run", run, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting /run");
fs_logger("whitelist /run");
// chroot in the new filesystem
if (chroot(oroot) == -1)
errExit("chroot");
// update /var directory in order to support multiple sandboxes running on the same root directory
if (!arg_private_dev)
fs_dev_shm();
fs_var_lock();
fs_var_tmp();
fs_var_log();
fs_var_lib();
fs_var_cache();
fs_var_utmp();
// don't leak user information
restrict_users();
disable_firejail_config();
// cleanup and exit
free(option);
free(oroot);
free(odiff);
}
#ifdef HAVE_CHROOT
// return 1 if error
int fs_check_chroot_dir(const char *rootdir) {
EUID_ASSERT();
assert(rootdir);
struct stat s;
char *name;
// check /dev
if (asprintf(&name, "%s/dev", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == -1) {
fprintf(stderr, "Error: cannot find /dev in chroot directory\n");
return 1;
}
free(name);
// check /var/tmp
if (asprintf(&name, "%s/var/tmp", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == -1) {
fprintf(stderr, "Error: cannot find /var/tmp in chroot directory\n");
return 1;
}
free(name);
// check /proc
if (asprintf(&name, "%s/proc", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == -1) {
fprintf(stderr, "Error: cannot find /proc in chroot directory\n");
return 1;
}
free(name);
// check /proc
if (asprintf(&name, "%s/tmp", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == -1) {
fprintf(stderr, "Error: cannot find /tmp in chroot directory\n");
return 1;
}
free(name);
// check /bin/bash
if (asprintf(&name, "%s/bin/bash", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == -1) {
fprintf(stderr, "Error: cannot find /bin/bash in chroot directory\n");
return 1;
}
free(name);
return 0;
}
// chroot into an existing directory; mount exiting /dev and update /etc/resolv.conf
void fs_chroot(const char *rootdir) {
assert(rootdir);
//***********************************
// mount-bind a /dev in rootdir
//***********************************
// mount /dev
char *newdev;
if (asprintf(&newdev, "%s/dev", rootdir) == -1)
errExit("asprintf");
if (arg_debug)
printf("Mounting /dev on %s\n", newdev);
if (mount("/dev", newdev, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting /dev");
// some older distros don't have a /run directory
// create one by default
// no exit on error, let the user deal with any problems
char *rundir;
if (asprintf(&rundir, "%s/run", rootdir) == -1)
errExit("asprintf");
if (!is_dir(rundir)) {
int rv = mkdir(rundir, 0755);
(void) rv;
rv = chown(rundir, 0, 0);
(void) rv;
}
// copy /etc/resolv.conf in chroot directory
// if resolv.conf in chroot is a symbolic link, this will fail
// no exit on error, let the user deal with the problem
char *fname;
if (asprintf(&fname, "%s/etc/resolv.conf", rootdir) == -1)
errExit("asprintf");
if (arg_debug)
printf("Updating /etc/resolv.conf in %s\n", fname);
if (is_link(fname)) {
fprintf(stderr, "Error: invalid %s file\n", fname);
exit(1);
}
if (copy_file("/etc/resolv.conf", fname) == -1)
fprintf(stderr, "Warning: /etc/resolv.conf not initialized\n");
// chroot into the new directory
if (arg_debug)
printf("Chrooting into %s\n", rootdir);
if (chroot(rootdir) < 0)
errExit("chroot");
// mount a new tmpfs in /run/firejail/mnt - the old one was lost in chroot
fs_build_remount_mnt_dir();
// update /var directory in order to support multiple sandboxes running on the same root directory
if (!arg_private_dev)
fs_dev_shm();
fs_var_lock();
fs_var_tmp();
fs_var_log();
fs_var_lib();
fs_var_cache();
fs_var_utmp();
// don't leak user information
restrict_users();
disable_firejail_config();
}
#endif
void fs_private_tmp(void) {
// mount tmpfs on top of /run/firejail/mnt
if (arg_debug)
printf("Mounting tmpfs on /tmp directory\n");
if (mount("tmpfs", "/tmp", "tmpfs", MS_NOSUID | MS_STRICTATIME | MS_REC, "mode=1777,gid=0") < 0)
errExit("mounting /tmp/firejail/mnt");
fs_logger2("tmpfs", "/tmp");
}
|