aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/firejail/sandbox.c3
-rwxr-xr-xtest/environment/deterministic-shutdown.exp5
-rwxr-xr-xtest/environment/environment.sh6
-rwxr-xr-xtest/environment/keep-fd-bad.exp40
-rwxr-xr-xtest/environment/keep-fd.exp223
5 files changed, 275 insertions, 2 deletions
diff --git a/src/firejail/sandbox.c b/src/firejail/sandbox.c
index 9c96f976a..96407d081 100644
--- a/src/firejail/sandbox.c
+++ b/src/firejail/sandbox.c
@@ -464,6 +464,9 @@ static void close_file_descriptors(void) {
464 if (arg_keep_fd_all) 464 if (arg_keep_fd_all)
465 return; 465 return;
466 466
467 if (arg_debug)
468 printf("Closing non-standard file descriptors\n");
469
467 if (!cfg.keep_fd) { 470 if (!cfg.keep_fd) {
468 close_all(NULL, 0); 471 close_all(NULL, 0);
469 return; 472 return;
diff --git a/test/environment/deterministic-shutdown.exp b/test/environment/deterministic-shutdown.exp
index dbbe226bb..be4e9c42e 100755
--- a/test/environment/deterministic-shutdown.exp
+++ b/test/environment/deterministic-shutdown.exp
@@ -3,14 +3,15 @@
3# Copyright (C) 2014-2022 Firejail Authors 3# Copyright (C) 2014-2022 Firejail Authors
4# License GPL v2 4# License GPL v2
5 5
6set timeout 5 6set timeout 10
7spawn $env(SHELL) 7spawn $env(SHELL)
8match_max 100000 8match_max 100000
9 9
10send -- "firejail --deterministic-shutdown bash -c \"sleep 10 & exec sleep 1\"\r" 10send -- "firejail --deterministic-shutdown bash -c \"sleep 100 & exec sleep 1\"\r"
11expect { 11expect {
12 timeout {puts "TESTING ERROR 0\n";exit} 12 timeout {puts "TESTING ERROR 0\n";exit}
13 "Parent is shutting down, bye..." 13 "Parent is shutting down, bye..."
14} 14}
15after 100
15 16
16puts "\nall done\n" 17puts "\nall done\n"
diff --git a/test/environment/environment.sh b/test/environment/environment.sh
index ce0bb306c..2b77973ac 100755
--- a/test/environment/environment.sh
+++ b/test/environment/environment.sh
@@ -127,5 +127,11 @@ echo "TESTING: deterministic exit code (test/environment/deterministic-exit-code
127echo "TESTING: deterministic shutdown (test/environment/deterministic-shutdown.exp)" 127echo "TESTING: deterministic shutdown (test/environment/deterministic-shutdown.exp)"
128./deterministic-shutdown.exp 128./deterministic-shutdown.exp
129 129
130echo "TESTING: keep fd (test/environment/keep-fd.exp)"
131./keep-fd.exp
132
133echo "TESTING: keep fd errors (test/environment/keep-fd-bad.exp)"
134./keep-fd-bad.exp
135
130echo "TESTING: retain umask (test/environment/umask.exp)" 136echo "TESTING: retain umask (test/environment/umask.exp)"
131(umask 123 && ./umask.exp) 137(umask 123 && ./umask.exp)
diff --git a/test/environment/keep-fd-bad.exp b/test/environment/keep-fd-bad.exp
new file mode 100755
index 000000000..e8b411ea0
--- /dev/null
+++ b/test/environment/keep-fd-bad.exp
@@ -0,0 +1,40 @@
1#!/usr/bin/expect -f
2# This file is part of Firejail project
3# Copyright (C) 2014-2022 Firejail Authors
4# License GPL v2
5
6set timeout 10
7spawn $env(SHELL)
8match_max 100000
9
10
11send -- "firejail --noprofile --keep-fd=\r"
12expect {
13 timeout {puts "TESTING ERROR 0\n";exit}
14 "Error: invalid keep-fd option"
15}
16after 100
17
18send -- "firejail --noprofile --keep-fd=,,,\r"
19expect {
20 timeout {puts "TESTING ERROR 1\n";exit}
21 "Error: invalid keep-fd option"
22}
23after 100
24
25send -- "firejail --noprofile --keep-fd=dall\r"
26expect {
27 timeout {puts "TESTING ERROR 2\n";exit}
28 "Error: invalid keep-fd option"
29}
30after 100
31
32send -- "firejail --noprofile --keep-fd=6,7,8,10b,11\r"
33expect {
34 timeout {puts "TESTING ERROR 3\n";exit}
35 "Error: invalid keep-fd option"
36}
37after 100
38
39
40puts "\nall done\n"
diff --git a/test/environment/keep-fd.exp b/test/environment/keep-fd.exp
new file mode 100755
index 000000000..222234ceb
--- /dev/null
+++ b/test/environment/keep-fd.exp
@@ -0,0 +1,223 @@
1#!/usr/bin/expect -f
2# This file is part of Firejail project
3# Copyright (C) 2014-2022 Firejail Authors
4# License GPL v2
5
6set timeout 10
7spawn $env(SHELL)
8match_max 100000
9
10
11#
12# obtain some open file descriptors
13#
14send -- "exec {WRITE_FD}> blabla\r"
15after 100
16
17send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
18expect {
19 timeout {puts "TESTING ERROR 0\n";exit}
20 "/blabla"
21}
22after 100
23
24send -- "exec {READ_FD}< blabla\r"
25after 100
26
27send -- "readlink -v /proc/self/fd/\$READ_FD\r"
28expect {
29 timeout {puts "TESTING ERROR 1\n";exit}
30 "/blabla"
31}
32after 100
33
34
35#
36# inherit environment variables
37#
38send -- "export READ_FD\r"
39send -- "export WRITE_FD\r"
40after 100
41
42
43#
44# close all file descriptors
45# 0, 1, 2 stay open
46#
47send -- "firejail --noprofile\r"
48expect {
49 timeout {puts "TESTING ERROR 2\n";exit}
50 "Child process initialized"
51}
52after 100
53
54# off by one because of ls
55send -- "ls /proc/self/fd | wc -w\r"
56expect {
57 timeout {puts "TESTING ERROR 3\n";exit}
58 "4"
59}
60after 100
61
62send -- "readlink -v /proc/self/fd/\$READ_FD\r"
63expect {
64 timeout {puts "TESTING ERROR 4\n";exit}
65 "No such file or directory"
66}
67after 100
68
69send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
70expect {
71 timeout {puts "TESTING ERROR 5\n";exit}
72 "No such file or directory"
73}
74after 100
75
76send -- "exit\r"
77after 500
78
79
80#
81# keep one file descriptor
82#
83send -- "firejail --noprofile --keep-fd=\$READ_FD\r"
84expect {
85 timeout {puts "TESTING ERROR 6\n";exit}
86 "Child process initialized"
87}
88after 100
89
90# off by one because of ls
91send -- "ls /proc/self/fd | wc -w\r"
92expect {
93 timeout {puts "TESTING ERROR 7\n";exit}
94 "5"
95}
96after 100
97
98send -- "readlink -v /proc/self/fd/\$READ_FD\r"
99expect {
100 timeout {puts "TESTING ERROR 8\n";exit}
101 "/blabla"
102}
103after 100
104
105send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
106expect {
107 timeout {puts "TESTING ERROR 9\n";exit}
108 "No such file or directory"
109}
110after 100
111
112send -- "exit\r"
113after 500
114
115
116#
117# keep other file descriptor
118#
119send -- "firejail --noprofile --keep-fd=\$WRITE_FD\r"
120expect {
121 timeout {puts "TESTING ERROR 10\n";exit}
122 "Child process initialized"
123}
124after 100
125
126# off by one because of ls
127send -- "ls /proc/self/fd | wc -w\r"
128expect {
129 timeout {puts "TESTING ERROR 11\n";exit}
130 "5"
131}
132after 100
133
134send -- "readlink -v /proc/self/fd/\$READ_FD\r"
135expect {
136 timeout {puts "TESTING ERROR 12\n";exit}
137 "No such file or directory"
138}
139after 100
140
141send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
142expect {
143 timeout {puts "TESTING ERROR 13\n";exit}
144 "/blabla"
145}
146after 100
147
148send -- "exit\r"
149after 500
150
151
152#
153# keep both file descriptors
154#
155send -- "firejail --noprofile --keep-fd=\$READ_FD,\$WRITE_FD\r"
156expect {
157 timeout {puts "TESTING ERROR 14\n";exit}
158 "Child process initialized"
159}
160after 100
161
162# off by one because of ls
163send -- "ls /proc/self/fd | wc -w\r"
164expect {
165 timeout {puts "TESTING ERROR 15\n";exit}
166 "6"
167}
168after 100
169
170send -- "readlink -v /proc/self/fd/\$READ_FD\r"
171expect {
172 timeout {puts "TESTING ERROR 16\n";exit}
173 "/blabla"
174}
175after 100
176
177send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
178expect {
179 timeout {puts "TESTING ERROR 17\n";exit}
180 "/blabla"
181}
182after 100
183
184send -- "exit\r"
185after 500
186
187
188#
189# keep all file descriptors
190#
191send -- "firejail --noprofile --keep-fd=all\r"
192expect {
193 timeout {puts "TESTING ERROR 18\n";exit}
194 "Child process initialized"
195}
196after 100
197
198send -- "readlink -v /proc/self/fd/\$READ_FD\r"
199expect {
200 timeout {puts "TESTING ERROR 19\n";exit}
201 "/blabla"
202}
203after 100
204
205send -- "readlink -v /proc/self/fd/\$WRITE_FD\r"
206expect {
207 timeout {puts "TESTING ERROR 20\n";exit}
208 "/blabla"
209}
210after 100
211
212send -- "exit\r"
213after 500
214
215
216#
217# cleanup
218#
219send -- "rm -f blabla\r"
220after 100
221
222
223puts "\nall done\n"