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
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
|
/*
* Copyright (C) 2014-2019 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/statvfs.h>
#include <sys/wait.h>
#include <linux/limits.h>
#include <fnmatch.h>
#include <glob.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#ifndef O_PATH
# define O_PATH 010000000
#endif
#define MAX_BUF 4096
#define EMPTY_STRING ("")
// check noblacklist statements not matched by a proper blacklist in disable-*.inc files
//#define TEST_NO_BLACKLIST_MATCHING
//***********************************************
// process profile file
//***********************************************
static char *opstr[] = {
[BLACKLIST_FILE] = "blacklist",
[BLACKLIST_NOLOG] = "blacklist-nolog",
[MOUNT_READONLY] = "read-only",
[MOUNT_TMPFS] = "tmpfs",
[MOUNT_NOEXEC] = "noexec",
[MOUNT_RDWR] = "read-write",
};
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;
// Resolve all symlinks
char* fname = realpath(filename, NULL);
if (fname == NULL && errno != EACCES) {
return;
}
if (fname == NULL && errno == EACCES) {
if (arg_debug)
printf("Debug: no access to file %s, forcing mount\n", filename);
// realpath and stat functions 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)
fwarning("%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)) {
fwarning("%s directory link was not blacklisted\n", filename);
}
else {
if (arg_debug) {
if (strcmp(filename, fname))
printf("Disable %s (requested %s)\n", fname, filename);
else
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 || op == MOUNT_RDWR || op == MOUNT_NOEXEC) {
fs_remount_rec(fname, op, 1);
// todo: last_disable = SUCCESSFUL;
}
else if (op == MOUNT_TMPFS) {
if (S_ISDIR(s.st_mode)) {
fs_tmpfs(fname, 0);
last_disable = SUCCESSFUL;
}
else
fwarning("%s is not a directory; cannot mount a tmpfs on top of it.\n", fname);
}
else
assert(0);
free(fname);
}
#ifdef TEST_NO_BLACKLIST_MATCHING
static int nbcheck_start = 0;
static size_t nbcheck_size = 0;
static int *nbcheck = NULL;
#endif
// 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);
#ifdef TEST_NO_BLACKLIST_MATCHING
if (nbcheck_start == 0) {
nbcheck_start = 1;
nbcheck_size = noblacklist_len;
nbcheck = malloc(sizeof(int) * noblacklist_len);
if (nbcheck == NULL)
errExit("malloc");
memset(nbcheck, 0, sizeof(int) * noblacklist_len);
}
#endif
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 | GLOB_PERIOD, 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;
#ifdef TEST_NO_BLACKLIST_MATCHING
if (j < nbcheck_size) // noblacklist checking
nbcheck[j] = 1;
#endif
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 directories by mounting empty files on top of them
void fs_blacklist(void) {
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 ||
strncmp(entry->data, "nowhitelist ", 12) == 0 ||
*entry->data == '\0') {
entry = entry->next;
continue;
}
// process bind command
if (strncmp(entry->data, "bind ", 5) == 0) {
struct stat s;
char *dname1 = entry->data + 5;
char *dname2 = split_comma(dname1);
if (dname2 == NULL ||
stat(dname1, &s) == -1 ||
stat(dname2, &s) == -1) {
fprintf(stderr, "Error: invalid bind command, directory missing\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 (set_perms(dname2, s.st_uid, s.st_gid,s.st_mode))
errExit("set_perms");
entry = entry->next;
continue;
}
// Process noblacklist command
if (strncmp(entry->data, "noblacklist ", 12) == 0) {
char **enames;
int i;
if (strncmp(entry->data + 12, "${PATH}", 7) == 0) {
// expand ${PATH} macro
char **paths = build_paths();
unsigned int npaths = count_paths();
enames = calloc(npaths, sizeof(char *));
if (!enames)
errExit("calloc");
for (i = 0; paths[i]; i++) {
if (asprintf(&enames[i], "%s%s", paths[i],
entry->data + 19) == -1)
errExit("asprintf");
}
assert(enames[npaths-1] == 0);
}
else {
// expand ${HOME} macro if found or pass as is
enames = calloc(2, sizeof(char *));
if (!enames)
errExit("calloc");
enames[0] = expand_macros(entry->data + 12);
assert(enames[1] == 0);
}
for (i = 0; enames[i]; i++) {
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");
}
noblacklist[noblacklist_c++] = enames[i];
}
free(enames);
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, "read-write ", 11) == 0) {
ptr = entry->data + 11;
op = MOUNT_RDWR;
}
else if (strncmp(entry->data, "noexec ", 7) == 0) {
ptr = entry->data + 7;
op = MOUNT_NOEXEC;
}
else if (strncmp(entry->data, "tmpfs ", 6) == 0) {
ptr = entry->data + 6;
op = MOUNT_TMPFS;
}
else if (strncmp(entry->data, "mkdir ", 6) == 0) {
EUID_USER();
fs_mkdir(entry->data + 6);
EUID_ROOT();
entry = entry->next;
continue;
}
else if (strncmp(entry->data, "mkfile ", 7) == 0) {
EUID_USER();
fs_mkfile(entry->data + 7);
EUID_ROOT();
entry = entry->next;
continue;
}
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_macros(ptr);
ptr = new_name;
// expand path macro - look for the file in /usr/local/bin, /usr/local/sbin, /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(); //{"/usr/local/bin", "/usr/local/sbin", "/bin", "/usr/bin/", "/sbin", "/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;
#ifdef TEST_NO_BLACKLIST_MATCHING
// noblacklist checking
for (i = 0; i < nbcheck_size; i++)
if (!arg_quiet && !nbcheck[i])
printf("TESTING warning: noblacklist %s not matched by a proper blacklist command in disable*.inc\n",
noblacklist[i]);
// free memory
if (nbcheck) {
free(nbcheck);
nbcheck = NULL;
nbcheck_size = 0;
}
#endif
for (i = 0; i < noblacklist_c; i++)
free(noblacklist[i]);
free(noblacklist);
}
static int get_mount_flags(const char *path, unsigned long *flags) {
struct statvfs buf;
if (statvfs(path, &buf) < 0)
return -errno;
*flags = buf.f_flag;
return 0;
}
//***********************************************
// mount namespace
// - functions need fully resolved paths
//***********************************************
// mount a writable tmpfs on directory
void fs_tmpfs(const char *dir, unsigned check_owner) {
assert(dir);
// get a file descriptor for dir, fails if there is any symlink
int fd = safe_fd(dir, O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
if (fd == -1)
errExit("safe_fd");
struct stat s;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (check_owner && s.st_uid != getuid()) {
fwarning("no tmpfs mounted on %s: not owned by the current user\n", dir);
close(fd);
return;
}
if (arg_debug)
printf("Mounting tmpfs on %s\n", dir);
// preserve ownership, mode
char *options;
if (asprintf(&options, "mode=%o,uid=%u,gid=%u", s.st_mode & 07777, s.st_uid, s.st_gid) == -1)
errExit("asprintf");
// preserve mount flags, but remove read-only flag
struct statvfs buf;
if (fstatvfs(fd, &buf) == -1)
errExit("fstatvfs");
unsigned long flags = buf.f_flag & ~(MS_RDONLY|MS_BIND);
// mount via the symbolic link in /proc/self/fd
char *proc;
if (asprintf(&proc, "/proc/self/fd/%d", fd) == -1)
errExit("asprintf");
if (mount("tmpfs", proc, "tmpfs", flags|MS_NOSUID|MS_NODEV, options) < 0)
errExit("mounting tmpfs");
// check the last mount operation
MountData *mdata = get_last_mount();
if (strcmp(mdata->fstype, "tmpfs") != 0 || strcmp(mdata->dir, dir) != 0)
errLogExit("invalid tmpfs mount");
fs_logger2("tmpfs", dir);
free(options);
free(proc);
close(fd);
}
void fs_remount(const char *dir, OPERATION op, unsigned check_mnt) {
assert(dir);
// check directory exists
struct stat s;
int rv = stat(dir, &s);
if (rv == 0) {
unsigned long flags = 0;
if (get_mount_flags(dir, &flags) != 0) {
fwarning("cannot remount %s\n", dir);
return;
}
if (op == MOUNT_RDWR) {
// allow only user owned directories, except the user is root
if (getuid() != 0 && s.st_uid != getuid()) {
fwarning("you are not allowed to change %s to read-write\n", dir);
return;
}
if ((flags & MS_RDONLY) == 0)
return;
flags &= ~MS_RDONLY;
}
else if (op == MOUNT_NOEXEC) {
if ((flags & (MS_NOEXEC|MS_NODEV|MS_NOSUID)) == (MS_NOEXEC|MS_NODEV|MS_NOSUID))
return;
flags |= MS_NOEXEC|MS_NODEV|MS_NOSUID;
}
else if (op == MOUNT_READONLY) {
if ((flags & MS_RDONLY) == MS_RDONLY)
return;
flags |= MS_RDONLY;
}
else
assert(0);
if (arg_debug)
printf("Mounting %s %s\n", opstr[op], dir);
// mount --bind /bin /bin
// mount --bind -o remount,rw /bin
if (mount(dir, dir, NULL, MS_BIND|MS_REC, NULL) < 0 ||
mount(NULL, dir, NULL, flags|MS_BIND|MS_REMOUNT, NULL) < 0)
errExit("remounting");
if (check_mnt) {
// run a sanity check on /proc/self/mountinfo
MountData *mptr = get_last_mount();
size_t len = strlen(dir);
if (strncmp(mptr->dir, dir, len) != 0 ||
(*(mptr->dir + len) != '\0' && *(mptr->dir + len) != '/'))
errLogExit("invalid %s mount", opstr[op]);
}
fs_logger2(opstr[op], dir);
}
}
void fs_remount_rec(const char *dir, OPERATION op, unsigned check_mnt) {
assert(dir);
// get mount point of the directory
int mountid = get_mount_id(dir);
if (mountid == -1)
return;
if (mountid == -2) {
// falling back to a simple remount on old kernels
static int mount_warning = 0;
if (!mount_warning) {
fwarning("read-only, read-write and noexec options are not applied recursively\n");
mount_warning = 1;
}
fs_remount(dir, op, check_mnt);
return;
}
// build array with all mount points that need to get remounted
char **arr = build_mount_array(mountid, dir);
assert(arr);
// remount
char **tmp = arr;
while (*tmp) {
fs_remount(*tmp, op, check_mnt);
free(*tmp++);
}
free(arr);
}
// Disable /mnt, /media, /run/mount and /run/media access
void fs_mnt(const int enforce) {
if (enforce) {
// disable-mnt set in firejail.config
// overriding with noblacklist is not possible in this case
disable_file(BLACKLIST_FILE, "/mnt");
disable_file(BLACKLIST_FILE, "/media");
disable_file(BLACKLIST_FILE, "/run/mount");
disable_file(BLACKLIST_FILE, "/run/media");
}
else {
EUID_USER();
profile_add("blacklist /mnt");
profile_add("blacklist /media");
profile_add("blacklist /run/mount");
profile_add("blacklist /run/media");
EUID_ROOT();
}
}
// mount /proc and /sys directories
void fs_proc_sys_dev_boot(void) {
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 ||
mount(NULL, "/proc/sys", NULL, MS_BIND | MS_REMOUNT | MS_RDONLY | MS_NOSUID | MS_NOEXEC | MS_NODEV | 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 this is an overlay, don't try to unmount, just mount a new sysfs
if (!arg_overlay) {
if (umount2("/sys", MNT_DETACH) < 0 && !cfg.chrootdir)
fwarning("failed to unmount /sys\n");
}
if (mount("sysfs", "/sys", "sysfs", MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REC, NULL) < 0)
fwarning("failed to mount /sys\n");
else
fs_logger("remount /sys");
disable_file(BLACKLIST_FILE, "/sys/firmware");
disable_file(BLACKLIST_FILE, "/sys/hypervisor");
{ // allow user access to some directories in /sys/ by specifying 'noblacklist' option
EUID_USER();
profile_add("blacklist /sys/fs");
profile_add("blacklist /sys/module");
EUID_ROOT();
}
disable_file(BLACKLIST_FILE, "/sys/power");
disable_file(BLACKLIST_FILE, "/sys/kernel/debug");
disable_file(BLACKLIST_FILE, "/sys/kernel/vmcoreinfo");
disable_file(BLACKLIST_FILE, "/sys/kernel/uevent_helper");
// various /proc/sys files
disable_file(BLACKLIST_FILE, "/proc/sys/security");
disable_file(BLACKLIST_FILE, "/proc/sys/efi/vars");
disable_file(BLACKLIST_FILE, "/proc/sys/fs/binfmt_misc");
disable_file(BLACKLIST_FILE, "/proc/sys/kernel/core_pattern");
disable_file(BLACKLIST_FILE, "/proc/sys/kernel/modprobe");
disable_file(BLACKLIST_FILE, "/proc/sysrq-trigger");
disable_file(BLACKLIST_FILE, "/proc/sys/kernel/hotplug");
disable_file(BLACKLIST_FILE, "/proc/sys/vm/panic_on_oom");
// various /proc files
disable_file(BLACKLIST_FILE, "/proc/irq");
disable_file(BLACKLIST_FILE, "/proc/bus");
disable_file(BLACKLIST_FILE, "/proc/config.gz");
disable_file(BLACKLIST_FILE, "/proc/sched_debug");
disable_file(BLACKLIST_FILE, "/proc/timer_list");
disable_file(BLACKLIST_FILE, "/proc/timer_stats");
disable_file(BLACKLIST_FILE, "/proc/kcore");
disable_file(BLACKLIST_FILE, "/proc/kallsyms");
disable_file(BLACKLIST_FILE, "/proc/mem");
disable_file(BLACKLIST_FILE, "/proc/kmem");
// remove kernel symbol information
if (!arg_allow_debuggers) {
disable_file(BLACKLIST_FILE, "/usr/src/linux");
disable_file(BLACKLIST_FILE, "/lib/modules");
disable_file(BLACKLIST_FILE, "/usr/lib/debug");
disable_file(BLACKLIST_FILE, "/boot");
}
// disable /selinux
disable_file(BLACKLIST_FILE, "/selinux");
// disable /dev/port
disable_file(BLACKLIST_FILE, "/dev/port");
// disable various ipc sockets in /run/user
if (!arg_writable_run_user) {
struct stat s;
char *fname;
if (asprintf(&fname, "/run/user/%d", getuid()) == -1)
errExit("asprintf");
if (is_dir(fname)) { // older distros don't have this directory
// disable /run/user/{uid}/gnupg
char *fnamegpg;
if (asprintf(&fnamegpg, "/run/user/%d/gnupg", getuid()) == -1)
errExit("asprintf");
if (create_empty_dir_as_user(fnamegpg, 0700))
fs_logger2("create", fnamegpg);
if (stat(fnamegpg, &s) == 0)
disable_file(BLACKLIST_FILE, fnamegpg);
free(fnamegpg);
// disable /run/user/{uid}/systemd
char *fnamesysd;
if (asprintf(&fnamesysd, "/run/user/%d/systemd", getuid()) == -1)
errExit("asprintf");
if (create_empty_dir_as_user(fnamesysd, 0755))
fs_logger2("create", fnamesysd);
if (stat(fnamesysd, &s) == 0)
disable_file(BLACKLIST_FILE, fnamesysd);
free(fnamesysd);
}
free(fname);
}
if (getuid() != 0) {
// disable /dev/kmsg and /proc/kmsg
disable_file(BLACKLIST_FILE, "/dev/kmsg");
disable_file(BLACKLIST_FILE, "/proc/kmsg");
}
}
// disable firejail configuration in /etc/firejail and in ~/.config/firejail
static void disable_config(void) {
struct stat s;
char *fname;
if (asprintf(&fname, "%s/.config/firejail", cfg.homedir) == -1)
errExit("asprintf");
if (stat(fname, &s) == 0)
disable_file(BLACKLIST_FILE, fname);
free(fname);
// disable run time information
if (stat(RUN_FIREJAIL_NETWORK_DIR, &s) == 0)
disable_file(BLACKLIST_FILE, RUN_FIREJAIL_NETWORK_DIR);
if (stat(RUN_FIREJAIL_BANDWIDTH_DIR, &s) == 0)
disable_file(BLACKLIST_FILE, RUN_FIREJAIL_BANDWIDTH_DIR);
if (stat(RUN_FIREJAIL_NAME_DIR, &s) == 0)
disable_file(BLACKLIST_FILE, RUN_FIREJAIL_NAME_DIR);
if (stat(RUN_FIREJAIL_X11_DIR, &s) == 0)
disable_file(BLACKLIST_FILE, RUN_FIREJAIL_X11_DIR);
}
// build a basic read-only filesystem
// top level directories could be links, run no after-mount checks
void fs_basic_fs(void) {
uid_t uid = getuid();
if (arg_debug)
printf("Basic read-only filesystem:\n");
if (!arg_writable_etc) {
fs_remount("/etc", MOUNT_READONLY, 0);
if (uid)
fs_remount("/etc", MOUNT_NOEXEC, 0);
}
if (!arg_writable_var) {
fs_remount("/var", MOUNT_READONLY, 0);
if (uid)
fs_remount("/var", MOUNT_NOEXEC, 0);
}
fs_remount("/bin", MOUNT_READONLY, 0);
fs_remount("/sbin", MOUNT_READONLY, 0);
fs_remount("/lib", MOUNT_READONLY, 0);
fs_remount("/lib64", MOUNT_READONLY, 0);
fs_remount("/lib32", MOUNT_READONLY, 0);
fs_remount("/libx32", MOUNT_READONLY, 0);
fs_remount("/usr", MOUNT_READONLY, 0);
// update /var directory in order to support multiple sandboxes running on the same root directory
fs_var_lock();
if (!arg_keep_var_tmp)
fs_var_tmp();
if (!arg_writable_var_log)
fs_var_log();
else
fs_remount("/var/log", MOUNT_RDWR, 0);
fs_var_lib();
fs_var_cache();
fs_var_utmp();
fs_machineid();
// don't leak user information
restrict_users();
// when starting as root, firejail config is not disabled;
if (uid)
disable_config();
}
#ifdef HAVE_OVERLAYFS
char *fs_check_overlay_dir(const char *subdirname, int allow_reuse) {
assert(subdirname);
struct stat s;
char *dirname;
if (asprintf(&dirname, "%s/.firejail", cfg.homedir) == -1)
errExit("asprintf");
// check if ~/.firejail already exists
if (lstat(dirname, &s) == 0) {
if (!S_ISDIR(s.st_mode)) {
if (S_ISLNK(s.st_mode))
fprintf(stderr, "Error: %s is a symbolic link\n", dirname);
else
fprintf(stderr, "Error: %s is not a directory\n", dirname);
exit(1);
}
if (s.st_uid != getuid()) {
fprintf(stderr, "Error: %s is not owned by the current user\n", dirname);
exit(1);
}
}
else {
// create ~/.firejail directory
create_empty_dir_as_user(dirname, 0700);
if (stat(dirname, &s) == -1) {
fprintf(stderr, "Error: cannot create directory %s\n", dirname);
exit(1);
}
}
free(dirname);
// check overlay directory
if (asprintf(&dirname, "%s/.firejail/%s", cfg.homedir, subdirname) == -1)
errExit("asprintf");
if (lstat(dirname, &s) == 0) {
if (!S_ISDIR(s.st_mode)) {
if (S_ISLNK(s.st_mode))
fprintf(stderr, "Error: %s is a symbolic link\n", dirname);
else
fprintf(stderr, "Error: %s is not a directory\n", dirname);
exit(1);
}
if (s.st_uid != 0) {
fprintf(stderr, "Error: overlay directory %s is not owned by the root user\n", dirname);
exit(1);
}
if (allow_reuse == 0) {
fprintf(stderr, "Error: overlay directory exists, but reuse is not allowed\n");
exit(1);
}
}
return dirname;
}
// 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, but consider keeping /dev/shm; add locking mechanism for overlay-clean
#include <sys/utsname.h>
void fs_overlayfs(void) {
struct stat s;
// 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;
char *oroot = RUN_OVERLAY_ROOT;
mkdir_attr(oroot, 0755, 0, 0);
// set base for working and diff directories
char *basedir = RUN_MNT_DIR;
int basefd = -1;
if (arg_overlay_keep) {
basedir = cfg.overlay_dir;
assert(basedir);
// get a file descriptor for ~/.firejail, fails if there is any symlink
char *firejail;
if (asprintf(&firejail, "%s/.firejail", cfg.homedir) == -1)
errExit("asprintf");
int fd = safe_fd(firejail, O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
if (fd == -1)
errExit("safe_fd");
free(firejail);
// create basedir if it doesn't exist
// the new directory will be owned by root
const char *dirname = gnu_basename(basedir);
if (mkdirat(fd, dirname, 0755) == -1 && errno != EEXIST) {
perror("mkdir");
fprintf(stderr, "Error: cannot create overlay directory %s\n", basedir);
exit(1);
}
// open basedir
basefd = openat(fd, dirname, O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
close(fd);
}
else {
basefd = open(basedir, O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
}
if (basefd == -1) {
perror("open");
fprintf(stderr, "Error: cannot open overlay directory %s\n", basedir);
exit(1);
}
// confirm once more base is owned by root
if (fstat(basefd, &s) == -1)
errExit("fstat");
if (s.st_uid != 0) {
fprintf(stderr, "Error: overlay directory %s is not owned by the root user\n", basedir);
exit(1);
}
// confirm permissions of base are 0755
if (((S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) & s.st_mode) != (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
fprintf(stderr, "Error: invalid permissions on overlay directory %s\n", basedir);
exit(1);
}
// create diff and work directories inside base
// no need to check arg_overlay_reuse
char *odiff;
if (asprintf(&odiff, "%s/odiff", basedir) == -1)
errExit("asprintf");
// the new directory will be owned by root
if (mkdirat(basefd, "odiff", 0755) == -1 && errno != EEXIST) {
perror("mkdir");
fprintf(stderr, "Error: cannot create overlay directory %s\n", odiff);
exit(1);
}
ASSERT_PERMS(odiff, 0, 0, 0755);
char *owork;
if (asprintf(&owork, "%s/owork", basedir) == -1)
errExit("asprintf");
// the new directory will be owned by root
if (mkdirat(basefd, "owork", 0755) == -1 && errno != EEXIST) {
perror("mkdir");
fprintf(stderr, "Error: cannot create overlay directory %s\n", owork);
exit(1);
}
ASSERT_PERMS(owork, 0, 0, 0755);
// 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(cfg.homedir, &s) == 0 && stat(overlayhome, &s) == -1) {
// no need to check arg_overlay_reuse
if (asprintf(&hdiff, "%s/hdiff", basedir) == -1)
errExit("asprintf");
// the new directory will be owned by root
if (mkdirat(basefd, "hdiff", 0755) == -1 && errno != EEXIST) {
perror("mkdir");
fprintf(stderr, "Error: cannot create overlay directory %s\n", hdiff);
exit(1);
}
ASSERT_PERMS(hdiff, 0, 0, 0755);
// no need to check arg_overlay_reuse
if (asprintf(&hwork, "%s/hwork", basedir) == -1)
errExit("asprintf");
// the new directory will be owned by root
if (mkdirat(basefd, "hwork", 0755) == -1 && errno != EEXIST) {
perror("mkdir");
fprintf(stderr, "Error: cannot create overlay directory %s\n", hwork);
exit(1);
}
ASSERT_PERMS(hwork, 0, 0, 0755);
// no homedir in overlay so now mount another overlay for /home
if (asprintf(&hroot, "%s/home", oroot) == -1)
errExit("asprintf");
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);
free(hroot);
free(hdiff);
free(hwork);
} // stat(overlayhome)
free(overlayhome);
}
// issue #263 end code
//***************************
}
fmessage("OverlayFS configured in %s directory\n", basedir);
close(basefd);
// /dev, /run and /tmp are not covered by the overlay
// 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");
// mount-bind tmp directory
if (arg_debug)
printf("Mounting /tmp\n");
char *tmp;
if (asprintf(&tmp, "%s/tmp", oroot) == -1)
errExit("asprintf");
if (mount("/tmp", tmp, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting /tmp");
fs_logger("whitelist /tmp");
// chroot in the new filesystem
#ifdef HAVE_GCOV
__gcov_flush();
#endif
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();
if (!arg_keep_var_tmp)
fs_var_tmp();
if (!arg_writable_var_log)
fs_var_log();
fs_var_lib();
fs_var_cache();
fs_var_utmp();
fs_machineid();
// don't leak user information
restrict_users();
// when starting as root, firejail config is not disabled;
if (getuid() != 0)
disable_config();
// cleanup and exit
free(option);
free(odiff);
free(owork);
free(dev);
free(run);
free(tmp);
}
#endif
#ifdef HAVE_CHROOT
// exit if error
void fs_check_chroot_dir(const char *rootdir) {
EUID_ASSERT();
assert(rootdir);
char *dir = EMPTY_STRING;
struct stat s;
char *overlay;
if (asprintf(&overlay, "%s/.firejail", cfg.homedir) == -1)
errExit("asprintf");
if (strncmp(rootdir, overlay, strlen(overlay)) == 0) {
fprintf(stderr, "Error: invalid chroot directory: no directories in %s are allowed\n", overlay);
exit(1);
}
free(overlay);
// fails if there is any symlink or if rootdir is not a directory
int parentfd = safe_fd(rootdir, O_PATH|O_DIRECTORY|O_NOFOLLOW|O_CLOEXEC);
if (parentfd == -1) {
fprintf(stderr, "Error: invalid chroot directory %s\n", rootdir);
exit(1);
}
// rootdir has to be owned by root and is not allowed to be generally writable,
// this also excludes /tmp, /var/tmp and such
if (fstat(parentfd, &s) == -1)
errExit("fstat");
if (s.st_uid != 0) {
fprintf(stderr, "Error: chroot directory should be owned by root\n");
exit(1);
}
if (((S_IWGRP|S_IWOTH) & s.st_mode) != 0) {
fprintf(stderr, "Error: only root user should be given write permission on chroot directory\n");
exit(1);
}
// check /dev
dir = "dev";
int fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
close(fd);
// check /var/tmp
dir = "var/tmp";
fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
close(fd);
// check /proc
dir = "proc";
fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
close(fd);
// check /tmp
dir = "tmp";
fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
close(fd);
// check /etc
dir = "etc";
fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
if (((S_IWGRP|S_IWOTH) & s.st_mode) != 0) {
fprintf(stderr, "Error: only root user should be given write permission on chroot /etc\n");
exit(1);
}
close(fd);
// there should be no checking on <chrootdir>/etc/resolv.conf
// the file is replaced with the real /etc/resolv.conf anyway
#if 0
if (asprintf(&name, "%s/etc/resolv.conf", rootdir) == -1)
errExit("asprintf");
if (stat(name, &s) == 0) {
if (s.st_uid != 0) {
fprintf(stderr, "Error: chroot /etc/resolv.conf should be owned by root\n");
exit(1);
}
}
else {
fprintf(stderr, "Error: chroot /etc/resolv.conf not found\n");
exit(1);
}
// on Arch /etc/resolv.conf could be a symlink to /run/systemd/resolve/resolv.conf
// on Ubuntu 17.04 /etc/resolv.conf could be a symlink to /run/resolveconf/resolv.conf
if (is_link(name)) {
// check the link points in chroot
char *rname = realpath(name, NULL);
if (!rname || strncmp(rname, rootdir, strlen(rootdir)) != 0) {
fprintf(stderr, "Error: chroot /etc/resolv.conf is pointing outside chroot\n");
exit(1);
}
}
free(name);
#endif
// check x11 socket directory
if (getenv("FIREJAIL_X11")) {
dir = "tmp/.X11-unix";
fd = openat(parentfd, dir, O_PATH|O_CLOEXEC);
if (fd == -1)
goto error1;
if (fstat(fd, &s) == -1)
errExit("fstat");
if (!S_ISDIR(s.st_mode) || s.st_uid != 0)
goto error2;
close(fd);
}
close(parentfd);
return;
error1:
if (errno == ENOENT)
fprintf(stderr, "Error: cannot find /%s in chroot directory\n", dir);
else {
perror("open");
fprintf(stderr, "Error: cannot open /%s in chroot directory\n", dir);
}
exit(1);
error2:
fprintf(stderr, "Error: chroot /%s should be a directory owned by root\n", dir);
exit(1);
}
// 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
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");
free(newdev);
// x11
if (getenv("FIREJAIL_X11")) {
char *newx11;
if (asprintf(&newx11, "%s/tmp/.X11-unix", rootdir) == -1)
errExit("asprintf");
if (arg_debug)
printf("Mounting /tmp/.X11-unix on %s\n", newx11);
if (mount("/tmp/.X11-unix", newx11, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting /tmp/.X11-unix");
free(newx11);
}
// some older distros don't have a /run directory
// create one by default
char *rundir;
if (asprintf(&rundir, "%s/run", rootdir) == -1)
errExit("asprintf");
struct stat s;
if (lstat(rundir, &s) == 0) {
if (S_ISLNK(s.st_mode)) {
fprintf(stderr, "Error: chroot /run is a symbolic link\n");
exit(1);
}
if (!S_ISDIR(s.st_mode) || s.st_uid != 0) {
fprintf(stderr, "Error: chroot /run should be a directory owned by root\n");
exit(1);
}
if (((S_IWGRP|S_IWOTH) & s.st_mode) != 0) {
fprintf(stderr, "Error: only root user should be given write permission on chroot /run\n");
exit(1);
}
}
else {
// several sandboxes could race to create /run
if (mkdir(rundir, 0755) == -1 && errno != EEXIST)
errExit("mkdir");
ASSERT_PERMS(rundir, 0, 0, 0755);
}
free(rundir);
// create /run/firejail directory in chroot
if (asprintf(&rundir, "%s/run/firejail", rootdir) == -1)
errExit("asprintf");
if (mkdir(rundir, 0755) == -1 && errno != EEXIST)
errExit("mkdir");
ASSERT_PERMS(rundir, 0, 0, 0755);
free(rundir);
// create /run/firejail/lib directory in chroot and mount it
if (asprintf(&rundir, "%s%s", rootdir, RUN_FIREJAIL_LIB_DIR) == -1)
errExit("asprintf");
if (mkdir(rundir, 0755) == -1 && errno != EEXIST)
errExit("mkdir");
ASSERT_PERMS(rundir, 0, 0, 0755);
if (mount(RUN_FIREJAIL_LIB_DIR, rundir, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mount bind");
free(rundir);
// create /run/firejail/mnt directory in chroot and mount the current one
if (asprintf(&rundir, "%s%s", rootdir, RUN_MNT_DIR) == -1)
errExit("asprintf");
if (mkdir(rundir, 0755) == -1 && errno != EEXIST)
errExit("mkdir");
ASSERT_PERMS(rundir, 0, 0, 0755);
if (mount(RUN_MNT_DIR, rundir, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mount bind");
free(rundir);
// copy /etc/resolv.conf in chroot directory
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);
unlink(fname);
if (copy_file("/etc/resolv.conf", fname, 0, 0, 0644) == -1) // root needed
fwarning("/etc/resolv.conf not initialized\n");
free(fname);
// chroot into the new directory
#ifdef HAVE_GCOV
__gcov_flush();
#endif
// mount the chroot dir on top of /run/firejail/mnt/oroot in order to reuse the apparmor rules for overlay
// and chroot into this new directory
if (arg_debug)
printf("Chrooting into %s\n", rootdir);
char *oroot = RUN_OVERLAY_ROOT;
if (mkdir(oroot, 0755) == -1)
errExit("mkdir");
if (mount(rootdir, oroot, NULL, MS_BIND|MS_REC, NULL) < 0)
errExit("mounting rootdir oroot");
if (chroot(oroot) < 0)
errExit("chroot");
// create all other /run/firejail files and directories
preproc_build_firejail_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();
if (!arg_keep_var_tmp)
fs_var_tmp();
if (!arg_writable_var_log)
fs_var_log();
fs_var_lib();
fs_var_cache();
fs_var_utmp();
fs_machineid();
// don't leak user information
restrict_users();
// when starting as root, firejail config is not disabled;
if (getuid() != 0)
disable_config();
}
#endif
// this function is called from sandbox.c before blacklist/whitelist functions
void fs_private_tmp(void) {
// check XAUTHORITY file, KDE keeps it under /tmp
char *xauth = getenv("XAUTHORITY");
if (xauth) {
char *rp = realpath(xauth, NULL);
if (rp && strncmp(rp, "/tmp/", 5) == 0) {
char *cmd;
if (asprintf(&cmd, "whitelist %s", rp) == -1)
errExit("asprintf");
profile_add(cmd); // profile_add does not duplicate the string
}
if (rp)
free(rp);
}
// whitelist x11 directory
profile_add("whitelist /tmp/.X11-unix");
// read-only x11 directory
profile_add("read-only /tmp/.X11-unix");
// whitelist any pulse* file in /tmp directory
// some distros use PulseAudio sockets under /tmp instead of the socket in /urn/user
DIR *dir;
if (!(dir = opendir("/tmp"))) {
// sleep 2 seconds and try again
sleep(2);
if (!(dir = opendir("/tmp"))) {
return;
}
}
struct dirent *entry;
while ((entry = readdir(dir))) {
if (strncmp(entry->d_name, "pulse-", 6) == 0) {
char *cmd;
if (asprintf(&cmd, "whitelist /tmp/%s", entry->d_name) == -1)
errExit("asprintf");
profile_add(cmd); // profile_add does not duplicate the string
}
}
closedir(dir);
}
// this function is called from sandbox.c before blacklist/whitelist functions
void fs_private_cache(void) {
char *cache;
if (asprintf(&cache, "%s/.cache", cfg.homedir) == -1)
errExit("asprintf");
// check if ~/.cache is a valid destination
struct stat s;
if (lstat(cache, &s) == -1) {
fwarning("cannot find %s, tmpfs not mounted\n", cache);
free(cache);
return;
}
if (!S_ISDIR(s.st_mode)) {
if (S_ISLNK(s.st_mode))
fwarning("%s is a symbolic link, tmpfs not mounted\n", cache);
else
fwarning("%s is not a directory; cannot mount a tmpfs on top of it\n", cache);
free(cache);
return;
}
// do the mount
fs_tmpfs(cache, getuid()); // check ownership of ~/.cache
free(cache);
}
|