Skip to content

Commit 588ad8d

Browse files
committed
Support scoping abstract unix sockets
Closes: #330 Signed-off-by: Rahul Sandhu <[email protected]>
1 parent d6180f2 commit 588ad8d

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

bubblewrap.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@
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>
39+
#include <linux/landlock.h>
3840

3941
#include "utils.h"
4042
#include "network.h"
@@ -92,6 +94,7 @@ static int opt_userns_fd = -1;
9294
static int opt_userns2_fd = -1;
9395
static int opt_pidns_fd = -1;
9496
static int opt_tmp_overlay_count = 0;
97+
static bool opt_scope_abstract_unix_sockets = false;
9598
static int next_perms = -1;
9699
static size_t next_size_arg = 0;
97100
static int next_overlay_src_count = 0;
@@ -373,6 +376,7 @@ usage (int ecode, FILE *out)
373376
" --perms OCTAL Set permissions of next argument (--bind-data, --file, etc.)\n"
374377
" --size BYTES Set size of next argument (only for --tmpfs)\n"
375378
" --chmod OCTAL PATH Change permissions of PATH (must already exist)\n"
379+
" --scope-abstract-af-unix Scope access to abstract unix sockets to within in the sandbox\n"
376380
);
377381
exit (ecode);
378382
}
@@ -2736,6 +2740,10 @@ parse_args_recurse (int *argcp,
27362740
argv += 2;
27372741
argc -= 2;
27382742
}
2743+
else if (strcmp (arg, "--scope-abstract-af-unix") == 0)
2744+
{
2745+
opt_scope_abstract_unix_sockets = true;
2746+
}
27392747
else if (strcmp (arg, "--") == 0)
27402748
{
27412749
argv += 1;
@@ -2867,6 +2875,24 @@ namespace_ids_write (int fd,
28672875
}
28682876
}
28692877

2878+
#ifndef landlock_create_ruleset
2879+
static inline int
2880+
landlock_create_ruleset (const struct landlock_ruleset_attr *attr,
2881+
size_t size,
2882+
uint32_t flags)
2883+
{
2884+
return syscall (SYS_landlock_create_ruleset, attr, size, flags);
2885+
}
2886+
#endif
2887+
2888+
#ifndef landlock_restrict_self
2889+
static inline int
2890+
landlock_restrict_self (int ruleset_fd, uint32_t flags)
2891+
{
2892+
return syscall (SYS_landlock_restrict_self, ruleset_fd, flags);
2893+
}
2894+
#endif
2895+
28702896
int
28712897
main (int argc,
28722898
char **argv)
@@ -3491,6 +3517,23 @@ main (int argc,
34913517
die ("creation of new user namespaces was not disabled as requested");
34923518
}
34933519

3520+
if (opt_scope_abstract_unix_sockets)
3521+
{
3522+
static const struct landlock_ruleset_attr ruleset_attr = {
3523+
.scoped = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET
3524+
};
3525+
const int abi = landlock_create_ruleset (NULL, 0, LANDLOCK_CREATE_RULESET_VERSION);
3526+
if (abi < 0)
3527+
die_with_error ("failed to check Landlock compatibility");
3528+
if (abi < 6)
3529+
die ("supported kernel Landlock ABI too old, version 6 or above required");
3530+
const int ruleset_fd = landlock_create_ruleset (&ruleset_attr, sizeof (ruleset_attr), 0);
3531+
if (ruleset_fd < 0)
3532+
die_with_error ("failed to create Landlock ruleset");
3533+
if (landlock_restrict_self (ruleset_fd, 0) < 0)
3534+
die_with_error ("failed to enforce Landlock ruleset");
3535+
}
3536+
34943537
/* All privileged ops are done now, so drop caps we don't need */
34953538
drop_privs (!is_privileged, true);
34963539

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-unix-sockets
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-unix-sockets[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'

0 commit comments

Comments
 (0)