Network support for Landlock
Subsystems (7)
v11 cover Network support for Landlock
Hi,
This is a new V11 patch related to Landlock LSM network confinement. It is based on the landlock's -next branch on top of v6.2-rc3+ kernel version:
https://git.kernel.org/pub/scm/linux/kernel/git/mic/linux.git/log/?h=next
It brings refactoring of previous patch version V10.
Mostly there are fixes of logic and typos, refactoring some selftests.
All test were run in QEMU evironment and compiled with
-static flag.
1. network_test: 36/36 tests passed.
2. base_test: 7/7 tests passed.
3. fs_test: 78/78 tests passed.
4. ptrace_test: 8/8 tests passed.
Previous versions:
v10: https://lore.kernel.org/linux-security-module/[email protected]/
v9: https://lore.kernel.org/linux-security-module/[email protected]/
v8: https://lore.kernel.org/linux-security-module/[email protected]/
v7: https://lore.kernel.org/linux-security-module/[email protected]/
v6: https://lore.kernel.org/linux-security-module/[email protected]/
v5: https://lore.kernel.org/linux-security-module/[email protected]
v4: https://lore.kernel.org/linux-security-module/[email protected]/
v3: https://lore.kernel.org/linux-security-module/[email protected]/
v2: https://lore.kernel.org/linux-security-module/[email protected]/
v1: https://lore.kernel.org/linux-security-module/[email protected]/
Konstantin Meskhidze (11):
landlock: Make ruleset's access masks more generic
landlock: Refactor landlock_find_rule/insert_rule
landlock: Refactor merge/inherit_ruleset functions
landlock: Move and rename layer helpers
landlock: Refactor layer helpers
landlock: Refactor landlock_add_rule() syscall
landlock: Add network rules and TCP hooks support selftests/landlock: Share enforce_ruleset() selftests/landlock: Add 11 new test suites dedicated to network samples/landlock: Add network demo
landlock: Document Landlock's network support
Mickaël Salaün (1):
landlock: Allow filesystem layout changes for domains without such
rule type
Documentation/userspace-api/landlock.rst | 89 +- include/uapi/linux/landlock.h | 48 + samples/landlock/sandboxer.c | 128 +- security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/fs.c | 232 +-- security/landlock/limits.h | 7 +- security/landlock/net.c | 174 +++ security/landlock/net.h | 26 + security/landlock/ruleset.c | 405 +++++- security/landlock/ruleset.h | 185 ++- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 163 ++- tools/testing/selftests/landlock/base_test.c | 2 +- tools/testing/selftests/landlock/common.h | 10 + tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 74 +- tools/testing/selftests/landlock/net_test.c | 1317 ++++++++++++++++++ 18 files changed, 2520 insertions(+), 349 deletions(-) create mode 100644 security/landlock/net.c create mode 100644 security/landlock/net.h create mode 100644 tools/testing/selftests/landlock/net_test.c
--
2.25.1
PATCH v11 1/12 landlock: Make ruleset's access masks more generic
To support network type rules, this modification renames ruleset's access masks and modifies it's type to access_masks_t. This patch adds filesystem helper functions to add and get filesystem mask.
---
Changes since v10:
* Squashes landlock_get_fs_access_mask() part from commit 2.
Changes since v9:
* None.
Changes since v8:
* Minor fixes.
Changes since v7:
* Refactors commit message.
Changes since v6:
* Adds a new access_masks_t for struct ruleset.
* Renames landlock_set_fs_access_mask() to landlock_add_fs_access_mask() because it OR values.
* Makes landlock_add_fs_access_mask() more resilient incorrect values.
* Refactors landlock_get_fs_access_mask().
Changes since v6:
* Adds a new access_masks_t for struct ruleset.
* Renames landlock_set_fs_access_mask() to landlock_add_fs_access_mask() because it OR values.
* Makes landlock_add_fs_access_mask() more resilient incorrect values.
* Refactors landlock_get_fs_access_mask().
Changes since v5:
* Changes access_mask_t to u32.
* Formats code with clang-format-14.
Changes since v4:
* Deletes struct landlock_access_mask.
Changes since v3:
* Splits commit.
* Adds get_mask, set_mask helpers for filesystem.
* Adds new struct landlock_access_mask.
--- security/landlock/fs.c | 10 +++++----- security/landlock/limits.h | 1 + security/landlock/ruleset.c | 17 +++++++++-------- security/landlock/ruleset.h | 34 ++++++++++++++++++++++++++++++---- security/landlock/syscalls.c | 7 ++++--- 5 files changed, 49 insertions(+), 20 deletions(-)
security/landlock/fs.c +5 -5
@@ -178,9 +178,9 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,return -EINVAL ;/* Transforms relative access rights to absolute ones. */ - access_rights |= -LANDLOCK_MASK_ACCESS_FS & - ~(ruleset->fs_access_masks[0 ] |ACCESS_INITIALLY_DENIED ); + access_rights |=LANDLOCK_MASK_ACCESS_FS & + ~(landlock_get_fs_access_mask (ruleset,0 ) | +ACCESS_INITIALLY_DENIED ); object =get_inode_object (d_backing_inode (path->dentry));if (IS_ERR (object))return PTR_ERR (object);
@@ -294,7 +294,7 @@ get_handled_accesses(const struct landlock_ruleset *const domain)size_t layer_level;for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) - access_dom |= domain->fs_access_masks[layer_level]; + access_dom |=landlock_get_fs_access_mask (domain, layer_level);return access_dom &LANDLOCK_MASK_ACCESS_FS ; }
@@ -336,7 +336,7 @@ init_layer_masks(const struct landlock_ruleset *const domain,* access rights. */ if (BIT_ULL (access_bit) & - (domain->fs_access_masks[layer_level] | + (landlock_get_fs_access_mask (domain, layer_level) |ACCESS_INITIALLY_DENIED )) { (*layer_masks)[access_bit] |=BIT_ULL (layer_level);
security/landlock/limits.h +1 -0
@@ -21,6 +21,7 @@#define LANDLOCK_LAST_ACCESS_FS LANDLOCK_ACCESS_FS_TRUNCATE#define LANDLOCK_MASK_ACCESS_FS ((LANDLOCK_LAST_ACCESS_FS << 1) - 1)#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS) +#define LANDLOCK_SHIFT_ACCESS_FS 0/* clang-format on */
security/landlock/ruleset.c +9 -8
@@ -29,7 +29,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)struct landlock_ruleset *new_ruleset; new_ruleset = -kzalloc (struct_size (new_ruleset, fs_access_masks, num_layers), +kzalloc (struct_size (new_ruleset, access_masks, num_layers),GFP_KERNEL_ACCOUNT );if (!new_ruleset)return ERR_PTR (-ENOMEM );
@@ -40,7 +40,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)/* * hierarchy = NULL * num_rules = 0 -* fs_access_masks[] = 0 +* access_masks[] = 0 */ return new_ruleset; }
@@ -55,7 +55,7 @@ landlock_create_ruleset(const access_mask_t fs_access_mask)return ERR_PTR (-ENOMSG ); new_ruleset =create_ruleset (1 );if (!IS_ERR (new_ruleset)) - new_ruleset->fs_access_masks[0 ] = fs_access_mask; +landlock_add_fs_access_mask (new_ruleset, fs_access_mask,0 );return new_ruleset; }
@@ -117,11 +117,12 @@ static void build_check_ruleset(void) .num_rules = ~0, .num_layers = ~0, }; - typeof(ruleset.fs_access_masks[0]) fs_access_mask = ~0; + typeof(ruleset.access_masks[0]) access_masks = ~0;BUILD_BUG_ON (ruleset.num_rules <LANDLOCK_MAX_NUM_RULES );BUILD_BUG_ON (ruleset.num_layers <LANDLOCK_MAX_NUM_LAYERS ); -BUILD_BUG_ON (fs_access_mask <LANDLOCK_MASK_ACCESS_FS ); +BUILD_BUG_ON (access_masks < + (LANDLOCK_MASK_ACCESS_FS <<LANDLOCK_SHIFT_ACCESS_FS )); }/**
@@ -281,7 +282,7 @@ static int merge_ruleset(struct landlock_ruleset *const dst, err = -EINVAL ; goto out_unlock; } - dst->fs_access_masks[dst->num_layers -1 ] = src->fs_access_masks[0 ]; + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ];/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, &src->root,
@@ -340,8 +341,8 @@ static int inherit_ruleset(struct landlock_ruleset *const parent, goto out_unlock; }/* Copies the parent layer stack and leaves a space for the new layer. */ -memcpy (child->fs_access_masks, parent->fs_access_masks, -flex_array_size (parent, fs_access_masks, parent->num_layers)); +memcpy (child->access_masks, parent->access_masks, +flex_array_size (parent, access_masks, parent->num_layers));if (WARN_ON_ONCE (!parent->hierarchy)) { err = -EINVAL ;
security/landlock/ruleset.h +30 -4
@@ -25,6 +25,11 @@ static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);/* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */ static_assert (sizeof (unsigned long ) >=sizeof (access_mask_t)); +/* Ruleset access masks. */ +typedef u16 access_masks_t ; +/* Makes sure all ruleset access rights can be stored. */ +static_assert (BITS_PER_TYPE (access_masks_t) >=LANDLOCK_NUM_ACCESS_FS ); +typedef u16 layer_mask_t ;/* Makes sure all layers can be checked. */ static_assert (BITS_PER_TYPE (layer_mask_t) >=LANDLOCK_MAX_NUM_LAYERS );
@@ -110,7 +115,7 @@ struct landlock_ruleset {* section. This is only used by * landlock_put_ruleset_deferred() when @usage reaches zero. * The fields @lock, @usage, @num_rules, @num_layers and -* @fs_access_masks are then unused. +* @access_masks are then unused. */ struct work_struct work_free;struct {
@@ -137,7 +142,7 @@ struct landlock_ruleset {*/ u32 num_layers;/** -* @fs_access_masks: Contains the subset of filesystem +* @access_masks: Contains the subset of filesystem * 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
@@ -148,13 +153,13 @@ struct landlock_ruleset {* layers are set once and never changed for the * lifetime of the ruleset. */ -access_mask_t fs_access_masks[]; +access_masks_t access_masks[]; }; }; };struct landlock_ruleset * -landlock_create_ruleset (const access_mask_t fs_access_mask); +landlock_create_ruleset (const access_mask_t access_mask);void landlock_put_ruleset (struct landlock_ruleset *const ruleset);void landlock_put_ruleset_deferred (struct landlock_ruleset *const ruleset);
@@ -177,4 +182,25 @@ static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset) refcount_inc(&ruleset->usage); } +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); + ruleset->access_masks[layer_level] |= + (fs_mask <<LANDLOCK_SHIFT_ACCESS_FS ); +} + +static inline access_mask_t +landlock_get_fs_access_mask (const struct landlock_ruleset *const ruleset, +const u16 layer_level) +{ +return (ruleset->access_masks[layer_level] >> +LANDLOCK_SHIFT_ACCESS_FS ) & +LANDLOCK_MASK_ACCESS_FS ; +}#endif /* _SECURITY_LANDLOCK_RULESET_H */
security/landlock/syscalls.c +4 -3
@@ -310,6 +310,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,struct path path;struct landlock_ruleset *ruleset;int res, err; +access_mask_t mask;if (!landlock_initialized)return -EOPNOTSUPP ;
@@ -346,10 +347,10 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, }/* * Checks that allowed_access matches the @ruleset constraints -* (ruleset->fs_access_masks[0] is automatically upgraded to 64-bits). +* (ruleset->access_masks[0] is automatically upgraded to 64-bits). */ -if ((path_beneath_attr.allowed_access | ruleset->fs_access_masks[0 ]) != - ruleset->fs_access_masks[0 ]) { + mask =landlock_get_fs_access_mask (ruleset,0 ); +if ((path_beneath_attr.allowed_access | mask) != mask) { err = -EINVAL ; goto out_put_ruleset; }
--
2.25.1
PATCH v11 2/12 landlock: Allow filesystem layout changes for domains without such rule type
In reply to Konstantin MeskhidzeFrom: Mickaël Salaün <[email protected]>
Allow mount point and root directory changes when there is no filesystem rule tied to the current Landlock domain. This doesn't change anything for now because a domain must have at least a (filesystem) rule, but this will change when other rule types will come. For instance, a domain only restricting the network should have no impact on filesystem restrictions.
Add a new get_current_fs_domain() helper to quickly check filesystem rule existence for all filesystem LSM hooks.
Remove unnecessary inlining.
---
Changes since v10:
* Squashes landlock_get_fs_access_mask() part into commit 1.
* Opportunistically removes inline get_raw_handled_fs_accesses function with changing it's signature.
Changes since v9:
* Refactors documentaion landlock.rst.
* Changes ACCESS_FS_INITIALLY_DENIED constant to LANDLOCK_ACCESS_FS_INITIALLY_DENIED.
* Gets rid of unnecessary masking of access_dom in get_raw_handled_fs_accesses() function.
Changes since v8:
* Refactors get_handled_fs_accesses().
* Adds landlock_get_raw_fs_access_mask() helper.
--- Documentation/userspace-api/landlock.rst | 6 +- security/landlock/fs.c | 74 ++++++++++++------------ security/landlock/ruleset.h | 25 +++++++- security/landlock/syscalls.c | 2 +- 4 files changed, 64 insertions(+), 43 deletions(-)
Documentation/userspace-api/landlock.rst +3 -3
@@ -387,9 +387,9 @@ Current limitations Filesystem topology modification -------------------------------- -As for file renaming and linking, a sandboxed thread cannot modify its -filesystem topology, whether via :manpage:`mount(2)` or -:manpage:`pivot_root(2)`. However, :manpage:`chroot(2)` calls are not denied. +Threads sandboxed with filesystem restrictions cannot modify filesystem +topology, whether via :manpage:`mount(2)` or :manpage:`pivot_root(2)`. +However, :manpage:`chroot(2)` calls are not denied. Special filesystems -------------------
security/landlock/fs.c +37 -37
169 lines not on this page · raw message
security/landlock/ruleset.h +23 -2
@@ -15,10 +15,21 @@#include <linux/rbtree.h> #include <linux/refcount.h> #include <linux/workqueue.h> +#include <uapi/linux/landlock.h> #include "limits.h" #include "object.h" +/* +* 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 */ +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 );
@@ -196,11 +207,21 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset, }static inline access_mask_t -landlock_get_fs_access_mask (const struct landlock_ruleset *const ruleset, -const u16 layer_level) +landlock_get_raw_fs_access_mask (const struct landlock_ruleset *const ruleset, +const u16 layer_level) {return (ruleset->access_masks[layer_level] >>LANDLOCK_SHIFT_ACCESS_FS ) &LANDLOCK_MASK_ACCESS_FS ; } + +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. */ +return landlock_get_raw_fs_access_mask (ruleset, layer_level) | +LANDLOCK_ACCESS_FS_INITIALLY_DENIED ; +} +#endif /* _SECURITY_LANDLOCK_RULESET_H */
security/landlock/syscalls.c +1 -1
@@ -349,7 +349,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,* Checks that allowed_access matches the @ruleset constraints * (ruleset->access_masks[0] is automatically upgraded to 64-bits). */ - mask =landlock_get_fs_access_mask (ruleset,0 ); + mask =landlock_get_raw_fs_access_mask (ruleset,0 );if ((path_beneath_attr.allowed_access | mask) != mask) { err = -EINVAL ; goto out_put_ruleset;
--
2.25.1
PATCH v11 3/12 landlock: Refactor landlock_find_rule/insert_rule
In reply to Konstantin MeskhidzeAdd a new landlock_key union and landlock_id structure to support a socket port rule type. A struct landlock_id identifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.
This patch also adds is_object_pointer() and get_root() helpers. is_object_pointer() returns true if key type is LANDLOCK_KEY_INODE. get_root() helper returns a red_black tree root pointer according to a key type.
Refactor landlock_insert_rule() and landlock_find_rule() to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.
---
Changes since v10:
* None.
Changes since v9:
* Splits commit.
* Refactors commit message.
* Minor fixes.
Changes since v8:
* Refactors commit message.
* Removes inlining.
* Minor fixes.
Changes since v7:
* Completes all the new field descriptions landlock_key, landlock_key_type, landlock_id.
* Refactors commit message, adds a co-developer.
Changes since v6:
* Adds union landlock_key, enum landlock_key_type, and struct landlock_id.
* Refactors ruleset functions and improves switch/cases: create_rule(), insert_rule(), get_root(), is_object_pointer(), free_rule(), landlock_find_rule().
* Refactors landlock_append_fs_rule() functions to support new landlock_id type.
Changes since v5:
* Formats code with clang-format-14.
Changes since v4:
* Refactors insert_rule() and create_rule() functions by deleting rule_type from their arguments list, it helps to reduce useless code.
Changes since v3:
* Splits commit.
* Refactors landlock_insert_rule and landlock_find_rule functions.
* Rename new_ruleset->root_inode.
--- security/landlock/fs.c | 21 +++--- security/landlock/ruleset.c | 134 ++++++++++++++++++++++++++---------- security/landlock/ruleset.h | 65 ++++++++++++++--- 3 files changed, 166 insertions(+), 54 deletions(-)
security/landlock/fs.c +13 -8
@@ -158,7 +158,9 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, access_mask_t access_rights) {int err; -struct landlock_object *object; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Files only get access rights that make sense. */ if (!d_is_dir(path->dentry) &&
@@ -170,17 +172,17 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,/* Transforms relative access rights to absolute ones. */ access_rights |=LANDLOCK_MASK_ACCESS_FS & ~landlock_get_fs_access_mask (ruleset,0 ); - object =get_inode_object (d_backing_inode (path->dentry)); -if (IS_ERR (object)) -return PTR_ERR (object); + id.key.object =get_inode_object (d_backing_inode (path->dentry)); +if (IS_ERR (id.key.object)) +return PTR_ERR (id.key.object);mutex_lock (&ruleset->lock); - err =landlock_insert_rule (ruleset, object, access_rights); + err =landlock_insert_rule (ruleset, id, access_rights);mutex_unlock (&ruleset->lock);/* * No need to check for an error because landlock_insert_rule() * increments the refcount for the new object if needed. */ -landlock_put_object (object); +landlock_put_object (id.key.object);return err; }
@@ -197,6 +199,9 @@ find_rule(const struct landlock_ruleset *const domain, {const struct landlock_rule *rule;const struct inode *inode; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Ignores nonexistent leafs. */ if (d_is_negative (dentry))
@@ -204,8 +209,8 @@ find_rule(const struct landlock_ruleset *const domain, inode =d_backing_inode (dentry);rcu_read_lock (); - rule =landlock_find_rule ( - domain,rcu_dereference (landlock_inode (inode)->object)); + id.key.object =rcu_dereference (landlock_inode (inode)->object); + rule =landlock_find_rule (domain, id);rcu_read_unlock ();return rule; }
security/landlock/ruleset.c +99 -35
286 lines not on this page · raw message
security/landlock/ruleset.h +54 -11
97 lines not on this page · raw message
--
2.25.1
Mickaël Salaün
maintainerRe: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
Quoted 63 lines · Konstantin Meskhidze
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* None.Changes since v9:
* Splits commit.
* Refactors commit message.
* Minor fixes.Changes since v8:
* Refactors commit message.
* Removes inlining.
* Minor fixes.Changes since v7:
* Completes all the new field descriptionslandlock_key,landlock_key_type,landlock_id.
* Refactors commit message, adds a co-developer.Changes since v6:
* Adds unionlandlock_key, enumlandlock_key_type, and structlandlock_id.
* Refactors ruleset functions and improves switch/cases:create_rule(),insert_rule(),get_root(),is_object_pointer(),free_rule(),landlock_find_rule().
* Refactorslandlock_append_fs_rule()functions to support newlandlock_idtype.Changes since v5:
* Formats code with clang-format-14.Changes since v4:
* Refactorsinsert_rule()andcreate_rule()functions by deletingrule_typefrom their arguments list, it helps to reduce useless code.Changes since v3:
* Splits commit.
* Refactorslandlock_insert_ruleandlandlock_find_rulefunctions.
* Renamenew_ruleset->root_inode.--- security/landlock/fs.c | 21 +++--- security/landlock/ruleset.c | 134 ++++++++++++++++++++++++++---------- security/landlock/ruleset.h | 65 ++++++++++++++--- 3 files changed, 166 insertions(+), 54 deletions(-)
[...]
Quoted 4 lines
security/landlock/ruleset.c +0
-0
[...]
Quoted 19 lines
security/landlock/ruleset.c +5
-0@@ -316,21 +368,29 @@ static int inherit_ruleset(struct landlock_ruleset *const parent, struct landlock_ruleset *const child) {struct landlock_rule *walker_rule, *next_rule; +struct rb_root *parent_root;int err =0 ;might_sleep ();if (!parent)return 0 ; + parent_root =get_root (parent,LANDLOCK_KEY_INODE ); +if (IS_ERR (parent_root)) +return PTR_ERR (parent_root); +/* Locks @child first because we are its only owner. */ mutex_lock (&child->lock);mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING );
security/landlock/ruleset.c static int inherit_ruleset(struct landlock_ruleset *const parent, · line 392
Quoted 10 lines
security/landlock/ruleset.c +5
-3/* Copies the @parent tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, - &parent->root, node) { - err = insert_rule(child, walker_rule->object, - &walker_rule->layers, + parent_root, node) { +const struct landlock_id id = { + .key = walker_rule->key, + .type =LANDLOCK_KEY_INODE , + };
Please add a line break here instead of in a the following refactoring commit.
Quoted 4 lines
security/landlock/ruleset.c +1
-0+ err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) goto out_unlock;
Re: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
6/26/2023 9:40 PM, Mickaël Salaün пишет:
Quoted 94 lines · Konstantin Meskhidze
Quoted 63 lines
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* None.Changes since v9:
* Splits commit.
* Refactors commit message.
* Minor fixes.Changes since v8:
* Refactors commit message.
* Removes inlining.
* Minor fixes.Changes since v7:
* Completes all the new field descriptionslandlock_key,landlock_key_type,landlock_id.
* Refactors commit message, adds a co-developer.Changes since v6:
* Adds unionlandlock_key, enumlandlock_key_type, and structlandlock_id.
* Refactors ruleset functions and improves switch/cases:create_rule(),insert_rule(),get_root(),is_object_pointer(),free_rule(),landlock_find_rule().
* Refactorslandlock_append_fs_rule()functions to support newlandlock_idtype.Changes since v5:
* Formats code with clang-format-14.Changes since v4:
* Refactorsinsert_rule()andcreate_rule()functions by deletingrule_typefrom their arguments list, it helps to reduce useless code.Changes since v3:
* Splits commit.
* Refactorslandlock_insert_ruleandlandlock_find_rulefunctions.
* Renamenew_ruleset->root_inode.--- security/landlock/fs.c | 21 +++--- security/landlock/ruleset.c | 134 ++++++++++++++++++++++++++---------- security/landlock/ruleset.h | 65 ++++++++++++++--- 3 files changed, 166 insertions(+), 54 deletions(-)[...]
Quoted 4 lines
security/landlock/ruleset.c +0
-0[...]
Quoted 19 lines
security/landlock/ruleset.c +5
-0@@ -316,21 +368,29 @@ static int inherit_ruleset(struct landlock_ruleset *const parent, struct landlock_ruleset *const child) {struct landlock_rule *walker_rule, *next_rule; +struct rb_root *parent_root;int err =0 ;might_sleep ();if (!parent)return 0 ; + parent_root =get_root (parent,LANDLOCK_KEY_INODE ); +if (IS_ERR (parent_root)) +return PTR_ERR (parent_root); +/* Locks @child first because we are its only owner. */ mutex_lock (&child->lock);mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING );
Quoted 13 lines
security/landlock/ruleset.c static int inherit_ruleset(struct landlock_ruleset *const parent, · line 392
Quoted 10 lines
security/landlock/ruleset.c +5
-3/* Copies the @parent tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, - &parent->root, node) { - err = insert_rule(child, walker_rule->object, - &walker_rule->layers, + parent_root, node) { +const struct landlock_id id = { + .key = walker_rule->key, + .type =LANDLOCK_KEY_INODE , + };Please add a line break here instead of in a the following refactoring commit.
Ok. Will be addded.
Quoted 7 lines
security/landlock/ruleset.c static int inherit_ruleset(struct landlock_ruleset *const parent, · line 396
Quoted 6 lines
security/landlock/ruleset.c +1
-0+ err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) goto out_unlock;.
Mickaël Salaün
maintainerRe: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
In reply to Konstantin MeskhidzeComplementary review:
Quoted 229 lines · Konstantin Meskhidze
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* None.Changes since v9:
* Splits commit.
* Refactors commit message.
* Minor fixes.Changes since v8:
* Refactors commit message.
* Removes inlining.
* Minor fixes.Changes since v7:
* Completes all the new field descriptionslandlock_key,landlock_key_type,landlock_id.
* Refactors commit message, adds a co-developer.Changes since v6:
* Adds unionlandlock_key, enumlandlock_key_type, and structlandlock_id.
* Refactors ruleset functions and improves switch/cases:create_rule(),insert_rule(),get_root(),is_object_pointer(),free_rule(),landlock_find_rule().
* Refactorslandlock_append_fs_rule()functions to support newlandlock_idtype.Changes since v5:
* Formats code with clang-format-14.Changes since v4:
* Refactorsinsert_rule()andcreate_rule()functions by deletingrule_typefrom their arguments list, it helps to reduce useless code.Changes since v3:
* Splits commit.
* Refactorslandlock_insert_ruleandlandlock_find_rulefunctions.
* Renamenew_ruleset->root_inode.--- security/landlock/fs.c | 21 +++--- security/landlock/ruleset.c | 134 ++++++++++++++++++++++++++---------- security/landlock/ruleset.h | 65 ++++++++++++++--- 3 files changed, 166 insertions(+), 54 deletions(-)security/landlock/fs.c +13
-8@@ -158,7 +158,9 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, access_mask_t access_rights) {int err; -struct landlock_object *object; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Files only get access rights that make sense. */ if (!d_is_dir(path->dentry) &&@@ -170,17 +172,17 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,/* Transforms relative access rights to absolute ones. */ access_rights |=LANDLOCK_MASK_ACCESS_FS & ~landlock_get_fs_access_mask (ruleset,0 ); - object =get_inode_object (d_backing_inode (path->dentry)); -if (IS_ERR (object)) -return PTR_ERR (object); + id.key.object =get_inode_object (d_backing_inode (path->dentry)); +if (IS_ERR (id.key.object)) +return PTR_ERR (id.key.object);mutex_lock (&ruleset->lock); - err =landlock_insert_rule (ruleset, object, access_rights); + err =landlock_insert_rule (ruleset, id, access_rights);mutex_unlock (&ruleset->lock);/* * No need to check for an error because landlock_insert_rule() * increments the refcount for the new object if needed. */ -landlock_put_object (object); +landlock_put_object (id.key.object);return err; }@@ -197,6 +199,9 @@ find_rule(const struct landlock_ruleset *const domain, {const struct landlock_rule *rule;const struct inode *inode; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Ignores nonexistent leafs. */ if (d_is_negative (dentry))@@ -204,8 +209,8 @@ find_rule(const struct landlock_ruleset *const domain, inode =d_backing_inode (dentry);rcu_read_lock (); - rule =landlock_find_rule ( - domain,rcu_dereference (landlock_inode (inode)->object)); + id.key.object =rcu_dereference (landlock_inode (inode)->object); + rule =landlock_find_rule (domain, id);rcu_read_unlock ();return rule; }security/landlock/ruleset.c +43
-10@@ -35,7 +35,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)return ERR_PTR (-ENOMEM );refcount_set (&new_ruleset->usage,1 );mutex_init (&new_ruleset->lock); - new_ruleset->root =RB_ROOT ; + new_ruleset->root_inode =RB_ROOT ; new_ruleset->num_layers = num_layers;/* * hierarchy = NULL @@ -68,8 +68,18 @@ static void build_check_rule(void)BUILD_BUG_ON (rule.num_layers <LANDLOCK_MAX_NUM_LAYERS ); } +static bool is_object_pointer (const enum landlock_key_type key_type) +{ +switch (key_type) { +case LANDLOCK_KEY_INODE : +return true; + } +WARN_ON_ONCE (1 ); +return false; +} +static struct landlock_rule * -create_rule (struct landlock_object *const object, +create_rule (const struct landlock_id id,const struct landlock_layer (*const layers)[],const u32 num_layers,const struct landlock_layer *const new_layer) {@@ -90,8 +100,13 @@ create_rule(struct landlock_object *const object,if (!new_rule)return ERR_PTR (-ENOMEM );RB_CLEAR_NODE (&new_rule->node); -landlock_get_object (object); - new_rule->object = object; +if (is_object_pointer (id.type)) { +/* This should be catched by insert_rule(). */ +WARN_ON_ONCE (!id.key.object); +landlock_get_object (id.key.object); + } + + new_rule->key = id.key; new_rule->num_layers = new_num_layers;/* Copies the original layer stack. */ memcpy(new_rule->layers, layers,@@ -102,12 +117,29 @@ create_rule(struct landlock_object *const object,return new_rule; } -static void free_rule (struct landlock_rule *const rule) +static struct rb_root *get_root (struct landlock_ruleset *const ruleset, +const enum landlock_key_type key_type) +{ +struct rb_root *root =NULL ; + +switch (key_type) { +case LANDLOCK_KEY_INODE : + root = &ruleset->root_inode; +break ; + } +if (WARN_ON_ONCE (!root)) +return ERR_PTR (-EINVAL ); +return root; +} + +static void free_rule (struct landlock_rule *const rule, +const enum landlock_key_type key_type) {might_sleep ();if (!rule)return ; -landlock_put_object (rule->object); +if (is_object_pointer (key_type)) +landlock_put_object (rule->key.object);kfree (rule); }@@ -129,8 +161,8 @@ static void build_check_ruleset(void)* insert_rule - Create and insert a rule in a ruleset * * @ruleset: The ruleset to be updated. -* @object: The object to build the new rule with. The underlying kernel -* object must be held by the caller. +* @id: The ID to build the new rule with. The underlying kernel object, if +* any, must be held by the caller. * @layers: One or multiple layers to be copied into the new rule. * @num_layers: The number of @layers entries. * @@ -144,26 +176,37 @@ static void build_check_ruleset(void)* access rights. */ static int insert_rule (struct landlock_ruleset *const ruleset, -struct landlock_object *const object, +const struct landlock_id id,const struct landlock_layer (*const layers)[], -size_t num_layers) +const size_t num_layers) {struct rb_node **walker_node;struct rb_node *parent_node =NULL ;struct landlock_rule *new_rule; +struct rb_root *root;
security/landlock/ruleset.c static void build_check_ruleset(void) · line 194
Quoted 10 lines
security/landlock/ruleset.c +4
-2might_sleep ();lockdep_assert_held (&ruleset->lock); -if (WARN_ON_ONCE (!object || !layers)) +if (WARN_ON_ONCE (!layers))return -ENOENT ; - walker_node = &(ruleset->root.rb_node); + +if (is_object_pointer (id.type)) { +if (WARN_ON_ONCE (!id.key.object))
This would be simpler:
if (is_object_pointer(id.type) && WARN_ON_ONCE(!id.key.object))
return -ENOENT;
Quoted 3 lines
security/landlock/ruleset.c +3
-0+return -ENOENT ; + } +
Re: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
6/26/2023 9:58 PM, Mickaël Salaün пишет:
Quoted 245 lines
Complementary review:
Quoted 229 lines · Konstantin Meskhidze
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* None.Changes since v9:
* Splits commit.
* Refactors commit message.
* Minor fixes.Changes since v8:
* Refactors commit message.
* Removes inlining.
* Minor fixes.Changes since v7:
* Completes all the new field descriptionslandlock_key,landlock_key_type,landlock_id.
* Refactors commit message, adds a co-developer.Changes since v6:
* Adds unionlandlock_key, enumlandlock_key_type, and structlandlock_id.
* Refactors ruleset functions and improves switch/cases:create_rule(),insert_rule(),get_root(),is_object_pointer(),free_rule(),landlock_find_rule().
* Refactorslandlock_append_fs_rule()functions to support newlandlock_idtype.Changes since v5:
* Formats code with clang-format-14.Changes since v4:
* Refactorsinsert_rule()andcreate_rule()functions by deletingrule_typefrom their arguments list, it helps to reduce useless code.Changes since v3:
* Splits commit.
* Refactorslandlock_insert_ruleandlandlock_find_rulefunctions.
* Renamenew_ruleset->root_inode.--- security/landlock/fs.c | 21 +++--- security/landlock/ruleset.c | 134 ++++++++++++++++++++++++++---------- security/landlock/ruleset.h | 65 ++++++++++++++--- 3 files changed, 166 insertions(+), 54 deletions(-)security/landlock/fs.c +13
-8@@ -158,7 +158,9 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset, access_mask_t access_rights) {int err; -struct landlock_object *object; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Files only get access rights that make sense. */ if (!d_is_dir(path->dentry) &&@@ -170,17 +172,17 @@ int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,/* Transforms relative access rights to absolute ones. */ access_rights |=LANDLOCK_MASK_ACCESS_FS & ~landlock_get_fs_access_mask (ruleset,0 ); - object =get_inode_object (d_backing_inode (path->dentry)); -if (IS_ERR (object)) -return PTR_ERR (object); + id.key.object =get_inode_object (d_backing_inode (path->dentry)); +if (IS_ERR (id.key.object)) +return PTR_ERR (id.key.object);mutex_lock (&ruleset->lock); - err =landlock_insert_rule (ruleset, object, access_rights); + err =landlock_insert_rule (ruleset, id, access_rights);mutex_unlock (&ruleset->lock);/* * No need to check for an error because landlock_insert_rule() * increments the refcount for the new object if needed. */ -landlock_put_object (object); +landlock_put_object (id.key.object);return err; }@@ -197,6 +199,9 @@ find_rule(const struct landlock_ruleset *const domain, {const struct landlock_rule *rule;const struct inode *inode; +struct landlock_id id = { + .type =LANDLOCK_KEY_INODE , + };/* Ignores nonexistent leafs. */ if (d_is_negative (dentry))@@ -204,8 +209,8 @@ find_rule(const struct landlock_ruleset *const domain, inode =d_backing_inode (dentry);rcu_read_lock (); - rule =landlock_find_rule ( - domain,rcu_dereference (landlock_inode (inode)->object)); + id.key.object =rcu_dereference (landlock_inode (inode)->object); + rule =landlock_find_rule (domain, id);rcu_read_unlock ();return rule; }security/landlock/ruleset.c +43
-10@@ -35,7 +35,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)return ERR_PTR (-ENOMEM );refcount_set (&new_ruleset->usage,1 );mutex_init (&new_ruleset->lock); - new_ruleset->root =RB_ROOT ; + new_ruleset->root_inode =RB_ROOT ; new_ruleset->num_layers = num_layers;/* * hierarchy = NULL @@ -68,8 +68,18 @@ static void build_check_rule(void)BUILD_BUG_ON (rule.num_layers <LANDLOCK_MAX_NUM_LAYERS ); } +static bool is_object_pointer (const enum landlock_key_type key_type) +{ +switch (key_type) { +case LANDLOCK_KEY_INODE : +return true; + } +WARN_ON_ONCE (1 ); +return false; +} +static struct landlock_rule * -create_rule (struct landlock_object *const object, +create_rule (const struct landlock_id id,const struct landlock_layer (*const layers)[],const u32 num_layers,const struct landlock_layer *const new_layer) {@@ -90,8 +100,13 @@ create_rule(struct landlock_object *const object,if (!new_rule)return ERR_PTR (-ENOMEM );RB_CLEAR_NODE (&new_rule->node); -landlock_get_object (object); - new_rule->object = object; +if (is_object_pointer (id.type)) { +/* This should be catched by insert_rule(). */ +WARN_ON_ONCE (!id.key.object); +landlock_get_object (id.key.object); + } + + new_rule->key = id.key; new_rule->num_layers = new_num_layers;/* Copies the original layer stack. */ memcpy(new_rule->layers, layers,@@ -102,12 +117,29 @@ create_rule(struct landlock_object *const object,return new_rule; } -static void free_rule (struct landlock_rule *const rule) +static struct rb_root *get_root (struct landlock_ruleset *const ruleset, +const enum landlock_key_type key_type) +{ +struct rb_root *root =NULL ; + +switch (key_type) { +case LANDLOCK_KEY_INODE : + root = &ruleset->root_inode; +break ; + } +if (WARN_ON_ONCE (!root)) +return ERR_PTR (-EINVAL ); +return root; +} + +static void free_rule (struct landlock_rule *const rule, +const enum landlock_key_type key_type) {might_sleep ();if (!rule)return ; -landlock_put_object (rule->object); +if (is_object_pointer (key_type)) +landlock_put_object (rule->key.object);kfree (rule); }@@ -129,8 +161,8 @@ static void build_check_ruleset(void)* insert_rule - Create and insert a rule in a ruleset * * @ruleset: The ruleset to be updated. -* @object: The object to build the new rule with. The underlying kernel -* object must be held by the caller. +* @id: The ID to build the new rule with. The underlying kernel object, if +* any, must be held by the caller. * @layers: One or multiple layers to be copied into the new rule. * @num_layers: The number of @layers entries. * @@ -144,26 +176,37 @@ static void build_check_ruleset(void)* access rights. */ static int insert_rule (struct landlock_ruleset *const ruleset, -struct landlock_object *const object, +const struct landlock_id id,const struct landlock_layer (*const layers)[], -size_t num_layers) +const size_t num_layers) {struct rb_node **walker_node;struct rb_node *parent_node =NULL ;struct landlock_rule *new_rule; +struct rb_root *root;security/landlock/ruleset.c static void build_check_ruleset(void) · line 194
Quoted 10 lines
security/landlock/ruleset.c +4
-2might_sleep ();lockdep_assert_held (&ruleset->lock); -if (WARN_ON_ONCE (!object || !layers)) +if (WARN_ON_ONCE (!layers))return -ENOENT ; - walker_node = &(ruleset->root.rb_node); + +if (is_object_pointer (id.type)) { +if (WARN_ON_ONCE (!id.key.object))This would be simpler:
Quoted 3 lines
if (
is_object_pointer(id.type)&&WARN_ON_ONCE(!id.key.object))return -ENOENT;
Yep. Thank you for the tip.
Quoted 5 lines
security/landlock/ruleset.c static void build_check_ruleset(void) · line 197
Quoted 4 lines
security/landlock/ruleset.c +3
-0+return -ENOENT ; + } +.
Mickaël Salaün
maintainerRe: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
In reply to Konstantin MeskhidzeQuoted 18 lines · Konstantin Meskhidze
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
[...]
Quoted 14 lines
security/landlock/ruleset.c +1
-1@@ -35,7 +35,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)return ERR_PTR (-ENOMEM );refcount_set (&new_ruleset->usage,1 );mutex_init (&new_ruleset->lock); - new_ruleset->root =RB_ROOT ; + new_ruleset->root_inode =RB_ROOT ; new_ruleset->num_layers = num_layers;/* * hierarchy = NULL
security/landlock/ruleset.c static void build_check_rule(void) · line 77
Quoted 10 lines
security/landlock/ruleset.c +6
-0@@ -68,8 +68,18 @@ static void build_check_rule(void)BUILD_BUG_ON (rule.num_layers <LANDLOCK_MAX_NUM_LAYERS ); } +static bool is_object_pointer (const enum landlock_key_type key_type) +{ +switch (key_type) { +case LANDLOCK_KEY_INODE : +return true; + }
Because of enum change [1], could you please put the following block inside this commit's switch with a new "default:" case, and add a line break after the previous return like this:
\n
default:
security/landlock/ruleset.c static void build_check_rule(void) · line 78
Quoted 2 lines
security/landlock/ruleset.c +2
-0+WARN_ON_ONCE (1 ); +return false;
break;
}
Quoted 25 lines
security/landlock/ruleset.c +3
-1+} +static struct landlock_rule * -create_rule (struct landlock_object *const object, +create_rule (const struct landlock_id id,const struct landlock_layer (*const layers)[],const u32 num_layers,const struct landlock_layer *const new_layer){security/landlock/ruleset.c +7
-2@@ -90,8 +100,13 @@ create_rule(struct landlock_object *const object,if (!new_rule)return ERR_PTR (-ENOMEM );RB_CLEAR_NODE (&new_rule->node); -landlock_get_object (object); - new_rule->object = object; +if (is_object_pointer (id.type)) { +/* This should be catched by insert_rule(). */ +WARN_ON_ONCE (!id.key.object); +landlock_get_object (id.key.object); + } + + new_rule->key = id.key; new_rule->num_layers = new_num_layers;/* Copies the original layer stack. */ memcpy(new_rule->layers, layers,
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 122
Quoted 7 lines
security/landlock/ruleset.c +3
-1@@ -102,12 +117,29 @@ create_rule(struct landlock_object *const object,return new_rule; } -static void free_rule (struct landlock_rule *const rule) +static struct rb_root *get_root (struct landlock_ruleset *const ruleset, +const enum landlock_key_type key_type) +{
Same here, you can remove the "root" variable:
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 128
Quoted 6 lines
security/landlock/ruleset.c +6
-0+struct rb_root *root =NULL ; + +switch (key_type) { +case LANDLOCK_KEY_INODE : + root = &ruleset->root_inode; +break ;
return &ruleset->root_inode;
\n
default:
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 131
Quoted 2 lines
security/landlock/ruleset.c +2
-0+if (WARN_ON_ONCE (!root)) +return ERR_PTR (-EINVAL );
break;
}
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 131
Quoted 1 line
security/landlock/ruleset.c +1
-0+}
Actually, I've pushed this change here: https://git.kernel.org/mic/c/8c96c7eee3ff (landlock-net-v11 branch)
Re: [PATCH v11 03/12] landlock: Refactor landlock_find_rule/insert_rule
7/6/2023 5:34 PM, Mickaël Salaün пишет:
Quoted 53 lines · Konstantin Meskhidze
Quoted 18 lines
Add a new
landlock_keyunion andlandlock_idstructure to support a socket port rule type. A structlandlock_ididentifies a unique entry in a ruleset: either a kernel object (e.g inode) or typed data (e.g TCP port). There is one red-black tree per key type.This patch also adds
is_object_pointer()andget_root()helpers.is_object_pointer()returns true if key type isLANDLOCK_KEY_INODE.get_root()helper returns ared_blacktree root pointer according to a key type.Refactor
landlock_insert_rule()andlandlock_find_rule()to support coming network modifications. Adding or searching a rule in ruleset can now be done thanks to a Landlock ID argument passed to these helpers.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
[...]
Quoted 14 lines
security/landlock/ruleset.c +1
-1@@ -35,7 +35,7 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)return ERR_PTR (-ENOMEM );refcount_set (&new_ruleset->usage,1 );mutex_init (&new_ruleset->lock); - new_ruleset->root =RB_ROOT ; + new_ruleset->root_inode =RB_ROOT ; new_ruleset->num_layers = num_layers;/* * hierarchy = NULL security/landlock/ruleset.c static void build_check_rule(void) · line 77
Quoted 10 lines
security/landlock/ruleset.c +6
-0@@ -68,8 +68,18 @@ static void build_check_rule(void)BUILD_BUG_ON (rule.num_layers <LANDLOCK_MAX_NUM_LAYERS ); } +static bool is_object_pointer (const enum landlock_key_type key_type) +{ +switch (key_type) { +case LANDLOCK_KEY_INODE : +return true; + }Because of enum change [1], could you please put the following block inside this commit's switch with a new "default:" case, and add a line break after the previous return like this:
Quoted 7 lines
\n
default:security/landlock/ruleset.c static void build_check_rule(void) · line 78
Quoted 2 lines
security/landlock/ruleset.c +2
-0+WARN_ON_ONCE (1 ); +return false;break;
}
Ok. I will add "default: case.
Thank you.
Quoted 46 lines
Quoted 26 lines
security/landlock/ruleset.c +3
-1+} +static struct landlock_rule * -create_rule (struct landlock_object *const object, +create_rule (const struct landlock_id id,const struct landlock_layer (*const layers)[],const u32 num_layers,const struct landlock_layer *const new_layer){security/landlock/ruleset.c +7
-2@@ -90,8 +100,13 @@ create_rule(struct landlock_object *const object,if (!new_rule)return ERR_PTR (-ENOMEM );RB_CLEAR_NODE (&new_rule->node); -landlock_get_object (object); - new_rule->object = object; +if (is_object_pointer (id.type)) { +/* This should be catched by insert_rule(). */ +WARN_ON_ONCE (!id.key.object); +landlock_get_object (id.key.object); + } + + new_rule->key = id.key; new_rule->num_layers = new_num_layers;/* Copies the original layer stack. */ memcpy(new_rule->layers, layers,security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 122
Quoted 7 lines
security/landlock/ruleset.c +3
-1@@ -102,12 +117,29 @@ create_rule(struct landlock_object *const object,return new_rule; } -static void free_rule (struct landlock_rule *const rule) +static struct rb_root *get_root (struct landlock_ruleset *const ruleset, +const enum landlock_key_type key_type) +{Same here, you can remove the "root" variable:
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 128
Quoted 6 lines
security/landlock/ruleset.c +6
-0+struct rb_root *root =NULL ; + +switch (key_type) { +case LANDLOCK_KEY_INODE : + root = &ruleset->root_inode; +break ;return
&ruleset->root_inode;
\n
default:
Quoted 4 lines
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 131
Quoted 2 lines
security/landlock/ruleset.c +2
-0+if (WARN_ON_ONCE (!root)) +return ERR_PTR (-EINVAL );break;
}
Ok. Will be fixed.
Quoted 5 lines
security/landlock/ruleset.c create_rule(struct landlock_object *const object, · line 131
Quoted 2 lines
security/landlock/ruleset.c +1
-0+}Actually, I've pushed this change here:
https://git.kernel.org/mic/c/8c96c7eee3ff (landlock-net-v11 branch)
Thank you.
Quoted 1 line
.
PATCH v11 4/12 landlock: Refactor merge/inherit_ruleset functions
In reply to Konstantin MeskhidzeRefactor merge_ruleset() and inherit_ruleset() functions to support new rule types. This patch adds merge_tree() and inherit_tree() helpers. They use a specific ruleset's red-black tree according to a key type argument.
---
Changes since v10:
* Refactors merge_tree() function.
Changes since v9:
* None
Changes since v8:
* Refactors commit message.
* Minor fixes.
Changes since v7:
* Adds missed lockdep_assert_held it inherit_tree() and merge_tree().
* Fixes comment.
Changes since v6:
* Refactors merge_ruleset() and inherit_ruleset() functions to support new rule types.
* Renames tree_merge() to merge_tree() (and reorder arguments), and tree_copy() to inherit_tree().
Changes since v5:
* Refactors some logic errors.
* Formats code with clang-format-14.
Changes since v4:
* None
--- security/landlock/ruleset.c | 122 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 43 deletions(-)
security/landlock/ruleset.c +79 -43
@@ -302,36 +302,22 @@ static void put_hierarchy(struct landlock_hierarchy *hierarchy) } } -static int merge_ruleset (struct landlock_ruleset *const dst, -struct landlock_ruleset *const src) +static int merge_tree (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *src_root;int err =0 ;might_sleep (); -/* Should already be checked by landlock_merge_ruleset() */ -if (WARN_ON_ONCE (!src)) -return 0 ; -/* Only merge into a domain. */ -if (WARN_ON_ONCE (!dst || !dst->hierarchy)) -return -EINVAL ; +lockdep_assert_held (&dst->lock); +lockdep_assert_held (&src->lock); - src_root =get_root (src,LANDLOCK_KEY_INODE ); + src_root =get_root (src, key_type);if (IS_ERR (src_root))return PTR_ERR (src_root); -/* Locks @dst first because we are its only owner. */ -mutex_lock (&dst->lock); -mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); - -/* Stacks the new layer. */ -if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { - err = -EINVAL ; - goto out_unlock; - } - dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; -/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, src_root, node) {
@@ -340,23 +326,52 @@ static int merge_ruleset(struct landlock_ruleset *const dst, } };const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; -if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { - err = -EINVAL ; - goto out_unlock; - } -if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { - err = -EINVAL ; - goto out_unlock; - } +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) +return -EINVAL ; + +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) +return -EINVAL ; + layers[0 ].access = walker_rule->layers[0 ].access; err =insert_rule (dst, id, &layers,ARRAY_SIZE (layers));if (err) - goto out_unlock; +return err; + } +return err; +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; } + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; + +/* Merges the @src inode tree. */ + err =merge_tree (dst, src,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock; out_unlock:mutex_unlock (&src->lock);
@@ -364,43 +379,64 @@ static int merge_ruleset(struct landlock_ruleset *const dst,return err; } -static int inherit_ruleset (struct landlock_ruleset *const parent, -struct landlock_ruleset *const child) +static int inherit_tree (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *parent_root;int err =0 ;might_sleep (); -if (!parent) -return 0 ; +lockdep_assert_held (&parent->lock); +lockdep_assert_held (&child->lock); - parent_root =get_root (parent,LANDLOCK_KEY_INODE ); + parent_root =get_root (parent, key_type);if (IS_ERR (parent_root))return PTR_ERR (parent_root); -/* Locks @child first because we are its only owner. */ -mutex_lock (&child->lock); -mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); - -/* Copies the @parent tree. */ +/* Copies the @parent inode or network tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, parent_root, node) {const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; + err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) - goto out_unlock; +return err; } +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent inode tree. */ + err =inherit_tree (parent, child,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock;if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { err = -EINVAL ; goto out_unlock; } -/* Copies the parent layer stack and leaves a space for the new layer. */ +/* +* Copies the parent layer stack and leaves a space +* for the new layer. +*/ memcpy (child->access_masks, parent->access_masks,flex_array_size (parent, access_masks, parent->num_layers));
--
2.25.1
Mickaël Salaün
maintainerRe: [PATCH v11 04/12] landlock: Refactor merge/inherit_ruleset functions
Quoted 214 lines · Konstantin Meskhidze
Refactor
merge_ruleset()andinherit_ruleset()functions to support new rule types. This patch addsmerge_tree()andinherit_tree()helpers. They use a specific ruleset's red-black tree according to a key type argument.Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Refactorsmerge_tree()function.Changes since v9:
* NoneChanges since v8:
* Refactors commit message.
* Minor fixes.Changes since v7:
* Adds missedlockdep_assert_helditinherit_tree()andmerge_tree().
* Fixes comment.Changes since v6:
* Refactorsmerge_ruleset()andinherit_ruleset()functions to support new rule types.
* Renamestree_merge()tomerge_tree()(and reorder arguments), andtree_copy()toinherit_tree().Changes since v5:
* Refactors some logic errors.
* Formats code with clang-format-14.Changes since v4:
* None--- security/landlock/ruleset.c | 122 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 43 deletions(-)security/landlock/ruleset.c +75
-42@@ -302,36 +302,22 @@ static void put_hierarchy(struct landlock_hierarchy *hierarchy) } } -static int merge_ruleset (struct landlock_ruleset *const dst, -struct landlock_ruleset *const src) +static int merge_tree (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *src_root;int err =0 ;might_sleep (); -/* Should already be checked by landlock_merge_ruleset() */ -if (WARN_ON_ONCE (!src)) -return 0 ; -/* Only merge into a domain. */ -if (WARN_ON_ONCE (!dst || !dst->hierarchy)) -return -EINVAL ; +lockdep_assert_held (&dst->lock); +lockdep_assert_held (&src->lock); - src_root =get_root (src,LANDLOCK_KEY_INODE ); + src_root =get_root (src, key_type);if (IS_ERR (src_root))return PTR_ERR (src_root); -/* Locks @dst first because we are its only owner. */ -mutex_lock (&dst->lock); -mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); - -/* Stacks the new layer. */ -if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { - err = -EINVAL ; - goto out_unlock; - } - dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; -/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, src_root, node) {@@ -340,23 +326,52 @@ static int merge_ruleset(struct landlock_ruleset *const dst, } };const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; -if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { - err = -EINVAL ; - goto out_unlock; - } -if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { - err = -EINVAL ; - goto out_unlock; - } +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) +return -EINVAL ; + +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) +return -EINVAL ; + layers[0 ].access = walker_rule->layers[0 ].access; err =insert_rule (dst, id, &layers,ARRAY_SIZE (layers));if (err) - goto out_unlock; +return err; + } +return err; +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; } + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; + +/* Merges the @src inode tree. */ + err =merge_tree (dst, src,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock; out_unlock:mutex_unlock (&src->lock);@@ -364,43 +379,64 @@ static int merge_ruleset(struct landlock_ruleset *const dst,return err; } -static int inherit_ruleset (struct landlock_ruleset *const parent, -struct landlock_ruleset *const child) +static int inherit_tree (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *parent_root;int err =0 ;might_sleep (); -if (!parent) -return 0 ; +lockdep_assert_held (&parent->lock); +lockdep_assert_held (&child->lock); - parent_root =get_root (parent,LANDLOCK_KEY_INODE ); + parent_root =get_root (parent, key_type);if (IS_ERR (parent_root))return PTR_ERR (parent_root); -/* Locks @child first because we are its only owner. */ -mutex_lock (&child->lock); -mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); - -/* Copies the @parent tree. */ +/* Copies the @parent inode or network tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, parent_root, node) {const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; + err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) - goto out_unlock; +return err; } +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent inode tree. */ + err =inherit_tree (parent, child,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock;
security/landlock/ruleset.c static int merge_ruleset(struct landlock_ruleset *const dst, · line 439
Quoted 10 lines
security/landlock/ruleset.c +4
-1if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { err = -EINVAL ; goto out_unlock; } -/* Copies the parent layer stack and leaves a space for the new layer. */ +/* +* Copies the parent layer stack and leaves a space +* for the new layer. +*/
Did that get formatted because of clang-format? The original line exceed the 80 columns limit, but it is not caught by different version of clang-format I tested. Anyway, we should remove this hunk for now because it has no link with the current patch.
Quoted 6 lines
security/landlock/ruleset.c +0
-1memcpy (child->access_masks, parent->access_masks,flex_array_size (parent, access_masks, parent->num_layers)); --2.25.1
Re: [PATCH v11 04/12] landlock: Refactor merge/inherit_ruleset functions
6/26/2023 9:40 PM, Mickaël Salaün пишет:
Quoted 217 lines · Konstantin Meskhidze
Quoted 215 lines
Refactor
merge_ruleset()andinherit_ruleset()functions to support new rule types. This patch addsmerge_tree()andinherit_tree()helpers. They use a specific ruleset's red-black tree according to a key type argument.Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Refactorsmerge_tree()function.Changes since v9:
* NoneChanges since v8:
* Refactors commit message.
* Minor fixes.Changes since v7:
* Adds missedlockdep_assert_helditinherit_tree()andmerge_tree().
* Fixes comment.Changes since v6:
* Refactorsmerge_ruleset()andinherit_ruleset()functions to support new rule types.
* Renamestree_merge()tomerge_tree()(and reorder arguments), andtree_copy()toinherit_tree().Changes since v5:
* Refactors some logic errors.
* Formats code with clang-format-14.Changes since v4:
* None--- security/landlock/ruleset.c | 122 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 43 deletions(-)security/landlock/ruleset.c +75
-42@@ -302,36 +302,22 @@ static void put_hierarchy(struct landlock_hierarchy *hierarchy) } } -static int merge_ruleset (struct landlock_ruleset *const dst, -struct landlock_ruleset *const src) +static int merge_tree (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *src_root;int err =0 ;might_sleep (); -/* Should already be checked by landlock_merge_ruleset() */ -if (WARN_ON_ONCE (!src)) -return 0 ; -/* Only merge into a domain. */ -if (WARN_ON_ONCE (!dst || !dst->hierarchy)) -return -EINVAL ; +lockdep_assert_held (&dst->lock); +lockdep_assert_held (&src->lock); - src_root =get_root (src,LANDLOCK_KEY_INODE ); + src_root =get_root (src, key_type);if (IS_ERR (src_root))return PTR_ERR (src_root); -/* Locks @dst first because we are its only owner. */ -mutex_lock (&dst->lock); -mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); - -/* Stacks the new layer. */ -if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { - err = -EINVAL ; - goto out_unlock; - } - dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; -/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, src_root, node) {@@ -340,23 +326,52 @@ static int merge_ruleset(struct landlock_ruleset *const dst, } };const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; -if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { - err = -EINVAL ; - goto out_unlock; - } -if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { - err = -EINVAL ; - goto out_unlock; - } +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) +return -EINVAL ; + +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) +return -EINVAL ; + layers[0 ].access = walker_rule->layers[0 ].access; err =insert_rule (dst, id, &layers,ARRAY_SIZE (layers));if (err) - goto out_unlock; +return err; + } +return err; +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; } + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; + +/* Merges the @src inode tree. */ + err =merge_tree (dst, src,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock; out_unlock:mutex_unlock (&src->lock);@@ -364,43 +379,64 @@ static int merge_ruleset(struct landlock_ruleset *const dst,return err; } -static int inherit_ruleset (struct landlock_ruleset *const parent, -struct landlock_ruleset *const child) +static int inherit_tree (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *parent_root;int err =0 ;might_sleep (); -if (!parent) -return 0 ; +lockdep_assert_held (&parent->lock); +lockdep_assert_held (&child->lock); - parent_root =get_root (parent,LANDLOCK_KEY_INODE ); + parent_root =get_root (parent, key_type);if (IS_ERR (parent_root))return PTR_ERR (parent_root); -/* Locks @child first because we are its only owner. */ -mutex_lock (&child->lock); -mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); - -/* Copies the @parent tree. */ +/* Copies the @parent inode or network tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, parent_root, node) {const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; + err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) - goto out_unlock; +return err; } +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent inode tree. */ + err =inherit_tree (parent, child,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock;
Quoted 14 lines
security/landlock/ruleset.c static int merge_ruleset(struct landlock_ruleset *const dst, · line 439
Quoted 9 lines
security/landlock/ruleset.c +4
-1if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { err = -EINVAL ; goto out_unlock; } -/* Copies the parent layer stack and leaves a space for the new layer. */ +/* +* Copies the parent layer stack and leaves a space +* for the new layer. +*/ Did that get formatted because of clang-format? The original line exceed the 80 columns limit, but it is not caught by different version of clang-format I tested. Anyway, we should remove this hunk for now because it has no link with the current patch.
Yep. I format every patch with clnag-format. I will remove this hunk and let it be as it was.
Quoted 9 lines
Quoted 8 lines
security/landlock/ruleset.c +0
-1memcpy (child->access_masks, parent->access_masks,flex_array_size (parent, access_masks, parent->num_layers)); --2.25.1
.
Mickaël Salaün
maintainerRe: [PATCH v11 04/12] landlock: Refactor merge/inherit_ruleset functions
Quoted 235 lines · Konstantin Meskhidze (A)
6/26/2023 9:40 PM, Mickaël Salaün пишет:
Quoted 217 lines · Konstantin Meskhidze
Quoted 215 lines
Refactor
merge_ruleset()andinherit_ruleset()functions to support new rule types. This patch addsmerge_tree()andinherit_tree()helpers. They use a specific ruleset's red-black tree according to a key type argument.Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Refactorsmerge_tree()function.Changes since v9:
* NoneChanges since v8:
* Refactors commit message.
* Minor fixes.Changes since v7:
* Adds missedlockdep_assert_helditinherit_tree()andmerge_tree().
* Fixes comment.Changes since v6:
* Refactorsmerge_ruleset()andinherit_ruleset()functions to support new rule types.
* Renamestree_merge()tomerge_tree()(and reorder arguments), andtree_copy()toinherit_tree().Changes since v5:
* Refactors some logic errors.
* Formats code with clang-format-14.Changes since v4:
* None--- security/landlock/ruleset.c | 122 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 43 deletions(-)security/landlock/ruleset.c +75
-42@@ -302,36 +302,22 @@ static void put_hierarchy(struct landlock_hierarchy *hierarchy) } } -static int merge_ruleset (struct landlock_ruleset *const dst, -struct landlock_ruleset *const src) +static int merge_tree (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *src_root;int err =0 ;might_sleep (); -/* Should already be checked by landlock_merge_ruleset() */ -if (WARN_ON_ONCE (!src)) -return 0 ; -/* Only merge into a domain. */ -if (WARN_ON_ONCE (!dst || !dst->hierarchy)) -return -EINVAL ; +lockdep_assert_held (&dst->lock); +lockdep_assert_held (&src->lock); - src_root =get_root (src,LANDLOCK_KEY_INODE ); + src_root =get_root (src, key_type);if (IS_ERR (src_root))return PTR_ERR (src_root); -/* Locks @dst first because we are its only owner. */ -mutex_lock (&dst->lock); -mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); - -/* Stacks the new layer. */ -if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { - err = -EINVAL ; - goto out_unlock; - } - dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; -/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, src_root, node) {@@ -340,23 +326,52 @@ static int merge_ruleset(struct landlock_ruleset *const dst, } };const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; -if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { - err = -EINVAL ; - goto out_unlock; - } -if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { - err = -EINVAL ; - goto out_unlock; - } +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) +return -EINVAL ; + +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) +return -EINVAL ; + layers[0 ].access = walker_rule->layers[0 ].access; err =insert_rule (dst, id, &layers,ARRAY_SIZE (layers));if (err) - goto out_unlock; +return err; + } +return err; +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; } + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; + +/* Merges the @src inode tree. */ + err =merge_tree (dst, src,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock; out_unlock:mutex_unlock (&src->lock);@@ -364,43 +379,64 @@ static int merge_ruleset(struct landlock_ruleset *const dst,return err; } -static int inherit_ruleset (struct landlock_ruleset *const parent, -struct landlock_ruleset *const child) +static int inherit_tree (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *parent_root;int err =0 ;might_sleep (); -if (!parent) -return 0 ; +lockdep_assert_held (&parent->lock); +lockdep_assert_held (&child->lock); - parent_root =get_root (parent,LANDLOCK_KEY_INODE ); + parent_root =get_root (parent, key_type);if (IS_ERR (parent_root))return PTR_ERR (parent_root); -/* Locks @child first because we are its only owner. */ -mutex_lock (&child->lock); -mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); - -/* Copies the @parent tree. */ +/* Copies the @parent inode or network tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, parent_root, node) {const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; + err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) - goto out_unlock; +return err; } +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent inode tree. */ + err =inherit_tree (parent, child,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock;Quoted 14 lines
security/landlock/ruleset.c static int merge_ruleset(struct landlock_ruleset *const dst, · line 439
Quoted 9 lines
security/landlock/ruleset.c +4
-1if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { err = -EINVAL ; goto out_unlock; } -/* Copies the parent layer stack and leaves a space for the new layer. */ +/* +* Copies the parent layer stack and leaves a space +* for the new layer. +*/ Did that get formatted because of clang-format? The original line exceed the 80 columns limit, but it is not caught by different version of clang-format I tested. Anyway, we should remove this hunk for now because it has no link with the current patch.
Quoted 2 lines
Yep. I format every patch with clnag-format. I will remove this hunk and let it be as it was.
It's weird because clang-format doesn't touch this hunk for me. Which version do you use? Do you have any specific configuration?
Re: [PATCH v11 04/12] landlock: Refactor merge/inherit_ruleset functions
7/5/2023 1:16 PM, Mickaël Salaün пишет:
Quoted 237 lines · Konstantin Meskhidze (A)
Quoted 235 lines
6/26/2023 9:40 PM, Mickaël Salaün пишет:
Quoted 231 lines · Konstantin Meskhidze
Quoted 214 lines
Refactor
merge_ruleset()andinherit_ruleset()functions to support new rule types. This patch addsmerge_tree()andinherit_tree()helpers. They use a specific ruleset's red-black tree according to a key type argument.Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Refactorsmerge_tree()function.Changes since v9:
* NoneChanges since v8:
* Refactors commit message.
* Minor fixes.Changes since v7:
* Adds missedlockdep_assert_helditinherit_tree()andmerge_tree().
* Fixes comment.Changes since v6:
* Refactorsmerge_ruleset()andinherit_ruleset()functions to support new rule types.
* Renamestree_merge()tomerge_tree()(and reorder arguments), andtree_copy()toinherit_tree().Changes since v5:
* Refactors some logic errors.
* Formats code with clang-format-14.Changes since v4:
* None--- security/landlock/ruleset.c | 122 +++++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 43 deletions(-)security/landlock/ruleset.c +75
-42@@ -302,36 +302,22 @@ static void put_hierarchy(struct landlock_hierarchy *hierarchy) } } -static int merge_ruleset (struct landlock_ruleset *const dst, -struct landlock_ruleset *const src) +static int merge_tree (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *src_root;int err =0 ;might_sleep (); -/* Should already be checked by landlock_merge_ruleset() */ -if (WARN_ON_ONCE (!src)) -return 0 ; -/* Only merge into a domain. */ -if (WARN_ON_ONCE (!dst || !dst->hierarchy)) -return -EINVAL ; +lockdep_assert_held (&dst->lock); +lockdep_assert_held (&src->lock); - src_root =get_root (src,LANDLOCK_KEY_INODE ); + src_root =get_root (src, key_type);if (IS_ERR (src_root))return PTR_ERR (src_root); -/* Locks @dst first because we are its only owner. */ -mutex_lock (&dst->lock); -mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); - -/* Stacks the new layer. */ -if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { - err = -EINVAL ; - goto out_unlock; - } - dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; -/* Merges the @src tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, src_root, node) {@@ -340,23 +326,52 @@ static int merge_ruleset(struct landlock_ruleset *const dst, } };const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; -if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) { - err = -EINVAL ; - goto out_unlock; - } -if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) { - err = -EINVAL ; - goto out_unlock; - } +if (WARN_ON_ONCE (walker_rule->num_layers !=1 )) +return -EINVAL ; + +if (WARN_ON_ONCE (walker_rule->layers[0 ].level !=0 )) +return -EINVAL ; + layers[0 ].access = walker_rule->layers[0 ].access; err =insert_rule (dst, id, &layers,ARRAY_SIZE (layers));if (err) - goto out_unlock; +return err; + } +return err; +} + +static int merge_ruleset (struct landlock_ruleset *const dst, +struct landlock_ruleset *const src) +{ +int err =0 ; + +might_sleep (); +/* Should already be checked by landlock_merge_ruleset() */ +if (WARN_ON_ONCE (!src)) +return 0 ; +/* Only merge into a domain. */ +if (WARN_ON_ONCE (!dst || !dst->hierarchy)) +return -EINVAL ; + +/* Locks @dst first because we are its only owner. */ +mutex_lock (&dst->lock); +mutex_lock_nested (&src->lock,SINGLE_DEPTH_NESTING ); + +/* Stacks the new layer. */ +if (WARN_ON_ONCE (src->num_layers !=1 || dst->num_layers <1 )) { + err = -EINVAL ; + goto out_unlock; } + dst->access_masks[dst->num_layers -1 ] = src->access_masks[0 ]; + +/* Merges the @src inode tree. */ + err =merge_tree (dst, src,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock; out_unlock:mutex_unlock (&src->lock);@@ -364,43 +379,64 @@ static int merge_ruleset(struct landlock_ruleset *const dst,return err; } -static int inherit_ruleset (struct landlock_ruleset *const parent, -struct landlock_ruleset *const child) +static int inherit_tree (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child, +const enum landlock_key_type key_type) {struct landlock_rule *walker_rule, *next_rule;struct rb_root *parent_root;int err =0 ;might_sleep (); -if (!parent) -return 0 ; +lockdep_assert_held (&parent->lock); +lockdep_assert_held (&child->lock); - parent_root =get_root (parent,LANDLOCK_KEY_INODE ); + parent_root =get_root (parent, key_type);if (IS_ERR (parent_root))return PTR_ERR (parent_root); -/* Locks @child first because we are its only owner. */ -mutex_lock (&child->lock); -mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); - -/* Copies the @parent tree. */ +/* Copies the @parent inode or network tree. */ rbtree_postorder_for_each_entry_safe (walker_rule, next_rule, parent_root, node) {const struct landlock_id id = { .key = walker_rule->key, - .type =LANDLOCK_KEY_INODE , + .type = key_type, }; + err =insert_rule (child, id, &walker_rule->layers, walker_rule->num_layers);if (err) - goto out_unlock; +return err; } +return err; +} + +static int inherit_ruleset (struct landlock_ruleset *const parent, +struct landlock_ruleset *const child) +{ +int err =0 ; + +might_sleep (); +if (!parent) +return 0 ; + +/* Locks @child first because we are its only owner. */ +mutex_lock (&child->lock); +mutex_lock_nested (&parent->lock,SINGLE_DEPTH_NESTING ); + +/* Copies the @parent inode tree. */ + err =inherit_tree (parent, child,LANDLOCK_KEY_INODE ); +if (err) + goto out_unlock;security/landlock/ruleset.c static int merge_ruleset(struct landlock_ruleset *const dst, · line 439
Quoted 10 lines
security/landlock/ruleset.c +4
-1if (WARN_ON_ONCE (child->num_layers <= parent->num_layers)) { err = -EINVAL ; goto out_unlock; } -/* Copies the parent layer stack and leaves a space for the new layer. */ +/* +* Copies the parent layer stack and leaves a space +* for the new layer. +*/ Did that get formatted because of clang-format? The original line exceed the 80 columns limit, but it is not caught by different version of clang-format I tested. Anyway, we should remove this hunk for now because it has no link with the current patch.
Quoted 5 lines
Quoted 2 lines
Yep. I format every patch with clnag-format. I will remove this hunk and let it be as it was.
It's weird because clang-format doesn't touch this hunk for me. Which version do you use? Do you have any specific configuration?
Sorry for misleading, its my fault. I realized that I had formated it
by myself (more than 80 columns length). You are right that clang-format does not have to do with it - just checked it. I will remove the hunk.
Quoted 1 line
.
PATCH v11 5/12 landlock: Move and rename layer helpers
In reply to Konstantin MeskhidzeThis patch renames and moves landlock_unmask_layers() and landlock_init_layer_masks() helpers to ruleset.c to share them with Landlock network implementation in following commits.
---
Changes since v10:
* None.
Changes since v9:
* Minor fixes.
Changes since v8:
* Refactors commit message.
* Adds "landlock_" prefix for moved helpers.
Changes since v7:
* Refactors commit message.
Changes since v6:
* Moves get_handled_accesses() helper from ruleset.c back to fs.c, cause it's not used in coming network commits.
Changes since v5:
* Splits commit.
* Moves init_layer_masks() and get_handled_accesses() helpers to ruleset.c and makes then non-static.
* Formats code with clang-format-14.
--- security/landlock/fs.c | 136 ++++++------------------------------ security/landlock/ruleset.c | 98 ++++++++++++++++++++++++++ security/landlock/ruleset.h | 9 +++ 3 files changed, 128 insertions(+), 115 deletions(-)
security/landlock/fs.c +21 -115
@@ -215,60 +215,6 @@ find_rule(const struct landlock_ruleset *const domain,return rule; } -/* -* @layer_masks is read and may be updated according to the access request and -* the matching rule. -* -* Returns true if the request is allowed (i.e. relevant layer masks for the -* request are empty). -*/ -static inline bool -unmask_layers (const struct landlock_rule *const rule, -const access_mask_t access_request, -layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS ]) -{ -size_t layer_level; - -if (!access_request || !layer_masks) -return true; -if (!rule) -return false; - -/* -* An access is granted if, for each policy layer, at least one rule -* encountered on the pathwalk grants the requested access, -* regardless of its position in the layer stack. We must then check -* the remaining layers for each inode, from the first added layer to -* the last one. When there is multiple requested accesses, for each -* policy layer, the full set of requested accesses may not be granted -* by only one rule, but by the union (binary OR) of multiple rules. -* E.g. /a/b <execute> + /a <read> => /a/b <execute + read> -*/ -for (layer_level =0 ; layer_level < rule->num_layers; layer_level++) { -const struct landlock_layer *const layer = - &rule->layers[layer_level]; -const layer_mask_t layer_bit =BIT_ULL (layer->level -1 ); -const unsigned long access_req = access_request; -unsigned long access_bit; -bool is_empty; - -/* -* Records in @layer_masks which layer grants access to each -* requested access. -*/ - is_empty = true; -for_each_set_bit (access_bit, &access_req, -ARRAY_SIZE (*layer_masks)) { -if (layer->access &BIT_ULL (access_bit)) - (*layer_masks)[access_bit] &= ~layer_bit; - is_empty = is_empty && !(*layer_masks)[access_bit]; - } -if (is_empty) -return true; - } -return false; -} -/* * Allows access to pseudo filesystems that will never be mountable (e.g. * sockfs, pipefs), but can still be reachable through
@@ -293,50 +239,6 @@ get_raw_handled_fs_accesses(const struct landlock_ruleset *const domain)return access_dom; } -/** -* init_layer_masks - Initialize layer masks from an access request -* -* Populates @layer_masks such that for each access right in @access_request, -* the bits for all the layers are set where this access right is handled. -* -* @domain: The domain that defines the current restrictions. -* @access_request: The requested access rights to check. -* @layer_masks: The layer masks to populate. -* -* Returns: An access mask where each access right bit is set which is handled -* in any of the active layers in @domain. -*/ -static inline access_mask_t -init_layer_masks (const struct landlock_ruleset *const domain, -const access_mask_t access_request, -layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS ]) -{ -access_mask_t handled_accesses =0 ; -size_t layer_level; - -memset (layer_masks,0 ,sizeof (*layer_masks)); -/* An empty access request can happen because of O_WRONLY | O_RDWR. */ -if (!access_request) -return 0 ; - -/* Saves all handled accesses per layer. */ -for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) { -const unsigned long access_req = access_request; -unsigned long access_bit; - -for_each_set_bit (access_bit, &access_req, -ARRAY_SIZE (*layer_masks)) { -if (BIT_ULL (access_bit) & -landlock_get_fs_access_mask (domain, layer_level)) { - (*layer_masks)[access_bit] |= -BIT_ULL (layer_level); - handled_accesses |=BIT_ULL (access_bit); - } - } - } -return handled_accesses; -} -static access_mask_t get_handled_fs_accesses (const struct landlock_ruleset *const domain) {
@@ -540,18 +442,20 @@ static bool is_access_to_paths_allowed( }if (unlikely (dentry_child1)) { -unmask_layers (find_rule (domain, dentry_child1), -init_layer_masks (domain,LANDLOCK_MASK_ACCESS_FS , +landlock_unmask_layers (find_rule (domain, dentry_child1), +landlock_init_layer_masks ( + domain,LANDLOCK_MASK_ACCESS_FS , &_layer_masks_child1), - &_layer_masks_child1); + &_layer_masks_child1); layer_masks_child1 = &_layer_masks_child1; child1_is_directory =d_is_dir (dentry_child1); }if (unlikely (dentry_child2)) { -unmask_layers (find_rule (domain, dentry_child2), -init_layer_masks (domain,LANDLOCK_MASK_ACCESS_FS , +landlock_unmask_layers (find_rule (domain, dentry_child2), +landlock_init_layer_masks ( + domain,LANDLOCK_MASK_ACCESS_FS , &_layer_masks_child2), - &_layer_masks_child2); + &_layer_masks_child2); layer_masks_child2 = &_layer_masks_child2; child2_is_directory =d_is_dir (dentry_child2); }
@@ -603,10 +507,10 @@ static bool is_access_to_paths_allowed( } rule =find_rule (domain, walker_path.dentry); - allowed_parent1 =unmask_layers (rule, access_masked_parent1, - layer_masks_parent1); - allowed_parent2 =unmask_layers (rule, access_masked_parent2, - layer_masks_parent2); + allowed_parent1 =landlock_unmask_layers ( + rule, access_masked_parent1, layer_masks_parent1); + allowed_parent2 =landlock_unmask_layers ( + rule, access_masked_parent2, layer_masks_parent2);/* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2)
@@ -650,7 +554,8 @@ static inline int check_access_path(const struct landlock_ruleset *const domain, {layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS ] = {}; - access_request =init_layer_masks (domain, access_request, &layer_masks); + access_request = +landlock_init_layer_masks (domain, access_request, &layer_masks);if (is_access_to_paths_allowed (domain, path, access_request, &layer_masks,NULL ,0 ,NULL ,NULL ))return 0 ;
@@ -735,16 +640,16 @@ static bool collect_domain_accesses(if (is_nouser_or_private (dir))return true; - access_dom =init_layer_masks (domain,LANDLOCK_MASK_ACCESS_FS , - layer_masks_dom); + access_dom =landlock_init_layer_masks (domain,LANDLOCK_MASK_ACCESS_FS , + layer_masks_dom);dget (dir);while (true) {struct dentry *parent_dentry;/* Gets all layers allowing all domain accesses. */ -if (unmask_layers (find_rule (domain, dir), access_dom, - layer_masks_dom)) { +if (landlock_unmask_layers (find_rule (domain, dir), access_dom, + layer_masks_dom)) {/* * Stops when all handled accesses are allowed by at * least one rule in each layer.
@@ -857,7 +762,7 @@ static int current_check_refer_path(struct dentry *const old_dentry,* The LANDLOCK_ACCESS_FS_REFER access right is not required * for same-directory referer (i.e. no reparenting). */ - access_request_parent1 =init_layer_masks ( + access_request_parent1 =landlock_init_layer_masks ( dom, access_request_parent1 | access_request_parent2, &layer_masks_parent1); if (is_access_to_paths_allowed(
@@ -1234,7 +1139,8 @@ static int hook_file_open(struct file *const file)if (is_access_to_paths_allowed ( dom, &file->f_path, -init_layer_masks (dom, full_access_request, &layer_masks), +landlock_init_layer_masks (dom, full_access_request, + &layer_masks), &layer_masks,NULL ,0 ,NULL ,NULL )) { allowed_access = full_access_request; }else {
security/landlock/ruleset.c +98 -0
101 lines not on this page · raw message
security/landlock/ruleset.h +9 -0
@@ -266,5 +266,14 @@ landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,return landlock_get_raw_fs_access_mask (ruleset, layer_level) | LANDLOCK_ACCESS_FS_INITIALLY_DENIED; } +bool landlock_unmask_layers ( +const struct landlock_rule *const rule, +const access_mask_t access_request, +layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS ]); + +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)[LANDLOCK_NUM_ACCESS_FS ]);#endif /* _SECURITY_LANDLOCK_RULESET_H */
--
2.25.1
PATCH v11 6/12 landlock: Refactor layer helpers
In reply to Konstantin MeskhidzeAdd new key_type argument to the landlock_init_layer_masks() helper. Add a masks_array_size argument to the landlock_unmask_layers() helper. These modifications support implementing new rule types in the next Landlock versions.
---
Changes since v10:
* Minor fix.
Changes since v9:
* Refactors commit message.
Changes since v8:
* None.
Changes since v7:
* Refactors commit message, adds a co-developer.
* Minor fixes.
Changes since v6:
* Removes masks_size attribute from init_layer_masks().
* Refactors init_layer_masks() with new landlock_key_type.
Changes since v5:
* Splits commit.
* Formats code with clang-format-14.
Changes since v4:
* Refactors init_layer_masks(), get_handled_accesses() and unmask_layers() functions to support multiple rule types.
* Refactors landlock_get_fs_access_mask() function with LANDLOCK_MASK_ACCESS_FS mask.
Changes since v3:
* Splits commit.
* Refactors landlock_unmask_layers functions.
--- security/landlock/fs.c | 43 ++++++++++++++++++--------------- security/landlock/ruleset.c | 48 +++++++++++++++++++++++++------------ security/landlock/ruleset.h | 17 ++++++------- 3 files changed, 66 insertions(+), 42 deletions(-)
security/landlock/fs.c +24 -19
@@ -442,20 +442,22 @@ static bool is_access_to_paths_allowed( }if (unlikely (dentry_child1)) { -landlock_unmask_layers (find_rule (domain, dentry_child1), -landlock_init_layer_masks ( - domain,LANDLOCK_MASK_ACCESS_FS , - &_layer_masks_child1), - &_layer_masks_child1); +landlock_unmask_layers ( +find_rule (domain, dentry_child1), +landlock_init_layer_masks ( + domain,LANDLOCK_MASK_ACCESS_FS , + &_layer_masks_child1,LANDLOCK_KEY_INODE ), + &_layer_masks_child1,ARRAY_SIZE (_layer_masks_child1)); layer_masks_child1 = &_layer_masks_child1; child1_is_directory =d_is_dir (dentry_child1); }if (unlikely (dentry_child2)) { -landlock_unmask_layers (find_rule (domain, dentry_child2), -landlock_init_layer_masks ( - domain,LANDLOCK_MASK_ACCESS_FS , - &_layer_masks_child2), - &_layer_masks_child2); +landlock_unmask_layers ( +find_rule (domain, dentry_child2), +landlock_init_layer_masks ( + domain,LANDLOCK_MASK_ACCESS_FS , + &_layer_masks_child2,LANDLOCK_KEY_INODE ), + &_layer_masks_child2,ARRAY_SIZE (_layer_masks_child2)); layer_masks_child2 = &_layer_masks_child2; child2_is_directory =d_is_dir (dentry_child2); }
@@ -508,14 +510,15 @@ static bool is_access_to_paths_allowed( rule =find_rule (domain, walker_path.dentry); allowed_parent1 =landlock_unmask_layers ( - rule, access_masked_parent1, layer_masks_parent1); + rule, access_masked_parent1, layer_masks_parent1, +ARRAY_SIZE (*layer_masks_parent1)); allowed_parent2 =landlock_unmask_layers ( - rule, access_masked_parent2, layer_masks_parent2); + rule, access_masked_parent2, layer_masks_parent2, +ARRAY_SIZE (*layer_masks_parent2));/* Stops when a rule from each layer grants access. */ if (allowed_parent1 && allowed_parent2)break ; - jump_up:if (walker_path.dentry == walker_path.mnt->mnt_root) {if (follow_up (&walker_path)) {
@@ -554,8 +557,8 @@ static inline int check_access_path(const struct landlock_ruleset *const domain, {layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS ] = {}; - access_request = -landlock_init_layer_masks (domain, access_request, &layer_masks); + access_request =landlock_init_layer_masks ( + domain, access_request, &layer_masks,LANDLOCK_KEY_INODE );if (is_access_to_paths_allowed (domain, path, access_request, &layer_masks,NULL ,0 ,NULL ,NULL ))return 0 ;
@@ -641,7 +644,8 @@ static bool collect_domain_accesses(return true; access_dom =landlock_init_layer_masks (domain,LANDLOCK_MASK_ACCESS_FS , - layer_masks_dom); + layer_masks_dom, +LANDLOCK_KEY_INODE );dget (dir);while (true) {
@@ -649,7 +653,8 @@ static bool collect_domain_accesses(/* Gets all layers allowing all domain accesses. */ if (landlock_unmask_layers (find_rule (domain, dir), access_dom, - layer_masks_dom)) { + layer_masks_dom, +ARRAY_SIZE (*layer_masks_dom))) {/* * Stops when all handled accesses are allowed by at * least one rule in each layer.
@@ -764,7 +769,7 @@ static int current_check_refer_path(struct dentry *const old_dentry,*/ access_request_parent1 =landlock_init_layer_masks ( dom, access_request_parent1 | access_request_parent2, - &layer_masks_parent1); + &layer_masks_parent1,LANDLOCK_KEY_INODE );if (is_access_to_paths_allowed ( dom, new_dir, access_request_parent1, &layer_masks_parent1,NULL ,0 ,NULL ,NULL ))
@@ -1140,7 +1145,7 @@ static int hook_file_open(struct file *const file)if (is_access_to_paths_allowed ( dom, &file->f_path,landlock_init_layer_masks (dom, full_access_request, - &layer_masks), + &layer_masks,LANDLOCK_KEY_INODE ), &layer_masks,NULL ,0 ,NULL ,NULL )) { allowed_access = full_access_request; }else {
security/landlock/ruleset.c +33 -15
90 lines not on this page · raw message
security/landlock/ruleset.h +9 -8
@@ -266,14 +266,15 @@ landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,return landlock_get_raw_fs_access_mask (ruleset, layer_level) | LANDLOCK_ACCESS_FS_INITIALLY_DENIED; } -bool landlock_unmask_layers ( -const struct landlock_rule *const rule, -const access_mask_t access_request, -layer_mask_t (*const layer_masks)[LANDLOCK_NUM_ACCESS_FS ]); +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); -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)[LANDLOCK_NUM_ACCESS_FS ]); +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);#endif /* _SECURITY_LANDLOCK_RULESET_H */
--
2.25.1
PATCH v11 7/12 landlock: Refactor landlock_add_rule() syscall
In reply to Konstantin MeskhidzeChange the landlock_add_rule() syscall to support new rule types in future Landlock versions. Add the add_rule_path_beneath() helper to support current filesystem rules.
---
Changes since v10:
* None.
Changes since v9:
* Minor fixes:
- deletes unnecessary curly braces.
- deletes unnecessary empty line.
Changes since v8:
* Refactors commit message.
* Minor fixes.
Changes since v7:
* None
Changes since v6:
* None
Changes since v5:
* Refactors syscall landlock_add_rule() and add_rule_path_beneath() helper to make argument check ordering consistent and get rid of partial revertings in following patches.
* Rolls back refactoring base_test.c seltest.
* Formats code with clang-format-14.
Changes since v4:
* Refactors add_rule_path_beneath() and landlock_add_rule() functions to optimize code usage.
* Refactors base_test.c seltest: adds LANDLOCK_RULE_PATH_BENEATH rule type in landlock_add_rule() call.
Changes since v3:
* Split commit.
* Refactors landlock_add_rule syscall.
--- security/landlock/syscalls.c | 92 +++++++++++++++++++----------------- 1 file changed, 48 insertions(+), 44 deletions(-)
security/landlock/syscalls.c +48 -44
@@ -274,6 +274,47 @@ static int get_path_from_fd(const s32 fd, struct path *const path)return err; } +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; + +/* Copies raw user space buffer, only one type for now. */ + 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 +* (ruleset->access_masks[0] is automatically upgraded to 64-bits). +*/ + mask =landlock_get_raw_fs_access_mask (ruleset,0 ); +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; +} +/** * sys_landlock_add_rule - Add a new rule to a ruleset *
@@ -306,11 +347,8 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd, const enum landlock_rule_type, rule_type, const void __user *const, rule_attr, const __u32, flags) { - struct landlock_path_beneath_attr path_beneath_attr; -struct path path; struct landlock_ruleset *ruleset; -int res, err; -access_mask_t mask; +int err;if (!landlock_initialized)return -EOPNOTSUPP ;
@@ -324,48 +362,14 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,if (IS_ERR (ruleset))return PTR_ERR (ruleset); -if (rule_type !=LANDLOCK_RULE_PATH_BENEATH ) { +switch (rule_type) { +case LANDLOCK_RULE_PATH_BENEATH : + err =add_rule_path_beneath (ruleset, rule_attr); +break ; +default : err = -EINVAL ; - goto out_put_ruleset; - } - -/* Copies raw user space buffer, only one type for now. */ - res =copy_from_user (&path_beneath_attr, rule_attr, -sizeof (path_beneath_attr)); -if (res) { - err = -EFAULT ; - goto out_put_ruleset; +break ; } - -/* -* Informs about useless rule: empty allowed_access (i.e. deny rules) -* are ignored in path walks. -*/ -if (!path_beneath_attr.allowed_access) { - err = -ENOMSG ; - goto out_put_ruleset; - } -/* -* Checks that allowed_access matches the @ruleset constraints -* (ruleset->access_masks[0] is automatically upgraded to 64-bits). -*/ - mask =landlock_get_raw_fs_access_mask (ruleset,0 ); -if ((path_beneath_attr.allowed_access | mask) != mask) { - err = -EINVAL ; - goto out_put_ruleset; - } - -/* Gets and checks the new rule. */ - err =get_path_from_fd (path_beneath_attr.parent_fd, &path); -if (err) - goto out_put_ruleset; - -/* Imports the new rule. */ - err =landlock_append_fs_rule (ruleset, &path, - path_beneath_attr.allowed_access); -path_put (&path); - -out_put_ruleset:landlock_put_ruleset (ruleset);return err; }
--
2.25.1
PATCH v11 8/12 landlock: Add network rules and TCP hooks support
In reply to Konstantin MeskhidzeThis commit adds network rules support in the ruleset management helpers and the landlock_create_ruleset syscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expand access_masks_t to u32 to be sure network access rights can be stored. Implement socket_bind() and socket_connect() LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.
---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletes get_port() and check_addrlen() helpers.
* Refactors check_socket_access() by squashing get_port() and check_addrlen() helpers into it.
* Fixes commit message.
Changes since v9:
* Changes UAPI port field to __u64.
* Moves shared code into check_socket_access().
* Adds get_raw_handled_net_accesses() andget_current_net_domain() helpers.
* Minor fixes.
Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to __be16.
* Changes logic of bind/connect hooks with AF_UNSPEC families.
* Adds address length checking.
* Minor fixes.
Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.
Changes since v6:
* Renames landlock_set_net_access_mask() to landlock_add_net_access_mask() because it OR values.
* Makes landlock_add_net_access_mask() more resilient incorrect values.
* Refactors landlock_get_net_access_mask().
* Renames LANDLOCK_MASK_SHIFT_NET to LANDLOCK_SHIFT_ACCESS_NET and use LANDLOCK_NUM_ACCESS_FS as value.
* Updates access_masks_t to u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.
Changes since v5:
* Gets rid of partial revert from landlock_add_rule syscall.
* Formats code with clang-format-14.
Changes since v4:
* Refactors landlock_create_ruleset() - splits ruleset and masks checks.
* Refactors landlock_create_ruleset() and landlock mask setters/getters to support two rule types.
* Refactors landlock_add_rule syscall add_rule_path_beneath function by factoring out get_ruleset_from_fd() and landlock_put_ruleset().
Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Adds set_mask and get_mask for network.
* Adds rb_root root_net_port.
--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-) create mode 100644 security/landlock/net.c create mode 100644 security/landlock/net.h
include/uapi/linux/landlock.h +48 -0
@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; + +/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/*
@@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/**
@@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed)); +/** +* struct landlock_net_service_attr - TCP subnet definition +* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr { +/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +}; +/** * DOC: fs_access *
@@ -189,4 +218,23 @@ struct landlock_path_beneath_attr {#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)/* clang-format on */ +/** +* DOC: net_access +* +* Network flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of network +* actions. +* +* TCP sockets with allowed actions: +* +* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port. +* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to +* a remote port. +*/ +/* clang-format off */ +#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0) +#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) +/* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */
security/landlock/Kconfig +1 -0
@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY && !ARCH_EPHEMERAL_INODES + select SECURITY_NETWORK select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrict
security/landlock/Makefile +2 -0
@@ -2,3 +2,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o + +landlock-$(CONFIG_INET) += net.o
security/landlock/limits.h +5 -1
@@ -23,6 +23,10 @@#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)#define LANDLOCK_SHIFT_ACCESS_FS 0 -/* clang-format on */ +#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP +#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1) +#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) +#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FS +/* clang-format on */ #endif /* _SECURITY_LANDLOCK_LIMITS_H */
security/landlock/net.c new file +174 -0
174 lines not on this page · raw message
security/landlock/net.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +*/ + +#ifndef _SECURITY_LANDLOCK_NET_H +#define _SECURITY_LANDLOCK_NET_H + +#include "common.h" +#include "ruleset.h" +#include "setup.h" + +#if IS_ENABLED (CONFIG_INET ) +__init void landlock_add_net_hooks (void ); + +int landlock_append_net_rule (struct landlock_ruleset *const ruleset, +const u16 port,access_mask_t access_rights); +#else /* IS_ENABLED(CONFIG_INET) */ +static inline void landlock_add_net_hooks (void ) +{ +} +#endif /* IS_ENABLED(CONFIG_INET) */ + +#endif /* _SECURITY_LANDLOCK_NET_H */
security/landlock/ruleset.c +47 -5
119 lines not on this page · raw message
security/landlock/ruleset.h +53 -10
110 lines not on this page · raw message
security/landlock/setup.c +2 -0
@@ -14,6 +14,7 @@#include "fs.h" #include "ptrace.h" #include "setup.h" +#include "net.h" bool landlock_initialized __lsm_ro_after_init = false;
@@ -29,6 +30,7 @@ static int __init landlock_init(void)landlock_add_cred_hooks ();landlock_add_ptrace_hooks ();landlock_add_fs_hooks (); +landlock_add_net_hooks (); landlock_initialized = true;pr_info ("Up and running.\n" );return 0 ;
security/landlock/syscalls.c +66 -6
132 lines not on this page · raw message
tools/testing/selftests/landlock/base_test.c +1 -1
@@ -75,7 +75,7 @@ TEST(abi_version)const struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs =LANDLOCK_ACCESS_FS_READ_FILE , }; -ASSERT_EQ (3 ,landlock_create_ruleset (NULL ,0 , +ASSERT_EQ (4 ,landlock_create_ruleset (NULL ,0 ,LANDLOCK_CREATE_RULESET_VERSION )); ASSERT_EQ(-1, landlock_create_ruleset(&ruleset_attr, 0,
--
2.25.1
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
Quoted 10 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>
Quoted 79 lines
Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.h
[...]
security/landlock/net.c line 6
Quoted 12 lines
security/landlock/net.c new file +6
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation
You can replace these dates with "2022-2023", and same for all your other "2022" I guess.
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
6/26/2023 9:41 PM, Mickaël Salaün пишет:
Quoted 98 lines · Konstantin Meskhidze
Quoted 10 lines
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>Quoted 79 lines
Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.h[...]
Quoted 15 lines
security/landlock/net.c line 6
Quoted 12 lines
security/landlock/net.c new file +6
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation You can replace these dates with "2022-2023", and same for all your other "2022" I guess.
Ok. Thanks. I will change the dates.
Quoted 1 line
.
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
In reply to Konstantin MeskhidzeComplementary review:
Quoted 94 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.hinclude/uapi/linux/landlock.h +0
-0
include/uapi/linux/landlock.h struct landlock_ruleset_attr { · line 34
Quoted 4 lines
include/uapi/linux/landlock.h +1
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; +
Please remove this empty line.
Quoted 22 lines
include/uapi/linux/landlock.h +11
-0+/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/**
include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 95
Quoted 5 lines
include/uapi/linux/landlock.h +2
-0@@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed)); +/** +* struct landlock_net_service_attr - TCP subnet definition
s/TCP subnet definition/Network service definition/
Quoted 70 lines
include/uapi/linux/landlock.h +34
-0+* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr { +/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +}; +/** * DOC: fs_access * @@ -189,4 +218,23 @@ struct landlock_path_beneath_attr {#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)/* clang-format on */ +/** +* DOC: net_access +* +* Network flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of network +* actions. +* +* TCP sockets with allowed actions: +* +* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port. +* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to +* a remote port. +*/ +/* clang-format off */ +#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0) +#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) +/* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/Kconfig +1
-0@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY && !ARCH_EPHEMERAL_INODES + select SECURITY_NETWORK select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrictsecurity/landlock/Makefile +2
-0@@ -2,3 +2,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o + +landlock-$(CONFIG_INET) += net.osecurity/landlock/limits.h +0
-0
security/landlock/limits.h line 31
Quoted 10 lines
security/landlock/limits.h +5
-1@@ -23,6 +23,10 @@#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)#define LANDLOCK_SHIFT_ACCESS_FS 0 -/* clang-format on */ +#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP +#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1) +#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) +#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FS +/* clang-format on */
Please the empty line to make this patch clean.
Quoted 75 lines
security/landlock/limits.h +0
-0#endif /* _SECURITY_LANDLOCK_LIMITS_H */ security/landlock/net.c new file +68
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation +*/ + +#include <linux/in.h> +#include <linux/net.h> +#include <linux/socket.h> +#include <net/ipv6.h> + +#include "common.h" +#include "cred.h" +#include "limits.h" +#include "net.h" +#include "ruleset.h" + +int landlock_append_net_rule (struct landlock_ruleset *const ruleset, +const u16 port,access_mask_t access_rights) +{ +int err; +const struct landlock_id id = { + .key.data = (__force uintptr_t )htons (port), + .type =LANDLOCK_KEY_NET_PORT , + }; + +BUILD_BUG_ON (sizeof (port) >sizeof (id.key.data)); + +/* Transforms relative access rights to absolute ones. */ + access_rights |=LANDLOCK_MASK_ACCESS_NET & + ~landlock_get_net_access_mask (ruleset,0 ); + +mutex_lock (&ruleset->lock); + err =landlock_insert_rule (ruleset, id, access_rights); +mutex_unlock (&ruleset->lock); + +return err; +} + +static access_mask_t +get_raw_handled_net_accesses (const struct landlock_ruleset *const domain) +{ +access_mask_t access_dom =0 ; +size_t layer_level; + +for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) + access_dom |=landlock_get_net_access_mask (domain, layer_level); +return access_dom; +} + +static const struct landlock_ruleset *get_current_net_domain (void ) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom || !get_raw_handled_net_accesses (dom)) +return NULL ; + +return dom; +} + +static int check_socket_access (struct socket *const sock, +struct sockaddr *const address, +const int addrlen, +const access_mask_t access_request) +{
security/landlock/net.c line 78
Quoted 10 lines
security/landlock/net.c new file +10
-0+__be16 port; +layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET ] = {}; +const struct landlock_rule *rule; +access_mask_t handled_access; +struct landlock_id id = { + .type =LANDLOCK_KEY_NET_PORT , + }; +const struct landlock_ruleset *const domain =get_current_net_domain (); + +if (WARN_ON_ONCE (!domain))
The WARN_ON_ONCE() needs to be removed for processes not sandboxed. This should be printed when running the tests.
security/landlock/net.c line 88
Quoted 10 lines
security/landlock/net.c new file +10
-0+return 0 ; +if (WARN_ON_ONCE (domain->num_layers <1 )) +return -EACCES ; + +/* Checks if it's a TCP socket. */ +if (sock->type !=SOCK_STREAM ) +return 0 ; + +/* Checks for minimal header length. */ + if (addrlen < offsetofend(struct sockaddr, sa_family))
You can use "typeof(*address)" instead of struct sockaddr, this makes it easier to review.
Quoted 2 lines
security/landlock/net.c new file +2
-0+return -EINVAL ; +
[...]
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
6/26/2023 9:57 PM, Mickaël Salaün пишет:
Quoted 92 lines
Complementary review:
Quoted 89 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.h
Quoted 11 lines
include/uapi/linux/landlock.h struct landlock_ruleset_attr { · line 34
Quoted 9 lines
include/uapi/linux/landlock.h +1
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; +Please remove this empty line.
Ok. Thanks.
Quoted 27 lines
Quoted 27 lines
include/uapi/linux/landlock.h +11
-0+/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/** @@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed));
Quoted 5 lines
include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 95
Quoted 2 lines
include/uapi/linux/landlock.h +2
-0+/** +* struct landlock_net_service_attr - TCP subnet definition s/TCP subnet definition/Network service definition/
Ok. Thanks.
Quoted 80 lines
Quoted 80 lines
include/uapi/linux/landlock.h +34
-0+* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr { +/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +}; +/** * DOC: fs_access * @@ -189,4 +218,23 @@ struct landlock_path_beneath_attr {#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)/* clang-format on */ +/** +* DOC: net_access +* +* Network flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of network +* actions. +* +* TCP sockets with allowed actions: +* +* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port. +* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to +* a remote port. +*/ +/* clang-format off */ +#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0) +#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) +/* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/Kconfig +1
-0@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY && !ARCH_EPHEMERAL_INODES + select SECURITY_NETWORK select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrictsecurity/landlock/Makefile +2
-0@@ -2,3 +2,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o + +landlock-$(CONFIG_INET) += net.osecurity/landlock/limits.h +4
-1@@ -23,6 +23,10 @@#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)#define LANDLOCK_SHIFT_ACCESS_FS 0 -/* clang-format on */ +#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP +#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1) +#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) +#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FS
Quoted 3 lines
security/landlock/limits.h line 31
Quoted 1 line
security/landlock/limits.h +1
-0+/* clang-format on */ Please the empty line to make this patch clean.
Ok. I will.
Quoted 82 lines
Quoted 82 lines
security/landlock/limits.h +0
-0#endif /* _SECURITY_LANDLOCK_LIMITS_H */ security/landlock/net.c new file +73
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation +*/ + +#include <linux/in.h> +#include <linux/net.h> +#include <linux/socket.h> +#include <net/ipv6.h> + +#include "common.h" +#include "cred.h" +#include "limits.h" +#include "net.h" +#include "ruleset.h" + +int landlock_append_net_rule (struct landlock_ruleset *const ruleset, +const u16 port,access_mask_t access_rights) +{ +int err; +const struct landlock_id id = { + .key.data = (__force uintptr_t )htons (port), + .type =LANDLOCK_KEY_NET_PORT , + }; + +BUILD_BUG_ON (sizeof (port) >sizeof (id.key.data)); + +/* Transforms relative access rights to absolute ones. */ + access_rights |=LANDLOCK_MASK_ACCESS_NET & + ~landlock_get_net_access_mask (ruleset,0 ); + +mutex_lock (&ruleset->lock); + err =landlock_insert_rule (ruleset, id, access_rights); +mutex_unlock (&ruleset->lock); + +return err; +} + +static access_mask_t +get_raw_handled_net_accesses (const struct landlock_ruleset *const domain) +{ +access_mask_t access_dom =0 ; +size_t layer_level; + +for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) + access_dom |=landlock_get_net_access_mask (domain, layer_level); +return access_dom; +} + +static const struct landlock_ruleset *get_current_net_domain (void ) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom || !get_raw_handled_net_accesses (dom)) +return NULL ; + +return dom; +} + +static int check_socket_access (struct socket *const sock, +struct sockaddr *const address, +const int addrlen, +const access_mask_t access_request) +{ +__be16 port; +layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET ] = {}; +const struct landlock_rule *rule; +access_mask_t handled_access; +struct landlock_id id = {
Quoted 9 lines
security/landlock/net.c line 78
Quoted 5 lines
security/landlock/net.c new file +5
-0+ .type = LANDLOCK_KEY_NET_PORT, + }; +const struct landlock_ruleset *const domain =get_current_net_domain (); + +if (WARN_ON_ONCE (!domain))The
WARN_ON_ONCE()needs to be removed for processes not sandboxed. This should be printed when running the tests.
Probably during the boot stage it prints once since no process is landlocked at this stage.
So we need to leave it like this:
if (!domain)
return 0;????
Quoted 16 lines
security/landlock/net.c line 88
Quoted 12 lines
security/landlock/net.c new file +10
-0+return 0 ; +if (WARN_ON_ONCE (domain->num_layers <1 )) +return -EACCES ; + +/* Checks if it's a TCP socket. */ +if (sock->type !=SOCK_STREAM ) +return 0 ; + +/* Checks for minimal header length. */ + if (addrlen < offsetofend(struct sockaddr, sa_family))You can use "
typeof(*address)" instead of struct sockaddr, this makes it easier to review.
Do mean to change offsetofend() by typeof(*address)??
Quoted 6 lines
security/landlock/net.c line 90
Quoted 3 lines
security/landlock/net.c new file +2
-0+return -EINVAL ; +[...]
.
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
Quoted 317 lines · Konstantin Meskhidze (A)
6/26/2023 9:57 PM, Mickaël Salaün пишет:
Quoted 92 lines
Complementary review:
Quoted 89 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.hQuoted 11 lines
include/uapi/linux/landlock.h struct landlock_ruleset_attr { · line 34
Quoted 9 lines
include/uapi/linux/landlock.h +1
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; +Please remove this empty line.
Ok. Thanks.
Quoted 27 lines
Quoted 27 lines
include/uapi/linux/landlock.h +11
-0+/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/** @@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed));Quoted 5 lines
include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 95
Quoted 2 lines
include/uapi/linux/landlock.h +2
-0+/** +* struct landlock_net_service_attr - TCP subnet definition s/TCP subnet definition/Network service definition/
Ok. Thanks.
Quoted 80 lines
Quoted 80 lines
include/uapi/linux/landlock.h +34
-0+* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr { +/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +}; +/** * DOC: fs_access * @@ -189,4 +218,23 @@ struct landlock_path_beneath_attr {#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)/* clang-format on */ +/** +* DOC: net_access +* +* Network flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of network +* actions. +* +* TCP sockets with allowed actions: +* +* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port. +* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to +* a remote port. +*/ +/* clang-format off */ +#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0) +#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) +/* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/Kconfig +1
-0@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY && !ARCH_EPHEMERAL_INODES + select SECURITY_NETWORK select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrictsecurity/landlock/Makefile +2
-0@@ -2,3 +2,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o + +landlock-$(CONFIG_INET) += net.osecurity/landlock/limits.h +4
-1@@ -23,6 +23,10 @@#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)#define LANDLOCK_SHIFT_ACCESS_FS 0 -/* clang-format on */ +#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP +#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1) +#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) +#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FSQuoted 3 lines
security/landlock/limits.h line 31
Quoted 1 line
security/landlock/limits.h +1
-0+/* clang-format on */ Please the empty line to make this patch clean.
Ok. I will.
Quoted 82 lines
Quoted 82 lines
security/landlock/limits.h +0
-0#endif /* _SECURITY_LANDLOCK_LIMITS_H */ security/landlock/net.c new file +73
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation +*/ + +#include <linux/in.h> +#include <linux/net.h> +#include <linux/socket.h> +#include <net/ipv6.h> + +#include "common.h" +#include "cred.h" +#include "limits.h" +#include "net.h" +#include "ruleset.h" + +int landlock_append_net_rule (struct landlock_ruleset *const ruleset, +const u16 port,access_mask_t access_rights) +{ +int err; +const struct landlock_id id = { + .key.data = (__force uintptr_t )htons (port), + .type =LANDLOCK_KEY_NET_PORT , + }; + +BUILD_BUG_ON (sizeof (port) >sizeof (id.key.data)); + +/* Transforms relative access rights to absolute ones. */ + access_rights |=LANDLOCK_MASK_ACCESS_NET & + ~landlock_get_net_access_mask (ruleset,0 ); + +mutex_lock (&ruleset->lock); + err =landlock_insert_rule (ruleset, id, access_rights); +mutex_unlock (&ruleset->lock); + +return err; +} + +static access_mask_t +get_raw_handled_net_accesses (const struct landlock_ruleset *const domain) +{ +access_mask_t access_dom =0 ; +size_t layer_level; + +for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) + access_dom |=landlock_get_net_access_mask (domain, layer_level); +return access_dom; +} + +static const struct landlock_ruleset *get_current_net_domain (void ) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom || !get_raw_handled_net_accesses (dom)) +return NULL ; + +return dom; +} + +static int check_socket_access (struct socket *const sock, +struct sockaddr *const address, +const int addrlen, +const access_mask_t access_request) +{ +__be16 port; +layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET ] = {}; +const struct landlock_rule *rule; +access_mask_t handled_access; +struct landlock_id id = {Quoted 9 lines
security/landlock/net.c line 78
Quoted 5 lines
security/landlock/net.c new file +5
-0+ .type = LANDLOCK_KEY_NET_PORT, + }; +const struct landlock_ruleset *const domain =get_current_net_domain (); + +if (WARN_ON_ONCE (!domain))The
WARN_ON_ONCE()needs to be removed for processes not sandboxed. This should be printed when running the tests.
Quoted 5 lines
Probably during the boot stage it prints once since no process is landlocked at this stage.
So we need to leave it like this:if (!domain) return 0;
Yes
Quoted 17 lines
????Quoted 16 lines
security/landlock/net.c line 88
Quoted 12 lines
security/landlock/net.c new file +10
-0+return 0 ; +if (WARN_ON_ONCE (domain->num_layers <1 )) +return -EACCES ; + +/* Checks if it's a TCP socket. */ +if (sock->type !=SOCK_STREAM ) +return 0 ; + +/* Checks for minimal header length. */ + if (addrlen < offsetofend(struct sockaddr, sa_family))You can use "
typeof(*address)" instead of struct sockaddr, this makes it easier to review.
Quoted 1 line
Do mean to change
offsetofend()bytypeof(*address)??
No: if (addrlen < offsetofend(typeof(*address), sa_family))
Quoted 6 lines
Quoted 6 lines
security/landlock/net.c line 90
Quoted 3 lines
security/landlock/net.c new file +2
-0+return -EINVAL ; +[...]
.
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
7/3/2023 8:06 PM, Mickaël Salaün пишет:
Quoted 345 lines · Konstantin Meskhidze (A)
Quoted 317 lines
6/26/2023 9:57 PM, Mickaël Salaün пишет:
Quoted 92 lines
Complementary review:
Quoted 89 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.hQuoted 11 lines
include/uapi/linux/landlock.h struct landlock_ruleset_attr { · line 34
Quoted 9 lines
include/uapi/linux/landlock.h +1
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; +Please remove this empty line.
Ok. Thanks.
Quoted 27 lines
Quoted 27 lines
include/uapi/linux/landlock.h +11
-0+/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/** @@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed));Quoted 5 lines
include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 95
Quoted 2 lines
include/uapi/linux/landlock.h +2
-0+/** +* struct landlock_net_service_attr - TCP subnet definition s/TCP subnet definition/Network service definition/
Ok. Thanks.
Quoted 80 lines
Quoted 80 lines
include/uapi/linux/landlock.h +34
-0+* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr { +/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +}; +/** * DOC: fs_access * @@ -189,4 +218,23 @@ struct landlock_path_beneath_attr {#define LANDLOCK_ACCESS_FS_TRUNCATE (1ULL << 14)/* clang-format on */ +/** +* DOC: net_access +* +* Network flags +* ~~~~~~~~~~~~~~~~ +* +* These flags enable to restrict a sandboxed process to a set of network +* actions. +* +* TCP sockets with allowed actions: +* +* - %LANDLOCK_ACCESS_NET_BIND_TCP: Bind a TCP socket to a local port. +* - %LANDLOCK_ACCESS_NET_CONNECT_TCP: Connect an active TCP socket to +* a remote port. +*/ +/* clang-format off */ +#define LANDLOCK_ACCESS_NET_BIND_TCP (1ULL << 0) +#define LANDLOCK_ACCESS_NET_CONNECT_TCP (1ULL << 1) +/* clang-format on */ #endif /* _UAPI_LINUX_LANDLOCK_H */ security/landlock/Kconfig +1
-0@@ -3,6 +3,7 @@ config SECURITY_LANDLOCK bool "Landlock support" depends on SECURITY && !ARCH_EPHEMERAL_INODES + select SECURITY_NETWORK select SECURITY_PATH help Landlock is a sandboxing mechanism that enables processes to restrictsecurity/landlock/Makefile +2
-0@@ -2,3 +2,5 @@ obj-$(CONFIG_SECURITY_LANDLOCK) := landlock.o landlock-y := setup.o syscalls.o object.o ruleset.o \ cred.o ptrace.o fs.o + +landlock-$(CONFIG_INET) += net.osecurity/landlock/limits.h +4
-1@@ -23,6 +23,10 @@#define LANDLOCK_NUM_ACCESS_FS __const_hweight64(LANDLOCK_MASK_ACCESS_FS)#define LANDLOCK_SHIFT_ACCESS_FS 0 -/* clang-format on */ +#define LANDLOCK_LAST_ACCESS_NET LANDLOCK_ACCESS_NET_CONNECT_TCP +#define LANDLOCK_MASK_ACCESS_NET ((LANDLOCK_LAST_ACCESS_NET << 1) - 1) +#define LANDLOCK_NUM_ACCESS_NET __const_hweight64(LANDLOCK_MASK_ACCESS_NET) +#define LANDLOCK_SHIFT_ACCESS_NET LANDLOCK_NUM_ACCESS_FSQuoted 3 lines
security/landlock/limits.h line 31
Quoted 1 line
security/landlock/limits.h +1
-0+/* clang-format on */ Please the empty line to make this patch clean.
Ok. I will.
Quoted 82 lines
Quoted 82 lines
security/landlock/limits.h +0
-0#endif /* _SECURITY_LANDLOCK_LIMITS_H */ security/landlock/net.c new file +73
-0@@ -0,0 +1,174 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock LSM - Network management and hooks +* +* Copyright © 2022 Huawei Tech. Co., Ltd. +* Copyright © 2022 Microsoft Corporation +*/ + +#include <linux/in.h> +#include <linux/net.h> +#include <linux/socket.h> +#include <net/ipv6.h> + +#include "common.h" +#include "cred.h" +#include "limits.h" +#include "net.h" +#include "ruleset.h" + +int landlock_append_net_rule (struct landlock_ruleset *const ruleset, +const u16 port,access_mask_t access_rights) +{ +int err; +const struct landlock_id id = { + .key.data = (__force uintptr_t )htons (port), + .type =LANDLOCK_KEY_NET_PORT , + }; + +BUILD_BUG_ON (sizeof (port) >sizeof (id.key.data)); + +/* Transforms relative access rights to absolute ones. */ + access_rights |=LANDLOCK_MASK_ACCESS_NET & + ~landlock_get_net_access_mask (ruleset,0 ); + +mutex_lock (&ruleset->lock); + err =landlock_insert_rule (ruleset, id, access_rights); +mutex_unlock (&ruleset->lock); + +return err; +} + +static access_mask_t +get_raw_handled_net_accesses (const struct landlock_ruleset *const domain) +{ +access_mask_t access_dom =0 ; +size_t layer_level; + +for (layer_level =0 ; layer_level < domain->num_layers; layer_level++) + access_dom |=landlock_get_net_access_mask (domain, layer_level); +return access_dom; +} + +static const struct landlock_ruleset *get_current_net_domain (void ) +{ +const struct landlock_ruleset *const dom = +landlock_get_current_domain (); + +if (!dom || !get_raw_handled_net_accesses (dom)) +return NULL ; + +return dom; +} + +static int check_socket_access (struct socket *const sock, +struct sockaddr *const address, +const int addrlen, +const access_mask_t access_request) +{ +__be16 port; +layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET ] = {}; +const struct landlock_rule *rule; +access_mask_t handled_access; +struct landlock_id id = {Quoted 9 lines
security/landlock/net.c line 78
Quoted 5 lines
security/landlock/net.c new file +5
-0+ .type = LANDLOCK_KEY_NET_PORT, + }; +const struct landlock_ruleset *const domain =get_current_net_domain (); + +if (WARN_ON_ONCE (!domain))The
WARN_ON_ONCE()needs to be removed for processes not sandboxed. This should be printed when running the tests.Quoted 5 lines
Probably during the boot stage it prints once since no process is landlocked at this stage.
So we need to leave it like this:if (!domain) return 0;Yes
Quoted 17 lines
????Quoted 16 lines
security/landlock/net.c line 88
Quoted 12 lines
security/landlock/net.c new file +10
-0+return 0 ; +if (WARN_ON_ONCE (domain->num_layers <1 )) +return -EACCES ; + +/* Checks if it's a TCP socket. */ +if (sock->type !=SOCK_STREAM ) +return 0 ; + +/* Checks for minimal header length. */ + if (addrlen < offsetofend(struct sockaddr, sa_family))You can use "
typeof(*address)" instead of struct sockaddr, this makes it easier to review.
Quoted 4 lines
Quoted 1 line
Do mean to change
offsetofend()bytypeof(*address)??No: if (addrlen <
offsetofend(typeof(*address), sa_family))
Got it. Thanks.
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
security/landlock/net.c line 90
Quoted 3 lines
security/landlock/net.c new file +2
-0+return -EINVAL ; +[...]
..
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
In reply to Konstantin MeskhidzeQuoted 129 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.hinclude/uapi/linux/landlock.h +18
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; + +/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/** @@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed)); +/** +* struct landlock_net_service_attr - TCP subnet definition +* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr {
include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 109
Quoted 10 lines
include/uapi/linux/landlock.h +10
-0+/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +};
The "net service" name reflects the semantic but it doesn't fit well with the data type. It works with TCP, UDP and other protocols such as VSOCK, but not unix sockets. I think it makes more sense to rename LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT and landlock_net_service_attr to landlock_net_port_attr. Please be careful with the documentation, non-kernel code, and comments as well.
In the future, we could add a landlock_net_path_attr to identify a unix abstract path, which would also be a network service, but it would not accept the TCP access rights.
The access right names (LANDLOCK_ACCESS_NET_{BIND,CONNECT}_TCP) are still good.
I'm still improving tests, so you might wait a bit before renaming the related files.
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
Quoted 10 lines · Mickaël Salaün
Quoted 8 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.
It is important to explain the decision rationales. Please explain new types, something like this:
The new landlock_net_port_attr structure has two fields. The allowed_access field contains the LANDLOCK_ACCESS_NET_* rights. The port field contains the port value according to the the allowed protocol. This field can take up to a 64-bit value [1] but the maximum value depends on the related protocol (e.g. 16-bit for TCP).
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
6/29/2023 5:04 PM, Mickaël Salaün пишет:
Quoted 13 lines · Mickaël Salaün
Quoted 10 lines · Konstantin Meskhidze
Quoted 8 lines
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.
Quoted 9 lines
It is important to explain the decision rationales. Please explain new types, something like this:
The new
landlock_net_port_attrstructure has two fields. Theallowed_accessfield contains the LANDLOCK_ACCESS_NET_* rights. The port field contains the port value according to the the allowed protocol. This field can take up to a 64-bit value [1] but the maximum value depends on the related protocol (e.g. 16-bit for TCP).
Ok. Thanks.
Quoted 3 lines
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
In reply to Mickaël Salaün6/27/2023 7:14 PM, Mickaël Salaün пишет:
Quoted 149 lines · Konstantin Meskhidze
Quoted 129 lines
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
Changes since v10:
* Removes "packed" attribute.
* Applies Mickaёl's patch with some refactoring.
* Deletesget_port()andcheck_addrlen()helpers.
* Refactorscheck_socket_access()by squashingget_port()andcheck_addrlen()helpers into it.
* Fixes commit message.Changes since v9:
* Changes UAPI port field to__u64.
* Moves shared code intocheck_socket_access().
* Addsget_raw_handled_net_accesses()andget_current_net_domain()helpers.
* Minor fixes.Changes since v8:
* Squashes commits.
* Refactors commit message.
* Changes UAPI port field to__be16.
* Changes logic of bind/connect hooks withAF_UNSPECfamilies.
* Adds address length checking.
* Minor fixes.Changes since v7:
* Squashes commits.
* Increments ABI version to 4.
* Refactors commit message.
* Minor fixes.Changes since v6:
* Renameslandlock_set_net_access_mask()tolandlock_add_net_access_mask()because it OR values.
* Makeslandlock_add_net_access_mask()more resilient incorrect values.
* Refactorslandlock_get_net_access_mask().
* RenamesLANDLOCK_MASK_SHIFT_NETtoLANDLOCK_SHIFT_ACCESS_NETand useLANDLOCK_NUM_ACCESS_FSas value.
* Updatesaccess_masks_tto u32 to support network access actions.
* Refactors landlock internal functions to support network actions with landlock_key/key_type/id types.Changes since v5:
* Gets rid of partial revert fromlandlock_add_rulesyscall.
* Formats code with clang-format-14.Changes since v4:
* Refactorslandlock_create_ruleset()- splits ruleset and masks checks.
* Refactorslandlock_create_ruleset()and landlock mask setters/getters to support two rule types.
* Refactorslandlock_add_rulesyscalladd_rule_path_beneathfunction by factoring outget_ruleset_from_fd()andlandlock_put_ruleset().Changes since v3:
* Splits commit.
* Adds network rule support for internal landlock functions.
* Addsset_maskandget_maskfor network.
* Addsrb_rootroot_net_port.--- include/uapi/linux/landlock.h | 48 +++++ security/landlock/Kconfig | 1 + security/landlock/Makefile | 2 + security/landlock/limits.h | 6 +- security/landlock/net.c | 174 +++++++++++++++++++ security/landlock/net.h | 26 +++ security/landlock/ruleset.c | 52 +++++- security/landlock/ruleset.h | 63 +++++-- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 72 +++++++- tools/testing/selftests/landlock/base_test.c | 2 +- 11 files changed, 425 insertions(+), 23 deletions(-)create mode 100644
security/landlock/net.c
create mode 100644security/landlock/net.hinclude/uapi/linux/landlock.h +18
-0@@ -31,6 +31,13 @@ struct landlock_ruleset_attr {* this access right. */ __u64 handled_access_fs; + +/** +* @handled_access_net: Bitmask of actions (cf. `Network flags`_) +* that is handled by this ruleset and should then be forbidden if no +* rule explicitly allow them. +*/ +__u64 handled_access_net; };/* @@ -54,6 +61,11 @@ enum landlock_rule_type {* landlock_path_beneath_attr . */ LANDLOCK_RULE_PATH_BENEATH =1 , +/** +* @LANDLOCK_RULE_NET_SERVICE: Type of a &struct +* landlock_net_service_attr . +*/ + LANDLOCK_RULE_NET_SERVICE = 2, };/** @@ -79,6 +91,23 @@ struct landlock_path_beneath_attr {*/ } __attribute__((packed)); +/** +* struct landlock_net_service_attr - TCP subnet definition +* +* Argument of sys_landlock_add_rule(). +*/ +struct landlock_net_service_attr {include/uapi/linux/landlock.h struct landlock_path_beneath_attr { · line 109
Quoted 10 lines
include/uapi/linux/landlock.h +10
-0+/** +* @allowed_access: Bitmask of allowed access network for services +* (cf. `Network flags`_). +*/ +__u64 allowed_access; +/** +* @port: Network port. +*/ +__u64 port; +};The "net service" name reflects the semantic but it doesn't fit well with the data type. It works with TCP, UDP and other protocols such as VSOCK, but not unix sockets. I think it makes more sense to rename
LANDLOCK_RULE_NET_SERVICEtoLANDLOCK_RULE_NET_PORTandlandlock_net_service_attrtolandlock_net_port_attr. Please be careful with the documentation, non-kernel code, and comments as well.
Quoted 7 lines
In the future, we could add a
landlock_net_path_attrto identify a unix abstract path, which would also be a network service, but it would not accept the TCP access rights.The access right names (LANDLOCK_ACCESS_NET_{BIND,CONNECT}_TCP) are still good.
Ok. "net service" will be renamed.
Thanks.
Quoted 4 lines
I'm still improving tests, so you might wait a bit before renaming the related files.
.
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
In reply to Konstantin Meskhidzesecurity/landlock/syscalls.c static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, · line 340
Quoted 9 lines · Konstantin Meskhidze
security/landlock/syscalls.c +9
-0+static int add_rule_net_service (struct landlock_ruleset *ruleset, +const void __user *const rule_attr) +{ +#if IS_ENABLED (CONFIG_INET ) +struct landlock_net_service_attr net_service_attr; +int res; +access_mask_t mask; + +/* Copies raw user space buffer, only one type for now. */
Nit, which I came across by accident: I believe the remark "only one type for now" referred to the fact that path_beneath_attr was the only rule type up until now - but that is not true any more. Please adapt the wording also in add_rule_path_beneath, where the same comment exists.
–Günther
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
6/27/2023 10:48 PM, Günther Noack пишет:
Quoted 8 lines · Konstantin Meskhidze
Quoted 7 lines
security/landlock/syscalls.c +7
-0+static int add_rule_net_service (struct landlock_ruleset *ruleset, +const void __user *const rule_attr) +{ +#if IS_ENABLED (CONFIG_INET ) +struct landlock_net_service_attr net_service_attr; +int res; +access_mask_t mask;
Quoted 9 lines
security/landlock/syscalls.c static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, · line 340
Quoted 2 lines
security/landlock/syscalls.c +2
-0+ +/* Copies raw user space buffer, only one type for now. */ Nit, which I came across by accident: I believe the remark "only one type for now" referred to the fact that
path_beneath_attrwas the only rule type up until now - but that is not true any more. Please adapt the wording also inadd_rule_path_beneath, where the same comment exists.
Thanks. I will fix it.
Quoted 2 lines
–Günther
.
Mickaël Salaün
maintainerRe: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
In reply to Konstantin MeskhidzeQuoted 12 lines · Konstantin Meskhidze
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
[...]
Quoted 4 lines
security/landlock/syscalls.c +0
-0
[...]
security/landlock/syscalls.c static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, · line 335
Quoted 4 lines
security/landlock/syscalls.c +4
-0+static int add_rule_net_service (struct landlock_ruleset *ruleset, +const void __user *const rule_attr) +{ +#if IS_ENABLED (CONFIG_INET )
We should define two add_rule_net_service() functions according to IS_ENABLED(CONFIG_INET) instead of changing the body of the only function. The second function would only return -EAFNOSUPPORT. This cosmetic change would make the code cleaner.
Quoted 36 lines
security/landlock/syscalls.c +36
-0+struct landlock_net_service_attr net_service_attr; +int res; +access_mask_t mask; + +/* Copies raw user space buffer, only one type for now. */ + res =copy_from_user (&net_service_attr, rule_attr, +sizeof (net_service_attr)); +if (res) +return -EFAULT ; + +/* +* Informs about useless rule: empty allowed_access (i.e. deny rules) +* are ignored by network actions. +*/ +if (!net_service_attr.allowed_access) +return -ENOMSG ; + +/* +* Checks that allowed_access matches the @ruleset constraints +* (ruleset->access_masks[0] is automatically upgraded to 64-bits). +*/ + mask =landlock_get_net_access_mask (ruleset,0 ); +if ((net_service_attr.allowed_access | mask) != mask) +return -EINVAL ; + +/* Denies inserting a rule with port 0 or higher than 65535. */ +if ((net_service_attr.port ==0 ) || (net_service_attr.port >U16_MAX )) +return -EINVAL ; + +/* Imports the new rule. */ +return landlock_append_net_rule (ruleset, net_service_attr.port, + net_service_attr.allowed_access); +#else /* IS_ENABLED(CONFIG_INET) */ +return -EAFNOSUPPORT ; +#endif /* IS_ENABLED(CONFIG_INET) */ +}
Re: [PATCH v11 08/12] landlock: Add network rules and TCP hooks support
8/3/2023 5:12 PM, Mickaël Salaün пишет:
Quoted 23 lines · Konstantin Meskhidze
Quoted 12 lines
This commit adds network rules support in the ruleset management helpers and the
landlock_create_rulesetsyscall.
Refactor user space API to support network actions. Add new network access flags, network rule and network attributes. Increment Landlock ABI version. Expandaccess_masks_tto u32 to be sure network access rights can be stored. Implementsocket_bind()andsocket_connect()LSM hooks, which enables to restrict TCP socket binding and connection to specific ports.Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Konstantin Meskhidze <[email protected]>---
[...]
Quoted 4 lines
security/landlock/syscalls.c +0
-0[...]
Quoted 9 lines
security/landlock/syscalls.c static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, · line 335
Quoted 4 lines
security/landlock/syscalls.c +4
-0+static int add_rule_net_service (struct landlock_ruleset *ruleset, +const void __user *const rule_attr) +{ +#if IS_ENABLED (CONFIG_INET )We should define two
add_rule_net_service()functions according toIS_ENABLED(CONFIG_INET)instead of changing the body of the only function. The second function would only return-EAFNOSUPPORT. This cosmetic change would make the code cleaner.
Ok. Got it.
Quoted 39 lines
Quoted 28 lines
security/landlock/syscalls.c +26
-0+struct landlock_net_service_attr net_service_attr; +int res; +access_mask_t mask; + +/* Copies raw user space buffer, only one type for now. */ + res =copy_from_user (&net_service_attr, rule_attr, +sizeof (net_service_attr)); +if (res) +return -EFAULT ; + +/* +* Informs about useless rule: empty allowed_access (i.e. deny rules) +* are ignored by network actions. +*/ +if (!net_service_attr.allowed_access) +return -ENOMSG ; + +/* +* Checks that allowed_access matches the @ruleset constraints +* (ruleset->access_masks[0] is automatically upgraded to 64-bits). +*/ + mask =landlock_get_net_access_mask (ruleset,0 ); +if ((net_service_attr.allowed_access | mask) != mask) +return -EINVAL ; + +/* Denies inserting a rule with port 0 or higher than 65535. */ security/landlock/syscalls.c static int add_rule_path_beneath(struct landlock_ruleset *const ruleset, · line 371
Quoted 10 lines
security/landlock/syscalls.c +10
-0+if ((net_service_attr.port ==0 ) || (net_service_attr.port >U16_MAX )) +return -EINVAL ; + +/* Imports the new rule. */ +return landlock_append_net_rule (ruleset, net_service_attr.port, + net_service_attr.allowed_access); +#else /* IS_ENABLED(CONFIG_INET) */ +return -EAFNOSUPPORT ; +#endif /* IS_ENABLED(CONFIG_INET) */ +}.
PATCH v11 9/12 selftests/landlock: Share enforce_ruleset()
In reply to Konstantin MeskhidzeThis commit moves enforce_ruleset() helper function to common.h so that it can be used both by filesystem tests and network ones.
---
Changes since v10:
* Refactors commit message.
Changes since v9:
* None.
Changes since v8:
* Adds __maybe_unused attribute for enforce_ruleset() helper.
Changes since v7:
* Refactors commit message.
Changes since v6:
* None.
Changes since v5:
* Splits commit.
* Moves enforce_ruleset helper into common.h
* Formats code with clang-format-14.
--- tools/testing/selftests/landlock/common.h | 10 ++++++++++ tools/testing/selftests/landlock/fs_test.c | 10 ---------- 2 files changed, 10 insertions(+), 10 deletions(-)
tools/testing/selftests/landlock/common.h +10 -0
@@ -256,3 +256,13 @@ static int __maybe_unused send_fd(int usock, int fd_tx)return -errno;return 0 ; } + +static void __maybe_unused +enforce_ruleset (struct __test_metadata *const _metadata,const int ruleset_fd) +{ +ASSERT_EQ (0 ,prctl (PR_SET_NO_NEW_PRIVS ,1 ,0 ,0 ,0 )); + ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)) + { +TH_LOG ("Failed to enforce ruleset: %s" ,strerror (errno)); + } +}
tools/testing/selftests/landlock/fs_test.c +0 -10
@@ -598,16 +598,6 @@ static int create_ruleset(struct __test_metadata *const _metadata,return ruleset_fd; } -static void enforce_ruleset (struct __test_metadata *const _metadata, -const int ruleset_fd) -{ -ASSERT_EQ (0 ,prctl (PR_SET_NO_NEW_PRIVS ,1 ,0 ,0 ,0 )); - ASSERT_EQ(0, landlock_restrict_self(ruleset_fd, 0)) - { -TH_LOG ("Failed to enforce ruleset: %s" ,strerror (errno)); - } -} - TEST_F_FORK(layout1, proc_nsfs) {const struct rule rules[] = {
--
2.25.1
PATCH v11 10/12 selftests/landlock: Add 11 new test suites dedicated to network
In reply to Konstantin MeskhidzeThese test suites try to check edge cases for TCP sockets bind() and connect() actions.
inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action with AF_UNSPEC socket family.
* connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action with AF_UNSPEC socket family.
* ruleset_overlap: Tests with overlapping rules for one port.
* ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
* inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more than U16_MAX.
port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
* bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.
layout1:
* with_net: Tests with network bind() socket action within filesystem directory access test.
Test coverage for security/landlock is 94.8% of 934 lines according to gcc/gcov-11.
---
Changes since v10:
* Replaces FIXTURE_VARIANT() with struct _fixture_variant_ .
* Changes tests names socket -> inet, standalone -> port.
* Gets rid of some DEFINEs.
* Changes names and groups tests' variables.
* Changes create_socket_variant() helper name to socket_variant().
* Refactors FIXTURE_SETUP(port) logic.
* Changes TEST_F_FORK -> TEST_F since there no teardown.
* Refactors some tests' logic.
* Minor fixes.
* Refactors commit message.
Changes since v9:
* Fixes mixing code declaration and code.
* Refactors FIXTURE_TEARDOWN() with clang-format.
* Replaces struct _fixture_variant_socket with FIXTURE_VARIANT(socket).
* Deletes useless condition if (variant->is_sandboxed) in multiple locations.
* Deletes zero_size argument in bind_variant() and connect_variant().
* Adds tests for port values exceeding U16_MAX.
Changes since v8:
* Adds is_sandboxed const for FIXTURE_VARIANT(socket).
* Refactors AF_UNSPEC tests.
* Adds address length checking tests.
* Convert ports in all tests to __be16.
* Adds invalid port values tests.
* Minor fixes.
Changes since v7:
* Squashes all selftest commits.
* Adds fs test with network bind() socket action.
* Minor fixes.
--- tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 64 + tools/testing/selftests/landlock/net_test.c | 1317 +++++++++++++++++++ 3 files changed, 1385 insertions(+) create mode 100644 tools/testing/selftests/landlock/net_test.c
tools/testing/selftests/landlock/config +4 -0
@@ -1,3 +1,7 @@ +CONFIG_INET=y +CONFIG_IPV6=y +CONFIG_NET=y +CONFIG_NET_NS=y CONFIG_OVERLAY_FS=y CONFIG_SECURITY_LANDLOCK=y CONFIG_SECURITY_PATH=y
tools/testing/selftests/landlock/fs_test.c +64 -0
82 lines not on this page · raw message
tools/testing/selftests/landlock/net_test.c new file +1317 -0
1,317 lines not on this page · raw message
--
2.25.1
Re: [PATCH v11 10/12] selftests/landlock: Add 11 new test suites dedicated to network
Hi!
Quoted 1 line · Konstantin Meskhidze
+TEST_F(inet, bind)
If you are using TEST_F() and you are enforcing a Landlock ruleset within that test, doesn't that mean that the same Landlock ruleset is now also enabled on other tests that get run after that test?
Most of the other Landlock selftests use TEST_F_FORK() for that reason, so that the Landlock enforcement stays local to the specific test, and does not accidentally influence the observed behaviour in other tests.
The same question applies to other test functions in this file as well.
–Günther
Mickaël Salaün
maintainerRe: [PATCH v11 10/12] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 5 lines · Günther Noack
Hi!
Quoted 1 line · Konstantin Meskhidze
+TEST_F(inet, bind)
Quoted 8 lines
If you are using
TEST_F()and you are enforcing a Landlock ruleset within that test, doesn't that mean that the same Landlock ruleset is now also enabled on other tests that get run after that test?Most of the other Landlock selftests use
TEST_F_FORK()for that reason, so that the Landlock enforcement stays local to the specific test, and does not accidentally influence the observed behaviour in other tests.
Initially Konstantin wrote tests with TEST_F_FORK() but I asked him to only use TEST_F() because TEST_F_FORK() is only useful when a FIXTURE_TEARDOWN() needs access rights that were dropped with a TEST_F(), e.g. to unmount mount points set up with a FIXTURE_SETUP() while Landlock restricted a test process.
Indeed, TEST_F() already fork() to make sure there is no side effect with tests.
Quoted 5 lines
The same question applies to other test functions in this file as well.
–Günther
Re: [PATCH v11 10/12] selftests/landlock: Add 11 new test suites dedicated to network
7/2/2023 11:45 AM, Mickaël Salaün пишет:
Quoted 16 lines · Günther Noack
Quoted 5 lines
Hi!
Quoted 1 line · Konstantin Meskhidze
+TEST_F(inet, bind)
Quoted 8 lines
If you are using
TEST_F()and you are enforcing a Landlock ruleset within that test, doesn't that mean that the same Landlock ruleset is now also enabled on other tests that get run after that test?Most of the other Landlock selftests use
TEST_F_FORK()for that reason, so that the Landlock enforcement stays local to the specific test, and does not accidentally influence the observed behaviour in other tests.
Quoted 9 lines
Initially Konstantin wrote tests with
TEST_F_FORK()but I asked him to only useTEST_F()becauseTEST_F_FORK()is only useful when aFIXTURE_TEARDOWN()needs access rights that were dropped with aTEST_F(), e.g. to unmount mount points set up with aFIXTURE_SETUP()while Landlock restricted a test process.Indeed,
TEST_F()alreadyfork()to make sure there is no side effect with tests.
Hi, Günther
Yep. Mickaёl asked me to replace TEST_F_FORK() with TEST_F(). Please
check this thread
https://lore.kernel.org/netdev/[email protected]/
T
Quoted 6 lines
Quoted 5 lines
The same question applies to other test functions in this file as well.
–Günther
.
Günther Noack
reviewerRe: [PATCH v11 10/12] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 11 lines · Konstantin Meskhidze (A)
7/2/2023 11:45 AM, Mickaël Salaün пишет:
Quoted 9 lines
Initially Konstantin wrote tests with
TEST_F_FORK()but I asked him to only useTEST_F()becauseTEST_F_FORK()is only useful when aFIXTURE_TEARDOWN()needs access rights that were dropped with aTEST_F(), e.g. to unmount mount points set up with aFIXTURE_SETUP()while Landlock restricted a test process.Indeed,
TEST_F()alreadyfork()to make sure there is no side effect with tests.
Quoted 6 lines
Hi, Günther
Yep. Mickaёl asked me to replaceTEST_F_FORK()withTEST_F(). Please check
this thread
Ah thanks, this makes a lot of sense!
I had not realized that TEST_F would also fork a child process, I should have double checked the selftest implementation.
—Günther
Signature · 1 line
Sent using Mutt 🐕 Woof Woof
Mickaël Salaün
maintainerPATCH 1/1 selftests/landlock: Add 11 new test suites dedicated to network
In reply to Konstantin MeskhidzeFrom: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
These test suites try to check edge cases for TCP sockets bind() and connect() actions.
inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action with AF_UNSPEC socket family.
* connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action with AF_UNSPEC socket family.
* ruleset_overlap: Tests with overlapping rules for one port.
* ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
* inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more than U16_MAX.
port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
* bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.
layout1:
* with_net: Tests with network bind() socket action within filesystem directory access test.
Test coverage for security/landlock is 94.8% of 934 lines according to gcc/gcov-11.
---
Changes since v11 (from Mickaël Salaün):
- Add ipv4.from_unix_to_tcp test suite to check that socket family is the same between a socket and a sockaddr by trying to connect/bind on a unix socket (stream or dgram) using an inet family. Landlock should not change the error code. This found a bug (which needs to be fixed) with the TCP restriction.
- Revamp the inet.{bind,connect} tests into protocol.{bind,connect}:
- Merge bind_connect_unix_dgram_socket, bind_connect_unix_dgram_socket and bind_connect_inval_addrlen into it: add a full test matrix of IPv4/TCP, IPv6/TCP, IPv4/UDP, IPv6/UDP, unix/stream, unix/dgram, all of them with or without sandboxing. This improve coverage and it enables to check that a TCP restriction work as expected but doesn't restrict other stream or datagram protocols. This also enables to check consistency of the network stack with or without Landlock. We now have 76 test suites for the network.
- Add full send/recv checks.
- Make a generic framework that will be ready for future protocol supports.
- Replace most ASSERT with EXPECT according to the criticity of an
action: if we can get more meaningful information with following checks. For instance, failure to create a kernel object (e.g. socket(), accept() or fork() call) is critical if it is used by following checks. For Landlock ruleset building, the following checks don't make sense if the sandbox is not complete. However, it doesn't make sense to continue a FIXTURE_SETUP() if any check failed.
- Add a new unspec fixture to replace inet.bind_afunspec with unspec.bind and inet.connect_afunspec with unspec.connect, factoring and simplifying code.
- Replace inet.bind_afunspec with protocol.bind_unspec, and inet.connect_afunspec with protocol.connect_unspec. Extend these tests with the matrix of all "protocol" variants. Don't test connect with the same socket which is already binded/listening (I guess this was an copy-paste error). The protocol.bind_unspec tests found a bug (which needs to be fixed).
- Add and use set_service() and setup_loopback() helpers to configure network services. Add and use and test_bind_and_connect() to factor out a lot of checks.
- Add new types (protocol_variant, service_fixture) and update related helpers to get more generic test code.
- Replace static (port) arrays with service_fixture variables.
- Add new helpers: {bind,connect}_variant_addrlen() and get_addrlen() to cover all protocols with previous bind_connect_inval_addrlen tests. Make them return -errno in case of error.
- Switch from a unix socket path address to an abstract one. This enables to avoid file cleanup in test teardowns.
- Close all rulesets after enforcement.
- Remove the duplicate "empty access" test.
- Replace inet.ruleset_overlay with tcp_layers.ruleset_overlap and simplify test:
- Always run sandbox tests because test were always run sandboxed and it doesn't give more guarantees to do it not sandboxed.
- Rewrite test with variant->num_layers to make it simpler and configurable.
- Add another test layer to tcp_layers used for ruleset_overlap and test without sandbox.
- Leverage test_bind_and_connect() and avoid using SO_REUSEADDR because the socket was not listened to, and don't use the same socket/FD for server and client.
- Replace inet.ruleset_expanding with tcp_layers.ruleset_expand.
- Drop capabilities in all FIXTURE_SETUP().
- Change test ports to cover more ranges.
- Add "mini" tests:
- Replace the invalid ruleset attribute test from port.inval with mini.unknow_access_rights.
- Simplify port.inval and move some code to other mini.* tests.
- Add new mini.network_access_rights test.
- Rewrite inet.inval_port_format into mini.tcp_port_overflow:
- Remove useless is_sandbox checks.
- Extend tests with bind/connect checks.
- Interleave valid requests with invalid ones.
- Add two_srv.port_endianness test, extracted and extended from inet.inval_port_format .
- Add Microsoft copyright.
- Rename some variables to make them easier to read.
- Constify variables.
- Add minimal logs to help debug test failures.
--- tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 64 + tools/testing/selftests/landlock/net_test.c | 1439 +++++++++++++++++++ 3 files changed, 1507 insertions(+) create mode 100644 tools/testing/selftests/landlock/net_test.c
tools/testing/selftests/landlock/config +4 -0
@@ -1,3 +1,7 @@ +CONFIG_INET=y +CONFIG_IPV6=y +CONFIG_NET=y +CONFIG_NET_NS=y CONFIG_OVERLAY_FS=y CONFIG_SECURITY_LANDLOCK=y CONFIG_SECURITY_PATH=y
tools/testing/selftests/landlock/fs_test.c +64 -0
82 lines not on this page · raw message
tools/testing/selftests/landlock/net_test.c new file +1439 -0
1,439 lines not on this page · raw message
Signature · 1 line
2.41.0
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 135 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.These test suites try to check edge cases for TCP sockets
bind()andconnect()actions.inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
*bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action withAF_UNSPECsocket family.
*connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action withAF_UNSPECsocket family.
*ruleset_overlap: Tests with overlapping rules for one port.
*ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
*inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more thanU16_MAX.port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
*bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.layout1:
*with_net: Tests with networkbind()socket action within filesystem directory access test.Test coverage for
security/landlockis 94.8% of 934 lines according to gcc/gcov-11.Signed-off-by Konstantin Meskhidze <[email protected]>Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v11 (from Mickaël Salaün):
- Add ipv4.from_unix_to_tcp test suite to check that socket family is the same between a socket and a sockaddr by trying to connect/bind on a unix socket (stream or dgram) using an inet family. Landlock should not change the error code. This found a bug (which needs to be fixed) with the TCP restriction.
- Revamp the inet.{bind,connect} tests into protocol.{bind,connect}:
- Mergebind_connect_unix_dgram_socket,bind_connect_unix_dgram_socketandbind_connect_inval_addrleninto it: add a full test matrix of IPv4/TCP, IPv6/TCP, IPv4/UDP, IPv6/UDP, unix/stream, unix/dgram, all of them with or without sandboxing. This improve coverage and it enables to check that a TCP restriction work as expected but doesn't restrict other stream or datagram protocols. This also enables to check consistency of the network stack with or without Landlock. We now have 76 test suites for the network.
- Add full send/recv checks.
- Make a generic framework that will be ready for future protocol supports.
- Replace most ASSERT with EXPECT according to the criticity of an
action: if we can get more meaningful information with following checks. For instance, failure to create a kernel object (e.g.socket(),accept()orfork()call) is critical if it is used by following checks. For Landlock ruleset building, the following checks don't make sense if the sandbox is not complete. However, it doesn't make sense to continue aFIXTURE_SETUP()if any check failed.
- Add a new unspec fixture to replace inet.bind_afunspec with unspec.bind and inet.connect_afunspec with unspec.connect, factoring and simplifying code.
- Replace inet.bind_afunspec with protocol.bind_unspec, and inet.connect_afunspec with protocol.connect_unspec. Extend these tests with the matrix of all "protocol" variants. Don't test connect with the same socket which is already binded/listening (I guess this was an copy-paste error). The protocol.bind_unspec tests found a bug (which needs to be fixed).
- Add and useset_service()andsetup_loopback()helpers to configure network services. Add and use andtest_bind_and_connect()to factor out a lot of checks.
- Add new types (protocol_variant,service_fixture) and update related helpers to get more generic test code.
- Replace static (port) arrays withservice_fixturevariables.
- Add new helpers: {bind,connect}_variant_addrlen() andget_addrlen()to cover all protocols with previousbind_connect_inval_addrlentests. Make them return -errno in case of error.
- Switch from a unix socket path address to an abstract one. This enables to avoid file cleanup in test teardowns.
- Close all rulesets after enforcement.
- Remove the duplicate "empty access" test.
- Replace inet.ruleset_overlay with tcp_layers.ruleset_overlap and simplify test:
- Always run sandbox tests because test were always run sandboxed and it doesn't give more guarantees to do it not sandboxed.
- Rewrite test withvariant->num_layersto make it simpler and configurable.
- Add another test layer totcp_layersused forruleset_overlapand test without sandbox.
- Leveragetest_bind_and_connect()and avoid usingSO_REUSEADDRbecause the socket was not listened to, and don't use the same socket/FD for server and client.
- Replace inet.ruleset_expanding with tcp_layers.ruleset_expand.
- Drop capabilities in allFIXTURE_SETUP().
- Change test ports to cover more ranges.
- Add "mini" tests:
- Replace the invalid ruleset attribute test from port.inval with mini.unknow_access_rights.
- Simplify port.inval and move some code to other mini.* tests.
- Add new mini.network_access_rights test.
- Rewrite inet.inval_port_format into mini.tcp_port_overflow:
- Remove uselessis_sandboxchecks.
- Extend tests with bind/connect checks.
- Interleave valid requests with invalid ones.
- Add two_srv.port_endianness test, extracted and extended from inet.inval_port_format .
- Add Microsoft copyright.
- Rename some variables to make them easier to read.
- Constify variables.
- Add minimal logs to help debug test failures.--- tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 64 + tools/testing/selftests/landlock/net_test.c | 1439 +++++++++++++++++++ 3 files changed, 1507 insertions(+)create mode 100644
tools/testing/selftests/landlock/net_test.c
[...]
tools/testing/selftests/landlock/net_test.c line 1332
Quoted 9 lines
tools/testing/selftests/landlock/net_test.c new file +9
-0+ +FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +}; + +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed;
Well, this "is_sandboxed" variable can now be removed, and the variants updated accordingly.
tools/testing/selftests/landlock/net_test.c line 270
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+const struct protocol_variant prot; +}; + +/* clang-format off */
Rename to ipv4 and same for the ipv6 variants.
Quoted 40 lines
tools/testing/selftests/landlock/net_test.c new file +39
-0+FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; +
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
7/6/2023 7:09 PM, Mickaël Salaün пишет:
Quoted 141 lines · Mickaël Salaün
Quoted 135 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.These test suites try to check edge cases for TCP sockets
bind()andconnect()actions.inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
*bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action withAF_UNSPECsocket family.
*connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action withAF_UNSPECsocket family.
*ruleset_overlap: Tests with overlapping rules for one port.
*ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
*inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more thanU16_MAX.port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
*bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.layout1:
*with_net: Tests with networkbind()socket action within filesystem directory access test.Test coverage for
security/landlockis 94.8% of 934 lines according to gcc/gcov-11.Signed-off-by Konstantin Meskhidze <[email protected]>Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v11 (from Mickaël Salaün):
- Add ipv4.from_unix_to_tcp test suite to check that socket family is the same between a socket and a sockaddr by trying to connect/bind on a unix socket (stream or dgram) using an inet family. Landlock should not change the error code. This found a bug (which needs to be fixed) with the TCP restriction.
- Revamp the inet.{bind,connect} tests into protocol.{bind,connect}:
- Mergebind_connect_unix_dgram_socket,bind_connect_unix_dgram_socketandbind_connect_inval_addrleninto it: add a full test matrix of IPv4/TCP, IPv6/TCP, IPv4/UDP, IPv6/UDP, unix/stream, unix/dgram, all of them with or without sandboxing. This improve coverage and it enables to check that a TCP restriction work as expected but doesn't restrict other stream or datagram protocols. This also enables to check consistency of the network stack with or without Landlock. We now have 76 test suites for the network.
- Add full send/recv checks.
- Make a generic framework that will be ready for future protocol supports.
- Replace most ASSERT with EXPECT according to the criticity of an
action: if we can get more meaningful information with following checks. For instance, failure to create a kernel object (e.g.socket(),accept()orfork()call) is critical if it is used by following checks. For Landlock ruleset building, the following checks don't make sense if the sandbox is not complete. However, it doesn't make sense to continue aFIXTURE_SETUP()if any check failed.
- Add a new unspec fixture to replace inet.bind_afunspec with unspec.bind and inet.connect_afunspec with unspec.connect, factoring and simplifying code.
- Replace inet.bind_afunspec with protocol.bind_unspec, and inet.connect_afunspec with protocol.connect_unspec. Extend these tests with the matrix of all "protocol" variants. Don't test connect with the same socket which is already binded/listening (I guess this was an copy-paste error). The protocol.bind_unspec tests found a bug (which needs to be fixed).
- Add and useset_service()andsetup_loopback()helpers to configure network services. Add and use andtest_bind_and_connect()to factor out a lot of checks.
- Add new types (protocol_variant,service_fixture) and update related helpers to get more generic test code.
- Replace static (port) arrays withservice_fixturevariables.
- Add new helpers: {bind,connect}_variant_addrlen() andget_addrlen()to cover all protocols with previousbind_connect_inval_addrlentests. Make them return -errno in case of error.
- Switch from a unix socket path address to an abstract one. This enables to avoid file cleanup in test teardowns.
- Close all rulesets after enforcement.
- Remove the duplicate "empty access" test.
- Replace inet.ruleset_overlay with tcp_layers.ruleset_overlap and simplify test:
- Always run sandbox tests because test were always run sandboxed and it doesn't give more guarantees to do it not sandboxed.
- Rewrite test withvariant->num_layersto make it simpler and configurable.
- Add another test layer totcp_layersused forruleset_overlapand test without sandbox.
- Leveragetest_bind_and_connect()and avoid usingSO_REUSEADDRbecause the socket was not listened to, and don't use the same socket/FD for server and client.
- Replace inet.ruleset_expanding with tcp_layers.ruleset_expand.
- Drop capabilities in allFIXTURE_SETUP().
- Change test ports to cover more ranges.
- Add "mini" tests:
- Replace the invalid ruleset attribute test from port.inval with mini.unknow_access_rights.
- Simplify port.inval and move some code to other mini.* tests.
- Add new mini.network_access_rights test.
- Rewrite inet.inval_port_format into mini.tcp_port_overflow:
- Remove uselessis_sandboxchecks.
- Extend tests with bind/connect checks.
- Interleave valid requests with invalid ones.
- Add two_srv.port_endianness test, extracted and extended from inet.inval_port_format .
- Add Microsoft copyright.
- Rename some variables to make them easier to read.
- Constify variables.
- Add minimal logs to help debug test failures.--- tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 64 + tools/testing/selftests/landlock/net_test.c | 1439 +++++++++++++++++++ 3 files changed, 1507 insertions(+)create mode 100644
tools/testing/selftests/landlock/net_test.c[...]
Quoted 12 lines
tools/testing/selftests/landlock/net_test.c line 1332
Quoted 9 lines
tools/testing/selftests/landlock/net_test.c new file +9
-0+ +FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +}; + +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed;Well, this "
is_sandboxed" variable can now be removed, and the variants updated accordingly.
Ok. Thanks. Im reviewing the patch now.
Quoted 7 lines
tools/testing/selftests/landlock/net_test.c line 270
Quoted 5 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+const struct protocol_variant prot; +}; + +/* clang-format off */ Rename to ipv4 and same for the ipv6 variants.
Got it.
Quoted 42 lines
Quoted 31 lines
tools/testing/selftests/landlock/net_test.c new file +29
-0+FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; +tools/testing/selftests/landlock/net_test.c line 1375
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; +.
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
In reply to Mickaël SalaünQuoted 14 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]
Quoted 12 lines
tools/testing/selftests/landlock/net_test.c new file +12
-0+static int bind_variant_addrlen (const int sock_fd, +const struct service_fixture *const srv, +const socklen_t addrlen) +{ +int ret; + +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + ret =bind (sock_fd, &srv->ipv4_addr, addrlen); +break ; +
tools/testing/selftests/landlock/net_test.c line 183
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+case AF_INET6 : + ret =bind (sock_fd, &srv->ipv6_addr, addrlen); +break ; + +case AF_UNIX : + ret =bind (sock_fd, &srv->unix_addr, addrlen); +break ; + +default : + errno = -EAFNOSUPPORT ;
This should be errno = EAFNOSUPPORT
Quoted 6 lines
tools/testing/selftests/landlock/net_test.c new file +6
-0+return -errno; + } + +if (ret <0 ) +return -errno; +return ret;
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
7/10/2023 7:06 PM, Mickaël Salaün пишет:
Quoted 36 lines · Mickaël Salaün
Quoted 14 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
Quoted 16 lines
tools/testing/selftests/landlock/net_test.c new file +16
-0+static int bind_variant_addrlen (const int sock_fd, +const struct service_fixture *const srv, +const socklen_t addrlen) +{ +int ret; + +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + ret =bind (sock_fd, &srv->ipv4_addr, addrlen); +break ; + +case AF_INET6 : + ret =bind (sock_fd, &srv->ipv6_addr, addrlen); +break ; +
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 183
Quoted 6 lines
tools/testing/selftests/landlock/net_test.c new file +6
-0+case AF_UNIX : + ret =bind (sock_fd, &srv->unix_addr, addrlen); +break ; + +default : + errno = -EAFNOSUPPORT ;This should be
errno = EAFNOSUPPORT
Ok. Got it.
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 189
Quoted 7 lines
tools/testing/selftests/landlock/net_test.c new file +6
-0+return -errno; + } + +if (ret <0 ) +return -errno; +return ret;.
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
In reply to Mickaël SalaünQuoted 13 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]
Quoted 65 lines
tools/testing/selftests/landlock/net_test.c new file +65
-0+FIXTURE_SETUP (ipv4) +{ +const struct protocol_variant prot = { + .domain =AF_INET , + .type = variant->type, + }; + +disable_caps (_metadata); + +set_service (&self->srv0, prot,0 ); +set_service (&self->srv1, prot,1 ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (ipv4) +{ +} + +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp +TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ + EXPECT_EQ(-EINVAL, bind_variant(unix_stream_fd, &self->srv1)) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + }
tools/testing/selftests/landlock/net_test.c line 883
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +}
We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add AF_INET6 variants, which will result in 8 "ip" variants:
TEST_F(ip, port_zero)
{
if (variant->sandbox == TCP_SANDBOX) {
/* Denies any connect and bind. */
}
/* Checks errno for port 0. */
}[...]
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};
The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "ipv4_tcp".
Quoted 111 lines
tools/testing/selftests/landlock/net_test.c new file +111
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 )); +enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 100 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
Quoted 65 lines
tools/testing/selftests/landlock/net_test.c new file +65
-0+FIXTURE_SETUP (ipv4) +{ +const struct protocol_variant prot = { + .domain =AF_INET , + .type = variant->type, + }; + +disable_caps (_metadata); + +set_service (&self->srv0, prot,0 ); +set_service (&self->srv1, prot,1 ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (ipv4) +{ +} + +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp +TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ + EXPECT_EQ(-EINVAL, bind_variant(unix_stream_fd, &self->srv1)) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + }tools/testing/selftests/landlock/net_test.c line 883
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +}We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:
Quoted 7 lines
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }
Ok. Got it.
Quoted 11 lines
[...]
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".
Right. Thanks.
Quoted 113 lines
Quoted 102 lines
tools/testing/selftests/landlock/net_test.c new file +101
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 ));tools/testing/selftests/landlock/net_test.c line 1439
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
In reply to Mickaël Salaün7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 100 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
Quoted 65 lines
tools/testing/selftests/landlock/net_test.c new file +65
-0+FIXTURE_SETUP (ipv4) +{ +const struct protocol_variant prot = { + .domain =AF_INET , + .type = variant->type, + }; + +disable_caps (_metadata); + +set_service (&self->srv0, prot,0 ); +set_service (&self->srv1, prot,1 ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (ipv4) +{ +} + +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp +TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ + EXPECT_EQ(-EINVAL, bind_variant(unix_stream_fd, &self->srv1)) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + }tools/testing/selftests/landlock/net_test.c line 883
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +}We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:
Quoted 7 lines
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }
As I understand the would be the next test cases:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be
bounded to random port).
3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be
bounded to random port).
5. ip4, sandboxed, bind some available port, connect port 0 -> should
return -EACCES (denied by landlock).
6. ip4, non-sandboxed, bind some available port, connect port 0 ->
should return ECONNREFUSED.
7. ip6, sandboxed, bind some available port, connect port 0 -> should
return -EACCES (denied by landlock)
8. ip6, non-sandboxed, some bind available port, connect port 0 ->
should return ECONNREFUSED.
Correct?
Quoted 11 lines
[...]
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".
So inet should be changed to ipv4_tcp and ipv6_tcp with next variants:
- ipv4_tcp.no_sandbox_with_ipv4.port_endianness
- ipv4_tcp.sandbox_with_ipv4.port_endianness
- ipv6_tcp.no_sandbox_with_ipv6.port_endianness
- ipv6_tcp.sandbox_with_ipv6.port_endianness
????
in this case we need double copy of TEST_F(inet, port_endianness) :
TEST_F(ipv4_tcp, port_endianness)
TEST_F(ipv6_tcp, port_endianness)
Quoted 113 lines
Quoted 102 lines
tools/testing/selftests/landlock/net_test.c new file +101
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 ));tools/testing/selftests/landlock/net_test.c line 1439
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 34 lines · Konstantin Meskhidze (A)
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 24 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 7 lines
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }
Quoted 4 lines
As I understand the would be the next test cases:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
Without any allowed port, yes. This test case is useful.
By tuning /proc/sys/net/ipv4/ip_local_port_range (see inet_csk_find_open_port call) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with -EACCES).
However, I think the current LSM API don't enable to infer this random port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Cc Paul
Quoted 2 lines
2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).
I think so but we need to make sure the random port cannot be < 1024, I guess with /proc/sys/net/ipv4/ip_local_port_range but I don't know for IPv6.
Quoted 6 lines
3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).
5. ip4, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock).
Yes, but don't need to bind to anything (same for the next ones).
Quoted 2 lines
6. ip4, non-sandboxed, bind some available port, connect port 0 -> should
return ECONNREFUSED.
Yes, but without any binding.
Quoted 6 lines
7. ip6, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock)
8. ip6, non-sandboxed, some bind available port, connect port 0 -> should
return ECONNREFUSED.Correct?
Thinking more about this case, being able to add a rule with port zero *for a connect action* looks legitimate. A rule with both connect and bind actions on port zero should then be denied. We should fix add_rule_net_service() and test that (with a first layer allowing port zero, and a second without rule, for connect).
Quoted 20 lines
Quoted 4 lines
[...]
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".So inet should be changed to
ipv4_tcpandipv6_tcpwith next variants:- ipv4_tcp.no_sandbox_with_ipv4.port_endianness
- ipv4_tcp.sandbox_with_ipv4.port_endianness
- ipv6_tcp.no_sandbox_with_ipv6.port_endianness
- ipv6_tcp.sandbox_with_ipv6.port_endianness
????
Quoted 3 lines
in this case we need double copy of
TEST_F(inet, port_endianness):
TEST_F(ipv4_tcp, port_endianness)
TEST_F(ipv6_tcp, port_endianness)
There is no need for any variant for the port_endianness test. You can rename "inet" to "ipv4_tcp" (and not "inet_tcp" like I said before).
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
8/17/2023 6:08 PM, Mickaël Salaün пишет:
Quoted 40 lines · Konstantin Meskhidze (A)
Quoted 34 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 24 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 7 lines
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }Quoted 4 lines
As I understand the would be the next test cases:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
Quoted 6 lines
Without any allowed port, yes. This test case is useful.
By tuning
/proc/sys/net/ipv4/ip_local_port_range(seeinet_csk_find_open_portcall) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with-EACCES).
Sorry, did not get this case. Could please explain it with more details?
Quoted 7 lines
However, I think the current LSM API don't enable to infer this random port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Yep, if some LSM rule (for bind) has been applied a with specific
port, other attemps to bind with zero/random ports would be refused by LSM security checks.
Quoted 9 lines
Cc Paul
Quoted 2 lines
2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).I think so but we need to make sure the random port cannot be < 1024, I guess with
/proc/sys/net/ipv4/ip_local_port_rangebut I don't know for IPv6.
For ipv4 when connecting to a server a client binds to a random port
within /proc/sys/net/ipv4/ip_local_port_range, by default one my machine this range is: cat /proc/sys/net/ipv4/ip_local_port_range
32768 60999.
But for ipv6 there is no such tuning range.
Quoted 20 lines
Quoted 7 lines
3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).
5. ip4, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock).Yes, but don't need to bind to anything (same for the next ones).
Quoted 2 lines
6. ip4, non-sandboxed, bind some available port, connect port 0 -> should
return ECONNREFUSED.Yes, but without any binding.
Quoted 5 lines
7. ip6, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock)
8. ip6, non-sandboxed, some bind available port, connect port 0 -> should
return ECONNREFUSED.
Quoted 7 lines
Quoted 1 line
Correct?
Thinking more about this case, being able to add a rule with port zero *for a connect action* looks legitimate. A rule with both connect and bind actions on port zero should then be denied. We should fix
add_rule_net_service()and test that (with a first layer allowing port zero, and a second without rule, for connect).
So with first rule allowing port 0 connect action, the second rule
with some another port and connect action, as a result test should allow that. Correct?
Quoted 29 lines
Quoted 22 lines
Quoted 6 lines
[...]
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".So inet should be changed to
ipv4_tcpandipv6_tcpwith next variants:- ipv4_tcp.no_sandbox_with_ipv4.port_endianness
- ipv4_tcp.sandbox_with_ipv4.port_endianness
- ipv6_tcp.no_sandbox_with_ipv6.port_endianness
- ipv6_tcp.sandbox_with_ipv6.port_endianness
????Quoted 3 lines
in this case we need double copy of
TEST_F(inet, port_endianness):
TEST_F(ipv4_tcp, port_endianness)
TEST_F(ipv6_tcp, port_endianness)There is no need for any variant for the
port_endiannesstest. You can rename "inet" to "ipv4_tcp" (and not "inet_tcp" like I said before). .
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 40 lines · Konstantin Meskhidze (A)
8/17/2023 6:08 PM, Mickaël Salaün пишет:
Quoted 37 lines · Konstantin Meskhidze (A)
Quoted 35 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 3 lines
Quoted 3 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 17 lines
Quoted 11 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 7 lines
Quoted 1 line
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }As I understand the would be the next test cases:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
Quoted 7 lines
Quoted 6 lines
Without any allowed port, yes. This test case is useful.
By tuning
/proc/sys/net/ipv4/ip_local_port_range(seeinet_csk_find_open_portcall) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with-EACCES).Sorry, did not get this case. Could please explain it with more details?
According to bind(2), if no port are available, the syscall should return EADDRINUSE. And this returned value should be the same whatever the process is sandbox or not (and never EACCES). But as I explained just below, we cannot know this random port from the LSM hook, so no need to tweak /proc/sys/net/ipv4/ip_local_port_range, and your this is correct:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by landlock).
Quoted 11 lines
Quoted 7 lines
However, I think the current LSM API don't enable to infer this random port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Yep, if some LSM rule (for bind) has been applied a with specific port,
other attemps to bind with zero/random ports would be refused by LSM security checks.
To say it another way, we should not allow to add a rule with port 0 for LANDLOCK_ACCESS_NET_BIND_TCP, but return -EINVAL in this case. This limitation should be explained, documented and tested.
With (only) LANDLOCK_ACCESS_NET_CONNECT_TCP it should be allowed though (except if there is also LANDLOCK_ACCESS_NET_BIND_TCP) of course. Another test should cover the case with a new rule with these two access rights and port 0.
Quoted 10 lines
Quoted 9 lines
Cc Paul
Quoted 2 lines
2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).I think so but we need to make sure the random port cannot be < 1024, I guess with
/proc/sys/net/ipv4/ip_local_port_rangebut I don't know for IPv6.
Quoted 5 lines
For ipv4 when connecting to a server a client binds to a random port
within/proc/sys/net/ipv4/ip_local_port_range, by default one my machine this range is: cat/proc/sys/net/ipv4/ip_local_port_range
32768 60999.
But for ipv6 there is no such tuning range.
Ok, let's just assume that the test system doesn't have ip_local_port_range < 1024, put this assumption in a comment, and don't touch ip_local_port_range at all.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).
5. ip4, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock).
If a rule allows connecting to port 0, then it should be ECONNREFUSED, otherwise EACCES indeed. Both cases should be tested.
Quoted 15 lines
Quoted 15 lines
Yes, but don't need to bind to anything (same for the next ones).
Quoted 2 lines
6. ip4, non-sandboxed, bind some available port, connect port 0 -> should
return ECONNREFUSED.Yes, but without any binding.
Quoted 6 lines
7. ip6, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock)
8. ip6, non-sandboxed, some bind available port, connect port 0 -> should
return ECONNREFUSED.Correct?
Quoted 8 lines
Quoted 5 lines
Thinking more about this case, being able to add a rule with port zero *for a connect action* looks legitimate. A rule with both connect and bind actions on port zero should then be denied. We should fix
add_rule_net_service()and test that (with a first layer allowing port zero, and a second without rule, for connect).So with first rule allowing port 0 connect action, the second rule with
some another port and connect action,
Yes, but the first rule being part of a first layer/restriction, and the second rule part of a second layer.
Quoted 2 lines
as a result test should allow that.
Correct?
The first layer should return ECONNREFUSED when connecting on port 0 (allowed but nothing listening), and once the second layer is enforced, EACCES should be returned on port 0.
Quoted 27 lines
Quoted 27 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
[...]
Quoted 1 line
+FIXTURE(inet)
tools/testing/selftests/landlock/net_test.c new file +3
-0+{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The
"inet"
Quoted 2 lines
fixture can then be renamed to "
ipv4_tcp".
So inet should be changed toipv4_tcpandipv6_tcpwith nextvariants:
- ipv4_tcp.no_sandbox_with_ipv4.port_endianness
- ipv4_tcp.sandbox_with_ipv4.port_endianness
- ipv6_tcp.no_sandbox_with_ipv6.port_endianness
- ipv6_tcp.sandbox_with_ipv6.port_endianness
????Quoted 3 lines
in this case we need double copy of
TEST_F(inet, port_endianness):
TEST_F(ipv4_tcp, port_endianness)
TEST_F(ipv6_tcp, port_endianness)There is no need for any variant for the
port_endiannesstest. You can rename "inet" to "ipv4_tcp" (and not "inet_tcp" like I said before). .
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
9/14/2023 11:08 AM, Mickaël Salaün пишет:
Quoted 56 lines · Konstantin Meskhidze (A)
Quoted 40 lines
8/17/2023 6:08 PM, Mickaël Salaün пишет:
Quoted 37 lines · Konstantin Meskhidze (A)
Quoted 35 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 3 lines
Quoted 3 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 17 lines
Quoted 11 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 7 lines
Quoted 1 line
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }As I understand the would be the next test cases:
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).Quoted 7 lines
Quoted 6 lines
Without any allowed port, yes. This test case is useful.
By tuning
/proc/sys/net/ipv4/ip_local_port_range(seeinet_csk_find_open_portcall) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with-EACCES).Sorry, did not get this case. Could please explain it with more details?
According to bind(2), if no port are available, the syscall should return EADDRINUSE. And this returned value should be the same whatever the process is sandbox or not (and never EACCES). But as I explained just below, we cannot know this random port from the LSM hook, so no need to tweak
/proc/sys/net/ipv4/ip_local_port_range, and your this is correct:
Quoted 2 lines
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by landlock).
yep, adding rule with port 0 (for bind) returns EINVAL then calling bind port 0 returns EACCES cause there is no rule with port 0.
Quoted 13 lines
Quoted 9 lines
Quoted 8 lines
However, I think the current LSM API don't enable to infer this random port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Quoted 3 lines
Yep, if some LSM rule (for bind) has been applied a with specific port,
other attemps to bind with zero/random ports would be refused by LSM security checks.
Quoted 8 lines
To say it another way, we should not allow to add a rule with port 0 for
LANDLOCK_ACCESS_NET_BIND_TCP, but return-EINVALin this case. This limitation should be explained, documented and tested.With (only)
LANDLOCK_ACCESS_NET_CONNECT_TCPit should be allowed though (except if there is alsoLANDLOCK_ACCESS_NET_BIND_TCP) of course. Another test should cover the case with a new rule with these two access rights and port 0.
I think it's possible to have LANDLOCK_ACCESS_NET_CONNECT_TCP with
port 0 with LANDLOCK_ACCESS_NET_BIND_TCP at the same time, cause LANDLOCK_ACCESS_NET_BIND_TCP rule is allowed (by Landlock) with any other port but 0.
Quoted 95 lines
Quoted 11 lines
Quoted 10 lines
Cc Paul
Quoted 2 lines
2. ip4, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).I think so but we need to make sure the random port cannot be < 1024, I guess with
/proc/sys/net/ipv4/ip_local_port_rangebut I don't know for IPv6.Quoted 5 lines
For ipv4 when connecting to a server a client binds to a random port
within/proc/sys/net/ipv4/ip_local_port_range, by default one my machine this range is: cat/proc/sys/net/ipv4/ip_local_port_range
32768 60999.
But for ipv6 there is no such tuning range.Ok, let's just assume that the test system doesn't have
ip_local_port_range< 1024, put this assumption in a comment, and don't touchip_local_port_rangeat all.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
3. ip6, sandboxed, bind port 0 -> should return EACCES (denied by
landlock).
4. ip6, non-sandboxed, bind port 0 -> should return 0 (should be bounded to
random port).
5. ip4, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock).If a rule allows connecting to port 0, then it should be ECONNREFUSED, otherwise EACCES indeed. Both cases should be tested.
Quoted 15 lines
Quoted 15 lines
Yes, but don't need to bind to anything (same for the next ones).
Quoted 2 lines
6. ip4, non-sandboxed, bind some available port, connect port 0 -> should
return ECONNREFUSED.Yes, but without any binding.
Quoted 6 lines
7. ip6, sandboxed, bind some available port, connect port 0 -> should
return-EACCES(denied by landlock)
8. ip6, non-sandboxed, some bind available port, connect port 0 -> should
return ECONNREFUSED.Correct?
Quoted 8 lines
Quoted 5 lines
Thinking more about this case, being able to add a rule with port zero *for a connect action* looks legitimate. A rule with both connect and bind actions on port zero should then be denied. We should fix
add_rule_net_service()and test that (with a first layer allowing port zero, and a second without rule, for connect).So with first rule allowing port 0 connect action, the second rule with
some another port and connect action,Yes, but the first rule being part of a first layer/restriction, and the second rule part of a second layer.
Quoted 2 lines
as a result test should allow that.
Correct?The first layer should return ECONNREFUSED when connecting on port 0 (allowed but nothing listening), and once the second layer is enforced, EACCES should be returned on port 0.
Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines
[...]
Quoted 1 line
+FIXTURE(inet)
tools/testing/selftests/landlock/net_test.c new file +3
-0+{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The
"inet"
Quoted 2 lines
fixture can then be renamed to "
ipv4_tcp".
So inet should be changed toipv4_tcpandipv6_tcpwith nextvariants:
- ipv4_tcp.no_sandbox_with_ipv4.port_endianness
- ipv4_tcp.sandbox_with_ipv4.port_endianness
- ipv6_tcp.no_sandbox_with_ipv6.port_endianness
- ipv6_tcp.sandbox_with_ipv6.port_endianness
????Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
in this case we need double copy of
TEST_F(inet, port_endianness):
TEST_F(ipv4_tcp, port_endianness)
TEST_F(ipv6_tcp, port_endianness)There is no need for any variant for the
port_endiannesstest. You can rename "inet" to "ipv4_tcp" (and not "inet_tcp" like I said before). ..
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 82 lines · Konstantin Meskhidze (A)
9/14/2023 11:08 AM, Mickaël Salaün пишет:
Quoted 52 lines · Konstantin Meskhidze (A)
Quoted 35 lines
8/17/2023 6:08 PM, Mickaël Salaün пишет:
Quoted 6 lines · Konstantin Meskhidze (A)
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
7/12/2023 10:02 AM, Mickaël Salaün пишет:
On 06/07/2023 16:55, Mickaël Salaün wrote:
From: Konstantin Meskhidze <[email protected]>Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message. I think this patch is good but it would appreciate reviews. You can find the diff of my changes here but it is not really readable:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]
We should also add a test to make sure errno is the samewith and
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 1 line
TEST_F(ip, port_zero)Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
{ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }As I understand the would be the next test cases:
Quoted 1 line
Quoted 1 line
1. ip4, sandboxed, bind port 0 -> should return EACCES
Quoted 8 lines
(denied by
Quoted 6 lines
Quoted 3 lines
landlock).
Without any allowed port, yes. This test case is useful. By tuning/proc/sys/net/ipv4/ip_local_port_range(see
inet_csk_find_open_portcall) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with-EACCES).Sorry, did not get this case. Could please explain it with more details?
According to bind(2), if no port are available, the syscall should return EADDRINUSE. And this returned value should be the same whatever the process is sandbox or not (and never EACCES). But as I explained just below, we cannot know this random port from the LSM hook, so no need to tweak
/proc/sys/net/ipv4/ip_local_port_range, and your this is correct:Quoted 2 lines
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by landlock).
yep, adding rule with port 0 (for bind) returns EINVAL then calling bind port 0 returns EACCES cause there is no rule with port 0.
Quoted 13 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
However, I think the current LSM API don't enable to infer this
random
Quoted 5 lines
port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Quoted 3 lines
Yep, if some LSM rule (for bind) has been applied a with specific port,
other attemps to bind with zero/random ports would be refused by LSM security checks.Quoted 8 lines
To say it another way, we should not allow to add a rule with port 0 for
LANDLOCK_ACCESS_NET_BIND_TCP, but return-EINVALin this case. This limitation should be explained, documented and tested.With (only)
LANDLOCK_ACCESS_NET_CONNECT_TCPit should be allowed though (except if there is alsoLANDLOCK_ACCESS_NET_BIND_TCP) of course. Another test should cover the case with a new rule with these two access rights and port 0.
Quoted 4 lines
I think it's possible to have
LANDLOCK_ACCESS_NET_CONNECT_TCPwith port 0
withLANDLOCK_ACCESS_NET_BIND_TCPat the same time, causeLANDLOCK_ACCESS_NET_BIND_TCPrule is allowed (by Landlock) with any other port but 0.
It would mask the fact that port zero cannot be allowed, which could be possible one day. So for now we need to return EINVAL in this case.
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
9/18/2023 9:56 AM, Mickaël Salaün пишет:
Quoted 83 lines · Konstantin Meskhidze (A)
Quoted 82 lines
9/14/2023 11:08 AM, Mickaël Salaün пишет:
Quoted 52 lines · Konstantin Meskhidze (A)
Quoted 35 lines
8/17/2023 6:08 PM, Mickaël Salaün пишет:
Quoted 6 lines · Konstantin Meskhidze (A)
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
7/12/2023 10:02 AM, Mickaël Salaün пишет:
On 06/07/2023 16:55, Mickaël Salaün wrote:
From: Konstantin Meskhidze <[email protected]>Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message. I think this patch is good but it would appreciate reviews. You can find the diff of my changes here but it is not really readable:
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]
We should also add a test to make sure errno is the samewith and
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:Quoted 1 line
TEST_F(ip, port_zero)Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
{ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }As I understand the would be the next test cases:
Quoted 1 line
Quoted 1 line
1. ip4, sandboxed, bind port 0 -> should return EACCES
Quoted 8 lines
(denied by
Quoted 6 lines
Quoted 3 lines
landlock).
Without any allowed port, yes. This test case is useful. By tuning/proc/sys/net/ipv4/ip_local_port_range(see
inet_csk_find_open_portcall) we should be able to pick a specific allowed port and test it. We can also test for the EADDRINUSE error to make sure error ordering is correct (compared with-EACCES).Sorry, did not get this case. Could please explain it with more details?
According to bind(2), if no port are available, the syscall should return EADDRINUSE. And this returned value should be the same whatever the process is sandbox or not (and never EACCES). But as I explained just below, we cannot know this random port from the LSM hook, so no need to tweak
/proc/sys/net/ipv4/ip_local_port_range, and your this is correct:Quoted 2 lines
1. ip4, sandboxed, bind port 0 -> should return EACCES (denied by landlock).
yep, adding rule with port 0 (for bind) returns EINVAL then calling bind port 0 returns EACCES cause there is no rule with port 0.
Quoted 21 lines
Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
However, I think the current LSM API don't enable to infer this
random
Quoted 5 lines
port because the LSM hook is called before a port is picked. If this is correct, the best way to control port binding would be to always deny binding on port zero/random (when restricting port binding, whatever exception rules are in place). This explanation should be part of a comment for this specific exception.
Quoted 3 lines
Yep, if some LSM rule (for bind) has been applied a with specific port,
other attemps to bind with zero/random ports would be refused by LSM security checks.To say it another way, we should not allow to add a rule with port 0 for
LANDLOCK_ACCESS_NET_BIND_TCP, but return-EINVALin this case. This limitation should be explained, documented and tested.With (only)
LANDLOCK_ACCESS_NET_CONNECT_TCPit should be allowed though (except if there is alsoLANDLOCK_ACCESS_NET_BIND_TCP) of course. Another test should cover the case with a new rule with these two access rights and port 0.
Quoted 7 lines
Quoted 4 lines
I think it's possible to have
LANDLOCK_ACCESS_NET_CONNECT_TCPwith port 0
withLANDLOCK_ACCESS_NET_BIND_TCPat the same time, causeLANDLOCK_ACCESS_NET_BIND_TCPrule is allowed (by Landlock) with any other port but 0.It would mask the fact that port zero cannot be allowed, which could be possible one day. So for now we need to return EINVAL in this case.
Got it. I added bind mask in add_rule_net_service() to check that
zero port is not allowed with bind action. I sent all changes in the latest V12 patch.
Quoted 1 line
.
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
In reply to Mickaël Salaün7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 110 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
Quoted 65 lines
tools/testing/selftests/landlock/net_test.c new file +65
-0+FIXTURE_SETUP (ipv4) +{ +const struct protocol_variant prot = { + .domain =AF_INET , + .type = variant->type, + }; + +disable_caps (_metadata); + +set_service (&self->srv0, prot,0 ); +set_service (&self->srv1, prot,1 ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (ipv4) +{ +} + +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp +TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ + EXPECT_EQ(-EINVAL, bind_variant(unix_stream_fd, &self->srv1)) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + }tools/testing/selftests/landlock/net_test.c line 883
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +}We should also add a test to make sure errno is the same with and without sandboxing when using port 0 for connect and consistent with bind (using an available port). The test fixture and variants should be quite similar to the "ipv4" ones, but we can also add
AF_INET6variants, which will result in 8 "ip" variants:
TEST_F(ip, port_zero){ if (variant->sandbox == TCP_SANDBOX) { /* Denies any connect and bind. */ } /* Checks errno for port 0. */ }[...]
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".
Maybe its better to name it "tcp". So we dont need to copyTEST_F(tcp, port_endianness) for ipv6 and ipv4. What do you think?
Quoted 113 lines
Quoted 102 lines
tools/testing/selftests/landlock/net_test.c new file +101
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 ));tools/testing/selftests/landlock/net_test.c line 1439
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 29 lines · Konstantin Meskhidze (A)
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 18 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".
Quoted 3 lines
Maybe its better to name it "tcp". So we dont need to copy TEST_F(tcp,
port_endianness) for ipv6 and ipv4.
What do you think?
I don't see any need to test with IPv4 and IPv6, hence the "inet" name (and without variants). You can rename it to "inet_tcp" to highlight the specificities of this fixture.
Quoted 114 lines
Quoted 114 lines
Quoted 103 lines
tools/testing/selftests/landlock/net_test.c new file +101
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 ));tools/testing/selftests/landlock/net_test.c line 1439
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
8/17/2023 4:19 PM, Mickaël Salaün пишет:
Quoted 30 lines · Konstantin Meskhidze (A)
Quoted 29 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 26 lines · Mickaël Salaün
Quoted 13 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.[...]
tools/testing/selftests/landlock/net_test.c line 1328
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The "inet" fixture can then be renamed to "
ipv4_tcp".
Quoted 8 lines
Quoted 3 lines
Maybe its better to name it "tcp". So we dont need to copy TEST_F(tcp,
port_endianness) for ipv6 and ipv4.
What do you think?I don't see any need to test with IPv4 and IPv6, hence the "inet" name (and without variants). You can rename it to "
inet_tcp" to highlight the specificities of this fixture.
I think there was some misunderstanding from my side. So I will rename
inet to inet_tcp and keep all fixture variants:
- no_sandbox_with_ipv4.
- sandbox_with_ipv4.
- no_sandbox_with_ipv6.
- sandbox_with_ipv6.
Correct?
Quoted 115 lines
Quoted 106 lines
Quoted 106 lines
Quoted 106 lines
tools/testing/selftests/landlock/net_test.c new file +104
-0+ +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 )); +enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1439
Quoted 7 lines
tools/testing/selftests/landlock/net_test.c new file +7
-0+test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
.
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 40 lines · Konstantin Meskhidze (A)
8/17/2023 4:19 PM, Mickaël Salaün пишет:
Quoted 28 lines · Konstantin Meskhidze (A)
Quoted 27 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 3 lines
Quoted 3 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 12 lines
Quoted 11 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The
"inet"
Quoted 2 lines
fixture can then be renamed to "
ipv4_tcp".
Maybe its better to name it "tcp". So we dont need to copyQuoted 8 lines
Quoted 3 lines
TEST_F(tcp,
port_endianness) for ipv6 and ipv4.
What do you think?I don't see any need to test with IPv4 and IPv6, hence the "inet" name (and without variants). You can rename it to "
inet_tcp" to highlight the specificities of this fixture.
Quoted 7 lines
I think there was some misunderstanding from my side. So I will rename
inet toinet_tcpand keep all fixture variants:
-no_sandbox_with_ipv4.
-sandbox_with_ipv4.
-no_sandbox_with_ipv6.
-sandbox_with_ipv6.
Correct?
No, you just need to remove the FIXTURE_VARIANT and the four FIXTURE_VARIANT_ADD blocks bellow. And according to another reply, "ipv4_tcp" seems more appropriate.
Quoted 113 lines
Quoted 113 lines
Quoted 104 lines
Quoted 104 lines
Quoted 104 lines
Quoted 1 line
+
tools/testing/selftests/landlock/net_test.c new file +103
-0+FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 )); +enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 1439
Quoted 7 lines
tools/testing/selftests/landlock/net_test.c new file +7
-0+test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
.
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
8/17/2023 6:34 PM, Mickaël Salaün пишет:
Quoted 49 lines · Konstantin Meskhidze (A)
Quoted 40 lines
8/17/2023 4:19 PM, Mickaël Salaün пишет:
Quoted 28 lines · Konstantin Meskhidze (A)
Quoted 27 lines
7/12/2023 10:02 AM, Mickaël Salaün пишет:
Quoted 3 lines
Quoted 3 lines · Mickaël Salaün
From: Konstantin Meskhidze <[email protected]>
Quoted 1 line
Quoted 1 line
This patch is a revamp of the v11 tests [1] with new tests
(see the
Quoted 12 lines
Quoted 11 lines
"Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.
[...]Quoted 5 lines
Quoted 5 lines
tools/testing/selftests/landlock/net_test.c new file +4
-0+FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +};The "inet" variants are useless and should be removed. The
"inet"
Quoted 2 lines
fixture can then be renamed to "
ipv4_tcp".
Maybe its better to name it "tcp". So we dont need to copyQuoted 8 lines
Quoted 3 lines
TEST_F(tcp,
port_endianness) for ipv6 and ipv4.
What do you think?I don't see any need to test with IPv4 and IPv6, hence the "inet" name (and without variants). You can rename it to "
inet_tcp" to highlight the specificities of this fixture.Quoted 7 lines
I think there was some misunderstanding from my side. So I will rename
inet toinet_tcpand keep all fixture variants:
-no_sandbox_with_ipv4.
-sandbox_with_ipv4.
-no_sandbox_with_ipv6.
-sandbox_with_ipv6.
Correct?
Quoted 4 lines
No, you just need to remove the
FIXTURE_VARIANTand the fourFIXTURE_VARIANT_ADDblocks bellow. And according to another reply, "ipv4_tcp" seems more appropriate.
Ok. Got it. Thank you.
Quoted 115 lines
Quoted 113 lines
Quoted 113 lines
Quoted 113 lines
Quoted 113 lines
Quoted 102 lines
Quoted 2 lines
+
tools/testing/selftests/landlock/net_test.c new file +100
-0+FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 ));tools/testing/selftests/landlock/net_test.c line 1439
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN .
Quoted 1 line
Quoted 1 line
.
.
Re: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
In reply to Mickaël Salaün7/6/2023 5:55 PM, Mickaël Salaün пишет:
Quoted 1,061 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.These test suites try to check edge cases for TCP sockets
bind()andconnect()actions.inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
*bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action withAF_UNSPECsocket family.
*connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action withAF_UNSPECsocket family.
*ruleset_overlap: Tests with overlapping rules for one port.
*ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
*inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more thanU16_MAX.port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
*bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.layout1:
*with_net: Tests with networkbind()socket action within filesystem directory access test.Test coverage for
security/landlockis 94.8% of 934 lines according to gcc/gcov-11.Signed-off-by Konstantin Meskhidze <[email protected]>Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>---
Changes since v11 (from Mickaël Salaün):
- Add ipv4.from_unix_to_tcp test suite to check that socket family is the same between a socket and a sockaddr by trying to connect/bind on a unix socket (stream or dgram) using an inet family. Landlock should not change the error code. This found a bug (which needs to be fixed) with the TCP restriction.
- Revamp the inet.{bind,connect} tests into protocol.{bind,connect}:
- Mergebind_connect_unix_dgram_socket,bind_connect_unix_dgram_socketandbind_connect_inval_addrleninto it: add a full test matrix of IPv4/TCP, IPv6/TCP, IPv4/UDP, IPv6/UDP, unix/stream, unix/dgram, all of them with or without sandboxing. This improve coverage and it enables to check that a TCP restriction work as expected but doesn't restrict other stream or datagram protocols. This also enables to check consistency of the network stack with or without Landlock. We now have 76 test suites for the network.
- Add full send/recv checks.
- Make a generic framework that will be ready for future protocol supports.
- Replace most ASSERT with EXPECT according to the criticity of an
action: if we can get more meaningful information with following checks. For instance, failure to create a kernel object (e.g.socket(),accept()orfork()call) is critical if it is used by following checks. For Landlock ruleset building, the following checks don't make sense if the sandbox is not complete. However, it doesn't make sense to continue aFIXTURE_SETUP()if any check failed.
- Add a new unspec fixture to replace inet.bind_afunspec with unspec.bind and inet.connect_afunspec with unspec.connect, factoring and simplifying code.
- Replace inet.bind_afunspec with protocol.bind_unspec, and inet.connect_afunspec with protocol.connect_unspec. Extend these tests with the matrix of all "protocol" variants. Don't test connect with the same socket which is already binded/listening (I guess this was an copy-paste error). The protocol.bind_unspec tests found a bug (which needs to be fixed).
- Add and useset_service()andsetup_loopback()helpers to configure network services. Add and use andtest_bind_and_connect()to factor out a lot of checks.
- Add new types (protocol_variant,service_fixture) and update related helpers to get more generic test code.
- Replace static (port) arrays withservice_fixturevariables.
- Add new helpers: {bind,connect}_variant_addrlen() andget_addrlen()to cover all protocols with previousbind_connect_inval_addrlentests. Make them return -errno in case of error.
- Switch from a unix socket path address to an abstract one. This enables to avoid file cleanup in test teardowns.
- Close all rulesets after enforcement.
- Remove the duplicate "empty access" test.
- Replace inet.ruleset_overlay with tcp_layers.ruleset_overlap and simplify test:
- Always run sandbox tests because test were always run sandboxed and it doesn't give more guarantees to do it not sandboxed.
- Rewrite test withvariant->num_layersto make it simpler and configurable.
- Add another test layer totcp_layersused forruleset_overlapand test without sandbox.
- Leveragetest_bind_and_connect()and avoid usingSO_REUSEADDRbecause the socket was not listened to, and don't use the same socket/FD for server and client.
- Replace inet.ruleset_expanding with tcp_layers.ruleset_expand.
- Drop capabilities in allFIXTURE_SETUP().
- Change test ports to cover more ranges.
- Add "mini" tests:
- Replace the invalid ruleset attribute test from port.inval with mini.unknow_access_rights.
- Simplify port.inval and move some code to other mini.* tests.
- Add new mini.network_access_rights test.
- Rewrite inet.inval_port_format into mini.tcp_port_overflow:
- Remove uselessis_sandboxchecks.
- Extend tests with bind/connect checks.
- Interleave valid requests with invalid ones.
- Add two_srv.port_endianness test, extracted and extended from inet.inval_port_format .
- Add Microsoft copyright.
- Rename some variables to make them easier to read.
- Constify variables.
- Add minimal logs to help debug test failures.--- tools/testing/selftests/landlock/config | 4 + tools/testing/selftests/landlock/fs_test.c | 64 + tools/testing/selftests/landlock/net_test.c | 1439 +++++++++++++++++++ 3 files changed, 1507 insertions(+)create mode 100644
tools/testing/selftests/landlock/net_test.ctools/testing/selftests/landlock/config +4
-0@@ -1,3 +1,7 @@ +CONFIG_INET=y +CONFIG_IPV6=y +CONFIG_NET=y +CONFIG_NET_NS=y CONFIG_OVERLAY_FS=y CONFIG_SECURITY_LANDLOCK=y CONFIG_SECURITY_PATH=ytools/testing/selftests/landlock/fs_test.c +64
-0@@ -8,8 +8,10 @@*/ #define _GNU_SOURCE +#include <arpa/inet.h> #include <fcntl.h> #include <linux/landlock.h> +#include <netinet/in.h> #include <sched.h> #include <stdio.h> #include <string.h> @@ -17,6 +19,7 @@#include <sys/mount.h> #include <sys/prctl.h> #include <sys/sendfile.h> +#include <sys/socket.h> #include <sys/stat.h> #include <sys/sysmacros.h> #include <unistd.h> @@ -4413,4 +4416,65 @@ TEST_F_FORK(layout2_overlay, same_content_different_file) } } +static const char loopback_ipv4[] ="127.0.0.1" ; +const unsigned short sock_port =15000 ; + +TEST_F_FORK(layout1, with_net) +{ +const struct rule rules[] = { + { + .path = dir_s1d2, + .access =ACCESS_RO , + }, + {}, + }; +struct landlock_ruleset_attr ruleset_attr_net = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + + .port = sock_port, + }; +int sockfd, ruleset_fd, ruleset_fd_net; +struct sockaddr_in addr4; + + addr4.sin_family =AF_INET ; + addr4.sin_port =htons (sock_port); + addr4.sin_addr.s_addr =inet_addr (loopback_ipv4); +memset (&addr4.sin_zero,'\0' ,8 ); + +/* Creates ruleset for network access. */ + ruleset_fd_net =landlock_create_ruleset (&ruleset_attr_net, +sizeof (ruleset_attr_net),0 ); +ASSERT_LE (0 , ruleset_fd_net); + +/* Adds a network rule. */ +ASSERT_EQ (0 , +landlock_add_rule (ruleset_fd_net,LANDLOCK_RULE_NET_SERVICE , + &tcp_bind,0 )); + +enforce_ruleset (_metadata, ruleset_fd_net); +ASSERT_EQ (0 ,close (ruleset_fd_net)); + + ruleset_fd =create_ruleset (_metadata,ACCESS_RW , rules); + +ASSERT_LE (0 , ruleset_fd); +enforce_ruleset (_metadata, ruleset_fd); +ASSERT_EQ (0 ,close (ruleset_fd)); + +/* Tests on a directory with the network rule loaded. */ +ASSERT_EQ (0 ,test_open (dir_s1d2,O_RDONLY )); +ASSERT_EQ (0 ,test_open (file1_s1d2,O_RDONLY )); + + sockfd =socket (AF_INET ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , sockfd); +/* Binds a socket to port 15000. */ +ASSERT_EQ (0 ,bind (sockfd, &addr4,sizeof (addr4))); + +/* Closes bounded socket. */ +ASSERT_EQ (0 ,close (sockfd)); +} +TEST_HARNESS_MAIN tools/testing/selftests/landlock/net_test.c new file +818
-0@@ -0,0 +1,1439 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Landlock tests - Network +* +* Copyright © 2022-2023 Huawei Tech. Co., Ltd. +* Copyright © 2023 Microsoft Corporation +*/ + +#define _GNU_SOURCE +#include <arpa/inet.h> +#include <errno.h> +#include <fcntl.h> +#include <linux/landlock.h> +#include <linux/in.h> +#include <sched.h> +#include <stdint.h> +#include <string.h> +#include <sys/prctl.h> +#include <sys/socket.h> +#include <sys/un.h> + +#include "common.h" + +const short sock_port_start = (1 <<10 ); + +static const char loopback_ipv4[] ="127.0.0.1" ; +static const char loopback_ipv6[] ="::1" ; + +/* Number pending connections queue to be hold. */ +const short backlog =10 ; + +enum sandbox_type { +NO_SANDBOX , +/* This may be used to test rules that allow *and* deny accesses. */ +TCP_SANDBOX , +}; + +struct protocol_variant { +int domain; +int type; +}; + +struct service_fixture { +struct protocol_variant protocol; +/* port is also stored in ipv4_addr.sin_port or ipv6_addr.sin6_port */ +unsigned short port; +union { +struct sockaddr_in ipv4_addr; +struct sockaddr_in6 ipv6_addr; +struct { +struct sockaddr_un unix_addr; +socklen_t unix_addr_len; + }; + }; +}; + +static int set_service (struct service_fixture *const srv, +const struct protocol_variant prot, +const unsigned short index) +{ +memset (srv,0 ,sizeof (*srv)); + +/* +* Copies all protocol properties in case of the variant only contains +* a subset of them. +*/ + srv->protocol = prot; + +/* Checks for port overflow. */ +if (index >2 ) +return 1 ; + srv->port = sock_port_start << (2 * index); + +switch (prot.domain) { +case AF_UNSPEC : +case AF_INET : + srv->ipv4_addr.sin_family = prot.domain; + srv->ipv4_addr.sin_port =htons (srv->port); + srv->ipv4_addr.sin_addr.s_addr =inet_addr (loopback_ipv4); +return 0 ; + +case AF_INET6 : + srv->ipv6_addr.sin6_family = prot.domain; + srv->ipv6_addr.sin6_port =htons (srv->port); +inet_pton (AF_INET6 , loopback_ipv6, &srv->ipv6_addr.sin6_addr); +return 0 ; + +case AF_UNIX : + srv->unix_addr.sun_family = prot.domain; +sprintf (srv->unix_addr.sun_path, +"_selftests-landlock-net-tid%d-index%d" ,gettid (), + index); + srv->unix_addr_len =SUN_LEN (&srv->unix_addr); + srv->unix_addr.sun_path[0 ] ='\0' ; +return 0 ; + } +return 1 ; +} + +static void setup_loopback (struct __test_metadata *const _metadata) +{ +set_cap (_metadata,CAP_SYS_ADMIN ); +ASSERT_EQ (0 ,unshare (CLONE_NEWNET )); +ASSERT_EQ (0 ,system ("ip link set dev lo up" )); +clear_cap (_metadata,CAP_SYS_ADMIN ); +} + +static bool is_restricted (const struct protocol_variant *const prot, +const enum sandbox_type sandbox) +{ +switch (prot->domain) { +case AF_INET : +case AF_INET6 : +switch (prot->type) { +case SOCK_STREAM : +return sandbox ==TCP_SANDBOX ; + } +break ; + } +return false; +} + +static int socket_variant (const struct service_fixture *const srv) +{ +int ret; + + ret =socket (srv->protocol.domain, srv->protocol.type |SOCK_CLOEXEC , +0 ); +if (ret <0 ) +return -errno; +return ret; +} + +#ifndef SIN6_LEN_RFC2133 +#define SIN6_LEN_RFC2133 24 +#endif + +static socklen_t get_addrlen (const struct service_fixture *const srv, +const bool minimal) +{ +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : +return sizeof (srv->ipv4_addr); + +case AF_INET6 : +if (minimal) +return SIN6_LEN_RFC2133 ; +return sizeof (srv->ipv6_addr); + +case AF_UNIX : +if (minimal) +return sizeof (srv->unix_addr) - +sizeof (srv->unix_addr.sun_path); +return srv->unix_addr_len; + +default : +return 0 ; + } +} + +static int bind_variant_addrlen (const int sock_fd, +const struct service_fixture *const srv, +const socklen_t addrlen) +{ +int ret; + +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + ret =bind (sock_fd, &srv->ipv4_addr, addrlen); +break ; + +case AF_INET6 : + ret =bind (sock_fd, &srv->ipv6_addr, addrlen); +break ; + +case AF_UNIX : + ret =bind (sock_fd, &srv->unix_addr, addrlen); +break ; + +default : + errno = -EAFNOSUPPORT ; +return -errno; + } + +if (ret <0 ) +return -errno; +return ret; +} + +static int bind_variant (const int sock_fd, +const struct service_fixture *const srv) +{ +return bind_variant_addrlen (sock_fd, srv,get_addrlen (srv, false)); +} + +static int connect_variant_addrlen (const int sock_fd, +const struct service_fixture *const srv, +const socklen_t addrlen) +{ +int ret; + +switch (srv->protocol.domain) { +case AF_UNSPEC : +case AF_INET : + ret =connect (sock_fd, &srv->ipv4_addr, addrlen); +break ; + +case AF_INET6 : + ret =connect (sock_fd, &srv->ipv6_addr, addrlen); +break ; + +case AF_UNIX : + ret =connect (sock_fd, &srv->unix_addr, addrlen); +break ; + +default : + errno = -EAFNOSUPPORT ; +return -errno; + } + +if (ret <0 ) +return -errno; +return ret; +} + +static int connect_variant (const int sock_fd, +const struct service_fixture *const srv) +{ +return connect_variant_addrlen (sock_fd, srv,get_addrlen (srv, false)); +} + +FIXTURE (protocol) +{ +struct service_fixture srv0, srv1, srv2, unspec_any, unspec_srv0; +}; + +FIXTURE_VARIANT (protocol) +{ +const enum sandbox_type sandbox; +const struct protocol_variant prot; +}; + +FIXTURE_SETUP (protocol) +{ +const struct protocol_variant prot_unspec = { + .domain =AF_UNSPEC , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, variant->prot,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, variant->prot,1 )); +ASSERT_EQ (0 ,set_service (&self->srv2, variant->prot,2 )); + +ASSERT_EQ (0 ,set_service (&self->unspec_srv0, prot_unspec,0 )); + +ASSERT_EQ (0 ,set_service (&self->unspec_any, prot_unspec,0 )); + self->unspec_any.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (protocol) +{ +} + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_ipv4_tcp) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_ipv6_tcp) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_ipv4_udp) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_INET, + .type = SOCK_DGRAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_ipv6_udp) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_INET6, + .type = SOCK_DGRAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_unix_stream) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_UNIX, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, no_sandbox_with_unix_datagram) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .prot = { + .domain = AF_UNIX, + .type = SOCK_DGRAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_ipv4_tcp) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_ipv6_tcp) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_ipv4_udp) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_INET, + .type = SOCK_DGRAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_ipv6_udp) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_INET6, + .type = SOCK_DGRAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_unix_stream) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_UNIX, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(protocol, tcp_sandbox_with_unix_datagram) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .prot = { + .domain = AF_UNIX, + .type = SOCK_DGRAM, + }, +}; + +static void test_bind_and_connect (struct __test_metadata *const _metadata, +const struct service_fixture *const srv, +const bool deny_bind,const bool deny_connect) +{ +char buf ='\0' ; +int inval_fd, bind_fd, client_fd, status, ret; +pid_t child; + +/* Starts invalid addrlen tests with bind. */ + inval_fd =socket_variant (srv); + ASSERT_LE(0, inval_fd) + { +TH_LOG ("Failed to create socket: %s" ,strerror (errno)); + } + +/* Tries to bind with zero as addrlen. */ +EXPECT_EQ (-EINVAL ,bind_variant_addrlen (inval_fd, srv,0 )); + +/* Tries to bind with too small addrlen. */ +EXPECT_EQ (-EINVAL ,bind_variant_addrlen (inval_fd, srv, +get_addrlen (srv, true) -1 )); + +/* Tries to bind with minimal addrlen. */ + ret =bind_variant_addrlen (inval_fd, srv,get_addrlen (srv, true)); +if (deny_bind) { +EXPECT_EQ (-EACCES , ret); + }else { + EXPECT_EQ(0, ret) + { +TH_LOG ("Failed to bind to socket: %s" ,strerror (errno)); + } + } +EXPECT_EQ (0 ,close (inval_fd)); + +/* Starts invalid addrlen tests with connect. */ + inval_fd =socket_variant (srv); +ASSERT_LE (0 , inval_fd); + +/* Tries to connect with zero as addrlen. */ +EXPECT_EQ (-EINVAL ,connect_variant_addrlen (inval_fd, srv,0 )); + +/* Tries to connect with too small addrlen. */ +EXPECT_EQ (-EINVAL ,connect_variant_addrlen (inval_fd, srv, +get_addrlen (srv, true) -1 )); + +/* Tries to connect with minimal addrlen. */ + ret =connect_variant_addrlen (inval_fd, srv,get_addrlen (srv, true)); +if (srv->protocol.domain ==AF_UNIX ) { +EXPECT_EQ (-EINVAL , ret); + }else if (deny_connect) { +EXPECT_EQ (-EACCES , ret); + }else if (srv->protocol.type ==SOCK_STREAM ) { +/* No listening server, whatever the value of deny_bind. */ +EXPECT_EQ (-ECONNREFUSED , ret); + }else { + EXPECT_EQ(0, ret) + { +TH_LOG ("Failed to connect to socket: %s" , +strerror (errno)); + } + } +EXPECT_EQ (0 ,close (inval_fd)); + +/* Starts connection tests. */ + bind_fd =socket_variant (srv); +ASSERT_LE (0 , bind_fd); + + ret =bind_variant (bind_fd, srv); +if (deny_bind) { +EXPECT_EQ (-EACCES , ret); + }else { +EXPECT_EQ (0 , ret); + +/* Creates a listening socket. */ +if (srv->protocol.type ==SOCK_STREAM ) +EXPECT_EQ (0 ,listen (bind_fd, backlog)); + } + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +int connect_fd, ret; + +/* Closes listening socket for the child. */ +EXPECT_EQ (0 ,close (bind_fd)); + +/* Starts connection tests. */ + connect_fd =socket_variant (srv); +ASSERT_LE (0 , connect_fd); + ret =connect_variant (connect_fd, srv); +if (deny_connect) { +EXPECT_EQ (-EACCES , ret); + }else if (deny_bind) { +/* No listening server. */ +EXPECT_EQ (-ECONNREFUSED , ret); + }else { +EXPECT_EQ (0 , ret); +EXPECT_EQ (1 ,write (connect_fd,"." ,1 )); + } + +EXPECT_EQ (0 ,close (connect_fd)); +_exit (_metadata->passed ?EXIT_SUCCESS :EXIT_FAILURE ); +return ; + } + +/* Accepts connection from the child. */ + client_fd = bind_fd; +if (!deny_bind && !deny_connect) { +if (srv->protocol.type ==SOCK_STREAM ) { + client_fd =accept (bind_fd,NULL ,0 ); +ASSERT_LE (0 , client_fd); + } + +EXPECT_EQ (1 ,read (client_fd, &buf,1 )); +EXPECT_EQ ('.' , buf); + } + +EXPECT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (1 ,WIFEXITED (status)); +EXPECT_EQ (EXIT_SUCCESS ,WEXITSTATUS (status)); + +/* Closes connection, if any. */ +if (client_fd != bind_fd) +EXPECT_LE (0 ,close (client_fd)); + +/* Closes listening socket. */ +EXPECT_EQ (0 ,close (bind_fd)); +} + +TEST_F(protocol, bind) +{ +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +const struct landlock_net_service_attr tcp_connect_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv1.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for the first port. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +/* Allows connect and denies bind for the second port. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_connect_p1,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* Binds a socket to the first port. */ +test_bind_and_connect (_metadata, &self->srv0, false, false); + +/* Binds a socket to the second port. */ +test_bind_and_connect (_metadata, &self->srv1, +is_restricted (&variant->prot, variant->sandbox), + false); + +/* Binds a socket to the third port. */ +test_bind_and_connect (_metadata, &self->srv2, +is_restricted (&variant->prot, variant->sandbox), +is_restricted (&variant->prot, variant->sandbox)); +} + +TEST_F(protocol, connect) +{ +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +const struct landlock_net_service_attr tcp_bind_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv1.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for the first port. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +/* Allows bind and denies connect for the second port. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_p1,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +test_bind_and_connect (_metadata, &self->srv0, false, false); + +test_bind_and_connect (_metadata, &self->srv1, false, +is_restricted (&variant->prot, variant->sandbox)); + +test_bind_and_connect (_metadata, &self->srv2, +is_restricted (&variant->prot, variant->sandbox), +is_restricted (&variant->prot, variant->sandbox)); +} + +// Kernel FIXME: tcp_sandbox_with_ipv6_tcp and tcp_sandbox_with_unix_stream +TEST_F(protocol, bind_unspec) +{ +int bind_fd, ret; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows bind. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); + +/* Binds on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any); +if (variant->prot.domain ==AF_INET ) { + EXPECT_EQ(0, ret) + { +TH_LOG ("Failed to bind to unspec/any socket: %s" , +strerror (errno)); + } + }else { +EXPECT_EQ (-EINVAL , ret); + } +EXPECT_EQ (0 ,close (bind_fd)); + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); + ret =bind_variant (bind_fd, &self->unspec_srv0); +if (variant->prot.domain ==AF_INET ) { +EXPECT_EQ (-EAFNOSUPPORT , ret); + }else { + EXPECT_EQ(-EINVAL, ret) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + } + } +EXPECT_EQ (0 ,close (bind_fd)); +} + +TEST_F(protocol, connect_unspec) +{ +int bind_fd, client_fd, status; +pid_t child; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* Generic connection tests. */ +test_bind_and_connect (_metadata, &self->srv0, false, false); + +/* Specific connection tests. */ + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); +EXPECT_EQ (0 ,bind_variant (bind_fd, &self->srv0)); +if (self->srv0.protocol.type ==SOCK_STREAM ) +EXPECT_EQ (0 ,listen (bind_fd, backlog)); + + child =fork (); +ASSERT_LE (0 , child); +if (child ==0 ) { +int connect_fd, ret; + +/* Closes listening socket for the child. */ +EXPECT_EQ (0 ,close (bind_fd)); + + connect_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , connect_fd); +EXPECT_EQ (0 ,connect_variant (connect_fd, &self->srv0)); + +/* Tries to connect again, or set peer. */ + ret =connect_variant (connect_fd, &self->srv0); +if (self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EISCONN , ret); + }else { +EXPECT_EQ (0 , ret); + } + +/* Disconnects already connected socket, or set peer. */ + ret =connect_variant (connect_fd, &self->unspec_any); +if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EINVAL , ret); + }else { +EXPECT_EQ (0 , ret); + } + +/* Tries to reconnect, or set peer. */ + ret =connect_variant (connect_fd, &self->srv0); +if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EISCONN , ret); + }else { +EXPECT_EQ (0 , ret); + } + +EXPECT_EQ (0 ,close (connect_fd)); +_exit (_metadata->passed ?EXIT_SUCCESS :EXIT_FAILURE ); +return ; + } + + client_fd = bind_fd; +if (self->srv0.protocol.type ==SOCK_STREAM ) { + client_fd =accept (bind_fd,NULL ,0 ); +ASSERT_LE (0 , client_fd); + } + +EXPECT_EQ (child,waitpid (child, &status,0 )); +EXPECT_EQ (1 ,WIFEXITED (status)); +EXPECT_EQ (EXIT_SUCCESS ,WEXITSTATUS (status)); + +/* Closes connection, if any. */ +if (client_fd != bind_fd) +EXPECT_LE (0 ,close (client_fd)); + +/* Closes listening socket. */ +EXPECT_EQ (0 ,close (bind_fd)); +} + +FIXTURE (ipv4) +{ +struct service_fixture srv0, srv1; +}; + +FIXTURE_VARIANT (ipv4) +{ +const enum sandbox_type sandbox; +const int type; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ipv4, no_sandbox_with_tcp) { +/* clang-format on */ + .sandbox = NO_SANDBOX, + .type =SOCK_STREAM , +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ipv4, tcp_sandbox_with_tcp) { +/* clang-format on */ + .sandbox =TCP_SANDBOX , + .type = SOCK_STREAM, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ipv4, no_sandbox_with_udp) { +/* clang-format on */ + .sandbox =NO_SANDBOX , + .type = SOCK_DGRAM, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(ipv4, tcp_sandbox_with_udp) { +/* clang-format on */ + .sandbox = TCP_SANDBOX, + .type = SOCK_DGRAM, +}; + +FIXTURE_SETUP (ipv4) +{ +const struct protocol_variant prot = { + .domain =AF_INET , + .type = variant->type, + }; + +disable_caps (_metadata); + +set_service (&self->srv0, prot,0 );
tools/testing/selftests/landlock/net_test.c line 828
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+set_service (&self->srv1, prot,1 ); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (ipv4) +{ +} + +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
I debugged the code in qemu and came to a conclusion that we don't
check if socket's family equals to address's one in check_socket_access(...) function in net.c So I added the next lines (marked with !!!):
static int check_socket_access(struct socket *const sock,
struct sockaddr *const address, const int addrlen,
const access_mask_t access_request)
{
__be16 port;
layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET] = {};
const struct landlock_rule *rule;
access_mask_t handled_access;
struct landlock_id id = {
.type = LANDLOCK_KEY_NET_PORT,
};
const struct landlock_ruleset *const domain =
get_current_net_domain();
if (!domain)
return 0;
if (WARN_ON_ONCE(domain->num_layers < 1))
return -EACCES;
/* FIXES network tests */ !!!
if (sock->sk->__sk_common.skc_family != address->sa_family) !!!
return 0; !!!
/* Checks if it's a TCP socket. */
if (sock->type != SOCK_STREAM)
return 0;
......So now all network tests pass.
What do you think?
Quoted 24 lines
tools/testing/selftests/landlock/net_test.c new file +24
-0+TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ + ASSERT_EQ(0, landlock_add_rule(ruleset_fd, + LANDLOCK_RULE_NET_SERVICE,
tools/testing/selftests/landlock/net_test.c line 862
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+ &tcp_bind_connect_p0, 0)); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 );
Minor mistyping SOCK_STREAM -> SOCK_DGRAM.
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c new file +8
-0+ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv1))
tools/testing/selftests/landlock/net_test.c line 880
Quoted 10 lines
tools/testing/selftests/landlock/net_test.c new file +10
-0+ { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + } +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1)); + +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */
Should be "Checks... for srv1."
Quoted 559 lines
tools/testing/selftests/landlock/net_test.c new file +559
-0+EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +} + +FIXTURE (tcp_layers) +{ +struct service_fixture srv0, srv1; +}; + +FIXTURE_VARIANT (tcp_layers) +{ +const size_t num_layers; +const int domain; +}; + +FIXTURE_SETUP (tcp_layers) +{ +const struct protocol_variant prot = { + .domain = variant->domain, + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, prot,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, prot,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (tcp_layers) +{ +} + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, no_sandbox_with_ipv4) { +/* clang-format on */ + .domain = AF_INET, + .num_layers =0 , +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, one_sandbox_with_ipv4) { +/* clang-format on */ + .domain =AF_INET , + .num_layers = 1, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, two_sandboxes_with_ipv4) { +/* clang-format on */ + .domain =AF_INET , + .num_layers = 2, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, three_sandboxes_with_ipv4) { +/* clang-format on */ + .domain = AF_INET, + .num_layers =3 , +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, no_sandbox_with_ipv6) { +/* clang-format on */ + .domain =AF_INET6 , + .num_layers = 0, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, one_sandbox_with_ipv6) { +/* clang-format on */ + .domain =AF_INET6 , + .num_layers = 1, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, two_sandboxes_with_ipv6) { +/* clang-format on */ + .domain = AF_INET6, + .num_layers =2 , +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(tcp_layers, three_sandboxes_with_ipv6) { +/* clang-format on */ + .domain =AF_INET6 , + .num_layers = 3, +}; + +TEST_F(tcp_layers, ruleset_overlap) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + }; +const struct landlock_net_service_attr tcp_bind_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; + +if (variant->num_layers >=1 ) { +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows bind. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind,0 )); +/* Also allows bind, but allows connect too. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +if (variant->num_layers >=2 ) { +int ruleset_fd; + +/* Creates another ruleset layer. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Only allows bind. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +if (variant->num_layers >=3 ) { +int ruleset_fd; + +/* Creates another ruleset layer. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Try to allow bind and connect. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +/* +* Forbids to connect to the socket because only one ruleset layer +* allows connect. +*/ +test_bind_and_connect (_metadata, &self->srv0, false, + variant->num_layers >=2 ); +} + +TEST_F(tcp_layers, ruleset_expand) +{ +if (variant->num_layers >=1 ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , + }; +/* Allows bind for srv0. */ +const struct landlock_net_service_attr bind_srv0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &bind_srv0,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +if (variant->num_layers >=2 ) { +/* Expands network mask with connect action. */ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +/* Allows bind for srv0 and connect to srv0. */ +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +/* Try to allow bind for srv1. */ +const struct landlock_net_service_attr tcp_bind_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv1.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_p1,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +if (variant->num_layers >=3 ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +/* Allows connect to srv0, without bind rule. */ +const struct landlock_net_service_attr tcp_bind_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_p0,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + +test_bind_and_connect (_metadata, &self->srv0, false, + variant->num_layers >=3 ); + +test_bind_and_connect (_metadata, &self->srv1, variant->num_layers >=1 , + variant->num_layers >=2 ); +} + +/* clang-format off */ +FIXTURE (mini) {}; +/* clang-format on */ + +FIXTURE_SETUP (mini) +{ +disable_caps (_metadata); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (mini) +{ +} + +/* clang-format off */ + +#define ACCESS_LAST LANDLOCK_ACCESS_NET_CONNECT_TCP + +#define ACCESS_ALL ( \ + LANDLOCK_ACCESS_NET_BIND_TCP | \ + LANDLOCK_ACCESS_NET_CONNECT_TCP) + +/* clang-format on */ + +TEST_F(mini, network_access_rights) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =ACCESS_ALL , + }; +struct landlock_net_service_attr net_service = { + .port = sock_port_start, + }; +int ruleset_fd; +__u64 access; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +for (access =1 ; access <=ACCESS_LAST ; access <<=1 ) { + net_service.allowed_access = access; +EXPECT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &net_service, 0)) + { +TH_LOG ("Failed to add rule with access 0x%llx: %s" , + access,strerror (errno)); + } + } +EXPECT_EQ (0 ,close (ruleset_fd)); +} + +/* Checks invalid attribute, out of landlock network access range. */ +TEST_F(mini, unknown_access_rights) +{ +__u64 access_mask; + +for (access_mask =1ULL <<63 ; access_mask !=ACCESS_LAST ; + access_mask >>=1 ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net = access_mask, + }; + +EXPECT_EQ (-1 ,landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 )); +EXPECT_EQ (EINVAL , errno); + } +} + +TEST_F(mini, inval) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP + }; +const struct landlock_net_service_attr tcp_bind_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = sock_port_start, + }; +const struct landlock_net_service_attr tcp_bind_port_zero = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =0 , + }; +const struct landlock_net_service_attr tcp_denied = { + .allowed_access =0 , + .port = sock_port_start, + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = sock_port_start, + }; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Checks unhandled allowed_access. */ +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect,0 )); +EXPECT_EQ (EINVAL , errno); + +/* Checks zero port value. */ +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_port_zero,0 )); +EXPECT_EQ (EINVAL , errno); + +/* Checks zero access value. */ +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &tcp_denied,0 )); +EXPECT_EQ (ENOMSG , errno); + +/* Adds with legitimate values. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &tcp_bind,0 )); +} + +TEST_F(mini, tcp_port_overflow) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr port_max_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =UINT16_MAX , + }; +const struct landlock_net_service_attr port_max_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port =UINT16_MAX , + }; +const struct landlock_net_service_attr port_overflow1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =UINT16_MAX +1 , + }; +const struct landlock_net_service_attr port_overflow2 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =UINT16_MAX +2 , + }; +const struct landlock_net_service_attr port_overflow3 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =UINT32_MAX +1UL , + }; +const struct landlock_net_service_attr port_overflow4 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port =UINT32_MAX +2UL , + }; +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; +struct service_fixture srv_denied, srv_max_allowed; +int ruleset_fd; + +ASSERT_EQ (0 ,set_service (&srv_denied, ipv4_tcp,0 )); + +/* Be careful to avoid port inconsistencies. */ + srv_max_allowed = srv_denied; + srv_max_allowed.port = port_max_bind.port; + srv_max_allowed.ipv4_addr.sin_port =htons (port_max_bind.port); + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_max_bind,0 )); + +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_overflow1,0 )); +EXPECT_EQ (EINVAL , errno); + +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_overflow2,0 )); +EXPECT_EQ (EINVAL , errno); + +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_overflow3,0 )); +EXPECT_EQ (EINVAL , errno); + +/* Interleaves with invalid rule additions. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_max_connect,0 )); + +EXPECT_EQ (-1 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &port_overflow4,0 )); +EXPECT_EQ (EINVAL , errno); + +enforce_ruleset (_metadata, ruleset_fd); + +test_bind_and_connect (_metadata, &srv_denied, true, true); +test_bind_and_connect (_metadata, &srv_max_allowed, false, false); +} + +FIXTURE (inet) +{ +struct service_fixture srv0, srv1; +}; + +FIXTURE_VARIANT (inet) +{ +const bool is_sandboxed; +const struct protocol_variant prot; +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv4) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, no_sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = false, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +/* clang-format off */ +FIXTURE_VARIANT_ADD(inet, sandbox_with_ipv6) { +/* clang-format on */ + .is_sandboxed = true, + .prot = { + .domain = AF_INET6, + .type = SOCK_STREAM, + }, +}; + +FIXTURE_SETUP (inet) +{ +const struct protocol_variant ipv4_tcp = { + .domain =AF_INET , + .type =SOCK_STREAM , + }; + +disable_caps (_metadata); + +ASSERT_EQ (0 ,set_service (&self->srv0, ipv4_tcp,0 )); +ASSERT_EQ (0 ,set_service (&self->srv1, ipv4_tcp,1 )); + +setup_loopback (_metadata); +}; + +FIXTURE_TEARDOWN (inet) +{ +} + +TEST_F(inet, port_endianness) +{ +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr bind_host_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , +/* Host port format. */ + .port = self->srv0.port, + }; +const struct landlock_net_service_attr connect_big_endian_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Big endian port format. */ + .port =htons (self->srv0.port), + }; +const struct landlock_net_service_attr bind_connect_host_endian_p1 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , +/* Host port format. */ + .port = self->srv1.port, + }; +const unsigned int one =1 ; +const char little_endian = *(const char *)&one; +int ruleset_fd; + + ruleset_fd = +landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_host_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &connect_big_endian_p0,0 )); +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &bind_connect_host_endian_p1,0 )); +enforce_ruleset (_metadata, ruleset_fd); + +/* No restriction for big endinan CPU. */ +test_bind_and_connect (_metadata, &self->srv0, false, little_endian); + +/* No restriction for any CPU. */ +test_bind_and_connect (_metadata, &self->srv1, false, false); +} + +TEST_HARNESS_MAIN
Mickaël Salaün
maintainerRe: [PATCH v11.1] selftests/landlock: Add 11 new test suites dedicated to network
Quoted 54 lines · Konstantin Meskhidze (A)
7/6/2023 5:55 PM, Mickaël Salaün пишет:
Quoted 51 lines
From: Konstantin Meskhidze <[email protected]>
This patch is a revamp of the v11 tests [1] with new tests (see the "Changes since v11" description). I (Mickaël) only added the following todo list and the "Changes since v11" sections in this commit message.
I think this patch is good but it would appreciate reviews.
You can find the diff of my changes here but it is not really readable:
https://git.kernel.org/mic/c/78edf722fba5 (landlock-net-v11 branch)
[1] https://lore.kernel.org/all/[email protected]/
TODO:
- Rename all "net_service" to "net_port".
- Fix the two kernel bugs found with the new tests.
- Update this commit message with a small description of all tests.These test suites try to check edge cases for TCP sockets
bind()andconnect()actions.inet:
* bind: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
* connect: Tests with non-landlocked/landlocked ipv4 and ipv6 sockets.
*bind_afunspec: Tests with non-landlocked/landlocked restrictions for bind action withAF_UNSPECsocket family.
*connect_afunspec: Tests with non-landlocked/landlocked restrictions for connect action withAF_UNSPECsocket family.
*ruleset_overlap: Tests with overlapping rules for one port.
*ruleset_expanding: Tests with expanding rulesets in which rules are gradually added one by one, restricting sockets' connections.
*inval_port_format: Tests with wrong port format for ipv4/ipv6 sockets and with port values more thanU16_MAX.port:
* inval: Tests with invalid user space supplied data:
- out of range ruleset attribute;
- unhandled allowed access;
- zero port value;
- zero access value;
- legitimate access values;
*bind_connect_inval_addrlen: Tests with invalid address length.
* bind_connect_unix_*_socket: Tests to make sure unix sockets' actions are not restricted by Landlock rules applied to TCP ones.layout1:
*with_net: Tests with networkbind()socket action within filesystem directory access test.Test coverage for
security/landlockis 94.8% of 934 lines according to gcc/gcov-11.Signed-off-by Konstantin Meskhidze <[email protected]>Co-developed-by Mickaël Salaün <[email protected]>Signed-off-by Mickaël Salaün <[email protected]>
[...]
Quoted 35 lines
Quoted 1 line
+// Kernel FIXME:
tcp_sandbox_with_tcpandtcp_sandbox_with_udpI debugged the code in qemu and came to a conclusion that we don't
check if socket's family equals to address's one incheck_socket_access(...)function innet.c
So I added the next lines (marked with !!!):static int check_socket_access(struct socket *const sock,
struct sockaddr *const address, const int addrlen,
constaccess_mask_taccess_request){ __be16 port; layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_NET] = {}; const struct landlock_rule *rule; access_mask_t handled_access; struct landlock_id id = { .type = LANDLOCK_KEY_NET_PORT, }; const struct landlock_ruleset *const domain = get_current_net_domain(); if (!domain) return 0; if (WARN_ON_ONCE(domain->num_layers < 1)) return -EACCES; /* FIXES network tests */ !!! if (sock->sk->__sk_common.skc_family != address->sa_family) !!! return 0; !!! /* Checks if it's a TCP socket. */ if (sock->type != SOCK_STREAM) return 0; ......
Quoted 2 lines
So now all network tests pass.
What do you think?
Good catch, we should indeed check this inconsistency, but this fix also adds two issues:
- sa_family is read before checking if it is out of bound (see offsetofend() check bellow). A simple fix is to move down the new check.
- access request with AF_UNSPEC on a bind operation doesn't go through the specific AF_UNSPEC handling branch. There are two things to fix
here: handle AF_UNSPEC specifically in this new af_family check, and add a new test to make sure bind with AF_UNSPEC and INADDR_ANY is properly restricted.
I'll reply to this message with a patch extending your fix.
Quoted 29 lines
Quoted 29 lines
tools/testing/selftests/landlock/net_test.c new file +28
-0+TEST_F(ipv4, from_unix_to_inet) +{ +int unix_stream_fd, unix_dgram_fd; + +if (variant->sandbox ==TCP_SANDBOX ) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_bind_connect_p0 = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + }; +int ruleset_fd; + +/* Denies connect and bind to check errno value. */ + ruleset_fd =landlock_create_ruleset (&ruleset_attr, +sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect and bind for srv0. */ +ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_bind_connect_p0,0 )); + +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd));
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 862
Quoted 6 lines
tools/testing/selftests/landlock/net_test.c new file +6
-0+ } + + unix_stream_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 ); +ASSERT_LE (0 , unix_stream_fd); + + unix_dgram_fd =socket (AF_UNIX ,SOCK_STREAM |SOCK_CLOEXEC ,0 );Minor mistyping SOCK_STREAM -> SOCK_DGRAM.
Good catch!
Quoted 13 lines
Quoted 13 lines
tools/testing/selftests/landlock/net_test.c new file +12
-0+ASSERT_LE (0 , unix_dgram_fd); + +/* Checks unix stream bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_stream_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv0)); + +/* Checks unix stream bind and connect for srv1. */ + EXPECT_EQ(-EINVAL, bind_variant(unix_stream_fd, &self->srv1)) + { +TH_LOG ("Wrong bind error: %s" ,strerror (errno)); + } +EXPECT_EQ (-EINVAL ,connect_variant (unix_stream_fd, &self->srv1));
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c line 880
Quoted 6 lines
tools/testing/selftests/landlock/net_test.c new file +6
-0+ +/* Checks unix datagram bind and connect for srv0. */ +EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv0)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv0)); + +/* Checks unix datagram bind and connect for srv0. */ Should be "Checks... for srv1."
indeed
Quoted 4 lines
Quoted 4 lines
tools/testing/selftests/landlock/net_test.c new file +3
-0+EXPECT_EQ (-EINVAL ,bind_variant (unix_dgram_fd, &self->srv1)); +EXPECT_EQ (-EINVAL ,connect_variant (unix_dgram_fd, &self->srv1)); +}
Mickaël Salaün
maintainerPATCH 1/1 landlock: Fix and test network AF inconsistencies
Check af_family consistency while handling AF_UNSPEC specifically.
This patch should be squashed into the "Network support for Landlock" v11 patch series.
--- security/landlock/net.c | 29 ++++- tools/testing/selftests/landlock/net_test.c | 124 +++++++++++++------- 2 files changed, 108 insertions(+), 45 deletions(-)
security/landlock/net.c +26 -3
@@ -80,11 +80,11 @@ static int check_socket_access(struct socket *const sock,if (WARN_ON_ONCE (domain->num_layers <1 ))return -EACCES ; -/* Checks if it's a TCP socket. */ +/* Checks if it's a (potential) TCP socket. */ if (sock->type !=SOCK_STREAM )return 0 ; -/* Checks for minimal header length. */ +/* Checks for minimal header length to safely read sa_family. */ if (addrlen < offsetofend(struct sockaddr, sa_family))return -EINVAL ;
@@ -106,7 +106,6 @@ static int check_socket_access(struct socket *const sock,return 0 ; } -/* Specific AF_UNSPEC handling. */ if (address->sa_family ==AF_UNSPEC ) {/* * Connecting to an address with AF_UNSPEC dissolves the TCP
@@ -114,6 +113,10 @@ static int check_socket_access(struct socket *const sock,* connection while retaining the socket object (i.e., the file * descriptor). As for dropping privileges, closing * connections is always allowed. +* +* For a TCP access control system, this request is legitimate. +* Let the network stack handle potential inconsistencies and +* return -EINVAL if needed. */ if (access_request ==LANDLOCK_ACCESS_NET_CONNECT_TCP )return 0 ;
@@ -124,14 +127,34 @@ static int check_socket_access(struct socket *const sock,* INADDR_ANY (cf. __inet_bind). Checking the address is * required to not wrongfully return -EACCES instead of * -EAFNOSUPPORT. +* +* We could return 0 and let the network stack handle these +* checks, but it is safer to return a proper error and test +* consistency thanks to kselftest. */ if (access_request ==LANDLOCK_ACCESS_NET_BIND_TCP ) { +/* addrlen has already been checked for AF_UNSPEC. */ const struct sockaddr_in *const sockaddr = (struct sockaddr_in *)address; +if (sock->sk->__sk_common.skc_family !=AF_INET ) +return -EINVAL ; +if (sockaddr->sin_addr.s_addr !=htonl (INADDR_ANY ))return -EAFNOSUPPORT ; } + }else { +/* +* Checks sa_family consistency to not wrongfully return +* -EACCES instead of -EINVAL. Valid sa_family changes are +* only (from AF_INET or AF_INET6) to AF_UNSPEC. +* +* We could return 0 and let the network stack handle this +* check, but it is safer to return a proper error and test +* consistency thanks to kselftest. +*/ +if (address->sa_family != sock->sk->__sk_common.skc_family) +return -EINVAL ; } id.key.data = (__force uintptr_t )port;
tools/testing/selftests/landlock/net_test.c +82 -42
179 lines not on this page · raw message
Signature · 1 line
2.41.0
Re: [PATCH] landlock: Fix and test network AF inconsistencies
8/17/2023 4:00 PM, Mickaël Salaün пишет:
Quoted 4 lines
Check
af_familyconsistency while handlingAF_UNSPECspecifically.This patch should be squashed into the "Network support for Landlock" v11 patch series.
Thank you so much.
Can I find this patch in https://git.kernel.org/pub/scm/linux/kernel/git/mic/linux ???
Quoted 270 lines
Signed-off-by Mickaël Salaün <[email protected]>--- security/landlock/net.c | 29 ++++- tools/testing/selftests/landlock/net_test.c | 124 +++++++++++++------- 2 files changed, 108 insertions(+), 45 deletions(-)security/landlock/net.c +26
-3@@ -80,11 +80,11 @@ static int check_socket_access(struct socket *const sock,if (WARN_ON_ONCE (domain->num_layers <1 ))return -EACCES ; -/* Checks if it's a TCP socket. */ +/* Checks if it's a (potential) TCP socket. */ if (sock->type !=SOCK_STREAM )return 0 ; -/* Checks for minimal header length. */ +/* Checks for minimal header length to safely read sa_family. */ if (addrlen < offsetofend(struct sockaddr, sa_family))return -EINVAL ;@@ -106,7 +106,6 @@ static int check_socket_access(struct socket *const sock,return 0 ; } -/* Specific AF_UNSPEC handling. */ if (address->sa_family ==AF_UNSPEC ) {/* * Connecting to an address with AF_UNSPEC dissolves the TCP @@ -114,6 +113,10 @@ static int check_socket_access(struct socket *const sock,* connection while retaining the socket object (i.e., the file * descriptor). As for dropping privileges, closing * connections is always allowed. +* +* For a TCP access control system, this request is legitimate. +* Let the network stack handle potential inconsistencies and +* return -EINVAL if needed. */ if (access_request ==LANDLOCK_ACCESS_NET_CONNECT_TCP )return 0 ;@@ -124,14 +127,34 @@ static int check_socket_access(struct socket *const sock,* INADDR_ANY (cf. __inet_bind). Checking the address is * required to not wrongfully return -EACCES instead of * -EAFNOSUPPORT. +* +* We could return 0 and let the network stack handle these +* checks, but it is safer to return a proper error and test +* consistency thanks to kselftest. */ if (access_request ==LANDLOCK_ACCESS_NET_BIND_TCP ) { +/* addrlen has already been checked for AF_UNSPEC. */ const struct sockaddr_in *const sockaddr = (struct sockaddr_in *)address; +if (sock->sk->__sk_common.skc_family !=AF_INET ) +return -EINVAL ; +if (sockaddr->sin_addr.s_addr !=htonl (INADDR_ANY ))return -EAFNOSUPPORT ; } + }else { +/* +* Checks sa_family consistency to not wrongfully return +* -EACCES instead of -EINVAL. Valid sa_family changes are +* only (from AF_INET or AF_INET6) to AF_UNSPEC. +* +* We could return 0 and let the network stack handle this +* check, but it is safer to return a proper error and test +* consistency thanks to kselftest. +*/ +if (address->sa_family != sock->sk->__sk_common.skc_family) +return -EINVAL ; } id.key.data = (__force uintptr_t )port;tools/testing/selftests/landlock/net_test.c +82
-42@@ -233,7 +233,7 @@ static int connect_variant(const int sock_fd,FIXTURE (protocol) { -struct service_fixture srv0, srv1, srv2, unspec_any, unspec_srv0; +struct service_fixture srv0, srv1, srv2, unspec_any0, unspec_srv0; };FIXTURE_VARIANT (protocol)@@ -257,8 +257,8 @@ FIXTURE_SETUP(protocol)ASSERT_EQ (0 ,set_service (&self->unspec_srv0, prot_unspec,0 )); -ASSERT_EQ (0 ,set_service (&self->unspec_any, prot_unspec,0 )); - self->unspec_any.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY ); +ASSERT_EQ (0 ,set_service (&self->unspec_any0, prot_unspec,0 )); + self->unspec_any0.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY );setup_loopback (_metadata); };@@ -615,20 +615,18 @@ TEST_F(protocol, connect)// Kernel FIXME: tcp_sandbox_with_ipv6_tcp and tcp_sandbox_with_unix_stream TEST_F(protocol, bind_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + };int bind_fd, ret;if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , - }; -const struct landlock_net_service_attr tcp_bind = { - .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 );ASSERT_LE (0 , ruleset_fd);/* Allows bind. */ @@ -642,8 +640,8 @@ TEST_F(protocol, bind_unspec) bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); -/* Binds on AF_UNSPEC/INADDR_ANY. */ - ret =bind_variant (bind_fd, &self->unspec_any); +/* Allowed bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0);if (variant->prot.domain ==AF_INET ) { EXPECT_EQ(0, ret) {@@ -655,6 +653,33 @@ TEST_F(protocol, bind_unspec) }EXPECT_EQ (0 ,close (bind_fd)); +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies bind. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); + +/* Denied bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0); +if (variant->prot.domain ==AF_INET ) { +if (is_restricted (&variant->prot, variant->sandbox)) { +EXPECT_EQ (-EACCES , ret); + }else { +EXPECT_EQ (0 , ret); + } + }else { +EXPECT_EQ (-EINVAL , ret); + } +EXPECT_EQ (0 ,close (bind_fd)); + +/* Checks bind with AF_UNSPEC and the loopback address. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); ret =bind_variant (bind_fd, &self->unspec_srv0);@@ -671,34 +696,16 @@ TEST_F(protocol, bind_unspec) TEST_F(protocol, connect_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + };int bind_fd, client_fd, status;pid_t child; -if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , - }; -const struct landlock_net_service_attr tcp_connect = { - .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); -ASSERT_LE (0 , ruleset_fd); - -/* Allows connect. */ -ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, -LANDLOCK_RULE_NET_SERVICE , - &tcp_connect,0 )); -enforce_ruleset (_metadata, ruleset_fd); -EXPECT_EQ (0 ,close (ruleset_fd)); - } - -/* Generic connection tests. */ -test_bind_and_connect (_metadata, &self->srv0, false, false); -/* Specific connection tests. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd);@@ -726,8 +733,22 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect. */ +ASSERT_EQ (0 , +landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } +/* Disconnects already connected socket, or set peer. */ - ret =connect_variant (connect_fd, &self->unspec_any); + ret =connect_variant (connect_fd, &self->unspec_any0);if (self->srv0.protocol.domain ==AF_UNIX && self->srv0.protocol.type ==SOCK_STREAM ) {EXPECT_EQ (-EINVAL , ret);@@ -744,6 +765,25 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies connect. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + ret =connect_variant (connect_fd, &self->unspec_any0); +if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EINVAL , ret); + }else { +/* Always allowed to disconnect. */ +EXPECT_EQ (0 , ret); + } +EXPECT_EQ (0 ,close (connect_fd));_exit (_metadata->passed ?EXIT_SUCCESS :EXIT_FAILURE );return ;
Mickaël Salaün
maintainerRe: [PATCH] landlock: Fix and test network AF inconsistencies
Quoted 11 lines · Konstantin Meskhidze (A)
8/17/2023 4:00 PM, Mickaël Salaün пишет:
Quoted 4 lines
Check
af_familyconsistency while handlingAF_UNSPECspecifically.This patch should be squashed into the "Network support for Landlock" v11 patch series.
Thank you so much.
Can I find this patch in
https://git.kernel.org/pub/scm/linux/kernel/git/mic/linux ???
It is now in the landlock-net-v11 branch.
Quoted 251 lines
Quoted 251 lines
Signed-off-by Mickaël Salaün <[email protected]>--- security/landlock/net.c | 29 ++++- tools/testing/selftests/landlock/net_test.c | 124 +++++++++++++------- 2 files changed, 108 insertions(+), 45 deletions(-)security/landlock/net.c +26
-3@@ -80,11 +80,11 @@ static int check_socket_access(struct socket *const sock,if (WARN_ON_ONCE (domain->num_layers <1 ))return -EACCES ; -/* Checks if it's a TCP socket. */ +/* Checks if it's a (potential) TCP socket. */ if (sock->type !=SOCK_STREAM )return 0 ; -/* Checks for minimal header length. */ +/* Checks for minimal header length to safely read sa_family. */ if (addrlen < offsetofend(struct sockaddr, sa_family))return -EINVAL ;@@ -106,7 +106,6 @@ static int check_socket_access(struct socket *const sock,return 0 ; } -/* Specific AF_UNSPEC handling. */ if (address->sa_family ==AF_UNSPEC ) {/* * Connecting to an address with AF_UNSPEC dissolves the TCP @@ -114,6 +113,10 @@ static int check_socket_access(struct socket *const sock,* connection while retaining the socket object (i.e., the file * descriptor). As for dropping privileges, closing * connections is always allowed. +* +* For a TCP access control system, this request is legitimate. +* Let the network stack handle potential inconsistencies and +* return -EINVAL if needed. */ if (access_request ==LANDLOCK_ACCESS_NET_CONNECT_TCP )return 0 ;@@ -124,14 +127,34 @@ static int check_socket_access(struct socket *const sock,* INADDR_ANY (cf. __inet_bind). Checking the address is * required to not wrongfully return -EACCES instead of * -EAFNOSUPPORT. +* +* We could return 0 and let the network stack handle these +* checks, but it is safer to return a proper error and test +* consistency thanks to kselftest. */ if (access_request ==LANDLOCK_ACCESS_NET_BIND_TCP ) { +/* addrlen has already been checked for AF_UNSPEC. */ const struct sockaddr_in *const sockaddr = (struct sockaddr_in *)address; +if (sock->sk->__sk_common.skc_family !=AF_INET ) +return -EINVAL ; +if (sockaddr->sin_addr.s_addr !=htonl (INADDR_ANY ))return -EAFNOSUPPORT ; } + }else { +/* +* Checks sa_family consistency to not wrongfully return +* -EACCES instead of -EINVAL. Valid sa_family changes are +* only (from AF_INET or AF_INET6) to AF_UNSPEC. +* +* We could return 0 and let the network stack handle this +* check, but it is safer to return a proper error and test +* consistency thanks to kselftest. +*/ +if (address->sa_family != sock->sk->__sk_common.skc_family) +return -EINVAL ; } id.key.data = (__force uintptr_t )port;tools/testing/selftests/landlock/net_test.c +82
-42@@ -233,7 +233,7 @@ static int connect_variant(const int sock_fd,FIXTURE (protocol) { -struct service_fixture srv0, srv1, srv2, unspec_any, unspec_srv0; +struct service_fixture srv0, srv1, srv2, unspec_any0, unspec_srv0; };FIXTURE_VARIANT (protocol)@@ -257,8 +257,8 @@ FIXTURE_SETUP(protocol)ASSERT_EQ (0 ,set_service (&self->unspec_srv0, prot_unspec,0 )); -ASSERT_EQ (0 ,set_service (&self->unspec_any, prot_unspec,0 )); - self->unspec_any.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY ); +ASSERT_EQ (0 ,set_service (&self->unspec_any0, prot_unspec,0 )); + self->unspec_any0.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY );setup_loopback (_metadata); };@@ -615,20 +615,18 @@ TEST_F(protocol, connect)// Kernel FIXME: tcp_sandbox_with_ipv6_tcp and tcp_sandbox_with_unix_stream TEST_F(protocol, bind_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + };int bind_fd, ret;if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , - }; -const struct landlock_net_service_attr tcp_bind = { - .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 );ASSERT_LE (0 , ruleset_fd);/* Allows bind. */ @@ -642,8 +640,8 @@ TEST_F(protocol, bind_unspec) bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); -/* Binds on AF_UNSPEC/INADDR_ANY. */ - ret =bind_variant (bind_fd, &self->unspec_any); +/* Allowed bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0);if (variant->prot.domain ==AF_INET ) { EXPECT_EQ(0, ret) {@@ -655,6 +653,33 @@ TEST_F(protocol, bind_unspec) }EXPECT_EQ (0 ,close (bind_fd)); +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies bind. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); + +/* Denied bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0); +if (variant->prot.domain ==AF_INET ) { +if (is_restricted (&variant->prot, variant->sandbox)) { +EXPECT_EQ (-EACCES , ret); + }else { +EXPECT_EQ (0 , ret); + } + }else { +EXPECT_EQ (-EINVAL , ret); + } +EXPECT_EQ (0 ,close (bind_fd)); + +/* Checks bind with AF_UNSPEC and the loopback address. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); ret =bind_variant (bind_fd, &self->unspec_srv0);@@ -671,34 +696,16 @@ TEST_F(protocol, bind_unspec) TEST_F(protocol, connect_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + };int bind_fd, client_fd, status;pid_t child; -if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , - }; -const struct landlock_net_service_attr tcp_connect = { - .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); -ASSERT_LE (0 , ruleset_fd); - -/* Allows connect. */ -ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, -LANDLOCK_RULE_NET_SERVICE , - &tcp_connect,0 )); -enforce_ruleset (_metadata, ruleset_fd); -EXPECT_EQ (0 ,close (ruleset_fd)); - } - -/* Generic connection tests. */ -test_bind_and_connect (_metadata, &self->srv0, false, false); -/* Specific connection tests. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd);@@ -726,8 +733,22 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect. */ +ASSERT_EQ (0 , +landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } +/* Disconnects already connected socket, or set peer. */ - ret =connect_variant (connect_fd, &self->unspec_any); + ret =connect_variant (connect_fd, &self->unspec_any0);if (self->srv0.protocol.domain ==AF_UNIX && self->srv0.protocol.type ==SOCK_STREAM ) {EXPECT_EQ (-EINVAL , ret);@@ -744,6 +765,25 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies connect. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + ret =connect_variant (connect_fd, &self->unspec_any0); +if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EINVAL , ret); + }else { +/* Always allowed to disconnect. */ +EXPECT_EQ (0 , ret); + } +EXPECT_EQ (0 ,close (connect_fd));_exit (_metadata->passed ?EXIT_SUCCESS :EXIT_FAILURE );return ;
Re: [PATCH] landlock: Fix and test network AF inconsistencies
8/17/2023 6:36 PM, Mickaël Salaün пишет:
Quoted 6 lines · Konstantin Meskhidze (A)
Quoted 5 lines
8/17/2023 4:00 PM, Mickaël Salaün пишет:
Quoted 2 lines
Check
af_familyconsistency while handlingAF_UNSPECspecifically.
Quoted 9 lines
Quoted 6 lines
Quoted 2 lines
This patch should be squashed into the "Network support for Landlock" v11 patch series.
Thank you so much.
Can I find this patch in
https://git.kernel.org/pub/scm/linux/kernel/git/mic/linux ???It is now in the landlock-net-v11 branch.
Thanks.
Quoted 252 lines
Quoted 243 lines
Quoted 243 lines
Signed-off-by Mickaël Salaün <[email protected]>--- security/landlock/net.c | 29 ++++- tools/testing/selftests/landlock/net_test.c | 124 +++++++++++++------- 2 files changed, 108 insertions(+), 45 deletions(-)security/landlock/net.c +26
-3@@ -80,11 +80,11 @@ static int check_socket_access(struct socket *const sock,if (WARN_ON_ONCE (domain->num_layers <1 ))return -EACCES ; -/* Checks if it's a TCP socket. */ +/* Checks if it's a (potential) TCP socket. */ if (sock->type !=SOCK_STREAM )return 0 ; -/* Checks for minimal header length. */ +/* Checks for minimal header length to safely read sa_family. */ if (addrlen < offsetofend(struct sockaddr, sa_family))return -EINVAL ;@@ -106,7 +106,6 @@ static int check_socket_access(struct socket *const sock,return 0 ; } -/* Specific AF_UNSPEC handling. */ if (address->sa_family ==AF_UNSPEC ) {/* * Connecting to an address with AF_UNSPEC dissolves the TCP @@ -114,6 +113,10 @@ static int check_socket_access(struct socket *const sock,* connection while retaining the socket object (i.e., the file * descriptor). As for dropping privileges, closing * connections is always allowed. +* +* For a TCP access control system, this request is legitimate. +* Let the network stack handle potential inconsistencies and +* return -EINVAL if needed. */ if (access_request ==LANDLOCK_ACCESS_NET_CONNECT_TCP )return 0 ;@@ -124,14 +127,34 @@ static int check_socket_access(struct socket *const sock,* INADDR_ANY (cf. __inet_bind). Checking the address is * required to not wrongfully return -EACCES instead of * -EAFNOSUPPORT. +* +* We could return 0 and let the network stack handle these +* checks, but it is safer to return a proper error and test +* consistency thanks to kselftest. */ if (access_request ==LANDLOCK_ACCESS_NET_BIND_TCP ) { +/* addrlen has already been checked for AF_UNSPEC. */ const struct sockaddr_in *const sockaddr = (struct sockaddr_in *)address; +if (sock->sk->__sk_common.skc_family !=AF_INET ) +return -EINVAL ; +if (sockaddr->sin_addr.s_addr !=htonl (INADDR_ANY ))return -EAFNOSUPPORT ; } + }else { +/* +* Checks sa_family consistency to not wrongfully return +* -EACCES instead of -EINVAL. Valid sa_family changes are +* only (from AF_INET or AF_INET6) to AF_UNSPEC. +* +* We could return 0 and let the network stack handle this +* check, but it is safer to return a proper error and test +* consistency thanks to kselftest. +*/ +if (address->sa_family != sock->sk->__sk_common.skc_family) +return -EINVAL ; } id.key.data = (__force uintptr_t )port;tools/testing/selftests/landlock/net_test.c +77
-42@@ -233,7 +233,7 @@ static int connect_variant(const int sock_fd,FIXTURE (protocol) { -struct service_fixture srv0, srv1, srv2, unspec_any, unspec_srv0; +struct service_fixture srv0, srv1, srv2, unspec_any0, unspec_srv0; };FIXTURE_VARIANT (protocol)@@ -257,8 +257,8 @@ FIXTURE_SETUP(protocol)ASSERT_EQ (0 ,set_service (&self->unspec_srv0, prot_unspec,0 )); -ASSERT_EQ (0 ,set_service (&self->unspec_any, prot_unspec,0 )); - self->unspec_any.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY ); +ASSERT_EQ (0 ,set_service (&self->unspec_any0, prot_unspec,0 )); + self->unspec_any0.ipv4_addr.sin_addr.s_addr =htonl (INADDR_ANY );setup_loopback (_metadata); };@@ -615,20 +615,18 @@ TEST_F(protocol, connect)// Kernel FIXME: tcp_sandbox_with_ipv6_tcp and tcp_sandbox_with_unix_stream TEST_F(protocol, bind_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , + }; +const struct landlock_net_service_attr tcp_bind = { + .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , + .port = self->srv0.port, + };int bind_fd, ret;if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP , - }; -const struct landlock_net_service_attr tcp_bind = { - .allowed_access =LANDLOCK_ACCESS_NET_BIND_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 );ASSERT_LE (0 , ruleset_fd);/* Allows bind. */ @@ -642,8 +640,8 @@ TEST_F(protocol, bind_unspec) bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); -/* Binds on AF_UNSPEC/INADDR_ANY. */ - ret =bind_variant (bind_fd, &self->unspec_any); +/* Allowed bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0);if (variant->prot.domain ==AF_INET ) { EXPECT_EQ(0, ret) {@@ -655,6 +653,33 @@ TEST_F(protocol, bind_unspec) }EXPECT_EQ (0 ,close (bind_fd)); +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies bind. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + bind_fd =socket_variant (&self->srv0); +ASSERT_LE (0 , bind_fd); + +/* Denied bind on AF_UNSPEC/INADDR_ANY. */ + ret =bind_variant (bind_fd, &self->unspec_any0); +if (variant->prot.domain ==AF_INET ) { +if (is_restricted (&variant->prot, variant->sandbox)) { +EXPECT_EQ (-EACCES , ret); + }else { +EXPECT_EQ (0 , ret); + } + }else { +EXPECT_EQ (-EINVAL , ret); + } +EXPECT_EQ (0 ,close (bind_fd)); + +/* Checks bind with AF_UNSPEC and the loopback address. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd); ret =bind_variant (bind_fd, &self->unspec_srv0);@@ -671,34 +696,16 @@ TEST_F(protocol, bind_unspec) TEST_F(protocol, connect_unspec) { +const struct landlock_ruleset_attr ruleset_attr = { + .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , + }; +const struct landlock_net_service_attr tcp_connect = { + .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , + .port = self->srv0.port, + };int bind_fd, client_fd, status;pid_t child; -if (variant->sandbox ==TCP_SANDBOX ) { -const struct landlock_ruleset_attr ruleset_attr = { - .handled_access_net =LANDLOCK_ACCESS_NET_CONNECT_TCP , - }; -const struct landlock_net_service_attr tcp_connect = { - .allowed_access =LANDLOCK_ACCESS_NET_CONNECT_TCP , - .port = self->srv0.port, - }; -int ruleset_fd; - - ruleset_fd =landlock_create_ruleset (&ruleset_attr, -sizeof (ruleset_attr),0 ); -ASSERT_LE (0 , ruleset_fd); - -/* Allows connect. */ -ASSERT_EQ (0 ,landlock_add_rule (ruleset_fd, -LANDLOCK_RULE_NET_SERVICE , - &tcp_connect,0 )); -enforce_ruleset (_metadata, ruleset_fd); -EXPECT_EQ (0 ,close (ruleset_fd)); - } - -/* Generic connection tests. */ -test_bind_and_connect (_metadata, &self->srv0, false, false); -/* Specific connection tests. */ bind_fd =socket_variant (&self->srv0);ASSERT_LE (0 , bind_fd);@@ -726,8 +733,22 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Allows connect. */ +ASSERT_EQ (0 , +landlock_add_rule (ruleset_fd, +LANDLOCK_RULE_NET_SERVICE , + &tcp_connect,0 )); +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } +/* Disconnects already connected socket, or set peer. */ - ret =connect_variant (connect_fd, &self->unspec_any); + ret =connect_variant (connect_fd, &self->unspec_any0);if (self->srv0.protocol.domain ==AF_UNIX && self->srv0.protocol.type ==SOCK_STREAM ) {EXPECT_EQ (-EINVAL , ret);@@ -744,6 +765,25 @@ TEST_F(protocol, connect_unspec)EXPECT_EQ (0 , ret); } +if (variant->sandbox ==TCP_SANDBOX ) { +const int ruleset_fd =landlock_create_ruleset ( + &ruleset_attr,sizeof (ruleset_attr),0 ); +ASSERT_LE (0 , ruleset_fd); + +/* Denies connect. */ +enforce_ruleset (_metadata, ruleset_fd); +EXPECT_EQ (0 ,close (ruleset_fd)); + } + + ret =connect_variant (connect_fd, &self->unspec_any0); +if (self->srv0.protocol.domain ==AF_UNIX && + self->srv0.protocol.type ==SOCK_STREAM ) { +EXPECT_EQ (-EINVAL , ret);Quoted 8 lines
tools/testing/selftests/landlock/net_test.c TEST_F(protocol, connect_unspec) · line 789
Quoted 8 lines
tools/testing/selftests/landlock/net_test.c +5
-0+ }else { +/* Always allowed to disconnect. */ +EXPECT_EQ (0 , ret); + } +EXPECT_EQ (0 ,close (connect_fd));_exit (_metadata->passed ?EXIT_SUCCESS :EXIT_FAILURE );return ;.
PATCH v11 11/12 samples/landlock: Add network demo
In reply to Konstantin MeskhidzeThis commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
---
Changes since v10:
* Refactors populate_ruleset_net() helper.
* Code style minor fix.
Changes since v9:
* Deletes ports converting.
* Minor fixes.
Changes since v8:
* Convert ports to __be16.
* Minor fixes.
Changes since v7:
* Removes network support if ABI < 4.
* Removes network support if not set by a user.
Changes since v6:
* Removes network support if ABI < 3.
Changes since v5:
* Makes network ports sandboxing optional.
* Fixes some logic errors.
* Formats code with clang-format-14.
Changes since v4:
* Adds ENV_TCP_BIND_NAME "LL_TCP_BIND" and ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT" variables
to insert TCP ports.
* Renames populate_ruleset() to populate_ruleset_fs().
* Adds populate_ruleset_net() and parse_port_num() helpers.
* Refactors main() to support network sandboxing.
--- samples/landlock/sandboxer.c | 128 +++++++++++++++++++++++++++++++---- 1 file changed, 116 insertions(+), 12 deletions(-)
samples/landlock/sandboxer.c +116 -12
@@ -8,6 +8,7 @@*/ #define _GNU_SOURCE +#include <arpa/inet.h> #include <errno.h> #include <fcntl.h> #include <linux/landlock.h>
@@ -51,6 +52,8 @@ static inline int landlock_restrict_self(const int ruleset_fd,#define ENV_FS_RO_NAME "LL_FS_RO"#define ENV_FS_RW_NAME "LL_FS_RW" +#define ENV_TCP_BIND_NAME "LL_TCP_BIND" +#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"#define ENV_PATH_TOKEN ":"static int parse_path (char *env_path,const char ***const path_list)
@@ -71,6 +74,20 @@ static int parse_path(char *env_path, const char ***const path_list)return num_paths; } +static int parse_port_num (char *env_port) +{ +int i, num_ports =0 ; + +if (env_port) { + num_ports++; +for (i =0 ; env_port[i]; i++) { +if (env_port[i] ==ENV_PATH_TOKEN [0 ]) + num_ports++; + } + } +return num_ports; +} +/* clang-format off */ #define ACCESS_FILE ( \
@@ -81,8 +98,8 @@ static int parse_path(char *env_path, const char ***const path_list)/* clang-format on */ -static int populate_ruleset (const char *const env_var,const int ruleset_fd, -const __u64 allowed_access) +static int populate_ruleset_fs (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) {int num_paths, i, ret =1 ;char *env_path_name;
@@ -143,6 +160,45 @@ static int populate_ruleset(const char *const env_var, const int ruleset_fd,return ret; } +static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ; +char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var); +if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + } + +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN )); +if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +} +/* clang-format off */ #define ACCESS_FS_ROUGHLY_READ ( \
@@ -166,39 +222,58 @@ static int populate_ruleset(const char *const env_var, const int ruleset_fd,/* clang-format on */ -#define LANDLOCK_ABI_LAST 3 +#define LANDLOCK_ABI_LAST 4int main (const int argc,char *const argv[],char *const *const envp) {const char *cmd_path;char *const *cmd_argv;int ruleset_fd, abi; +char *env_port_name;__u64 access_fs_ro =ACCESS_FS_ROUGHLY_READ , access_fs_rw =ACCESS_FS_ROUGHLY_READ |ACCESS_FS_ROUGHLY_WRITE ; +struct landlock_ruleset_attr ruleset_attr = { .handled_access_fs = access_fs_rw, + .handled_access_net =LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP , };if (argc <2 ) {fprintf (stderr, -"usage: %s=\"...\" %s=\"...\" %s <cmd> [args]...\n\n" , -ENV_FS_RO_NAME ,ENV_FS_RW_NAME , argv[0 ]); +"usage: %s=\"...\" %s=\"...\" %s=\"...\" %s=\"...\"%s " +"<cmd> [args]...\n\n" , +ENV_FS_RO_NAME ,ENV_FS_RW_NAME ,ENV_TCP_BIND_NAME , +ENV_TCP_CONNECT_NAME , argv[0 ]);fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" ); +fprintf (stderr, +"* %s: list of ports allowed to bind (server).\n" , +ENV_TCP_BIND_NAME ); +fprintf (stderr, +"* %s: list of ports allowed to connect (client).\n" , +ENV_TCP_CONNECT_NAME );fprintf (stderr,"\nexample:\n" "%s=\"/bin:/lib:/usr:/proc:/etc:/dev/urandom\" " "%s=\"/dev/null:/dev/full:/dev/zero:/dev/pts:/tmp\" " +"%s=\"9418\" " +"%s=\"80:443\" " "%s bash -i\n\n" , -ENV_FS_RO_NAME ,ENV_FS_RW_NAME , argv[0 ]); +ENV_FS_RO_NAME ,ENV_FS_RW_NAME ,ENV_TCP_BIND_NAME , +ENV_TCP_CONNECT_NAME , argv[0 ]); fprintf(stderr, "This sandboxer can use Landlock features " "up to ABI version %d.\n",
@@ -255,7 +330,12 @@ int main(const int argc, char *const argv[], char *const *const envp)case 2 :/* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */ ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE ; - + __attribute__((fallthrough)); +case 3 : +/* Removes network support for ABI < 4 */ + ruleset_attr.handled_access_net &= + ~(LANDLOCK_ACCESS_NET_BIND_TCP | +LANDLOCK_ACCESS_NET_CONNECT_TCP );fprintf (stderr,"Hint: You should update the running kernel " "to leverage Landlock features "
@@ -274,18 +354,42 @@ int main(const int argc, char *const argv[], char *const *const envp) access_fs_ro &= ruleset_attr.handled_access_fs; access_fs_rw &= ruleset_attr.handled_access_fs; +/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + } + ruleset_fd =landlock_create_ruleset (&ruleset_attr,sizeof (ruleset_attr),0 );if (ruleset_fd <0 ) {perror ("Failed to create a ruleset" );return 1 ; } -if (populate_ruleset (ENV_FS_RO_NAME , ruleset_fd, access_fs_ro)) { + +if (populate_ruleset_fs (ENV_FS_RO_NAME , ruleset_fd, access_fs_ro)) { + goto err_close_ruleset; + } +if (populate_ruleset_fs (ENV_FS_RW_NAME , ruleset_fd, access_fs_rw)) { goto err_close_ruleset; } -if (populate_ruleset (ENV_FS_RW_NAME , ruleset_fd, access_fs_rw)) { + +if (populate_ruleset_net (ENV_TCP_BIND_NAME , ruleset_fd, +LANDLOCK_ACCESS_NET_BIND_TCP )) { goto err_close_ruleset; } +if (populate_ruleset_net (ENV_TCP_CONNECT_NAME , ruleset_fd, +LANDLOCK_ACCESS_NET_CONNECT_TCP )) { + goto err_close_ruleset; + } +if (prctl (PR_SET_NO_NEW_PRIVS ,1 ,0 ,0 ,0 )) {perror ("Failed to restrict privileges" ); goto err_close_ruleset;
--
2.25.1
Günther Noack
reviewerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0
...
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;
I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }
I don't understand why parse_port_num and strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.
Wouldn't it work like this as well?
while ((strport = strsep(&env_port_name, ":"))) {
net_service.port = atoi(strport);
/* etc */
}
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 3 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));
Naming of ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.
Quoted 23 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );
samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );
As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 12 lines
samples/landlock/sandboxer.c +12
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }
This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
—Günther
Signature · 1 line
Sent using Mutt 🐕 Woof Woof
Re: [PATCH v11 11/12] samples/landlock: Add network demo
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 20 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
Quoted 8 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Well, I just followed Mickaёl's way of logic here.
Quoted 23 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.
Quoted 6 lines
Wouldn't it work like this as well?
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }
Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?
Quoted 9 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 4 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.
What do you think of ENV_PORT_TOKEN or ENV_PORT_DELIMITER???
Quoted 45 lines
Quoted 23 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
Quoted 15 lines
samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 12 lines
samples/landlock/sandboxer.c +12
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
Mickaël Salaün
maintainerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Quoted 30 lines · Konstantin Meskhidze (A)
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?
I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Quoted 11 lines
Quoted 10 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 5 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???
You can rename ENV_PATH_TOKEN to ENV_DELIMITER for the FS and network parts.
Quoted 39 lines
Quoted 39 lines
Quoted 24 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
Re: [PATCH v11 11/12] samples/landlock: Add network demo
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 80 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?
- I will fix documentation and sandbox demo and sent patch v12?
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 7 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.
Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
Mickaël Salaün
maintainerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Quoted 83 lines · Konstantin Meskhidze (A)
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 80 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 7 lines
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?
No, you need to fix that with the next series (except the Signed-off-by warnings).
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Quoted 1 line
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Quoted 117 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
Re: [PATCH v11 11/12] samples/landlock: Add network demo
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 88 lines · Konstantin Meskhidze (A)
Quoted 86 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 82 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 166
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 183
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Quoted 7 lines
Quoted 4 lines
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?No, you need to fix that with the next series (except the Signed-off-by warnings).
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???
Quoted 4 lines
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Sorry. To be honest I'm busy with another project. I dont know how
much time it will take for me to set up and run syzkaller. I need your help here please, how you do this, some roadmap.
Quoted 5 lines
Quoted 2 lines
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Ok. Sure.
Quoted 118 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c static int populate_ruleset(const char *const env_var, const int ruleset_fd, · line 186
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 104 lines
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c int main(const int argc, char *const argv[], char *const *const envp) · line 368
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
.
Mickaël Salaün
maintainerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Quoted 96 lines · Konstantin Meskhidze (A)
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 93 lines · Konstantin Meskhidze (A)
Quoted 90 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 80 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?
Quoted 6 lines
Quoted 2 lines
No, you need to fix that with the next series (except the Signed-off-by warnings).
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???
Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Quoted 8 lines
Quoted 5 lines
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Sorry. To be honest I'm busy with another project. I dont know how
much time it will take for me to set up and run syzkaller. I need your help here please, how you do this, some roadmap.
Ok, no worries, I have it set up so I'll take care of it and keep you in the loop with your GitHub account.
Quoted 124 lines
Quoted 5 lines
Quoted 2 lines
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Ok. Sure.
Quoted 118 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 104 lines
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
.
Re: [PATCH v11 11/12] samples/landlock: Add network demo
6/22/2023 1:18 PM, Mickaël Salaün пишет:
Quoted 101 lines · Konstantin Meskhidze (A)
Quoted 99 lines
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 95 lines · Konstantin Meskhidze (A)
Quoted 83 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 80 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 7 lines
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?No, you need to fix that with the next series (except the Signed-off-by warnings).
Quoted 6 lines
Quoted 3 lines
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Stephen Rothwell sent a few warnings he got with powerpcpseries_le_defconfig. Do I need to fix it in V12 patch? How can I handle it cause no warnings in current .config?
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Quoted 7 lines
Quoted 3 lines
Sorry. To be honest I'm busy with another project. I dont know how
much time it will take for me to set up and run syzkaller. I need your help here please, how you do this, some roadmap.Ok, no worries, I have it set up so I'll take care of it and keep you in the loop with your GitHub account.
Thank you!!
Quoted 126 lines
Quoted 125 lines
Quoted 6 lines
Quoted 3 lines
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Ok. Sure.
Quoted 118 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 104 lines
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
.
.
Mickaël Salaün
maintainerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Quoted 108 lines · Konstantin Meskhidze (A)
6/22/2023 1:18 PM, Mickaël Salaün пишет:
Quoted 105 lines · Konstantin Meskhidze (A)
Quoted 102 lines
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 88 lines · Konstantin Meskhidze (A)
Quoted 86 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 82 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Quoted 7 lines
Quoted 4 lines
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?No, you need to fix that with the next series (except the Signed-off-by warnings).
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???
Quoted 6 lines
Quoted 2 lines
Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Stephen Rothwell sent a few warnings he got with powerpc
pseries_le_defconfig. Do I need to fix it in V12 patch? How can I handle it cause no warnings in current .config?
Yes, this need to be fixed in the next series. Could you point to the message?
I'm almost done with the test, I revamped code and I'll send that tomorrow.
Quoted 141 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Quoted 7 lines
Quoted 3 lines
Sorry. To be honest I'm busy with another project. I dont know how
much time it will take for me to set up and run syzkaller. I need your help here please, how you do this, some roadmap.Ok, no worries, I have it set up so I'll take care of it and keep you in the loop with your GitHub account.
Thank you!!
Quoted 126 lines
Quoted 125 lines
Quoted 6 lines
Quoted 3 lines
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Ok. Sure.
Quoted 118 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 104 lines
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
.
.
Re: [PATCH v11 11/12] samples/landlock: Add network demo
7/3/2023 8:09 PM, Mickaël Salaün пишет:
Quoted 113 lines · Konstantin Meskhidze (A)
Quoted 111 lines
6/22/2023 1:18 PM, Mickaël Salaün пишет:
Quoted 107 lines · Konstantin Meskhidze (A)
Quoted 96 lines
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 93 lines · Konstantin Meskhidze (A)
Quoted 90 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 80 lines · Konstantin Meskhidze (A)
Quoted 30 lines
6/6/2023 6:17 PM, Günther Noack пишет:
Quoted 27 lines
Hi Konstantin!
Apologies if some of this was discussed before, in this case, Mickaël's review overrules my opinions from the sidelines ;)
Quoted 11 lines · Konstantin Meskhidze
This commit adds network demo. It's possible to allow a sandboxer to bind/connect to a list of particular ports restricting network actions to the rest of ports.
Signed-off-by Konstantin Meskhidze <[email protected]>samples/landlock/sandboxer.c +0
-0...
samples/landlock/sandboxer.c
Quoted 4 lines
samples/landlock/sandboxer.c +4
-0+static int populate_ruleset_net (const char *const env_var,const int ruleset_fd, +const __u64 allowed_access) +{ +int num_ports, i, ret =1 ;I thought the convention was normally to set ret = 0 initially and to override it in case of error, rather than the other way around?
Which convention? In this case, by default the return code is an error.
Quoted 28 lines
Quoted 1 line
Well, I just followed Mickaёl's way of logic here. >
Quoted 25 lines
Quoted 7 lines
samples/landlock/sandboxer.c +7
-0+char *env_port_name; +struct landlock_net_service_attr net_service = { + .allowed_access = allowed_access, + .port =0 , + }; + + env_port_name =getenv (env_var);samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) +return 0 ; + env_port_name =strdup (env_port_name); +unsetenv (env_var); + num_ports =parse_port_num (env_port_name); + +if (num_ports ==1 && (strtok (env_port_name,ENV_PATH_TOKEN ) ==NULL )) { + ret =0 ; + goto out_free_name; + }I don't understand why
parse_port_numand strtok are necessary in this program. The man-page for strsep(3) describes it as a replacement to strtok(3) (in the HISTORY section), and it has a very short example for how it is used.Wouldn't it work like this as well?
Quoted 8 lines
Quoted 4 lines
while ((strport = strsep(&env_port_name, ":"))) { net_service.port = atoi(strport); /* etc */ }Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?Quoted 6 lines
Quoted 2 lines
No, you need to fix that with the next series (except the Signed-off-by warnings).
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Quoted 7 lines
Quoted 3 lines
Stephen Rothwell sent a few warnings he got with powerpc
pseries_le_defconfig. Do I need to fix it in V12 patch? How can I handle it cause no warnings in current .config?Yes, this need to be fixed in the next series. Could you point to the message?
Here you are please:
1.
https://lore.kernel.org/linux-next/[email protected]
2. https://lore.kernel.org/linux-next/[email protected]/ 3.
https://lore.kernel.org/linux-next/[email protected]/
Quoted 2 lines
I'm almost done with the test, I revamped code and I'll send that tomorrow.
Ok.Thanks you. Please take your time. I will wait.
Quoted 142 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
What is your status on syzkaller? Do you need some help? I can write the tests if it's too much.
Quoted 134 lines
Quoted 7 lines
Quoted 3 lines
Sorry. To be honest I'm busy with another project. I dont know how
much time it will take for me to set up and run syzkaller. I need your help here please, how you do this, some roadmap.Ok, no worries, I have it set up so I'll take care of it and keep you in the loop with your GitHub account.
Thank you!!
Quoted 126 lines
Quoted 125 lines
Quoted 6 lines
Quoted 3 lines
- I will fix documentation and sandbox demo and sent patch v12?
Yes please. Let me a few days to send more reviews.
Ok. Sure.
Quoted 118 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
samples/landlock/sandboxer.c
Quoted 8 lines
samples/landlock/sandboxer.c +3
-0+ +for (i =0 ; i < num_ports; i++) { + net_service.port =atoi (strsep (&env_port_name,ENV_PATH_TOKEN ));Naming of
ENV_PATH_TOKEN:
This usage is not related to paths, maybe rename the variable? It's also technically not the token, but the delimiter.Quoted 104 lines
Quoted 4 lines
Quoted 1 line
What do you think of
ENV_PORT_TOKENorENV_PORT_DELIMITER???You can rename
ENV_PATH_TOKENtoENV_DELIMITERfor the FS and network parts.Ok. Got it.
Quoted 99 lines
Quoted 40 lines
Quoted 40 lines
Quoted 25 lines
samples/landlock/sandboxer.c +17
-2+if (landlock_add_rule (ruleset_fd,LANDLOCK_RULE_NET_SERVICE , + &net_service,0 )) { +fprintf (stderr, +"Failed to update the ruleset with port \"%lld\": %s\n" , + net_service.port,strerror (errno)); + goto out_free_name; + } + } + ret =0 ; + +out_free_name: +free (env_port_name); +return ret; +}fprintf (stderr,"Launch a command in a restricted environment.\n\n" ); -fprintf (stderr,"Environment variables containing paths, " -"each separated by a colon:\n" ); +fprintf (stderr, +"Environment variables containing paths and ports " +"each separated by a colon:\n" );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +4
-1fprintf (stderr,"* %s: list of paths allowed to be used in a read-only way.\n" ,ENV_FS_RO_NAME );fprintf (stderr, -"* %s: list of paths allowed to be used in a read-write way.\n" , +"* %s: list of paths allowed to be used in a read-write way.\n\n" ,ENV_FS_RW_NAME ); +fprintf (stderr, +"Environment variables containing ports are optional " +"and could be skipped.\n" );As it is, I believe the program does something different when I'm setting these to the empty string (ENV_TCP_BIND_NAME=""), compared to when I'm unsetting them?
Quoted 5 lines
Quoted 5 lines
I think the case where we want to forbid all handle-able networking is a legit and very common use case - it could be clearer in the documentation how this is done with the tool. (And maybe the interface could be something more explicit than setting the environment variable to empty?)
I'd like to keep it simple, and it should be seen as an example code, not a full-feature sandboxer, but still a consistent and useful one. What would you suggest?
This sandboxer tool relies on environment variables for its configuration. This is definitely not a good fit for all use cases, but I think it is simple and flexible enough. One use case might be to export a set of environment variables and simply call this tool. I'd prefer to not deal with argument parsing, but maybe that was too simplistic? We might want to revisit this approach but probably not with this series.
Quoted 25 lines
Quoted 17 lines
Quoted 4 lines
samples/landlock/sandboxer.c +2
-0+/* Removes bind access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_BIND_NAME );samples/landlock/sandboxer.c
Quoted 10 lines
samples/landlock/sandboxer.c +10
-0+if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_BIND_TCP ; + } +/* Removes connect access attribute if not supported by a user. */ + env_port_name =getenv (ENV_TCP_CONNECT_NAME ); +if (!env_port_name) { + ruleset_attr.handled_access_net &= + ~LANDLOCK_ACCESS_NET_CONNECT_TCP ; + }This is the code where the program does not restrict network usage, if the corresponding environment variable is not set.
Yep. Right.
Quoted 5 lines
It's slightly inconsistent with what this tool does for filesystem paths. - If you don't specify any file paths, it will still restrict file operations there, independent of whether that env variable was set or not. (Apologies if it was discussed before.)
Quoted 4 lines
Mickaёl wanted to make network ports optional here. Please check:
https://lore.kernel.org/linux-security-module/[email protected]/
Right, the rationale is for compatibility with the previous version of this tool. We should not break compatibility when possible. A comment should explain the rationale though.
Quoted 5 lines
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 3 lines
—Günther
.
.
.
.
Mickaël Salaün
maintainerRe: [PATCH v11 11/12] samples/landlock: Add network demo
Quoted 23 lines · Konstantin Meskhidze (A)
7/3/2023 8:09 PM, Mickaël Salaün пишет:
Quoted 20 lines · Konstantin Meskhidze (A)
Quoted 18 lines
6/22/2023 1:18 PM, Mickaël Salaün пишет:
Quoted 15 lines · Konstantin Meskhidze (A)
Quoted 13 lines
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 10 lines · Konstantin Meskhidze (A)
Quoted 8 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 5 lines · Konstantin Meskhidze (A)
Quoted 3 lines
6/6/2023 6:17 PM, Günther Noack пишет:
[...]
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 22 lines
Quoted 22 lines
Quoted 21 lines
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines
Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
Quoted 7 lines
Quoted 2 lines
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?No, you need to fix that with the next series (except the Signed-off-by warnings).
Quoted 6 lines
Quoted 3 lines
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Stephen Rothwell sent a few warnings he got with powerpc
pseries_le_defconfig. Do I need to fix it in V12 patch? How can I handle it cause no warnings in current .config?
Quoted 6 lines
Quoted 3 lines
Yes, this need to be fixed in the next series. Could you point to the message?
Here you are please:
1.
https://lore.kernel.org/linux-next/[email protected]
This issue is because the WARN_ON_ONCE() is triggered by any non-landlocked process, so removing the WARN_ON_ONCE() will fix that.
Quoted 3 lines
2.Wrong printf format.
Quoted 2 lines
3.
It looks like htmldocs doesn't like #if in enum definition. Anyway, I think it should be better to not conditionally define an enum. I've pushed this change here: https://git.kernel.org/mic/c/8c96c7eee3ff (landlock-net-v11 branch)
Quoted 4 lines
Quoted 3 lines
I'm almost done with the test, I revamped code and I'll send that tomorrow.
Ok.Thanks you. Please take your time. I will wait.
[...]
Re: [PATCH v11 11/12] samples/landlock: Add network demo
7/6/2023 5:35 PM, Mickaël Salaün пишет:
Quoted 65 lines · Konstantin Meskhidze (A)
Quoted 23 lines
7/3/2023 8:09 PM, Mickaël Salaün пишет:
Quoted 20 lines · Konstantin Meskhidze (A)
Quoted 18 lines
6/22/2023 1:18 PM, Mickaël Salaün пишет:
Quoted 15 lines · Konstantin Meskhidze (A)
Quoted 13 lines
6/19/2023 9:19 PM, Mickaël Salaün пишет:
Quoted 10 lines · Konstantin Meskhidze (A)
Quoted 8 lines
6/13/2023 11:38 PM, Mickaël Salaün пишет:
Quoted 5 lines · Konstantin Meskhidze (A)
Quoted 3 lines
6/6/2023 6:17 PM, Günther Noack пишет:
[...]
Quoted 36 lines
Quoted 36 lines
Quoted 26 lines
Quoted 26 lines
Quoted 25 lines
Quoted 14 lines
Quoted 14 lines
Quoted 13 lines
Quoted 3 lines
Thanks for a tip. I think it's a better solution here. Now this
commit is in Mickaёl's -next branch. I could send a one-commit patch later. Mickaёl, what do you think?I removed this series from -next because there is some issues (see the bot's emails), but anyway, this doesn't mean these patches don't need to be changed, they do. The goal of -next is to test more widely a patch series and get more feedbacks, especially from bots. When this series will be fully ready (and fuzzed with syzkaller), I'll push it to Linus Torvalds.
I'll review the remaining tests and sample code this week, but you can still take into account the documentation review.
Quoted 7 lines
Quoted 4 lines
Hi, Mickaёl.
I have a few quetions?
- Are you going to fix warnings for bots, meanwhile I run syzcaller?No, you need to fix that with the next series (except the Signed-off-by warnings).
Hi, Mickaёl.
As I understand its possible to check bots warnings just after you
push the next V12 series again into your -next branch???Quoted 6 lines
Quoted 2 lines
Yes, we get bot warnings on the -next tree, but the command that generate it should be reproducible.
Stephen Rothwell sent a few warnings he got with powerpc
pseries_le_defconfig. Do I need to fix it in V12 patch? How can I handle it cause no warnings in current .config?Yes, this need to be fixed in the next series. Could you point to the message?
Quoted 7 lines
Quoted 3 lines
Here you are please:
1.
https://lore.kernel.org/linux-next/[email protected]This issue is because the
WARN_ON_ONCE()is triggered by any non-landlocked process, so removing theWARN_ON_ONCE()will fix that.
Got it. Will be fixed. Thanks.
Quoted 7 lines
Quoted 4 lines
2.Wrong printf format.
Ok. I will fix it.
Quoted 9 lines
Quoted 3 lines
3.It looks like htmldocs doesn't like #if in enum definition. Anyway, I think it should be better to not conditionally define an enum. I've pushed this change here: https://git.kernel.org/mic/c/8c96c7eee3ff
(landlock-net-v11 branch)
Ok. Thank you.
Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
I'm almost done with the test, I revamped code and I'll send that tomorrow.
Ok.Thanks you. Please take your time. I will wait.
[...]
.
PATCH v11 12/12 landlock: Document Landlock's network support
In reply to Konstantin MeskhidzeDescribe network access rules for TCP sockets. Add network access example in the tutorial. Add kernel configuration support for network.
---
Changes since v10:
* Fixes documentaion as Mickaёl suggested:
https://lore.kernel.org/linux-security-module/[email protected]/
Changes since v9:
* Minor refactoring.
Changes since v8:
* Minor refactoring.
Changes since v7:
* Fixes documentaion logic errors and typos as Mickaёl suggested:
https://lore.kernel.org/netdev/[email protected]/
Changes since v6:
* Adds network support documentaion.
--- Documentation/userspace-api/landlock.rst | 83 ++++++++++++++++++------ 1 file changed, 62 insertions(+), 21 deletions(-)
Documentation/userspace-api/landlock.rst +62 -21
@@ -11,10 +11,10 @@ Landlock: unprivileged access control :Date: October 2022 The goal of Landlock is to enable to restrict ambient rights (e.g. global -filesystem access) for a set of processes. Because Landlock is a stackable -LSM, it makes possible to create safe security sandboxes as new security layers -in addition to the existing system-wide access-controls. This kind of sandbox -is expected to help mitigate the security impact of bugs or +filesystem or network access) for a set of processes. Because Landlock +is a stackable LSM, it makes possible to create safe security sandboxes as new +security layers in addition to the existing system-wide access-controls. This +kind of sandbox is expected to help mitigate the security impact of bugs or unexpected/malicious behaviors in user space applications. Landlock empowers any process, including unprivileged ones, to securely restrict themselves.
@@ -28,20 +28,24 @@ appropriately <kernel_support>`. Landlock rules ============== -A Landlock rule describes an action on an object. An object is currently a -file hierarchy, and the related filesystem actions are defined with `access -rights`_. A set of rules is aggregated in a ruleset, which can then restrict -the thread enforcing it, and its future children. +A Landlock rule describes an action on a kernel object. Filesystem +objects can be defined with a file hierarchy. Since the fourth ABI +version, TCP ports enable to identify inbound or outbound connections. +Actions on these kernel objects are defined according to `access +rights`_. A set of rules is aggregated in a ruleset, which +can then restrict the thread enforcing it, and its future children. Defining and enforcing a security policy ---------------------------------------- We first need to define the ruleset that will contain our rules. For this -example, the ruleset will contain rules that only allow read actions, but write -actions will be denied. The ruleset then needs to handle both of these kind of -actions. This is required for backward and forward compatibility (i.e. the -kernel and user space may not know each other's supported restrictions), hence -the need to be explicit about the denied-by-default access rights. +example, the ruleset will contain rules that only allow filesystem read actions +and establish a specific TCP connection, but filesystem write actions +and other TCP actions will be denied. The ruleset then needs to handle both of +these kind of actions. This is required for backward and forward compatibility +(i.e. the kernel and user space may not know each other's supported +restrictions), hence the need to be explicit about the denied-by-default access +rights. .. code-block:: c
@@ -62,6 +66,9 @@ the need to be explicit about the denied-by-default access rights. LANDLOCK_ACCESS_FS_MAKE_SYM | LANDLOCK_ACCESS_FS_REFER | LANDLOCK_ACCESS_FS_TRUNCATE, + .handled_access_net = + LANDLOCK_ACCESS_NET_BIND_TCP | + LANDLOCK_ACCESS_NET_CONNECT_TCP, }; Because we may not know on which kernel version an application will be
@@ -70,14 +77,18 @@ should try to protect users as much as possible whatever the kernel they are using. To avoid binary enforcement (i.e. either all security features or none), we can leverage a dedicated Landlock command to get the current version of the Landlock ABI and adapt the handled accesses. Let's check if we should -remove the ``LANDLOCK_ACCESS_FS_REFER`` or ``LANDLOCK_ACCESS_FS_TRUNCATE`` -access rights, which are only supported starting with the second and third -version of the ABI. +remove the ``LANDLOCK_ACCESS_FS_REFER`` or ``LANDLOCK_ACCESS_FS_TRUNCATE`` or +network access rights, which are only supported starting with the second, +third and fourth version of the ABI. .. code-block:: c int abi; + #define ACCESS_NET_BIND_CONNECT ( \ + LANDLOCK_ACCESS_NET_BIND_TCP | \ + LANDLOCK_ACCESS_NET_CONNECT_TCP) + abi = landlock_create_ruleset(NULL, 0, LANDLOCK_CREATE_RULESET_VERSION); if (abi < 0) { /* Degrades gracefully if Landlock is not handled. */
@@ -92,6 +103,11 @@ version of the ABI. case 2: /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */ ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE; + case 3: + /* Removes network support for ABI < 4 */ + ruleset_attr.handled_access_net &= + ~(LANDLOCK_ACCESS_NET_BIND_TCP | + LANDLOCK_ACCESS_NET_CONNECT_TCP); } This enables to create an inclusive ruleset that will contain our rules.
@@ -143,10 +159,23 @@ for the ruleset creation, by filtering access rights according to the Landlock ABI version. In this example, this is not required because all of the requested ``allowed_access`` rights are already available in ABI 1. -We now have a ruleset with one rule allowing read access to ``/usr`` while -denying all other handled accesses for the filesystem. The next step is to -restrict the current thread from gaining more privileges (e.g. thanks to a SUID -binary). +For network access-control, we can add a set of rules that allow to use a port +number for a specific action: HTTPS connections. + +.. code-block:: c + + struct landlock_net_service_attr net_service = { + .allowed_access = NET_CONNECT_TCP, + .port = 443, + }; + + err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_SERVICE, + &net_service, 0); + +The next step is to restrict the current thread from gaining more privileges +(e.g. through a SUID binary). We now have a ruleset with the first rule allowing +read access to ``/usr`` while denying all other handled accesses for the filesystem, +and a second rule allowing HTTPS connections. .. code-block:: c
@@ -355,7 +384,7 @@ Access rights ------------- .. kernel-doc:: include/uapi/linux/landlock.h - :identifiers: fs_access + :identifiers: fs_access net_access Creating a new ruleset ----------------------
@@ -374,6 +403,7 @@ Extending a ruleset .. kernel-doc:: include/uapi/linux/landlock.h :identifiers: landlock_rule_type landlock_path_beneath_attr + landlock_net_service_attr Enforcing a ruleset -------------------
@@ -451,6 +481,12 @@ always allowed when using a kernel that only supports the first or second ABI. Starting with the Landlock ABI version 3, it is now possible to securely control truncation thanks to the new ``LANDLOCK_ACCESS_FS_TRUNCATE`` access right. +Network support (ABI < 4) +------------------------- + +Starting with the Landlock ABI version 4, it is now possible to restrict TCP +bind and connect actions to only a set of allowed ports. + .. _kernel_support: Kernel support
@@ -469,6 +505,11 @@ still enable it by adding ``lsm=landlock,[...]`` to Documentation/admin-guide/kernel-parameters.rst thanks to the bootloader configuration. +To be able to explicitly allow TCP operations (e.g., adding a network rule with +``LANDLOCK_ACCESS_NET_TCP_BIND``), the kernel must support TCP (``CONFIG_INET=y``). +Otherwise, sys_landlock_add_rule() returns an ``EAFNOSUPPORT`` error, which can +safely be ignored because this kind of TCP operation is already not possible. + Questions and answers =====================
--
2.25.1