Skip to content

Commit 374868d

Browse files
committed
xz: Move sandboxing code to sandbox.c and improve Landlock sandbox.
Landlock is now always used just like pledge(2) is: first in more permissive mode and later (under certain common conditions) in a strict mode that doesn't allow opening more files. I put pledge(2) first in sandbox.c because it's the simplest API to use and still somewhat fine-grained for basic applications. So it's the simplest thing to understand for anyone reading sandbox.c.
1 parent 7312dfb commit 374868d

8 files changed

Lines changed: 357 additions & 213 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,8 @@ if(NOT MSVC OR MSVC_VERSION GREATER_EQUAL 1900)
13931393
src/xz/options.c
13941394
src/xz/options.h
13951395
src/xz/private.h
1396+
src/xz/sandbox.c
1397+
src/xz/sandbox.h
13961398
src/xz/signals.c
13971399
src/xz/signals.h
13981400
src/xz/suffix.c

‎src/xz/Makefile.am‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ xz_SOURCES = \
2121
options.c \
2222
options.h \
2323
private.h \
24+
sandbox.c \
25+
sandbox.h \
2426
signals.c \
2527
signals.h \
2628
suffix.c \

‎src/xz/file_io.c‎

Lines changed: 2 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
9384
static 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;

‎src/xz/file_io.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ extern void io_write_to_user_abort_pipe(void);
9999
extern void io_no_sparse(void);
100100

101101

102-
#ifdef ENABLE_SANDBOX
103-
/// \brief main() calls this if conditions for sandboxing have been met.
104-
extern void io_allow_sandbox(void);
105-
#endif
106-
107-
108102
/// \brief Open the source file
109103
extern file_pair *io_open_src(const char *src_name);
110104

‎src/xz/main.c‎

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212
#include "private.h"
1313
#include <ctype.h>
1414

15-
// prctl(PR_SET_NO_NEW_PRIVS, ...) is required with Landlock but it can be
16-
// activated even when conditions for strict sandboxing aren't met.
17-
#ifdef HAVE_LINUX_LANDLOCK_H
18-
# include <sys/prctl.h>
19-
#endif
20-
2115

2216
/// Exit status to use. This can be changed with set_exit_status().
2317
static enum exit_status_type exit_status = E_SUCCESS;
@@ -148,32 +142,6 @@ read_name(const args_info *args)
148142
int
149143
main(int argc, char **argv)
150144
{
151-
#ifdef HAVE_PLEDGE
152-
// OpenBSD's pledge(2) sandbox
153-
//
154-
// Unconditionally enable sandboxing with fairly relaxed promises.
155-
// This is still way better than having no sandbox at all. :-)
156-
// More strict promises will be made later in file_io.c if possible.
157-
if (pledge("stdio rpath wpath cpath fattr", "")) {
158-
// Don't translate the string or use message_fatal() as
159-
// those haven't been initialized yet.
160-
fprintf(stderr, "%s: Failed to enable the sandbox\n", argv[0]);
161-
return E_ERROR;
162-
}
163-
#endif
164-
165-
#ifdef HAVE_LINUX_LANDLOCK_H
166-
// Prevent the process from gaining new privileges. This must be done
167-
// before landlock_restrict_self(2) in file_io.c but since we will
168-
// never need new privileges, this call can be done here already.
169-
//
170-
// This is supported since Linux 3.5. Ignore the return value to
171-
// keep compatibility with old kernels. landlock_restrict_self(2)
172-
// will fail if the no_new_privs attribute isn't set, thus if prctl()
173-
// fails here the error will still be detected when it matters.
174-
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
175-
#endif
176-
177145
#if defined(_WIN32) && !defined(__CYGWIN__)
178146
InitializeCriticalSection(&exit_status_cs);
179147
#endif
@@ -187,6 +155,20 @@ main(int argc, char **argv)
187155
// even indirectly like locale and gettext initializations.
188156
io_init();
189157

158+
#ifdef ENABLE_SANDBOX
159+
// Enable such sandboxing that can always be enabled.
160+
// This requires that progname has been set up.
161+
// It's also good that io_init() has been called because it
162+
// might need to do things that the initial sandbox won't allow.
163+
// Otherwise this should be called as early as possible.
164+
//
165+
// NOTE: Calling this before tuklib_gettext_init() means that
166+
// translated error message won't be available if sandbox
167+
// initialization fails. However, sandbox_init() shouldn't
168+
// fail and this order simply feels better.
169+
sandbox_init();
170+
#endif
171+
190172
// Set up the locale and message translations.
191173
tuklib_gettext_init(PACKAGE, LOCALEDIR);
192174

@@ -241,7 +223,7 @@ main(int argc, char **argv)
241223
signals_init();
242224

243225
#ifdef ENABLE_SANDBOX
244-
// Set a flag that sandboxing is allowed if all these are true:
226+
// Set a flag that strict sandboxing is allowed if all these are true:
245227
// - --files or --files0 wasn't used.
246228
// - There is exactly one input file or we are reading from stdin.
247229
// - We won't create any files: output goes to stdout or --test
@@ -255,7 +237,7 @@ main(int argc, char **argv)
255237
if (args.files_name == NULL && args.arg_count == 1
256238
&& (opt_stdout || strcmp("-", args.arg_names[0]) == 0
257239
|| opt_mode == MODE_LIST))
258-
io_allow_sandbox();
240+
sandbox_allow_strict();
259241
#endif
260242

261243
// coder_run() handles compression, decompression, and testing.

‎src/xz/private.h‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,6 @@
5151
# define STDERR_FILENO (fileno(stderr))
5252
#endif
5353

54-
#if defined(HAVE_CAP_RIGHTS_LIMIT) || defined(HAVE_PLEDGE) \
55-
|| defined(HAVE_LINUX_LANDLOCK_H)
56-
# define ENABLE_SANDBOX 1
57-
#endif
58-
5954
// Handling SIGTSTP keeps time-keeping for progress indicator correct
6055
// if xz is stopped. It requires use of clock_gettime() as that is
6156
// async-signal safe in POSIX. Require also SIGALRM support since
@@ -75,6 +70,7 @@
7570
#include "hardware.h"
7671
#include "file_io.h"
7772
#include "options.h"
73+
#include "sandbox.h"
7874
#include "signals.h"
7975
#include "suffix.h"
8076
#include "util.h"

0 commit comments

Comments
 (0)