mirror of
https://github.com/torvalds/linux.git
synced 2026-11-07 06:17:02 +01:00
selftests/landlock: Add namespace restriction tests
Add tests covering LANDLOCK_PERMISSION_NAMESPACE_USE (namespace creation via unshare/clone and namespace entry via setns) and its interaction with LANDLOCK_PERMISSION_CAPABILITY_USE. Rule validation covers the forward-compatibility contract: unknown CLONE_NEW* bits, including holes, upper-range bits and bit 63, are accepted at rule-add time yet have no runtime effect, because deny-by-default still applies once the domain is enforced. Empty masks, invalid permission selectors and non-zero flags are rejected. Creation tests use FIXTURE_VARIANT to exercise all eight namespace types across allowed/denied and privileged/unprivileged combinations, so every type is shown to reach security_namespace_init(). Stacking tests cover the three per-layer allow/deny combinations that exercise distinct walker paths, and one domain handles both permissions at once. Entry tests subject setns to the same type-based check through security_namespace_install(), across processes, and require both permissions to allow a non-user namespace. The four open_tree(2) and fsmount(2) variants, anonymous and not, all create a mount namespace: they converge in security_namespace_init() with CLONE_NEWNS through different alloc_mnt_ns() paths. A CLONE_NEWUSER | CLONE_NEWUTS unshare under partial-allow rules shows that creation is sequential, so a denial on either type rolls the whole syscall back with EPERM. Audit tests cover allowed and denied creation and entry, per-member quiet masks and the youngest-denying-layer rule. Trace tests cover handled permissions, successful and rejected rules, version history, namespace IDs, and quiet denials, including repeated and unknown-only effective no-ops. Cc: Christian Brauner <brauner@kernel.org> Cc: Günther Noack <gnoack@google.com> Cc: Paul Moore <paul@paul-moore.com> Cc: Serge E. Hallyn <serge@hallyn.com> Link: https://patch.msgid.link/20261014082718.1435842-6-mic@digikod.net Signed-off-by: Mickaël Salaün <mic@digikod.net>
This commit is contained in:
parent
97c8386f1d
commit
de3a636ca5
|
|
@ -9,10 +9,12 @@
|
|||
|
||||
#include <arpa/inet.h>
|
||||
#include <errno.h>
|
||||
#include <linux/nsfs.h>
|
||||
#include <linux/securebits.h>
|
||||
#include <sys/capability.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -130,6 +132,17 @@ static void __maybe_unused clear_ambient_cap(
|
|||
EXPECT_EQ(0, cap_get_ambient(cap));
|
||||
}
|
||||
|
||||
/* Returns true if the current process is in the initial user namespace. */
|
||||
static bool __maybe_unused is_in_init_user_ns(void)
|
||||
{
|
||||
struct stat user_ns;
|
||||
|
||||
if (stat("/proc/self/ns/user", &user_ns))
|
||||
return false;
|
||||
|
||||
return user_ns.st_ino == USER_NS_INIT_INO;
|
||||
}
|
||||
|
||||
/* Receives an FD from a UNIX socket. Returns the received FD, or -errno. */
|
||||
static int __maybe_unused recv_fd(int usock)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ CONFIG_CGROUP_SCHED=y
|
|||
CONFIG_ENABLE_DEFAULT_TRACERS=y
|
||||
CONFIG_FTRACE=y
|
||||
CONFIG_INET=y
|
||||
CONFIG_IPC_NS=y
|
||||
CONFIG_IPV6=y
|
||||
CONFIG_KEYS=y
|
||||
CONFIG_MPTCP=y
|
||||
|
|
@ -12,10 +13,14 @@ CONFIG_MPTCP_IPV6=y
|
|||
CONFIG_NET=y
|
||||
CONFIG_NET_NS=y
|
||||
CONFIG_OVERLAY_FS=y
|
||||
CONFIG_PID_NS=y
|
||||
CONFIG_PROC_FS=y
|
||||
CONFIG_SECURITY=y
|
||||
CONFIG_SECURITY_LANDLOCK=y
|
||||
CONFIG_SHMEM=y
|
||||
CONFIG_SYSFS=y
|
||||
CONFIG_TIME_NS=y
|
||||
CONFIG_TMPFS=y
|
||||
CONFIG_TMPFS_XATTR=y
|
||||
CONFIG_USER_NS=y
|
||||
CONFIG_UTS_NS=y
|
||||
|
|
|
|||
|
|
@ -57,13 +57,6 @@ int renameat2(int olddirfd, const char *oldpath, int newdirfd,
|
|||
}
|
||||
#endif
|
||||
|
||||
#ifndef open_tree
|
||||
int open_tree(int dfd, const char *filename, unsigned int flags)
|
||||
{
|
||||
return syscall(__NR_open_tree, dfd, filename, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
static int sys_execveat(int dirfd, const char *pathname, char *const argv[],
|
||||
char *const envp[], int flags)
|
||||
{
|
||||
|
|
@ -2628,9 +2621,9 @@ TEST_F_FORK(layout1, refer_mount_root_deny)
|
|||
|
||||
/* Creates a mount object from a non-mount point. */
|
||||
set_cap(_metadata, CAP_SYS_ADMIN);
|
||||
root_fd =
|
||||
open_tree(AT_FDCWD, dir_s1d1,
|
||||
AT_EMPTY_PATH | OPEN_TREE_CLONE | OPEN_TREE_CLOEXEC);
|
||||
root_fd = sys_open_tree(AT_FDCWD, dir_s1d1,
|
||||
AT_EMPTY_PATH | OPEN_TREE_CLONE |
|
||||
OPEN_TREE_CLOEXEC);
|
||||
clear_cap(_metadata, CAP_SYS_ADMIN);
|
||||
ASSERT_LE(0, root_fd);
|
||||
|
||||
|
|
|
|||
2647
tools/testing/selftests/landlock/ns_test.c
Normal file
2647
tools/testing/selftests/landlock/ns_test.c
Normal file
File diff suppressed because it is too large
Load Diff
|
|
@ -31,6 +31,8 @@
|
|||
TRACEFS_LANDLOCK_DIR "/landlock_add_rule_path_beneath/enable"
|
||||
#define TRACEFS_ADD_RULE_NET_PORT_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_add_rule_net_port/enable"
|
||||
#define TRACEFS_ADD_RULE_NAMESPACE_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_add_rule_namespace/enable"
|
||||
#define TRACEFS_CHECK_RULE_FS_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_check_rule_inode/enable"
|
||||
#define TRACEFS_CHECK_RULE_NET_ENABLE \
|
||||
|
|
@ -39,6 +41,8 @@
|
|||
TRACEFS_LANDLOCK_DIR "/landlock_deny_access_fs/enable"
|
||||
#define TRACEFS_DENY_ACCESS_NET_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_deny_access_net/enable"
|
||||
#define TRACEFS_DENY_PERMISSION_NAMESPACE_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_deny_permission_namespace/enable"
|
||||
#define TRACEFS_DENY_PTRACE_ENABLE \
|
||||
TRACEFS_LANDLOCK_DIR "/landlock_deny_ptrace/enable"
|
||||
#define TRACEFS_DENY_SCOPE_SIGNAL_ENABLE \
|
||||
|
|
@ -95,6 +99,17 @@
|
|||
"access_rights=[a-z_|]* " \
|
||||
"port=[0-9]\\+$"
|
||||
|
||||
#define REGEX_ADD_RULE_NAMESPACE_VERSION(task, version) \
|
||||
TRACE_PREFIX(task) \
|
||||
"landlock_add_rule_namespace: " \
|
||||
"ruleset=[0-9a-f]\\+\\." version " " \
|
||||
"permissions=[a-z._|]* " \
|
||||
"allowed_namespace_types=0x[0-9a-f]\\+ " \
|
||||
"quiet_namespace_types=0x[0-9a-f]\\+$"
|
||||
|
||||
#define REGEX_ADD_RULE_NAMESPACE(task) \
|
||||
REGEX_ADD_RULE_NAMESPACE_VERSION(task, "[0-9]\\+")
|
||||
|
||||
#define REGEX_CREATE_RULESET(task) \
|
||||
TRACE_PREFIX(task) \
|
||||
"landlock_create_ruleset: " \
|
||||
|
|
@ -148,6 +163,16 @@
|
|||
"blockers=[a-z_|]* " \
|
||||
"port=-\\?[0-9]\\+$"
|
||||
|
||||
#define REGEX_DENY_PERMISSION_NAMESPACE(task) \
|
||||
TRACE_PREFIX(task) \
|
||||
"landlock_deny_permission_namespace: " \
|
||||
"domain=[0-9a-f]\\+ " \
|
||||
"same_exec=[01] " \
|
||||
"logged=[01] " \
|
||||
"blockers=[a-z_|]* " \
|
||||
"namespace_type=0x[0-9a-f]\\+ " \
|
||||
"namespace_id=[0-9]\\+$"
|
||||
|
||||
#define REGEX_DENY_PTRACE(task) \
|
||||
TRACE_PREFIX(task) \
|
||||
"landlock_deny_ptrace: " \
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
#define _GNU_SOURCE
|
||||
#include <linux/landlock.h>
|
||||
#include <linux/sched.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
|
@ -45,3 +46,31 @@ static inline pid_t sys_gettid(void)
|
|||
{
|
||||
return syscall(__NR_gettid);
|
||||
}
|
||||
|
||||
static inline pid_t sys_clone3(struct clone_args *args, size_t size)
|
||||
{
|
||||
return syscall(__NR_clone3, args, size);
|
||||
}
|
||||
|
||||
static inline int sys_open_tree(int dfd, const char *filename,
|
||||
unsigned int flags)
|
||||
{
|
||||
return syscall(__NR_open_tree, dfd, filename, flags);
|
||||
}
|
||||
|
||||
static inline int sys_fsopen(const char *fsname, unsigned int flags)
|
||||
{
|
||||
return syscall(__NR_fsopen, fsname, flags);
|
||||
}
|
||||
|
||||
static inline int sys_fsconfig(int fs_fd, unsigned int cmd, const char *key,
|
||||
const void *value, int aux)
|
||||
{
|
||||
return syscall(__NR_fsconfig, fs_fd, cmd, key, value, aux);
|
||||
}
|
||||
|
||||
static inline int sys_fsmount(int fs_fd, unsigned int flags,
|
||||
unsigned int attr_flags)
|
||||
{
|
||||
return syscall(__NR_fsmount, fs_fd, flags, attr_flags);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user