|
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> |
| 39 | +#include <linux/landlock.h> |
38 | 40 |
|
39 | 41 | #include "utils.h" |
40 | 42 | #include "network.h" |
@@ -92,6 +94,7 @@ static int opt_userns_fd = -1; |
92 | 94 | static int opt_userns2_fd = -1; |
93 | 95 | static int opt_pidns_fd = -1; |
94 | 96 | static int opt_tmp_overlay_count = 0; |
| 97 | +static bool opt_scope_abstract_unix_sockets = false; |
95 | 98 | static int next_perms = -1; |
96 | 99 | static size_t next_size_arg = 0; |
97 | 100 | static int next_overlay_src_count = 0; |
@@ -373,6 +376,7 @@ usage (int ecode, FILE *out) |
373 | 376 | " --perms OCTAL Set permissions of next argument (--bind-data, --file, etc.)\n" |
374 | 377 | " --size BYTES Set size of next argument (only for --tmpfs)\n" |
375 | 378 | " --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" |
376 | 380 | ); |
377 | 381 | exit (ecode); |
378 | 382 | } |
@@ -2736,6 +2740,10 @@ parse_args_recurse (int *argcp, |
2736 | 2740 | argv += 2; |
2737 | 2741 | argc -= 2; |
2738 | 2742 | } |
| 2743 | + else if (strcmp (arg, "--scope-abstract-af-unix") == 0) |
| 2744 | + { |
| 2745 | + opt_scope_abstract_unix_sockets = true; |
| 2746 | + } |
2739 | 2747 | else if (strcmp (arg, "--") == 0) |
2740 | 2748 | { |
2741 | 2749 | argv += 1; |
@@ -2867,6 +2875,24 @@ namespace_ids_write (int fd, |
2867 | 2875 | } |
2868 | 2876 | } |
2869 | 2877 |
|
| 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 | + |
2870 | 2896 | int |
2871 | 2897 | main (int argc, |
2872 | 2898 | char **argv) |
@@ -3491,6 +3517,23 @@ main (int argc, |
3491 | 3517 | die ("creation of new user namespaces was not disabled as requested"); |
3492 | 3518 | } |
3493 | 3519 |
|
| 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 | + |
3494 | 3537 | /* All privileged ops are done now, so drop caps we don't need */ |
3495 | 3538 | drop_privs (!is_privileged, true); |
3496 | 3539 |
|
|
0 commit comments