2021-04-22 15:41:18 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
/*
|
|
|
|
* Landlock LSM - System call implementations and user space interfaces
|
|
|
|
*
|
|
|
|
* Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
|
|
|
|
* Copyright © 2018-2020 ANSSI
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <asm/current.h>
|
|
|
|
#include <linux/anon_inodes.h>
|
|
|
|
#include <linux/build_bug.h>
|
|
|
|
#include <linux/capability.h>
|
|
|
|
#include <linux/compiler_types.h>
|
|
|
|
#include <linux/dcache.h>
|
|
|
|
#include <linux/err.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/path.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/security.h>
|
|
|
|
#include <linux/stddef.h>
|
|
|
|
#include <linux/syscalls.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/uaccess.h>
|
|
|
|
#include <uapi/linux/landlock.h>
|
|
|
|
|
|
|
|
#include "cred.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "limits.h"
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
#include "net.h"
|
2021-04-22 15:41:18 +00:00
|
|
|
#include "ruleset.h"
|
|
|
|
#include "setup.h"
|
|
|
|
|
2024-02-27 11:05:50 +00:00
|
|
|
static bool is_initialized(void)
|
|
|
|
{
|
|
|
|
if (likely(landlock_initialized))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
pr_warn_once(
|
|
|
|
"Disabled but requested by user space. "
|
|
|
|
"You should enable Landlock at boot time: "
|
|
|
|
"https://docs.kernel.org/userspace-api/landlock.html#boot-time-configuration\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:41:18 +00:00
|
|
|
/**
|
|
|
|
* copy_min_struct_from_user - Safe future-proof argument copying
|
|
|
|
*
|
|
|
|
* Extend copy_struct_from_user() to check for consistent user buffer.
|
|
|
|
*
|
|
|
|
* @dst: Kernel space pointer or NULL.
|
|
|
|
* @ksize: Actual size of the data pointed to by @dst.
|
|
|
|
* @ksize_min: Minimal required size to be copied.
|
|
|
|
* @src: User space pointer or NULL.
|
|
|
|
* @usize: (Alleged) size of the data pointed to by @src.
|
|
|
|
*/
|
2022-05-06 16:05:08 +00:00
|
|
|
static __always_inline int
|
|
|
|
copy_min_struct_from_user(void *const dst, const size_t ksize,
|
|
|
|
const size_t ksize_min, const void __user *const src,
|
|
|
|
const size_t usize)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
|
|
|
/* Checks buffer inconsistencies. */
|
|
|
|
BUILD_BUG_ON(!dst);
|
|
|
|
if (!src)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* Checks size ranges. */
|
|
|
|
BUILD_BUG_ON(ksize <= 0);
|
|
|
|
BUILD_BUG_ON(ksize < ksize_min);
|
|
|
|
if (usize < ksize_min)
|
|
|
|
return -EINVAL;
|
|
|
|
if (usize > PAGE_SIZE)
|
|
|
|
return -E2BIG;
|
|
|
|
|
|
|
|
/* Copies user buffer and fills with zeros. */
|
|
|
|
return copy_struct_from_user(dst, ksize, src, usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function only contains arithmetic operations with constants, leading to
|
|
|
|
* BUILD_BUG_ON(). The related code is evaluated and checked at build time,
|
|
|
|
* but it is then ignored thanks to compiler optimizations.
|
|
|
|
*/
|
|
|
|
static void build_check_abi(void)
|
|
|
|
{
|
|
|
|
struct landlock_ruleset_attr ruleset_attr;
|
|
|
|
struct landlock_path_beneath_attr path_beneath_attr;
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
struct landlock_net_port_attr net_port_attr;
|
|
|
|
size_t ruleset_size, path_beneath_size, net_port_size;
|
2021-04-22 15:41:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* For each user space ABI structures, first checks that there is no
|
|
|
|
* hole in them, then checks that all architectures have the same
|
|
|
|
* struct size.
|
|
|
|
*/
|
|
|
|
ruleset_size = sizeof(ruleset_attr.handled_access_fs);
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
ruleset_size += sizeof(ruleset_attr.handled_access_net);
|
2024-09-05 00:13:55 +00:00
|
|
|
ruleset_size += sizeof(ruleset_attr.scoped);
|
2021-04-22 15:41:18 +00:00
|
|
|
BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
|
2024-09-05 00:13:55 +00:00
|
|
|
BUILD_BUG_ON(sizeof(ruleset_attr) != 24);
|
2021-04-22 15:41:18 +00:00
|
|
|
|
|
|
|
path_beneath_size = sizeof(path_beneath_attr.allowed_access);
|
|
|
|
path_beneath_size += sizeof(path_beneath_attr.parent_fd);
|
|
|
|
BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
|
|
|
|
BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
|
|
|
|
net_port_size = sizeof(net_port_attr.allowed_access);
|
|
|
|
net_port_size += sizeof(net_port_attr.port);
|
|
|
|
BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
|
|
|
|
BUILD_BUG_ON(sizeof(net_port_attr) != 16);
|
2021-04-22 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ruleset handling */
|
|
|
|
|
|
|
|
static int fop_ruleset_release(struct inode *const inode,
|
2022-05-06 16:05:08 +00:00
|
|
|
struct file *const filp)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
|
|
|
struct landlock_ruleset *ruleset = filp->private_data;
|
|
|
|
|
|
|
|
landlock_put_ruleset(ruleset);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fop_dummy_read(struct file *const filp, char __user *const buf,
|
2022-05-06 16:05:08 +00:00
|
|
|
const size_t size, loff_t *const ppos)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
|
|
|
/* Dummy handler to enable FMODE_CAN_READ. */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t fop_dummy_write(struct file *const filp,
|
2022-05-06 16:05:08 +00:00
|
|
|
const char __user *const buf, const size_t size,
|
|
|
|
loff_t *const ppos)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
|
|
|
/* Dummy handler to enable FMODE_CAN_WRITE. */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A ruleset file descriptor enables to build a ruleset by adding (i.e.
|
|
|
|
* writing) rule after rule, without relying on the task's context. This
|
|
|
|
* reentrant design is also used in a read way to enforce the ruleset on the
|
|
|
|
* current task.
|
|
|
|
*/
|
|
|
|
static const struct file_operations ruleset_fops = {
|
|
|
|
.release = fop_ruleset_release,
|
|
|
|
.read = fop_dummy_read,
|
|
|
|
.write = fop_dummy_write,
|
|
|
|
};
|
|
|
|
|
2024-09-05 00:13:55 +00:00
|
|
|
#define LANDLOCK_ABI_VERSION 6
|
2021-04-22 15:41:23 +00:00
|
|
|
|
2021-04-22 15:41:18 +00:00
|
|
|
/**
|
|
|
|
* sys_landlock_create_ruleset - Create a new ruleset
|
|
|
|
*
|
|
|
|
* @attr: Pointer to a &struct landlock_ruleset_attr identifying the scope of
|
|
|
|
* the new ruleset.
|
|
|
|
* @size: Size of the pointed &struct landlock_ruleset_attr (needed for
|
|
|
|
* backward and forward compatibility).
|
2021-04-22 15:41:23 +00:00
|
|
|
* @flags: Supported value: %LANDLOCK_CREATE_RULESET_VERSION.
|
2021-04-22 15:41:18 +00:00
|
|
|
*
|
|
|
|
* This system call enables to create a new Landlock ruleset, and returns the
|
|
|
|
* related file descriptor on success.
|
|
|
|
*
|
2021-04-22 15:41:23 +00:00
|
|
|
* If @flags is %LANDLOCK_CREATE_RULESET_VERSION and @attr is NULL and @size is
|
|
|
|
* 0, then the returned value is the highest supported Landlock ABI version
|
|
|
|
* (starting at 1).
|
|
|
|
*
|
2021-04-22 15:41:18 +00:00
|
|
|
* Possible returned errors are:
|
|
|
|
*
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
|
2024-09-05 00:13:55 +00:00
|
|
|
* - %EINVAL: unknown @flags, or unknown access, or unknown scope, or too small @size;
|
|
|
|
* - %E2BIG: @attr or @size inconsistencies;
|
|
|
|
* - %EFAULT: @attr or @size inconsistencies;
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %ENOMSG: empty &landlock_ruleset_attr.handled_access_fs.
|
2021-04-22 15:41:18 +00:00
|
|
|
*/
|
|
|
|
SYSCALL_DEFINE3(landlock_create_ruleset,
|
|
|
|
const struct landlock_ruleset_attr __user *const, attr,
|
|
|
|
const size_t, size, const __u32, flags)
|
|
|
|
{
|
|
|
|
struct landlock_ruleset_attr ruleset_attr;
|
|
|
|
struct landlock_ruleset *ruleset;
|
|
|
|
int err, ruleset_fd;
|
|
|
|
|
|
|
|
/* Build-time checks. */
|
|
|
|
build_check_abi();
|
|
|
|
|
2024-02-27 11:05:50 +00:00
|
|
|
if (!is_initialized())
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2021-04-22 15:41:23 +00:00
|
|
|
if (flags) {
|
2022-05-06 16:05:08 +00:00
|
|
|
if ((flags == LANDLOCK_CREATE_RULESET_VERSION) && !attr &&
|
|
|
|
!size)
|
2021-04-22 15:41:23 +00:00
|
|
|
return LANDLOCK_ABI_VERSION;
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EINVAL;
|
2021-04-22 15:41:23 +00:00
|
|
|
}
|
2021-04-22 15:41:18 +00:00
|
|
|
|
|
|
|
/* Copies raw user space buffer. */
|
|
|
|
err = copy_min_struct_from_user(&ruleset_attr, sizeof(ruleset_attr),
|
2022-05-06 16:05:08 +00:00
|
|
|
offsetofend(typeof(ruleset_attr),
|
|
|
|
handled_access_fs),
|
|
|
|
attr, size);
|
2021-04-22 15:41:18 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Checks content (and 32-bits cast). */
|
|
|
|
if ((ruleset_attr.handled_access_fs | LANDLOCK_MASK_ACCESS_FS) !=
|
2022-05-06 16:05:08 +00:00
|
|
|
LANDLOCK_MASK_ACCESS_FS)
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
/* Checks network content (and 32-bits cast). */
|
|
|
|
if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
|
|
|
|
LANDLOCK_MASK_ACCESS_NET)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2024-09-05 00:13:55 +00:00
|
|
|
/* Checks IPC scoping content (and 32-bits cast). */
|
|
|
|
if ((ruleset_attr.scoped | LANDLOCK_MASK_SCOPE) != LANDLOCK_MASK_SCOPE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-04-22 15:41:18 +00:00
|
|
|
/* Checks arguments and transforms to kernel struct. */
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
|
2024-09-05 00:13:55 +00:00
|
|
|
ruleset_attr.handled_access_net,
|
|
|
|
ruleset_attr.scoped);
|
2021-04-22 15:41:18 +00:00
|
|
|
if (IS_ERR(ruleset))
|
|
|
|
return PTR_ERR(ruleset);
|
|
|
|
|
|
|
|
/* Creates anonymous FD referring to the ruleset. */
|
2021-10-11 13:37:04 +00:00
|
|
|
ruleset_fd = anon_inode_getfd("[landlock-ruleset]", &ruleset_fops,
|
2022-05-06 16:05:08 +00:00
|
|
|
ruleset, O_RDWR | O_CLOEXEC);
|
2021-04-22 15:41:18 +00:00
|
|
|
if (ruleset_fd < 0)
|
|
|
|
landlock_put_ruleset(ruleset);
|
|
|
|
return ruleset_fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an owned ruleset from a FD. It is thus needed to call
|
|
|
|
* landlock_put_ruleset() on the return value.
|
|
|
|
*/
|
|
|
|
static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
|
2022-05-06 16:05:08 +00:00
|
|
|
const fmode_t mode)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
2024-07-20 00:17:58 +00:00
|
|
|
CLASS(fd, ruleset_f)(fd);
|
2021-04-22 15:41:18 +00:00
|
|
|
struct landlock_ruleset *ruleset;
|
|
|
|
|
2024-07-20 00:17:58 +00:00
|
|
|
if (fd_empty(ruleset_f))
|
2021-04-22 15:41:18 +00:00
|
|
|
return ERR_PTR(-EBADF);
|
|
|
|
|
|
|
|
/* Checks FD type and access right. */
|
2024-07-20 00:17:58 +00:00
|
|
|
if (fd_file(ruleset_f)->f_op != &ruleset_fops)
|
|
|
|
return ERR_PTR(-EBADFD);
|
|
|
|
if (!(fd_file(ruleset_f)->f_mode & mode))
|
|
|
|
return ERR_PTR(-EPERM);
|
2024-05-31 18:12:01 +00:00
|
|
|
ruleset = fd_file(ruleset_f)->private_data;
|
2024-07-20 00:17:58 +00:00
|
|
|
if (WARN_ON_ONCE(ruleset->num_layers != 1))
|
|
|
|
return ERR_PTR(-EINVAL);
|
2021-04-22 15:41:18 +00:00
|
|
|
landlock_get_ruleset(ruleset);
|
|
|
|
return ruleset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Path handling */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @path: Must call put_path(@path) after the call if it succeeded.
|
|
|
|
*/
|
|
|
|
static int get_path_from_fd(const s32 fd, struct path *const path)
|
|
|
|
{
|
2024-06-01 02:45:26 +00:00
|
|
|
CLASS(fd_raw, f)(fd);
|
2021-04-22 15:41:18 +00:00
|
|
|
|
2022-05-06 16:05:08 +00:00
|
|
|
BUILD_BUG_ON(!__same_type(
|
|
|
|
fd, ((struct landlock_path_beneath_attr *)NULL)->parent_fd));
|
2021-04-22 15:41:18 +00:00
|
|
|
|
2024-06-01 02:45:26 +00:00
|
|
|
if (fd_empty(f))
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EBADF;
|
|
|
|
/*
|
|
|
|
* Forbids ruleset FDs, internal filesystems (e.g. nsfs), including
|
|
|
|
* pseudo filesystems that will never be mountable (e.g. sockfs,
|
|
|
|
* pipefs).
|
|
|
|
*/
|
2024-05-31 18:12:01 +00:00
|
|
|
if ((fd_file(f)->f_op == &ruleset_fops) ||
|
|
|
|
(fd_file(f)->f_path.mnt->mnt_flags & MNT_INTERNAL) ||
|
|
|
|
(fd_file(f)->f_path.dentry->d_sb->s_flags & SB_NOUSER) ||
|
|
|
|
d_is_negative(fd_file(f)->f_path.dentry) ||
|
2024-06-01 02:45:26 +00:00
|
|
|
IS_PRIVATE(d_backing_inode(fd_file(f)->f_path.dentry)))
|
|
|
|
return -EBADFD;
|
|
|
|
|
2024-05-31 18:12:01 +00:00
|
|
|
*path = fd_file(f)->f_path;
|
2021-04-22 15:41:18 +00:00
|
|
|
path_get(path);
|
2024-06-01 02:45:26 +00:00
|
|
|
return 0;
|
2021-04-22 15:41:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 01:47:46 +00:00
|
|
|
static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
|
|
|
|
const void __user *const rule_attr)
|
|
|
|
{
|
|
|
|
struct landlock_path_beneath_attr path_beneath_attr;
|
|
|
|
struct path path;
|
|
|
|
int res, err;
|
|
|
|
access_mask_t mask;
|
|
|
|
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
/* Copies raw user space buffer. */
|
2023-10-26 01:47:46 +00:00
|
|
|
res = copy_from_user(&path_beneath_attr, rule_attr,
|
|
|
|
sizeof(path_beneath_attr));
|
|
|
|
if (res)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Informs about useless rule: empty allowed_access (i.e. deny rules)
|
|
|
|
* are ignored in path walks.
|
|
|
|
*/
|
|
|
|
if (!path_beneath_attr.allowed_access)
|
|
|
|
return -ENOMSG;
|
|
|
|
|
|
|
|
/* Checks that allowed_access matches the @ruleset constraints. */
|
2024-11-09 11:08:54 +00:00
|
|
|
mask = ruleset->access_masks[0].fs;
|
2023-10-26 01:47:46 +00:00
|
|
|
if ((path_beneath_attr.allowed_access | mask) != mask)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Gets and checks the new rule. */
|
|
|
|
err = get_path_from_fd(path_beneath_attr.parent_fd, &path);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
/* Imports the new rule. */
|
|
|
|
err = landlock_append_fs_rule(ruleset, &path,
|
|
|
|
path_beneath_attr.allowed_access);
|
|
|
|
path_put(&path);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
static int add_rule_net_port(struct landlock_ruleset *ruleset,
|
|
|
|
const void __user *const rule_attr)
|
|
|
|
{
|
|
|
|
struct landlock_net_port_attr net_port_attr;
|
|
|
|
int res;
|
|
|
|
access_mask_t mask;
|
|
|
|
|
|
|
|
/* Copies raw user space buffer. */
|
|
|
|
res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
|
|
|
|
if (res)
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Informs about useless rule: empty allowed_access (i.e. deny rules)
|
|
|
|
* are ignored by network actions.
|
|
|
|
*/
|
|
|
|
if (!net_port_attr.allowed_access)
|
|
|
|
return -ENOMSG;
|
|
|
|
|
|
|
|
/* Checks that allowed_access matches the @ruleset constraints. */
|
|
|
|
mask = landlock_get_net_access_mask(ruleset, 0);
|
|
|
|
if ((net_port_attr.allowed_access | mask) != mask)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Denies inserting a rule with port greater than 65535. */
|
|
|
|
if (net_port_attr.port > U16_MAX)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Imports the new rule. */
|
|
|
|
return landlock_append_net_rule(ruleset, net_port_attr.port,
|
|
|
|
net_port_attr.allowed_access);
|
|
|
|
}
|
|
|
|
|
2021-04-22 15:41:18 +00:00
|
|
|
/**
|
|
|
|
* sys_landlock_add_rule - Add a new rule to a ruleset
|
|
|
|
*
|
|
|
|
* @ruleset_fd: File descriptor tied to the ruleset that should be extended
|
|
|
|
* with the new rule.
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
* @rule_type: Identify the structure type pointed to by @rule_attr:
|
|
|
|
* %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
|
2024-07-15 16:03:29 +00:00
|
|
|
* @rule_attr: Pointer to a rule (matching the @rule_type).
|
2021-04-22 15:41:18 +00:00
|
|
|
* @flags: Must be 0.
|
|
|
|
*
|
|
|
|
* This system call enables to define a new rule and add it to an existing
|
|
|
|
* ruleset.
|
|
|
|
*
|
|
|
|
* Possible returned errors are:
|
|
|
|
*
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
* - %EAFNOSUPPORT: @rule_type is %LANDLOCK_RULE_NET_PORT but TCP/IP is not
|
|
|
|
* supported by the running kernel;
|
2024-07-15 16:03:29 +00:00
|
|
|
* - %EINVAL: @flags is not 0;
|
|
|
|
* - %EINVAL: The rule accesses are inconsistent (i.e.
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
* &landlock_path_beneath_attr.allowed_access or
|
2024-07-15 16:03:29 +00:00
|
|
|
* &landlock_net_port_attr.allowed_access is not a subset of the ruleset
|
|
|
|
* handled accesses)
|
|
|
|
* - %EINVAL: &landlock_net_port_attr.port is greater than 65535;
|
|
|
|
* - %ENOMSG: Empty accesses (e.g. &landlock_path_beneath_attr.allowed_access is
|
|
|
|
* 0);
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EBADF: @ruleset_fd is not a file descriptor for the current thread, or a
|
2021-04-22 15:41:18 +00:00
|
|
|
* member of @rule_attr is not a file descriptor as expected;
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EBADFD: @ruleset_fd is not a ruleset file descriptor, or a member of
|
2022-05-06 16:08:11 +00:00
|
|
|
* @rule_attr is not the expected file descriptor type;
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EPERM: @ruleset_fd has no write access to the underlying ruleset;
|
2024-07-15 16:03:29 +00:00
|
|
|
* - %EFAULT: @rule_attr was not a valid address.
|
2021-04-22 15:41:18 +00:00
|
|
|
*/
|
2022-05-06 16:05:08 +00:00
|
|
|
SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
|
|
|
|
const enum landlock_rule_type, rule_type,
|
2021-04-22 15:41:18 +00:00
|
|
|
const void __user *const, rule_attr, const __u32, flags)
|
|
|
|
{
|
|
|
|
struct landlock_ruleset *ruleset;
|
2023-10-26 01:47:46 +00:00
|
|
|
int err;
|
2021-04-22 15:41:18 +00:00
|
|
|
|
2024-02-27 11:05:50 +00:00
|
|
|
if (!is_initialized())
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
/* No flag for now. */
|
|
|
|
if (flags)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Gets and checks the ruleset. */
|
|
|
|
ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
|
|
|
|
if (IS_ERR(ruleset))
|
|
|
|
return PTR_ERR(ruleset);
|
|
|
|
|
2023-10-26 01:47:46 +00:00
|
|
|
switch (rule_type) {
|
|
|
|
case LANDLOCK_RULE_PATH_BENEATH:
|
|
|
|
err = add_rule_path_beneath(ruleset, rule_attr);
|
|
|
|
break;
|
landlock: Support network rules with TCP bind and connect
Add network rules support in the ruleset management helpers and the
landlock_create_ruleset() syscall. Extend user space API to support
network actions:
* Add new network access rights: LANDLOCK_ACCESS_NET_BIND_TCP and
LANDLOCK_ACCESS_NET_CONNECT_TCP.
* Add a new network rule type: LANDLOCK_RULE_NET_PORT tied to struct
landlock_net_port_attr. The allowed_access field contains the network
access rights, and the port field contains the port value according to
the controlled protocol. This field can take up to a 64-bit value
but the maximum value depends on the related protocol (e.g. 16-bit
value for TCP). Network port is in host endianness [1].
* Add a new handled_access_net field to struct landlock_ruleset_attr
that contains network access rights.
* Increment the Landlock ABI version to 4.
Implement socket_bind() and socket_connect() LSM hooks, which enable
to control TCP socket binding and connection to specific ports.
Expand access_masks_t from u16 to u32 to be able to store network access
rights alongside filesystem access rights for rulesets' handled access
rights.
Access rights are not tied to socket file descriptors but checked at
bind() or connect() call time against the caller's Landlock domain. For
the filesystem, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer
a newly created socket will give access to. Indeed, we need to wait for
a connect or bind request to identify the use case for this socket.
Likewise a directory file descriptor may enable to open another file
(i.e. a new data item), but this opening is also restricted by the
caller's domain, not the file descriptor's access rights [2].
[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20231026014751.414649-9-konstantin.meskhidze@huawei.com
[mic: Extend commit message, fix typo in comments, and specify
endianness in the documentation]
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2023-10-26 01:47:47 +00:00
|
|
|
case LANDLOCK_RULE_NET_PORT:
|
|
|
|
err = add_rule_net_port(ruleset, rule_attr);
|
|
|
|
break;
|
2023-10-26 01:47:46 +00:00
|
|
|
default:
|
2021-04-22 15:41:18 +00:00
|
|
|
err = -EINVAL;
|
2023-10-26 01:47:46 +00:00
|
|
|
break;
|
2021-04-22 15:41:18 +00:00
|
|
|
}
|
|
|
|
landlock_put_ruleset(ruleset);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Enforcement */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sys_landlock_restrict_self - Enforce a ruleset on the calling thread
|
|
|
|
*
|
|
|
|
* @ruleset_fd: File descriptor tied to the ruleset to merge with the target.
|
|
|
|
* @flags: Must be 0.
|
|
|
|
*
|
|
|
|
* This system call enables to enforce a Landlock ruleset on the current
|
2022-09-23 15:42:07 +00:00
|
|
|
* thread. Enforcing a ruleset requires that the task has %CAP_SYS_ADMIN in its
|
2021-04-22 15:41:18 +00:00
|
|
|
* namespace or is running with no_new_privs. This avoids scenarios where
|
|
|
|
* unprivileged tasks can affect the behavior of privileged children.
|
|
|
|
*
|
|
|
|
* Possible returned errors are:
|
|
|
|
*
|
2022-09-23 15:42:07 +00:00
|
|
|
* - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
|
|
|
|
* - %EINVAL: @flags is not 0.
|
|
|
|
* - %EBADF: @ruleset_fd is not a file descriptor for the current thread;
|
|
|
|
* - %EBADFD: @ruleset_fd is not a ruleset file descriptor;
|
|
|
|
* - %EPERM: @ruleset_fd has no read access to the underlying ruleset, or the
|
2021-04-22 15:41:18 +00:00
|
|
|
* current thread is not running with no_new_privs, or it doesn't have
|
2022-09-23 15:42:07 +00:00
|
|
|
* %CAP_SYS_ADMIN in its namespace.
|
|
|
|
* - %E2BIG: The maximum number of stacked rulesets is reached for the current
|
2021-04-22 15:41:18 +00:00
|
|
|
* thread.
|
|
|
|
*/
|
2022-05-06 16:05:08 +00:00
|
|
|
SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
|
|
|
|
flags)
|
2021-04-22 15:41:18 +00:00
|
|
|
{
|
|
|
|
struct landlock_ruleset *new_dom, *ruleset;
|
|
|
|
struct cred *new_cred;
|
|
|
|
struct landlock_cred_security *new_llcred;
|
|
|
|
int err;
|
|
|
|
|
2024-02-27 11:05:50 +00:00
|
|
|
if (!is_initialized())
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Similar checks as for seccomp(2), except that an -EPERM may be
|
|
|
|
* returned.
|
|
|
|
*/
|
|
|
|
if (!task_no_new_privs(current) &&
|
2022-05-06 16:05:08 +00:00
|
|
|
!ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
|
2021-04-22 15:41:18 +00:00
|
|
|
return -EPERM;
|
|
|
|
|
2022-05-06 16:08:19 +00:00
|
|
|
/* No flag for now. */
|
|
|
|
if (flags)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2021-04-22 15:41:18 +00:00
|
|
|
/* Gets and checks the ruleset. */
|
|
|
|
ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
|
|
|
|
if (IS_ERR(ruleset))
|
|
|
|
return PTR_ERR(ruleset);
|
|
|
|
|
|
|
|
/* Prepares new credentials. */
|
|
|
|
new_cred = prepare_creds();
|
|
|
|
if (!new_cred) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out_put_ruleset;
|
|
|
|
}
|
|
|
|
new_llcred = landlock_cred(new_cred);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is no possible race condition while copying and manipulating
|
|
|
|
* the current credentials because they are dedicated per thread.
|
|
|
|
*/
|
|
|
|
new_dom = landlock_merge_ruleset(new_llcred->domain, ruleset);
|
|
|
|
if (IS_ERR(new_dom)) {
|
|
|
|
err = PTR_ERR(new_dom);
|
|
|
|
goto out_put_creds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Replaces the old (prepared) domain. */
|
|
|
|
landlock_put_ruleset(new_llcred->domain);
|
|
|
|
new_llcred->domain = new_dom;
|
|
|
|
|
|
|
|
landlock_put_ruleset(ruleset);
|
|
|
|
return commit_creds(new_cred);
|
|
|
|
|
|
|
|
out_put_creds:
|
|
|
|
abort_creds(new_cred);
|
|
|
|
|
|
|
|
out_put_ruleset:
|
|
|
|
landlock_put_ruleset(ruleset);
|
|
|
|
return err;
|
|
|
|
}
|