@@ -28,15 +28,6 @@ static bool warn_fchown;
2828# include <utime.h>
2929#endif
3030
31- #ifdef HAVE_CAP_RIGHTS_LIMIT
32- # include <sys/capsicum.h>
33- #endif
34-
35- #ifdef HAVE_LINUX_LANDLOCK_H
36- # include <linux/landlock.h>
37- # include <sys/syscall.h>
38- #endif
39-
4031#include "tuklib_open_stdxxx.h"
4132
4233#ifdef _MSC_VER
@@ -92,11 +83,6 @@ typedef enum {
9283/// If true, try to create sparse files when decompressing.
9384static bool try_sparse = true;
9485
95- #ifdef ENABLE_SANDBOX
96- /// True if the conditions for sandboxing (described in main()) have been met.
97- static bool sandbox_allowed = false;
98- #endif
99-
10086#ifndef TUKLIB_DOSLIKE
10187/// File status flags of standard input. This is used by io_open_src()
10288/// and io_close_src().
@@ -181,159 +167,6 @@ io_no_sparse(void)
181167}
182168
183169
184- #ifdef ENABLE_SANDBOX
185- extern void
186- io_allow_sandbox (void )
187- {
188- sandbox_allowed = true;
189- return ;
190- }
191-
192-
193- /// Enables operating-system-specific sandbox if it is possible.
194- /// src_fd is the file descriptor of the input file.
195- static void
196- io_sandbox_enter (int src_fd )
197- {
198- if (!sandbox_allowed ) {
199- // This message is more often annoying than useful so
200- // it's commented out. It can be useful when developing
201- // the sandboxing code.
202- //message(V_DEBUG, _("Sandbox is disabled due "
203- // "to incompatible command line arguments"));
204- return ;
205- }
206-
207- const char dummy_str [] = "x" ;
208-
209- // Try to ensure that both libc and xz locale files have been
210- // loaded when NLS is enabled.
211- snprintf (NULL , 0 , "%s%s" , _ (dummy_str ), strerror (EINVAL ));
212-
213- // Try to ensure that iconv data files needed for handling multibyte
214- // characters have been loaded. This is needed at least with glibc.
215- tuklib_mbstr_width (dummy_str , NULL );
216-
217- #ifdef HAVE_CAP_RIGHTS_LIMIT
218- // Capsicum needs FreeBSD 10.2 or later.
219- cap_rights_t rights ;
220-
221- if (cap_enter ())
222- goto error ;
223-
224- if (cap_rights_limit (src_fd , cap_rights_init (& rights ,
225- CAP_EVENT , CAP_FCNTL , CAP_LOOKUP , CAP_READ , CAP_SEEK )))
226- goto error ;
227-
228- // If not reading from stdin, remove all capabilities from it.
229- if (src_fd != STDIN_FILENO && cap_rights_limit (
230- STDIN_FILENO , cap_rights_clear (& rights )))
231- goto error ;
232-
233- if (cap_rights_limit (STDOUT_FILENO , cap_rights_init (& rights ,
234- CAP_EVENT , CAP_FCNTL , CAP_FSTAT , CAP_LOOKUP ,
235- CAP_WRITE , CAP_SEEK )))
236- goto error ;
237-
238- if (cap_rights_limit (STDERR_FILENO , cap_rights_init (& rights ,
239- CAP_WRITE )))
240- goto error ;
241-
242- if (cap_rights_limit (user_abort_pipe [0 ], cap_rights_init (& rights ,
243- CAP_EVENT )))
244- goto error ;
245-
246- if (cap_rights_limit (user_abort_pipe [1 ], cap_rights_init (& rights ,
247- CAP_WRITE )))
248- goto error ;
249-
250- #elif defined(HAVE_PLEDGE )
251- // pledge() was introduced in OpenBSD 5.9.
252- //
253- // main() unconditionally calls pledge() with fairly relaxed
254- // promises which work in all situations. Here we make the
255- // sandbox more strict.
256- if (pledge ("stdio" , "" ))
257- goto error ;
258-
259- (void )src_fd ;
260-
261- #elif defined(HAVE_LINUX_LANDLOCK_H )
262- int landlock_abi = syscall (SYS_landlock_create_ruleset ,
263- (void * )NULL , 0 , LANDLOCK_CREATE_RULESET_VERSION );
264-
265- if (landlock_abi > 0 ) {
266- // We support ABI versions 1-3.
267- if (landlock_abi > 3 )
268- landlock_abi = 3 ;
269-
270- // We want to set all supported flags in handled_access_fs.
271- // This way the ruleset will initially forbid access to all
272- // actions that the available Landlock ABI version supports.
273- // Exceptions can be added using landlock_add_rule(2) to
274- // allow certain actions on certain files or directories.
275- //
276- // The same flag values are used on all archs. ABI v2 and v3
277- // both add one new flag.
278- //
279- // First in ABI v1: LANDLOCK_ACCESS_FS_EXECUTE = 1ULL << 0
280- // Last in ABI v1: LANDLOCK_ACCESS_FS_MAKE_SYM = 1ULL << 12
281- // Last in ABI v2: LANDLOCK_ACCESS_FS_REFER = 1ULL << 13
282- // Last in ABI v3: LANDLOCK_ACCESS_FS_TRUNCATE = 1ULL << 14
283- //
284- // This makes it simple to set the mask based on the ABI
285- // version and we don't need to care which flags are #defined
286- // in the installed <linux/landlock.h>.
287- const struct landlock_ruleset_attr attr = {
288- .handled_access_fs = (1ULL << (12 + landlock_abi )) - 1
289- };
290-
291- const int ruleset_fd = syscall (SYS_landlock_create_ruleset ,
292- & attr , sizeof (attr ), 0U );
293- if (ruleset_fd < 0 )
294- goto error ;
295-
296- // All files we need should have already been opened. Thus,
297- // we don't need to add any rules using landlock_add_rule(2)
298- // before activating the sandbox.
299- //
300- // NOTE: It's possible that the hack at the beginning of this
301- // function isn't be good enough. It tries to get translations
302- // and libc-specific files loaded but if it's not good enough
303- // then perhaps a Landlock rule to allow reading from /usr
304- // and/or the xz installation prefix would be needed.
305- //
306- // prctl(PR_SET_NO_NEW_PRIVS, ...) was already called in
307- // main() so we don't do it here again.
308- if (syscall (SYS_landlock_restrict_self , ruleset_fd , 0U ) != 0 )
309- goto error ;
310- }
311-
312- (void )src_fd ;
313-
314- #else
315- # error ENABLE_SANDBOX is defined but no sandboxing method was found.
316- #endif
317-
318- // This message is annoying in xz -lvv.
319- //message(V_DEBUG, _("Sandbox was successfully enabled"));
320- return ;
321-
322- error :
323- #ifdef HAVE_CAP_RIGHTS_LIMIT
324- // If a kernel is configured without capability mode support or
325- // used in an emulator that does not implement the capability
326- // system calls, then the Capsicum system calls will fail and set
327- // errno to ENOSYS. In that case xz will silently run without
328- // the sandbox.
329- if (errno == ENOSYS )
330- return ;
331- #endif
332- message_fatal (_ ("Failed to enable the sandbox" ));
333- }
334- #endif // ENABLE_SANDBOX
335-
336-
337170#ifndef TUKLIB_DOSLIKE
338171/// \brief Waits for input or output to become available or for a signal
339172///
@@ -889,7 +722,8 @@ io_open_src(const char *src_name)
889722
890723#ifdef ENABLE_SANDBOX
891724 if (!error )
892- io_sandbox_enter (pair .src_fd );
725+ sandbox_enable_strict_if_allowed (pair .src_fd ,
726+ user_abort_pipe [0 ], user_abort_pipe [1 ]);
893727#endif
894728
895729 return error ? NULL : & pair ;
0 commit comments