-
Notifications
You must be signed in to change notification settings - Fork 557
/
main.c
3274 lines (2987 loc) · 89.7 KB
/
main.c
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
/*
* Copyright (C) 2014-2022 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 "../include/pid.h"
#include "../include/firejail_user.h"
#include "../include/gcov_wrapper.h"
#include "../include/syscall.h"
#include "../include/seccomp.h"
#define _GNU_SOURCE
#include <sys/utsname.h>
#include <sched.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <errno.h>
#include <limits.h>
#include <sys/file.h>
#include <sys/prctl.h>
#include <signal.h>
#include <time.h>
#include <net/if.h>
#include <sys/utsname.h>
#include <fcntl.h>
#ifndef O_PATH
#define O_PATH 010000000
#endif
#ifdef __ia64__
/* clone(2) has a different interface on ia64, as it needs to know
the size of the stack */
int __clone2(int (*fn)(void *),
void *child_stack_base, size_t stack_size,
int flags, void *arg, ...
/* pid_t *ptid, struct user_desc *tls, pid_t *ctid */ );
#endif
uid_t firejail_uid = 0;
gid_t firejail_gid = 0;
#define STACK_SIZE (1024 * 1024)
#define STACK_ALIGNMENT 16
static char child_stack[STACK_SIZE] __attribute__((aligned(STACK_ALIGNMENT))); // space for child's stack
Config cfg; // configuration
int arg_private = 0; // mount private /home and /tmp directoryu
int arg_private_cache = 0; // mount private home/.cache
int arg_debug = 0; // print debug messages
int arg_debug_blacklists = 0; // print debug messages for blacklists
int arg_debug_whitelists = 0; // print debug messages for whitelists
int arg_debug_private_lib = 0; // print debug messages for private-lib
int arg_nonetwork = 0; // --net=none
int arg_command = 0; // -c
int arg_overlay = 0; // overlay option
int arg_overlay_keep = 0; // place overlay diff in a known directory
int arg_overlay_reuse = 0; // allow the reuse of overlays
int arg_seccomp = 0; // enable default seccomp filter
int arg_seccomp32 = 0; // enable default seccomp filter for 32 bit arch
int arg_seccomp_postexec = 0; // need postexec ld.preload library?
int arg_seccomp_block_secondary = 0; // block any secondary architectures
int arg_seccomp_error_action = 0;
int arg_caps_default_filter = 0; // enable default capabilities filter
int arg_caps_drop = 0; // drop list
int arg_caps_drop_all = 0; // drop all capabilities
int arg_caps_keep = 0; // keep list
char *arg_caps_list = NULL; // optional caps list
int arg_trace = 0; // syscall tracing support
char *arg_tracefile = NULL; // syscall tracing file
int arg_tracelog = 0; // blacklist tracing support
int arg_rlimit_cpu = 0; // rlimit max cpu time
int arg_rlimit_nofile = 0; // rlimit nofile
int arg_rlimit_nproc = 0; // rlimit nproc
int arg_rlimit_fsize = 0; // rlimit fsize
int arg_rlimit_sigpending = 0; // rlimit fsize
int arg_rlimit_as = 0; // rlimit as
int arg_nogroups = 0; // disable supplementary groups
int arg_nonewprivs = 0; // set the NO_NEW_PRIVS prctl
int arg_noroot = 0; // create a new user namespace and disable root user
int arg_netfilter; // enable netfilter
int arg_netfilter6; // enable netfilter6
char *arg_netfilter_file = NULL; // netfilter file
char *arg_netfilter6_file = NULL; // netfilter6 file
char *arg_netns = NULL; // "ip netns"-created network namespace to use
int arg_doubledash = 0; // double dash
int arg_shell_none = 0; // run the program directly without a shell
int arg_private_dev = 0; // private dev directory
int arg_keep_dev_shm = 0; // preserve /dev/shm
int arg_private_etc = 0; // private etc directory
int arg_private_opt = 0; // private opt directory
int arg_private_srv = 0; // private srv directory
int arg_private_bin = 0; // private bin directory
int arg_private_tmp = 0; // private tmp directory
int arg_private_lib = 0; // private lib directory
int arg_private_cwd = 0; // private working directory
int arg_scan = 0; // arp-scan all interfaces
int arg_whitelist = 0; // whitelist command
int arg_nosound = 0; // disable sound
int arg_novideo = 0; //disable video devices in /dev
int arg_no3d; // disable 3d hardware acceleration
int arg_noprinters = 0; // disable printers
int arg_quiet = 0; // no output for scripting
int arg_join_network = 0; // join only the network namespace
int arg_join_filesystem = 0; // join only the mount namespace
int arg_nice = 0; // nice value configured
int arg_ipc = 0; // enable ipc namespace
int arg_writable_etc = 0; // writable etc
int arg_keep_config_pulse = 0; // disable automatic ~/.config/pulse init
int arg_writable_var = 0; // writable var
int arg_keep_var_tmp = 0; // don't overwrite /var/tmp
int arg_writable_run_user = 0; // writable /run/user
int arg_writable_var_log = 0; // writable /var/log
int arg_appimage = 0; // appimage
int arg_apparmor = 0; // apparmor
int arg_allow_debuggers = 0; // allow debuggers
int arg_x11_block = 0; // block X11
int arg_x11_xorg = 0; // use X11 security extension
int arg_allusers = 0; // all user home directories visible
int arg_machineid = 0; // spoof /etc/machine-id
int arg_allow_private_blacklist = 0; // blacklist things in private directories
int arg_disable_mnt = 0; // disable /mnt and /media
int arg_noprofile = 0; // use default.profile if none other found/specified
int arg_memory_deny_write_execute = 0; // block writable and executable memory
int arg_notv = 0; // --notv
int arg_nodvd = 0; // --nodvd
int arg_nou2f = 0; // --nou2f
int arg_noinput = 0; // --noinput
int arg_deterministic_exit_code = 0; // always exit with first child's exit status
int arg_deterministic_shutdown = 0; // shut down the sandbox if first child dies
DbusPolicy arg_dbus_user = DBUS_POLICY_ALLOW; // --dbus-user
DbusPolicy arg_dbus_system = DBUS_POLICY_ALLOW; // --dbus-system
const char *arg_dbus_log_file = NULL;
int arg_dbus_log_user = 0;
int arg_dbus_log_system = 0;
int login_shell = 0;
int parent_to_child_fds[2];
int child_to_parent_fds[2];
char *fullargv[MAX_ARGS]; // expanded argv for restricted shell
int fullargc = 0;
static pid_t child = 0;
pid_t sandbox_pid;
mode_t orig_umask = 022;
static void clear_atexit(void) {
EUID_ROOT();
delete_run_files(getpid());
}
static void myexit(int rv) {
logmsg("exiting...");
if (!arg_command)
fmessage("\nParent is shutting down, bye...\n");
// delete sandbox files in shared memory
#ifdef HAVE_DBUSPROXY
dbus_proxy_stop();
#endif
EUID_ROOT();
delete_run_files(sandbox_pid);
appimage_clear();
flush_stdin();
exit(rv);
}
static void my_handler(int s) {
fmessage("\nParent received signal %d, shutting down the child process...\n", s);
logsignal(s);
if (waitpid(child, NULL, WNOHANG) == 0) {
// child is pid 1 of a pid namespace:
// signals are not delivered if there is no handler yet
if (has_handler(child, s))
kill(child, s);
else
kill(child, SIGKILL);
waitpid(child, NULL, 0);
}
myexit(128 + s);
}
static void install_handler(void) {
struct sigaction sga;
// block SIGTERM while handling SIGINT
sigemptyset(&sga.sa_mask);
sigaddset(&sga.sa_mask, SIGTERM);
sga.sa_handler = my_handler;
sga.sa_flags = 0;
sigaction(SIGINT, &sga, NULL);
// block SIGINT while handling SIGTERM
sigemptyset(&sga.sa_mask);
sigaddset(&sga.sa_mask, SIGINT);
sga.sa_handler = my_handler;
sga.sa_flags = 0;
sigaction(SIGTERM, &sga, NULL);
}
// init configuration
static void init_cfg(int argc, char **argv) {
EUID_ASSERT();
memset(&cfg, 0, sizeof(cfg));
cfg.original_argv = argv;
cfg.original_argc = argc;
cfg.bridge0.devsandbox = "eth0";
cfg.bridge1.devsandbox = "eth1";
cfg.bridge2.devsandbox = "eth2";
cfg.bridge3.devsandbox = "eth3";
// extract user data
EUID_ROOT(); // rise permissions for grsecurity
struct passwd *pw = getpwuid(getuid());
if (!pw)
errExit("getpwuid");
EUID_USER();
cfg.username = strdup(pw->pw_name);
if (!cfg.username)
errExit("strdup");
// check user database
if (!firejail_user_check(cfg.username)) {
fprintf(stderr, "Error: the user is not allowed to use Firejail.\n"
"Please add the user in %s/firejail.users file,\n"
"either by running \"sudo firecfg\", or by editing the file directly.\n"
"See \"man firejail-users\" for more details.\n\n", SYSCONFDIR);
// attempt to run the program as is
run_symlink(argc, argv, 1);
exit(1);
}
cfg.cwd = getcwd(NULL, 0);
if (!cfg.cwd && errno != ENOENT)
errExit("getcwd");
// build home directory name
if (pw->pw_dir == NULL) {
fprintf(stderr, "Error: user %s doesn't have a user directory assigned\n", cfg.username);
exit(1);
}
check_homedir(pw->pw_dir);
cfg.homedir = clean_pathname(pw->pw_dir);
// initialize random number generator
sandbox_pid = getpid();
time_t t = time(NULL);
srand(t ^ sandbox_pid);
arg_seccomp_error_action = EPERM;
cfg.seccomp_error_action = "EPERM";
}
static void fix_single_std_fd(int fd, const char *file, int flags) {
struct stat s;
if (fstat(fd, &s) == -1 && errno == EBADF) {
// something is wrong with fd, probably it is not opened
int nfd = open(file, flags);
if (nfd != fd || fstat(fd, &s) != 0)
_exit(1); // no further attempts to fix the situation
}
}
// glibc does this automatically if Firejail was started by a regular user
// run this for root user and as a fallback
static void fix_std_streams(void) {
fix_single_std_fd(0, "/dev/full", O_RDONLY|O_);
fix_single_std_fd(1, "/dev/null", O_WRONLY|O_);
fix_single_std_fd(2, "/dev/null", O_WRONLY|O_);
}
static void check_network(Bridge *br) {
assert(br);
if (br->macvlan == 0) // for bridge devices check network range or arp-scan and assign address
net_configure_sandbox_ip(br);
else if (br->ipsandbox) { // for macvlan check network range
char *rv = in_netrange(br->ipsandbox, br->ip, br->mask);
if (rv) {
fprintf(stderr, "%s\n", rv);
exit(1);
}
}
}
#ifdef HAVE_USERNS
void check_user_namespace(void) {
EUID_ASSERT();
if (getuid() == 0)
goto errout;
// test user namespaces available in the kernel
struct stat s1;
struct stat s2;
struct stat s3;
if (stat("/proc/self/ns/user", &s1) == 0 &&
stat("/proc/self/uid_map", &s2) == 0 &&
stat("/proc/self/gid_map", &s3) == 0)
arg_noroot = 1;
else
goto errout;
return;
errout:
fwarning("noroot option is not available\n");
arg_noroot = 0;
}
#endif
static void exit_err_feature(const char *feature) {
fprintf(stderr, "Error: %s feature is disabled in Firejail configuration file\n", feature);
exit(1);
}
// run independent commands and exit program
// this function handles command line options such as --version and --help
static void run_cmd_and_exit(int i, int argc, char **argv) {
EUID_ASSERT();
//*************************************
// basic arguments
//*************************************
if (strcmp(argv[i], "--help") == 0 ||
strcmp(argv[i], "-?") == 0) {
usage();
exit(0);
}
else if (strcmp(argv[i], "--version") == 0) {
printf("firejail version %s\n", VERSION);
printf("\n");
print_compiletime_support();
printf("\n");
exit(0);
}
#ifdef HAVE_OVERLAYFS
else if (strcmp(argv[i], "--overlay-clean") == 0) {
if (checkcfg(CFG_OVERLAYFS)) {
if (remove_overlay_directory()) {
fprintf(stderr, "Error: cannot remove overlay directory\n");
exit(1);
}
}
else
exit_err_feature("overlayfs");
exit(0);
}
#endif
#ifdef HAVE_X11
else if (strcmp(argv[i], "--x11") == 0) {
if (checkcfg(CFG_X11)) {
x11_start(argc, argv);
exit(0);
}
else
exit_err_feature("x11");
}
else if (strcmp(argv[i], "--x11=xpra") == 0) {
if (checkcfg(CFG_X11)) {
x11_start_xpra(argc, argv);
exit(0);
}
else
exit_err_feature("x11");
}
else if (strcmp(argv[i], "--x11=xephyr") == 0) {
if (checkcfg(CFG_X11)) {
x11_start_xephyr(argc, argv);
exit(0);
}
else
exit_err_feature("x11");
}
else if (strcmp(argv[i], "--x11=xvfb") == 0) {
if (checkcfg(CFG_X11)) {
x11_start_xvfb(argc, argv);
exit(0);
}
else
exit_err_feature("x11");
}
#endif
#ifdef HAVE_NETWORK
else if (strncmp(argv[i], "--nettrace=", 11) == 0) {
pid_t pid = require_pid(argv[i] + 11);
netfilter_trace(pid);
}
else if (strncmp(argv[i], "--bandwidth=", 12) == 0) {
if (checkcfg(CFG_NETWORK)) {
logargs(argc, argv);
// extract the command
if ((i + 1) == argc) {
fprintf(stderr, "Error: command expected after --bandwidth option\n");
exit(1);
}
char *cmd = argv[i + 1];
if (strcmp(cmd, "status") && strcmp(cmd, "clear") && strcmp(cmd, "set")) {
fprintf(stderr, "Error: invalid --bandwidth command.\nValid commands: set, clear, status.\n");
exit(1);
}
// extract network name
char *dev = NULL;
int down = 0;
int up = 0;
if (strcmp(cmd, "set") == 0 || strcmp(cmd, "clear") == 0) {
// extract device name
if ((i + 2) == argc) {
fprintf(stderr, "Error: network name expected after --bandwidth %s option\n", cmd);
exit(1);
}
dev = argv[i + 2];
// check device name
if (if_nametoindex(dev) == 0) {
fprintf(stderr, "Error: network device %s not found\n", dev);
exit(1);
}
// extract bandwidth
if (strcmp(cmd, "set") == 0) {
if ((i + 4) >= argc) {
fprintf(stderr, "Error: invalid --bandwidth set command\n");
exit(1);
}
down = atoi(argv[i + 3]);
if (down < 0) {
fprintf(stderr, "Error: invalid download speed\n");
exit(1);
}
up = atoi(argv[i + 4]);
if (up < 0) {
fprintf(stderr, "Error: invalid upload speed\n");
exit(1);
}
}
}
// extract pid or sandbox name
pid_t pid = require_pid(argv[i] + 12);
bandwidth_pid(pid, cmd, dev, down, up);
}
else
exit_err_feature("networking");
exit(0);
}
else if (strncmp(argv[i], "--netfilter.print=", 18) == 0) {
// extract pid or sandbox name
pid_t pid = require_pid(argv[i] + 18);
netfilter_print(pid, 0);
exit(0);
}
else if (strncmp(argv[i], "--netfilter6.print=", 19) == 0) {
// extract pid or sandbox name
pid_t pid = require_pid(argv[i] + 19);
netfilter_print(pid, 1);
exit(0);
}
#endif
//*************************************
// independent commands - the program will exit!
//*************************************
else if (strcmp(argv[i], "--debug-syscalls") == 0) {
if (checkcfg(CFG_SECCOMP)) {
int rv = sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FSECCOMP_MAIN, "debug-syscalls");
exit(rv);
}
else
exit_err_feature("seccomp");
}
else if (strcmp(argv[i], "--debug-syscalls32") == 0) {
if (checkcfg(CFG_SECCOMP)) {
int rv = sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FSECCOMP_MAIN, "debug-syscalls32");
exit(rv);
}
else
exit_err_feature("seccomp");
}
else if (strcmp(argv[i], "--debug-errnos") == 0) {
if (checkcfg(CFG_SECCOMP)) {
int rv = sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FSECCOMP_MAIN, "debug-errnos");
exit(rv);
}
else
exit_err_feature("seccomp");
exit(0);
}
else if (strncmp(argv[i], "--seccomp.print=", 16) == 0) {
if (checkcfg(CFG_SECCOMP)) {
// print seccomp filter for a sandbox specified by pid or by name
pid_t pid = require_pid(argv[i] + 16);
seccomp_print_filter(pid);
}
else
exit_err_feature("seccomp");
exit(0);
}
else if (strcmp(argv[i], "--debug-protocols") == 0) {
int rv = sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FSECCOMP_MAIN, "debug-protocols");
exit(rv);
}
else if (strncmp(argv[i], "--protocol.print=", 17) == 0) {
if (checkcfg(CFG_SECCOMP)) {
// print seccomp filter for a sandbox specified by pid or by name
pid_t pid = require_pid(argv[i] + 17);
protocol_print_filter(pid);
}
else
exit_err_feature("seccomp");
exit(0);
}
else if (strncmp(argv[i], "--profile.print=", 16) == 0) {
pid_t pid = require_pid(argv[i] + 16);
// print /run/firejail/profile/<PID> file
char *fname;
if (asprintf(&fname, RUN_FIREJAIL_PROFILE_DIR "/%d", pid) == -1)
errExit("asprintf");
FILE *fp = fopen(fname, "re");
if (!fp) {
fprintf(stderr, "Error: sandbox %s not found\n", argv[i] + 16);
exit(1);
}
#define MAXBUF 4096
char buf[MAXBUF];
if (fgets(buf, MAXBUF, fp))
printf("%s", buf);
fclose(fp);
exit(0);
}
else if (strncmp(argv[i], "--cpu.print=", 12) == 0) {
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 12);
cpu_print_filter(pid);
exit(0);
}
else if (strncmp(argv[i], "--apparmor.print=", 17) == 0) {
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 17);
char *pidstr;
if (asprintf(&pidstr, "%u", pid) == -1)
errExit("asprintf");
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 3, PATH_FIREMON, "--apparmor", pidstr);
free(pidstr);
exit(0);
}
else if (strncmp(argv[i], "--caps.print=", 13) == 0) {
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 13);
caps_print_filter(pid);
exit(0);
}
else if (strncmp(argv[i], "--fs.print=", 11) == 0) {
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 11);
fs_logger_print_log(pid);
exit(0);
}
else if (strncmp(argv[i], "--dns.print=", 12) == 0) {
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 12);
net_dns_print(pid);
exit(0);
}
else if (strcmp(argv[i], "--debug-caps") == 0) {
caps_print();
exit(0);
}
else if (strcmp(argv[i], "--list") == 0) {
if (pid_hidepid())
sbox_run(SBOX_ROOT| SBOX_CAPS_HIDEPID | SBOX_SECCOMP, 2, PATH_FIREMON, "--list");
else
sbox_run(SBOX_USER| SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FIREMON, "--list");
exit(0);
}
else if (strcmp(argv[i], "--tree") == 0) {
if (pid_hidepid())
sbox_run(SBOX_ROOT | SBOX_CAPS_HIDEPID | SBOX_SECCOMP, 2, PATH_FIREMON, "--tree");
else
sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP, 2, PATH_FIREMON, "--tree");
exit(0);
}
else if (strcmp(argv[i], "--top") == 0) {
if (pid_hidepid())
sbox_run(SBOX_ROOT | SBOX_CAPS_HIDEPID | SBOX_SECCOMP | SBOX_ALLOW_STDIN,
2, PATH_FIREMON, "--top");
else
sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP | SBOX_ALLOW_STDIN,
2, PATH_FIREMON, "--top");
exit(0);
}
#ifdef HAVE_NETWORK
else if (strcmp(argv[i], "--netstats") == 0) {
if (checkcfg(CFG_NETWORK)) {
struct stat s;
if (stat("/proc/sys/kernel/grsecurity", &s) == 0 || pid_hidepid())
sbox_run(SBOX_ROOT | SBOX_CAPS_HIDEPID | SBOX_SECCOMP | SBOX_ALLOW_STDIN,
2, PATH_FIREMON, "--netstats");
else
sbox_run(SBOX_USER | SBOX_CAPS_NONE | SBOX_SECCOMP | SBOX_ALLOW_STDIN,
2, PATH_FIREMON, "--netstats");
exit(0);
}
else
exit_err_feature("networking");
}
else if (strncmp(argv[i], "--net.print=", 12) == 0) {
if (checkcfg(CFG_NETWORK)) {
// extract pid or sandbox name
pid_t pid = require_pid(argv[i] + 12);
net_print(pid);
exit(0);
}
else
exit_err_feature("networking");
}
#endif
#ifdef HAVE_FILE_TRANSFER
else if (strncmp(argv[i], "--get=", 6) == 0) {
if (checkcfg(CFG_FILE_TRANSFER)) {
logargs(argc, argv);
if (arg_private_cwd) {
fprintf(stderr, "Error: --get and --private-cwd options are mutually exclusive\n");
exit(1);
}
// verify path
if ((i + 2) != argc) {
fprintf(stderr, "Error: invalid --get option, path expected\n");
exit(1);
}
char *path = argv[i + 1];
invalid_filename(path, 0); // no globbing
if (strstr(path, "..")) {
fprintf(stderr, "Error: invalid file name %s\n", path);
exit(1);
}
// get file
pid_t pid = require_pid(argv[i] + 6);
sandboxfs(SANDBOX_FS_GET, pid, path, NULL);
exit(0);
}
else
exit_err_feature("file transfer");
}
else if (strncmp(argv[i], "--put=", 6) == 0) {
if (checkcfg(CFG_FILE_TRANSFER)) {
logargs(argc, argv);
if (arg_private_cwd) {
fprintf(stderr, "Error: --put and --private-cwd options are mutually exclusive\n");
exit(1);
}
// verify path
if ((i + 3) != argc) {
fprintf(stderr, "Error: invalid --put option, 2 paths expected\n");
exit(1);
}
char *path1 = argv[i + 1];
invalid_filename(path1, 0); // no globbing
if (strstr(path1, "..")) {
fprintf(stderr, "Error: invalid file name %s\n", path1);
exit(1);
}
char *path2 = argv[i + 2];
invalid_filename(path2, 0); // no globbing
if (strstr(path2, "..")) {
fprintf(stderr, "Error: invalid file name %s\n", path2);
exit(1);
}
// get file
pid_t pid = require_pid(argv[i] + 6);
sandboxfs(SANDBOX_FS_PUT, pid, path1, path2);
exit(0);
}
else
exit_err_feature("file transfer");
}
else if (strncmp(argv[i], "--ls=", 5) == 0) {
if (checkcfg(CFG_FILE_TRANSFER)) {
logargs(argc, argv);
if (arg_private_cwd) {
fprintf(stderr, "Error: --ls and --private-cwd options are mutually exclusive\n");
exit(1);
}
// verify path
if ((i + 2) != argc) {
fprintf(stderr, "Error: invalid --ls option, path expected\n");
exit(1);
}
char *path = argv[i + 1];
invalid_filename(path, 0); // no globbing
if (strstr(path, "..")) {
fprintf(stderr, "Error: invalid file name %s\n", path);
exit(1);
}
// list directory contents
if (!arg_debug)
arg_quiet = 1;
pid_t pid = require_pid(argv[i] + 5);
sandboxfs(SANDBOX_FS_LS, pid, path, NULL);
exit(0);
}
else
exit_err_feature("file transfer");
}
else if (strncmp(argv[i], "--cat=", 6) == 0) {
if (checkcfg(CFG_FILE_TRANSFER)) {
logargs(argc, argv);
if (arg_private_cwd) {
fprintf(stderr, "Error: --cat and --private-cwd options are mutually exclusive\n");
exit(1);
}
if ((i + 2) != argc) {
fprintf(stderr, "Error: invalid --cat option, path expected\n");
exit(1);
}
char *path = argv[i + 1];
invalid_filename(path, 0); // no globbing
if (strstr(path, "..")) {
fprintf(stderr, "Error: invalid file name %s\n", path);
exit(1);
}
// write file contents to stdout
if (!arg_debug)
arg_quiet = 1;
pid_t pid = require_pid(argv[i] + 6);
sandboxfs(SANDBOX_FS_CAT, pid, path, NULL);
exit(0);
}
else
exit_err_feature("file transfer");
}
#endif
else if (strncmp(argv[i], "--join=", 7) == 0) {
if (checkcfg(CFG_JOIN) || getuid() == 0) {
logargs(argc, argv);
if (arg_shell_none) {
if (argc <= (i+1)) {
fprintf(stderr, "Error: --shell=none set, but no command specified\n");
exit(1);
}
cfg.original_program_index = i + 1;
}
if (!cfg.shell && !arg_shell_none)
cfg.shell = guess_shell();
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 7);
join(pid, argc, argv, i + 1);
exit(0);
}
else
exit_err_feature("join");
}
else if (strncmp(argv[i], "--join-or-start=", 16) == 0) {
// NOTE: this is first part of option handler,
// sandbox name is set in other part
if (checkcfg(CFG_JOIN) || getuid() == 0) {
logargs(argc, argv);
if (arg_shell_none) {
if (argc <= (i+1)) {
fprintf(stderr, "Error: --shell=none set, but no command specified\n");
exit(1);
}
cfg.original_program_index = i + 1;
}
// try to join by name only
pid_t pid;
if (!read_pid(argv[i] + 16, &pid)) {
if (!cfg.shell && !arg_shell_none)
cfg.shell = guess_shell();
join(pid, argc, argv, i + 1);
exit(0);
}
// if there no such sandbox continue argument processing
}
else
exit_err_feature("join");
}
#ifdef HAVE_NETWORK
else if (strncmp(argv[i], "--join-network=", 15) == 0) {
if (checkcfg(CFG_NETWORK)) {
logargs(argc, argv);
arg_join_network = 1;
if (getuid() != 0) {
fprintf(stderr, "Error: --join-network is only available to root user\n");
exit(1);
}
if (!cfg.shell && !arg_shell_none)
cfg.shell = guess_shell();
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 15);
join(pid, argc, argv, i + 1);
}
else
exit_err_feature("networking");
exit(0);
}
#endif
else if (strncmp(argv[i], "--join-filesystem=", 18) == 0) {
logargs(argc, argv);
arg_join_filesystem = 1;
if (getuid() != 0) {
fprintf(stderr, "Error: --join-filesystem is only available to root user\n");
exit(1);
}
if (!cfg.shell && !arg_shell_none)
cfg.shell = guess_shell();
// join sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 18);
join(pid, argc, argv, i + 1);
exit(0);
}
else if (strncmp(argv[i], "--shutdown=", 11) == 0) {
logargs(argc, argv);
// shutdown sandbox by pid or by name
pid_t pid = require_pid(argv[i] + 11);
shut(pid);
exit(0);
}
}
char *guess_shell(void) {
const char *shell;
char *retval;
shell = env_get("SHELL");
if (shell) {
invalid_filename(shell, 0); // no globbing
if (access(shell, X_OK) == 0 && !is_dir(shell) && strstr(shell, "..") == NULL &&
strcmp(gnu_basename(shell), "firejail") != 0)
goto found;
}
// shells in order of preference
static const char * const shells[] = {"/bin/bash", "/bin/csh", "/usr/bin/zsh", "/bin/sh", "/bin/ash", NULL };
int i = 0;
while (shells[i] != NULL) {
// access call checks as real UID/GID, not as effective UID/GID
if (access(shells[i], X_OK) == 0) {
shell = shells[i];
goto found;
}
i++;
}
return NULL;
found:
retval = strdup(shell);
if (!retval)
errExit("strdup");
return retval;
}
// return argument index
static int check_arg(int argc, char **argv, const char *argument, int strict) {
int i;
int found = 0;
for (i = 1; i < argc; i++) {
if (strict) {
if (strcmp(argv[i], argument) == 0) {
found = i;
break;
}
}
else {
if (strncmp(argv[i], argument, strlen(argument)) == 0) {
found = i;
break;
}
}
// detect end of firejail params
if (strcmp(argv[i], "--") == 0)
break;
if (strncmp(argv[i], "--", 2) != 0)
break;
}
return found;
}
static void run_builder(int argc, char **argv) {
EUID_ASSERT();
(void) argc;
// drop privileges
if (setresgid(-1, getgid(), getgid()) != 0)
errExit("setresgid");
if (setresuid(-1, getuid(), getuid()) != 0)
errExit("setresuid");
if (env_get("LD_PRELOAD") != NULL)
fprintf(stderr, "run_builder: LD_PRELOAD is: '%s'\n", env_get("LD_PRELOAD"));
assert(env_get("LD_PRELOAD") == NULL);
assert(getenv("LD_PRELOAD") == NULL);
umask(orig_umask);
// restore original environment variables
env_apply_all();
argv[0] = LIBDIR "/firejail/fbuilder";
execvp(argv[0], argv);
perror("execvp");
exit(1);
}
void filter_add_errno(int fd, int syscall, int arg, void *ptrarg, bool native) {
(void) fd;
(void) syscall;
(void) arg;
(void) ptrarg;
(void) native;
}
void filter_add_blacklist_override(int fd, int syscall, int arg, void *ptrarg, bool native) {
(void) fd;
(void) syscall;
(void) arg;
(void) ptrarg;
(void) native;
}
static int check_postexec(const char *list) {
char *prelist, *postlist;
if (list && list[0]) {
syscalls_in_list(list, "@default-keep", -1, &prelist, &postlist, true);
if (postlist)
return 1;
}
return 0;
}
//*******************************************
// Main program
//*******************************************
int main(int argc, char **argv, char **envp) {
int i;
int prog_index = -1; // index in argv where the program command starts
int lockfd_network = -1;
int lockfd_directory = -1;
int option_cgroup = 0;
int custom_profile = 0; // custom profile loaded
int arg_caps_cmdline = 0; // caps requested on command line (used to break out of --chroot)
int arg_netlock = 0;
char **ptr;