Skip to content

Commit d43692e

Browse files
committed
Support scoping abstract unix sockets
Closes: #330 Signed-off-by: Rahul Sandhu <nvraxn@gmail.com>
1 parent d6180f2 commit d43692e

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

bubblewrap.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,15 @@
3232
#include <sys/signalfd.h>
3333
#include <sys/capability.h>
3434
#include <sys/prctl.h>
35+
#include <sys/syscall.h>
3536
#include <linux/sched.h>
3637
#include <linux/seccomp.h>
3738
#include <linux/filter.h>
3839

40+
#ifdef HAVE_LANDLOCK_H
41+
#include <linux/landlock.h>
42+
#endif
43+
3944
#include "utils.h"
4045
#include "network.h"
4146
#include "bind-mount.h"
@@ -92,6 +97,7 @@ static int opt_userns_fd = -1;
9297
static int opt_userns2_fd = -1;
9398
static int opt_pidns_fd = -1;
9499
static int opt_tmp_overlay_count = 0;
100+
static bool opt_scope_abstract_unix_sockets = false;
95101
static int next_perms = -1;
96102
static size_t next_size_arg = 0;
97103
static int next_overlay_src_count = 0;
@@ -373,6 +379,7 @@ usage (int ecode, FILE *out)
373379
" --perms OCTAL Set permissions of next argument (--bind-data, --file, etc.)\n"
374380
" --size BYTES Set size of next argument (only for --tmpfs)\n"
375381
" --chmod OCTAL PATH Change permissions of PATH (must already exist)\n"
382+
" --scope-abstract-af-unix Scope access to abstract unix sockets to within in the sandbox\n"
376383
);
377384
exit (ecode);
378385
}
@@ -2736,6 +2743,10 @@ parse_args_recurse (int *argcp,
27362743
argv += 2;
27372744
argc -= 2;
27382745
}
2746+
else if (strcmp (arg, "--scope-abstract-af-unix") == 0)
2747+
{
2748+
opt_scope_abstract_unix_sockets = true;
2749+
}
27392750
else if (strcmp (arg, "--") == 0)
27402751
{
27412752
argv += 1;
@@ -2867,6 +2878,26 @@ namespace_ids_write (int fd,
28672878
}
28682879
}
28692880

2881+
#ifdef HAVE_LANDLOCK_H
2882+
#ifndef landlock_create_ruleset
2883+
static inline int
2884+
landlock_create_ruleset (const struct landlock_ruleset_attr *attr,
2885+
size_t size,
2886+
uint32_t flags)
2887+
{
2888+
return syscall (SYS_landlock_create_ruleset, attr, size, flags);
2889+
}
2890+
#endif
2891+
2892+
#ifndef landlock_restrict_self
2893+
static inline int
2894+
landlock_restrict_self (int ruleset_fd, uint32_t flags)
2895+
{
2896+
return syscall (SYS_landlock_restrict_self, ruleset_fd, flags);
2897+
}
2898+
#endif
2899+
#endif
2900+
28702901
int
28712902
main (int argc,
28722903
char **argv)
@@ -3491,6 +3522,27 @@ main (int argc,
34913522
die ("creation of new user namespaces was not disabled as requested");
34923523
}
34933524

3525+
if (opt_scope_abstract_unix_sockets)
3526+
{
3527+
#ifdef HAVE_LANDLOCK_H
3528+
static const struct landlock_ruleset_attr ruleset_attr = {
3529+
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
3530+
};
3531+
const int abi = landlock_create_ruleset (NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
3532+
if (abi < 0)
3533+
die_with_error ("failed to check Landlock compatibility");
3534+
if (abi < 6)
3535+
die ("supported kernel Landlock ABI too old, version 6 or above required");
3536+
const int ruleset_fd = landlock_create_ruleset (&ruleset_attr, sizeof (ruleset_attr), 0);
3537+
if (ruleset_fd < 0)
3538+
die_with_error ("failed to create Landlock ruleset");
3539+
if (landlock_restrict_self (ruleset_fd, 0) < 0)
3540+
die_with_error ("failed to enforce Landlock ruleset");
3541+
#else
3542+
die ("Landlock not available at compile time, cannot implement --scope-abstract-af-unix");
3543+
#endif
3544+
}
3545+
34943546
/* All privileged ops are done now, so drop caps we don't need */
34953547
drop_privs (!is_privileged, true);
34963548

bwrap.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,17 @@
617617
command line. Please be careful to the order they are specified.
618618
</para></listitem>
619619
</varlistentry>
620+
<varlistentry>
621+
<term><option>--scope-abstract-af-unix</option></term>
622+
<listitem><para>
623+
Scope access to abstract unix sockets. This option will prevent the newly
624+
created sandbox from talking to any abstract unix sockets, including in the
625+
current net namespace (i.e. in the absence of <option>--unshare-net</option>).
626+
627+
This has the same behaviour as LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET: see
628+
<citerefentry><refentrytitle>landlock</refentrytitle><manvolnum>7</manvolnum></citerefentry> for details.
629+
</para></listitem>
630+
</varlistentry>
620631
</variablelist>
621632
</refsect1>
622633

completions/bash/bwrap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ _bwrap() {
1515
--disable-userns
1616
--help
1717
--new-session
18+
--scope-abstract-af-unix
1819
--unshare-all
1920
--unshare-cgroup
2021
--unshare-cgroup-try

completions/zsh/_bwrap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ _bwrap_args=(
6060
'--remount-ro[Remount DEST as readonly; does not recursively remount]:mount point to remount read-only:_files'
6161
'--ro-bind-try[Equal to --ro-bind but ignores non-existent SRC]:source:_files:destination:_files'
6262
'--ro-bind[Bind mount the host path SRC readonly on DEST]:source:_files:destination:_files'
63+
'--scope-abstract-af-unix[Scope access to abstract unix sockets to within in the sandbox]'
6364
'--seccomp[Load and use seccomp rules from FD]: :_guard "[0-9]#" "file descriptor to read seccomp rules from"'
6465
'--setenv[Set an environment variable]:variable to set:_parameters -g "*export*":value of variable: :'
6566
'--size[Set size in bytes for next action argument]: :->after_size'

meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ if (
5757
], language : 'c')
5858
endif
5959

60+
if cc.check_header('linux/landlock.h')
61+
add_project_arguments('-DHAVE_LANDLOCK_H', language : 'c')
62+
endif
63+
6064
bash = find_program('bash', required : false)
6165

6266
if get_option('python') == ''

0 commit comments

Comments
 (0)