|
32 | 32 | #include <sys/signalfd.h> |
33 | 33 | #include <sys/capability.h> |
34 | 34 | #include <sys/prctl.h> |
| 35 | +#include <sys/syscall.h> |
35 | 36 | #include <linux/sched.h> |
36 | 37 | #include <linux/seccomp.h> |
37 | 38 | #include <linux/filter.h> |
38 | 39 |
|
| 40 | +#ifdef HAVE_LANDLOCK_H |
| 41 | +#include <linux/landlock.h> |
| 42 | +#endif |
| 43 | + |
39 | 44 | #include "utils.h" |
40 | 45 | #include "network.h" |
41 | 46 | #include "bind-mount.h" |
@@ -92,6 +97,7 @@ static int opt_userns_fd = -1; |
92 | 97 | static int opt_userns2_fd = -1; |
93 | 98 | static int opt_pidns_fd = -1; |
94 | 99 | static int opt_tmp_overlay_count = 0; |
| 100 | +static bool opt_scope_abstract_unix_sockets = false; |
95 | 101 | static int next_perms = -1; |
96 | 102 | static size_t next_size_arg = 0; |
97 | 103 | static int next_overlay_src_count = 0; |
@@ -373,6 +379,7 @@ usage (int ecode, FILE *out) |
373 | 379 | " --perms OCTAL Set permissions of next argument (--bind-data, --file, etc.)\n" |
374 | 380 | " --size BYTES Set size of next argument (only for --tmpfs)\n" |
375 | 381 | " --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" |
376 | 383 | ); |
377 | 384 | exit (ecode); |
378 | 385 | } |
@@ -2736,6 +2743,10 @@ parse_args_recurse (int *argcp, |
2736 | 2743 | argv += 2; |
2737 | 2744 | argc -= 2; |
2738 | 2745 | } |
| 2746 | + else if (strcmp (arg, "--scope-abstract-af-unix") == 0) |
| 2747 | + { |
| 2748 | + opt_scope_abstract_unix_sockets = true; |
| 2749 | + } |
2739 | 2750 | else if (strcmp (arg, "--") == 0) |
2740 | 2751 | { |
2741 | 2752 | argv += 1; |
@@ -2867,6 +2878,26 @@ namespace_ids_write (int fd, |
2867 | 2878 | } |
2868 | 2879 | } |
2869 | 2880 |
|
| 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 | + |
2870 | 2901 | int |
2871 | 2902 | main (int argc, |
2872 | 2903 | char **argv) |
@@ -3491,6 +3522,27 @@ main (int argc, |
3491 | 3522 | die ("creation of new user namespaces was not disabled as requested"); |
3492 | 3523 | } |
3493 | 3524 |
|
| 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 | + |
3494 | 3546 | /* All privileged ops are done now, so drop caps we don't need */ |
3495 | 3547 | drop_privs (!is_privileged, true); |
3496 | 3548 |
|
|
0 commit comments