2021-04-22 15:41:12 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
|
|
|
/*
|
|
|
|
* Landlock LSM - Ruleset management
|
|
|
|
*
|
|
|
|
* Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
|
|
|
|
* Copyright © 2018-2020 ANSSI
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _SECURITY_LANDLOCK_RULESET_H
|
|
|
|
#define _SECURITY_LANDLOCK_RULESET_H
|
|
|
|
|
2022-05-06 16:10:51 +00:00
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/build_bug.h>
|
2024-11-09 11:08:54 +00:00
|
|
|
#include <linux/kernel.h>
|
2021-04-22 15:41:12 +00:00
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/rbtree.h>
|
|
|
|
#include <linux/refcount.h>
|
|
|
|
#include <linux/workqueue.h>
|
2023-10-26 01:47:41 +00:00
|
|
|
#include <uapi/linux/landlock.h>
|
2021-04-22 15:41:12 +00:00
|
|
|
|
2022-05-06 16:10:51 +00:00
|
|
|
#include "limits.h"
|
2021-04-22 15:41:12 +00:00
|
|
|
#include "object.h"
|
|
|
|
|
2023-10-26 01:47:41 +00:00
|
|
|
/*
|
|
|
|
* All access rights that are denied by default whether they are handled or not
|
|
|
|
* by a ruleset/layer. This must be ORed with all ruleset->access_masks[]
|
|
|
|
* entries when we need to get the absolute handled access masks.
|
|
|
|
*/
|
|
|
|
/* clang-format off */
|
|
|
|
#define LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
|
|
|
|
LANDLOCK_ACCESS_FS_REFER)
|
|
|
|
/* clang-format on */
|
|
|
|
|
2022-05-06 16:10:51 +00:00
|
|
|
typedef u16 access_mask_t;
|
|
|
|
/* Makes sure all filesystem access rights can be stored. */
|
|
|
|
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_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
|
|
|
/* Makes sure all network access rights can be stored. */
|
|
|
|
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
|
2024-09-05 00:13:55 +00:00
|
|
|
/* Makes sure all scoped rights can be stored. */
|
|
|
|
static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
|
landlock: Fix same-layer rule unions
The original behavior was to check if the full set of requested accesses
was allowed by at least a rule of every relevant layer. This didn't
take into account requests for multiple accesses and same-layer rules
allowing the union of these accesses in a complementary way. As a
result, multiple accesses requested on a file hierarchy matching rules
that, together, allowed these accesses, but without a unique rule
allowing all of them, was illegitimately denied. This case should be
rare in practice and it can only be triggered by the path_rename or
file_open hook implementations.
For instance, if, for the same layer, a rule allows execution
beneath /a/b and another rule allows read beneath /a, requesting access
to read and execute at the same time for /a/b should be allowed for this
layer.
This was an inconsistency because the union of same-layer rule accesses
was already allowed if requested once at a time anyway.
This fix changes the way allowed accesses are gathered over a path walk.
To take into account all these rule accesses, we store in a matrix all
layer granting the set of requested accesses, according to the handled
accesses. To avoid heap allocation, we use an array on the stack which
is 2*13 bytes. A following commit bringing the LANDLOCK_ACCESS_FS_REFER
access right will increase this size to reach 112 bytes (2*14*4) in case
of link or rename actions.
Add a new layout1.layer_rule_unions test to check that accesses from
different rules pertaining to the same layer are ORed in a file
hierarchy. Also test that it is not the case for rules from different
layers.
Reviewed-by: Paul Moore <paul@paul-moore.com>
Link: https://lore.kernel.org/r/20220506161102.525323-5-mic@digikod.net
Cc: stable@vger.kernel.org
Signed-off-by: Mickaël Salaün <mic@digikod.net>
2022-05-06 16:10:54 +00:00
|
|
|
/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
|
|
|
|
static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
|
2022-05-06 16:10:51 +00:00
|
|
|
|
2023-10-26 01:47:40 +00:00
|
|
|
/* Ruleset access masks. */
|
2024-06-10 08:21:15 +00:00
|
|
|
struct access_masks {
|
|
|
|
access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
|
|
|
|
access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
|
2024-09-05 00:13:55 +00:00
|
|
|
access_mask_t scope : LANDLOCK_NUM_SCOPE;
|
2024-06-10 08:21:15 +00:00
|
|
|
};
|
2023-10-26 01:47:40 +00:00
|
|
|
|
2024-11-09 11:08:54 +00:00
|
|
|
union access_masks_all {
|
|
|
|
struct access_masks masks;
|
|
|
|
u32 all;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Makes sure all fields are covered. */
|
|
|
|
static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
|
|
|
|
sizeof(typeof_member(union access_masks_all, all)));
|
|
|
|
|
2022-05-06 16:10:52 +00:00
|
|
|
typedef u16 layer_mask_t;
|
|
|
|
/* Makes sure all layers can be checked. */
|
|
|
|
static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
|
|
|
|
|
2021-04-22 15:41:12 +00:00
|
|
|
/**
|
|
|
|
* struct landlock_layer - Access rights for a given layer
|
|
|
|
*/
|
|
|
|
struct landlock_layer {
|
|
|
|
/**
|
|
|
|
* @level: Position of this layer in the layer stack.
|
|
|
|
*/
|
|
|
|
u16 level;
|
|
|
|
/**
|
|
|
|
* @access: Bitfield of allowed actions on the kernel object. They are
|
|
|
|
* relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
|
|
|
|
*/
|
2022-05-06 16:10:51 +00:00
|
|
|
access_mask_t access;
|
2021-04-22 15:41:12 +00:00
|
|
|
};
|
|
|
|
|
2023-10-26 01:47:42 +00:00
|
|
|
/**
|
|
|
|
* union landlock_key - Key of a ruleset's red-black tree
|
|
|
|
*/
|
|
|
|
union landlock_key {
|
|
|
|
/**
|
|
|
|
* @object: Pointer to identify a kernel object (e.g. an inode).
|
|
|
|
*/
|
|
|
|
struct landlock_object *object;
|
|
|
|
/**
|
|
|
|
* @data: Raw data to identify an arbitrary 32-bit value
|
|
|
|
* (e.g. a TCP port).
|
|
|
|
*/
|
|
|
|
uintptr_t data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* enum landlock_key_type - Type of &union landlock_key
|
|
|
|
*/
|
|
|
|
enum landlock_key_type {
|
|
|
|
/**
|
|
|
|
* @LANDLOCK_KEY_INODE: Type of &landlock_ruleset.root_inode's node
|
|
|
|
* keys.
|
|
|
|
*/
|
|
|
|
LANDLOCK_KEY_INODE = 1,
|
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_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
|
|
|
|
* node keys.
|
|
|
|
*/
|
|
|
|
LANDLOCK_KEY_NET_PORT,
|
2023-10-26 01:47:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct landlock_id - Unique rule identifier for a ruleset
|
|
|
|
*/
|
|
|
|
struct landlock_id {
|
|
|
|
/**
|
|
|
|
* @key: Identifies either a kernel object (e.g. an inode) or
|
|
|
|
* a raw value (e.g. a TCP port).
|
|
|
|
*/
|
|
|
|
union landlock_key key;
|
|
|
|
/**
|
|
|
|
* @type: Type of a landlock_ruleset's root tree.
|
|
|
|
*/
|
|
|
|
const enum landlock_key_type type;
|
|
|
|
};
|
|
|
|
|
2021-04-22 15:41:12 +00:00
|
|
|
/**
|
|
|
|
* struct landlock_rule - Access rights tied to an object
|
|
|
|
*/
|
|
|
|
struct landlock_rule {
|
|
|
|
/**
|
|
|
|
* @node: Node in the ruleset's red-black tree.
|
|
|
|
*/
|
|
|
|
struct rb_node node;
|
|
|
|
/**
|
2023-10-26 01:47:42 +00:00
|
|
|
* @key: A union to identify either a kernel object (e.g. an inode) or
|
|
|
|
* a raw data value (e.g. a network socket port). This is used as a key
|
|
|
|
* for this ruleset element. The pointer is set once and never
|
|
|
|
* modified. It always points to an allocated object because each rule
|
|
|
|
* increments the refcount of its object.
|
2021-04-22 15:41:12 +00:00
|
|
|
*/
|
2023-10-26 01:47:42 +00:00
|
|
|
union landlock_key key;
|
2021-04-22 15:41:12 +00:00
|
|
|
/**
|
|
|
|
* @num_layers: Number of entries in @layers.
|
|
|
|
*/
|
|
|
|
u32 num_layers;
|
|
|
|
/**
|
|
|
|
* @layers: Stack of layers, from the latest to the newest, implemented
|
|
|
|
* as a flexible array member (FAM).
|
|
|
|
*/
|
2023-08-17 21:03:01 +00:00
|
|
|
struct landlock_layer layers[] __counted_by(num_layers);
|
2021-04-22 15:41:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct landlock_hierarchy - Node in a ruleset hierarchy
|
|
|
|
*/
|
|
|
|
struct landlock_hierarchy {
|
|
|
|
/**
|
|
|
|
* @parent: Pointer to the parent node, or NULL if it is a root
|
|
|
|
* Landlock domain.
|
|
|
|
*/
|
|
|
|
struct landlock_hierarchy *parent;
|
|
|
|
/**
|
|
|
|
* @usage: Number of potential children domains plus their parent
|
|
|
|
* domain.
|
|
|
|
*/
|
|
|
|
refcount_t usage;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct landlock_ruleset - Landlock ruleset
|
|
|
|
*
|
|
|
|
* This data structure must contain unique entries, be updatable, and quick to
|
|
|
|
* match an object.
|
|
|
|
*/
|
|
|
|
struct landlock_ruleset {
|
|
|
|
/**
|
2023-10-26 01:47:42 +00:00
|
|
|
* @root_inode: Root of a red-black tree containing &struct
|
|
|
|
* landlock_rule nodes with inode object. Once a ruleset is tied to a
|
|
|
|
* process (i.e. as a domain), this tree is immutable until @usage
|
|
|
|
* reaches zero.
|
2021-04-22 15:41:12 +00:00
|
|
|
*/
|
2023-10-26 01:47:42 +00:00
|
|
|
struct rb_root root_inode;
|
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
|
|
|
|
|
|
|
#if IS_ENABLED(CONFIG_INET)
|
|
|
|
/**
|
|
|
|
* @root_net_port: Root of a red-black tree containing &struct
|
|
|
|
* landlock_rule nodes with network port. Once a ruleset is tied to a
|
|
|
|
* process (i.e. as a domain), this tree is immutable until @usage
|
|
|
|
* reaches zero.
|
|
|
|
*/
|
|
|
|
struct rb_root root_net_port;
|
|
|
|
#endif /* IS_ENABLED(CONFIG_INET) */
|
|
|
|
|
2021-04-22 15:41:12 +00:00
|
|
|
/**
|
|
|
|
* @hierarchy: Enables hierarchy identification even when a parent
|
|
|
|
* domain vanishes. This is needed for the ptrace protection.
|
|
|
|
*/
|
|
|
|
struct landlock_hierarchy *hierarchy;
|
|
|
|
union {
|
|
|
|
/**
|
|
|
|
* @work_free: Enables to free a ruleset within a lockless
|
|
|
|
* section. This is only used by
|
|
|
|
* landlock_put_ruleset_deferred() when @usage reaches zero.
|
|
|
|
* The fields @lock, @usage, @num_rules, @num_layers and
|
2023-10-26 01:47:40 +00:00
|
|
|
* @access_masks are then unused.
|
2021-04-22 15:41:12 +00:00
|
|
|
*/
|
|
|
|
struct work_struct work_free;
|
|
|
|
struct {
|
|
|
|
/**
|
|
|
|
* @lock: Protects against concurrent modifications of
|
|
|
|
* @root, if @usage is greater than zero.
|
|
|
|
*/
|
|
|
|
struct mutex lock;
|
|
|
|
/**
|
|
|
|
* @usage: Number of processes (i.e. domains) or file
|
|
|
|
* descriptors referencing this ruleset.
|
|
|
|
*/
|
|
|
|
refcount_t usage;
|
|
|
|
/**
|
|
|
|
* @num_rules: Number of non-overlapping (i.e. not for
|
|
|
|
* the same object) rules in this ruleset.
|
|
|
|
*/
|
|
|
|
u32 num_rules;
|
|
|
|
/**
|
|
|
|
* @num_layers: Number of layers that are used in this
|
|
|
|
* ruleset. This enables to check that all the layers
|
|
|
|
* allow an access request. A value of 0 identifies a
|
|
|
|
* non-merged ruleset (i.e. not a domain).
|
|
|
|
*/
|
|
|
|
u32 num_layers;
|
|
|
|
/**
|
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
|
|
|
* @access_masks: Contains the subset of filesystem and
|
|
|
|
* network actions that are restricted by a ruleset.
|
|
|
|
* A domain saves all layers of merged rulesets in a
|
|
|
|
* stack (FAM), starting from the first layer to the
|
|
|
|
* last one. These layers are used when merging
|
|
|
|
* rulesets, for user space backward compatibility
|
|
|
|
* (i.e. future-proof), and to properly handle merged
|
2021-04-22 15:41:12 +00:00
|
|
|
* rulesets without overlapping access rights. These
|
|
|
|
* layers are set once and never changed for the
|
|
|
|
* lifetime of the ruleset.
|
|
|
|
*/
|
2024-06-10 08:21:15 +00:00
|
|
|
struct access_masks access_masks[];
|
2021-04-22 15:41:12 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-05-06 16:10:51 +00:00
|
|
|
struct landlock_ruleset *
|
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_create_ruleset(const access_mask_t access_mask_fs,
|
2024-09-05 00:13:55 +00:00
|
|
|
const access_mask_t access_mask_net,
|
|
|
|
const access_mask_t scope_mask);
|
2021-04-22 15:41:12 +00:00
|
|
|
|
|
|
|
void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
|
|
|
|
void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
|
|
|
|
|
|
|
|
int landlock_insert_rule(struct landlock_ruleset *const ruleset,
|
2023-10-26 01:47:42 +00:00
|
|
|
const struct landlock_id id,
|
2022-05-06 16:10:51 +00:00
|
|
|
const access_mask_t access);
|
2021-04-22 15:41:12 +00:00
|
|
|
|
2022-05-06 16:05:08 +00:00
|
|
|
struct landlock_ruleset *
|
|
|
|
landlock_merge_ruleset(struct landlock_ruleset *const parent,
|
|
|
|
struct landlock_ruleset *const ruleset);
|
2021-04-22 15:41:12 +00:00
|
|
|
|
2022-05-06 16:05:08 +00:00
|
|
|
const struct landlock_rule *
|
|
|
|
landlock_find_rule(const struct landlock_ruleset *const ruleset,
|
2023-10-26 01:47:42 +00:00
|
|
|
const struct landlock_id id);
|
2021-04-22 15:41:12 +00:00
|
|
|
|
|
|
|
static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
|
|
|
|
{
|
|
|
|
if (ruleset)
|
|
|
|
refcount_inc(&ruleset->usage);
|
|
|
|
}
|
|
|
|
|
2024-11-09 11:08:54 +00:00
|
|
|
/**
|
|
|
|
* landlock_union_access_masks - Return all access rights handled in the
|
|
|
|
* domain
|
|
|
|
*
|
|
|
|
* @domain: Landlock ruleset (used as a domain)
|
|
|
|
*
|
|
|
|
* Returns: an access_masks result of the OR of all the domain's access masks.
|
|
|
|
*/
|
|
|
|
static inline struct access_masks
|
|
|
|
landlock_union_access_masks(const struct landlock_ruleset *const domain)
|
|
|
|
{
|
|
|
|
union access_masks_all matches = {};
|
|
|
|
size_t layer_level;
|
|
|
|
|
|
|
|
for (layer_level = 0; layer_level < domain->num_layers; layer_level++) {
|
|
|
|
union access_masks_all layer = {
|
|
|
|
.masks = domain->access_masks[layer_level],
|
|
|
|
};
|
|
|
|
|
|
|
|
matches.all |= layer.all;
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches.masks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* landlock_get_applicable_domain - Return @domain if it applies to (handles)
|
|
|
|
* at least one of the access rights specified
|
|
|
|
* in @masks
|
|
|
|
*
|
|
|
|
* @domain: Landlock ruleset (used as a domain)
|
|
|
|
* @masks: access masks
|
|
|
|
*
|
|
|
|
* Returns: @domain if any access rights specified in @masks is handled, or
|
|
|
|
* NULL otherwise.
|
|
|
|
*/
|
|
|
|
static inline const struct landlock_ruleset *
|
|
|
|
landlock_get_applicable_domain(const struct landlock_ruleset *const domain,
|
|
|
|
const struct access_masks masks)
|
|
|
|
{
|
|
|
|
const union access_masks_all masks_all = {
|
|
|
|
.masks = masks,
|
|
|
|
};
|
|
|
|
union access_masks_all merge = {};
|
|
|
|
|
|
|
|
if (!domain)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
merge.masks = landlock_union_access_masks(domain);
|
|
|
|
if (merge.all & masks_all.all)
|
|
|
|
return domain;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-10-26 01:47:40 +00:00
|
|
|
static inline void
|
|
|
|
landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
|
|
|
|
const access_mask_t fs_access_mask,
|
|
|
|
const u16 layer_level)
|
|
|
|
{
|
|
|
|
access_mask_t fs_mask = fs_access_mask & LANDLOCK_MASK_ACCESS_FS;
|
|
|
|
|
|
|
|
/* Should already be checked in sys_landlock_create_ruleset(). */
|
|
|
|
WARN_ON_ONCE(fs_access_mask != fs_mask);
|
2024-06-10 08:21:15 +00:00
|
|
|
ruleset->access_masks[layer_level].fs |= fs_mask;
|
2023-10-26 01:47:40 +00:00
|
|
|
}
|
|
|
|
|
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 inline void
|
|
|
|
landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
|
|
|
|
const access_mask_t net_access_mask,
|
|
|
|
const u16 layer_level)
|
|
|
|
{
|
|
|
|
access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
|
|
|
|
|
|
|
|
/* Should already be checked in sys_landlock_create_ruleset(). */
|
|
|
|
WARN_ON_ONCE(net_access_mask != net_mask);
|
2024-06-10 08:21:15 +00:00
|
|
|
ruleset->access_masks[layer_level].net |= net_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
|
|
|
}
|
|
|
|
|
2024-09-05 00:13:55 +00:00
|
|
|
static inline void
|
|
|
|
landlock_add_scope_mask(struct landlock_ruleset *const ruleset,
|
|
|
|
const access_mask_t scope_mask, const u16 layer_level)
|
|
|
|
{
|
|
|
|
access_mask_t mask = scope_mask & LANDLOCK_MASK_SCOPE;
|
|
|
|
|
|
|
|
/* Should already be checked in sys_landlock_create_ruleset(). */
|
|
|
|
WARN_ON_ONCE(scope_mask != mask);
|
|
|
|
ruleset->access_masks[layer_level].scope |= mask;
|
|
|
|
}
|
|
|
|
|
2023-10-26 01:47:41 +00:00
|
|
|
static inline access_mask_t
|
|
|
|
landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,
|
|
|
|
const u16 layer_level)
|
|
|
|
{
|
|
|
|
/* Handles all initially denied by default access rights. */
|
2024-11-09 11:08:54 +00:00
|
|
|
return ruleset->access_masks[layer_level].fs |
|
2023-10-26 01:47:41 +00:00
|
|
|
LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
|
|
|
|
}
|
|
|
|
|
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 inline access_mask_t
|
|
|
|
landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
|
|
|
|
const u16 layer_level)
|
|
|
|
{
|
2024-06-10 08:21:15 +00:00
|
|
|
return ruleset->access_masks[layer_level].net;
|
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
|
|
|
}
|
|
|
|
|
2024-09-05 00:13:55 +00:00
|
|
|
static inline access_mask_t
|
|
|
|
landlock_get_scope_mask(const struct landlock_ruleset *const ruleset,
|
|
|
|
const u16 layer_level)
|
|
|
|
{
|
|
|
|
return ruleset->access_masks[layer_level].scope;
|
|
|
|
}
|
|
|
|
|
2023-10-26 01:47:45 +00:00
|
|
|
bool landlock_unmask_layers(const struct landlock_rule *const rule,
|
|
|
|
const access_mask_t access_request,
|
|
|
|
layer_mask_t (*const layer_masks)[],
|
|
|
|
const size_t masks_array_size);
|
2023-10-26 01:47:44 +00:00
|
|
|
|
2023-10-26 01:47:45 +00:00
|
|
|
access_mask_t
|
|
|
|
landlock_init_layer_masks(const struct landlock_ruleset *const domain,
|
|
|
|
const access_mask_t access_request,
|
|
|
|
layer_mask_t (*const layer_masks)[],
|
|
|
|
const enum landlock_key_type key_type);
|
2023-10-26 01:47:44 +00:00
|
|
|
|
2021-04-22 15:41:12 +00:00
|
|
|
#endif /* _SECURITY_LANDLOCK_RULESET_H */
|