118 Commits

Author SHA1 Message Date
Simon McVittie
7816e01298 Merge pull request #603 from quag/nixos-recursive-test-fix
Make test "can pivot to new rootfs recursively" work on NixOS
2023-10-02 10:08:35 +01:00
Jonathan Wright
5e2b6a3079 Make test "can pivot to new rootfs recursively" work on NixOS
Signed-off-by: Jonathan Wright <quaggy@gmail.com>
2023-10-01 11:11:28 -07:00
Jonathan Wright
0ff3430dff Replace last /bin/bash with bash in test-run.sh
The other three references to bash already use "bash" instead of
"/bin/bash". Similarly, "#!/bin/bash" has already been replaced with
"#!/usr/bin/env bash".

Signed-off-by: Jonathan Wright <quaggy@gmail.com>
2023-10-01 10:26:28 -07:00
Jonathan Wright
70699505e2 Fix typo in test case name (prefxing instead of prefixing)
Signed-off-by: Jonathan Wright <quaggy@gmail.com>
2023-10-01 10:26:28 -07:00
Jonathan Wright
1cba87136e Fix MRs conflicting when they add unit tests
Fixes containers/bubblewrap#420

Signed-off-by: Jonathan Wright <quaggy@gmail.com>
2023-10-01 10:26:23 -07:00
Simon McVittie
4a7bb29257 Merge pull request #599 from swick/wip/mnt-symlink-test
tests: Skip test when host /mnt is a symlink
2023-10-01 13:01:46 +01:00
Jonathan Wright
4303430642 Add --argv0 option
Fixes containers/bubblewrap#91

Add the ability to overwrite argv[0] when starting a process in a
container. Using --argv0 to be consistent with ld.so --argv0.

Overwriting argv[0] is useful as some tools change their behavior based
on the value of argv[0]. For example, when bash is symlinked to sh it
behaves as sh. Similarly, unxz is a symlink to xz and changes the
default from compressing to decompressing. An extreme example is on many
systems, date, df, cat and so on are all symlinks to the coreutils
binary.

Example usage: bwrap --bind / / --argv0 sh bash

Signed-off-by: Jonathan Wright <quaggy@gmail.com>
2023-09-30 14:23:18 -07:00
Sebastian Wick
ad09be9443 tests: Skip test when host /mnt is a symlink
The test wants to mount /tmp on /mnt but /mnt comes from the host and
can be a symlink in which case the test fails. Skip the test in this
situation.

Signed-off-by: Sebastian Wick <sebastian.wick@redhat.com>
2023-09-28 18:16:20 +02:00
Simon McVittie
2ba9a9af91 tests: Try harder to evade --disable-userns
The worst-case scenario in terms of enforcing --disable-userns is that
we're retaining all capabilities, so test that too, to make sure that
the option is genuinely restricting even a privileged user.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-02-23 12:19:38 +00:00
Simon McVittie
140936fd73 tests: Explicitly unshare userns when testing --disable-userns
If we're running the tests as uid 0 with capabilities, then bwrap will
not create a new user namespace by default, which means the limit won't
be exceeded and the test will fail. Make sure we always try to create
the new user namespace.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-02-23 12:17:59 +00:00
Simon McVittie
41fd02ad14 test-run: Filter out no-new-privs in capsh output
Older versions of capsh would only show the capabilities, which we
expect not to change when we don't drop capabilities; but newer
versions also display whether the NO_NEW_PRIVS bit is set, and we *do*
expect to change that.

Resolves: https://github.com/containers/bubblewrap/issues/544
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-01-04 09:47:04 +01:00
Simon McVittie
b5f672355b Add --assert-userns-disabled option
We can't combine --disable-userns with entering an existing user
namespace via --userns if the existing user namespace was created with
--disable-userns, because its ability to create nested user namespaces
has already been disabled. However, the next best thing is to verify
that we are already in the desired state.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-01-03 11:04:09 +01:00
Simon McVittie
b33c333bcb Add an option to disable nested user namespaces by setting limit to 1
Some use-cases of bubblewrap want to ensure that the subprocess can't
further re-arrange the filesystem namespace, or do other more complex
namespace modification. For example, Flatpak wants to prevent sandboxed
processes from altering their /proc/$pid/root/.flatpak-info, so that
/.flatpak-info can safely be used as an indicator that a process is part
of a Flatpak app.

This approach was suggested by lukts30 on containers/bubblewrap#452.
The sysctl-controlled maximum numbers of namespaces are themselves
namespaced, so we can disable nested user namespaces by setting the
limit to 1 and then entering a new, nested user namespace. The resulting
process loses its privileges in the namespace where the limit was set
to 1, so it is unable to move the limit back up.

Co-authored-by: Alexander Larsson <alexl@redhat.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
2023-01-03 11:04:09 +01:00
Simon McVittie
5080b233fa test-run: Don't rely on df supporting the --output=size option
df --output was new in coreutils 8.21 (2013), and non-GNU
implementations like busybox df don't have it.

This avoids a test failure in Steam Runtime 1 'scout', which is based
on Ubuntu 12.04 (2012). It'll also be helpful for anyone maintaining
an OS with non-GNU shell utilities.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-07 18:30:21 +00:00
Simon McVittie
cfc15df5f1 test-run: If bubblewrap is setuid, assert that --size is not allowed
Previously, this test would have failed for a setuid bubblewrap.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-11-07 18:26:21 +00:00
Simon McVittie
d41edb969a Merge pull request #509 from tomsmeding/tmpfs-size
Add --size option to control size of a --tmpfs
2022-10-26 21:43:25 +01:00
Tom Smeding
906a7a75bd Tests for --size and --perms
Signed-off-by: Tom Smeding <tom@tomsmeding.com>
2022-10-26 19:46:59 +02:00
a1346054
a29f535b74 test-run: replace nonstandard which
`command -v` is the standardized replacement for `which` and never
writes to stderr

Signed-off-by: a1346054 <36859588+a1346054@users.noreply.github.com>
2022-09-12 22:09:11 +00:00
Simon McVittie
8af578d088 Merge pull request #510 from smcv/etc-shadow
Adjust tests related to /etc/shadow
2022-07-26 19:54:46 +01:00
Simon McVittie
7d54d050a2 meson: Add options to set a RPATH/RUNPATH on the bwrap executable
This is useful when building a self-contained, relocatable tree
containing a build of bubblewrap and all of its non-glibc dependencies
(in practice this means libcap and maybe libselinux), as is done in
the Steam container runtime. A RPATH/RUNPATH pointing to ${ORIGIN}/../lib
allows bwrap to find an adjacent, bundled copy of libcap.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-16 10:19:24 +02:00
Simon McVittie
2b4c4a7899 test-run: Add another assertion that we cannot read /etc/shadow
The goal of this assertion was to demonstrate that a setuid bwrap does
not give us access to otherwise unreadable files, but if we want to
check that, we should probably be looking at the bind-mount destination
instead of the source file.

Leave the old assertion in too, just in case *that* fails.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-11 16:01:11 +01:00
Simon McVittie
a996acc892 test-run: Skip a test if we can read /etc/shadow
Ordinarily, we would not be able to read /etc/shadow if we're not uid 0;
but when building in a sysroot owned by the current user (for example
by setting it up using bwrap, as steam-runtime-tools does), we might
actually be able to read it. Skip the assertion that we cannot read it
in this case.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-11 15:55:10 +01:00
Simon McVittie
5ba8fc31ca Merge pull request #492 from smcv/mips
try-syscall: Use compiler-predefined macros to detect mips ABI
2022-05-10 16:31:02 +01:00
Simon McVittie
14d6a7f0df Merge pull request #496 from smcv/missing-def
Allow building on old glibc without PR_SET_CHILD_SUBREAPER defined
2022-05-10 16:23:02 +01:00
Simon McVittie
3faa4c1ff2 tests: Disable leak detection when joining user-specified pid namespace
If we don't do this, AddressSanitizer busy-loops with this backtrace:

    #0  in sched_yield
    #1  in __sanitizer::StopTheWorld
    #2  in __lsan::LockStuffAndStopTheWorldCallback
    #3  in __GI___dl_iterate_phdr
    #4  in __lsan::LockStuffAndStopTheWorld
    #5  in __lsan::CheckForLeaks
    #6  in __lsan::DoLeakCheck
    #7  __lsan::DoLeakCheck
    #8  in __cxa_finalize
    #9  in __do_global_dtors_aux
    #10 in ??
    #11 in _dl_fini

This fixes the hang described in commit 2e3d6e7d, so remove the
workarounds from that commit.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-03 14:02:03 +02:00
Simon McVittie
c991f2dd9a tests: Disable sanitizers for try-syscall
gcc's AddressSanitizer makes system calls that our filter doesn't
allow for, resulting in a fatal error when run under a restrictive
seccomp filter.

try-syscall is a helper for the test, rather than being code under test
itself, so we don't really need this instrumentation in it: all we want
it to do is make some specific syscalls.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-05-03 14:02:03 +02:00
Simon McVittie
7e62e8dafe Allow building on old glibc without PR_SET_CHILD_SUBREAPER defined
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-05 14:48:23 +01:00
Simon McVittie
bab52ef198 try-syscall: Use compiler-predefined macros to detect mips ABI
_MIPS_SIM_ABI32 etc. are defined by Linux <asm/sgidefs.h>, which is
included by glibc <sys/syscall.h> (which defers to Linux headers to
get syscall numbers), but not by musl <sys/syscall.h>.

_ABIO32 etc. are predefined by the compiler, so they are always
available, regardless of libc. References:

https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=27d54b2a6c18ef1ae50f1a5b432d590438445b90
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=0ea339ea4d9c3e04ae17da6bf389617eb0251e57

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-04-05 13:49:04 +01:00
Marc-Antoine Perennou
08dc2bb546 tests: properly tag ok as being printf-like
Avoids breaking warning with clang

Fixes #478

Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com>
2022-03-18 10:41:54 +01:00
Simon McVittie
2e3d6e7dc7 Disable test-specifying-pidns.sh under 'meson dist' while I investigate
This test is hanging when run under 'meson dist' for some reason, but
not when run under 'meson test', and not locally, only in the Github
Workflow-based CI. Disable it for now.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-20 11:41:27 +00:00
Simon McVittie
3fe7cff04f tests: Fix compiler warnings for unused arguments
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-20 11:41:27 +00:00
Simon McVittie
d60632db19 meson: Run test scripts from $srcdir
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-20 11:41:27 +00:00
Simon McVittie
d80dcd80df meson: Make G_TEST_SRCDIR, G_TEST_BUILDDIR match Autotools
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-20 11:41:27 +00:00
Simon McVittie
12c920b492 meson: Run the Python test script with Python, not bash
The python build option can be used to swap to a different interpreter,
for environments like the Steam Runtime where the python3 executable in
the PATH is extremely old but there is a better interpreter available.

This is treated as non-optional, because Meson is written in Python,
so the situation where there is no Python interpreter at build-time
shouldn't arise.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-20 11:41:24 +00:00
Simon McVittie
5750517e1c meson: Build the try-syscall helper
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-19 14:46:37 +00:00
Simon McVittie
b07adc68f9 meson: Build tests with equivalent of -I$(top_srcdir) -I$(top_builddir)
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-19 14:46:20 +00:00
Simon McVittie
1927981240 Add a Meson build system
This allows bwrap to be built as a subproject in larger Meson projects.
When built as a subproject, we install into the --libexecdir and
require a program prefix to be specified: for example, Flatpak would use
program_prefix=flatpak- to get /usr/libexec/flatpak-bwrap. Verified to
be backwards-compatible as far as Meson 0.49.0 (Debian 9 backports).

Loosely based on previous work by Jussi Pakkanen (see #133).

Differences between the Autotools and Meson builds:

The Meson build requires a version of libcap that has pkg-config
metadata (introduced in libcap 2.23, in 2013).

The Meson build has no equivalent of --with-priv-mode=setuid. On
distributions like Debian <= 10 and RHEL <= 7 that require a setuid bwrap
executable, the sysadmin or distribution packaging will need to set the
correct permissions on the bwrap executable; Debian already did this via
packaging rather than the upstream build system.

The Meson build supports being used as a subproject, and there is CI
for this. It automatically disables shell completions and man pages,
moves the bubblewrap executable to ${libexecdir}, and renames the
bubblewrap executable according to a program_prefix option that the
caller must specify (for example, Flatpak would use
-Dprogram_prefix=flatpak- to get /usr/libexec/flatpak-bwrap). See the
tests/use-as-subproject/ directory for an example.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-02-18 10:42:55 +00:00
Simon McVittie
3612534c2c tests: Exercise seccomp filters
Signed-off-by: Simon McVittie <smcv@collabora.com>
2022-01-31 17:13:46 +00:00
a1346054
366564eaa0 Fix spelling
Signed-off-by: a1346054 <36859588+a1346054@users.noreply.github.com>
2021-10-14 14:40:10 +02:00
a1346054
d69d21b036 bash: Fix shellcheck warnings
Signed-off-by: a1346054 <36859588+a1346054@users.noreply.github.com>
2021-10-14 14:40:10 +02:00
a1346054
f2a2dbbeb9 bash: Invoke bash using /usr/bin/env
Signed-off-by: a1346054 <36859588+a1346054@users.noreply.github.com>
2021-10-14 14:40:10 +02:00
Simon McVittie
a6c90c516b Add SPDX-License-Identifier for files that already specify license
This is a step towards REUSE compliance. Third-party files that we do
not otherwise edit (git.mk, m4/attributes.m4) are excluded here.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-08-31 12:32:40 +01:00
Simon McVittie
4509ee8061 tests: Use preferred spelling for SPDX license identifiers
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-08-31 12:31:58 +01:00
Simon McVittie
9154e722cd tests: Split out tests involving fifos and sharing namespaces
These are more time-consuming than the rest of test-run.sh combined due
to their use of a wait loop, and separating them out is helpful for the
possible addition of a Meson build system. The test-case that is moved
into tests/test-specifying-pidns.sh hangs and times out under
"meson dist" on Github Actions CI, but not under "meson test" or
Autotools, and not when tested locally; putting this in its own script
might help to isolate and fix that failure.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:55 +01:00
Simon McVittie
02e3a30081 tests: Use $() in preference to backquotes
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:54 +01:00
Simon McVittie
8f854f4ddd tests: Remove a useless use of cat
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:53 +01:00
Simon McVittie
33ebfa862d tests: Really clean up test temporary directory
The flag file we create and the flag file we check for were not in sync.
Also similarly correct the variable name in an info message.

Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:35 +01:00
Simon McVittie
7b514a097f libtest: Add directive and 'set -e' for better shellcheck diagnostics
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:34 +01:00
Simon McVittie
165bbf9647 tests: Remove unnecessary nesting
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:33 +01:00
Simon McVittie
97979be1c9 tests: Quote more defensively
Signed-off-by: Simon McVittie <smcv@collabora.com>
2021-06-28 11:11:32 +01:00