All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v13 00/12] Network support for Landlock
@ 2023-10-16  1:50 Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
                   ` (11 more replies)
  0 siblings, 12 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Hi,
This is a new V13 patch related to Landlock LSM network confinement.
It is based on v6.6-rc2 kernel version.

It brings refactoring of previous patch version V12.
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: 77/77 tests passed.
 2. base_test: 7/7 tests passed.
 3. fs_test: 108/108 tests passed.
 4. ptrace_test: 8/8 tests passed.

Previous versions:
v12: https://lore.kernel.org/linux-security-module/20230920092641.832134-1-konstantin.meskhidze@huawei.com/
v11: https://lore.kernel.org/linux-security-module/20230515161339.631577-1-konstantin.meskhidze@huawei.com/
v10: https://lore.kernel.org/linux-security-module/20230323085226.1432550-1-konstantin.meskhidze@huawei.com/
v9: https://lore.kernel.org/linux-security-module/20230116085818.165539-1-konstantin.meskhidze@huawei.com/
v8: https://lore.kernel.org/linux-security-module/20221021152644.155136-1-konstantin.meskhidze@huawei.com/
v7: https://lore.kernel.org/linux-security-module/20220829170401.834298-1-konstantin.meskhidze@huawei.com/
v6: https://lore.kernel.org/linux-security-module/20220621082313.3330667-1-konstantin.meskhidze@huawei.com/
v5: https://lore.kernel.org/linux-security-module/20220516152038.39594-1-konstantin.meskhidze@huawei.com
v4: https://lore.kernel.org/linux-security-module/20220309134459.6448-1-konstantin.meskhidze@huawei.com/
v3: https://lore.kernel.org/linux-security-module/20220124080215.265538-1-konstantin.meskhidze@huawei.com/
v2: https://lore.kernel.org/linux-security-module/20211228115212.703084-1-konstantin.meskhidze@huawei.com/
v1: https://lore.kernel.org/linux-security-module/20211210072123.386713-1-konstantin.meskhidze@huawei.com/

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 7 new test variants dedicated to network
  samples/landlock: Add network demo
  landlock: Document Landlock's network support

Mickaël Salaün (1):
  landlock: Allow FS topology changes for domains without such rule type

 Documentation/userspace-api/landlock.rst     |   93 +-
 include/uapi/linux/landlock.h                |   56 +
 samples/landlock/sandboxer.c                 |  115 +-
 security/landlock/Kconfig                    |    1 +
 security/landlock/Makefile                   |    2 +
 security/landlock/fs.c                       |  232 +--
 security/landlock/limits.h                   |    6 +
 security/landlock/net.c                      |  198 ++
 security/landlock/net.h                      |   33 +
 security/landlock/ruleset.c                  |  405 ++++-
 security/landlock/ruleset.h                  |  181 +-
 security/landlock/setup.c                    |    2 +
 security/landlock/syscalls.c                 |  158 +-
 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   |   73 +-
 tools/testing/selftests/landlock/net_test.c  | 1688 ++++++++++++++++++
 18 files changed, 2910 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


^ permalink raw reply	[flat|nested] 45+ messages in thread

* [PATCH v13 01/12] landlock: Make ruleset's access masks more generic
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:28   ` Mickaël Salaün
  2023-10-16  1:50 ` [PATCH v13 02/12] landlock: Allow FS topology changes for domains without such rule type Konstantin Meskhidze
                   ` (10 subsequent siblings)
  11 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-2-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* None.

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(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 1c0c198f6fdb..6953801619ac 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -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);
diff --git a/security/landlock/limits.h b/security/landlock/limits.h
index 82288f0e9e5e..bafb3b8dc677 100644
--- a/security/landlock/limits.h
+++ b/security/landlock/limits.h
@@ -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 */

diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 996484f98bfd..1f3188b4e313 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -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;
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index 55b1df8f66a8..e19c7a99dfc6 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -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 */
diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
index 245cc650a4dc..7ec6bbed7117 100644
--- a/security/landlock/syscalls.c
+++ b/security/landlock/syscalls.c
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 02/12] landlock: Allow FS topology changes for domains without such rule type
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 03/12] landlock: Refactor landlock_find_rule/insert_rule Konstantin Meskhidze
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

From: Mickaël Salaün <mic@digikod.net>

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.

Link: https://lore.kernel.org/r/20230920092641.832134-3-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* Refactors commit message.

Changes since v11:
* None.

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(-)

diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst
index d8cd8cd9ce25..f6a7da21708a 100644
--- a/Documentation/userspace-api/landlock.rst
+++ b/Documentation/userspace-api/landlock.rst
@@ -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
 -------------------
diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 6953801619ac..e8d680c2c80a 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -150,16 +150,6 @@ static struct landlock_object *get_inode_object(struct inode *const inode)
 	LANDLOCK_ACCESS_FS_TRUNCATE)
 /* clang-format on */

-/*
- * 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->fs_access_masks[]
- * entries when we need to get the absolute handled access masks.
- */
-/* clang-format off */
-#define ACCESS_INITIALLY_DENIED ( \
-	LANDLOCK_ACCESS_FS_REFER)
-/* clang-format on */
-
 /*
  * @path: Should have been checked by get_path_from_fd().
  */
@@ -179,8 +169,7 @@ 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) |
-			   ACCESS_INITIALLY_DENIED);
+			 ~landlock_get_fs_access_mask(ruleset, 0);
 	object = get_inode_object(d_backing_inode(path->dentry));
 	if (IS_ERR(object))
 		return PTR_ERR(object);
@@ -287,15 +276,16 @@ static inline bool is_nouser_or_private(const struct dentry *dentry)
 		unlikely(IS_PRIVATE(d_backing_inode(dentry))));
 }

-static inline access_mask_t
-get_handled_accesses(const struct landlock_ruleset *const domain)
+static access_mask_t
+get_raw_handled_fs_accesses(const struct landlock_ruleset *const domain)
 {
-	access_mask_t access_dom = ACCESS_INITIALLY_DENIED;
+	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_fs_access_mask(domain, layer_level);
-	return access_dom & LANDLOCK_MASK_ACCESS_FS;
+		access_dom |=
+			landlock_get_raw_fs_access_mask(domain, layer_level);
+	return access_dom;
 }

 /**
@@ -331,13 +321,8 @@ init_layer_masks(const struct landlock_ruleset *const domain,

 		for_each_set_bit(access_bit, &access_req,
 				 ARRAY_SIZE(*layer_masks)) {
-			/*
-			 * Artificially handles all initially denied by default
-			 * access rights.
-			 */
 			if (BIT_ULL(access_bit) &
-			    (landlock_get_fs_access_mask(domain, layer_level) |
-			     ACCESS_INITIALLY_DENIED)) {
+			    landlock_get_fs_access_mask(domain, layer_level)) {
 				(*layer_masks)[access_bit] |=
 					BIT_ULL(layer_level);
 				handled_accesses |= BIT_ULL(access_bit);
@@ -347,6 +332,25 @@ init_layer_masks(const struct landlock_ruleset *const domain,
 	return handled_accesses;
 }

+static access_mask_t
+get_handled_fs_accesses(const struct landlock_ruleset *const domain)
+{
+	/* Handles all initially denied by default access rights. */
+	return get_raw_handled_fs_accesses(domain) |
+	       LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
+}
+
+static const struct landlock_ruleset *get_current_fs_domain(void)
+{
+	const struct landlock_ruleset *const dom =
+		landlock_get_current_domain();
+
+	if (!dom || !get_raw_handled_fs_accesses(dom))
+		return NULL;
+
+	return dom;
+}
+
 /*
  * Check that a destination file hierarchy has more restrictions than a source
  * file hierarchy.  This is only used for link and rename actions.
@@ -519,7 +523,7 @@ static bool is_access_to_paths_allowed(
 		 * a superset of the meaningful requested accesses).
 		 */
 		access_masked_parent1 = access_masked_parent2 =
-			get_handled_accesses(domain);
+			get_handled_fs_accesses(domain);
 		is_dom_check = true;
 	} else {
 		if (WARN_ON_ONCE(dentry_child1 || dentry_child2))
@@ -651,8 +655,7 @@ static inline int check_access_path(const struct landlock_ruleset *const domain,
 static inline int current_check_access_path(const struct path *const path,
 					    const access_mask_t access_request)
 {
-	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+	const struct landlock_ruleset *const dom = get_current_fs_domain();

 	if (!dom)
 		return 0;
@@ -815,8 +818,7 @@ static int current_check_refer_path(struct dentry *const old_dentry,
 				    struct dentry *const new_dentry,
 				    const bool removable, const bool exchange)
 {
-	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+	const struct landlock_ruleset *const dom = get_current_fs_domain();
 	bool allow_parent1, allow_parent2;
 	access_mask_t access_request_parent1, access_request_parent2;
 	struct path mnt_dir;
@@ -1050,7 +1052,7 @@ static int hook_sb_mount(const char *const dev_name,
 			 const struct path *const path, const char *const type,
 			 const unsigned long flags, void *const data)
 {
-	if (!landlock_get_current_domain())
+	if (!get_current_fs_domain())
 		return 0;
 	return -EPERM;
 }
@@ -1058,7 +1060,7 @@ static int hook_sb_mount(const char *const dev_name,
 static int hook_move_mount(const struct path *const from_path,
 			   const struct path *const to_path)
 {
-	if (!landlock_get_current_domain())
+	if (!get_current_fs_domain())
 		return 0;
 	return -EPERM;
 }
@@ -1069,14 +1071,14 @@ static int hook_move_mount(const struct path *const from_path,
  */
 static int hook_sb_umount(struct vfsmount *const mnt, const int flags)
 {
-	if (!landlock_get_current_domain())
+	if (!get_current_fs_domain())
 		return 0;
 	return -EPERM;
 }

 static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
 {
-	if (!landlock_get_current_domain())
+	if (!get_current_fs_domain())
 		return 0;
 	return -EPERM;
 }
@@ -1092,7 +1094,7 @@ static int hook_sb_remount(struct super_block *const sb, void *const mnt_opts)
 static int hook_sb_pivotroot(const struct path *const old_path,
 			     const struct path *const new_path)
 {
-	if (!landlock_get_current_domain())
+	if (!get_current_fs_domain())
 		return 0;
 	return -EPERM;
 }
@@ -1128,8 +1130,7 @@ static int hook_path_mknod(const struct path *const dir,
 			   struct dentry *const dentry, const umode_t mode,
 			   const unsigned int dev)
 {
-	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+	const struct landlock_ruleset *const dom = get_current_fs_domain();

 	if (!dom)
 		return 0;
@@ -1208,8 +1209,7 @@ static int hook_file_open(struct file *const file)
 	layer_mask_t layer_masks[LANDLOCK_NUM_ACCESS_FS] = {};
 	access_mask_t open_access_request, full_access_request, allowed_access;
 	const access_mask_t optional_access = LANDLOCK_ACCESS_FS_TRUNCATE;
-	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+	const struct landlock_ruleset *const dom = get_current_fs_domain();

 	if (!dom)
 		return 0;
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index e19c7a99dfc6..6e2ad1546ab3 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -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 */
diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
index 7ec6bbed7117..d35cd5d304db 100644
--- a/security/landlock/syscalls.c
+++ b/security/landlock/syscalls.c
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 03/12] landlock: Refactor landlock_find_rule/insert_rule
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 02/12] landlock: Allow FS topology changes for domains without such rule type Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 04/12] landlock: Refactor merge/inherit_ruleset functions Konstantin Meskhidze
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Add 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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-4-konstantin.meskhidze@huawei.com
Co-developed-by: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* Adds "default" case in is_object_pointer() and get_root().
* Removes the "root" variable from get_root().
* Minor fixes.

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 | 132 ++++++++++++++++++++++++++----------
 security/landlock/ruleset.h |  65 +++++++++++++++---
 3 files changed, 164 insertions(+), 54 deletions(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index e8d680c2c80a..9edb64ac8251 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -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;
 }
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 1f3188b4e313..d5993e6254e4 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -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,20 @@ 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;
+
+	default:
+		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 +102,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 +119,26 @@ 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)
+{
+	switch (key_type) {
+	case LANDLOCK_KEY_INODE:
+		return &ruleset->root_inode;
+
+	default:
+		return ERR_PTR(-EINVAL);
+	}
+}
+
+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 +160,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 +175,35 @@ 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;

 	might_sleep();
 	lockdep_assert_held(&ruleset->lock);
-	if (WARN_ON_ONCE(!object || !layers))
+	if (WARN_ON_ONCE(!layers))
+		return -ENOENT;
+
+	if (is_object_pointer(id.type) && WARN_ON_ONCE(!id.key.object))
 		return -ENOENT;
-	walker_node = &(ruleset->root.rb_node);
+
+	root = get_root(ruleset, id.type);
+	if (IS_ERR(root))
+		return PTR_ERR(root);
+
+	walker_node = &root->rb_node;
 	while (*walker_node) {
 		struct landlock_rule *const this =
 			rb_entry(*walker_node, struct landlock_rule, node);

-		if (this->object != object) {
+		if (this->key.data != id.key.data) {
 			parent_node = *walker_node;
-			if (this->object < object)
+			if (this->key.data < id.key.data)
 				walker_node = &((*walker_node)->rb_right);
 			else
 				walker_node = &((*walker_node)->rb_left);
@@ -195,24 +235,24 @@ static int insert_rule(struct landlock_ruleset *const ruleset,
 		 * Intersects access rights when it is a merge between a
 		 * ruleset and a domain.
 		 */
-		new_rule = create_rule(object, &this->layers, this->num_layers,
+		new_rule = create_rule(id, &this->layers, this->num_layers,
 				       &(*layers)[0]);
 		if (IS_ERR(new_rule))
 			return PTR_ERR(new_rule);
-		rb_replace_node(&this->node, &new_rule->node, &ruleset->root);
-		free_rule(this);
+		rb_replace_node(&this->node, &new_rule->node, root);
+		free_rule(this, id.type);
 		return 0;
 	}

-	/* There is no match for @object. */
+	/* There is no match for @id. */
 	build_check_ruleset();
 	if (ruleset->num_rules >= LANDLOCK_MAX_NUM_RULES)
 		return -E2BIG;
-	new_rule = create_rule(object, layers, num_layers, NULL);
+	new_rule = create_rule(id, layers, num_layers, NULL);
 	if (IS_ERR(new_rule))
 		return PTR_ERR(new_rule);
 	rb_link_node(&new_rule->node, parent_node, walker_node);
-	rb_insert_color(&new_rule->node, &ruleset->root);
+	rb_insert_color(&new_rule->node, root);
 	ruleset->num_rules++;
 	return 0;
 }
@@ -230,7 +270,7 @@ static void build_check_layer(void)

 /* @ruleset must be locked by the caller. */
 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
-			 struct landlock_object *const object,
+			 const struct landlock_id id,
 			 const access_mask_t access)
 {
 	struct landlock_layer layers[] = { {
@@ -240,7 +280,7 @@ int landlock_insert_rule(struct landlock_ruleset *const ruleset,
 	} };

 	build_check_layer();
-	return insert_rule(ruleset, object, &layers, ARRAY_SIZE(layers));
+	return insert_rule(ruleset, id, &layers, ARRAY_SIZE(layers));
 }

 static inline void get_hierarchy(struct landlock_hierarchy *const hierarchy)
@@ -263,6 +303,7 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
 			 struct landlock_ruleset *const src)
 {
 	struct landlock_rule *walker_rule, *next_rule;
+	struct rb_root *src_root;
 	int err = 0;

 	might_sleep();
@@ -273,6 +314,10 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
 	if (WARN_ON_ONCE(!dst || !dst->hierarchy))
 		return -EINVAL;

+	src_root = get_root(src, LANDLOCK_KEY_INODE);
+	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);
@@ -285,11 +330,15 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
 	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,
+	rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, src_root,
 					     node) {
 		struct landlock_layer layers[] = { {
 			.level = dst->num_layers,
 		} };
+		const struct landlock_id id = {
+			.key = walker_rule->key,
+			.type = LANDLOCK_KEY_INODE,
+		};

 		if (WARN_ON_ONCE(walker_rule->num_layers != 1)) {
 			err = -EINVAL;
@@ -300,8 +349,8 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
 			goto out_unlock;
 		}
 		layers[0].access = walker_rule->layers[0].access;
-		err = insert_rule(dst, walker_rule->object, &layers,
-				  ARRAY_SIZE(layers));
+
+		err = insert_rule(dst, id, &layers, ARRAY_SIZE(layers));
 		if (err)
 			goto out_unlock;
 	}
@@ -316,21 +365,30 @@ 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);

 	/* 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,
+		};
+
+		err = insert_rule(child, id, &walker_rule->layers,
 				  walker_rule->num_layers);
 		if (err)
 			goto out_unlock;
@@ -362,8 +420,9 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
 	struct landlock_rule *freeme, *next;

 	might_sleep();
-	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root, node)
-		free_rule(freeme);
+	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
+					     node)
+		free_rule(freeme, LANDLOCK_KEY_INODE);
 	put_hierarchy(ruleset->hierarchy);
 	kfree(ruleset);
 }
@@ -454,20 +513,23 @@ landlock_merge_ruleset(struct landlock_ruleset *const parent,
  */
 const struct landlock_rule *
 landlock_find_rule(const struct landlock_ruleset *const ruleset,
-		   const struct landlock_object *const object)
+		   const struct landlock_id id)
 {
+	const struct rb_root *root;
 	const struct rb_node *node;

-	if (!object)
+	root = get_root((struct landlock_ruleset *)ruleset, id.type);
+	if (IS_ERR(root))
 		return NULL;
-	node = ruleset->root.rb_node;
+	node = root->rb_node;
+
 	while (node) {
 		struct landlock_rule *this =
 			rb_entry(node, struct landlock_rule, node);

-		if (this->object == object)
+		if (this->key.data == id.key.data)
 			return this;
-		if (this->object < object)
+		if (this->key.data < id.key.data)
 			node = node->rb_right;
 		else
 			node = node->rb_left;
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index 6e2ad1546ab3..9e04c666b23c 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -60,6 +60,47 @@ struct landlock_layer {
 	access_mask_t access;
 };

+/**
+ * union landlock_key - Key of a ruleset's red-black tree
+ */
+union landlock_key {
+	/**
+	 * @object: Pointer to identify a kernel object (e.g. an inode).
+	 */
+	struct landlock_object *object;
+	/**
+	 * @data: Raw data to identify an arbitrary 32-bit value
+	 * (e.g. a TCP port).
+	 */
+	uintptr_t data;
+};
+
+/**
+ * enum landlock_key_type - Type of &union landlock_key
+ */
+enum landlock_key_type {
+	/**
+	 * @LANDLOCK_KEY_INODE: Type of &landlock_ruleset.root_inode's node
+	 * keys.
+	 */
+	LANDLOCK_KEY_INODE = 1,
+};
+
+/**
+ * struct landlock_id - Unique rule identifier for a ruleset
+ */
+struct landlock_id {
+	/**
+	 * @key: Identifies either a kernel object (e.g. an inode) or
+	 * a raw value (e.g. a TCP port).
+	 */
+	union landlock_key key;
+	/**
+	 * @type: Type of a landlock_ruleset's root tree.
+	 */
+	const enum landlock_key_type type;
+};
+
 /**
  * struct landlock_rule - Access rights tied to an object
  */
@@ -69,12 +110,13 @@ struct landlock_rule {
 	 */
 	struct rb_node node;
 	/**
-	 * @object: Pointer to identify a kernel object (e.g. an inode).  This
-	 * is used as a key for this ruleset element.  This pointer is set once
-	 * and never modified.  It always points to an allocated object because
-	 * each rule increments the refcount of its object.
+	 * @key: A union to identify either a kernel object (e.g. an inode) or
+	 * a raw data value (e.g. a network socket port). This is used as a key
+	 * for this ruleset element.  The pointer is set once and never
+	 * modified.  It always points to an allocated object because each rule
+	 * increments the refcount of its object.
 	 */
-	struct landlock_object *object;
+	union landlock_key key;
 	/**
 	 * @num_layers: Number of entries in @layers.
 	 */
@@ -110,11 +152,12 @@ struct landlock_hierarchy {
  */
 struct landlock_ruleset {
 	/**
-	 * @root: Root of a red-black tree containing &struct landlock_rule
-	 * nodes.  Once a ruleset is tied to a process (i.e. as a domain), this
-	 * tree is immutable until @usage reaches zero.
+	 * @root_inode: Root of a red-black tree containing &struct
+	 * landlock_rule nodes with inode object.  Once a ruleset is tied to a
+	 * process (i.e. as a domain), this tree is immutable until @usage
+	 * reaches zero.
 	 */
-	struct rb_root root;
+	struct rb_root root_inode;
 	/**
 	 * @hierarchy: Enables hierarchy identification even when a parent
 	 * domain vanishes.  This is needed for the ptrace protection.
@@ -176,7 +219,7 @@ void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);

 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
-			 struct landlock_object *const object,
+			 const struct landlock_id id,
 			 const access_mask_t access);

 struct landlock_ruleset *
@@ -185,7 +228,7 @@ landlock_merge_ruleset(struct landlock_ruleset *const parent,

 const struct landlock_rule *
 landlock_find_rule(const struct landlock_ruleset *const ruleset,
-		   const struct landlock_object *const object);
+		   const struct landlock_id id);

 static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
 {
--
2.25.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 04/12] landlock: Refactor merge/inherit_ruleset functions
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (2 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 03/12] landlock: Refactor landlock_find_rule/insert_rule Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 05/12] landlock: Move and rename layer helpers Konstantin Meskhidze
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Refactor 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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-5-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None

Changes since v11:
* Fixes comment in inherit_ruleset().
https://lore.kernel.org/netdev/3b52ba0c-d013-b7a9-0f08-2e6d677a1df0@digikod.net/

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 | 116 +++++++++++++++++++++++-------------
 1 file changed, 74 insertions(+), 42 deletions(-)

diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index d5993e6254e4..958d57654b8e 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -299,36 +299,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) {
@@ -337,23 +323,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);
@@ -361,38 +376,55 @@ 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;
--
2.25.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 05/12] landlock: Move and rename layer helpers
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (3 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 04/12] landlock: Refactor merge/inherit_ruleset functions Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 06/12] landlock: Refactor " Konstantin Meskhidze
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

This 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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-6-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* None.

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(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index 9edb64ac8251..e6a19ff1765a 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -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 {
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 958d57654b8e..1cf6ac7a3c2f 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -568,3 +568,101 @@ landlock_find_rule(const struct landlock_ruleset *const ruleset,
 	}
 	return NULL;
 }
+
+/*
+ * @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).
+ */
+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])
+{
+	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;
+}
+
+/**
+ * landlock_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.
+ */
+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 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;
+}
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index 9e04c666b23c..3a7f03b39d4f 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 06/12] landlock: Refactor layer helpers
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (4 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 05/12] landlock: Move and rename layer helpers Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Add 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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-7-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* None.

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(-)

diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index e6a19ff1765a..bc7c126deea2 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -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 {
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 1cf6ac7a3c2f..4c209acee01e 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -572,14 +572,15 @@ landlock_find_rule(const struct landlock_ruleset *const ruleset,
 /*
  * @layer_masks is read and may be updated according to the access request and
  * the matching rule.
+ * @masks_array_size must be equal to ARRAY_SIZE(*layer_masks).
  *
  * Returns true if the request is allowed (i.e. relevant layer masks for the
  * request are empty).
  */
-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)
 {
 	size_t layer_level;

@@ -611,8 +612,7 @@ bool landlock_unmask_layers(
 		 * requested access.
 		 */
 		is_empty = true;
-		for_each_set_bit(access_bit, &access_req,
-				 ARRAY_SIZE(*layer_masks)) {
+		for_each_set_bit(access_bit, &access_req, masks_array_size) {
 			if (layer->access & BIT_ULL(access_bit))
 				(*layer_masks)[access_bit] &= ~layer_bit;
 			is_empty = is_empty && !(*layer_masks)[access_bit];
@@ -623,6 +623,10 @@ bool landlock_unmask_layers(
 	return false;
 }

+typedef access_mask_t
+get_access_mask_t(const struct landlock_ruleset *const ruleset,
+		  const u16 layer_level);
+
 /**
  * landlock_init_layer_masks - Initialize layer masks from an access request
  *
@@ -632,19 +636,34 @@ bool landlock_unmask_layers(
  * @domain: The domain that defines the current restrictions.
  * @access_request: The requested access rights to check.
  * @layer_masks: The layer masks to populate.
+ * @key_type: The key type to switch between access masks of different types.
  *
  * Returns: An access mask where each access right bit is set which is handled
  * in any of the active layers in @domain.
  */
-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)
 {
 	access_mask_t handled_accesses = 0;
-	size_t layer_level;
+	size_t layer_level, num_access;
+	get_access_mask_t *get_access_mask;
+
+	switch (key_type) {
+	case LANDLOCK_KEY_INODE:
+		get_access_mask = landlock_get_fs_access_mask;
+		num_access = LANDLOCK_NUM_ACCESS_FS;
+		break;
+	default:
+		WARN_ON_ONCE(1);
+		return 0;
+	}
+
+	memset(layer_masks, 0,
+	       array_size(sizeof((*layer_masks)[0]), num_access));

-	memset(layer_masks, 0, sizeof(*layer_masks));
 	/* An empty access request can happen because of O_WRONLY | O_RDWR. */
 	if (!access_request)
 		return 0;
@@ -654,10 +673,9 @@ access_mask_t landlock_init_layer_masks(
 		const unsigned long access_req = access_request;
 		unsigned long access_bit;

-		for_each_set_bit(access_bit, &access_req,
-				 ARRAY_SIZE(*layer_masks)) {
+		for_each_set_bit(access_bit, &access_req, num_access) {
 			if (BIT_ULL(access_bit) &
-			    landlock_get_fs_access_mask(domain, layer_level)) {
+			    get_access_mask(domain, layer_level)) {
 				(*layer_masks)[access_bit] |=
 					BIT_ULL(layer_level);
 				handled_accesses |= BIT_ULL(access_bit);
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index 3a7f03b39d4f..1ede2b9a79b7 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (5 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 06/12] landlock: Refactor " Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:28   ` Mickaël Salaün
  2023-10-18 16:34   ` Mickaël Salaün
  2023-10-16  1:50 ` [PATCH v13 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
                   ` (4 subsequent siblings)
  11 siblings, 2 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Change 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.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-8-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* None.

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(-)

diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
index d35cd5d304db..8a54e87dbb17 100644
--- a/security/landlock/syscalls.c
+++ b/security/landlock/syscalls.c
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (6 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:29   ` Mickaël Salaün
  2023-10-18 16:34   ` Mickaël Salaün
  2023-10-16  1:50 ` [PATCH v13 09/12] selftests/landlock: Share enforce_ruleset() Konstantin Meskhidze
                   ` (3 subsequent siblings)
  11 siblings, 2 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

This 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.
For the file system, a file descriptor is a direct access to a file/data.
But for the network, it's impossible to identify for which data/peer a
newly created socket will give access to, it's needed to wait for a
connect or bind request to identify the use case for this socket.
That's why the access rights (related to ports) are tied to an opened
socket, but this would not align with the way Landlock access control
works for the filesystem [2].
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 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).

[1]
https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2]
https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
[mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
to add_rule_net_service()]
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* Moves add_rule_net_port() back in syscalls.c and makes it static.
* Deletes bind_access_mask allowing bind action rule on port 0.
* Adds comment about port 0 in landlock_net_port_attr structure.
* Removes !ARCH_EPHEMERAL_INODES from Kconfig.
* Minor fixes.
* Refactors commit message.

Changes since v11:
* Replaces dates with "2022-2023" in net.c/h files headers.
* Removes WARN_ON_ONCE(!domain) in check_socket_access().
* Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
* Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
* Renames landlock_net_service_attr to landlock_net_port_attr.
* Defines two add_rule_net_service() functions according to
  IS_ENABLED(CONFIG_INET) instead of changing the body of the only
  function.
* Adds af_family consistency check while handling AF_UNSPEC specifically.
* Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
  action on port zero.
* Minor fixes.
* Refactors commit message.

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() and
  get_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                |  56 ++++++
 security/landlock/Kconfig                    |   1 +
 security/landlock/Makefile                   |   2 +
 security/landlock/limits.h                   |   5 +
 security/landlock/net.c                      | 198 +++++++++++++++++++
 security/landlock/net.h                      |  33 ++++
 security/landlock/ruleset.c                  |  62 +++++-
 security/landlock/ruleset.h                  |  59 +++++-
 security/landlock/setup.c                    |   2 +
 security/landlock/syscalls.c                 |  69 ++++++-
 tools/testing/selftests/landlock/base_test.c |   2 +-
 11 files changed, 466 insertions(+), 23 deletions(-)
 create mode 100644 security/landlock/net.c
 create mode 100644 security/landlock/net.h

diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
index 81d09ef9aa50..25349666b19e 100644
--- a/include/uapi/linux/landlock.h
+++ b/include/uapi/linux/landlock.h
@@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
 	 * landlock_path_beneath_attr .
 	 */
 	LANDLOCK_RULE_PATH_BENEATH = 1,
+	/**
+	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
+	 * landlock_net_port_attr .
+	 */
+	LANDLOCK_RULE_NET_PORT = 2,
 };

 /**
@@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
 	 */
 } __attribute__((packed));

+/**
+ * struct landlock_net_port_attr - Network port definition
+ *
+ * Argument of sys_landlock_add_rule().
+ */
+struct landlock_net_port_attr {
+	/**
+	 * @allowed_access: Bitmask of allowed access network for a port
+	 * (cf. `Network flags`_).
+	 */
+	__u64 allowed_access;
+	/**
+	 * @port: Network port. Landlock does not forbid rules with port 0,
+	 * since some network services use it. Port 0 is a reserved one in
+	 * TCP/IP networking, meaning that it should not be used in TCP or
+	 * UDP messages. To allocate its source port number, services call
+	 * TCP/IP network functions like bind() to request one. With port 0
+	 * it triggers the operating system to automatically search for
+	 * and return a suitable available port in the TCP/IP dynamic
+	 * port number range. This port range can be controlled by a
+	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
+	 * which is also used by IPv6.
+	 */
+	__u64 port;
+};
+
 /**
  * DOC: fs_access
  *
@@ -189,4 +226,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 */
diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
index c1e862a38410..c4bf0d5eff39 100644
--- a/security/landlock/Kconfig
+++ b/security/landlock/Kconfig
@@ -3,6 +3,7 @@
 config SECURITY_LANDLOCK
 	bool "Landlock support"
 	depends on SECURITY
+	select SECURITY_NETWORK
 	select SECURITY_PATH
 	help
 	  Landlock is a sandboxing mechanism that enables processes to restrict
diff --git a/security/landlock/Makefile b/security/landlock/Makefile
index 7bbd2f413b3e..53d3c92ae22e 100644
--- a/security/landlock/Makefile
+++ b/security/landlock/Makefile
@@ -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
\ No newline at end of file
diff --git a/security/landlock/limits.h b/security/landlock/limits.h
index bafb3b8dc677..93c9c6f91556 100644
--- a/security/landlock/limits.h
+++ b/security/landlock/limits.h
@@ -23,6 +23,11 @@
 #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
 #define LANDLOCK_SHIFT_ACCESS_FS	0

+#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 */
diff --git a/security/landlock/net.c b/security/landlock/net.c
new file mode 100644
index 000000000000..1bf26cf3c41b
--- /dev/null
+++ b/security/landlock/net.c
@@ -0,0 +1,198 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Landlock LSM - Network management and hooks
+ *
+ * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
+ * Copyright © 2022-2023 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 = {
+		.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;
+
+	/* Checks if it's a (potential) TCP socket. */
+	if (sock->type != SOCK_STREAM)
+		return 0;
+
+	/* Checks for minimal header length to safely read sa_family. */
+	if (addrlen < offsetofend(typeof(*address), sa_family))
+		return -EINVAL;
+
+	switch (address->sa_family) {
+	case AF_UNSPEC:
+	case AF_INET:
+		if (addrlen < sizeof(struct sockaddr_in))
+			return -EINVAL;
+		port = ((struct sockaddr_in *)address)->sin_port;
+		break;
+#if IS_ENABLED(CONFIG_IPV6)
+	case AF_INET6:
+		if (addrlen < SIN6_LEN_RFC2133)
+			return -EINVAL;
+		port = ((struct sockaddr_in6 *)address)->sin6_port;
+		break;
+#endif
+	default:
+		return 0;
+	}
+
+	/* Specific AF_UNSPEC handling. */
+	if (address->sa_family == AF_UNSPEC) {
+		/*
+		 * Connecting to an address with AF_UNSPEC dissolves the TCP
+		 * association, which have the same effect as closing the
+		 * 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;
+
+		/*
+		 * For compatibility reason, accept AF_UNSPEC for bind
+		 * accesses (mapped to AF_INET) only if the address is
+		 * 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;
+	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
+
+	rule = landlock_find_rule(domain, id);
+	handled_access = landlock_init_layer_masks(
+		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
+	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
+				   ARRAY_SIZE(layer_masks)))
+		return 0;
+
+	return -EACCES;
+}
+
+static int hook_socket_bind(struct socket *const sock,
+			    struct sockaddr *const address, const int addrlen)
+{
+	return check_socket_access(sock, address, addrlen,
+				   LANDLOCK_ACCESS_NET_BIND_TCP);
+}
+
+static int hook_socket_connect(struct socket *const sock,
+			       struct sockaddr *const address,
+			       const int addrlen)
+{
+	return check_socket_access(sock, address, addrlen,
+				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
+}
+
+static struct security_hook_list landlock_hooks[] __ro_after_init = {
+	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
+	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
+};
+
+__init void landlock_add_net_hooks(void)
+{
+	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
+			   LANDLOCK_NAME);
+}
diff --git a/security/landlock/net.h b/security/landlock/net.h
new file mode 100644
index 000000000000..588a49fd6907
--- /dev/null
+++ b/security/landlock/net.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock LSM - Network management and hooks
+ *
+ * Copyright © 2022-2023 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)
+{
+}
+
+static inline int
+landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
+			 access_mask_t access_rights);
+{
+	return -EAFNOSUPPORT;
+}
+#endif /* IS_ENABLED(CONFIG_INET) */
+
+#endif /* _SECURITY_LANDLOCK_NET_H */
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 4c209acee01e..1fe4298ff4a7 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
 	refcount_set(&new_ruleset->usage, 1);
 	mutex_init(&new_ruleset->lock);
 	new_ruleset->root_inode = RB_ROOT;
+
+#if IS_ENABLED(CONFIG_INET)
+	new_ruleset->root_net_port = RB_ROOT;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	new_ruleset->num_layers = num_layers;
 	/*
 	 * hierarchy = NULL
@@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
 }

 struct landlock_ruleset *
-landlock_create_ruleset(const access_mask_t fs_access_mask)
+landlock_create_ruleset(const access_mask_t fs_access_mask,
+			const access_mask_t net_access_mask)
 {
 	struct landlock_ruleset *new_ruleset;

 	/* Informs about useless ruleset. */
-	if (!fs_access_mask)
+	if (!fs_access_mask && !net_access_mask)
 		return ERR_PTR(-ENOMSG);
 	new_ruleset = create_ruleset(1);
-	if (!IS_ERR(new_ruleset))
+	if (IS_ERR(new_ruleset))
+		return new_ruleset;
+	if (fs_access_mask)
 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
+	if (net_access_mask)
+		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
 	return new_ruleset;
 }

@@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
 	case LANDLOCK_KEY_INODE:
 		return true;

+#if IS_ENABLED(CONFIG_INET)
+	case LANDLOCK_KEY_NET_PORT:
+		return false;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	default:
 		WARN_ON_ONCE(1);
 		return false;
@@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
 	case LANDLOCK_KEY_INODE:
 		return &ruleset->root_inode;

+#if IS_ENABLED(CONFIG_INET)
+	case LANDLOCK_KEY_NET_PORT:
+		return &ruleset->root_net_port;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	default:
+		WARN_ON_ONCE(1);
 		return ERR_PTR(-EINVAL);
 	}
 }
@@ -153,7 +174,8 @@ static void build_check_ruleset(void)
 	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
 	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
 	BUILD_BUG_ON(access_masks <
-		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
+		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
+		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
 }

 /**
@@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
 	if (err)
 		goto out_unlock;

+#if IS_ENABLED(CONFIG_INET)
+	/* Merges the @src network port tree. */
+	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
+	if (err)
+		goto out_unlock;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 out_unlock:
 	mutex_unlock(&src->lock);
 	mutex_unlock(&dst->lock);
@@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
 	if (err)
 		goto out_unlock;

+#if IS_ENABLED(CONFIG_INET)
+	/* Copies the @parent network port tree. */
+	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
+	if (err)
+		goto out_unlock;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
 		err = -EINVAL;
 		goto out_unlock;
@@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
 	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
 					     node)
 		free_rule(freeme, LANDLOCK_KEY_INODE);
+
+#if IS_ENABLED(CONFIG_INET)
+	rbtree_postorder_for_each_entry_safe(freeme, next,
+					     &ruleset->root_net_port, node)
+		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	put_hierarchy(ruleset->hierarchy);
 	kfree(ruleset);
 }
@@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
  *
  * @domain: The domain that defines the current restrictions.
  * @access_request: The requested access rights to check.
- * @layer_masks: The layer masks to populate.
+ * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
+ * elements according to @key_type.
  * @key_type: The key type to switch between access masks of different types.
  *
  * Returns: An access mask where each access right bit is set which is handled
@@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
 		get_access_mask = landlock_get_fs_access_mask;
 		num_access = LANDLOCK_NUM_ACCESS_FS;
 		break;
+
+#if IS_ENABLED(CONFIG_INET)
+	case LANDLOCK_KEY_NET_PORT:
+		get_access_mask = landlock_get_net_access_mask;
+		num_access = LANDLOCK_NUM_ACCESS_NET;
+		break;
+#endif /* IS_ENABLED(CONFIG_INET) */
+
 	default:
 		WARN_ON_ONCE(1);
 		return 0;
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index 1ede2b9a79b7..ba4a06035599 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -33,13 +33,16 @@
 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);
+/* Makes sure all network access rights can be stored. */
+static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
 /* 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;
+typedef u32 access_masks_t;
 /* Makes sure all ruleset access rights can be stored. */
-static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
+static_assert(BITS_PER_TYPE(access_masks_t) >=
+	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);

 typedef u16 layer_mask_t;
 /* Makes sure all layers can be checked. */
@@ -84,6 +87,11 @@ enum landlock_key_type {
 	 * keys.
 	 */
 	LANDLOCK_KEY_INODE = 1,
+	/**
+	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
+	 * node keys.
+	 */
+	LANDLOCK_KEY_NET_PORT,
 };

 /**
@@ -158,6 +166,13 @@ struct landlock_ruleset {
 	 * reaches zero.
 	 */
 	struct rb_root root_inode;
+	/**
+	 * @root_net_port: Root of a red-black tree containing &struct
+	 * landlock_rule nodes with network port. Once a ruleset is tied to a
+	 * process (i.e. as a domain), this tree is immutable until @usage
+	 * reaches zero.
+	 */
+	struct rb_root root_net_port;
 	/**
 	 * @hierarchy: Enables hierarchy identification even when a parent
 	 * domain vanishes.  This is needed for the ptrace protection.
@@ -196,13 +211,13 @@ struct landlock_ruleset {
 			 */
 			u32 num_layers;
 			/**
-			 * @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
-			 * one.  These layers are used when merging rulesets,
-			 * for user space backward compatibility (i.e.
-			 * future-proof), and to properly handle merged
+			 * @access_masks: Contains the subset of filesystem and
+			 * network actions that are restricted by a ruleset.
+			 * A domain saves all layers of merged rulesets in a
+			 * stack (FAM), starting from the first layer to the
+			 * last one.  These layers are used when merging
+			 * rulesets, for user space backward compatibility
+			 * (i.e. future-proof), and to properly handle merged
 			 * rulesets without overlapping access rights.  These
 			 * layers are set once and never changed for the
 			 * lifetime of the ruleset.
@@ -213,7 +228,8 @@ struct landlock_ruleset {
 };

 struct landlock_ruleset *
-landlock_create_ruleset(const access_mask_t access_mask);
+landlock_create_ruleset(const access_mask_t access_mask_fs,
+			const access_mask_t access_mask_net);

 void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
@@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
 		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
 }

+static inline void
+landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
+			     const access_mask_t net_access_mask,
+			     const u16 layer_level)
+{
+	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
+
+	/* Should already be checked in sys_landlock_create_ruleset(). */
+	WARN_ON_ONCE(net_access_mask != net_mask);
+	ruleset->access_masks[layer_level] |=
+		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
+}
+
 static inline access_mask_t
 landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
 				const u16 layer_level)
@@ -266,6 +295,16 @@ 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;
 }
+
+static inline access_mask_t
+landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
+			     const u16 layer_level)
+{
+	return (ruleset->access_masks[layer_level] >>
+		LANDLOCK_SHIFT_ACCESS_NET) &
+	       LANDLOCK_MASK_ACCESS_NET;
+}
+
 bool landlock_unmask_layers(const struct landlock_rule *const rule,
 			    const access_mask_t access_request,
 			    layer_mask_t (*const layer_masks)[],
diff --git a/security/landlock/setup.c b/security/landlock/setup.c
index 0f6113528fa4..df81612811bf 100644
--- a/security/landlock/setup.c
+++ b/security/landlock/setup.c
@@ -14,6 +14,7 @@
 #include "fs.h"
 #include "ptrace.h"
 #include "setup.h"
+#include "net.h"

 bool landlock_initialized __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;
diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
index 8a54e87dbb17..3ad652d9a146 100644
--- a/security/landlock/syscalls.c
+++ b/security/landlock/syscalls.c
@@ -29,6 +29,7 @@
 #include "cred.h"
 #include "fs.h"
 #include "limits.h"
+#include "net.h"
 #include "ruleset.h"
 #include "setup.h"

@@ -74,7 +75,8 @@ static void build_check_abi(void)
 {
 	struct landlock_ruleset_attr ruleset_attr;
 	struct landlock_path_beneath_attr path_beneath_attr;
-	size_t ruleset_size, path_beneath_size;
+	struct landlock_net_port_attr net_port_attr;
+	size_t ruleset_size, path_beneath_size, net_port_size;

 	/*
 	 * For each user space ABI structures, first checks that there is no
@@ -82,13 +84,19 @@ static void build_check_abi(void)
 	 * struct size.
 	 */
 	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
+	ruleset_size += sizeof(ruleset_attr.handled_access_net);
 	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
-	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
+	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);

 	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
 	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
 	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
 	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
+
+	net_port_size = sizeof(net_port_attr.allowed_access);
+	net_port_size += sizeof(net_port_attr.port);
+	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
+	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
 }

 /* Ruleset handling */
@@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
 	.write = fop_dummy_write,
 };

-#define LANDLOCK_ABI_VERSION 3
+#define LANDLOCK_ABI_VERSION 4

 /**
  * sys_landlock_create_ruleset - Create a new ruleset
@@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
 	    LANDLOCK_MASK_ACCESS_FS)
 		return -EINVAL;

+	/* Checks network content (and 32-bits cast). */
+	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
+	    LANDLOCK_MASK_ACCESS_NET)
+		return -EINVAL;
+
 	/* Checks arguments and transforms to kernel struct. */
-	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
+	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
+					  ruleset_attr.handled_access_net);
 	if (IS_ERR(ruleset))
 		return PTR_ERR(ruleset);

@@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
 	int res, err;
 	access_mask_t mask;

-	/* Copies raw user space buffer, only one type for now. */
+	/* Copies raw user space buffer. */
 	res = copy_from_user(&path_beneath_attr, rule_attr,
 			     sizeof(path_beneath_attr));
 	if (res)
@@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
 	return err;
 }

+static int add_rule_net_port(struct landlock_ruleset *ruleset,
+			     const void __user *const rule_attr)
+{
+	struct landlock_net_port_attr net_port_attr;
+	int res;
+	access_mask_t mask;
+
+	/* Copies raw user space buffer. */
+	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
+	if (res)
+		return -EFAULT;
+
+	/*
+	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
+	 * are ignored by network actions.
+	 */
+	if (!net_port_attr.allowed_access)
+		return -ENOMSG;
+
+	/*
+	 * Checks that allowed_access matches the @ruleset constraints
+	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
+	 */
+	mask = landlock_get_net_access_mask(ruleset, 0);
+	if ((net_port_attr.allowed_access | mask) != mask)
+		return -EINVAL;
+
+	/* Denies inserting a rule with port higher than 65535. */
+	if (net_port_attr.port > U16_MAX)
+		return -EINVAL;
+
+	/* Imports the new rule. */
+	return landlock_append_net_rule(ruleset, net_port_attr.port,
+					net_port_attr.allowed_access);
+}
+
 /**
  * sys_landlock_add_rule - Add a new rule to a ruleset
  *
  * @ruleset_fd: File descriptor tied to the ruleset that should be extended
  *		with the new rule.
- * @rule_type: Identify the structure type pointed to by @rule_attr (only
- *             %LANDLOCK_RULE_PATH_BENEATH for now).
+ * @rule_type: Identify the structure type pointed to by @rule_attr:
+ *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
  * @rule_attr: Pointer to a rule (only of type &struct
  *             landlock_path_beneath_attr for now).
  * @flags: Must be 0.
@@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
  * Possible returned errors are:
  *
  * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
+ * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
+ *   supported by the running kernel;
  * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
  *   &landlock_path_beneath_attr.allowed_access is not a subset of the
  *   ruleset handled accesses);
@@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
 	case LANDLOCK_RULE_PATH_BENEATH:
 		err = add_rule_path_beneath(ruleset, rule_attr);
 		break;
+	case LANDLOCK_RULE_NET_PORT:
+		err = add_rule_net_port(ruleset, rule_attr);
+		break;
 	default:
 		err = -EINVAL;
 		break;
diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
index 792c3f0a59b4..646f778dfb1e 100644
--- a/tools/testing/selftests/landlock/base_test.c
+++ b/tools/testing/selftests/landlock/base_test.c
@@ -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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 09/12] selftests/landlock: Share enforce_ruleset()
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (7 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network Konstantin Meskhidze
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

This commit moves enforce_ruleset() helper function to common.h so that
it can be used both by filesystem tests and network ones.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-10-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* Fixes a bug TEST_F_FORK(layout1) -> TEST_F_FORK(layout0) in fs_test.c.

Changes since v11:
* None.

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(-)

diff --git a/tools/testing/selftests/landlock/common.h b/tools/testing/selftests/landlock/common.h
index d7987ae8d7fc..0fd6c4cf5e6f 100644
--- a/tools/testing/selftests/landlock/common.h
+++ b/tools/testing/selftests/landlock/common.h
@@ -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));
+	}
+}
diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 251594306d40..68b7a89cf65b 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -677,16 +677,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(layout0, proc_nsfs)
 {
 	const struct rule rules[] = {
--
2.25.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (8 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 09/12] selftests/landlock: Share enforce_ruleset() Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:32   ` Mickaël Salaün
  2023-10-16  1:50 ` [PATCH v13 11/12] samples/landlock: Add network demo Konstantin Meskhidze
  2023-10-16  1:50 ` [PATCH v13 12/12] landlock: Document Landlock's network support Konstantin Meskhidze
  11 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

These test suites try to check edge cases for TCP sockets
bind() and connect() actions.

protocol:
* bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
* connect: Tests with non-landlocked/landlocked ipv4, ipv6 and unix
sockets.
* bind_unspec: Tests with non-landlocked/landlocked restrictions
for bind action with AF_UNSPEC socket family.
* connect_unspec: Tests with non-landlocked/landlocked restrictions
for connect action with AF_UNSPEC socket family.

ipv4:
* from_unix_to_inet: Tests to make sure unix sockets' actions are not
restricted by Landlock rules applied to TCP ones.

tcp_layers:
* ruleset_overlap.
* ruleset_expand.

mini:
* network_access_rights: Tests with  legitimate access values.
* unknown_access_rights: Tests with invalid attributes, out of access range.
* inval:
    - unhandled allowed access.
    - zero access value.
* tcp_port_overflow: Tests with wrong port values more than U16_MAX.

ipv4_tcp:
* port_endianness: Tests with big/little endian port formats.

port_specific:
* bind_connect: Tests with specific port values.

layout1:
* with_net: Tests with network bind() socket action within
filesystem directory access test.

Test coverage for security/landlock is 94.5% of 932 lines according
to gcc/gcov-11.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
Co-developed-by:: Mickaël Salaün <mic@digikod.net>
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* Renames port_zero to port_specific fixture.
* Refactors port_specific test:
    - Adds set_port() and get_binded_port() helpers.
    - Adds checks for port 0, allowed by Landlock in this version.
    - Adds checks for port 1023.
* Refactors commit message.

Changes since v11:
* Adds 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.
* Revamps 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.
* Replaces 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.
* Adds a new unspec fixture to replace inet.bind_afunspec with
  unspec.bind and inet.connect_afunspec with unspec.connect, factoring
  and simplifying code.
* Replaces 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.
* Adds new types (protocol_variant, service_fixture) and update related
  helpers to get more generic test code.
* Replaces static (port) arrays with service_fixture variables.
* Adds 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.
* Switchs from a unix socket path address to an abstract one. This
  enables to avoid file cleanup in test teardowns.
* Closes all rulesets after enforcement.
* Removes the duplicate "empty access" test.
* Replaces 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.
* Drops capabilities in all FIXTURE_SETUP().
* Changes test ports to cover more ranges.
* Adds "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.
* Rewrites 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.
* Adds two_srv.port_endianness test, extracted and extended from
  inet.inval_port_format .
* Adds Microsoft copyright.
* Rename some variables to make them easier to read.
* Constifies variables.
* Adds minimal logs to help debug test failures.
* Renames inet test to ipv4 and deletes is_sandboxed and prot vars from
  FIXTURE_VARIANT.
* Adds port_zero tests.
* Renames all "net_service" to "net_port".

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  |   63 +
 tools/testing/selftests/landlock/net_test.c | 1688 +++++++++++++++++++
 3 files changed, 1755 insertions(+)
 create mode 100644 tools/testing/selftests/landlock/net_test.c

diff --git a/tools/testing/selftests/landlock/config b/tools/testing/selftests/landlock/config
index 3dc9e438eab1..0086efaa7b68 100644
--- a/tools/testing/selftests/landlock/config
+++ b/tools/testing/selftests/landlock/config
@@ -1,5 +1,9 @@
 CONFIG_CGROUPS=y
 CONFIG_CGROUP_SCHED=y
+CONFIG_INET=y
+CONFIG_IPV6=y
+CONFIG_NET=y
+CONFIG_NET_NS=y
 CONFIG_OVERLAY_FS=y
 CONFIG_PROC_FS=y
 CONFIG_SECURITY=y
diff --git a/tools/testing/selftests/landlock/fs_test.c b/tools/testing/selftests/landlock/fs_test.c
index 68b7a89cf65b..4fa9d3071ad2 100644
--- a/tools/testing/selftests/landlock/fs_test.c
+++ b/tools/testing/selftests/landlock/fs_test.c
@@ -8,9 +8,11 @@
  */

 #define _GNU_SOURCE
+#include <arpa/inet.h>
 #include <fcntl.h>
 #include <linux/landlock.h>
 #include <linux/magic.h>
+#include <netinet/in.h>
 #include <sched.h>
 #include <stdio.h>
 #include <string.h>
@@ -18,6 +20,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 <sys/vfs.h>
@@ -4752,4 +4755,64 @@ TEST_F_FORK(layout3_fs, release_inodes)
 	ASSERT_EQ(EACCES, test_open(TMP_DIR, O_RDONLY));
 }

+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_port_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_PORT,
+				       &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
diff --git a/tools/testing/selftests/landlock/net_test.c b/tools/testing/selftests/landlock/net_test.c
new file mode 100644
index 000000000000..962368458185
--- /dev/null
+++ b/tools/testing/selftests/landlock/net_test.c
@@ -0,0 +1,1688 @@
+// 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 void set_port(struct service_fixture *const srv, in_port_t port)
+{
+	switch (srv->protocol.domain) {
+	case AF_UNSPEC:
+	case AF_INET:
+		srv->ipv4_addr.sin_port = port;
+		return;
+
+	case AF_INET6:
+		srv->ipv6_addr.sin6_port = port;
+		return;
+
+	default:
+		return;
+	}
+}
+
+static in_port_t get_binded_port(int socket_fd,
+				 const struct protocol_variant *const prot)
+{
+	struct sockaddr_in ipv4_addr;
+	struct sockaddr_in6 ipv6_addr;
+	socklen_t ipv4_addr_len, ipv6_addr_len;
+
+	/* Gets binded port. */
+	switch (prot->domain) {
+	case AF_UNSPEC:
+	case AF_INET:
+		ipv4_addr_len = sizeof(ipv4_addr);
+		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
+		return ntohs(ipv4_addr.sin_port);
+
+	case AF_INET6:
+		ipv6_addr_len = sizeof(ipv6_addr);
+		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
+		return ntohs(ipv6_addr.sin6_port);
+
+	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_any0, 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_any0, prot_unspec, 0));
+	self->unspec_any0.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_port_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_port_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_PORT,
+					    &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_PORT,
+					    &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_port_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_port_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_PORT,
+					    &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_PORT,
+					    &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));
+}
+
+TEST_F(protocol, bind_unspec)
+{
+	const struct landlock_ruleset_attr ruleset_attr = {
+		.handled_access_net = LANDLOCK_ACCESS_NET_BIND_TCP,
+	};
+	const struct landlock_net_port_attr tcp_bind = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = self->srv0.port,
+	};
+	int bind_fd, 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 bind. */
+		ASSERT_EQ(0,
+			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					    &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);
+
+	/* 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)
+		{
+			TH_LOG("Failed to bind to unspec/any socket: %s",
+			       strerror(errno));
+		}
+	} else {
+		EXPECT_EQ(-EINVAL, ret);
+	}
+	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);
+	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)
+{
+	const struct landlock_ruleset_attr ruleset_attr = {
+		.handled_access_net = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+	};
+	const struct landlock_net_port_attr tcp_connect = {
+		.allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+		.port = self->srv0.port,
+	};
+	int bind_fd, client_fd, status;
+	pid_t child;
+
+	/* 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);
+		}
+
+		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_PORT,
+						       &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_any0);
+		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);
+		}
+
+		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;
+	}
+
+	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);
+	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_port_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_PORT,
+					    &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_DGRAM | 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));
+	}
+	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 srv1. */
+	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_port_attr tcp_bind = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = self->srv0.port,
+	};
+	const struct landlock_net_port_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_PORT,
+					    &tcp_bind, 0));
+		/* Also allows bind, but allows connect too. */
+		ASSERT_EQ(0,
+			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					    &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_PORT,
+					    &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_PORT,
+					    &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_port_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_PORT,
+					    &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_port_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_port_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_PORT,
+					    &tcp_bind_connect_p0, 0));
+		ASSERT_EQ(0,
+			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					    &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_port_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_PORT,
+					    &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_port_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_PORT,
+					    &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_port_attr tcp_bind_connect = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
+				  LANDLOCK_ACCESS_NET_CONNECT_TCP,
+		.port = sock_port_start,
+	};
+	const struct landlock_net_port_attr tcp_denied = {
+		.allowed_access = 0,
+		.port = sock_port_start,
+	};
+	const struct landlock_net_port_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_PORT,
+					&tcp_bind_connect, 0));
+	EXPECT_EQ(EINVAL, errno);
+
+	/* Checks zero access value. */
+	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					&tcp_denied, 0));
+	EXPECT_EQ(ENOMSG, errno);
+
+	/* Adds with legitimate values. */
+	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+				       &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_port_attr port_max_bind = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = UINT16_MAX,
+	};
+	const struct landlock_net_port_attr port_max_connect = {
+		.allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+		.port = UINT16_MAX,
+	};
+	const struct landlock_net_port_attr port_overflow1 = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = UINT16_MAX + 1,
+	};
+	const struct landlock_net_port_attr port_overflow2 = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = UINT16_MAX + 2,
+	};
+	const struct landlock_net_port_attr port_overflow3 = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		.port = UINT32_MAX + 1UL,
+	};
+	const struct landlock_net_port_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_PORT,
+				       &port_max_bind, 0));
+
+	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					&port_overflow1, 0));
+	EXPECT_EQ(EINVAL, errno);
+
+	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					&port_overflow2, 0));
+	EXPECT_EQ(EINVAL, errno);
+
+	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					&port_overflow3, 0));
+	EXPECT_EQ(EINVAL, errno);
+
+	/* Interleaves with invalid rule additions. */
+	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+				       &port_max_connect, 0));
+
+	EXPECT_EQ(-1, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					&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(ipv4_tcp)
+{
+	struct service_fixture srv0, srv1;
+};
+
+FIXTURE_SETUP(ipv4_tcp)
+{
+	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(ipv4_tcp)
+{
+}
+
+TEST_F(ipv4_tcp, 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_port_attr bind_host_endian_p0 = {
+		.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+		/* Host port format. */
+		.port = self->srv0.port,
+	};
+	const struct landlock_net_port_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_port_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_PORT,
+				       &bind_host_endian_p0, 0));
+	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+				       &connect_big_endian_p0, 0));
+	ASSERT_EQ(0, landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+				       &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);
+}
+
+FIXTURE(port_specific)
+{
+	struct service_fixture srv0;
+};
+
+FIXTURE_VARIANT(port_specific)
+{
+	const enum sandbox_type sandbox;
+	const struct protocol_variant prot;
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(port_specific, no_sandbox_with_ipv4) {
+	/* clang-format on */
+	.sandbox = NO_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(port_specific, sandbox_with_ipv4) {
+	/* clang-format on */
+	.sandbox = TCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET,
+		.type = SOCK_STREAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(port_specific, no_sandbox_with_ipv6) {
+	/* clang-format on */
+	.sandbox = NO_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+	},
+};
+
+/* clang-format off */
+FIXTURE_VARIANT_ADD(port_specific, sandbox_with_ipv6) {
+	/* clang-format on */
+	.sandbox = TCP_SANDBOX,
+	.prot = {
+		.domain = AF_INET6,
+		.type = SOCK_STREAM,
+	},
+};
+
+FIXTURE_SETUP(port_specific)
+{
+	disable_caps(_metadata);
+
+	ASSERT_EQ(0, set_service(&self->srv0, variant->prot, 0));
+
+	setup_loopback(_metadata);
+};
+
+FIXTURE_TEARDOWN(port_specific)
+{
+}
+
+TEST_F(port_specific, bind_connect)
+{
+	int socket_fd, ret;
+
+	/* Adds the first rule layer with bind and connect actions. */
+	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_port_attr tcp_bind_connect_zero = {
+			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
+					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
+			.port = htons(0),
+		};
+
+		int ruleset_fd;
+
+		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
+						     sizeof(ruleset_attr), 0);
+		ASSERT_LE(0, ruleset_fd);
+
+		/* Checks zero port value on bind and connect actions. */
+		EXPECT_EQ(0,
+			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					    &tcp_bind_connect_zero, 0));
+
+		enforce_ruleset(_metadata, ruleset_fd);
+		EXPECT_EQ(0, close(ruleset_fd));
+	}
+
+	socket_fd = socket_variant(&self->srv0);
+	ASSERT_LE(0, socket_fd);
+
+	/* Sets address port to 0 for both protocol families. */
+	set_port(&self->srv0, htons(0));
+
+	/* Binds on port 0. */
+	ret = bind_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	} else {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	}
+
+	/* Connects on port 0. */
+	ret = connect_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		EXPECT_EQ(-ECONNREFUSED, ret);
+	} else {
+		EXPECT_EQ(-ECONNREFUSED, ret);
+	}
+
+	/* Binds on port 0. */
+	ret = bind_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	} else {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	}
+
+	/* Sets binded port for both protocol families. */
+	set_port(&self->srv0,
+		 htons(get_binded_port(socket_fd, &variant->prot)));
+
+	/* Connects on the binded port. */
+	ret = connect_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Denied by Landlock. */
+		EXPECT_EQ(-EACCES, ret);
+	} else {
+		EXPECT_EQ(0, ret);
+	}
+
+	EXPECT_EQ(0, close(socket_fd));
+
+	/* Adds the second rule layer with just bind action. */
+	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_port_attr tcp_bind_zero = {
+			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+			.port = htons(0),
+		};
+
+		/* A rule with port value less than 1024. */
+		const struct landlock_net_port_attr tcp_bind_lower_range = {
+			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
+			.port = htons(1023),
+		};
+
+		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_PORT,
+					    &tcp_bind_lower_range, 0));
+		ASSERT_EQ(0,
+			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+					    &tcp_bind_zero, 0));
+
+		enforce_ruleset(_metadata, ruleset_fd);
+		EXPECT_EQ(0, close(ruleset_fd));
+	}
+
+	socket_fd = socket_variant(&self->srv0);
+	ASSERT_LE(0, socket_fd);
+
+	/* Sets address port to 1023 for both protocol families. */
+	set_port(&self->srv0, htons(1023));
+
+	/* Binds on port 1023. */
+	ret = bind_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Denied by the system. */
+		EXPECT_EQ(-EACCES, ret);
+	} else {
+		/* Denied by the system. */
+		EXPECT_EQ(-EACCES, ret);
+	}
+
+	/* Sets address port to 0 for both protocol families. */
+	set_port(&self->srv0, htons(0));
+
+	/* Binds on port 0. */
+	ret = bind_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	} else {
+		/* Binds to a random port within ip_local_port_range. */
+		EXPECT_EQ(0, ret);
+	}
+
+	/* Sets binded port for both protocol families. */
+	set_port(&self->srv0,
+		 htons(get_binded_port(socket_fd, &variant->prot)));
+
+	/* Connects on the binded port. */
+	ret = connect_variant(socket_fd, &self->srv0);
+	if (is_restricted(&variant->prot, variant->sandbox)) {
+		/* Denied by Landlock. */
+		EXPECT_EQ(-EACCES, ret);
+	} else {
+		EXPECT_EQ(0, ret);
+	}
+
+	EXPECT_EQ(0, close(socket_fd));
+}
+
+TEST_HARNESS_MAIN
--
2.25.1


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 11/12] samples/landlock: Add network demo
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (9 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:33   ` Mickaël Salaün
  2023-10-16  1:50 ` [PATCH v13 12/12] landlock: Document Landlock's network support Konstantin Meskhidze
  11 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

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 <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-12-konstantin.meskhidze@huawei.com
[mic: Define __SANE_USERSPACE_TYPES__ to select int-ll64.h and avoid
format warnings for PowerPC]
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* Defines __SANE_USERSPACE_TYPES__ to avoid warnings for PowerPC.

Changes since v11:
* Changes ENV_PATH_TOKEN to ENV_DELIMITER.
* Refactors populate_ruleset_net():
  - Deletes parse_port_num() helper.
  - Uses strsep() instead of strtok().
* Fixes wrong printf format.

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 | 115 ++++++++++++++++++++++++++++++-----
 1 file changed, 100 insertions(+), 15 deletions(-)

diff --git a/samples/landlock/sandboxer.c b/samples/landlock/sandboxer.c
index e2056c8b902c..08596c0ef070 100644
--- a/samples/landlock/sandboxer.c
+++ b/samples/landlock/sandboxer.c
@@ -8,6 +8,8 @@
  */

 #define _GNU_SOURCE
+#define __SANE_USERSPACE_TYPES__
+#include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <linux/landlock.h>
@@ -51,7 +53,9 @@ 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_PATH_TOKEN ":"
+#define ENV_TCP_BIND_NAME "LL_TCP_BIND"
+#define ENV_TCP_CONNECT_NAME "LL_TCP_CONNECT"
+#define ENV_DELIMITER ":"

 static int parse_path(char *env_path, const char ***const path_list)
 {
@@ -60,13 +64,13 @@ static int parse_path(char *env_path, const char ***const path_list)
 	if (env_path) {
 		num_paths++;
 		for (i = 0; env_path[i]; i++) {
-			if (env_path[i] == ENV_PATH_TOKEN[0])
+			if (env_path[i] == ENV_DELIMITER[0])
 				num_paths++;
 		}
 	}
 	*path_list = malloc(num_paths * sizeof(**path_list));
 	for (i = 0; i < num_paths; i++)
-		(*path_list)[i] = strsep(&env_path, ENV_PATH_TOKEN);
+		(*path_list)[i] = strsep(&env_path, ENV_DELIMITER);

 	return num_paths;
 }
@@ -81,8 +85,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 +147,39 @@ 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 ret = 1;
+	char *env_port_name, *strport;
+	struct landlock_net_port_attr net_port = {
+		.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);
+
+	while ((strport = strsep(&env_port_name, ENV_DELIMITER))) {
+		net_port.port = atoi(strport);
+		if (landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+				      &net_port, 0)) {
+			fprintf(stderr,
+				"Failed to update the ruleset with port \"%llu\": %s\n",
+				net_port.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 +203,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 4

 int 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 +311,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 +335,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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* [PATCH v13 12/12] landlock: Document Landlock's network support
  2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
                   ` (10 preceding siblings ...)
  2023-10-16  1:50 ` [PATCH v13 11/12] samples/landlock: Add network demo Konstantin Meskhidze
@ 2023-10-16  1:50 ` Konstantin Meskhidze
  2023-10-18 12:34   ` Mickaël Salaün
  11 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze @ 2023-10-16  1:50 UTC (permalink / raw)
  To: mic
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Describe network access rules for TCP sockets. Add network access
example in the tutorial. Add kernel configuration support for network.

Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
Link: https://lore.kernel.org/r/20230920092641.832134-13-konstantin.meskhidze@huawei.com
Signed-off-by: Mickaël Salaün <mic@digikod.net>
---

Changes since v12:
* None.

Changes since v11:
* Fixes documentaion as suggested in Günther's and Mickaёl's reviews:
https://lore.kernel.org/netdev/3ad02c76-90d8-4723-e554-7f97ef115fc0@digikod.net/

Changes since v10:
* Fixes documentaion as Mickaёl suggested:
https://lore.kernel.org/linux-security-module/ec23be77-566e-c8fd-179e-f50e025ac2cf@digikod.net/

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/9f354862-2bc3-39ea-92fd-53803d9bbc21@digikod.net/

Changes since v6:
* Adds network support documentaion.

---
 Documentation/userspace-api/landlock.rst | 87 ++++++++++++++++++------
 1 file changed, 66 insertions(+), 21 deletions(-)

diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst
index f6a7da21708a..affadd9ac662 100644
--- a/Documentation/userspace-api/landlock.rst
+++ b/Documentation/userspace-api/landlock.rst
@@ -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,34 @@ 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
+A Landlock rule describes an action on an object which the process intends to
+perform.  A set of rules is aggregated in a ruleset, which can then restrict
 the thread enforcing it, and its future children.

+The two existing types of rules are:
+
+Filesystem rules
+    For these rules, the object is a file hierarchy,
+    and the related filesystem actions are defined with
+    `filesystem access rights`.
+
+Network rules (since ABI v4)
+    For these rules, the object is currently a TCP port,
+    and the related actions are defined with `network access rights`.
+
 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.
+We first need to define the ruleset that will contain our rules.
+
+For this example, the ruleset will contain rules that only allow filesystem
+read actions and establish a specific TCP connection. 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 +76,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,9 +87,7 @@ 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 access rights which are only supported in higher versions of the ABI.

 .. code-block:: c

@@ -92,6 +107,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 +163,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_port_attr net_port = {
+        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
+        .port = 443,
+    };
+
+    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
+                            &net_port, 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 +388,7 @@ Access rights
 -------------

 .. kernel-doc:: include/uapi/linux/landlock.h
-    :identifiers: fs_access
+    :identifiers: fs_access net_access

 Creating a new ruleset
 ----------------------
@@ -374,6 +407,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 +485,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 +509,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


^ permalink raw reply related	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 01/12] landlock: Make ruleset's access masks more generic
  2023-10-16  1:50 ` [PATCH v13 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
@ 2023-10-18 12:28   ` Mickaël Salaün
  2023-10-19  1:45     ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:28 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:19AM +0800, Konstantin Meskhidze wrote:
> 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.
> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-2-konstantin.meskhidze@huawei.com

Please don't include Link that points to the previous patch series. I
add them when I apply a patch series to identify its source.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall
  2023-10-16  1:50 ` [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
@ 2023-10-18 12:28   ` Mickaël Salaün
  2023-10-19 11:59     ` Konstantin Meskhidze (A)
  2023-10-18 16:34   ` Mickaël Salaün
  1 sibling, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:28 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:25AM +0800, Konstantin Meskhidze wrote:
> Change the landlock_add_rule() syscall to support new rule types
> in future Landlock versions. Add the add_rule_path_beneath() helper

with next commits. Add the add_rule_path_beneath() helper

> to support current filesystem rules.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-16  1:50 ` [PATCH v13 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
@ 2023-10-18 12:29   ` Mickaël Salaün
  2023-10-20  4:08     ` Konstantin Meskhidze (A)
  2023-10-18 16:34   ` Mickaël Salaün
  1 sibling, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:29 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> This commit adds network rules support in the ruleset management

Here are some advices to better write commit messages:
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
The "Describe your changes in imperative mood" part is important for
this commit and others. Most of this patch series' commit messages need
small updates.

> 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

Please explain the "why" (when it makes sense) instead of just listing
the "what".

> rights can be stored. Implement socket_bind() and socket_connect()
> LSM hooks, which enables to restrict TCP socket binding and connection
> to specific ports.

I reworded and moved this part in last:
> For the file system, a file descriptor is a direct access to a file/data.
> But for the network, it's impossible to identify for which data/peer a
> newly created socket will give access to, it's needed to wait for a
> connect or bind request to identify the use case for this socket.
> That's why the access rights (related to ports) are tied to an opened
> socket, but this would not align with the way Landlock access control
> works for the filesystem [2].

Please add empty line to split paragraphs.

> 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 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).

For the file system, a file descriptor is a direct access to a file/data.
However, for network sockets, we cannot identify for which data or peer a newly
created socket will give access to. Indeed, we need to wait for a connect or
bind request to identify the use case for this socket.

Access rights are not tied to socket file descriptors. Instead, bind and
connect actions are controlled by the task's domain.  As for the filesystem, a
directory file descriptor may enable to open another file (i.e. a new data
item), but this opening is restricted by the task's domain, not the file
descriptor's access rights [2].

> 
> [1]
> https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
> [2]
> https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net

[1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
[2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net


> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
> [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
> to add_rule_net_service()]
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * Moves add_rule_net_port() back in syscalls.c and makes it static.
> * Deletes bind_access_mask allowing bind action rule on port 0.
> * Adds comment about port 0 in landlock_net_port_attr structure.
> * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
> * Minor fixes.
> * Refactors commit message.
> 
> Changes since v11:
> * Replaces dates with "2022-2023" in net.c/h files headers.
> * Removes WARN_ON_ONCE(!domain) in check_socket_access().
> * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
> * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
> * Renames landlock_net_service_attr to landlock_net_port_attr.
> * Defines two add_rule_net_service() functions according to
>   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>   function.
> * Adds af_family consistency check while handling AF_UNSPEC specifically.
> * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>   action on port zero.
> * Minor fixes.
> * Refactors commit message.
> 
> 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() and
>   get_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                |  56 ++++++
>  security/landlock/Kconfig                    |   1 +
>  security/landlock/Makefile                   |   2 +
>  security/landlock/limits.h                   |   5 +
>  security/landlock/net.c                      | 198 +++++++++++++++++++
>  security/landlock/net.h                      |  33 ++++
>  security/landlock/ruleset.c                  |  62 +++++-
>  security/landlock/ruleset.h                  |  59 +++++-
>  security/landlock/setup.c                    |   2 +
>  security/landlock/syscalls.c                 |  69 ++++++-
>  tools/testing/selftests/landlock/base_test.c |   2 +-
>  11 files changed, 466 insertions(+), 23 deletions(-)
>  create mode 100644 security/landlock/net.c
>  create mode 100644 security/landlock/net.h
> 
> diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
> index 81d09ef9aa50..25349666b19e 100644
> --- a/include/uapi/linux/landlock.h
> +++ b/include/uapi/linux/landlock.h
> @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>  	 * landlock_path_beneath_attr .
>  	 */
>  	LANDLOCK_RULE_PATH_BENEATH = 1,
> +	/**
> +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
> +	 * landlock_net_port_attr .
> +	 */
> +	LANDLOCK_RULE_NET_PORT = 2,

We don't need the explicit " = 2".

>  };
> 
>  /**
> @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>  	 */
>  } __attribute__((packed));
> 
> +/**
> + * struct landlock_net_port_attr - Network port definition
> + *
> + * Argument of sys_landlock_add_rule().
> + */
> +struct landlock_net_port_attr {
> +	/**
> +	 * @allowed_access: Bitmask of allowed access network for a port
> +	 * (cf. `Network flags`_).
> +	 */
> +	__u64 allowed_access;
> +	/**
> +	 * @port: Network port. Landlock does not forbid rules with port 0,
> +	 * since some network services use it. Port 0 is a reserved one in
> +	 * TCP/IP networking, meaning that it should not be used in TCP or
> +	 * UDP messages. To allocate its source port number, services call
> +	 * TCP/IP network functions like bind() to request one. With port 0
> +	 * it triggers the operating system to automatically search for
> +	 * and return a suitable available port in the TCP/IP dynamic
> +	 * port number range. This port range can be controlled by a
> +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
> +	 * which is also used by IPv6.

This looks too inspired from
https://www.lifewire.com/port-0-in-tcp-and-udp-818145

Let's make it simpler:

 * @port: Network port.
 *
 * It should be noted that port 0 passed to :manpage:`bind(2)` will
 * bind to an available port from a specific port range. This can be
 * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
 * sysctl (also used for IPv6). A Landlock rule with port 0 and the
 * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
 * on port 0 is allowed and it will automatically translate to binding
 * on the related port range.


> +	 */
> +	__u64 port;
> +};
> +
>  /**
>   * DOC: fs_access
>   *
> @@ -189,4 +226,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.

You can add:
"This is supported since ABI 4."

> + *
> + * 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 */
> diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
> index c1e862a38410..c4bf0d5eff39 100644
> --- a/security/landlock/Kconfig
> +++ b/security/landlock/Kconfig
> @@ -3,6 +3,7 @@
>  config SECURITY_LANDLOCK
>  	bool "Landlock support"
>  	depends on SECURITY
> +	select SECURITY_NETWORK
>  	select SECURITY_PATH
>  	help
>  	  Landlock is a sandboxing mechanism that enables processes to restrict
> diff --git a/security/landlock/Makefile b/security/landlock/Makefile
> index 7bbd2f413b3e..53d3c92ae22e 100644
> --- a/security/landlock/Makefile
> +++ b/security/landlock/Makefile
> @@ -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
> \ No newline at end of file
> diff --git a/security/landlock/limits.h b/security/landlock/limits.h
> index bafb3b8dc677..93c9c6f91556 100644
> --- a/security/landlock/limits.h
> +++ b/security/landlock/limits.h
> @@ -23,6 +23,11 @@
>  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>  #define LANDLOCK_SHIFT_ACCESS_FS	0
> 
> +#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 */
> diff --git a/security/landlock/net.c b/security/landlock/net.c
> new file mode 100644
> index 000000000000..1bf26cf3c41b
> --- /dev/null
> +++ b/security/landlock/net.c
> @@ -0,0 +1,198 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Landlock LSM - Network management and hooks
> + *
> + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
> + * Copyright © 2022-2023 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,

To be consistent with current_check_access_path(), please rename to
current_check_access_socket().

> +			       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();

For consistency with other functions, s/domain/dom/g

> +
> +	if (!domain)
> +		return 0;
> +	if (WARN_ON_ONCE(domain->num_layers < 1))
> +		return -EACCES;
> +
> +	/* Checks if it's a (potential) TCP socket. */
> +	if (sock->type != SOCK_STREAM)
> +		return 0;
> +
> +	/* Checks for minimal header length to safely read sa_family. */
> +	if (addrlen < offsetofend(typeof(*address), sa_family))
> +		return -EINVAL;
> +
> +	switch (address->sa_family) {
> +	case AF_UNSPEC:
> +	case AF_INET:
> +		if (addrlen < sizeof(struct sockaddr_in))
> +			return -EINVAL;
> +		port = ((struct sockaddr_in *)address)->sin_port;
> +		break;
> +#if IS_ENABLED(CONFIG_IPV6)
> +	case AF_INET6:
> +		if (addrlen < SIN6_LEN_RFC2133)
> +			return -EINVAL;
> +		port = ((struct sockaddr_in6 *)address)->sin6_port;
> +		break;
> +#endif

#endif /* IS_ENABLED(CONFIG_INET) */

> +	default:
> +		return 0;
> +	}
> +
> +	/* Specific AF_UNSPEC handling. */
> +	if (address->sa_family == AF_UNSPEC) {
> +		/*
> +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
> +		 * association, which have the same effect as closing the
> +		 * 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;
> +
> +		/*
> +		 * For compatibility reason, accept AF_UNSPEC for bind
> +		 * accesses (mapped to AF_INET) only if the address is
> +		 * 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;
> +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
> +
> +	rule = landlock_find_rule(domain, id);
> +	handled_access = landlock_init_layer_masks(
> +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
> +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
> +				   ARRAY_SIZE(layer_masks)))
> +		return 0;
> +
> +	return -EACCES;
> +}
> +
> +static int hook_socket_bind(struct socket *const sock,
> +			    struct sockaddr *const address, const int addrlen)
> +{
> +	return check_socket_access(sock, address, addrlen,
> +				   LANDLOCK_ACCESS_NET_BIND_TCP);
> +}
> +
> +static int hook_socket_connect(struct socket *const sock,
> +			       struct sockaddr *const address,
> +			       const int addrlen)
> +{
> +	return check_socket_access(sock, address, addrlen,
> +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
> +}
> +
> +static struct security_hook_list landlock_hooks[] __ro_after_init = {
> +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
> +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
> +};
> +
> +__init void landlock_add_net_hooks(void)
> +{
> +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
> +			   LANDLOCK_NAME);
> +}
> diff --git a/security/landlock/net.h b/security/landlock/net.h
> new file mode 100644
> index 000000000000..588a49fd6907
> --- /dev/null
> +++ b/security/landlock/net.h
> @@ -0,0 +1,33 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Landlock LSM - Network management and hooks
> + *
> + * Copyright © 2022-2023 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)
> +{
> +}
> +
> +static inline int
> +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
> +			 access_mask_t access_rights);
> +{
> +	return -EAFNOSUPPORT;
> +}
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
> +#endif /* _SECURITY_LANDLOCK_NET_H */
> diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> index 4c209acee01e..1fe4298ff4a7 100644
> --- a/security/landlock/ruleset.c
> +++ b/security/landlock/ruleset.c
> @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>  	refcount_set(&new_ruleset->usage, 1);
>  	mutex_init(&new_ruleset->lock);
>  	new_ruleset->root_inode = RB_ROOT;
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	new_ruleset->root_net_port = RB_ROOT;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	new_ruleset->num_layers = num_layers;
>  	/*
>  	 * hierarchy = NULL
> @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>  }
> 
>  struct landlock_ruleset *
> -landlock_create_ruleset(const access_mask_t fs_access_mask)
> +landlock_create_ruleset(const access_mask_t fs_access_mask,
> +			const access_mask_t net_access_mask)
>  {
>  	struct landlock_ruleset *new_ruleset;
> 
>  	/* Informs about useless ruleset. */
> -	if (!fs_access_mask)
> +	if (!fs_access_mask && !net_access_mask)
>  		return ERR_PTR(-ENOMSG);
>  	new_ruleset = create_ruleset(1);
> -	if (!IS_ERR(new_ruleset))
> +	if (IS_ERR(new_ruleset))
> +		return new_ruleset;
> +	if (fs_access_mask)
>  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> +	if (net_access_mask)
> +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);

This is good, but it is not tested: we need to add a test that both
handle FS and net restrictions. You can add one in net.c, just handling
LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
rule with path_beneath (e.g. /dev) and another with net_port, and check
that open("/") is denied, open("/dev") is allowed, and and only the
allowed port is allowed with bind(). This test should be simple and can
only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
after port_endianness. fcntl.h should then be included by net.c

I guess that was the purpose of layout1.with_net (in fs_test.c) but it
is not complete. You can revamp this test and move it to net.c
following the above suggestions, keeping it consistent with other tests
in net.c . You don't need the test_open() nor create_ruleset() helpers.

This test must failed if we change "ruleset->access_masks[layer_level] |="
to "ruleset->access_masks[layer_level] =" in
landlock_add_fs_access_mask() or landlock_add_net_access_mask().

>  	return new_ruleset;
>  }
> 
> @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>  	case LANDLOCK_KEY_INODE:
>  		return true;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		return false;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
>  		WARN_ON_ONCE(1);
>  		return false;
> @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>  	case LANDLOCK_KEY_INODE:
>  		return &ruleset->root_inode;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		return &ruleset->root_net_port;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
> +		WARN_ON_ONCE(1);

Please move this WARN to the patch that added the previous and next
lines.

>  		return ERR_PTR(-EINVAL);
>  	}
>  }
> @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>  	BUILD_BUG_ON(access_masks <
> -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
> +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
> +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>  }
> 
>  /**
> @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>  	if (err)
>  		goto out_unlock;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	/* Merges the @src network port tree. */
> +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
> +	if (err)
> +		goto out_unlock;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  out_unlock:
>  	mutex_unlock(&src->lock);
>  	mutex_unlock(&dst->lock);
> @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>  	if (err)
>  		goto out_unlock;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	/* Copies the @parent network port tree. */
> +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
> +	if (err)
> +		goto out_unlock;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>  		err = -EINVAL;
>  		goto out_unlock;
> @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>  					     node)
>  		free_rule(freeme, LANDLOCK_KEY_INODE);
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	rbtree_postorder_for_each_entry_safe(freeme, next,
> +					     &ruleset->root_net_port, node)
> +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	put_hierarchy(ruleset->hierarchy);
>  	kfree(ruleset);
>  }
> @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>   *
>   * @domain: The domain that defines the current restrictions.
>   * @access_request: The requested access rights to check.
> - * @layer_masks: The layer masks to populate.
> + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET

"%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"

> + * elements according to @key_type.
>   * @key_type: The key type to switch between access masks of different types.
>   *
>   * Returns: An access mask where each access right bit is set which is handled
> @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>  		get_access_mask = landlock_get_fs_access_mask;
>  		num_access = LANDLOCK_NUM_ACCESS_FS;
>  		break;
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		get_access_mask = landlock_get_net_access_mask;
> +		num_access = LANDLOCK_NUM_ACCESS_NET;
> +		break;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
>  		WARN_ON_ONCE(1);
>  		return 0;
> diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
> index 1ede2b9a79b7..ba4a06035599 100644
> --- a/security/landlock/ruleset.h
> +++ b/security/landlock/ruleset.h
> @@ -33,13 +33,16 @@
>  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);
> +/* Makes sure all network access rights can be stored. */
> +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>  /* 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;
> +typedef u32 access_masks_t;
>  /* Makes sure all ruleset access rights can be stored. */
> -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
> +static_assert(BITS_PER_TYPE(access_masks_t) >=
> +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
> 
>  typedef u16 layer_mask_t;
>  /* Makes sure all layers can be checked. */
> @@ -84,6 +87,11 @@ enum landlock_key_type {
>  	 * keys.
>  	 */
>  	LANDLOCK_KEY_INODE = 1,
> +	/**
> +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
> +	 * node keys.
> +	 */
> +	LANDLOCK_KEY_NET_PORT,
>  };
> 
>  /**
> @@ -158,6 +166,13 @@ struct landlock_ruleset {
>  	 * reaches zero.
>  	 */
>  	struct rb_root root_inode;

#if IS_ENABLED(CONFIG_INET)

> +	/**
> +	 * @root_net_port: Root of a red-black tree containing &struct
> +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
> +	 * process (i.e. as a domain), this tree is immutable until @usage
> +	 * reaches zero.
> +	 */
> +	struct rb_root root_net_port;

#endif /* IS_ENABLED(CONFIG_INET) */

>  	/**
>  	 * @hierarchy: Enables hierarchy identification even when a parent
>  	 * domain vanishes.  This is needed for the ptrace protection.
> @@ -196,13 +211,13 @@ struct landlock_ruleset {
>  			 */
>  			u32 num_layers;
>  			/**
> -			 * @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
> -			 * one.  These layers are used when merging rulesets,
> -			 * for user space backward compatibility (i.e.
> -			 * future-proof), and to properly handle merged
> +			 * @access_masks: Contains the subset of filesystem and
> +			 * network actions that are restricted by a ruleset.
> +			 * A domain saves all layers of merged rulesets in a
> +			 * stack (FAM), starting from the first layer to the
> +			 * last one.  These layers are used when merging
> +			 * rulesets, for user space backward compatibility
> +			 * (i.e. future-proof), and to properly handle merged
>  			 * rulesets without overlapping access rights.  These
>  			 * layers are set once and never changed for the
>  			 * lifetime of the ruleset.
> @@ -213,7 +228,8 @@ struct landlock_ruleset {
>  };
> 
>  struct landlock_ruleset *
> -landlock_create_ruleset(const access_mask_t access_mask);
> +landlock_create_ruleset(const access_mask_t access_mask_fs,
> +			const access_mask_t access_mask_net);
> 
>  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
> @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>  }
> 
> +static inline void
> +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
> +			     const access_mask_t net_access_mask,
> +			     const u16 layer_level)
> +{
> +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
> +
> +	/* Should already be checked in sys_landlock_create_ruleset(). */
> +	WARN_ON_ONCE(net_access_mask != net_mask);
> +	ruleset->access_masks[layer_level] |=
> +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
> +}
> +
>  static inline access_mask_t
>  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>  				const u16 layer_level)
> @@ -266,6 +295,16 @@ 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;
>  }
> +
> +static inline access_mask_t
> +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
> +			     const u16 layer_level)
> +{
> +	return (ruleset->access_masks[layer_level] >>
> +		LANDLOCK_SHIFT_ACCESS_NET) &
> +	       LANDLOCK_MASK_ACCESS_NET;
> +}
> +
>  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>  			    const access_mask_t access_request,
>  			    layer_mask_t (*const layer_masks)[],
> diff --git a/security/landlock/setup.c b/security/landlock/setup.c
> index 0f6113528fa4..df81612811bf 100644
> --- a/security/landlock/setup.c
> +++ b/security/landlock/setup.c
> @@ -14,6 +14,7 @@
>  #include "fs.h"
>  #include "ptrace.h"
>  #include "setup.h"
> +#include "net.h"
> 
>  bool landlock_initialized __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;
> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
> index 8a54e87dbb17..3ad652d9a146 100644
> --- a/security/landlock/syscalls.c
> +++ b/security/landlock/syscalls.c
> @@ -29,6 +29,7 @@
>  #include "cred.h"
>  #include "fs.h"
>  #include "limits.h"
> +#include "net.h"
>  #include "ruleset.h"
>  #include "setup.h"
> 
> @@ -74,7 +75,8 @@ static void build_check_abi(void)
>  {
>  	struct landlock_ruleset_attr ruleset_attr;
>  	struct landlock_path_beneath_attr path_beneath_attr;
> -	size_t ruleset_size, path_beneath_size;
> +	struct landlock_net_port_attr net_port_attr;
> +	size_t ruleset_size, path_beneath_size, net_port_size;
> 
>  	/*
>  	 * For each user space ABI structures, first checks that there is no
> @@ -82,13 +84,19 @@ static void build_check_abi(void)
>  	 * struct size.
>  	 */
>  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
> +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
> -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
> +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
> 
>  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
> +
> +	net_port_size = sizeof(net_port_attr.allowed_access);
> +	net_port_size += sizeof(net_port_attr.port);
> +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
> +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>  }
> 
>  /* Ruleset handling */
> @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>  	.write = fop_dummy_write,
>  };
> 
> -#define LANDLOCK_ABI_VERSION 3
> +#define LANDLOCK_ABI_VERSION 4
> 
>  /**
>   * sys_landlock_create_ruleset - Create a new ruleset
> @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>  	    LANDLOCK_MASK_ACCESS_FS)
>  		return -EINVAL;
> 
> +	/* Checks network content (and 32-bits cast). */
> +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
> +	    LANDLOCK_MASK_ACCESS_NET)
> +		return -EINVAL;
> +
>  	/* Checks arguments and transforms to kernel struct. */
> -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
> +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
> +					  ruleset_attr.handled_access_net);
>  	if (IS_ERR(ruleset))
>  		return PTR_ERR(ruleset);
> 
> @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>  	int res, err;
>  	access_mask_t mask;
> 
> -	/* Copies raw user space buffer, only one type for now. */
> +	/* Copies raw user space buffer. */

Shouldn't this be part of a previous patch?

>  	res = copy_from_user(&path_beneath_attr, rule_attr,
>  			     sizeof(path_beneath_attr));
>  	if (res)
> @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>  	return err;
>  }
> 
> +static int add_rule_net_port(struct landlock_ruleset *ruleset,
> +			     const void __user *const rule_attr)
> +{
> +	struct landlock_net_port_attr net_port_attr;
> +	int res;
> +	access_mask_t mask;
> +
> +	/* Copies raw user space buffer. */
> +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
> +	if (res)
> +		return -EFAULT;
> +
> +	/*
> +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
> +	 * are ignored by network actions.
> +	 */
> +	if (!net_port_attr.allowed_access)
> +		return -ENOMSG;
> +
> +	/*
> +	 * Checks that allowed_access matches the @ruleset constraints
> +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
> +	 */
> +	mask = landlock_get_net_access_mask(ruleset, 0);
> +	if ((net_port_attr.allowed_access | mask) != mask)
> +		return -EINVAL;
> +
> +	/* Denies inserting a rule with port higher than 65535. */

For consistency with the following comment:
"Denies inserting a rule with port greater than 65535."


> +	if (net_port_attr.port > U16_MAX)
> +		return -EINVAL;
> +
> +	/* Imports the new rule. */
> +	return landlock_append_net_rule(ruleset, net_port_attr.port,
> +					net_port_attr.allowed_access);
> +}
> +
>  /**
>   * sys_landlock_add_rule - Add a new rule to a ruleset
>   *
>   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
>   *		with the new rule.
> - * @rule_type: Identify the structure type pointed to by @rule_attr (only
> - *             %LANDLOCK_RULE_PATH_BENEATH for now).
> + * @rule_type: Identify the structure type pointed to by @rule_attr:
> + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
>   * @rule_attr: Pointer to a rule (only of type &struct
>   *             landlock_path_beneath_attr for now).
>   * @flags: Must be 0.
> @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>   * Possible returned errors are:
>   *
>   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
> + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not

%LANDLOCK_RULE_NET_PORT

> + *   supported by the running kernel;
>   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
>   *   &landlock_path_beneath_attr.allowed_access is not a subset of the

&landlock_path_beneath_attr.allowed_access or
&landlock_net_port_attr.allowed_access is not a subset of the

>   *   ruleset handled accesses);

EINVAL description needs to be updated, especially for port > U16_MAX:
- *   ruleset handled accesses);
+ *   ruleset handled accesses), or &landlock_net_port_attr.port is
+     greater than 65535;


> @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
>  	case LANDLOCK_RULE_PATH_BENEATH:
>  		err = add_rule_path_beneath(ruleset, rule_attr);
>  		break;
> +	case LANDLOCK_RULE_NET_PORT:
> +		err = add_rule_net_port(ruleset, rule_attr);
> +		break;
>  	default:
>  		err = -EINVAL;
>  		break;
> diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
> index 792c3f0a59b4..646f778dfb1e 100644
> --- a/tools/testing/selftests/landlock/base_test.c
> +++ b/tools/testing/selftests/landlock/base_test.c
> @@ -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
> 

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-16  1:50 ` [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network Konstantin Meskhidze
@ 2023-10-18 12:32   ` Mickaël Salaün
  2023-10-20 11:41     ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:32 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

You can update the subject with:
"selftests/landlock: Add network tests"

On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze wrote:
> These test suites try to check edge cases for TCP sockets
> bind() and connect() actions.

You can replace with that:
Add 77 test suites to check edge cases related to bind() and connect()
actions. They are defined with 6 fixtures and their variants:

> 
> protocol:
> * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.

As you already did, you can write one paragraph per fixture, but
starting by explaining the fixture and its related variants, and then
listing the tests and explaining their specificities. For instance:

The "protocol" fixture is extended with 12 variants defined as a matrix
of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
stream/datagram socket. 4 related tests suites are defined:
* bind: Test bind combinations with increasingly more
  restricting domains.
* connect: Test connect combinations with increasingly more
  restricting domains.
...

s/ipv/IPv/g

> * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and unix
> sockets.
> * bind_unspec: Tests with non-landlocked/landlocked restrictions
> for bind action with AF_UNSPEC socket family.
> * connect_unspec: Tests with non-landlocked/landlocked restrictions
> for connect action with AF_UNSPEC socket family.
> 
> ipv4:
> * from_unix_to_inet: Tests to make sure unix sockets' actions are not
> restricted by Landlock rules applied to TCP ones.
> 
> tcp_layers:
> * ruleset_overlap.
> * ruleset_expand.
> 
> mini:
> * network_access_rights: Tests with  legitimate access values.
> * unknown_access_rights: Tests with invalid attributes, out of access range.
> * inval:
>     - unhandled allowed access.
>     - zero access value.
> * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
> 
> ipv4_tcp:
> * port_endianness: Tests with big/little endian port formats.
> 
> port_specific:
> * bind_connect: Tests with specific port values.
> 
> layout1:
> * with_net: Tests with network bind() socket action within
> filesystem directory access test.
> 
> Test coverage for security/landlock is 94.5% of 932 lines according
> to gcc/gcov-11.
> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
> Co-developed-by:: Mickaël Salaün <mic@digikod.net>
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * Renames port_zero to port_specific fixture.
> * Refactors port_specific test:
>     - Adds set_port() and get_binded_port() helpers.
>     - Adds checks for port 0, allowed by Landlock in this version.
>     - Adds checks for port 1023.
> * Refactors commit message.
> 

> +static void set_port(struct service_fixture *const srv, in_port_t port)
> +{
> +	switch (srv->protocol.domain) {
> +	case AF_UNSPEC:
> +	case AF_INET:
> +		srv->ipv4_addr.sin_port = port;

We should call htons() here, and make port a uint16_t.

> +		return;
> +
> +	case AF_INET6:
> +		srv->ipv6_addr.sin6_port = port;
> +		return;
> +
> +	default:
> +		return;
> +	}
> +}
> +
> +static in_port_t get_binded_port(int socket_fd,

The returned type should be uint16_t (i.e. host endianess).

> +				 const struct protocol_variant *const prot)
> +{
> +	struct sockaddr_in ipv4_addr;
> +	struct sockaddr_in6 ipv6_addr;
> +	socklen_t ipv4_addr_len, ipv6_addr_len;
> +
> +	/* Gets binded port. */
> +	switch (prot->domain) {
> +	case AF_UNSPEC:
> +	case AF_INET:
> +		ipv4_addr_len = sizeof(ipv4_addr);
> +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
> +		return ntohs(ipv4_addr.sin_port);
> +
> +	case AF_INET6:
> +		ipv6_addr_len = sizeof(ipv6_addr);
> +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
> +		return ntohs(ipv6_addr.sin6_port);
> +
> +	default:
> +		return 0;
> +	}
> +}

These are good helpers!


> +FIXTURE_TEARDOWN(ipv4)
> +{
> +}
> +
> +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp

No FIXME should remain.

> +TEST_F(ipv4, from_unix_to_inet)

> +TEST_F(mini, network_access_rights)
> +{
> +	const struct landlock_ruleset_attr ruleset_attr = {
> +		.handled_access_net = ACCESS_ALL,
> +	};
> +	struct landlock_net_port_attr net_service = {

Please rename to "net_port" everywhere.

> +TEST_F(port_specific, bind_connect)
> +{
> +	int socket_fd, ret;
> +
> +	/* Adds the first rule layer with bind and connect actions. */
> +	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_port_attr tcp_bind_connect_zero = {
> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
> +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
> +			.port = htons(0),

We don't need any htons() calls anymore. It doesn't change the 0 value
in this case but this is not correct.

> +		};
> +

Useless new line.

> +		int ruleset_fd;
> +
> +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
> +						     sizeof(ruleset_attr), 0);
> +		ASSERT_LE(0, ruleset_fd);
> +
> +		/* Checks zero port value on bind and connect actions. */
> +		EXPECT_EQ(0,
> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> +					    &tcp_bind_connect_zero, 0));
> +
> +		enforce_ruleset(_metadata, ruleset_fd);
> +		EXPECT_EQ(0, close(ruleset_fd));
> +	}
> +
> +	socket_fd = socket_variant(&self->srv0);
> +	ASSERT_LE(0, socket_fd);
> +
> +	/* Sets address port to 0 for both protocol families. */
> +	set_port(&self->srv0, htons(0));

ditto

> +
> +	/* Binds on port 0. */
> +	ret = bind_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);
> +	} else {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);

If the results are the same, no need to add an if block.

> +	}
> +
> +	/* Connects on port 0. */
> +	ret = connect_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		EXPECT_EQ(-ECONNREFUSED, ret);
> +	} else {
> +		EXPECT_EQ(-ECONNREFUSED, ret);
> +	}

ditto

> +
> +	/* Binds on port 0. */

Please close sockets once they are used, and recreate one for another
bind/connect to avoid wrong checks.

> +	ret = bind_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);
> +	} else {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);
> +	}

Why this second bind() block? Furthermore, it is using the same
socket_fd.

> +
> +	/* Sets binded port for both protocol families. */
> +	set_port(&self->srv0,
> +		 htons(get_binded_port(socket_fd, &variant->prot)));

Ditto, these two endianess translations are useless.

You can also add this to make sure the returned port is not 0:
port = get_binded_port(socket_fd, &variant->prot);
EXPECT_NE(0, port);
set_port(&self->srv0, port);

> +
> +	/* Connects on the binded port. */
> +	ret = connect_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		/* Denied by Landlock. */
> +		EXPECT_EQ(-EACCES, ret);
> +	} else {
> +		EXPECT_EQ(0, ret);
> +	}
> +
> +	EXPECT_EQ(0, close(socket_fd));
> +



> +	/* Adds the second rule layer with just bind action. */

There is not only bind actions here.

This second part of the tests should be in a dedicated
TEST_F(port_specific, bind_1023).

> +	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_port_attr tcp_bind_zero = {
> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> +			.port = htons(0),
> +		};
> +

Useless new lines.

> +		/* A rule with port value less than 1024. */
> +		const struct landlock_net_port_attr tcp_bind_lower_range = {
> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> +			.port = htons(1023),
> +		};
> +

Useless new line.

> +		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_PORT,
> +					    &tcp_bind_lower_range, 0));
> +		ASSERT_EQ(0,
> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> +					    &tcp_bind_zero, 0));
> +
> +		enforce_ruleset(_metadata, ruleset_fd);
> +		EXPECT_EQ(0, close(ruleset_fd));
> +	}
> +
> +	socket_fd = socket_variant(&self->srv0);

We must have one socket FD dedicated to bind an another dedicated to
connect, e.g. bind_fd and connect_fd, an close them after each use,
otherwise tests might be inconsistent.

> +	ASSERT_LE(0, socket_fd);
> +
> +	/* Sets address port to 1023 for both protocol families. */
> +	set_port(&self->srv0, htons(1023));
> +
> +	/* Binds on port 1023. */
> +	ret = bind_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {

No need to add this check if the result is the same for sandboxed and
not sandboxed tests.

Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and clear_cap()
around this bind_variant() to make this test useful.

You will also need to patch common.h like this:
@@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
        cap_t cap_p;
        /* Only these three capabilities are useful for the tests. */
        const cap_value_t caps[] = {
+               /* clang-format off */
                CAP_DAC_OVERRIDE,
                CAP_MKNOD,
                CAP_SYS_ADMIN,
                CAP_SYS_CHROOT,
+               CAP_NET_BIND_SERVICE,
+               /* clang-format on */
        };

> +		/* Denied by the system. */
> +		EXPECT_EQ(-EACCES, ret);
> +	} else {
> +		/* Denied by the system. */
> +		EXPECT_EQ(-EACCES, ret);
> +	}
> +

I don't see why the following part is useful. Why did you add it?
Why tcp_bind_zero?

The other parts are good though!

> +	/* Sets address port to 0 for both protocol families. */
> +	set_port(&self->srv0, htons(0));
> +
> +	/* Binds on port 0. */
> +	ret = bind_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);
> +	} else {
> +		/* Binds to a random port within ip_local_port_range. */
> +		EXPECT_EQ(0, ret);
> +	}
> +
> +	/* Sets binded port for both protocol families. */
> +	set_port(&self->srv0,
> +		 htons(get_binded_port(socket_fd, &variant->prot)));
> +
> +	/* Connects on the binded port. */
> +	ret = connect_variant(socket_fd, &self->srv0);
> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> +		/* Denied by Landlock. */
> +		EXPECT_EQ(-EACCES, ret);
> +	} else {
> +		EXPECT_EQ(0, ret);
> +	}
> +
> +	EXPECT_EQ(0, close(socket_fd));
> +}
> +
> +TEST_HARNESS_MAIN
> --
> 2.25.1
> 

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 11/12] samples/landlock: Add network demo
  2023-10-16  1:50 ` [PATCH v13 11/12] samples/landlock: Add network demo Konstantin Meskhidze
@ 2023-10-18 12:33   ` Mickaël Salaün
  2023-10-20 11:59     ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:33 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

Please update the subject to "samples/landlock: Support TCP restrictions"

On Mon, Oct 16, 2023 at 09:50:29AM +0800, Konstantin Meskhidze wrote:
> 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 <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-12-konstantin.meskhidze@huawei.com
> [mic: Define __SANE_USERSPACE_TYPES__ to select int-ll64.h and avoid
> format warnings for PowerPC]

You can remove all this kind of "[mic: ]" comments, I add them when I
merge a patch with additional changes.

> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * Defines __SANE_USERSPACE_TYPES__ to avoid warnings for PowerPC.

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 12/12] landlock: Document Landlock's network support
  2023-10-16  1:50 ` [PATCH v13 12/12] landlock: Document Landlock's network support Konstantin Meskhidze
@ 2023-10-18 12:34   ` Mickaël Salaün
  2023-10-20 12:17     ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 12:34 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:30AM +0800, Konstantin Meskhidze wrote:
> Describe network access rules for TCP sockets. Add network access
> example in the tutorial. Add kernel configuration support for network.
> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-13-konstantin.meskhidze@huawei.com
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * None.
> 
> Changes since v11:
> * Fixes documentaion as suggested in Günther's and Mickaёl's reviews:
> https://lore.kernel.org/netdev/3ad02c76-90d8-4723-e554-7f97ef115fc0@digikod.net/
> 
> Changes since v10:
> * Fixes documentaion as Mickaёl suggested:
> https://lore.kernel.org/linux-security-module/ec23be77-566e-c8fd-179e-f50e025ac2cf@digikod.net/
> 
> 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/9f354862-2bc3-39ea-92fd-53803d9bbc21@digikod.net/
> 
> Changes since v6:
> * Adds network support documentaion.
> 
> ---
>  Documentation/userspace-api/landlock.rst | 87 ++++++++++++++++++------
>  1 file changed, 66 insertions(+), 21 deletions(-)
> 
> diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst
> index f6a7da21708a..affadd9ac662 100644
> --- a/Documentation/userspace-api/landlock.rst
> +++ b/Documentation/userspace-api/landlock.rst
> @@ -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,34 @@ 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
> +A Landlock rule describes an action on an object which the process intends to
> +perform.  A set of rules is aggregated in a ruleset, which can then restrict
>  the thread enforcing it, and its future children.
> 
> +The two existing types of rules are:
> +
> +Filesystem rules
> +    For these rules, the object is a file hierarchy,
> +    and the related filesystem actions are defined with
> +    `filesystem access rights`.
> +
> +Network rules (since ABI v4)
> +    For these rules, the object is currently a TCP port,
> +    and the related actions are defined with `network access rights`.
> +
>  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.
> +We first need to define the ruleset that will contain our rules.
> +
> +For this example, the ruleset will contain rules that only allow filesystem
> +read actions and establish a specific TCP connection. Filesystem write
> +actions and other TCP actions will be denied.
> +
> +The ruleset then needs to handle both of these kind of actions.  This is

two spelling issues:
"needs to handle both these kinds of actions."

> +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 +76,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,9 +87,7 @@ 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 access rights which are only supported in higher versions of the ABI.
> 
>  .. code-block:: c
> 
> @@ -92,6 +107,11 @@ version of the ABI.
>      case 2:
>          /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
>          ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;

Like just before "case 2" this is missing:
__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);
>      }
> 
>  This enables to create an inclusive ruleset that will contain our rules.
> @@ -143,10 +163,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_port_attr net_port = {
> +        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
> +        .port = 443,
> +    };
> +
> +    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> +                            &net_port, 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 +388,7 @@ Access rights
>  -------------
> 
>  .. kernel-doc:: include/uapi/linux/landlock.h
> -    :identifiers: fs_access
> +    :identifiers: fs_access net_access
> 
>  Creating a new ruleset
>  ----------------------
> @@ -374,6 +407,7 @@ Extending a ruleset
> 
>  .. kernel-doc:: include/uapi/linux/landlock.h
>      :identifiers: landlock_rule_type landlock_path_beneath_attr
> +                  landlock_net_service_attr

landlock_net_port_attr

> 
>  Enforcing a ruleset
>  -------------------
> @@ -451,6 +485,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.

bind and connect actions to only a set of allowed ports thanks to the new
``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
access rights.

> +
>  .. _kernel_support:
> 
>  Kernel support
> @@ -469,6 +509,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``).

LANDLOCK_ACCESS_NET_BIND_TCP

> +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
> 

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall
  2023-10-16  1:50 ` [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
  2023-10-18 12:28   ` Mickaël Salaün
@ 2023-10-18 16:34   ` Mickaël Salaün
  2023-10-19 11:57     ` Konstantin Meskhidze (A)
  1 sibling, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 16:34 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:25AM +0800, Konstantin Meskhidze wrote:
> Change 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.
> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-8-konstantin.meskhidze@huawei.com
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * None.
> 
> Changes since v11:
> * None.
> 
> 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(-)
> 
> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
> index d35cd5d304db..8a54e87dbb17 100644
> --- a/security/landlock/syscalls.c
> +++ b/security/landlock/syscalls.c
> @@ -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).
> +	 */

You now can replace this comment block with that:
+	/* Checks that allowed_access matches the @ruleset constraints. */

> +	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;
> +}

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-16  1:50 ` [PATCH v13 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
  2023-10-18 12:29   ` Mickaël Salaün
@ 2023-10-18 16:34   ` Mickaël Salaün
  2023-10-20  9:40     ` Konstantin Meskhidze (A)
  1 sibling, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-18 16:34 UTC (permalink / raw)
  To: Konstantin Meskhidze
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> This 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.
> For the file system, a file descriptor is a direct access to a file/data.
> But for the network, it's impossible to identify for which data/peer a
> newly created socket will give access to, it's needed to wait for a
> connect or bind request to identify the use case for this socket.
> That's why the access rights (related to ports) are tied to an opened
> socket, but this would not align with the way Landlock access control
> works for the filesystem [2].
> 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 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).
> 
> [1]
> https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
> [2]
> https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
> 
> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
> [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
> to add_rule_net_service()]
> Signed-off-by: Mickaël Salaün <mic@digikod.net>
> ---
> 
> Changes since v12:
> * Moves add_rule_net_port() back in syscalls.c and makes it static.
> * Deletes bind_access_mask allowing bind action rule on port 0.
> * Adds comment about port 0 in landlock_net_port_attr structure.
> * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
> * Minor fixes.
> * Refactors commit message.
> 
> Changes since v11:
> * Replaces dates with "2022-2023" in net.c/h files headers.
> * Removes WARN_ON_ONCE(!domain) in check_socket_access().
> * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
> * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
> * Renames landlock_net_service_attr to landlock_net_port_attr.
> * Defines two add_rule_net_service() functions according to
>   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>   function.
> * Adds af_family consistency check while handling AF_UNSPEC specifically.
> * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>   action on port zero.
> * Minor fixes.
> * Refactors commit message.
> 
> 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() and
>   get_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                |  56 ++++++
>  security/landlock/Kconfig                    |   1 +
>  security/landlock/Makefile                   |   2 +
>  security/landlock/limits.h                   |   5 +
>  security/landlock/net.c                      | 198 +++++++++++++++++++
>  security/landlock/net.h                      |  33 ++++
>  security/landlock/ruleset.c                  |  62 +++++-
>  security/landlock/ruleset.h                  |  59 +++++-
>  security/landlock/setup.c                    |   2 +
>  security/landlock/syscalls.c                 |  69 ++++++-
>  tools/testing/selftests/landlock/base_test.c |   2 +-
>  11 files changed, 466 insertions(+), 23 deletions(-)
>  create mode 100644 security/landlock/net.c
>  create mode 100644 security/landlock/net.h
> 
> diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
> index 81d09ef9aa50..25349666b19e 100644
> --- a/include/uapi/linux/landlock.h
> +++ b/include/uapi/linux/landlock.h
> @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>  	 * landlock_path_beneath_attr .
>  	 */
>  	LANDLOCK_RULE_PATH_BENEATH = 1,
> +	/**
> +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
> +	 * landlock_net_port_attr .
> +	 */
> +	LANDLOCK_RULE_NET_PORT = 2,
>  };
> 
>  /**
> @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>  	 */
>  } __attribute__((packed));
> 
> +/**
> + * struct landlock_net_port_attr - Network port definition
> + *
> + * Argument of sys_landlock_add_rule().
> + */
> +struct landlock_net_port_attr {
> +	/**
> +	 * @allowed_access: Bitmask of allowed access network for a port
> +	 * (cf. `Network flags`_).
> +	 */
> +	__u64 allowed_access;
> +	/**
> +	 * @port: Network port. Landlock does not forbid rules with port 0,
> +	 * since some network services use it. Port 0 is a reserved one in
> +	 * TCP/IP networking, meaning that it should not be used in TCP or
> +	 * UDP messages. To allocate its source port number, services call
> +	 * TCP/IP network functions like bind() to request one. With port 0
> +	 * it triggers the operating system to automatically search for
> +	 * and return a suitable available port in the TCP/IP dynamic
> +	 * port number range. This port range can be controlled by a
> +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
> +	 * which is also used by IPv6.
> +	 */
> +	__u64 port;
> +};
> +
>  /**
>   * DOC: fs_access
>   *
> @@ -189,4 +226,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 */
> diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
> index c1e862a38410..c4bf0d5eff39 100644
> --- a/security/landlock/Kconfig
> +++ b/security/landlock/Kconfig
> @@ -3,6 +3,7 @@
>  config SECURITY_LANDLOCK
>  	bool "Landlock support"
>  	depends on SECURITY
> +	select SECURITY_NETWORK
>  	select SECURITY_PATH
>  	help
>  	  Landlock is a sandboxing mechanism that enables processes to restrict
> diff --git a/security/landlock/Makefile b/security/landlock/Makefile
> index 7bbd2f413b3e..53d3c92ae22e 100644
> --- a/security/landlock/Makefile
> +++ b/security/landlock/Makefile
> @@ -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
> \ No newline at end of file
> diff --git a/security/landlock/limits.h b/security/landlock/limits.h
> index bafb3b8dc677..93c9c6f91556 100644
> --- a/security/landlock/limits.h
> +++ b/security/landlock/limits.h
> @@ -23,6 +23,11 @@
>  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>  #define LANDLOCK_SHIFT_ACCESS_FS	0
> 
> +#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 */
> diff --git a/security/landlock/net.c b/security/landlock/net.c
> new file mode 100644
> index 000000000000..1bf26cf3c41b
> --- /dev/null
> +++ b/security/landlock/net.c
> @@ -0,0 +1,198 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Landlock LSM - Network management and hooks
> + *
> + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
> + * Copyright © 2022-2023 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 = {
> +		.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;
> +
> +	/* Checks if it's a (potential) TCP socket. */
> +	if (sock->type != SOCK_STREAM)
> +		return 0;
> +
> +	/* Checks for minimal header length to safely read sa_family. */
> +	if (addrlen < offsetofend(typeof(*address), sa_family))
> +		return -EINVAL;
> +
> +	switch (address->sa_family) {
> +	case AF_UNSPEC:
> +	case AF_INET:
> +		if (addrlen < sizeof(struct sockaddr_in))
> +			return -EINVAL;
> +		port = ((struct sockaddr_in *)address)->sin_port;
> +		break;
> +#if IS_ENABLED(CONFIG_IPV6)
> +	case AF_INET6:
> +		if (addrlen < SIN6_LEN_RFC2133)
> +			return -EINVAL;
> +		port = ((struct sockaddr_in6 *)address)->sin6_port;
> +		break;
> +#endif
> +	default:
> +		return 0;
> +	}
> +
> +	/* Specific AF_UNSPEC handling. */
> +	if (address->sa_family == AF_UNSPEC) {
> +		/*
> +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
> +		 * association, which have the same effect as closing the
> +		 * 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;
> +
> +		/*
> +		 * For compatibility reason, accept AF_UNSPEC for bind
> +		 * accesses (mapped to AF_INET) only if the address is
> +		 * 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;
> +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
> +
> +	rule = landlock_find_rule(domain, id);
> +	handled_access = landlock_init_layer_masks(
> +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
> +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
> +				   ARRAY_SIZE(layer_masks)))
> +		return 0;
> +
> +	return -EACCES;
> +}
> +
> +static int hook_socket_bind(struct socket *const sock,
> +			    struct sockaddr *const address, const int addrlen)
> +{
> +	return check_socket_access(sock, address, addrlen,
> +				   LANDLOCK_ACCESS_NET_BIND_TCP);
> +}
> +
> +static int hook_socket_connect(struct socket *const sock,
> +			       struct sockaddr *const address,
> +			       const int addrlen)
> +{
> +	return check_socket_access(sock, address, addrlen,
> +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
> +}
> +
> +static struct security_hook_list landlock_hooks[] __ro_after_init = {
> +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
> +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
> +};
> +
> +__init void landlock_add_net_hooks(void)
> +{
> +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
> +			   LANDLOCK_NAME);
> +}
> diff --git a/security/landlock/net.h b/security/landlock/net.h
> new file mode 100644
> index 000000000000..588a49fd6907
> --- /dev/null
> +++ b/security/landlock/net.h
> @@ -0,0 +1,33 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Landlock LSM - Network management and hooks
> + *
> + * Copyright © 2022-2023 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)
> +{
> +}
> +
> +static inline int
> +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
> +			 access_mask_t access_rights);
> +{
> +	return -EAFNOSUPPORT;
> +}
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
> +#endif /* _SECURITY_LANDLOCK_NET_H */
> diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> index 4c209acee01e..1fe4298ff4a7 100644
> --- a/security/landlock/ruleset.c
> +++ b/security/landlock/ruleset.c
> @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>  	refcount_set(&new_ruleset->usage, 1);
>  	mutex_init(&new_ruleset->lock);
>  	new_ruleset->root_inode = RB_ROOT;
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	new_ruleset->root_net_port = RB_ROOT;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	new_ruleset->num_layers = num_layers;
>  	/*
>  	 * hierarchy = NULL
> @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>  }
> 
>  struct landlock_ruleset *
> -landlock_create_ruleset(const access_mask_t fs_access_mask)
> +landlock_create_ruleset(const access_mask_t fs_access_mask,
> +			const access_mask_t net_access_mask)
>  {
>  	struct landlock_ruleset *new_ruleset;
> 
>  	/* Informs about useless ruleset. */
> -	if (!fs_access_mask)
> +	if (!fs_access_mask && !net_access_mask)
>  		return ERR_PTR(-ENOMSG);
>  	new_ruleset = create_ruleset(1);
> -	if (!IS_ERR(new_ruleset))
> +	if (IS_ERR(new_ruleset))
> +		return new_ruleset;
> +	if (fs_access_mask)
>  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> +	if (net_access_mask)
> +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>  	return new_ruleset;
>  }
> 
> @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>  	case LANDLOCK_KEY_INODE:
>  		return true;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		return false;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
>  		WARN_ON_ONCE(1);
>  		return false;
> @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>  	case LANDLOCK_KEY_INODE:
>  		return &ruleset->root_inode;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		return &ruleset->root_net_port;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
> +		WARN_ON_ONCE(1);
>  		return ERR_PTR(-EINVAL);
>  	}
>  }
> @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>  	BUILD_BUG_ON(access_masks <
> -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
> +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
> +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>  }
> 
>  /**
> @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>  	if (err)
>  		goto out_unlock;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	/* Merges the @src network port tree. */
> +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
> +	if (err)
> +		goto out_unlock;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  out_unlock:
>  	mutex_unlock(&src->lock);
>  	mutex_unlock(&dst->lock);
> @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>  	if (err)
>  		goto out_unlock;
> 
> +#if IS_ENABLED(CONFIG_INET)
> +	/* Copies the @parent network port tree. */
> +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
> +	if (err)
> +		goto out_unlock;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>  		err = -EINVAL;
>  		goto out_unlock;
> @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>  					     node)
>  		free_rule(freeme, LANDLOCK_KEY_INODE);
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	rbtree_postorder_for_each_entry_safe(freeme, next,
> +					     &ruleset->root_net_port, node)
> +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	put_hierarchy(ruleset->hierarchy);
>  	kfree(ruleset);
>  }
> @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>   *
>   * @domain: The domain that defines the current restrictions.
>   * @access_request: The requested access rights to check.
> - * @layer_masks: The layer masks to populate.
> + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
> + * elements according to @key_type.
>   * @key_type: The key type to switch between access masks of different types.
>   *
>   * Returns: An access mask where each access right bit is set which is handled
> @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>  		get_access_mask = landlock_get_fs_access_mask;
>  		num_access = LANDLOCK_NUM_ACCESS_FS;
>  		break;
> +
> +#if IS_ENABLED(CONFIG_INET)
> +	case LANDLOCK_KEY_NET_PORT:
> +		get_access_mask = landlock_get_net_access_mask;
> +		num_access = LANDLOCK_NUM_ACCESS_NET;
> +		break;
> +#endif /* IS_ENABLED(CONFIG_INET) */
> +
>  	default:
>  		WARN_ON_ONCE(1);
>  		return 0;
> diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
> index 1ede2b9a79b7..ba4a06035599 100644
> --- a/security/landlock/ruleset.h
> +++ b/security/landlock/ruleset.h
> @@ -33,13 +33,16 @@
>  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);
> +/* Makes sure all network access rights can be stored. */
> +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>  /* 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;
> +typedef u32 access_masks_t;
>  /* Makes sure all ruleset access rights can be stored. */
> -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
> +static_assert(BITS_PER_TYPE(access_masks_t) >=
> +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
> 
>  typedef u16 layer_mask_t;
>  /* Makes sure all layers can be checked. */
> @@ -84,6 +87,11 @@ enum landlock_key_type {
>  	 * keys.
>  	 */
>  	LANDLOCK_KEY_INODE = 1,
> +	/**
> +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
> +	 * node keys.
> +	 */
> +	LANDLOCK_KEY_NET_PORT,
>  };
> 
>  /**
> @@ -158,6 +166,13 @@ struct landlock_ruleset {
>  	 * reaches zero.
>  	 */
>  	struct rb_root root_inode;
> +	/**
> +	 * @root_net_port: Root of a red-black tree containing &struct
> +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
> +	 * process (i.e. as a domain), this tree is immutable until @usage
> +	 * reaches zero.
> +	 */
> +	struct rb_root root_net_port;
>  	/**
>  	 * @hierarchy: Enables hierarchy identification even when a parent
>  	 * domain vanishes.  This is needed for the ptrace protection.
> @@ -196,13 +211,13 @@ struct landlock_ruleset {
>  			 */
>  			u32 num_layers;
>  			/**
> -			 * @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
> -			 * one.  These layers are used when merging rulesets,
> -			 * for user space backward compatibility (i.e.
> -			 * future-proof), and to properly handle merged
> +			 * @access_masks: Contains the subset of filesystem and
> +			 * network actions that are restricted by a ruleset.
> +			 * A domain saves all layers of merged rulesets in a
> +			 * stack (FAM), starting from the first layer to the
> +			 * last one.  These layers are used when merging
> +			 * rulesets, for user space backward compatibility
> +			 * (i.e. future-proof), and to properly handle merged
>  			 * rulesets without overlapping access rights.  These
>  			 * layers are set once and never changed for the
>  			 * lifetime of the ruleset.
> @@ -213,7 +228,8 @@ struct landlock_ruleset {
>  };
> 
>  struct landlock_ruleset *
> -landlock_create_ruleset(const access_mask_t access_mask);
> +landlock_create_ruleset(const access_mask_t access_mask_fs,
> +			const access_mask_t access_mask_net);
> 
>  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
> @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>  }
> 
> +static inline void
> +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
> +			     const access_mask_t net_access_mask,
> +			     const u16 layer_level)
> +{
> +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
> +
> +	/* Should already be checked in sys_landlock_create_ruleset(). */
> +	WARN_ON_ONCE(net_access_mask != net_mask);
> +	ruleset->access_masks[layer_level] |=
> +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
> +}
> +
>  static inline access_mask_t
>  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>  				const u16 layer_level)
> @@ -266,6 +295,16 @@ 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;
>  }
> +
> +static inline access_mask_t
> +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
> +			     const u16 layer_level)
> +{
> +	return (ruleset->access_masks[layer_level] >>
> +		LANDLOCK_SHIFT_ACCESS_NET) &
> +	       LANDLOCK_MASK_ACCESS_NET;
> +}
> +
>  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>  			    const access_mask_t access_request,
>  			    layer_mask_t (*const layer_masks)[],
> diff --git a/security/landlock/setup.c b/security/landlock/setup.c
> index 0f6113528fa4..df81612811bf 100644
> --- a/security/landlock/setup.c
> +++ b/security/landlock/setup.c
> @@ -14,6 +14,7 @@
>  #include "fs.h"
>  #include "ptrace.h"
>  #include "setup.h"
> +#include "net.h"
> 
>  bool landlock_initialized __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;
> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
> index 8a54e87dbb17..3ad652d9a146 100644
> --- a/security/landlock/syscalls.c
> +++ b/security/landlock/syscalls.c
> @@ -29,6 +29,7 @@
>  #include "cred.h"
>  #include "fs.h"
>  #include "limits.h"
> +#include "net.h"
>  #include "ruleset.h"
>  #include "setup.h"
> 
> @@ -74,7 +75,8 @@ static void build_check_abi(void)
>  {
>  	struct landlock_ruleset_attr ruleset_attr;
>  	struct landlock_path_beneath_attr path_beneath_attr;
> -	size_t ruleset_size, path_beneath_size;
> +	struct landlock_net_port_attr net_port_attr;
> +	size_t ruleset_size, path_beneath_size, net_port_size;
> 
>  	/*
>  	 * For each user space ABI structures, first checks that there is no
> @@ -82,13 +84,19 @@ static void build_check_abi(void)
>  	 * struct size.
>  	 */
>  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
> +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
> -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
> +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
> 
>  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
> +
> +	net_port_size = sizeof(net_port_attr.allowed_access);
> +	net_port_size += sizeof(net_port_attr.port);
> +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
> +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>  }
> 
>  /* Ruleset handling */
> @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>  	.write = fop_dummy_write,
>  };
> 
> -#define LANDLOCK_ABI_VERSION 3
> +#define LANDLOCK_ABI_VERSION 4
> 
>  /**
>   * sys_landlock_create_ruleset - Create a new ruleset
> @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>  	    LANDLOCK_MASK_ACCESS_FS)
>  		return -EINVAL;
> 
> +	/* Checks network content (and 32-bits cast). */
> +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
> +	    LANDLOCK_MASK_ACCESS_NET)
> +		return -EINVAL;
> +
>  	/* Checks arguments and transforms to kernel struct. */
> -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
> +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
> +					  ruleset_attr.handled_access_net);
>  	if (IS_ERR(ruleset))
>  		return PTR_ERR(ruleset);
> 
> @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>  	int res, err;
>  	access_mask_t mask;
> 
> -	/* Copies raw user space buffer, only one type for now. */
> +	/* Copies raw user space buffer. */
>  	res = copy_from_user(&path_beneath_attr, rule_attr,
>  			     sizeof(path_beneath_attr));
>  	if (res)
> @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>  	return err;
>  }
> 
> +static int add_rule_net_port(struct landlock_ruleset *ruleset,
> +			     const void __user *const rule_attr)
> +{
> +	struct landlock_net_port_attr net_port_attr;
> +	int res;
> +	access_mask_t mask;
> +
> +	/* Copies raw user space buffer. */
> +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
> +	if (res)
> +		return -EFAULT;
> +
> +	/*
> +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
> +	 * are ignored by network actions.
> +	 */
> +	if (!net_port_attr.allowed_access)
> +		return -ENOMSG;
> +
> +	/*
> +	 * Checks that allowed_access matches the @ruleset constraints
> +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
> +	 */

You can replace this comment block with that:
+	/* Checks that allowed_access matches the @ruleset constraints. */

> +	mask = landlock_get_net_access_mask(ruleset, 0);
> +	if ((net_port_attr.allowed_access | mask) != mask)
> +		return -EINVAL;
> +
> +	/* Denies inserting a rule with port higher than 65535. */
> +	if (net_port_attr.port > U16_MAX)
> +		return -EINVAL;
> +
> +	/* Imports the new rule. */
> +	return landlock_append_net_rule(ruleset, net_port_attr.port,
> +					net_port_attr.allowed_access);
> +}

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 01/12] landlock: Make ruleset's access masks more generic
  2023-10-18 12:28   ` Mickaël Salaün
@ 2023-10-19  1:45     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-19  1:45 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:28 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:19AM +0800, Konstantin Meskhidze wrote:
>> 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.
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-2-konstantin.meskhidze@huawei.com
> 
> Please don't include Link that points to the previous patch series. I
> add them when I apply a patch series to identify its source.

   Ok. Will be fixed.
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall
  2023-10-18 16:34   ` Mickaël Salaün
@ 2023-10-19 11:57     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-19 11:57 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 7:34 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:25AM +0800, Konstantin Meskhidze wrote:
>> Change 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.
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-8-konstantin.meskhidze@huawei.com
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * None.
>> 
>> Changes since v11:
>> * None.
>> 
>> 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(-)
>> 
>> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> index d35cd5d304db..8a54e87dbb17 100644
>> --- a/security/landlock/syscalls.c
>> +++ b/security/landlock/syscalls.c
>> @@ -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).
>> +	 */
> 
> You now can replace this comment block with that:
> +	/* Checks that allowed_access matches the @ruleset constraints. */

   Done.
> 
>> +	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;
>> +}
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall
  2023-10-18 12:28   ` Mickaël Salaün
@ 2023-10-19 11:59     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-19 11:59 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:28 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:25AM +0800, Konstantin Meskhidze wrote:
>> Change the landlock_add_rule() syscall to support new rule types
>> in future Landlock versions. Add the add_rule_path_beneath() helper
> 
> with next commits. Add the add_rule_path_beneath() helper

  Done.
> 
>> to support current filesystem rules.
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-18 12:29   ` Mickaël Salaün
@ 2023-10-20  4:08     ` Konstantin Meskhidze (A)
  2023-10-20  9:49       ` Mickaël Salaün
  0 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20  4:08 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:29 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> This commit adds network rules support in the ruleset management
> 
> Here are some advices to better write commit messages:
> https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
> The "Describe your changes in imperative mood" part is important for
> this commit and others. Most of this patch series' commit messages need
> small updates.

  Ok. I will refactor commit messages with "imperative mood". Thanks.
> 
>> 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
> 
> Please explain the "why" (when it makes sense) instead of just listing
> the "what".

   Ok.

> 
>> rights can be stored. Implement socket_bind() and socket_connect()
>> LSM hooks, which enables to restrict TCP socket binding and connection
>> to specific ports.
> 
> I reworded and moved this part in last:
>> For the file system, a file descriptor is a direct access to a file/data.
>> But for the network, it's impossible to identify for which data/peer a
>> newly created socket will give access to, it's needed to wait for a
>> connect or bind request to identify the use case for this socket.
>> That's why the access rights (related to ports) are tied to an opened
>> socket, but this would not align with the way Landlock access control
>> works for the filesystem [2].

    Thanks.
> 
> Please add empty line to split paragraphs.

   Got it.
> 
>> 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 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).
> 
> For the file system, a file descriptor is a direct access to a file/data.
> However, for network sockets, we cannot identify for which data or peer a newly
> created socket will give access to. Indeed, we need to wait for a connect or
> bind request to identify the use case for this socket.
> 
> Access rights are not tied to socket file descriptors. Instead, bind and
> connect actions are controlled by the task's domain.  As for the filesystem, a
> directory file descriptor may enable to open another file (i.e. a new data
> item), but this opening is restricted by the task's domain, not the file
> descriptor's access rights [2].
> 
>> 
>> [1]
>> https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> [2]
>> https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
> 
> [1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
> [2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>  
   Thanks.
> 
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
>> [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
>> to add_rule_net_service()]
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * Moves add_rule_net_port() back in syscalls.c and makes it static.
>> * Deletes bind_access_mask allowing bind action rule on port 0.
>> * Adds comment about port 0 in landlock_net_port_attr structure.
>> * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
>> * Minor fixes.
>> * Refactors commit message.
>> 
>> Changes since v11:
>> * Replaces dates with "2022-2023" in net.c/h files headers.
>> * Removes WARN_ON_ONCE(!domain) in check_socket_access().
>> * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
>> * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
>> * Renames landlock_net_service_attr to landlock_net_port_attr.
>> * Defines two add_rule_net_service() functions according to
>>   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>>   function.
>> * Adds af_family consistency check while handling AF_UNSPEC specifically.
>> * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>>   action on port zero.
>> * Minor fixes.
>> * Refactors commit message.
>> 
>> 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() and
>>   get_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                |  56 ++++++
>>  security/landlock/Kconfig                    |   1 +
>>  security/landlock/Makefile                   |   2 +
>>  security/landlock/limits.h                   |   5 +
>>  security/landlock/net.c                      | 198 +++++++++++++++++++
>>  security/landlock/net.h                      |  33 ++++
>>  security/landlock/ruleset.c                  |  62 +++++-
>>  security/landlock/ruleset.h                  |  59 +++++-
>>  security/landlock/setup.c                    |   2 +
>>  security/landlock/syscalls.c                 |  69 ++++++-
>>  tools/testing/selftests/landlock/base_test.c |   2 +-
>>  11 files changed, 466 insertions(+), 23 deletions(-)
>>  create mode 100644 security/landlock/net.c
>>  create mode 100644 security/landlock/net.h
>> 
>> diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
>> index 81d09ef9aa50..25349666b19e 100644
>> --- a/include/uapi/linux/landlock.h
>> +++ b/include/uapi/linux/landlock.h
>> @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>>  	 * landlock_path_beneath_attr .
>>  	 */
>>  	LANDLOCK_RULE_PATH_BENEATH = 1,
>> +	/**
>> +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
>> +	 * landlock_net_port_attr .
>> +	 */
>> +	LANDLOCK_RULE_NET_PORT = 2,
> 
> We don't need the explicit " = 2".

   Fixed. Thanks.
> 
>>  };
>> 
>>  /**
>> @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>>  	 */
>>  } __attribute__((packed));
>> 
>> +/**
>> + * struct landlock_net_port_attr - Network port definition
>> + *
>> + * Argument of sys_landlock_add_rule().
>> + */
>> +struct landlock_net_port_attr {
>> +	/**
>> +	 * @allowed_access: Bitmask of allowed access network for a port
>> +	 * (cf. `Network flags`_).
>> +	 */
>> +	__u64 allowed_access;
>> +	/**
>> +	 * @port: Network port. Landlock does not forbid rules with port 0,
>> +	 * since some network services use it. Port 0 is a reserved one in
>> +	 * TCP/IP networking, meaning that it should not be used in TCP or
>> +	 * UDP messages. To allocate its source port number, services call
>> +	 * TCP/IP network functions like bind() to request one. With port 0
>> +	 * it triggers the operating system to automatically search for
>> +	 * and return a suitable available port in the TCP/IP dynamic
>> +	 * port number range. This port range can be controlled by a
>> +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
>> +	 * which is also used by IPv6.
> 
> This looks too inspired from
> https://www.lifewire.com/port-0-in-tcp-and-udp-818145

   Yep. You are right.
> 
> Let's make it simpler:
> 
>   * @port: Network port.
>   *
>   * It should be noted that port 0 passed to :manpage:`bind(2)` will
>   * bind to an available port from a specific port range. This can be
>   * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
>   * sysctl (also used for IPv6). A Landlock rule with port 0 and the
>   * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
>   * on port 0 is allowed and it will automatically translate to binding
>   * on the related port range.
>   
     Thanks.
> 
>> +	 */
>> +	__u64 port;
>> +};
>> +
>>  /**
>>   * DOC: fs_access
>>   *
>> @@ -189,4 +226,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.
> 
> You can add:
> "This is supported since ABI 4."

    Updated.
> 
>> + *
>> + * 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 */
>> diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
>> index c1e862a38410..c4bf0d5eff39 100644
>> --- a/security/landlock/Kconfig
>> +++ b/security/landlock/Kconfig
>> @@ -3,6 +3,7 @@
>>  config SECURITY_LANDLOCK
>>  	bool "Landlock support"
>>  	depends on SECURITY
>> +	select SECURITY_NETWORK
>>  	select SECURITY_PATH
>>  	help
>>  	  Landlock is a sandboxing mechanism that enables processes to restrict
>> diff --git a/security/landlock/Makefile b/security/landlock/Makefile
>> index 7bbd2f413b3e..53d3c92ae22e 100644
>> --- a/security/landlock/Makefile
>> +++ b/security/landlock/Makefile
>> @@ -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
>> \ No newline at end of file
>> diff --git a/security/landlock/limits.h b/security/landlock/limits.h
>> index bafb3b8dc677..93c9c6f91556 100644
>> --- a/security/landlock/limits.h
>> +++ b/security/landlock/limits.h
>> @@ -23,6 +23,11 @@
>>  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>>  #define LANDLOCK_SHIFT_ACCESS_FS	0
>> 
>> +#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 */
>> diff --git a/security/landlock/net.c b/security/landlock/net.c
>> new file mode 100644
>> index 000000000000..1bf26cf3c41b
>> --- /dev/null
>> +++ b/security/landlock/net.c
>> @@ -0,0 +1,198 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * Landlock LSM - Network management and hooks
>> + *
>> + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
>> + * Copyright © 2022-2023 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,
> 
> To be consistent with current_check_access_path(), please rename to
> current_check_access_socket().

   Done. Thanks.
> 
>> +			       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();
> 
> For consistency with other functions, s/domain/dom/g

  Ok. Fixed.
> 
>> +
>> +	if (!domain)
>> +		return 0;
>> +	if (WARN_ON_ONCE(domain->num_layers < 1))
>> +		return -EACCES;
>> +
>> +	/* Checks if it's a (potential) TCP socket. */
>> +	if (sock->type != SOCK_STREAM)
>> +		return 0;
>> +
>> +	/* Checks for minimal header length to safely read sa_family. */
>> +	if (addrlen < offsetofend(typeof(*address), sa_family))
>> +		return -EINVAL;
>> +
>> +	switch (address->sa_family) {
>> +	case AF_UNSPEC:
>> +	case AF_INET:
>> +		if (addrlen < sizeof(struct sockaddr_in))
>> +			return -EINVAL;
>> +		port = ((struct sockaddr_in *)address)->sin_port;
>> +		break;
>> +#if IS_ENABLED(CONFIG_IPV6)
>> +	case AF_INET6:
>> +		if (addrlen < SIN6_LEN_RFC2133)
>> +			return -EINVAL;
>> +		port = ((struct sockaddr_in6 *)address)->sin6_port;
>> +		break;
>> +#endif
> 
> #endif /* IS_ENABLED(CONFIG_INET) */

   #endif /* IS_ENABLED(CONFIG_IPV6) */ I suppose.
> 
>> +	default:
>> +		return 0;
>> +	}
>> +
>> +	/* Specific AF_UNSPEC handling. */
>> +	if (address->sa_family == AF_UNSPEC) {
>> +		/*
>> +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
>> +		 * association, which have the same effect as closing the
>> +		 * 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;
>> +
>> +		/*
>> +		 * For compatibility reason, accept AF_UNSPEC for bind
>> +		 * accesses (mapped to AF_INET) only if the address is
>> +		 * 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;
>> +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
>> +
>> +	rule = landlock_find_rule(domain, id);
>> +	handled_access = landlock_init_layer_masks(
>> +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
>> +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
>> +				   ARRAY_SIZE(layer_masks)))
>> +		return 0;
>> +
>> +	return -EACCES;
>> +}
>> +
>> +static int hook_socket_bind(struct socket *const sock,
>> +			    struct sockaddr *const address, const int addrlen)
>> +{
>> +	return check_socket_access(sock, address, addrlen,
>> +				   LANDLOCK_ACCESS_NET_BIND_TCP);
>> +}
>> +
>> +static int hook_socket_connect(struct socket *const sock,
>> +			       struct sockaddr *const address,
>> +			       const int addrlen)
>> +{
>> +	return check_socket_access(sock, address, addrlen,
>> +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
>> +}
>> +
>> +static struct security_hook_list landlock_hooks[] __ro_after_init = {
>> +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
>> +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
>> +};
>> +
>> +__init void landlock_add_net_hooks(void)
>> +{
>> +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
>> +			   LANDLOCK_NAME);
>> +}
>> diff --git a/security/landlock/net.h b/security/landlock/net.h
>> new file mode 100644
>> index 000000000000..588a49fd6907
>> --- /dev/null
>> +++ b/security/landlock/net.h
>> @@ -0,0 +1,33 @@
>> +/* SPDX-License-Identifier: GPL-2.0-only */
>> +/*
>> + * Landlock LSM - Network management and hooks
>> + *
>> + * Copyright © 2022-2023 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)
>> +{
>> +}
>> +
>> +static inline int
>> +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> +			 access_mask_t access_rights);
>> +{
>> +	return -EAFNOSUPPORT;
>> +}
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>> +#endif /* _SECURITY_LANDLOCK_NET_H */
>> diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> index 4c209acee01e..1fe4298ff4a7 100644
>> --- a/security/landlock/ruleset.c
>> +++ b/security/landlock/ruleset.c
>> @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>>  	refcount_set(&new_ruleset->usage, 1);
>>  	mutex_init(&new_ruleset->lock);
>>  	new_ruleset->root_inode = RB_ROOT;
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	new_ruleset->root_net_port = RB_ROOT;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	new_ruleset->num_layers = num_layers;
>>  	/*
>>  	 * hierarchy = NULL
>> @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>>  }
>> 
>>  struct landlock_ruleset *
>> -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> +			const access_mask_t net_access_mask)
>>  {
>>  	struct landlock_ruleset *new_ruleset;
>> 
>>  	/* Informs about useless ruleset. */
>> -	if (!fs_access_mask)
>> +	if (!fs_access_mask && !net_access_mask)
>>  		return ERR_PTR(-ENOMSG);
>>  	new_ruleset = create_ruleset(1);
>> -	if (!IS_ERR(new_ruleset))
>> +	if (IS_ERR(new_ruleset))
>> +		return new_ruleset;
>> +	if (fs_access_mask)
>>  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> +	if (net_access_mask)
>> +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> 
> This is good, but it is not tested: we need to add a test that both
> handle FS and net restrictions. You can add one in net.c, just handling
> LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> rule with path_beneath (e.g. /dev) and another with net_port, and check
> that open("/") is denied, open("/dev") is allowed, and and only the
> allowed port is allowed with bind(). This test should be simple and can
> only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> after port_endianness. fcntl.h should then be included by net.c

   Ok.
> 
> I guess that was the purpose of layout1.with_net (in fs_test.c) but it

   Yep. I added this kind of nest in fs_test.c to test both fs and 
network rules together.
> is not complete. You can revamp this test and move it to net.c
> following the above suggestions, keeping it consistent with other tests
> in net.c . You don't need the test_open() nor create_ruleset() helpers.
> 
> This test must failed if we change "ruleset->access_masks[layer_level] |="
> to "ruleset->access_masks[layer_level] =" in
> landlock_add_fs_access_mask() or landlock_add_net_access_mask().

   Do you want to change it? Why?
   Fs and network masks are ORed to not intersect with each other.
> 
>>  	return new_ruleset;
>>  }
>> 
>> @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>>  	case LANDLOCK_KEY_INODE:
>>  		return true;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		return false;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>>  		WARN_ON_ONCE(1);
>>  		return false;
>> @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>>  	case LANDLOCK_KEY_INODE:
>>  		return &ruleset->root_inode;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		return &ruleset->root_net_port;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>> +		WARN_ON_ONCE(1);
> 
> Please move this WARN to the patch that added the previous and next
> lines.

   OK. Will be moved.
> 
>>  		return ERR_PTR(-EINVAL);
>>  	}
>>  }
>> @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>>  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>>  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>>  	BUILD_BUG_ON(access_masks <
>> -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
>> +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
>> +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>>  }
>> 
>>  /**
>> @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>>  	if (err)
>>  		goto out_unlock;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	/* Merges the @src network port tree. */
>> +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
>> +	if (err)
>> +		goto out_unlock;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  out_unlock:
>>  	mutex_unlock(&src->lock);
>>  	mutex_unlock(&dst->lock);
>> @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>>  	if (err)
>>  		goto out_unlock;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	/* Copies the @parent network port tree. */
>> +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
>> +	if (err)
>> +		goto out_unlock;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>>  		err = -EINVAL;
>>  		goto out_unlock;
>> @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>>  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>>  					     node)
>>  		free_rule(freeme, LANDLOCK_KEY_INODE);
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	rbtree_postorder_for_each_entry_safe(freeme, next,
>> +					     &ruleset->root_net_port, node)
>> +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	put_hierarchy(ruleset->hierarchy);
>>  	kfree(ruleset);
>>  }
>> @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>>   *
>>   * @domain: The domain that defines the current restrictions.
>>   * @access_request: The requested access rights to check.
>> - * @layer_masks: The layer masks to populate.
>> + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
> 
> "%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"

   Done.
> 
>> + * elements according to @key_type.
>>   * @key_type: The key type to switch between access masks of different types.
>>   *
>>   * Returns: An access mask where each access right bit is set which is handled
>> @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>>  		get_access_mask = landlock_get_fs_access_mask;
>>  		num_access = LANDLOCK_NUM_ACCESS_FS;
>>  		break;
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		get_access_mask = landlock_get_net_access_mask;
>> +		num_access = LANDLOCK_NUM_ACCESS_NET;
>> +		break;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>>  		WARN_ON_ONCE(1);
>>  		return 0;
>> diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
>> index 1ede2b9a79b7..ba4a06035599 100644
>> --- a/security/landlock/ruleset.h
>> +++ b/security/landlock/ruleset.h
>> @@ -33,13 +33,16 @@
>>  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);
>> +/* Makes sure all network access rights can be stored. */
>> +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>>  /* 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;
>> +typedef u32 access_masks_t;
>>  /* Makes sure all ruleset access rights can be stored. */
>> -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
>> +static_assert(BITS_PER_TYPE(access_masks_t) >=
>> +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
>> 
>>  typedef u16 layer_mask_t;
>>  /* Makes sure all layers can be checked. */
>> @@ -84,6 +87,11 @@ enum landlock_key_type {
>>  	 * keys.
>>  	 */
>>  	LANDLOCK_KEY_INODE = 1,
>> +	/**
>> +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
>> +	 * node keys.
>> +	 */
>> +	LANDLOCK_KEY_NET_PORT,
>>  };
>> 
>>  /**
>> @@ -158,6 +166,13 @@ struct landlock_ruleset {
>>  	 * reaches zero.
>>  	 */
>>  	struct rb_root root_inode;
> 
> #if IS_ENABLED(CONFIG_INET)
>  
   OK. Done.
>> +	/**
>> +	 * @root_net_port: Root of a red-black tree containing &struct
>> +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
>> +	 * process (i.e. as a domain), this tree is immutable until @usage
>> +	 * reaches zero.
>> +	 */
>> +	struct rb_root root_net_port;
> 
> #endif /* IS_ENABLED(CONFIG_INET) */

  Done.
> 
>>  	/**
>>  	 * @hierarchy: Enables hierarchy identification even when a parent
>>  	 * domain vanishes.  This is needed for the ptrace protection.
>> @@ -196,13 +211,13 @@ struct landlock_ruleset {
>>  			 */
>>  			u32 num_layers;
>>  			/**
>> -			 * @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
>> -			 * one.  These layers are used when merging rulesets,
>> -			 * for user space backward compatibility (i.e.
>> -			 * future-proof), and to properly handle merged
>> +			 * @access_masks: Contains the subset of filesystem and
>> +			 * network actions that are restricted by a ruleset.
>> +			 * A domain saves all layers of merged rulesets in a
>> +			 * stack (FAM), starting from the first layer to the
>> +			 * last one.  These layers are used when merging
>> +			 * rulesets, for user space backward compatibility
>> +			 * (i.e. future-proof), and to properly handle merged
>>  			 * rulesets without overlapping access rights.  These
>>  			 * layers are set once and never changed for the
>>  			 * lifetime of the ruleset.
>> @@ -213,7 +228,8 @@ struct landlock_ruleset {
>>  };
>> 
>>  struct landlock_ruleset *
>> -landlock_create_ruleset(const access_mask_t access_mask);
>> +landlock_create_ruleset(const access_mask_t access_mask_fs,
>> +			const access_mask_t access_mask_net);
>> 
>>  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>>  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
>> @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>>  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>>  }
>> 
>> +static inline void
>> +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
>> +			     const access_mask_t net_access_mask,
>> +			     const u16 layer_level)
>> +{
>> +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
>> +
>> +	/* Should already be checked in sys_landlock_create_ruleset(). */
>> +	WARN_ON_ONCE(net_access_mask != net_mask);
>> +	ruleset->access_masks[layer_level] |=
>> +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
>> +}
>> +
>>  static inline access_mask_t
>>  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>>  				const u16 layer_level)
>> @@ -266,6 +295,16 @@ 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;
>>  }
>> +
>> +static inline access_mask_t
>> +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
>> +			     const u16 layer_level)
>> +{
>> +	return (ruleset->access_masks[layer_level] >>
>> +		LANDLOCK_SHIFT_ACCESS_NET) &
>> +	       LANDLOCK_MASK_ACCESS_NET;
>> +}
>> +
>>  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>>  			    const access_mask_t access_request,
>>  			    layer_mask_t (*const layer_masks)[],
>> diff --git a/security/landlock/setup.c b/security/landlock/setup.c
>> index 0f6113528fa4..df81612811bf 100644
>> --- a/security/landlock/setup.c
>> +++ b/security/landlock/setup.c
>> @@ -14,6 +14,7 @@
>>  #include "fs.h"
>>  #include "ptrace.h"
>>  #include "setup.h"
>> +#include "net.h"
>> 
>>  bool landlock_initialized __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;
>> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> index 8a54e87dbb17..3ad652d9a146 100644
>> --- a/security/landlock/syscalls.c
>> +++ b/security/landlock/syscalls.c
>> @@ -29,6 +29,7 @@
>>  #include "cred.h"
>>  #include "fs.h"
>>  #include "limits.h"
>> +#include "net.h"
>>  #include "ruleset.h"
>>  #include "setup.h"
>> 
>> @@ -74,7 +75,8 @@ static void build_check_abi(void)
>>  {
>>  	struct landlock_ruleset_attr ruleset_attr;
>>  	struct landlock_path_beneath_attr path_beneath_attr;
>> -	size_t ruleset_size, path_beneath_size;
>> +	struct landlock_net_port_attr net_port_attr;
>> +	size_t ruleset_size, path_beneath_size, net_port_size;
>> 
>>  	/*
>>  	 * For each user space ABI structures, first checks that there is no
>> @@ -82,13 +84,19 @@ static void build_check_abi(void)
>>  	 * struct size.
>>  	 */
>>  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
>> +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>>  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
>> -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
>> +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
>> 
>>  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>>  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
>> +
>> +	net_port_size = sizeof(net_port_attr.allowed_access);
>> +	net_port_size += sizeof(net_port_attr.port);
>> +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
>> +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>>  }
>> 
>>  /* Ruleset handling */
>> @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>>  	.write = fop_dummy_write,
>>  };
>> 
>> -#define LANDLOCK_ABI_VERSION 3
>> +#define LANDLOCK_ABI_VERSION 4
>> 
>>  /**
>>   * sys_landlock_create_ruleset - Create a new ruleset
>> @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>>  	    LANDLOCK_MASK_ACCESS_FS)
>>  		return -EINVAL;
>> 
>> +	/* Checks network content (and 32-bits cast). */
>> +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
>> +	    LANDLOCK_MASK_ACCESS_NET)
>> +		return -EINVAL;
>> +
>>  	/* Checks arguments and transforms to kernel struct. */
>> -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
>> +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
>> +					  ruleset_attr.handled_access_net);
>>  	if (IS_ERR(ruleset))
>>  		return PTR_ERR(ruleset);
>> 
>> @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>>  	int res, err;
>>  	access_mask_t mask;
>> 
>> -	/* Copies raw user space buffer, only one type for now. */
>> +	/* Copies raw user space buffer. */
> 
> Shouldn't this be part of a previous patch?

I did it according Gunter's suggestion
https://lore.kernel.org/netdev/20230627.82cde73b1efe@gnoack.org/
> 
>>  	res = copy_from_user(&path_beneath_attr, rule_attr,
>>  			     sizeof(path_beneath_attr));
>>  	if (res)
>> @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>>  	return err;
>>  }
>> 
>> +static int add_rule_net_port(struct landlock_ruleset *ruleset,
>> +			     const void __user *const rule_attr)
>> +{
>> +	struct landlock_net_port_attr net_port_attr;
>> +	int res;
>> +	access_mask_t mask;
>> +
>> +	/* Copies raw user space buffer. */
>> +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
>> +	if (res)
>> +		return -EFAULT;
>> +
>> +	/*
>> +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
>> +	 * are ignored by network actions.
>> +	 */
>> +	if (!net_port_attr.allowed_access)
>> +		return -ENOMSG;
>> +
>> +	/*
>> +	 * Checks that allowed_access matches the @ruleset constraints
>> +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
>> +	 */
>> +	mask = landlock_get_net_access_mask(ruleset, 0);
>> +	if ((net_port_attr.allowed_access | mask) != mask)
>> +		return -EINVAL;
>> +
>> +	/* Denies inserting a rule with port higher than 65535. */
> 
> For consistency with the following comment:
> "Denies inserting a rule with port greater than 65535."
> 
   Done. Thanks.
> 
>> +	if (net_port_attr.port > U16_MAX)
>> +		return -EINVAL;
>> +
>> +	/* Imports the new rule. */
>> +	return landlock_append_net_rule(ruleset, net_port_attr.port,
>> +					net_port_attr.allowed_access);
>> +}
>> +
>>  /**
>>   * sys_landlock_add_rule - Add a new rule to a ruleset
>>   *
>>   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
>>   *		with the new rule.
>> - * @rule_type: Identify the structure type pointed to by @rule_attr (only
>> - *             %LANDLOCK_RULE_PATH_BENEATH for now).
>> + * @rule_type: Identify the structure type pointed to by @rule_attr:
>> + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
>>   * @rule_attr: Pointer to a rule (only of type &struct
>>   *             landlock_path_beneath_attr for now).
>>   * @flags: Must be 0.
>> @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>>   * Possible returned errors are:
>>   *
>>   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
>> + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
> 
> %LANDLOCK_RULE_NET_PORT

  Done.
> 
>> + *   supported by the running kernel;
>>   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
>>   *   &landlock_path_beneath_attr.allowed_access is not a subset of the
> 
> &landlock_path_beneath_attr.allowed_access or
> &landlock_net_port_attr.allowed_access is not a subset of the

   Fixed. Thanks.
> 
>>   *   ruleset handled accesses);
> 
> EINVAL description needs to be updated, especially for port > U16_MAX:
> - *   ruleset handled accesses);
> + *   ruleset handled accesses), or &landlock_net_port_attr.port is
> +     greater than 65535;

  Done. Thanks.
> 
> 
>> @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
>>  	case LANDLOCK_RULE_PATH_BENEATH:
>>  		err = add_rule_path_beneath(ruleset, rule_attr);
>>  		break;
>> +	case LANDLOCK_RULE_NET_PORT:
>> +		err = add_rule_net_port(ruleset, rule_attr);
>> +		break;
>>  	default:
>>  		err = -EINVAL;
>>  		break;
>> diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
>> index 792c3f0a59b4..646f778dfb1e 100644
>> --- a/tools/testing/selftests/landlock/base_test.c
>> +++ b/tools/testing/selftests/landlock/base_test.c
>> @@ -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
>> 
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-18 16:34   ` Mickaël Salaün
@ 2023-10-20  9:40     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20  9:40 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 7:34 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> This 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.
>> For the file system, a file descriptor is a direct access to a file/data.
>> But for the network, it's impossible to identify for which data/peer a
>> newly created socket will give access to, it's needed to wait for a
>> connect or bind request to identify the use case for this socket.
>> That's why the access rights (related to ports) are tied to an opened
>> socket, but this would not align with the way Landlock access control
>> works for the filesystem [2].
>> 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 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).
>> 
>> [1]
>> https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> [2]
>> https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
>> [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
>> to add_rule_net_service()]
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * Moves add_rule_net_port() back in syscalls.c and makes it static.
>> * Deletes bind_access_mask allowing bind action rule on port 0.
>> * Adds comment about port 0 in landlock_net_port_attr structure.
>> * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
>> * Minor fixes.
>> * Refactors commit message.
>> 
>> Changes since v11:
>> * Replaces dates with "2022-2023" in net.c/h files headers.
>> * Removes WARN_ON_ONCE(!domain) in check_socket_access().
>> * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
>> * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
>> * Renames landlock_net_service_attr to landlock_net_port_attr.
>> * Defines two add_rule_net_service() functions according to
>>   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>>   function.
>> * Adds af_family consistency check while handling AF_UNSPEC specifically.
>> * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>>   action on port zero.
>> * Minor fixes.
>> * Refactors commit message.
>> 
>> 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() and
>>   get_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                |  56 ++++++
>>  security/landlock/Kconfig                    |   1 +
>>  security/landlock/Makefile                   |   2 +
>>  security/landlock/limits.h                   |   5 +
>>  security/landlock/net.c                      | 198 +++++++++++++++++++
>>  security/landlock/net.h                      |  33 ++++
>>  security/landlock/ruleset.c                  |  62 +++++-
>>  security/landlock/ruleset.h                  |  59 +++++-
>>  security/landlock/setup.c                    |   2 +
>>  security/landlock/syscalls.c                 |  69 ++++++-
>>  tools/testing/selftests/landlock/base_test.c |   2 +-
>>  11 files changed, 466 insertions(+), 23 deletions(-)
>>  create mode 100644 security/landlock/net.c
>>  create mode 100644 security/landlock/net.h
>> 
>> diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
>> index 81d09ef9aa50..25349666b19e 100644
>> --- a/include/uapi/linux/landlock.h
>> +++ b/include/uapi/linux/landlock.h
>> @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>>  	 * landlock_path_beneath_attr .
>>  	 */
>>  	LANDLOCK_RULE_PATH_BENEATH = 1,
>> +	/**
>> +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
>> +	 * landlock_net_port_attr .
>> +	 */
>> +	LANDLOCK_RULE_NET_PORT = 2,
>>  };
>> 
>>  /**
>> @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>>  	 */
>>  } __attribute__((packed));
>> 
>> +/**
>> + * struct landlock_net_port_attr - Network port definition
>> + *
>> + * Argument of sys_landlock_add_rule().
>> + */
>> +struct landlock_net_port_attr {
>> +	/**
>> +	 * @allowed_access: Bitmask of allowed access network for a port
>> +	 * (cf. `Network flags`_).
>> +	 */
>> +	__u64 allowed_access;
>> +	/**
>> +	 * @port: Network port. Landlock does not forbid rules with port 0,
>> +	 * since some network services use it. Port 0 is a reserved one in
>> +	 * TCP/IP networking, meaning that it should not be used in TCP or
>> +	 * UDP messages. To allocate its source port number, services call
>> +	 * TCP/IP network functions like bind() to request one. With port 0
>> +	 * it triggers the operating system to automatically search for
>> +	 * and return a suitable available port in the TCP/IP dynamic
>> +	 * port number range. This port range can be controlled by a
>> +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
>> +	 * which is also used by IPv6.
>> +	 */
>> +	__u64 port;
>> +};
>> +
>>  /**
>>   * DOC: fs_access
>>   *
>> @@ -189,4 +226,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 */
>> diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
>> index c1e862a38410..c4bf0d5eff39 100644
>> --- a/security/landlock/Kconfig
>> +++ b/security/landlock/Kconfig
>> @@ -3,6 +3,7 @@
>>  config SECURITY_LANDLOCK
>>  	bool "Landlock support"
>>  	depends on SECURITY
>> +	select SECURITY_NETWORK
>>  	select SECURITY_PATH
>>  	help
>>  	  Landlock is a sandboxing mechanism that enables processes to restrict
>> diff --git a/security/landlock/Makefile b/security/landlock/Makefile
>> index 7bbd2f413b3e..53d3c92ae22e 100644
>> --- a/security/landlock/Makefile
>> +++ b/security/landlock/Makefile
>> @@ -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
>> \ No newline at end of file
>> diff --git a/security/landlock/limits.h b/security/landlock/limits.h
>> index bafb3b8dc677..93c9c6f91556 100644
>> --- a/security/landlock/limits.h
>> +++ b/security/landlock/limits.h
>> @@ -23,6 +23,11 @@
>>  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>>  #define LANDLOCK_SHIFT_ACCESS_FS	0
>> 
>> +#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 */
>> diff --git a/security/landlock/net.c b/security/landlock/net.c
>> new file mode 100644
>> index 000000000000..1bf26cf3c41b
>> --- /dev/null
>> +++ b/security/landlock/net.c
>> @@ -0,0 +1,198 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * Landlock LSM - Network management and hooks
>> + *
>> + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
>> + * Copyright © 2022-2023 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 = {
>> +		.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;
>> +
>> +	/* Checks if it's a (potential) TCP socket. */
>> +	if (sock->type != SOCK_STREAM)
>> +		return 0;
>> +
>> +	/* Checks for minimal header length to safely read sa_family. */
>> +	if (addrlen < offsetofend(typeof(*address), sa_family))
>> +		return -EINVAL;
>> +
>> +	switch (address->sa_family) {
>> +	case AF_UNSPEC:
>> +	case AF_INET:
>> +		if (addrlen < sizeof(struct sockaddr_in))
>> +			return -EINVAL;
>> +		port = ((struct sockaddr_in *)address)->sin_port;
>> +		break;
>> +#if IS_ENABLED(CONFIG_IPV6)
>> +	case AF_INET6:
>> +		if (addrlen < SIN6_LEN_RFC2133)
>> +			return -EINVAL;
>> +		port = ((struct sockaddr_in6 *)address)->sin6_port;
>> +		break;
>> +#endif
>> +	default:
>> +		return 0;
>> +	}
>> +
>> +	/* Specific AF_UNSPEC handling. */
>> +	if (address->sa_family == AF_UNSPEC) {
>> +		/*
>> +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
>> +		 * association, which have the same effect as closing the
>> +		 * 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;
>> +
>> +		/*
>> +		 * For compatibility reason, accept AF_UNSPEC for bind
>> +		 * accesses (mapped to AF_INET) only if the address is
>> +		 * 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;
>> +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
>> +
>> +	rule = landlock_find_rule(domain, id);
>> +	handled_access = landlock_init_layer_masks(
>> +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
>> +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
>> +				   ARRAY_SIZE(layer_masks)))
>> +		return 0;
>> +
>> +	return -EACCES;
>> +}
>> +
>> +static int hook_socket_bind(struct socket *const sock,
>> +			    struct sockaddr *const address, const int addrlen)
>> +{
>> +	return check_socket_access(sock, address, addrlen,
>> +				   LANDLOCK_ACCESS_NET_BIND_TCP);
>> +}
>> +
>> +static int hook_socket_connect(struct socket *const sock,
>> +			       struct sockaddr *const address,
>> +			       const int addrlen)
>> +{
>> +	return check_socket_access(sock, address, addrlen,
>> +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
>> +}
>> +
>> +static struct security_hook_list landlock_hooks[] __ro_after_init = {
>> +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
>> +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
>> +};
>> +
>> +__init void landlock_add_net_hooks(void)
>> +{
>> +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
>> +			   LANDLOCK_NAME);
>> +}
>> diff --git a/security/landlock/net.h b/security/landlock/net.h
>> new file mode 100644
>> index 000000000000..588a49fd6907
>> --- /dev/null
>> +++ b/security/landlock/net.h
>> @@ -0,0 +1,33 @@
>> +/* SPDX-License-Identifier: GPL-2.0-only */
>> +/*
>> + * Landlock LSM - Network management and hooks
>> + *
>> + * Copyright © 2022-2023 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)
>> +{
>> +}
>> +
>> +static inline int
>> +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> +			 access_mask_t access_rights);
>> +{
>> +	return -EAFNOSUPPORT;
>> +}
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>> +#endif /* _SECURITY_LANDLOCK_NET_H */
>> diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> index 4c209acee01e..1fe4298ff4a7 100644
>> --- a/security/landlock/ruleset.c
>> +++ b/security/landlock/ruleset.c
>> @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>>  	refcount_set(&new_ruleset->usage, 1);
>>  	mutex_init(&new_ruleset->lock);
>>  	new_ruleset->root_inode = RB_ROOT;
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	new_ruleset->root_net_port = RB_ROOT;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	new_ruleset->num_layers = num_layers;
>>  	/*
>>  	 * hierarchy = NULL
>> @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>>  }
>> 
>>  struct landlock_ruleset *
>> -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> +			const access_mask_t net_access_mask)
>>  {
>>  	struct landlock_ruleset *new_ruleset;
>> 
>>  	/* Informs about useless ruleset. */
>> -	if (!fs_access_mask)
>> +	if (!fs_access_mask && !net_access_mask)
>>  		return ERR_PTR(-ENOMSG);
>>  	new_ruleset = create_ruleset(1);
>> -	if (!IS_ERR(new_ruleset))
>> +	if (IS_ERR(new_ruleset))
>> +		return new_ruleset;
>> +	if (fs_access_mask)
>>  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> +	if (net_access_mask)
>> +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>>  	return new_ruleset;
>>  }
>> 
>> @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>>  	case LANDLOCK_KEY_INODE:
>>  		return true;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		return false;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>>  		WARN_ON_ONCE(1);
>>  		return false;
>> @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>>  	case LANDLOCK_KEY_INODE:
>>  		return &ruleset->root_inode;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		return &ruleset->root_net_port;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>> +		WARN_ON_ONCE(1);
>>  		return ERR_PTR(-EINVAL);
>>  	}
>>  }
>> @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>>  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>>  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>>  	BUILD_BUG_ON(access_masks <
>> -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
>> +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
>> +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>>  }
>> 
>>  /**
>> @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>>  	if (err)
>>  		goto out_unlock;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	/* Merges the @src network port tree. */
>> +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
>> +	if (err)
>> +		goto out_unlock;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  out_unlock:
>>  	mutex_unlock(&src->lock);
>>  	mutex_unlock(&dst->lock);
>> @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>>  	if (err)
>>  		goto out_unlock;
>> 
>> +#if IS_ENABLED(CONFIG_INET)
>> +	/* Copies the @parent network port tree. */
>> +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
>> +	if (err)
>> +		goto out_unlock;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>>  		err = -EINVAL;
>>  		goto out_unlock;
>> @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>>  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>>  					     node)
>>  		free_rule(freeme, LANDLOCK_KEY_INODE);
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	rbtree_postorder_for_each_entry_safe(freeme, next,
>> +					     &ruleset->root_net_port, node)
>> +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	put_hierarchy(ruleset->hierarchy);
>>  	kfree(ruleset);
>>  }
>> @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>>   *
>>   * @domain: The domain that defines the current restrictions.
>>   * @access_request: The requested access rights to check.
>> - * @layer_masks: The layer masks to populate.
>> + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
>> + * elements according to @key_type.
>>   * @key_type: The key type to switch between access masks of different types.
>>   *
>>   * Returns: An access mask where each access right bit is set which is handled
>> @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>>  		get_access_mask = landlock_get_fs_access_mask;
>>  		num_access = LANDLOCK_NUM_ACCESS_FS;
>>  		break;
>> +
>> +#if IS_ENABLED(CONFIG_INET)
>> +	case LANDLOCK_KEY_NET_PORT:
>> +		get_access_mask = landlock_get_net_access_mask;
>> +		num_access = LANDLOCK_NUM_ACCESS_NET;
>> +		break;
>> +#endif /* IS_ENABLED(CONFIG_INET) */
>> +
>>  	default:
>>  		WARN_ON_ONCE(1);
>>  		return 0;
>> diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
>> index 1ede2b9a79b7..ba4a06035599 100644
>> --- a/security/landlock/ruleset.h
>> +++ b/security/landlock/ruleset.h
>> @@ -33,13 +33,16 @@
>>  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);
>> +/* Makes sure all network access rights can be stored. */
>> +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>>  /* 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;
>> +typedef u32 access_masks_t;
>>  /* Makes sure all ruleset access rights can be stored. */
>> -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
>> +static_assert(BITS_PER_TYPE(access_masks_t) >=
>> +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
>> 
>>  typedef u16 layer_mask_t;
>>  /* Makes sure all layers can be checked. */
>> @@ -84,6 +87,11 @@ enum landlock_key_type {
>>  	 * keys.
>>  	 */
>>  	LANDLOCK_KEY_INODE = 1,
>> +	/**
>> +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
>> +	 * node keys.
>> +	 */
>> +	LANDLOCK_KEY_NET_PORT,
>>  };
>> 
>>  /**
>> @@ -158,6 +166,13 @@ struct landlock_ruleset {
>>  	 * reaches zero.
>>  	 */
>>  	struct rb_root root_inode;
>> +	/**
>> +	 * @root_net_port: Root of a red-black tree containing &struct
>> +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
>> +	 * process (i.e. as a domain), this tree is immutable until @usage
>> +	 * reaches zero.
>> +	 */
>> +	struct rb_root root_net_port;
>>  	/**
>>  	 * @hierarchy: Enables hierarchy identification even when a parent
>>  	 * domain vanishes.  This is needed for the ptrace protection.
>> @@ -196,13 +211,13 @@ struct landlock_ruleset {
>>  			 */
>>  			u32 num_layers;
>>  			/**
>> -			 * @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
>> -			 * one.  These layers are used when merging rulesets,
>> -			 * for user space backward compatibility (i.e.
>> -			 * future-proof), and to properly handle merged
>> +			 * @access_masks: Contains the subset of filesystem and
>> +			 * network actions that are restricted by a ruleset.
>> +			 * A domain saves all layers of merged rulesets in a
>> +			 * stack (FAM), starting from the first layer to the
>> +			 * last one.  These layers are used when merging
>> +			 * rulesets, for user space backward compatibility
>> +			 * (i.e. future-proof), and to properly handle merged
>>  			 * rulesets without overlapping access rights.  These
>>  			 * layers are set once and never changed for the
>>  			 * lifetime of the ruleset.
>> @@ -213,7 +228,8 @@ struct landlock_ruleset {
>>  };
>> 
>>  struct landlock_ruleset *
>> -landlock_create_ruleset(const access_mask_t access_mask);
>> +landlock_create_ruleset(const access_mask_t access_mask_fs,
>> +			const access_mask_t access_mask_net);
>> 
>>  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>>  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
>> @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>>  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>>  }
>> 
>> +static inline void
>> +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
>> +			     const access_mask_t net_access_mask,
>> +			     const u16 layer_level)
>> +{
>> +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
>> +
>> +	/* Should already be checked in sys_landlock_create_ruleset(). */
>> +	WARN_ON_ONCE(net_access_mask != net_mask);
>> +	ruleset->access_masks[layer_level] |=
>> +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
>> +}
>> +
>>  static inline access_mask_t
>>  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>>  				const u16 layer_level)
>> @@ -266,6 +295,16 @@ 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;
>>  }
>> +
>> +static inline access_mask_t
>> +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
>> +			     const u16 layer_level)
>> +{
>> +	return (ruleset->access_masks[layer_level] >>
>> +		LANDLOCK_SHIFT_ACCESS_NET) &
>> +	       LANDLOCK_MASK_ACCESS_NET;
>> +}
>> +
>>  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>>  			    const access_mask_t access_request,
>>  			    layer_mask_t (*const layer_masks)[],
>> diff --git a/security/landlock/setup.c b/security/landlock/setup.c
>> index 0f6113528fa4..df81612811bf 100644
>> --- a/security/landlock/setup.c
>> +++ b/security/landlock/setup.c
>> @@ -14,6 +14,7 @@
>>  #include "fs.h"
>>  #include "ptrace.h"
>>  #include "setup.h"
>> +#include "net.h"
>> 
>>  bool landlock_initialized __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;
>> diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> index 8a54e87dbb17..3ad652d9a146 100644
>> --- a/security/landlock/syscalls.c
>> +++ b/security/landlock/syscalls.c
>> @@ -29,6 +29,7 @@
>>  #include "cred.h"
>>  #include "fs.h"
>>  #include "limits.h"
>> +#include "net.h"
>>  #include "ruleset.h"
>>  #include "setup.h"
>> 
>> @@ -74,7 +75,8 @@ static void build_check_abi(void)
>>  {
>>  	struct landlock_ruleset_attr ruleset_attr;
>>  	struct landlock_path_beneath_attr path_beneath_attr;
>> -	size_t ruleset_size, path_beneath_size;
>> +	struct landlock_net_port_attr net_port_attr;
>> +	size_t ruleset_size, path_beneath_size, net_port_size;
>> 
>>  	/*
>>  	 * For each user space ABI structures, first checks that there is no
>> @@ -82,13 +84,19 @@ static void build_check_abi(void)
>>  	 * struct size.
>>  	 */
>>  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
>> +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>>  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
>> -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
>> +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
>> 
>>  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>>  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>>  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
>> +
>> +	net_port_size = sizeof(net_port_attr.allowed_access);
>> +	net_port_size += sizeof(net_port_attr.port);
>> +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
>> +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>>  }
>> 
>>  /* Ruleset handling */
>> @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>>  	.write = fop_dummy_write,
>>  };
>> 
>> -#define LANDLOCK_ABI_VERSION 3
>> +#define LANDLOCK_ABI_VERSION 4
>> 
>>  /**
>>   * sys_landlock_create_ruleset - Create a new ruleset
>> @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>>  	    LANDLOCK_MASK_ACCESS_FS)
>>  		return -EINVAL;
>> 
>> +	/* Checks network content (and 32-bits cast). */
>> +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
>> +	    LANDLOCK_MASK_ACCESS_NET)
>> +		return -EINVAL;
>> +
>>  	/* Checks arguments and transforms to kernel struct. */
>> -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
>> +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
>> +					  ruleset_attr.handled_access_net);
>>  	if (IS_ERR(ruleset))
>>  		return PTR_ERR(ruleset);
>> 
>> @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>>  	int res, err;
>>  	access_mask_t mask;
>> 
>> -	/* Copies raw user space buffer, only one type for now. */
>> +	/* Copies raw user space buffer. */
>>  	res = copy_from_user(&path_beneath_attr, rule_attr,
>>  			     sizeof(path_beneath_attr));
>>  	if (res)
>> @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>>  	return err;
>>  }
>> 
>> +static int add_rule_net_port(struct landlock_ruleset *ruleset,
>> +			     const void __user *const rule_attr)
>> +{
>> +	struct landlock_net_port_attr net_port_attr;
>> +	int res;
>> +	access_mask_t mask;
>> +
>> +	/* Copies raw user space buffer. */
>> +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
>> +	if (res)
>> +		return -EFAULT;
>> +
>> +	/*
>> +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
>> +	 * are ignored by network actions.
>> +	 */
>> +	if (!net_port_attr.allowed_access)
>> +		return -ENOMSG;
>> +
>> +	/*
>> +	 * Checks that allowed_access matches the @ruleset constraints
>> +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
>> +	 */
> 
> You can replace this comment block with that:
> +	/* Checks that allowed_access matches the @ruleset constraints. */
> 
    Done.
>> +	mask = landlock_get_net_access_mask(ruleset, 0);
>> +	if ((net_port_attr.allowed_access | mask) != mask)
>> +		return -EINVAL;
>> +
>> +	/* Denies inserting a rule with port higher than 65535. */
>> +	if (net_port_attr.port > U16_MAX)
>> +		return -EINVAL;
>> +
>> +	/* Imports the new rule. */
>> +	return landlock_append_net_rule(ruleset, net_port_attr.port,
>> +					net_port_attr.allowed_access);
>> +}
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20  4:08     ` Konstantin Meskhidze (A)
@ 2023-10-20  9:49       ` Mickaël Salaün
  2023-10-20 11:58         ` Konstantin Meskhidze (A)
                           ` (2 more replies)
  0 siblings, 3 replies; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-20  9:49 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/18/2023 3:29 PM, Mickaël Salaün пишет:
> > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> > > This commit adds network rules support in the ruleset management
> > 
> > Here are some advices to better write commit messages:
> > https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
> > The "Describe your changes in imperative mood" part is important for
> > this commit and others. Most of this patch series' commit messages need
> > small updates.
> 
>  Ok. I will refactor commit messages with "imperative mood". Thanks.
> > 
> > > 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
> > 
> > Please explain the "why" (when it makes sense) instead of just listing
> > the "what".
> 
>   Ok.
> 
> > 
> > > rights can be stored. Implement socket_bind() and socket_connect()
> > > LSM hooks, which enables to restrict TCP socket binding and connection
> > > to specific ports.
> > 
> > I reworded and moved this part in last:
> > > For the file system, a file descriptor is a direct access to a file/data.
> > > But for the network, it's impossible to identify for which data/peer a
> > > newly created socket will give access to, it's needed to wait for a
> > > connect or bind request to identify the use case for this socket.
> > > That's why the access rights (related to ports) are tied to an opened
> > > socket, but this would not align with the way Landlock access control
> > > works for the filesystem [2].
> 
>    Thanks.
> > 
> > Please add empty line to split paragraphs.
> 
>   Got it.
> > 
> > > 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 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).
> > 
> > For the file system, a file descriptor is a direct access to a file/data.
> > However, for network sockets, we cannot identify for which data or peer a newly
> > created socket will give access to. Indeed, we need to wait for a connect or
> > bind request to identify the use case for this socket.
> > 
> > Access rights are not tied to socket file descriptors. Instead, bind and
> > connect actions are controlled by the task's domain.  As for the filesystem, a
> > directory file descriptor may enable to open another file (i.e. a new data
> > item), but this opening is restricted by the task's domain, not the file
> > descriptor's access rights [2].
> > 
> > > 
> > > [1]
> > > https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
> > > [2]
> > > https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
> > 
> > [1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
> > [2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>   Thanks.
> > 
> > > 
> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> > > Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
> > > [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
> > > to add_rule_net_service()]
> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
> > > ---
> > > 
> > > Changes since v12:
> > > * Moves add_rule_net_port() back in syscalls.c and makes it static.
> > > * Deletes bind_access_mask allowing bind action rule on port 0.
> > > * Adds comment about port 0 in landlock_net_port_attr structure.
> > > * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
> > > * Minor fixes.
> > > * Refactors commit message.
> > > 
> > > Changes since v11:
> > > * Replaces dates with "2022-2023" in net.c/h files headers.
> > > * Removes WARN_ON_ONCE(!domain) in check_socket_access().
> > > * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
> > > * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
> > > * Renames landlock_net_service_attr to landlock_net_port_attr.
> > > * Defines two add_rule_net_service() functions according to
> > >   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
> > >   function.
> > > * Adds af_family consistency check while handling AF_UNSPEC specifically.
> > > * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
> > >   action on port zero.
> > > * Minor fixes.
> > > * Refactors commit message.
> > > 
> > > 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() and
> > >   get_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                |  56 ++++++
> > >  security/landlock/Kconfig                    |   1 +
> > >  security/landlock/Makefile                   |   2 +
> > >  security/landlock/limits.h                   |   5 +
> > >  security/landlock/net.c                      | 198 +++++++++++++++++++
> > >  security/landlock/net.h                      |  33 ++++
> > >  security/landlock/ruleset.c                  |  62 +++++-
> > >  security/landlock/ruleset.h                  |  59 +++++-
> > >  security/landlock/setup.c                    |   2 +
> > >  security/landlock/syscalls.c                 |  69 ++++++-
> > >  tools/testing/selftests/landlock/base_test.c |   2 +-
> > >  11 files changed, 466 insertions(+), 23 deletions(-)
> > >  create mode 100644 security/landlock/net.c
> > >  create mode 100644 security/landlock/net.h
> > > 
> > > diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
> > > index 81d09ef9aa50..25349666b19e 100644
> > > --- a/include/uapi/linux/landlock.h
> > > +++ b/include/uapi/linux/landlock.h
> > > @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
> > >  	 * landlock_path_beneath_attr .
> > >  	 */
> > >  	LANDLOCK_RULE_PATH_BENEATH = 1,
> > > +	/**
> > > +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
> > > +	 * landlock_net_port_attr .
> > > +	 */
> > > +	LANDLOCK_RULE_NET_PORT = 2,
> > 
> > We don't need the explicit " = 2".
> 
>   Fixed. Thanks.
> > 
> > >  };
> > > 
> > >  /**
> > > @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
> > >  	 */
> > >  } __attribute__((packed));
> > > 
> > > +/**
> > > + * struct landlock_net_port_attr - Network port definition
> > > + *
> > > + * Argument of sys_landlock_add_rule().
> > > + */
> > > +struct landlock_net_port_attr {
> > > +	/**
> > > +	 * @allowed_access: Bitmask of allowed access network for a port
> > > +	 * (cf. `Network flags`_).
> > > +	 */
> > > +	__u64 allowed_access;
> > > +	/**
> > > +	 * @port: Network port. Landlock does not forbid rules with port 0,
> > > +	 * since some network services use it. Port 0 is a reserved one in
> > > +	 * TCP/IP networking, meaning that it should not be used in TCP or
> > > +	 * UDP messages. To allocate its source port number, services call
> > > +	 * TCP/IP network functions like bind() to request one. With port 0
> > > +	 * it triggers the operating system to automatically search for
> > > +	 * and return a suitable available port in the TCP/IP dynamic
> > > +	 * port number range. This port range can be controlled by a
> > > +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
> > > +	 * which is also used by IPv6.
> > 
> > This looks too inspired from
> > https://www.lifewire.com/port-0-in-tcp-and-udp-818145
> 
>   Yep. You are right.
> > 
> > Let's make it simpler:
> > 
> >   * @port: Network port.
> >   *
> >   * It should be noted that port 0 passed to :manpage:`bind(2)` will
> >   * bind to an available port from a specific port range. This can be
> >   * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
> >   * sysctl (also used for IPv6). A Landlock rule with port 0 and the
> >   * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
> >   * on port 0 is allowed and it will automatically translate to binding
> >   * on the related port range.
>     Thanks.
> > 
> > > +	 */
> > > +	__u64 port;
> > > +};
> > > +
> > >  /**
> > >   * DOC: fs_access
> > >   *
> > > @@ -189,4 +226,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.
> > 
> > You can add:
> > "This is supported since ABI 4."
> 
>    Updated.
> > 
> > > + *
> > > + * 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 */
> > > diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
> > > index c1e862a38410..c4bf0d5eff39 100644
> > > --- a/security/landlock/Kconfig
> > > +++ b/security/landlock/Kconfig
> > > @@ -3,6 +3,7 @@
> > >  config SECURITY_LANDLOCK
> > >  	bool "Landlock support"
> > >  	depends on SECURITY
> > > +	select SECURITY_NETWORK
> > >  	select SECURITY_PATH
> > >  	help
> > >  	  Landlock is a sandboxing mechanism that enables processes to restrict
> > > diff --git a/security/landlock/Makefile b/security/landlock/Makefile
> > > index 7bbd2f413b3e..53d3c92ae22e 100644
> > > --- a/security/landlock/Makefile
> > > +++ b/security/landlock/Makefile
> > > @@ -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
> > > \ No newline at end of file
> > > diff --git a/security/landlock/limits.h b/security/landlock/limits.h
> > > index bafb3b8dc677..93c9c6f91556 100644
> > > --- a/security/landlock/limits.h
> > > +++ b/security/landlock/limits.h
> > > @@ -23,6 +23,11 @@
> > >  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
> > >  #define LANDLOCK_SHIFT_ACCESS_FS	0
> > > 
> > > +#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 */
> > > diff --git a/security/landlock/net.c b/security/landlock/net.c
> > > new file mode 100644
> > > index 000000000000..1bf26cf3c41b
> > > --- /dev/null
> > > +++ b/security/landlock/net.c
> > > @@ -0,0 +1,198 @@
> > > +// SPDX-License-Identifier: GPL-2.0-only
> > > +/*
> > > + * Landlock LSM - Network management and hooks
> > > + *
> > > + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
> > > + * Copyright © 2022-2023 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,
> > 
> > To be consistent with current_check_access_path(), please rename to
> > current_check_access_socket().
> 
>   Done. Thanks.
> > 
> > > +			       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();
> > 
> > For consistency with other functions, s/domain/dom/g
> 
>  Ok. Fixed.
> > 
> > > +
> > > +	if (!domain)
> > > +		return 0;
> > > +	if (WARN_ON_ONCE(domain->num_layers < 1))
> > > +		return -EACCES;
> > > +
> > > +	/* Checks if it's a (potential) TCP socket. */
> > > +	if (sock->type != SOCK_STREAM)
> > > +		return 0;
> > > +
> > > +	/* Checks for minimal header length to safely read sa_family. */
> > > +	if (addrlen < offsetofend(typeof(*address), sa_family))
> > > +		return -EINVAL;
> > > +
> > > +	switch (address->sa_family) {
> > > +	case AF_UNSPEC:
> > > +	case AF_INET:
> > > +		if (addrlen < sizeof(struct sockaddr_in))
> > > +			return -EINVAL;
> > > +		port = ((struct sockaddr_in *)address)->sin_port;
> > > +		break;
> > > +#if IS_ENABLED(CONFIG_IPV6)
> > > +	case AF_INET6:
> > > +		if (addrlen < SIN6_LEN_RFC2133)
> > > +			return -EINVAL;
> > > +		port = ((struct sockaddr_in6 *)address)->sin6_port;
> > > +		break;
> > > +#endif
> > 
> > #endif /* IS_ENABLED(CONFIG_INET) */
> 
>   #endif /* IS_ENABLED(CONFIG_IPV6) */ I suppose.

Indeed

> > 
> > > +	default:
> > > +		return 0;
> > > +	}
> > > +
> > > +	/* Specific AF_UNSPEC handling. */
> > > +	if (address->sa_family == AF_UNSPEC) {
> > > +		/*
> > > +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
> > > +		 * association, which have the same effect as closing the
> > > +		 * 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;
> > > +
> > > +		/*
> > > +		 * For compatibility reason, accept AF_UNSPEC for bind
> > > +		 * accesses (mapped to AF_INET) only if the address is
> > > +		 * 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;
> > > +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
> > > +
> > > +	rule = landlock_find_rule(domain, id);
> > > +	handled_access = landlock_init_layer_masks(
> > > +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
> > > +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
> > > +				   ARRAY_SIZE(layer_masks)))
> > > +		return 0;
> > > +
> > > +	return -EACCES;
> > > +}
> > > +
> > > +static int hook_socket_bind(struct socket *const sock,
> > > +			    struct sockaddr *const address, const int addrlen)
> > > +{
> > > +	return check_socket_access(sock, address, addrlen,
> > > +				   LANDLOCK_ACCESS_NET_BIND_TCP);
> > > +}
> > > +
> > > +static int hook_socket_connect(struct socket *const sock,
> > > +			       struct sockaddr *const address,
> > > +			       const int addrlen)
> > > +{
> > > +	return check_socket_access(sock, address, addrlen,
> > > +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
> > > +}
> > > +
> > > +static struct security_hook_list landlock_hooks[] __ro_after_init = {
> > > +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
> > > +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
> > > +};
> > > +
> > > +__init void landlock_add_net_hooks(void)
> > > +{
> > > +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
> > > +			   LANDLOCK_NAME);
> > > +}
> > > diff --git a/security/landlock/net.h b/security/landlock/net.h
> > > new file mode 100644
> > > index 000000000000..588a49fd6907
> > > --- /dev/null
> > > +++ b/security/landlock/net.h
> > > @@ -0,0 +1,33 @@
> > > +/* SPDX-License-Identifier: GPL-2.0-only */
> > > +/*
> > > + * Landlock LSM - Network management and hooks
> > > + *
> > > + * Copyright © 2022-2023 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)
> > > +{
> > > +}
> > > +
> > > +static inline int
> > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
> > > +			 access_mask_t access_rights);
> > > +{
> > > +	return -EAFNOSUPPORT;
> > > +}
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > > +#endif /* _SECURITY_LANDLOCK_NET_H */
> > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> > > index 4c209acee01e..1fe4298ff4a7 100644
> > > --- a/security/landlock/ruleset.c
> > > +++ b/security/landlock/ruleset.c
> > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > >  	refcount_set(&new_ruleset->usage, 1);
> > >  	mutex_init(&new_ruleset->lock);
> > >  	new_ruleset->root_inode = RB_ROOT;
> > > +
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	new_ruleset->root_net_port = RB_ROOT;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	new_ruleset->num_layers = num_layers;
> > >  	/*
> > >  	 * hierarchy = NULL
> > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > >  }
> > > 
> > >  struct landlock_ruleset *
> > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
> > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
> > > +			const access_mask_t net_access_mask)
> > >  {
> > >  	struct landlock_ruleset *new_ruleset;
> > > 
> > >  	/* Informs about useless ruleset. */
> > > -	if (!fs_access_mask)
> > > +	if (!fs_access_mask && !net_access_mask)
> > >  		return ERR_PTR(-ENOMSG);
> > >  	new_ruleset = create_ruleset(1);
> > > -	if (!IS_ERR(new_ruleset))
> > > +	if (IS_ERR(new_ruleset))
> > > +		return new_ruleset;
> > > +	if (fs_access_mask)
> > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > +	if (net_access_mask)
> > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > 
> > This is good, but it is not tested: we need to add a test that both
> > handle FS and net restrictions. You can add one in net.c, just handling
> > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> > rule with path_beneath (e.g. /dev) and another with net_port, and check
> > that open("/") is denied, open("/dev") is allowed, and and only the
> > allowed port is allowed with bind(). This test should be simple and can
> > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> > after port_endianness. fcntl.h should then be included by net.c
> 
>   Ok.
> > 
> > I guess that was the purpose of layout1.with_net (in fs_test.c) but it
> 
>   Yep. I added this kind of nest in fs_test.c to test both fs and network
> rules together.
> > is not complete. You can revamp this test and move it to net.c
> > following the above suggestions, keeping it consistent with other tests
> > in net.c . You don't need the test_open() nor create_ruleset() helpers.
> > 
> > This test must failed if we change "ruleset->access_masks[layer_level] |="
> > to "ruleset->access_masks[layer_level] =" in
> > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
> 
>   Do you want to change it? Why?

The kernel code is correct and must not be changed. However, if by
mistake we change it and remove the OR, a test should catch that. We
need a test to assert this assumption.

>   Fs and network masks are ORed to not intersect with each other.

Yes, they are ORed, and we need a test to check that. Noting is
currently testing this OR (and the different rule type consistency).
I'm suggesting to revamp the layout1.with_net test into
ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
addition of different types.

> > 
> > >  	return new_ruleset;
> > >  }
> > > 
> > > @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
> > >  	case LANDLOCK_KEY_INODE:
> > >  		return true;
> > > 
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	case LANDLOCK_KEY_NET_PORT:
> > > +		return false;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	default:
> > >  		WARN_ON_ONCE(1);
> > >  		return false;
> > > @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
> > >  	case LANDLOCK_KEY_INODE:
> > >  		return &ruleset->root_inode;
> > > 
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	case LANDLOCK_KEY_NET_PORT:
> > > +		return &ruleset->root_net_port;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	default:
> > > +		WARN_ON_ONCE(1);
> > 
> > Please move this WARN to the patch that added the previous and next
> > lines.
> 
>   OK. Will be moved.
> > 
> > >  		return ERR_PTR(-EINVAL);
> > >  	}
> > >  }
> > > @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
> > >  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
> > >  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
> > >  	BUILD_BUG_ON(access_masks <
> > > -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
> > > +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
> > > +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
> > >  }
> > > 
> > >  /**
> > > @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
> > >  	if (err)
> > >  		goto out_unlock;
> > > 
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	/* Merges the @src network port tree. */
> > > +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
> > > +	if (err)
> > > +		goto out_unlock;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  out_unlock:
> > >  	mutex_unlock(&src->lock);
> > >  	mutex_unlock(&dst->lock);
> > > @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
> > >  	if (err)
> > >  		goto out_unlock;
> > > 
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	/* Copies the @parent network port tree. */
> > > +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
> > > +	if (err)
> > > +		goto out_unlock;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
> > >  		err = -EINVAL;
> > >  		goto out_unlock;
> > > @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
> > >  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
> > >  					     node)
> > >  		free_rule(freeme, LANDLOCK_KEY_INODE);
> > > +
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	rbtree_postorder_for_each_entry_safe(freeme, next,
> > > +					     &ruleset->root_net_port, node)
> > > +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	put_hierarchy(ruleset->hierarchy);
> > >  	kfree(ruleset);
> > >  }
> > > @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
> > >   *
> > >   * @domain: The domain that defines the current restrictions.
> > >   * @access_request: The requested access rights to check.
> > > - * @layer_masks: The layer masks to populate.
> > > + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
> > 
> > "%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"
> 
>   Done.
> > 
> > > + * elements according to @key_type.
> > >   * @key_type: The key type to switch between access masks of different types.
> > >   *
> > >   * Returns: An access mask where each access right bit is set which is handled
> > > @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
> > >  		get_access_mask = landlock_get_fs_access_mask;
> > >  		num_access = LANDLOCK_NUM_ACCESS_FS;
> > >  		break;
> > > +
> > > +#if IS_ENABLED(CONFIG_INET)
> > > +	case LANDLOCK_KEY_NET_PORT:
> > > +		get_access_mask = landlock_get_net_access_mask;
> > > +		num_access = LANDLOCK_NUM_ACCESS_NET;
> > > +		break;
> > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > +
> > >  	default:
> > >  		WARN_ON_ONCE(1);
> > >  		return 0;
> > > diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
> > > index 1ede2b9a79b7..ba4a06035599 100644
> > > --- a/security/landlock/ruleset.h
> > > +++ b/security/landlock/ruleset.h
> > > @@ -33,13 +33,16 @@
> > >  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);
> > > +/* Makes sure all network access rights can be stored. */
> > > +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
> > >  /* 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;
> > > +typedef u32 access_masks_t;
> > >  /* Makes sure all ruleset access rights can be stored. */
> > > -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
> > > +static_assert(BITS_PER_TYPE(access_masks_t) >=
> > > +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
> > > 
> > >  typedef u16 layer_mask_t;
> > >  /* Makes sure all layers can be checked. */
> > > @@ -84,6 +87,11 @@ enum landlock_key_type {
> > >  	 * keys.
> > >  	 */
> > >  	LANDLOCK_KEY_INODE = 1,
> > > +	/**
> > > +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
> > > +	 * node keys.
> > > +	 */
> > > +	LANDLOCK_KEY_NET_PORT,
> > >  };
> > > 
> > >  /**
> > > @@ -158,6 +166,13 @@ struct landlock_ruleset {
> > >  	 * reaches zero.
> > >  	 */
> > >  	struct rb_root root_inode;
> > 
> > #if IS_ENABLED(CONFIG_INET)
>   OK. Done.
> > > +	/**
> > > +	 * @root_net_port: Root of a red-black tree containing &struct
> > > +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
> > > +	 * process (i.e. as a domain), this tree is immutable until @usage
> > > +	 * reaches zero.
> > > +	 */
> > > +	struct rb_root root_net_port;
> > 
> > #endif /* IS_ENABLED(CONFIG_INET) */
> 
>  Done.
> > 
> > >  	/**
> > >  	 * @hierarchy: Enables hierarchy identification even when a parent
> > >  	 * domain vanishes.  This is needed for the ptrace protection.
> > > @@ -196,13 +211,13 @@ struct landlock_ruleset {
> > >  			 */
> > >  			u32 num_layers;
> > >  			/**
> > > -			 * @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
> > > -			 * one.  These layers are used when merging rulesets,
> > > -			 * for user space backward compatibility (i.e.
> > > -			 * future-proof), and to properly handle merged
> > > +			 * @access_masks: Contains the subset of filesystem and
> > > +			 * network actions that are restricted by a ruleset.
> > > +			 * A domain saves all layers of merged rulesets in a
> > > +			 * stack (FAM), starting from the first layer to the
> > > +			 * last one.  These layers are used when merging
> > > +			 * rulesets, for user space backward compatibility
> > > +			 * (i.e. future-proof), and to properly handle merged
> > >  			 * rulesets without overlapping access rights.  These
> > >  			 * layers are set once and never changed for the
> > >  			 * lifetime of the ruleset.
> > > @@ -213,7 +228,8 @@ struct landlock_ruleset {
> > >  };
> > > 
> > >  struct landlock_ruleset *
> > > -landlock_create_ruleset(const access_mask_t access_mask);
> > > +landlock_create_ruleset(const access_mask_t access_mask_fs,
> > > +			const access_mask_t access_mask_net);
> > > 
> > >  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
> > >  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
> > > @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
> > >  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
> > >  }
> > > 
> > > +static inline void
> > > +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
> > > +			     const access_mask_t net_access_mask,
> > > +			     const u16 layer_level)
> > > +{
> > > +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
> > > +
> > > +	/* Should already be checked in sys_landlock_create_ruleset(). */
> > > +	WARN_ON_ONCE(net_access_mask != net_mask);
> > > +	ruleset->access_masks[layer_level] |=
> > > +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
> > > +}
> > > +
> > >  static inline access_mask_t
> > >  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
> > >  				const u16 layer_level)
> > > @@ -266,6 +295,16 @@ 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;
> > >  }
> > > +
> > > +static inline access_mask_t
> > > +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
> > > +			     const u16 layer_level)
> > > +{
> > > +	return (ruleset->access_masks[layer_level] >>
> > > +		LANDLOCK_SHIFT_ACCESS_NET) &
> > > +	       LANDLOCK_MASK_ACCESS_NET;
> > > +}
> > > +
> > >  bool landlock_unmask_layers(const struct landlock_rule *const rule,
> > >  			    const access_mask_t access_request,
> > >  			    layer_mask_t (*const layer_masks)[],
> > > diff --git a/security/landlock/setup.c b/security/landlock/setup.c
> > > index 0f6113528fa4..df81612811bf 100644
> > > --- a/security/landlock/setup.c
> > > +++ b/security/landlock/setup.c
> > > @@ -14,6 +14,7 @@
> > >  #include "fs.h"
> > >  #include "ptrace.h"
> > >  #include "setup.h"
> > > +#include "net.h"
> > > 
> > >  bool landlock_initialized __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;
> > > diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
> > > index 8a54e87dbb17..3ad652d9a146 100644
> > > --- a/security/landlock/syscalls.c
> > > +++ b/security/landlock/syscalls.c
> > > @@ -29,6 +29,7 @@
> > >  #include "cred.h"
> > >  #include "fs.h"
> > >  #include "limits.h"
> > > +#include "net.h"
> > >  #include "ruleset.h"
> > >  #include "setup.h"
> > > 
> > > @@ -74,7 +75,8 @@ static void build_check_abi(void)
> > >  {
> > >  	struct landlock_ruleset_attr ruleset_attr;
> > >  	struct landlock_path_beneath_attr path_beneath_attr;
> > > -	size_t ruleset_size, path_beneath_size;
> > > +	struct landlock_net_port_attr net_port_attr;
> > > +	size_t ruleset_size, path_beneath_size, net_port_size;
> > > 
> > >  	/*
> > >  	 * For each user space ABI structures, first checks that there is no
> > > @@ -82,13 +84,19 @@ static void build_check_abi(void)
> > >  	 * struct size.
> > >  	 */
> > >  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
> > > +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
> > >  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
> > > -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
> > > +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
> > > 
> > >  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
> > >  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
> > > +
> > > +	net_port_size = sizeof(net_port_attr.allowed_access);
> > > +	net_port_size += sizeof(net_port_attr.port);
> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
> > >  }
> > > 
> > >  /* Ruleset handling */
> > > @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
> > >  	.write = fop_dummy_write,
> > >  };
> > > 
> > > -#define LANDLOCK_ABI_VERSION 3
> > > +#define LANDLOCK_ABI_VERSION 4
> > > 
> > >  /**
> > >   * sys_landlock_create_ruleset - Create a new ruleset
> > > @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
> > >  	    LANDLOCK_MASK_ACCESS_FS)
> > >  		return -EINVAL;
> > > 
> > > +	/* Checks network content (and 32-bits cast). */
> > > +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
> > > +	    LANDLOCK_MASK_ACCESS_NET)
> > > +		return -EINVAL;
> > > +
> > >  	/* Checks arguments and transforms to kernel struct. */
> > > -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
> > > +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
> > > +					  ruleset_attr.handled_access_net);
> > >  	if (IS_ERR(ruleset))
> > >  		return PTR_ERR(ruleset);
> > > 
> > > @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
> > >  	int res, err;
> > >  	access_mask_t mask;
> > > 
> > > -	/* Copies raw user space buffer, only one type for now. */
> > > +	/* Copies raw user space buffer. */
> > 
> > Shouldn't this be part of a previous patch?
> 
> I did it according Gunter's suggestion
> https://lore.kernel.org/netdev/20230627.82cde73b1efe@gnoack.org/

Ok, that indeed makes more sense in this patch, please keep it.

> > 
> > >  	res = copy_from_user(&path_beneath_attr, rule_attr,
> > >  			     sizeof(path_beneath_attr));
> > >  	if (res)
> > > @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
> > >  	return err;
> > >  }
> > > 
> > > +static int add_rule_net_port(struct landlock_ruleset *ruleset,
> > > +			     const void __user *const rule_attr)
> > > +{
> > > +	struct landlock_net_port_attr net_port_attr;
> > > +	int res;
> > > +	access_mask_t mask;
> > > +
> > > +	/* Copies raw user space buffer. */
> > > +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
> > > +	if (res)
> > > +		return -EFAULT;
> > > +
> > > +	/*
> > > +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
> > > +	 * are ignored by network actions.
> > > +	 */
> > > +	if (!net_port_attr.allowed_access)
> > > +		return -ENOMSG;
> > > +
> > > +	/*
> > > +	 * Checks that allowed_access matches the @ruleset constraints
> > > +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
> > > +	 */
> > > +	mask = landlock_get_net_access_mask(ruleset, 0);
> > > +	if ((net_port_attr.allowed_access | mask) != mask)
> > > +		return -EINVAL;
> > > +
> > > +	/* Denies inserting a rule with port higher than 65535. */
> > 
> > For consistency with the following comment:
> > "Denies inserting a rule with port greater than 65535."
> > 
>   Done. Thanks.
> > 
> > > +	if (net_port_attr.port > U16_MAX)
> > > +		return -EINVAL;
> > > +
> > > +	/* Imports the new rule. */
> > > +	return landlock_append_net_rule(ruleset, net_port_attr.port,
> > > +					net_port_attr.allowed_access);
> > > +}
> > > +
> > >  /**
> > >   * sys_landlock_add_rule - Add a new rule to a ruleset
> > >   *
> > >   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
> > >   *		with the new rule.
> > > - * @rule_type: Identify the structure type pointed to by @rule_attr (only
> > > - *             %LANDLOCK_RULE_PATH_BENEATH for now).
> > > + * @rule_type: Identify the structure type pointed to by @rule_attr:
> > > + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
> > >   * @rule_attr: Pointer to a rule (only of type &struct
> > >   *             landlock_path_beneath_attr for now).
> > >   * @flags: Must be 0.
> > > @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
> > >   * Possible returned errors are:
> > >   *
> > >   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
> > > + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
> > 
> > %LANDLOCK_RULE_NET_PORT
> 
>  Done.
> > 
> > > + *   supported by the running kernel;
> > >   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
> > >   *   &landlock_path_beneath_attr.allowed_access is not a subset of the
> > 
> > &landlock_path_beneath_attr.allowed_access or
> > &landlock_net_port_attr.allowed_access is not a subset of the
> 
>   Fixed. Thanks.
> > 
> > >   *   ruleset handled accesses);
> > 
> > EINVAL description needs to be updated, especially for port > U16_MAX:
> > - *   ruleset handled accesses);
> > + *   ruleset handled accesses), or &landlock_net_port_attr.port is
> > +     greater than 65535;
> 
>  Done. Thanks.
> > 
> > 
> > > @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
> > >  	case LANDLOCK_RULE_PATH_BENEATH:
> > >  		err = add_rule_path_beneath(ruleset, rule_attr);
> > >  		break;
> > > +	case LANDLOCK_RULE_NET_PORT:
> > > +		err = add_rule_net_port(ruleset, rule_attr);
> > > +		break;
> > >  	default:
> > >  		err = -EINVAL;
> > >  		break;
> > > diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
> > > index 792c3f0a59b4..646f778dfb1e 100644
> > > --- a/tools/testing/selftests/landlock/base_test.c
> > > +++ b/tools/testing/selftests/landlock/base_test.c
> > > @@ -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
> > > 
> > .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-18 12:32   ` Mickaël Salaün
@ 2023-10-20 11:41     ` Konstantin Meskhidze (A)
  2023-10-20 15:40       ` Mickaël Salaün
  0 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20 11:41 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:32 PM, Mickaël Salaün пишет:
> You can update the subject with:
> "selftests/landlock: Add network tests"

Ok.
> 
> On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze wrote:
>> These test suites try to check edge cases for TCP sockets
>> bind() and connect() actions.
> 
> You can replace with that:
> Add 77 test suites to check edge cases related to bind() and connect()
> actions. They are defined with 6 fixtures and their variants:
> 
>> 
>> protocol:
>> * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
> 
> As you already did, you can write one paragraph per fixture, but
> starting by explaining the fixture and its related variants, and then
> listing the tests and explaining their specificities. For instance:
> 
> The "protocol" fixture is extended with 12 variants defined as a matrix
> of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
> stream/datagram socket. 4 related tests suites are defined:
> * bind: Test bind combinations with increasingly more
>    restricting domains.
> * connect: Test connect combinations with increasingly more
>    restricting domains.
> ...

   Ok. Will be updated.
> 
> s/ipv/IPv/g

   Got it. Thanks.
> 
>> * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and unix
>> sockets.
>> * bind_unspec: Tests with non-landlocked/landlocked restrictions
>> for bind action with AF_UNSPEC socket family.
>> * connect_unspec: Tests with non-landlocked/landlocked restrictions
>> for connect action with AF_UNSPEC socket family.
>> 
>> ipv4:
>> * from_unix_to_inet: Tests to make sure unix sockets' actions are not
>> restricted by Landlock rules applied to TCP ones.
>> 
>> tcp_layers:
>> * ruleset_overlap.
>> * ruleset_expand.
>> 
>> mini:
>> * network_access_rights: Tests with  legitimate access values.
>> * unknown_access_rights: Tests with invalid attributes, out of access range.
>> * inval:
>>     - unhandled allowed access.
>>     - zero access value.
>> * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
>> 
>> ipv4_tcp:
>> * port_endianness: Tests with big/little endian port formats.
>> 
>> port_specific:
>> * bind_connect: Tests with specific port values.
>> 
>> layout1:
>> * with_net: Tests with network bind() socket action within
>> filesystem directory access test.
>> 
>> Test coverage for security/landlock is 94.5% of 932 lines according
>> to gcc/gcov-11.
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
>> Co-developed-by:: Mickaël Salaün <mic@digikod.net>
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * Renames port_zero to port_specific fixture.
>> * Refactors port_specific test:
>>     - Adds set_port() and get_binded_port() helpers.
>>     - Adds checks for port 0, allowed by Landlock in this version.
>>     - Adds checks for port 1023.
>> * Refactors commit message.
>> 
> 
>> +static void set_port(struct service_fixture *const srv, in_port_t port)
>> +{
>> +	switch (srv->protocol.domain) {
>> +	case AF_UNSPEC:
>> +	case AF_INET:
>> +		srv->ipv4_addr.sin_port = port;
> 
> We should call htons() here, and make port a uint16_t.

   Done.
> 
>> +		return;
>> +
>> +	case AF_INET6:
>> +		srv->ipv6_addr.sin6_port = port;
>> +		return;
>> +
>> +	default:
>> +		return;
>> +	}
>> +}
>> +
>> +static in_port_t get_binded_port(int socket_fd,
> 
> The returned type should be uint16_t (i.e. host endianess).

   Done.
> 
>> +				 const struct protocol_variant *const prot)
>> +{
>> +	struct sockaddr_in ipv4_addr;
>> +	struct sockaddr_in6 ipv6_addr;
>> +	socklen_t ipv4_addr_len, ipv6_addr_len;
>> +
>> +	/* Gets binded port. */
>> +	switch (prot->domain) {
>> +	case AF_UNSPEC:
>> +	case AF_INET:
>> +		ipv4_addr_len = sizeof(ipv4_addr);
>> +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
>> +		return ntohs(ipv4_addr.sin_port);
>> +
>> +	case AF_INET6:
>> +		ipv6_addr_len = sizeof(ipv6_addr);
>> +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
>> +		return ntohs(ipv6_addr.sin6_port);
>> +
>> +	default:
>> +		return 0;
>> +	}
>> +}
> 
> These are good helpers!
> 
> 
>> +FIXTURE_TEARDOWN(ipv4)
>> +{
>> +}
>> +
>> +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
> 
> No FIXME should remain.

   Ok. Deleted.
> 
>> +TEST_F(ipv4, from_unix_to_inet)
> 
>> +TEST_F(mini, network_access_rights)
>> +{
>> +	const struct landlock_ruleset_attr ruleset_attr = {
>> +		.handled_access_net = ACCESS_ALL,
>> +	};
>> +	struct landlock_net_port_attr net_service = {
> 
> Please rename to "net_port" everywhere.

   Done.
> 
>> +TEST_F(port_specific, bind_connect)
>> +{
>> +	int socket_fd, ret;
>> +
>> +	/* Adds the first rule layer with bind and connect actions. */
>> +	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_port_attr tcp_bind_connect_zero = {
>> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
>> +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
>> +			.port = htons(0),
> 
> We don't need any htons() calls anymore. It doesn't change the 0 value
> in this case but this is not correct.

  Yep. We call htons(port) in landlock_append_net_rule().
  Thanks.
> 
>> +		};
>> +
> 
> Useless new line.

   Ok. Thanks.
> 
>> +		int ruleset_fd;
>> +
>> +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
>> +						     sizeof(ruleset_attr), 0);
>> +		ASSERT_LE(0, ruleset_fd);
>> +
>> +		/* Checks zero port value on bind and connect actions. */
>> +		EXPECT_EQ(0,
>> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> +					    &tcp_bind_connect_zero, 0));
>> +
>> +		enforce_ruleset(_metadata, ruleset_fd);
>> +		EXPECT_EQ(0, close(ruleset_fd));
>> +	}
>> +
>> +	socket_fd = socket_variant(&self->srv0);
>> +	ASSERT_LE(0, socket_fd);
>> +
>> +	/* Sets address port to 0 for both protocol families. */
>> +	set_port(&self->srv0, htons(0));
> 
> ditto
> 
>> +
>> +	/* Binds on port 0. */
>> +	ret = bind_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
>> +	} else {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
> 
> If the results are the same, no need to add an if block.

   Right. Updated.
> 
>> +	}
>> +
>> +	/* Connects on port 0. */
>> +	ret = connect_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		EXPECT_EQ(-ECONNREFUSED, ret);
>> +	} else {
>> +		EXPECT_EQ(-ECONNREFUSED, ret);
>> +	}
> 
> ditto
> 
  Updated.
>> +
>> +	/* Binds on port 0. */
> 
> Please close sockets once they are used, and recreate one for another
> bind/connect to avoid wrong checks.

   Ok. But I can reuse socket_fd after closeing a socket. Correct?
> 
>> +	ret = bind_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
>> +	} else {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
>> +	}
> 
> Why this second bind() block? Furthermore, it is using the same
> socket_fd.

   I will refactor the code this way -  sockets will be recreated for 
each bind/connect, and I prefer to use self-connected sockets (use one 
socket descriptor) in these tests to make code simpler; testing logic 
remains the same way as if we have 2 sockets.

What do you think???

> 
>> +
>> +	/* Sets binded port for both protocol families. */
>> +	set_port(&self->srv0,
>> +		 htons(get_binded_port(socket_fd, &variant->prot)));
> 
> Ditto, these two endianess translations are useless.

   Updated. Thanks.
> 
> You can also add this to make sure the returned port is not 0:
> port = get_binded_port(socket_fd, &variant->prot);
> EXPECT_NE(0, port);
> set_port(&self->srv0, port);

   Ok. Thanks for the tip.
> 
>> +
>> +	/* Connects on the binded port. */
>> +	ret = connect_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		/* Denied by Landlock. */
>> +		EXPECT_EQ(-EACCES, ret);
>> +	} else {
>> +		EXPECT_EQ(0, ret);
>> +	}
>> +
>> +	EXPECT_EQ(0, close(socket_fd));
>> +
> 
> 
> 
>> +	/* Adds the second rule layer with just bind action. */
> 
> There is not only bind actions here.

   Right.
> 
> This second part of the tests should be in a dedicated
> TEST_F(port_specific, bind_1023).

   Got it.
> 
>> +	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_port_attr tcp_bind_zero = {
>> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> +			.port = htons(0),
>> +		};
>> +
> 
> Useless new lines.

   Got it.
> 
>> +		/* A rule with port value less than 1024. */
>> +		const struct landlock_net_port_attr tcp_bind_lower_range = {
>> +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> +			.port = htons(1023),
>> +		};
>> +
> 
> Useless new line.

   Got it.
> 
>> +		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_PORT,
>> +					    &tcp_bind_lower_range, 0));
>> +		ASSERT_EQ(0,
>> +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> +					    &tcp_bind_zero, 0));
>> +
>> +		enforce_ruleset(_metadata, ruleset_fd);
>> +		EXPECT_EQ(0, close(ruleset_fd));
>> +	}
>> +
>> +	socket_fd = socket_variant(&self->srv0);
> 
> We must have one socket FD dedicated to bind an another dedicated to
> connect, e.g. bind_fd and connect_fd, an close them after each use,
> otherwise tests might be inconsistent.

   Why can't we use self-connected sockets here? Why tests might be 
inconsistent? Tests will be working the same way as if we have 2 
sockets, plus the code is simpler.
> 
>> +	ASSERT_LE(0, socket_fd);
>> +
>> +	/* Sets address port to 1023 for both protocol families. */
>> +	set_port(&self->srv0, htons(1023));
>> +
>> +	/* Binds on port 1023. */
>> +	ret = bind_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
> 
> No need to add this check if the result is the same for sandboxed and
> not sandboxed tests.

  Ok. Thanks.
> 
> Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and clear_cap()
> around this bind_variant() to make this test useful.
> 
> You will also need to patch common.h like this:
> @@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
>          cap_t cap_p;
>          /* Only these three capabilities are useful for the tests. */
>          const cap_value_t caps[] = {
> +               /* clang-format off */
>                  CAP_DAC_OVERRIDE,
>                  CAP_MKNOD,
>                  CAP_SYS_ADMIN,
>                  CAP_SYS_CHROOT,
> +               CAP_NET_BIND_SERVICE,
> +               /* clang-format on */
>          };

  OK. Thanks.
> 
>> +		/* Denied by the system. */
>> +		EXPECT_EQ(-EACCES, ret);
>> +	} else {
>> +		/* Denied by the system. */
>> +		EXPECT_EQ(-EACCES, ret);
>> +	}
>> +
> 
> I don't see why the following part is useful. Why did you add it?
   Binding to ports < 1024 are forbidden by the system, not by Landlock.
   I added a rule with port 1023 to make sure it works as expected.

> Why tcp_bind_zero?
    Beacause it's a bind action with port zero rule.

> 
> The other parts are good though!
> 
>> +	/* Sets address port to 0 for both protocol families. */
>> +	set_port(&self->srv0, htons(0));
>> +
>> +	/* Binds on port 0. */
>> +	ret = bind_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
>> +	} else {
>> +		/* Binds to a random port within ip_local_port_range. */
>> +		EXPECT_EQ(0, ret);
>> +	}
>> +
>> +	/* Sets binded port for both protocol families. */
>> +	set_port(&self->srv0,
>> +		 htons(get_binded_port(socket_fd, &variant->prot)));
>> +
>> +	/* Connects on the binded port. */
>> +	ret = connect_variant(socket_fd, &self->srv0);
>> +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> +		/* Denied by Landlock. */
>> +		EXPECT_EQ(-EACCES, ret);
>> +	} else {
>> +		EXPECT_EQ(0, ret);
>> +	}
>> +
>> +	EXPECT_EQ(0, close(socket_fd));
>> +}
>> +
>> +TEST_HARNESS_MAIN
>> --
>> 2.25.1
>> 
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20  9:49       ` Mickaël Salaün
@ 2023-10-20 11:58         ` Konstantin Meskhidze (A)
  2023-10-20 15:41           ` Mickaël Salaün
  2023-10-24  2:51         ` Konstantin Meskhidze (A)
  2023-10-24  3:18         ` Konstantin Meskhidze (A)
  2 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20 11:58 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/20/2023 12:49 PM, Mickaël Salaün пишет:
> On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> > > This commit adds network rules support in the ruleset management
>> > 
>> > Here are some advices to better write commit messages:
>> > https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
>> > The "Describe your changes in imperative mood" part is important for
>> > this commit and others. Most of this patch series' commit messages need
>> > small updates.
>> 
>>  Ok. I will refactor commit messages with "imperative mood". Thanks.
>> > 
>> > > 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
>> > 
>> > Please explain the "why" (when it makes sense) instead of just listing
>> > the "what".
>> 
>>   Ok.
>> 
>> > 
>> > > rights can be stored. Implement socket_bind() and socket_connect()
>> > > LSM hooks, which enables to restrict TCP socket binding and connection
>> > > to specific ports.
>> > 
>> > I reworded and moved this part in last:
>> > > For the file system, a file descriptor is a direct access to a file/data.
>> > > But for the network, it's impossible to identify for which data/peer a
>> > > newly created socket will give access to, it's needed to wait for a
>> > > connect or bind request to identify the use case for this socket.
>> > > That's why the access rights (related to ports) are tied to an opened
>> > > socket, but this would not align with the way Landlock access control
>> > > works for the filesystem [2].
>> 
>>    Thanks.
>> > 
>> > Please add empty line to split paragraphs.
>> 
>>   Got it.
>> > 
>> > > 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 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).
>> > 
>> > For the file system, a file descriptor is a direct access to a file/data.
>> > However, for network sockets, we cannot identify for which data or peer a newly
>> > created socket will give access to. Indeed, we need to wait for a connect or
>> > bind request to identify the use case for this socket.
>> > 
>> > Access rights are not tied to socket file descriptors. Instead, bind and
>> > connect actions are controlled by the task's domain.  As for the filesystem, a
>> > directory file descriptor may enable to open another file (i.e. a new data
>> > item), but this opening is restricted by the task's domain, not the file
>> > descriptor's access rights [2].
>> > 
>> > > 
>> > > [1]
>> > > https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > > [2]
>> > > https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>> > 
>> > [1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > [2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>>   Thanks.
>> > 
>> > > 
>> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> > > Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
>> > > [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
>> > > to add_rule_net_service()]
>> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> > > ---
>> > > 
>> > > Changes since v12:
>> > > * Moves add_rule_net_port() back in syscalls.c and makes it static.
>> > > * Deletes bind_access_mask allowing bind action rule on port 0.
>> > > * Adds comment about port 0 in landlock_net_port_attr structure.
>> > > * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > Changes since v11:
>> > > * Replaces dates with "2022-2023" in net.c/h files headers.
>> > > * Removes WARN_ON_ONCE(!domain) in check_socket_access().
>> > > * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
>> > > * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
>> > > * Renames landlock_net_service_attr to landlock_net_port_attr.
>> > > * Defines two add_rule_net_service() functions according to
>> > >   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>> > >   function.
>> > > * Adds af_family consistency check while handling AF_UNSPEC specifically.
>> > > * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>> > >   action on port zero.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > 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() and
>> > >   get_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                |  56 ++++++
>> > >  security/landlock/Kconfig                    |   1 +
>> > >  security/landlock/Makefile                   |   2 +
>> > >  security/landlock/limits.h                   |   5 +
>> > >  security/landlock/net.c                      | 198 +++++++++++++++++++
>> > >  security/landlock/net.h                      |  33 ++++
>> > >  security/landlock/ruleset.c                  |  62 +++++-
>> > >  security/landlock/ruleset.h                  |  59 +++++-
>> > >  security/landlock/setup.c                    |   2 +
>> > >  security/landlock/syscalls.c                 |  69 ++++++-
>> > >  tools/testing/selftests/landlock/base_test.c |   2 +-
>> > >  11 files changed, 466 insertions(+), 23 deletions(-)
>> > >  create mode 100644 security/landlock/net.c
>> > >  create mode 100644 security/landlock/net.h
>> > > 
>> > > diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
>> > > index 81d09ef9aa50..25349666b19e 100644
>> > > --- a/include/uapi/linux/landlock.h
>> > > +++ b/include/uapi/linux/landlock.h
>> > > @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>> > >  	 * landlock_path_beneath_attr .
>> > >  	 */
>> > >  	LANDLOCK_RULE_PATH_BENEATH = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
>> > > +	 * landlock_net_port_attr .
>> > > +	 */
>> > > +	LANDLOCK_RULE_NET_PORT = 2,
>> > 
>> > We don't need the explicit " = 2".
>> 
>>   Fixed. Thanks.
>> > 
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>> > >  	 */
>> > >  } __attribute__((packed));
>> > > 
>> > > +/**
>> > > + * struct landlock_net_port_attr - Network port definition
>> > > + *
>> > > + * Argument of sys_landlock_add_rule().
>> > > + */
>> > > +struct landlock_net_port_attr {
>> > > +	/**
>> > > +	 * @allowed_access: Bitmask of allowed access network for a port
>> > > +	 * (cf. `Network flags`_).
>> > > +	 */
>> > > +	__u64 allowed_access;
>> > > +	/**
>> > > +	 * @port: Network port. Landlock does not forbid rules with port 0,
>> > > +	 * since some network services use it. Port 0 is a reserved one in
>> > > +	 * TCP/IP networking, meaning that it should not be used in TCP or
>> > > +	 * UDP messages. To allocate its source port number, services call
>> > > +	 * TCP/IP network functions like bind() to request one. With port 0
>> > > +	 * it triggers the operating system to automatically search for
>> > > +	 * and return a suitable available port in the TCP/IP dynamic
>> > > +	 * port number range. This port range can be controlled by a
>> > > +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
>> > > +	 * which is also used by IPv6.
>> > 
>> > This looks too inspired from
>> > https://www.lifewire.com/port-0-in-tcp-and-udp-818145
>> 
>>   Yep. You are right.
>> > 
>> > Let's make it simpler:
>> > 
>> >   * @port: Network port.
>> >   *
>> >   * It should be noted that port 0 passed to :manpage:`bind(2)` will
>> >   * bind to an available port from a specific port range. This can be
>> >   * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
>> >   * sysctl (also used for IPv6). A Landlock rule with port 0 and the
>> >   * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
>> >   * on port 0 is allowed and it will automatically translate to binding
>> >   * on the related port range.
>>     Thanks.
>> > 
>> > > +	 */
>> > > +	__u64 port;
>> > > +};
>> > > +
>> > >  /**
>> > >   * DOC: fs_access
>> > >   *
>> > > @@ -189,4 +226,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.
>> > 
>> > You can add:
>> > "This is supported since ABI 4."
>> 
>>    Updated.
>> > 
>> > > + *
>> > > + * 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 */
>> > > diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
>> > > index c1e862a38410..c4bf0d5eff39 100644
>> > > --- a/security/landlock/Kconfig
>> > > +++ b/security/landlock/Kconfig
>> > > @@ -3,6 +3,7 @@
>> > >  config SECURITY_LANDLOCK
>> > >  	bool "Landlock support"
>> > >  	depends on SECURITY
>> > > +	select SECURITY_NETWORK
>> > >  	select SECURITY_PATH
>> > >  	help
>> > >  	  Landlock is a sandboxing mechanism that enables processes to restrict
>> > > diff --git a/security/landlock/Makefile b/security/landlock/Makefile
>> > > index 7bbd2f413b3e..53d3c92ae22e 100644
>> > > --- a/security/landlock/Makefile
>> > > +++ b/security/landlock/Makefile
>> > > @@ -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
>> > > \ No newline at end of file
>> > > diff --git a/security/landlock/limits.h b/security/landlock/limits.h
>> > > index bafb3b8dc677..93c9c6f91556 100644
>> > > --- a/security/landlock/limits.h
>> > > +++ b/security/landlock/limits.h
>> > > @@ -23,6 +23,11 @@
>> > >  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>> > >  #define LANDLOCK_SHIFT_ACCESS_FS	0
>> > > 
>> > > +#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 */
>> > > diff --git a/security/landlock/net.c b/security/landlock/net.c
>> > > new file mode 100644
>> > > index 000000000000..1bf26cf3c41b
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.c
>> > > @@ -0,0 +1,198 @@
>> > > +// SPDX-License-Identifier: GPL-2.0-only
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
>> > > + * Copyright © 2022-2023 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,
>> > 
>> > To be consistent with current_check_access_path(), please rename to
>> > current_check_access_socket().
>> 
>>   Done. Thanks.
>> > 
>> > > +			       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();
>> > 
>> > For consistency with other functions, s/domain/dom/g
>> 
>>  Ok. Fixed.
>> > 
>> > > +
>> > > +	if (!domain)
>> > > +		return 0;
>> > > +	if (WARN_ON_ONCE(domain->num_layers < 1))
>> > > +		return -EACCES;
>> > > +
>> > > +	/* Checks if it's a (potential) TCP socket. */
>> > > +	if (sock->type != SOCK_STREAM)
>> > > +		return 0;
>> > > +
>> > > +	/* Checks for minimal header length to safely read sa_family. */
>> > > +	if (addrlen < offsetofend(typeof(*address), sa_family))
>> > > +		return -EINVAL;
>> > > +
>> > > +	switch (address->sa_family) {
>> > > +	case AF_UNSPEC:
>> > > +	case AF_INET:
>> > > +		if (addrlen < sizeof(struct sockaddr_in))
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in *)address)->sin_port;
>> > > +		break;
>> > > +#if IS_ENABLED(CONFIG_IPV6)
>> > > +	case AF_INET6:
>> > > +		if (addrlen < SIN6_LEN_RFC2133)
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in6 *)address)->sin6_port;
>> > > +		break;
>> > > +#endif
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>   #endif /* IS_ENABLED(CONFIG_IPV6) */ I suppose.
> 
> Indeed
> 
>> > 
>> > > +	default:
>> > > +		return 0;
>> > > +	}
>> > > +
>> > > +	/* Specific AF_UNSPEC handling. */
>> > > +	if (address->sa_family == AF_UNSPEC) {
>> > > +		/*
>> > > +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
>> > > +		 * association, which have the same effect as closing the
>> > > +		 * 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;
>> > > +
>> > > +		/*
>> > > +		 * For compatibility reason, accept AF_UNSPEC for bind
>> > > +		 * accesses (mapped to AF_INET) only if the address is
>> > > +		 * 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;
>> > > +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
>> > > +
>> > > +	rule = landlock_find_rule(domain, id);
>> > > +	handled_access = landlock_init_layer_masks(
>> > > +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
>> > > +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
>> > > +				   ARRAY_SIZE(layer_masks)))
>> > > +		return 0;
>> > > +
>> > > +	return -EACCES;
>> > > +}
>> > > +
>> > > +static int hook_socket_bind(struct socket *const sock,
>> > > +			    struct sockaddr *const address, const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_BIND_TCP);
>> > > +}
>> > > +
>> > > +static int hook_socket_connect(struct socket *const sock,
>> > > +			       struct sockaddr *const address,
>> > > +			       const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
>> > > +}
>> > > +
>> > > +static struct security_hook_list landlock_hooks[] __ro_after_init = {
>> > > +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
>> > > +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
>> > > +};
>> > > +
>> > > +__init void landlock_add_net_hooks(void)
>> > > +{
>> > > +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
>> > > +			   LANDLOCK_NAME);
>> > > +}
>> > > diff --git a/security/landlock/net.h b/security/landlock/net.h
>> > > new file mode 100644
>> > > index 000000000000..588a49fd6907
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.h
>> > > @@ -0,0 +1,33 @@
>> > > +/* SPDX-License-Identifier: GPL-2.0-only */
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 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)
>> > > +{
>> > > +}
>> > > +
>> > > +static inline int
>> > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> > > +			 access_mask_t access_rights);
>> > > +{
>> > > +	return -EAFNOSUPPORT;
>> > > +}
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > > +#endif /* _SECURITY_LANDLOCK_NET_H */
>> > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > --- a/security/landlock/ruleset.c
>> > > +++ b/security/landlock/ruleset.c
>> > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  	refcount_set(&new_ruleset->usage, 1);
>> > >  	mutex_init(&new_ruleset->lock);
>> > >  	new_ruleset->root_inode = RB_ROOT;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	new_ruleset->num_layers = num_layers;
>> > >  	/*
>> > >  	 * hierarchy = NULL
>> > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  }
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > +			const access_mask_t net_access_mask)
>> > >  {
>> > >  	struct landlock_ruleset *new_ruleset;
>> > > 
>> > >  	/* Informs about useless ruleset. */
>> > > -	if (!fs_access_mask)
>> > > +	if (!fs_access_mask && !net_access_mask)
>> > >  		return ERR_PTR(-ENOMSG);
>> > >  	new_ruleset = create_ruleset(1);
>> > > -	if (!IS_ERR(new_ruleset))
>> > > +	if (IS_ERR(new_ruleset))
>> > > +		return new_ruleset;
>> > > +	if (fs_access_mask)
>> > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > +	if (net_access_mask)
>> > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > 
>> > This is good, but it is not tested: we need to add a test that both
>> > handle FS and net restrictions. You can add one in net.c, just handling
>> > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > that open("/") is denied, open("/dev") is allowed, and and only the
>> > allowed port is allowed with bind(). This test should be simple and can
>> > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > after port_endianness. fcntl.h should then be included by net.c
>> 
>>   Ok.
>> > 
>> > I guess that was the purpose of layout1.with_net (in fs_test.c) but it
>> 
>>   Yep. I added this kind of nest in fs_test.c to test both fs and network
>> rules together.
>> > is not complete. You can revamp this test and move it to net.c
>> > following the above suggestions, keeping it consistent with other tests
>> > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > 
>> > This test must failed if we change "ruleset->access_masks[layer_level] |="
>> > to "ruleset->access_masks[layer_level] =" in
>> > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> 
>>   Do you want to change it? Why?
> 
> The kernel code is correct and must not be changed. However, if by
> mistake we change it and remove the OR, a test should catch that. We
> need a test to assert this assumption.
> 
   OK. I will add additional assert simulating 
"ruleset->access_masks[layer_level] =" kernel code.
>>   Fs and network masks are ORed to not intersect with each other.
> 
> Yes, they are ORed, and we need a test to check that. Noting is
> currently testing this OR (and the different rule type consistency).
> I'm suggesting to revamp the layout1.with_net test into
> ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> addition of different types.

   I will move layout1.with_net test into net.c and rename it. Looks like
   it just needed to add "ruleset->access_masks[layer_level] =" assert
   because the test already has rule addition with different types.

   Do you have any more review updates so far?
> 
>> > 
>> > >  	return new_ruleset;
>> > >  }
>> > > 
>> > > @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return true;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return false;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return false;
>> > > @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return &ruleset->root_inode;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return &ruleset->root_net_port;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > > +		WARN_ON_ONCE(1);
>> > 
>> > Please move this WARN to the patch that added the previous and next
>> > lines.
>> 
>>   OK. Will be moved.
>> > 
>> > >  		return ERR_PTR(-EINVAL);
>> > >  	}
>> > >  }
>> > > @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>> > >  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>> > >  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>> > >  	BUILD_BUG_ON(access_masks <
>> > > -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
>> > > +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
>> > > +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>> > >  }
>> > > 
>> > >  /**
>> > > @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Merges the @src network port tree. */
>> > > +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  out_unlock:
>> > >  	mutex_unlock(&src->lock);
>> > >  	mutex_unlock(&dst->lock);
>> > > @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Copies the @parent network port tree. */
>> > > +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>> > >  		err = -EINVAL;
>> > >  		goto out_unlock;
>> > > @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>> > >  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>> > >  					     node)
>> > >  		free_rule(freeme, LANDLOCK_KEY_INODE);
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	rbtree_postorder_for_each_entry_safe(freeme, next,
>> > > +					     &ruleset->root_net_port, node)
>> > > +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	put_hierarchy(ruleset->hierarchy);
>> > >  	kfree(ruleset);
>> > >  }
>> > > @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>> > >   *
>> > >   * @domain: The domain that defines the current restrictions.
>> > >   * @access_request: The requested access rights to check.
>> > > - * @layer_masks: The layer masks to populate.
>> > > + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
>> > 
>> > "%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"
>> 
>>   Done.
>> > 
>> > > + * elements according to @key_type.
>> > >   * @key_type: The key type to switch between access masks of different types.
>> > >   *
>> > >   * Returns: An access mask where each access right bit is set which is handled
>> > > @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>> > >  		get_access_mask = landlock_get_fs_access_mask;
>> > >  		num_access = LANDLOCK_NUM_ACCESS_FS;
>> > >  		break;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		get_access_mask = landlock_get_net_access_mask;
>> > > +		num_access = LANDLOCK_NUM_ACCESS_NET;
>> > > +		break;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return 0;
>> > > diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
>> > > index 1ede2b9a79b7..ba4a06035599 100644
>> > > --- a/security/landlock/ruleset.h
>> > > +++ b/security/landlock/ruleset.h
>> > > @@ -33,13 +33,16 @@
>> > >  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);
>> > > +/* Makes sure all network access rights can be stored. */
>> > > +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>> > >  /* 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;
>> > > +typedef u32 access_masks_t;
>> > >  /* Makes sure all ruleset access rights can be stored. */
>> > > -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
>> > > +static_assert(BITS_PER_TYPE(access_masks_t) >=
>> > > +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
>> > > 
>> > >  typedef u16 layer_mask_t;
>> > >  /* Makes sure all layers can be checked. */
>> > > @@ -84,6 +87,11 @@ enum landlock_key_type {
>> > >  	 * keys.
>> > >  	 */
>> > >  	LANDLOCK_KEY_INODE = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
>> > > +	 * node keys.
>> > > +	 */
>> > > +	LANDLOCK_KEY_NET_PORT,
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -158,6 +166,13 @@ struct landlock_ruleset {
>> > >  	 * reaches zero.
>> > >  	 */
>> > >  	struct rb_root root_inode;
>> > 
>> > #if IS_ENABLED(CONFIG_INET)
>>   OK. Done.
>> > > +	/**
>> > > +	 * @root_net_port: Root of a red-black tree containing &struct
>> > > +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
>> > > +	 * process (i.e. as a domain), this tree is immutable until @usage
>> > > +	 * reaches zero.
>> > > +	 */
>> > > +	struct rb_root root_net_port;
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>  Done.
>> > 
>> > >  	/**
>> > >  	 * @hierarchy: Enables hierarchy identification even when a parent
>> > >  	 * domain vanishes.  This is needed for the ptrace protection.
>> > > @@ -196,13 +211,13 @@ struct landlock_ruleset {
>> > >  			 */
>> > >  			u32 num_layers;
>> > >  			/**
>> > > -			 * @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
>> > > -			 * one.  These layers are used when merging rulesets,
>> > > -			 * for user space backward compatibility (i.e.
>> > > -			 * future-proof), and to properly handle merged
>> > > +			 * @access_masks: Contains the subset of filesystem and
>> > > +			 * network actions that are restricted by a ruleset.
>> > > +			 * A domain saves all layers of merged rulesets in a
>> > > +			 * stack (FAM), starting from the first layer to the
>> > > +			 * last one.  These layers are used when merging
>> > > +			 * rulesets, for user space backward compatibility
>> > > +			 * (i.e. future-proof), and to properly handle merged
>> > >  			 * rulesets without overlapping access rights.  These
>> > >  			 * layers are set once and never changed for the
>> > >  			 * lifetime of the ruleset.
>> > > @@ -213,7 +228,8 @@ struct landlock_ruleset {
>> > >  };
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t access_mask);
>> > > +landlock_create_ruleset(const access_mask_t access_mask_fs,
>> > > +			const access_mask_t access_mask_net);
>> > > 
>> > >  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>> > >  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
>> > > @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>> > >  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>> > >  }
>> > > 
>> > > +static inline void
>> > > +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
>> > > +			     const access_mask_t net_access_mask,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
>> > > +
>> > > +	/* Should already be checked in sys_landlock_create_ruleset(). */
>> > > +	WARN_ON_ONCE(net_access_mask != net_mask);
>> > > +	ruleset->access_masks[layer_level] |=
>> > > +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
>> > > +}
>> > > +
>> > >  static inline access_mask_t
>> > >  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>> > >  				const u16 layer_level)
>> > > @@ -266,6 +295,16 @@ 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;
>> > >  }
>> > > +
>> > > +static inline access_mask_t
>> > > +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	return (ruleset->access_masks[layer_level] >>
>> > > +		LANDLOCK_SHIFT_ACCESS_NET) &
>> > > +	       LANDLOCK_MASK_ACCESS_NET;
>> > > +}
>> > > +
>> > >  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>> > >  			    const access_mask_t access_request,
>> > >  			    layer_mask_t (*const layer_masks)[],
>> > > diff --git a/security/landlock/setup.c b/security/landlock/setup.c
>> > > index 0f6113528fa4..df81612811bf 100644
>> > > --- a/security/landlock/setup.c
>> > > +++ b/security/landlock/setup.c
>> > > @@ -14,6 +14,7 @@
>> > >  #include "fs.h"
>> > >  #include "ptrace.h"
>> > >  #include "setup.h"
>> > > +#include "net.h"
>> > > 
>> > >  bool landlock_initialized __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;
>> > > diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> > > index 8a54e87dbb17..3ad652d9a146 100644
>> > > --- a/security/landlock/syscalls.c
>> > > +++ b/security/landlock/syscalls.c
>> > > @@ -29,6 +29,7 @@
>> > >  #include "cred.h"
>> > >  #include "fs.h"
>> > >  #include "limits.h"
>> > > +#include "net.h"
>> > >  #include "ruleset.h"
>> > >  #include "setup.h"
>> > > 
>> > > @@ -74,7 +75,8 @@ static void build_check_abi(void)
>> > >  {
>> > >  	struct landlock_ruleset_attr ruleset_attr;
>> > >  	struct landlock_path_beneath_attr path_beneath_attr;
>> > > -	size_t ruleset_size, path_beneath_size;
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	size_t ruleset_size, path_beneath_size, net_port_size;
>> > > 
>> > >  	/*
>> > >  	 * For each user space ABI structures, first checks that there is no
>> > > @@ -82,13 +84,19 @@ static void build_check_abi(void)
>> > >  	 * struct size.
>> > >  	 */
>> > >  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
>> > > +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>> > >  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
>> > > -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
>> > > +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
>> > > 
>> > >  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>> > >  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
>> > > +
>> > > +	net_port_size = sizeof(net_port_attr.allowed_access);
>> > > +	net_port_size += sizeof(net_port_attr.port);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>> > >  }
>> > > 
>> > >  /* Ruleset handling */
>> > > @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>> > >  	.write = fop_dummy_write,
>> > >  };
>> > > 
>> > > -#define LANDLOCK_ABI_VERSION 3
>> > > +#define LANDLOCK_ABI_VERSION 4
>> > > 
>> > >  /**
>> > >   * sys_landlock_create_ruleset - Create a new ruleset
>> > > @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>> > >  	    LANDLOCK_MASK_ACCESS_FS)
>> > >  		return -EINVAL;
>> > > 
>> > > +	/* Checks network content (and 32-bits cast). */
>> > > +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
>> > > +	    LANDLOCK_MASK_ACCESS_NET)
>> > > +		return -EINVAL;
>> > > +
>> > >  	/* Checks arguments and transforms to kernel struct. */
>> > > -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
>> > > +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
>> > > +					  ruleset_attr.handled_access_net);
>> > >  	if (IS_ERR(ruleset))
>> > >  		return PTR_ERR(ruleset);
>> > > 
>> > > @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	int res, err;
>> > >  	access_mask_t mask;
>> > > 
>> > > -	/* Copies raw user space buffer, only one type for now. */
>> > > +	/* Copies raw user space buffer. */
>> > 
>> > Shouldn't this be part of a previous patch?
>> 
>> I did it according Gunter's suggestion
>> https://lore.kernel.org/netdev/20230627.82cde73b1efe@gnoack.org/
> 
> Ok, that indeed makes more sense in this patch, please keep it.

   OK.
> 
>> > 
>> > >  	res = copy_from_user(&path_beneath_attr, rule_attr,
>> > >  			     sizeof(path_beneath_attr));
>> > >  	if (res)
>> > > @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	return err;
>> > >  }
>> > > 
>> > > +static int add_rule_net_port(struct landlock_ruleset *ruleset,
>> > > +			     const void __user *const rule_attr)
>> > > +{
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	int res;
>> > > +	access_mask_t mask;
>> > > +
>> > > +	/* Copies raw user space buffer. */
>> > > +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
>> > > +	if (res)
>> > > +		return -EFAULT;
>> > > +
>> > > +	/*
>> > > +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
>> > > +	 * are ignored by network actions.
>> > > +	 */
>> > > +	if (!net_port_attr.allowed_access)
>> > > +		return -ENOMSG;
>> > > +
>> > > +	/*
>> > > +	 * Checks that allowed_access matches the @ruleset constraints
>> > > +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
>> > > +	 */
>> > > +	mask = landlock_get_net_access_mask(ruleset, 0);
>> > > +	if ((net_port_attr.allowed_access | mask) != mask)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Denies inserting a rule with port higher than 65535. */
>> > 
>> > For consistency with the following comment:
>> > "Denies inserting a rule with port greater than 65535."
>> > 
>>   Done. Thanks.
>> > 
>> > > +	if (net_port_attr.port > U16_MAX)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Imports the new rule. */
>> > > +	return landlock_append_net_rule(ruleset, net_port_attr.port,
>> > > +					net_port_attr.allowed_access);
>> > > +}
>> > > +
>> > >  /**
>> > >   * sys_landlock_add_rule - Add a new rule to a ruleset
>> > >   *
>> > >   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
>> > >   *		with the new rule.
>> > > - * @rule_type: Identify the structure type pointed to by @rule_attr (only
>> > > - *             %LANDLOCK_RULE_PATH_BENEATH for now).
>> > > + * @rule_type: Identify the structure type pointed to by @rule_attr:
>> > > + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
>> > >   * @rule_attr: Pointer to a rule (only of type &struct
>> > >   *             landlock_path_beneath_attr for now).
>> > >   * @flags: Must be 0.
>> > > @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >   * Possible returned errors are:
>> > >   *
>> > >   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
>> > > + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
>> > 
>> > %LANDLOCK_RULE_NET_PORT
>> 
>>  Done.
>> > 
>> > > + *   supported by the running kernel;
>> > >   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
>> > >   *   &landlock_path_beneath_attr.allowed_access is not a subset of the
>> > 
>> > &landlock_path_beneath_attr.allowed_access or
>> > &landlock_net_port_attr.allowed_access is not a subset of the
>> 
>>   Fixed. Thanks.
>> > 
>> > >   *   ruleset handled accesses);
>> > 
>> > EINVAL description needs to be updated, especially for port > U16_MAX:
>> > - *   ruleset handled accesses);
>> > + *   ruleset handled accesses), or &landlock_net_port_attr.port is
>> > +     greater than 65535;
>> 
>>  Done. Thanks.
>> > 
>> > 
>> > > @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
>> > >  	case LANDLOCK_RULE_PATH_BENEATH:
>> > >  		err = add_rule_path_beneath(ruleset, rule_attr);
>> > >  		break;
>> > > +	case LANDLOCK_RULE_NET_PORT:
>> > > +		err = add_rule_net_port(ruleset, rule_attr);
>> > > +		break;
>> > >  	default:
>> > >  		err = -EINVAL;
>> > >  		break;
>> > > diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
>> > > index 792c3f0a59b4..646f778dfb1e 100644
>> > > --- a/tools/testing/selftests/landlock/base_test.c
>> > > +++ b/tools/testing/selftests/landlock/base_test.c
>> > > @@ -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
>> > > 
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 11/12] samples/landlock: Add network demo
  2023-10-18 12:33   ` Mickaël Salaün
@ 2023-10-20 11:59     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20 11:59 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:33 PM, Mickaël Salaün пишет:
> Please update the subject to "samples/landlock: Support TCP restrictions"

   Got it.
> 
> On Mon, Oct 16, 2023 at 09:50:29AM +0800, Konstantin Meskhidze wrote:
>> 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 <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-12-konstantin.meskhidze@huawei.com
>> [mic: Define __SANE_USERSPACE_TYPES__ to select int-ll64.h and avoid
>> format warnings for PowerPC]
> 
> You can remove all this kind of "[mic: ]" comments, I add them when I
> merge a patch with additional changes.

   Ok. Thanks.
> 
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * Defines __SANE_USERSPACE_TYPES__ to avoid warnings for PowerPC.
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 12/12] landlock: Document Landlock's network support
  2023-10-18 12:34   ` Mickaël Salaün
@ 2023-10-20 12:17     ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-20 12:17 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/18/2023 3:34 PM, Mickaël Salaün пишет:
> On Mon, Oct 16, 2023 at 09:50:30AM +0800, Konstantin Meskhidze wrote:
>> Describe network access rules for TCP sockets. Add network access
>> example in the tutorial. Add kernel configuration support for network.
>> 
>> Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> Link: https://lore.kernel.org/r/20230920092641.832134-13-konstantin.meskhidze@huawei.com
>> Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> ---
>> 
>> Changes since v12:
>> * None.
>> 
>> Changes since v11:
>> * Fixes documentaion as suggested in Günther's and Mickaёl's reviews:
>> https://lore.kernel.org/netdev/3ad02c76-90d8-4723-e554-7f97ef115fc0@digikod.net/
>> 
>> Changes since v10:
>> * Fixes documentaion as Mickaёl suggested:
>> https://lore.kernel.org/linux-security-module/ec23be77-566e-c8fd-179e-f50e025ac2cf@digikod.net/
>> 
>> 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/9f354862-2bc3-39ea-92fd-53803d9bbc21@digikod.net/
>> 
>> Changes since v6:
>> * Adds network support documentaion.
>> 
>> ---
>>  Documentation/userspace-api/landlock.rst | 87 ++++++++++++++++++------
>>  1 file changed, 66 insertions(+), 21 deletions(-)
>> 
>> diff --git a/Documentation/userspace-api/landlock.rst b/Documentation/userspace-api/landlock.rst
>> index f6a7da21708a..affadd9ac662 100644
>> --- a/Documentation/userspace-api/landlock.rst
>> +++ b/Documentation/userspace-api/landlock.rst
>> @@ -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,34 @@ 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
>> +A Landlock rule describes an action on an object which the process intends to
>> +perform.  A set of rules is aggregated in a ruleset, which can then restrict
>>  the thread enforcing it, and its future children.
>> 
>> +The two existing types of rules are:
>> +
>> +Filesystem rules
>> +    For these rules, the object is a file hierarchy,
>> +    and the related filesystem actions are defined with
>> +    `filesystem access rights`.
>> +
>> +Network rules (since ABI v4)
>> +    For these rules, the object is currently a TCP port,
>> +    and the related actions are defined with `network access rights`.
>> +
>>  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.
>> +We first need to define the ruleset that will contain our rules.
>> +
>> +For this example, the ruleset will contain rules that only allow filesystem
>> +read actions and establish a specific TCP connection. Filesystem write
>> +actions and other TCP actions will be denied.
>> +
>> +The ruleset then needs to handle both of these kind of actions.  This is
> 
> two spelling issues:
> "needs to handle both these kinds of actions."

   Thanks. Will be updated.
> 
>> +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 +76,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,9 +87,7 @@ 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 access rights which are only supported in higher versions of the ABI.
>> 
>>  .. code-block:: c
>> 
>> @@ -92,6 +107,11 @@ version of the ABI.
>>      case 2:
>>          /* Removes LANDLOCK_ACCESS_FS_TRUNCATE for ABI < 3 */
>>          ruleset_attr.handled_access_fs &= ~LANDLOCK_ACCESS_FS_TRUNCATE;
> 
> Like just before "case 2" this is missing:
> __attribute__((fallthrough));

   Yep. Will be added.
> 
>> +    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 +163,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_port_attr net_port = {
>> +        .allowed_access = LANDLOCK_ACCESS_NET_CONNECT_TCP,
>> +        .port = 443,
>> +    };
>> +
>> +    err = landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> +                            &net_port, 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 +388,7 @@ Access rights
>>  -------------
>> 
>>  .. kernel-doc:: include/uapi/linux/landlock.h
>> -    :identifiers: fs_access
>> +    :identifiers: fs_access net_access
>> 
>>  Creating a new ruleset
>>  ----------------------
>> @@ -374,6 +407,7 @@ Extending a ruleset
>> 
>>  .. kernel-doc:: include/uapi/linux/landlock.h
>>      :identifiers: landlock_rule_type landlock_path_beneath_attr
>> +                  landlock_net_service_attr
> 
> landlock_net_port_attr
> 
>> 
>>  Enforcing a ruleset
>>  -------------------
>> @@ -451,6 +485,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.
> 
> bind and connect actions to only a set of allowed ports thanks to the new
> ``LANDLOCK_ACCESS_NET_BIND_TCP`` and ``LANDLOCK_ACCESS_NET_CONNECT_TCP``
> access rights.

  OK. Thanks.
> 
>> +
>>  .. _kernel_support:
>> 
>>  Kernel support
>> @@ -469,6 +509,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``).
> 
> LANDLOCK_ACCESS_NET_BIND_TCP

   OK. Got it.
> 
>> +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
>> 
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-20 11:41     ` Konstantin Meskhidze (A)
@ 2023-10-20 15:40       ` Mickaël Salaün
  2023-10-23  7:09         ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-20 15:40 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Fri, Oct 20, 2023 at 02:41:42PM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/18/2023 3:32 PM, Mickaël Salaün пишет:
> > You can update the subject with:
> > "selftests/landlock: Add network tests"
> 
> Ok.
> > 
> > On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze wrote:
> > > These test suites try to check edge cases for TCP sockets
> > > bind() and connect() actions.
> > 
> > You can replace with that:
> > Add 77 test suites to check edge cases related to bind() and connect()
> > actions. They are defined with 6 fixtures and their variants:
> > 
> > > 
> > > protocol:
> > > * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
> > 
> > As you already did, you can write one paragraph per fixture, but
> > starting by explaining the fixture and its related variants, and then
> > listing the tests and explaining their specificities. For instance:
> > 
> > The "protocol" fixture is extended with 12 variants defined as a matrix
> > of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
> > stream/datagram socket. 4 related tests suites are defined:
> > * bind: Test bind combinations with increasingly more
> >    restricting domains.
> > * connect: Test connect combinations with increasingly more
> >    restricting domains.
> > ...
> 
>   Ok. Will be updated.
> > 
> > s/ipv/IPv/g
> 
>   Got it. Thanks.
> > 
> > > * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and unix
> > > sockets.
> > > * bind_unspec: Tests with non-landlocked/landlocked restrictions
> > > for bind action with AF_UNSPEC socket family.
> > > * connect_unspec: Tests with non-landlocked/landlocked restrictions
> > > for connect action with AF_UNSPEC socket family.
> > > 
> > > ipv4:
> > > * from_unix_to_inet: Tests to make sure unix sockets' actions are not
> > > restricted by Landlock rules applied to TCP ones.
> > > 
> > > tcp_layers:
> > > * ruleset_overlap.
> > > * ruleset_expand.
> > > 
> > > mini:
> > > * network_access_rights: Tests with  legitimate access values.
> > > * unknown_access_rights: Tests with invalid attributes, out of access range.
> > > * inval:
> > >     - unhandled allowed access.
> > >     - zero access value.
> > > * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
> > > 
> > > ipv4_tcp:
> > > * port_endianness: Tests with big/little endian port formats.
> > > 
> > > port_specific:
> > > * bind_connect: Tests with specific port values.
> > > 
> > > layout1:
> > > * with_net: Tests with network bind() socket action within
> > > filesystem directory access test.
> > > 
> > > Test coverage for security/landlock is 94.5% of 932 lines according
> > > to gcc/gcov-11.
> > > 
> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
> > > Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
> > > Co-developed-by:: Mickaël Salaün <mic@digikod.net>
> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
> > > ---
> > > 
> > > Changes since v12:
> > > * Renames port_zero to port_specific fixture.
> > > * Refactors port_specific test:
> > >     - Adds set_port() and get_binded_port() helpers.
> > >     - Adds checks for port 0, allowed by Landlock in this version.
> > >     - Adds checks for port 1023.
> > > * Refactors commit message.
> > > 
> > 
> > > +static void set_port(struct service_fixture *const srv, in_port_t port)
> > > +{
> > > +	switch (srv->protocol.domain) {
> > > +	case AF_UNSPEC:
> > > +	case AF_INET:
> > > +		srv->ipv4_addr.sin_port = port;
> > 
> > We should call htons() here, and make port a uint16_t.
> 
>   Done.
> > 
> > > +		return;
> > > +
> > > +	case AF_INET6:
> > > +		srv->ipv6_addr.sin6_port = port;
> > > +		return;
> > > +
> > > +	default:
> > > +		return;
> > > +	}
> > > +}
> > > +
> > > +static in_port_t get_binded_port(int socket_fd,
> > 
> > The returned type should be uint16_t (i.e. host endianess).
> 
>   Done.
> > 
> > > +				 const struct protocol_variant *const prot)
> > > +{
> > > +	struct sockaddr_in ipv4_addr;
> > > +	struct sockaddr_in6 ipv6_addr;
> > > +	socklen_t ipv4_addr_len, ipv6_addr_len;
> > > +
> > > +	/* Gets binded port. */
> > > +	switch (prot->domain) {
> > > +	case AF_UNSPEC:
> > > +	case AF_INET:
> > > +		ipv4_addr_len = sizeof(ipv4_addr);
> > > +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
> > > +		return ntohs(ipv4_addr.sin_port);
> > > +
> > > +	case AF_INET6:
> > > +		ipv6_addr_len = sizeof(ipv6_addr);
> > > +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
> > > +		return ntohs(ipv6_addr.sin6_port);
> > > +
> > > +	default:
> > > +		return 0;
> > > +	}
> > > +}
> > 
> > These are good helpers!
> > 
> > 
> > > +FIXTURE_TEARDOWN(ipv4)
> > > +{
> > > +}
> > > +
> > > +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
> > 
> > No FIXME should remain.
> 
>   Ok. Deleted.
> > 
> > > +TEST_F(ipv4, from_unix_to_inet)
> > 
> > > +TEST_F(mini, network_access_rights)
> > > +{
> > > +	const struct landlock_ruleset_attr ruleset_attr = {
> > > +		.handled_access_net = ACCESS_ALL,
> > > +	};
> > > +	struct landlock_net_port_attr net_service = {
> > 
> > Please rename to "net_port" everywhere.
> 
>   Done.
> > 
> > > +TEST_F(port_specific, bind_connect)
> > > +{
> > > +	int socket_fd, ret;
> > > +
> > > +	/* Adds the first rule layer with bind and connect actions. */
> > > +	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_port_attr tcp_bind_connect_zero = {
> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
> > > +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
> > > +			.port = htons(0),
> > 
> > We don't need any htons() calls anymore. It doesn't change the 0 value
> > in this case but this is not correct.
> 
>  Yep. We call htons(port) in landlock_append_net_rule().
>  Thanks.
> > 
> > > +		};
> > > +
> > 
> > Useless new line.
> 
>   Ok. Thanks.
> > 
> > > +		int ruleset_fd;
> > > +
> > > +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
> > > +						     sizeof(ruleset_attr), 0);
> > > +		ASSERT_LE(0, ruleset_fd);
> > > +
> > > +		/* Checks zero port value on bind and connect actions. */
> > > +		EXPECT_EQ(0,
> > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> > > +					    &tcp_bind_connect_zero, 0));
> > > +
> > > +		enforce_ruleset(_metadata, ruleset_fd);
> > > +		EXPECT_EQ(0, close(ruleset_fd));
> > > +	}
> > > +
> > > +	socket_fd = socket_variant(&self->srv0);
> > > +	ASSERT_LE(0, socket_fd);
> > > +
> > > +	/* Sets address port to 0 for both protocol families. */
> > > +	set_port(&self->srv0, htons(0));
> > 
> > ditto
> > 
> > > +
> > > +	/* Binds on port 0. */
> > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > > +	} else {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > 
> > If the results are the same, no need to add an if block.
> 
>   Right. Updated.
> > 
> > > +	}
> > > +
> > > +	/* Connects on port 0. */
> > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		EXPECT_EQ(-ECONNREFUSED, ret);
> > > +	} else {
> > > +		EXPECT_EQ(-ECONNREFUSED, ret);
> > > +	}
> > 
> > ditto
> > 
>  Updated.
> > > +
> > > +	/* Binds on port 0. */
> > 
> > Please close sockets once they are used, and recreate one for another
> > bind/connect to avoid wrong checks.
> 
>   Ok. But I can reuse socket_fd after closeing a socket. Correct?

It would be clearer to have one variable for the client socket
(connect_fd) and another variable for the server socket (bind_fd).
But once the socket is closed, you can reuse the same variable by
storing a new socket in it. You then only need two variables for sockets
in this test.

> > 
> > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > > +	} else {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > > +	}
> > 
> > Why this second bind() block? Furthermore, it is using the same
> > socket_fd.

Is this block useful?

> 
>   I will refactor the code this way -  sockets will be recreated for each
> bind/connect, and I prefer to use self-connected sockets (use one socket
> descriptor) in these tests to make code simpler; testing logic remains the
> same way as if we have 2 sockets.
> 
> What do you think???

I find it confusing to use self-connected sockets, it's not clear at all
what is going on, and AFAIK it doesn't reflect real use cases. Please
don't do that.

Using the same variable for both bind and connect socket will lead to
issues difficult to debug, and leaked FDs. For instance with the bind +
get_binded_port + connect test you should use one variable per socket.
To make it easier to read, please follow this rule everywhere (the only
case when this is done seems to be with the port_specific.bind_connect
test).

> 
> > 
> > > +
> > > +	/* Sets binded port for both protocol families. */
> > > +	set_port(&self->srv0,
> > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
> > 
> > Ditto, these two endianess translations are useless.
> 
>   Updated. Thanks.
> > 
> > You can also add this to make sure the returned port is not 0:
> > port = get_binded_port(socket_fd, &variant->prot);
> > EXPECT_NE(0, port);
> > set_port(&self->srv0, port);
> 
>   Ok. Thanks for the tip.
> > 
> > > +
> > > +	/* Connects on the binded port. */
> > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		/* Denied by Landlock. */
> > > +		EXPECT_EQ(-EACCES, ret);
> > > +	} else {
> > > +		EXPECT_EQ(0, ret);
> > > +	}
> > > +
> > > +	EXPECT_EQ(0, close(socket_fd));
> > > +
> > 
> > 
> > 
> > > +	/* Adds the second rule layer with just bind action. */
> > 
> > There is not only bind actions here.
> 
>   Right.
> > 
> > This second part of the tests should be in a dedicated
> > TEST_F(port_specific, bind_1023).
> 
>   Got it.
> > 
> > > +	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_port_attr tcp_bind_zero = {
> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> > > +			.port = htons(0),
> > > +		};
> > > +
> > 
> > Useless new lines.
> 
>   Got it.
> > 
> > > +		/* A rule with port value less than 1024. */
> > > +		const struct landlock_net_port_attr tcp_bind_lower_range = {
> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> > > +			.port = htons(1023),
> > > +		};
> > > +
> > 
> > Useless new line.
> 
>   Got it.
> > 
> > > +		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_PORT,
> > > +					    &tcp_bind_lower_range, 0));
> > > +		ASSERT_EQ(0,
> > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> > > +					    &tcp_bind_zero, 0));
> > > +
> > > +		enforce_ruleset(_metadata, ruleset_fd);
> > > +		EXPECT_EQ(0, close(ruleset_fd));
> > > +	}
> > > +
> > > +	socket_fd = socket_variant(&self->srv0);
> > 
> > We must have one socket FD dedicated to bind an another dedicated to
> > connect, e.g. bind_fd and connect_fd, an close them after each use,
> > otherwise tests might be inconsistent.
> 
>   Why can't we use self-connected sockets here? Why tests might be
> inconsistent? Tests will be working the same way as if we have 2 sockets,
> plus the code is simpler.

AFAIK it doesn't reflect real use cases of sockets, and I find it really
confusing. Where did you see this kind of usage?

Test might be inconsistent for instance if you change the port from 1023
to 1024 and you adjust the (denied by system) EXPECT_EQ(-EACCES, ret),
you'll get a new error in the following block, which doesn't make sense
at first, but then you realize it is because the socket is already
binded.  To avoid this kind of issues, and avoid leaking FDs, please use
a socket per usage and close them before testing something else.

> > 
> > > +	ASSERT_LE(0, socket_fd);
> > > +
> > > +	/* Sets address port to 1023 for both protocol families. */
> > > +	set_port(&self->srv0, htons(1023));
> > > +
> > > +	/* Binds on port 1023. */
> > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > 
> > No need to add this check if the result is the same for sandboxed and
> > not sandboxed tests.
> 
>  Ok. Thanks.
> > 
> > Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and clear_cap()
> > around this bind_variant() to make this test useful.
> > 
> > You will also need to patch common.h like this:
> > @@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
> >          cap_t cap_p;
> >          /* Only these three capabilities are useful for the tests. */
> >          const cap_value_t caps[] = {
> > +               /* clang-format off */
> >                  CAP_DAC_OVERRIDE,
> >                  CAP_MKNOD,
> >                  CAP_SYS_ADMIN,
> >                  CAP_SYS_CHROOT,
> > +               CAP_NET_BIND_SERVICE,
> > +               /* clang-format on */
> >          };
> 
>  OK. Thanks.
> > 
> > > +		/* Denied by the system. */
> > > +		EXPECT_EQ(-EACCES, ret);
> > > +	} else {
> > > +		/* Denied by the system. */
> > > +		EXPECT_EQ(-EACCES, ret);
> > > +	}
> > > +
> > 
> > I don't see why the following part is useful. Why did you add it?
>   Binding to ports < 1024 are forbidden by the system, not by Landlock.
>   I added a rule with port 1023 to make sure it works as expected.

Landlock, as any LSM, can only add more restrictions. That's why it
would make more sense to test with CAP_NET_BIND_SERVICE, to make sure
Landlock rules work the same with this kind of privileged ports, but you
can test both cases (all within the same TEST_F though, and without
other tests).

> 
> > Why tcp_bind_zero?
>    Beacause it's a bind action with port zero rule.

Yes but I don't see why it's relevant to test that here, because it was
tested just before.

> 
> > 
> > The other parts are good though!
> > 
> > > +	/* Sets address port to 0 for both protocol families. */
> > > +	set_port(&self->srv0, htons(0));
> > > +
> > > +	/* Binds on port 0. */
> > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > > +	} else {
> > > +		/* Binds to a random port within ip_local_port_range. */
> > > +		EXPECT_EQ(0, ret);
> > > +	}
> > > +
> > > +	/* Sets binded port for both protocol families. */
> > > +	set_port(&self->srv0,
> > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
> > > +
> > > +	/* Connects on the binded port. */
> > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > +		/* Denied by Landlock. */
> > > +		EXPECT_EQ(-EACCES, ret);
> > > +	} else {
> > > +		EXPECT_EQ(0, ret);
> > > +	}
> > > +
> > > +	EXPECT_EQ(0, close(socket_fd));
> > > +}
> > > +
> > > +TEST_HARNESS_MAIN
> > > --
> > > 2.25.1
> > > 
> > .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20 11:58         ` Konstantin Meskhidze (A)
@ 2023-10-20 15:41           ` Mickaël Salaün
  2023-10-23  7:23             ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-20 15:41 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Fri, Oct 20, 2023 at 02:58:31PM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/20/2023 12:49 PM, Mickaël Salaün пишет:
> > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
> > > 
> > > 
> > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
> > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:

> > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> > > > > index 4c209acee01e..1fe4298ff4a7 100644
> > > > > --- a/security/landlock/ruleset.c
> > > > > +++ b/security/landlock/ruleset.c
> > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > >  	refcount_set(&new_ruleset->usage, 1);
> > > > >  	mutex_init(&new_ruleset->lock);
> > > > >  	new_ruleset->root_inode = RB_ROOT;
> > > > > +
> > > > > +#if IS_ENABLED(CONFIG_INET)
> > > > > +	new_ruleset->root_net_port = RB_ROOT;
> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > +
> > > > >  	new_ruleset->num_layers = num_layers;
> > > > >  	/*
> > > > >  	 * hierarchy = NULL
> > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > >  }
> > > > > > >  struct landlock_ruleset *
> > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
> > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
> > > > > +			const access_mask_t net_access_mask)
> > > > >  {
> > > > >  	struct landlock_ruleset *new_ruleset;
> > > > > > >  	/* Informs about useless ruleset. */
> > > > > -	if (!fs_access_mask)
> > > > > +	if (!fs_access_mask && !net_access_mask)
> > > > >  		return ERR_PTR(-ENOMSG);
> > > > >  	new_ruleset = create_ruleset(1);
> > > > > -	if (!IS_ERR(new_ruleset))
> > > > > +	if (IS_ERR(new_ruleset))
> > > > > +		return new_ruleset;
> > > > > +	if (fs_access_mask)
> > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > > > +	if (net_access_mask)
> > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > > > > This is good, but it is not tested: we need to add a test that
> > > both
> > > > handle FS and net restrictions. You can add one in net.c, just handling
> > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
> > > > that open("/") is denied, open("/dev") is allowed, and and only the
> > > > allowed port is allowed with bind(). This test should be simple and can
> > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> > > > after port_endianness. fcntl.h should then be included by net.c
> > > 
> > >   Ok.
> > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
> > > but it
> > > 
> > >   Yep. I added this kind of nest in fs_test.c to test both fs and network
> > > rules together.
> > > > is not complete. You can revamp this test and move it to net.c
> > > > following the above suggestions, keeping it consistent with other tests
> > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
> > > > > This test must failed if we change
> > > "ruleset->access_masks[layer_level] |="
> > > > to "ruleset->access_masks[layer_level] =" in
> > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
> > > 
> > >   Do you want to change it? Why?
> > 
> > The kernel code is correct and must not be changed. However, if by
> > mistake we change it and remove the OR, a test should catch that. We
> > need a test to assert this assumption.
> > 
>   OK. I will add additional assert simulating
> "ruleset->access_masks[layer_level] =" kernel code.
> > >   Fs and network masks are ORed to not intersect with each other.
> > 
> > Yes, they are ORed, and we need a test to check that. Noting is
> > currently testing this OR (and the different rule type consistency).
> > I'm suggesting to revamp the layout1.with_net test into
> > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> > addition of different types.
> 
>   I will move layout1.with_net test into net.c and rename it. Looks like
>   it just needed to add "ruleset->access_masks[layer_level] =" assert
>   because the test already has rule addition with different types.

The with_net test doesn't have FS rules, which is the main missing part.
You'll need to rely on the net.c helpers, use the hardcoded paths, and
only handle one access right of each type as I suggested above.

> 
>   Do you have any more review updates so far?

That's all for this patch series. :)

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-20 15:40       ` Mickaël Salaün
@ 2023-10-23  7:09         ` Konstantin Meskhidze (A)
  2023-10-23  8:44           ` Mickaël Salaün
  0 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-23  7:09 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/20/2023 6:40 PM, Mickaël Salaün пишет:
> On Fri, Oct 20, 2023 at 02:41:42PM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/18/2023 3:32 PM, Mickaël Salaün пишет:
>> > You can update the subject with:
>> > "selftests/landlock: Add network tests"
>> 
>> Ok.
>> > 
>> > On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze wrote:
>> > > These test suites try to check edge cases for TCP sockets
>> > > bind() and connect() actions.
>> > 
>> > You can replace with that:
>> > Add 77 test suites to check edge cases related to bind() and connect()
>> > actions. They are defined with 6 fixtures and their variants:
>> > 
>> > > 
>> > > protocol:
>> > > * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
>> > 
>> > As you already did, you can write one paragraph per fixture, but
>> > starting by explaining the fixture and its related variants, and then
>> > listing the tests and explaining their specificities. For instance:
>> > 
>> > The "protocol" fixture is extended with 12 variants defined as a matrix
>> > of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
>> > stream/datagram socket. 4 related tests suites are defined:
>> > * bind: Test bind combinations with increasingly more
>> >    restricting domains.
>> > * connect: Test connect combinations with increasingly more
>> >    restricting domains.
>> > ...
>> 
>>   Ok. Will be updated.
>> > 
>> > s/ipv/IPv/g
>> 
>>   Got it. Thanks.
>> > 
>> > > * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and unix
>> > > sockets.
>> > > * bind_unspec: Tests with non-landlocked/landlocked restrictions
>> > > for bind action with AF_UNSPEC socket family.
>> > > * connect_unspec: Tests with non-landlocked/landlocked restrictions
>> > > for connect action with AF_UNSPEC socket family.
>> > > 
>> > > ipv4:
>> > > * from_unix_to_inet: Tests to make sure unix sockets' actions are not
>> > > restricted by Landlock rules applied to TCP ones.
>> > > 
>> > > tcp_layers:
>> > > * ruleset_overlap.
>> > > * ruleset_expand.
>> > > 
>> > > mini:
>> > > * network_access_rights: Tests with  legitimate access values.
>> > > * unknown_access_rights: Tests with invalid attributes, out of access range.
>> > > * inval:
>> > >     - unhandled allowed access.
>> > >     - zero access value.
>> > > * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
>> > > 
>> > > ipv4_tcp:
>> > > * port_endianness: Tests with big/little endian port formats.
>> > > 
>> > > port_specific:
>> > > * bind_connect: Tests with specific port values.
>> > > 
>> > > layout1:
>> > > * with_net: Tests with network bind() socket action within
>> > > filesystem directory access test.
>> > > 
>> > > Test coverage for security/landlock is 94.5% of 932 lines according
>> > > to gcc/gcov-11.
>> > > 
>> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> > > Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
>> > > Co-developed-by:: Mickaël Salaün <mic@digikod.net>
>> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> > > ---
>> > > 
>> > > Changes since v12:
>> > > * Renames port_zero to port_specific fixture.
>> > > * Refactors port_specific test:
>> > >     - Adds set_port() and get_binded_port() helpers.
>> > >     - Adds checks for port 0, allowed by Landlock in this version.
>> > >     - Adds checks for port 1023.
>> > > * Refactors commit message.
>> > > 
>> > 
>> > > +static void set_port(struct service_fixture *const srv, in_port_t port)
>> > > +{
>> > > +	switch (srv->protocol.domain) {
>> > > +	case AF_UNSPEC:
>> > > +	case AF_INET:
>> > > +		srv->ipv4_addr.sin_port = port;
>> > 
>> > We should call htons() here, and make port a uint16_t.
>> 
>>   Done.
>> > 
>> > > +		return;
>> > > +
>> > > +	case AF_INET6:
>> > > +		srv->ipv6_addr.sin6_port = port;
>> > > +		return;
>> > > +
>> > > +	default:
>> > > +		return;
>> > > +	}
>> > > +}
>> > > +
>> > > +static in_port_t get_binded_port(int socket_fd,
>> > 
>> > The returned type should be uint16_t (i.e. host endianess).
>> 
>>   Done.
>> > 
>> > > +				 const struct protocol_variant *const prot)
>> > > +{
>> > > +	struct sockaddr_in ipv4_addr;
>> > > +	struct sockaddr_in6 ipv6_addr;
>> > > +	socklen_t ipv4_addr_len, ipv6_addr_len;
>> > > +
>> > > +	/* Gets binded port. */
>> > > +	switch (prot->domain) {
>> > > +	case AF_UNSPEC:
>> > > +	case AF_INET:
>> > > +		ipv4_addr_len = sizeof(ipv4_addr);
>> > > +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
>> > > +		return ntohs(ipv4_addr.sin_port);
>> > > +
>> > > +	case AF_INET6:
>> > > +		ipv6_addr_len = sizeof(ipv6_addr);
>> > > +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
>> > > +		return ntohs(ipv6_addr.sin6_port);
>> > > +
>> > > +	default:
>> > > +		return 0;
>> > > +	}
>> > > +}
>> > 
>> > These are good helpers!
>> > 
>> > 
>> > > +FIXTURE_TEARDOWN(ipv4)
>> > > +{
>> > > +}
>> > > +
>> > > +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
>> > 
>> > No FIXME should remain.
>> 
>>   Ok. Deleted.
>> > 
>> > > +TEST_F(ipv4, from_unix_to_inet)
>> > 
>> > > +TEST_F(mini, network_access_rights)
>> > > +{
>> > > +	const struct landlock_ruleset_attr ruleset_attr = {
>> > > +		.handled_access_net = ACCESS_ALL,
>> > > +	};
>> > > +	struct landlock_net_port_attr net_service = {
>> > 
>> > Please rename to "net_port" everywhere.
>> 
>>   Done.
>> > 
>> > > +TEST_F(port_specific, bind_connect)
>> > > +{
>> > > +	int socket_fd, ret;
>> > > +
>> > > +	/* Adds the first rule layer with bind and connect actions. */
>> > > +	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_port_attr tcp_bind_connect_zero = {
>> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
>> > > +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
>> > > +			.port = htons(0),
>> > 
>> > We don't need any htons() calls anymore. It doesn't change the 0 value
>> > in this case but this is not correct.
>> 
>>  Yep. We call htons(port) in landlock_append_net_rule().
>>  Thanks.
>> > 
>> > > +		};
>> > > +
>> > 
>> > Useless new line.
>> 
>>   Ok. Thanks.
>> > 
>> > > +		int ruleset_fd;
>> > > +
>> > > +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
>> > > +						     sizeof(ruleset_attr), 0);
>> > > +		ASSERT_LE(0, ruleset_fd);
>> > > +
>> > > +		/* Checks zero port value on bind and connect actions. */
>> > > +		EXPECT_EQ(0,
>> > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> > > +					    &tcp_bind_connect_zero, 0));
>> > > +
>> > > +		enforce_ruleset(_metadata, ruleset_fd);
>> > > +		EXPECT_EQ(0, close(ruleset_fd));
>> > > +	}
>> > > +
>> > > +	socket_fd = socket_variant(&self->srv0);
>> > > +	ASSERT_LE(0, socket_fd);
>> > > +
>> > > +	/* Sets address port to 0 for both protocol families. */
>> > > +	set_port(&self->srv0, htons(0));
>> > 
>> > ditto
>> > 
>> > > +
>> > > +	/* Binds on port 0. */
>> > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > > +	} else {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > 
>> > If the results are the same, no need to add an if block.
>> 
>>   Right. Updated.
>> > 
>> > > +	}
>> > > +
>> > > +	/* Connects on port 0. */
>> > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		EXPECT_EQ(-ECONNREFUSED, ret);
>> > > +	} else {
>> > > +		EXPECT_EQ(-ECONNREFUSED, ret);
>> > > +	}
>> > 
>> > ditto
>> > 
>>  Updated.
>> > > +
>> > > +	/* Binds on port 0. */
>> > 
>> > Please close sockets once they are used, and recreate one for another
>> > bind/connect to avoid wrong checks.
>> 
>>   Ok. But I can reuse socket_fd after closeing a socket. Correct?
> 
> It would be clearer to have one variable for the client socket
> (connect_fd) and another variable for the server socket (bind_fd).
> But once the socket is closed, you can reuse the same variable by
> storing a new socket in it. You then only need two variables for sockets
> in this test.

  Ok. Thanks.
> 
>> > 
>> > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > > +	} else {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > > +	}
>> > 
>> > Why this second bind() block? Furthermore, it is using the same
>> > socket_fd.
> 
> Is this block useful?

   For a self-connected socket after connection try we need to rebind it 
again. I checked this logic in a small standalone test (with gdb on). So 
for 2 sockets (differnt fds) there is no need to do that.
> 
>> 
>>   I will refactor the code this way -  sockets will be recreated for each
>> bind/connect, and I prefer to use self-connected sockets (use one socket
>> descriptor) in these tests to make code simpler; testing logic remains the
>> same way as if we have 2 sockets.
>> 
>> What do you think???
> 
> I find it confusing to use self-connected sockets, it's not clear at all
> what is going on, and AFAIK it doesn't reflect real use cases. Please
> don't do that.
> 
> Using the same variable for both bind and connect socket will lead to
> issues difficult to debug, and leaked FDs. For instance with the bind +
> get_binded_port + connect test you should use one variable per socket.
> To make it easier to read, please follow this rule everywhere (the only
> case when this is done seems to be with the port_specific.bind_connect
> test).

   OK. I will use 2 fds for bind and connect sockets.
> 
>> 
>> > 
>> > > +
>> > > +	/* Sets binded port for both protocol families. */
>> > > +	set_port(&self->srv0,
>> > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
>> > 
>> > Ditto, these two endianess translations are useless.
>> 
>>   Updated. Thanks.
>> > 
>> > You can also add this to make sure the returned port is not 0:
>> > port = get_binded_port(socket_fd, &variant->prot);
>> > EXPECT_NE(0, port);
>> > set_port(&self->srv0, port);
>> 
>>   Ok. Thanks for the tip.
>> > 
>> > > +
>> > > +	/* Connects on the binded port. */
>> > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		/* Denied by Landlock. */
>> > > +		EXPECT_EQ(-EACCES, ret);
>> > > +	} else {
>> > > +		EXPECT_EQ(0, ret);
>> > > +	}
>> > > +
>> > > +	EXPECT_EQ(0, close(socket_fd));
>> > > +
>> > 
>> > 
>> > 
>> > > +	/* Adds the second rule layer with just bind action. */
>> > 
>> > There is not only bind actions here.
>> 
>>   Right.
>> > 
>> > This second part of the tests should be in a dedicated
>> > TEST_F(port_specific, bind_1023).
>> 
>>   Got it.
>> > 
>> > > +	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_port_attr tcp_bind_zero = {
>> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> > > +			.port = htons(0),
>> > > +		};
>> > > +
>> > 
>> > Useless new lines.
>> 
>>   Got it.
>> > 
>> > > +		/* A rule with port value less than 1024. */
>> > > +		const struct landlock_net_port_attr tcp_bind_lower_range = {
>> > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> > > +			.port = htons(1023),
>> > > +		};
>> > > +
>> > 
>> > Useless new line.
>> 
>>   Got it.
>> > 
>> > > +		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_PORT,
>> > > +					    &tcp_bind_lower_range, 0));
>> > > +		ASSERT_EQ(0,
>> > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> > > +					    &tcp_bind_zero, 0));
>> > > +
>> > > +		enforce_ruleset(_metadata, ruleset_fd);
>> > > +		EXPECT_EQ(0, close(ruleset_fd));
>> > > +	}
>> > > +
>> > > +	socket_fd = socket_variant(&self->srv0);
>> > 
>> > We must have one socket FD dedicated to bind an another dedicated to
>> > connect, e.g. bind_fd and connect_fd, an close them after each use,
>> > otherwise tests might be inconsistent.
>> 
>>   Why can't we use self-connected sockets here? Why tests might be
>> inconsistent? Tests will be working the same way as if we have 2 sockets,
>> plus the code is simpler.
> 
> AFAIK it doesn't reflect real use cases of sockets, and I find it really
> confusing. Where did you see this kind of usage?
> 
> Test might be inconsistent for instance if you change the port from 1023
> to 1024 and you adjust the (denied by system) EXPECT_EQ(-EACCES, ret),
> you'll get a new error in the following block, which doesn't make sense
> at first, but then you realize it is because the socket is already
> binded.  To avoid this kind of issues, and avoid leaking FDs, please use
> a socket per usage and close them before testing something else.

  Ok. Got it.
> 
>> > 
>> > > +	ASSERT_LE(0, socket_fd);
>> > > +
>> > > +	/* Sets address port to 1023 for both protocol families. */
>> > > +	set_port(&self->srv0, htons(1023));
>> > > +
>> > > +	/* Binds on port 1023. */
>> > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > 
>> > No need to add this check if the result is the same for sandboxed and
>> > not sandboxed tests.
>> 
>>  Ok. Thanks.
>> > 
>> > Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and clear_cap()
>> > around this bind_variant() to make this test useful.
>> > 
>> > You will also need to patch common.h like this:
>> > @@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
>> >          cap_t cap_p;
>> >          /* Only these three capabilities are useful for the tests. */
>> >          const cap_value_t caps[] = {
>> > +               /* clang-format off */
>> >                  CAP_DAC_OVERRIDE,
>> >                  CAP_MKNOD,
>> >                  CAP_SYS_ADMIN,
>> >                  CAP_SYS_CHROOT,
>> > +               CAP_NET_BIND_SERVICE,
>> > +               /* clang-format on */
>> >          };
>> 
>>  OK. Thanks.
>> > 
>> > > +		/* Denied by the system. */
>> > > +		EXPECT_EQ(-EACCES, ret);
>> > > +	} else {
>> > > +		/* Denied by the system. */
>> > > +		EXPECT_EQ(-EACCES, ret);
>> > > +	}
>> > > +
>> > 
>> > I don't see why the following part is useful. Why did you add it?
>>   Binding to ports < 1024 are forbidden by the system, not by Landlock.
>>   I added a rule with port 1023 to make sure it works as expected.
> 
> Landlock, as any LSM, can only add more restrictions. That's why it
> would make more sense to test with CAP_NET_BIND_SERVICE, to make sure
> Landlock rules work the same with this kind of privileged ports, but you
> can test both cases (all within the same TEST_F though, and without
> other tests).

  Do you mean during the test to set CAP_NET_BIND_SERVICE, check it with 
landlock (it will success), then switch  CAP_NET_BIND_SERVICE cap off 
and bind it again ( will be refused by the system)?
Am I correct?
> 
>> 
>> > Why tcp_bind_zero?
>>    Beacause it's a bind action with port zero rule.
> 
> Yes but I don't see why it's relevant to test that here, because it was
> tested just before.
> 
   OK. I just leave binding to 1023 port here.
   I'm thinking to add binding to 1024 port then to show that this port 
is allowed by the system but denied by landlock ( we have just rule with 
1023 port).
   What do you think?
>> 
>> > 
>> > The other parts are good though!
>> > 
>> > > +	/* Sets address port to 0 for both protocol families. */
>> > > +	set_port(&self->srv0, htons(0));
>> > > +
>> > > +	/* Binds on port 0. */
>> > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > > +	} else {
>> > > +		/* Binds to a random port within ip_local_port_range. */
>> > > +		EXPECT_EQ(0, ret);
>> > > +	}
>> > > +
>> > > +	/* Sets binded port for both protocol families. */
>> > > +	set_port(&self->srv0,
>> > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
>> > > +
>> > > +	/* Connects on the binded port. */
>> > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > +		/* Denied by Landlock. */
>> > > +		EXPECT_EQ(-EACCES, ret);
>> > > +	} else {
>> > > +		EXPECT_EQ(0, ret);
>> > > +	}
>> > > +
>> > > +	EXPECT_EQ(0, close(socket_fd));
>> > > +}
>> > > +
>> > > +TEST_HARNESS_MAIN
>> > > --
>> > > 2.25.1
>> > > 
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20 15:41           ` Mickaël Salaün
@ 2023-10-23  7:23             ` Konstantin Meskhidze (A)
  2023-10-23  8:30               ` Mickaël Salaün
  0 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-23  7:23 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/20/2023 6:41 PM, Mickaël Salaün пишет:
> On Fri, Oct 20, 2023 at 02:58:31PM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/20/2023 12:49 PM, Mickaël Salaün пишет:
>> > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> 
>> > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > > > --- a/security/landlock/ruleset.c
>> > > > > +++ b/security/landlock/ruleset.c
>> > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > >  	refcount_set(&new_ruleset->usage, 1);
>> > > > >  	mutex_init(&new_ruleset->lock);
>> > > > >  	new_ruleset->root_inode = RB_ROOT;
>> > > > > +
>> > > > > +#if IS_ENABLED(CONFIG_INET)
>> > > > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > +
>> > > > >  	new_ruleset->num_layers = num_layers;
>> > > > >  	/*
>> > > > >  	 * hierarchy = NULL
>> > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > >  }
>> > > > > > >  struct landlock_ruleset *
>> > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > > > +			const access_mask_t net_access_mask)
>> > > > >  {
>> > > > >  	struct landlock_ruleset *new_ruleset;
>> > > > > > >  	/* Informs about useless ruleset. */
>> > > > > -	if (!fs_access_mask)
>> > > > > +	if (!fs_access_mask && !net_access_mask)
>> > > > >  		return ERR_PTR(-ENOMSG);
>> > > > >  	new_ruleset = create_ruleset(1);
>> > > > > -	if (!IS_ERR(new_ruleset))
>> > > > > +	if (IS_ERR(new_ruleset))
>> > > > > +		return new_ruleset;
>> > > > > +	if (fs_access_mask)
>> > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > > > +	if (net_access_mask)
>> > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > > > > This is good, but it is not tested: we need to add a test that
>> > > both
>> > > > handle FS and net restrictions. You can add one in net.c, just handling
>> > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > > > that open("/") is denied, open("/dev") is allowed, and and only the
>> > > > allowed port is allowed with bind(). This test should be simple and can
>> > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > > > after port_endianness. fcntl.h should then be included by net.c
>> > > 
>> > >   Ok.
>> > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
>> > > but it
>> > > 
>> > >   Yep. I added this kind of nest in fs_test.c to test both fs and network
>> > > rules together.
>> > > > is not complete. You can revamp this test and move it to net.c
>> > > > following the above suggestions, keeping it consistent with other tests
>> > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > > > > This test must failed if we change
>> > > "ruleset->access_masks[layer_level] |="
>> > > > to "ruleset->access_masks[layer_level] =" in
>> > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> > > 
>> > >   Do you want to change it? Why?
>> > 
>> > The kernel code is correct and must not be changed. However, if by
>> > mistake we change it and remove the OR, a test should catch that. We
>> > need a test to assert this assumption.
>> > 
>>   OK. I will add additional assert simulating
>> "ruleset->access_masks[layer_level] =" kernel code.
>> > >   Fs and network masks are ORed to not intersect with each other.
>> > 
>> > Yes, they are ORed, and we need a test to check that. Noting is
>> > currently testing this OR (and the different rule type consistency).
>> > I'm suggesting to revamp the layout1.with_net test into
>> > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
>> > addition of different types.
>> 
>>   I will move layout1.with_net test into net.c and rename it. Looks like
>>   it just needed to add "ruleset->access_masks[layer_level] =" assert
>>   because the test already has rule addition with different types.
> 
> The with_net test doesn't have FS rules, which is the main missing part.
> You'll need to rely on the net.c helpers, use the hardcoded paths, and
> only handle one access right of each type as I suggested above.
> 

  This is with_net code:

   ....
   /* Adds a network rule. */
	
ASSERT_EQ(0, landlock_add_rule(ruleset_fd_net, LANDLOCK_RULE_NET_PORT,
				       &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));
....

It has FS rules - just after ruleset_fd_net rule inforced.
Or maybe I missed something?

>> 
>>   Do you have any more review updates so far?
> 
> That's all for this patch series. :)

   Ok. Thanks.
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-23  7:23             ` Konstantin Meskhidze (A)
@ 2023-10-23  8:30               ` Mickaël Salaün
  2023-10-23  8:56                 ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-23  8:30 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 23, 2023 at 10:23:35AM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/20/2023 6:41 PM, Mickaël Salaün пишет:
> > On Fri, Oct 20, 2023 at 02:58:31PM +0300, Konstantin Meskhidze (A) wrote:
> > > 
> > > 
> > > 10/20/2023 12:49 PM, Mickaël Salaün пишет:
> > > > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
> > > > > > > > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
> > > > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> > 
> > > > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> > > > > > > index 4c209acee01e..1fe4298ff4a7 100644
> > > > > > > --- a/security/landlock/ruleset.c
> > > > > > > +++ b/security/landlock/ruleset.c
> > > > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > > > >  	refcount_set(&new_ruleset->usage, 1);
> > > > > > >  	mutex_init(&new_ruleset->lock);
> > > > > > >  	new_ruleset->root_inode = RB_ROOT;
> > > > > > > +
> > > > > > > +#if IS_ENABLED(CONFIG_INET)
> > > > > > > +	new_ruleset->root_net_port = RB_ROOT;
> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > > > +
> > > > > > >  	new_ruleset->num_layers = num_layers;
> > > > > > >  	/*
> > > > > > >  	 * hierarchy = NULL
> > > > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > > > >  }
> > > > > > > > >  struct landlock_ruleset *
> > > > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
> > > > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
> > > > > > > +			const access_mask_t net_access_mask)
> > > > > > >  {
> > > > > > >  	struct landlock_ruleset *new_ruleset;
> > > > > > > > >  	/* Informs about useless ruleset. */
> > > > > > > -	if (!fs_access_mask)
> > > > > > > +	if (!fs_access_mask && !net_access_mask)
> > > > > > >  		return ERR_PTR(-ENOMSG);
> > > > > > >  	new_ruleset = create_ruleset(1);
> > > > > > > -	if (!IS_ERR(new_ruleset))
> > > > > > > +	if (IS_ERR(new_ruleset))
> > > > > > > +		return new_ruleset;
> > > > > > > +	if (fs_access_mask)
> > > > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > > > > > +	if (net_access_mask)
> > > > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > > > > > > This is good, but it is not tested: we need to add a test that
> > > > > both
> > > > > > handle FS and net restrictions. You can add one in net.c, just handling
> > > > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> > > > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
> > > > > > that open("/") is denied, open("/dev") is allowed, and and only the
> > > > > > allowed port is allowed with bind(). This test should be simple and can
> > > > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> > > > > > after port_endianness. fcntl.h should then be included by net.c
> > > > > > >   Ok.
> > > > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
> > > > > but it
> > > > > > >   Yep. I added this kind of nest in fs_test.c to test both
> > > fs and network
> > > > > rules together.
> > > > > > is not complete. You can revamp this test and move it to net.c
> > > > > > following the above suggestions, keeping it consistent with other tests
> > > > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
> > > > > > > This test must failed if we change
> > > > > "ruleset->access_masks[layer_level] |="
> > > > > > to "ruleset->access_masks[layer_level] =" in
> > > > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
> > > > > > >   Do you want to change it? Why?
> > > > > The kernel code is correct and must not be changed. However, if
> > > by
> > > > mistake we change it and remove the OR, a test should catch that. We
> > > > need a test to assert this assumption.
> > > >   OK. I will add additional assert simulating
> > > "ruleset->access_masks[layer_level] =" kernel code.
> > > > >   Fs and network masks are ORed to not intersect with each other.
> > > > > Yes, they are ORed, and we need a test to check that. Noting is
> > > > currently testing this OR (and the different rule type consistency).
> > > > I'm suggesting to revamp the layout1.with_net test into
> > > > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> > > > addition of different types.
> > > 
> > >   I will move layout1.with_net test into net.c and rename it. Looks like
> > >   it just needed to add "ruleset->access_masks[layer_level] =" assert
> > >   because the test already has rule addition with different types.
> > 
> > The with_net test doesn't have FS rules, which is the main missing part.
> > You'll need to rely on the net.c helpers, use the hardcoded paths, and
> > only handle one access right of each type as I suggested above.
> > 
> 
>  This is with_net code:
> 
>   ....
>   /* Adds a network rule. */
> 	
> ASSERT_EQ(0, landlock_add_rule(ruleset_fd_net, LANDLOCK_RULE_NET_PORT,
> 				       &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));
> ....
> 
> It has FS rules - just after ruleset_fd_net rule inforced.
> Or maybe I missed something?

ruleset_fd_net and ruleset_fd are two different rulesets, and then
they create two different layers. We need to test support for FS and net
with the same ruleset/layer to check ruleset->access_masks[layer_level].

> 
> > > 
> > >   Do you have any more review updates so far?
> > 
> > That's all for this patch series. :)
> 
>   Ok. Thanks.
> > .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-23  7:09         ` Konstantin Meskhidze (A)
@ 2023-10-23  8:44           ` Mickaël Salaün
  2023-10-23  9:15             ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-23  8:44 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Mon, Oct 23, 2023 at 10:09:54AM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/20/2023 6:40 PM, Mickaël Salaün пишет:
> > On Fri, Oct 20, 2023 at 02:41:42PM +0300, Konstantin Meskhidze (A) wrote:
> > > 
> > > 
> > > 10/18/2023 3:32 PM, Mickaël Salaün пишет:
> > > > You can update the subject with:
> > > > "selftests/landlock: Add network tests"
> > > 
> > > Ok.
> > > > > On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze
> > > wrote:
> > > > > These test suites try to check edge cases for TCP sockets
> > > > > bind() and connect() actions.
> > > > > You can replace with that:
> > > > Add 77 test suites to check edge cases related to bind() and connect()
> > > > actions. They are defined with 6 fixtures and their variants:
> > > > > > > > protocol:
> > > > > * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
> > > > > As you already did, you can write one paragraph per fixture, but
> > > > starting by explaining the fixture and its related variants, and then
> > > > listing the tests and explaining their specificities. For instance:
> > > > > The "protocol" fixture is extended with 12 variants defined as a
> > > matrix
> > > > of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
> > > > stream/datagram socket. 4 related tests suites are defined:
> > > > * bind: Test bind combinations with increasingly more
> > > >    restricting domains.
> > > > * connect: Test connect combinations with increasingly more
> > > >    restricting domains.
> > > > ...
> > > 
> > >   Ok. Will be updated.
> > > > > s/ipv/IPv/g
> > > 
> > >   Got it. Thanks.
> > > > > > * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and
> > > unix
> > > > > sockets.
> > > > > * bind_unspec: Tests with non-landlocked/landlocked restrictions
> > > > > for bind action with AF_UNSPEC socket family.
> > > > > * connect_unspec: Tests with non-landlocked/landlocked restrictions
> > > > > for connect action with AF_UNSPEC socket family.
> > > > > > > ipv4:
> > > > > * from_unix_to_inet: Tests to make sure unix sockets' actions are not
> > > > > restricted by Landlock rules applied to TCP ones.
> > > > > > > tcp_layers:
> > > > > * ruleset_overlap.
> > > > > * ruleset_expand.
> > > > > > > mini:
> > > > > * network_access_rights: Tests with  legitimate access values.
> > > > > * unknown_access_rights: Tests with invalid attributes, out of access range.
> > > > > * inval:
> > > > >     - unhandled allowed access.
> > > > >     - zero access value.
> > > > > * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
> > > > > > > ipv4_tcp:
> > > > > * port_endianness: Tests with big/little endian port formats.
> > > > > > > port_specific:
> > > > > * bind_connect: Tests with specific port values.
> > > > > > > layout1:
> > > > > * with_net: Tests with network bind() socket action within
> > > > > filesystem directory access test.
> > > > > > > Test coverage for security/landlock is 94.5% of 932 lines
> > > according
> > > > > to gcc/gcov-11.
> > > > > > > Signed-off-by: Konstantin Meskhidze
> > > <konstantin.meskhidze@huawei.com>
> > > > > Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
> > > > > Co-developed-by:: Mickaël Salaün <mic@digikod.net>
> > > > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
> > > > > ---
> > > > > > > Changes since v12:
> > > > > * Renames port_zero to port_specific fixture.
> > > > > * Refactors port_specific test:
> > > > >     - Adds set_port() and get_binded_port() helpers.
> > > > >     - Adds checks for port 0, allowed by Landlock in this version.
> > > > >     - Adds checks for port 1023.
> > > > > * Refactors commit message.
> > > > > > > > +static void set_port(struct service_fixture *const srv,
> > > in_port_t port)
> > > > > +{
> > > > > +	switch (srv->protocol.domain) {
> > > > > +	case AF_UNSPEC:
> > > > > +	case AF_INET:
> > > > > +		srv->ipv4_addr.sin_port = port;
> > > > > We should call htons() here, and make port a uint16_t.
> > > 
> > >   Done.
> > > > > > +		return;
> > > > > +
> > > > > +	case AF_INET6:
> > > > > +		srv->ipv6_addr.sin6_port = port;
> > > > > +		return;
> > > > > +
> > > > > +	default:
> > > > > +		return;
> > > > > +	}
> > > > > +}
> > > > > +
> > > > > +static in_port_t get_binded_port(int socket_fd,
> > > > > The returned type should be uint16_t (i.e. host endianess).
> > > 
> > >   Done.
> > > > > > +				 const struct protocol_variant *const prot)
> > > > > +{
> > > > > +	struct sockaddr_in ipv4_addr;
> > > > > +	struct sockaddr_in6 ipv6_addr;
> > > > > +	socklen_t ipv4_addr_len, ipv6_addr_len;
> > > > > +
> > > > > +	/* Gets binded port. */
> > > > > +	switch (prot->domain) {
> > > > > +	case AF_UNSPEC:
> > > > > +	case AF_INET:
> > > > > +		ipv4_addr_len = sizeof(ipv4_addr);
> > > > > +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
> > > > > +		return ntohs(ipv4_addr.sin_port);
> > > > > +
> > > > > +	case AF_INET6:
> > > > > +		ipv6_addr_len = sizeof(ipv6_addr);
> > > > > +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
> > > > > +		return ntohs(ipv6_addr.sin6_port);
> > > > > +
> > > > > +	default:
> > > > > +		return 0;
> > > > > +	}
> > > > > +}
> > > > > These are good helpers!
> > > > > > > +FIXTURE_TEARDOWN(ipv4)
> > > > > +{
> > > > > +}
> > > > > +
> > > > > +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
> > > > > No FIXME should remain.
> > > 
> > >   Ok. Deleted.
> > > > > > +TEST_F(ipv4, from_unix_to_inet)
> > > > > > +TEST_F(mini, network_access_rights)
> > > > > +{
> > > > > +	const struct landlock_ruleset_attr ruleset_attr = {
> > > > > +		.handled_access_net = ACCESS_ALL,
> > > > > +	};
> > > > > +	struct landlock_net_port_attr net_service = {
> > > > > Please rename to "net_port" everywhere.
> > > 
> > >   Done.
> > > > > > +TEST_F(port_specific, bind_connect)
> > > > > +{
> > > > > +	int socket_fd, ret;
> > > > > +
> > > > > +	/* Adds the first rule layer with bind and connect actions. */
> > > > > +	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_port_attr tcp_bind_connect_zero = {
> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
> > > > > +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
> > > > > +			.port = htons(0),
> > > > > We don't need any htons() calls anymore. It doesn't change the 0
> > > value
> > > > in this case but this is not correct.
> > > 
> > >  Yep. We call htons(port) in landlock_append_net_rule().
> > >  Thanks.
> > > > > > +		};
> > > > > +
> > > > > Useless new line.
> > > 
> > >   Ok. Thanks.
> > > > > > +		int ruleset_fd;
> > > > > +
> > > > > +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
> > > > > +						     sizeof(ruleset_attr), 0);
> > > > > +		ASSERT_LE(0, ruleset_fd);
> > > > > +
> > > > > +		/* Checks zero port value on bind and connect actions. */
> > > > > +		EXPECT_EQ(0,
> > > > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> > > > > +					    &tcp_bind_connect_zero, 0));
> > > > > +
> > > > > +		enforce_ruleset(_metadata, ruleset_fd);
> > > > > +		EXPECT_EQ(0, close(ruleset_fd));
> > > > > +	}
> > > > > +
> > > > > +	socket_fd = socket_variant(&self->srv0);
> > > > > +	ASSERT_LE(0, socket_fd);
> > > > > +
> > > > > +	/* Sets address port to 0 for both protocol families. */
> > > > > +	set_port(&self->srv0, htons(0));
> > > > > ditto
> > > > > > +
> > > > > +	/* Binds on port 0. */
> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	} else {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > If the results are the same, no need to add an if block.
> > > 
> > >   Right. Updated.
> > > > > > +	}
> > > > > +
> > > > > +	/* Connects on port 0. */
> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		EXPECT_EQ(-ECONNREFUSED, ret);
> > > > > +	} else {
> > > > > +		EXPECT_EQ(-ECONNREFUSED, ret);
> > > > > +	}
> > > > > ditto
> > > >  Updated.
> > > > > +
> > > > > +	/* Binds on port 0. */
> > > > > Please close sockets once they are used, and recreate one for
> > > another
> > > > bind/connect to avoid wrong checks.
> > > 
> > >   Ok. But I can reuse socket_fd after closeing a socket. Correct?
> > 
> > It would be clearer to have one variable for the client socket
> > (connect_fd) and another variable for the server socket (bind_fd).
> > But once the socket is closed, you can reuse the same variable by
> > storing a new socket in it. You then only need two variables for sockets
> > in this test.
> 
>  Ok. Thanks.
> > 
> > > > > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	} else {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	}
> > > > > Why this second bind() block? Furthermore, it is using the same
> > > > socket_fd.
> > 
> > Is this block useful?
> 
>   For a self-connected socket after connection try we need to rebind it
> again. I checked this logic in a small standalone test (with gdb on). So for
> 2 sockets (differnt fds) there is no need to do that.
> > 
> > > 
> > >   I will refactor the code this way -  sockets will be recreated for each
> > > bind/connect, and I prefer to use self-connected sockets (use one socket
> > > descriptor) in these tests to make code simpler; testing logic remains the
> > > same way as if we have 2 sockets.
> > > 
> > > What do you think???
> > 
> > I find it confusing to use self-connected sockets, it's not clear at all
> > what is going on, and AFAIK it doesn't reflect real use cases. Please
> > don't do that.
> > 
> > Using the same variable for both bind and connect socket will lead to
> > issues difficult to debug, and leaked FDs. For instance with the bind +
> > get_binded_port + connect test you should use one variable per socket.
> > To make it easier to read, please follow this rule everywhere (the only
> > case when this is done seems to be with the port_specific.bind_connect
> > test).
> 
>   OK. I will use 2 fds for bind and connect sockets.
> > 
> > > 
> > > > > > +
> > > > > +	/* Sets binded port for both protocol families. */
> > > > > +	set_port(&self->srv0,
> > > > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
> > > > > Ditto, these two endianess translations are useless.
> > > 
> > >   Updated. Thanks.
> > > > > You can also add this to make sure the returned port is not 0:
> > > > port = get_binded_port(socket_fd, &variant->prot);
> > > > EXPECT_NE(0, port);
> > > > set_port(&self->srv0, port);
> > > 
> > >   Ok. Thanks for the tip.
> > > > > > +
> > > > > +	/* Connects on the binded port. */
> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		/* Denied by Landlock. */
> > > > > +		EXPECT_EQ(-EACCES, ret);
> > > > > +	} else {
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	}
> > > > > +
> > > > > +	EXPECT_EQ(0, close(socket_fd));
> > > > > +
> > > > > > > > +	/* Adds the second rule layer with just bind action. */
> > > > > There is not only bind actions here.
> > > 
> > >   Right.
> > > > > This second part of the tests should be in a dedicated
> > > > TEST_F(port_specific, bind_1023).
> > > 
> > >   Got it.
> > > > > > +	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_port_attr tcp_bind_zero = {
> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> > > > > +			.port = htons(0),
> > > > > +		};
> > > > > +
> > > > > Useless new lines.
> > > 
> > >   Got it.
> > > > > > +		/* A rule with port value less than 1024. */
> > > > > +		const struct landlock_net_port_attr tcp_bind_lower_range = {
> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
> > > > > +			.port = htons(1023),
> > > > > +		};
> > > > > +
> > > > > Useless new line.
> > > 
> > >   Got it.
> > > > > > +		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_PORT,
> > > > > +					    &tcp_bind_lower_range, 0));
> > > > > +		ASSERT_EQ(0,
> > > > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
> > > > > +					    &tcp_bind_zero, 0));
> > > > > +
> > > > > +		enforce_ruleset(_metadata, ruleset_fd);
> > > > > +		EXPECT_EQ(0, close(ruleset_fd));
> > > > > +	}
> > > > > +
> > > > > +	socket_fd = socket_variant(&self->srv0);
> > > > > We must have one socket FD dedicated to bind an another
> > > dedicated to
> > > > connect, e.g. bind_fd and connect_fd, an close them after each use,
> > > > otherwise tests might be inconsistent.
> > > 
> > >   Why can't we use self-connected sockets here? Why tests might be
> > > inconsistent? Tests will be working the same way as if we have 2 sockets,
> > > plus the code is simpler.
> > 
> > AFAIK it doesn't reflect real use cases of sockets, and I find it really
> > confusing. Where did you see this kind of usage?
> > 
> > Test might be inconsistent for instance if you change the port from 1023
> > to 1024 and you adjust the (denied by system) EXPECT_EQ(-EACCES, ret),
> > you'll get a new error in the following block, which doesn't make sense
> > at first, but then you realize it is because the socket is already
> > binded.  To avoid this kind of issues, and avoid leaking FDs, please use
> > a socket per usage and close them before testing something else.
> 
>  Ok. Got it.
> > 
> > > > > > +	ASSERT_LE(0, socket_fd);
> > > > > +
> > > > > +	/* Sets address port to 1023 for both protocol families. */
> > > > > +	set_port(&self->srv0, htons(1023));
> > > > > +
> > > > > +	/* Binds on port 1023. */
> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > No need to add this check if the result is the same for
> > > sandboxed and
> > > > not sandboxed tests.
> > > 
> > >  Ok. Thanks.
> > > > > Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and
> > > clear_cap()
> > > > around this bind_variant() to make this test useful.
> > > > > You will also need to patch common.h like this:
> > > > @@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
> > > >          cap_t cap_p;
> > > >          /* Only these three capabilities are useful for the tests. */
> > > >          const cap_value_t caps[] = {
> > > > +               /* clang-format off */
> > > >                  CAP_DAC_OVERRIDE,
> > > >                  CAP_MKNOD,
> > > >                  CAP_SYS_ADMIN,
> > > >                  CAP_SYS_CHROOT,
> > > > +               CAP_NET_BIND_SERVICE,
> > > > +               /* clang-format on */
> > > >          };
> > > 
> > >  OK. Thanks.
> > > > > > +		/* Denied by the system. */
> > > > > +		EXPECT_EQ(-EACCES, ret);
> > > > > +	} else {
> > > > > +		/* Denied by the system. */
> > > > > +		EXPECT_EQ(-EACCES, ret);
> > > > > +	}
> > > > > +
> > > > > I don't see why the following part is useful. Why did you add
> > > it?
> > >   Binding to ports < 1024 are forbidden by the system, not by Landlock.
> > >   I added a rule with port 1023 to make sure it works as expected.
> > 
> > Landlock, as any LSM, can only add more restrictions. That's why it
> > would make more sense to test with CAP_NET_BIND_SERVICE, to make sure
> > Landlock rules work the same with this kind of privileged ports, but you
> > can test both cases (all within the same TEST_F though, and without
> > other tests).
> 
>  Do you mean during the test to set CAP_NET_BIND_SERVICE, check it with
> landlock (it will success), then switch  CAP_NET_BIND_SERVICE cap off and
> bind it again ( will be refused by the system)?
> Am I correct?

Yes, you can use something like this:

set_cap(_metadata, CAP_NET_BIND_SERVICE);
ret = bind_variant(socket_fd, &self->srv0);
clear_cap(_metadata, CAP_NET_BIND_SERVICE);


> > 
> > > 
> > > > Why tcp_bind_zero?
> > >    Beacause it's a bind action with port zero rule.
> > 
> > Yes but I don't see why it's relevant to test that here, because it was
> > tested just before.
> > 
>   OK. I just leave binding to 1023 port here.
>   I'm thinking to add binding to 1024 port then to show that this port is
> allowed by the system but denied by landlock ( we have just rule with 1023
> port).
>   What do you think?

Yes, you can do that. I guess you could use test_bind_and_connect() for
this test. You can group these checks (ports 1023 and 1024) in the same
dedicated TEST_F.

> > > 
> > > > > The other parts are good though!
> > > > > > +	/* Sets address port to 0 for both protocol families. */
> > > > > +	set_port(&self->srv0, htons(0));
> > > > > +
> > > > > +	/* Binds on port 0. */
> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	} else {
> > > > > +		/* Binds to a random port within ip_local_port_range. */
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	}
> > > > > +
> > > > > +	/* Sets binded port for both protocol families. */
> > > > > +	set_port(&self->srv0,
> > > > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
> > > > > +
> > > > > +	/* Connects on the binded port. */
> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
> > > > > +		/* Denied by Landlock. */
> > > > > +		EXPECT_EQ(-EACCES, ret);
> > > > > +	} else {
> > > > > +		EXPECT_EQ(0, ret);
> > > > > +	}
> > > > > +
> > > > > +	EXPECT_EQ(0, close(socket_fd));
> > > > > +}
> > > > > +
> > > > > +TEST_HARNESS_MAIN
> > > > > --
> > > > > 2.25.1
> > > > > > .
> > .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-23  8:30               ` Mickaël Salaün
@ 2023-10-23  8:56                 ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-23  8:56 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/23/2023 11:30 AM, Mickaël Salaün пишет:
> On Mon, Oct 23, 2023 at 10:23:35AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/20/2023 6:41 PM, Mickaël Salaün пишет:
>> > On Fri, Oct 20, 2023 at 02:58:31PM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 10/20/2023 12:49 PM, Mickaël Salaün пишет:
>> > > > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> > > > > > > > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > > > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> > 
>> > > > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > > > > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > > > > > --- a/security/landlock/ruleset.c
>> > > > > > > +++ b/security/landlock/ruleset.c
>> > > > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > > > >  	refcount_set(&new_ruleset->usage, 1);
>> > > > > > >  	mutex_init(&new_ruleset->lock);
>> > > > > > >  	new_ruleset->root_inode = RB_ROOT;
>> > > > > > > +
>> > > > > > > +#if IS_ENABLED(CONFIG_INET)
>> > > > > > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > > > +
>> > > > > > >  	new_ruleset->num_layers = num_layers;
>> > > > > > >  	/*
>> > > > > > >  	 * hierarchy = NULL
>> > > > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > > > >  }
>> > > > > > > > >  struct landlock_ruleset *
>> > > > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > > > > > +			const access_mask_t net_access_mask)
>> > > > > > >  {
>> > > > > > >  	struct landlock_ruleset *new_ruleset;
>> > > > > > > > >  	/* Informs about useless ruleset. */
>> > > > > > > -	if (!fs_access_mask)
>> > > > > > > +	if (!fs_access_mask && !net_access_mask)
>> > > > > > >  		return ERR_PTR(-ENOMSG);
>> > > > > > >  	new_ruleset = create_ruleset(1);
>> > > > > > > -	if (!IS_ERR(new_ruleset))
>> > > > > > > +	if (IS_ERR(new_ruleset))
>> > > > > > > +		return new_ruleset;
>> > > > > > > +	if (fs_access_mask)
>> > > > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > > > > > +	if (net_access_mask)
>> > > > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > > > > > > This is good, but it is not tested: we need to add a test that
>> > > > > both
>> > > > > > handle FS and net restrictions. You can add one in net.c, just handling
>> > > > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > > > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > > > > > that open("/") is denied, open("/dev") is allowed, and and only the
>> > > > > > allowed port is allowed with bind(). This test should be simple and can
>> > > > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > > > > > after port_endianness. fcntl.h should then be included by net.c
>> > > > > > >   Ok.
>> > > > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
>> > > > > but it
>> > > > > > >   Yep. I added this kind of nest in fs_test.c to test both
>> > > fs and network
>> > > > > rules together.
>> > > > > > is not complete. You can revamp this test and move it to net.c
>> > > > > > following the above suggestions, keeping it consistent with other tests
>> > > > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > > > > > > This test must failed if we change
>> > > > > "ruleset->access_masks[layer_level] |="
>> > > > > > to "ruleset->access_masks[layer_level] =" in
>> > > > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> > > > > > >   Do you want to change it? Why?
>> > > > > The kernel code is correct and must not be changed. However, if
>> > > by
>> > > > mistake we change it and remove the OR, a test should catch that. We
>> > > > need a test to assert this assumption.
>> > > >   OK. I will add additional assert simulating
>> > > "ruleset->access_masks[layer_level] =" kernel code.
>> > > > >   Fs and network masks are ORed to not intersect with each other.
>> > > > > Yes, they are ORed, and we need a test to check that. Noting is
>> > > > currently testing this OR (and the different rule type consistency).
>> > > > I'm suggesting to revamp the layout1.with_net test into
>> > > > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
>> > > > addition of different types.
>> > > 
>> > >   I will move layout1.with_net test into net.c and rename it. Looks like
>> > >   it just needed to add "ruleset->access_masks[layer_level] =" assert
>> > >   because the test already has rule addition with different types.
>> > 
>> > The with_net test doesn't have FS rules, which is the main missing part.
>> > You'll need to rely on the net.c helpers, use the hardcoded paths, and
>> > only handle one access right of each type as I suggested above.
>> > 
>> 
>>  This is with_net code:
>> 
>>   ....
>>   /* Adds a network rule. */
>> 	
>> ASSERT_EQ(0, landlock_add_rule(ruleset_fd_net, LANDLOCK_RULE_NET_PORT,
>> 				       &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));
>> ....
>> 
>> It has FS rules - just after ruleset_fd_net rule inforced.
>> Or maybe I missed something?
> 
> ruleset_fd_net and ruleset_fd are two different rulesets, and then
> they create two different layers. We need to test support for FS and net
> with the same ruleset/layer to check ruleset->access_masks[layer_level].
> 
   Got your point here. Thanks.
>> 
>> > > 
>> > >   Do you have any more review updates so far?
>> > 
>> > That's all for this patch series. :)
>> 
>>   Ok. Thanks.
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network
  2023-10-23  8:44           ` Mickaël Salaün
@ 2023-10-23  9:15             ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-23  9:15 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/23/2023 11:44 AM, Mickaël Salaün пишет:
> On Mon, Oct 23, 2023 at 10:09:54AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/20/2023 6:40 PM, Mickaël Salaün пишет:
>> > On Fri, Oct 20, 2023 at 02:41:42PM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 10/18/2023 3:32 PM, Mickaël Salaün пишет:
>> > > > You can update the subject with:
>> > > > "selftests/landlock: Add network tests"
>> > > 
>> > > Ok.
>> > > > > On Mon, Oct 16, 2023 at 09:50:28AM +0800, Konstantin Meskhidze
>> > > wrote:
>> > > > > These test suites try to check edge cases for TCP sockets
>> > > > > bind() and connect() actions.
>> > > > > You can replace with that:
>> > > > Add 77 test suites to check edge cases related to bind() and connect()
>> > > > actions. They are defined with 6 fixtures and their variants:
>> > > > > > > > protocol:
>> > > > > * bind: Tests with non-landlocked/landlocked ipv4, ipv6 and unix sockets.
>> > > > > As you already did, you can write one paragraph per fixture, but
>> > > > starting by explaining the fixture and its related variants, and then
>> > > > listing the tests and explaining their specificities. For instance:
>> > > > > The "protocol" fixture is extended with 12 variants defined as a
>> > > matrix
>> > > > of: sandboxed/not-sandboxed, IPv4/IPv6/unix network domain, and
>> > > > stream/datagram socket. 4 related tests suites are defined:
>> > > > * bind: Test bind combinations with increasingly more
>> > > >    restricting domains.
>> > > > * connect: Test connect combinations with increasingly more
>> > > >    restricting domains.
>> > > > ...
>> > > 
>> > >   Ok. Will be updated.
>> > > > > s/ipv/IPv/g
>> > > 
>> > >   Got it. Thanks.
>> > > > > > * connect: Tests with non-landlocked/landlocked ipv4, ipv6 and
>> > > unix
>> > > > > sockets.
>> > > > > * bind_unspec: Tests with non-landlocked/landlocked restrictions
>> > > > > for bind action with AF_UNSPEC socket family.
>> > > > > * connect_unspec: Tests with non-landlocked/landlocked restrictions
>> > > > > for connect action with AF_UNSPEC socket family.
>> > > > > > > ipv4:
>> > > > > * from_unix_to_inet: Tests to make sure unix sockets' actions are not
>> > > > > restricted by Landlock rules applied to TCP ones.
>> > > > > > > tcp_layers:
>> > > > > * ruleset_overlap.
>> > > > > * ruleset_expand.
>> > > > > > > mini:
>> > > > > * network_access_rights: Tests with  legitimate access values.
>> > > > > * unknown_access_rights: Tests with invalid attributes, out of access range.
>> > > > > * inval:
>> > > > >     - unhandled allowed access.
>> > > > >     - zero access value.
>> > > > > * tcp_port_overflow: Tests with wrong port values more than U16_MAX.
>> > > > > > > ipv4_tcp:
>> > > > > * port_endianness: Tests with big/little endian port formats.
>> > > > > > > port_specific:
>> > > > > * bind_connect: Tests with specific port values.
>> > > > > > > layout1:
>> > > > > * with_net: Tests with network bind() socket action within
>> > > > > filesystem directory access test.
>> > > > > > > Test coverage for security/landlock is 94.5% of 932 lines
>> > > according
>> > > > > to gcc/gcov-11.
>> > > > > > > Signed-off-by: Konstantin Meskhidze
>> > > <konstantin.meskhidze@huawei.com>
>> > > > > Link: https://lore.kernel.org/r/20230920092641.832134-11-konstantin.meskhidze@huawei.com
>> > > > > Co-developed-by:: Mickaël Salaün <mic@digikod.net>
>> > > > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> > > > > ---
>> > > > > > > Changes since v12:
>> > > > > * Renames port_zero to port_specific fixture.
>> > > > > * Refactors port_specific test:
>> > > > >     - Adds set_port() and get_binded_port() helpers.
>> > > > >     - Adds checks for port 0, allowed by Landlock in this version.
>> > > > >     - Adds checks for port 1023.
>> > > > > * Refactors commit message.
>> > > > > > > > +static void set_port(struct service_fixture *const srv,
>> > > in_port_t port)
>> > > > > +{
>> > > > > +	switch (srv->protocol.domain) {
>> > > > > +	case AF_UNSPEC:
>> > > > > +	case AF_INET:
>> > > > > +		srv->ipv4_addr.sin_port = port;
>> > > > > We should call htons() here, and make port a uint16_t.
>> > > 
>> > >   Done.
>> > > > > > +		return;
>> > > > > +
>> > > > > +	case AF_INET6:
>> > > > > +		srv->ipv6_addr.sin6_port = port;
>> > > > > +		return;
>> > > > > +
>> > > > > +	default:
>> > > > > +		return;
>> > > > > +	}
>> > > > > +}
>> > > > > +
>> > > > > +static in_port_t get_binded_port(int socket_fd,
>> > > > > The returned type should be uint16_t (i.e. host endianess).
>> > > 
>> > >   Done.
>> > > > > > +				 const struct protocol_variant *const prot)
>> > > > > +{
>> > > > > +	struct sockaddr_in ipv4_addr;
>> > > > > +	struct sockaddr_in6 ipv6_addr;
>> > > > > +	socklen_t ipv4_addr_len, ipv6_addr_len;
>> > > > > +
>> > > > > +	/* Gets binded port. */
>> > > > > +	switch (prot->domain) {
>> > > > > +	case AF_UNSPEC:
>> > > > > +	case AF_INET:
>> > > > > +		ipv4_addr_len = sizeof(ipv4_addr);
>> > > > > +		getsockname(socket_fd, &ipv4_addr, &ipv4_addr_len);
>> > > > > +		return ntohs(ipv4_addr.sin_port);
>> > > > > +
>> > > > > +	case AF_INET6:
>> > > > > +		ipv6_addr_len = sizeof(ipv6_addr);
>> > > > > +		getsockname(socket_fd, &ipv6_addr, &ipv6_addr_len);
>> > > > > +		return ntohs(ipv6_addr.sin6_port);
>> > > > > +
>> > > > > +	default:
>> > > > > +		return 0;
>> > > > > +	}
>> > > > > +}
>> > > > > These are good helpers!
>> > > > > > > +FIXTURE_TEARDOWN(ipv4)
>> > > > > +{
>> > > > > +}
>> > > > > +
>> > > > > +// Kernel FIXME: tcp_sandbox_with_tcp and tcp_sandbox_with_udp
>> > > > > No FIXME should remain.
>> > > 
>> > >   Ok. Deleted.
>> > > > > > +TEST_F(ipv4, from_unix_to_inet)
>> > > > > > +TEST_F(mini, network_access_rights)
>> > > > > +{
>> > > > > +	const struct landlock_ruleset_attr ruleset_attr = {
>> > > > > +		.handled_access_net = ACCESS_ALL,
>> > > > > +	};
>> > > > > +	struct landlock_net_port_attr net_service = {
>> > > > > Please rename to "net_port" everywhere.
>> > > 
>> > >   Done.
>> > > > > > +TEST_F(port_specific, bind_connect)
>> > > > > +{
>> > > > > +	int socket_fd, ret;
>> > > > > +
>> > > > > +	/* Adds the first rule layer with bind and connect actions. */
>> > > > > +	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_port_attr tcp_bind_connect_zero = {
>> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP |
>> > > > > +					  LANDLOCK_ACCESS_NET_CONNECT_TCP,
>> > > > > +			.port = htons(0),
>> > > > > We don't need any htons() calls anymore. It doesn't change the 0
>> > > value
>> > > > in this case but this is not correct.
>> > > 
>> > >  Yep. We call htons(port) in landlock_append_net_rule().
>> > >  Thanks.
>> > > > > > +		};
>> > > > > +
>> > > > > Useless new line.
>> > > 
>> > >   Ok. Thanks.
>> > > > > > +		int ruleset_fd;
>> > > > > +
>> > > > > +		ruleset_fd = landlock_create_ruleset(&ruleset_attr,
>> > > > > +						     sizeof(ruleset_attr), 0);
>> > > > > +		ASSERT_LE(0, ruleset_fd);
>> > > > > +
>> > > > > +		/* Checks zero port value on bind and connect actions. */
>> > > > > +		EXPECT_EQ(0,
>> > > > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> > > > > +					    &tcp_bind_connect_zero, 0));
>> > > > > +
>> > > > > +		enforce_ruleset(_metadata, ruleset_fd);
>> > > > > +		EXPECT_EQ(0, close(ruleset_fd));
>> > > > > +	}
>> > > > > +
>> > > > > +	socket_fd = socket_variant(&self->srv0);
>> > > > > +	ASSERT_LE(0, socket_fd);
>> > > > > +
>> > > > > +	/* Sets address port to 0 for both protocol families. */
>> > > > > +	set_port(&self->srv0, htons(0));
>> > > > > ditto
>> > > > > > +
>> > > > > +	/* Binds on port 0. */
>> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	} else {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > If the results are the same, no need to add an if block.
>> > > 
>> > >   Right. Updated.
>> > > > > > +	}
>> > > > > +
>> > > > > +	/* Connects on port 0. */
>> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		EXPECT_EQ(-ECONNREFUSED, ret);
>> > > > > +	} else {
>> > > > > +		EXPECT_EQ(-ECONNREFUSED, ret);
>> > > > > +	}
>> > > > > ditto
>> > > >  Updated.
>> > > > > +
>> > > > > +	/* Binds on port 0. */
>> > > > > Please close sockets once they are used, and recreate one for
>> > > another
>> > > > bind/connect to avoid wrong checks.
>> > > 
>> > >   Ok. But I can reuse socket_fd after closeing a socket. Correct?
>> > 
>> > It would be clearer to have one variable for the client socket
>> > (connect_fd) and another variable for the server socket (bind_fd).
>> > But once the socket is closed, you can reuse the same variable by
>> > storing a new socket in it. You then only need two variables for sockets
>> > in this test.
>> 
>>  Ok. Thanks.
>> > 
>> > > > > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	} else {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	}
>> > > > > Why this second bind() block? Furthermore, it is using the same
>> > > > socket_fd.
>> > 
>> > Is this block useful?
>> 
>>   For a self-connected socket after connection try we need to rebind it
>> again. I checked this logic in a small standalone test (with gdb on). So for
>> 2 sockets (differnt fds) there is no need to do that.
>> > 
>> > > 
>> > >   I will refactor the code this way -  sockets will be recreated for each
>> > > bind/connect, and I prefer to use self-connected sockets (use one socket
>> > > descriptor) in these tests to make code simpler; testing logic remains the
>> > > same way as if we have 2 sockets.
>> > > 
>> > > What do you think???
>> > 
>> > I find it confusing to use self-connected sockets, it's not clear at all
>> > what is going on, and AFAIK it doesn't reflect real use cases. Please
>> > don't do that.
>> > 
>> > Using the same variable for both bind and connect socket will lead to
>> > issues difficult to debug, and leaked FDs. For instance with the bind +
>> > get_binded_port + connect test you should use one variable per socket.
>> > To make it easier to read, please follow this rule everywhere (the only
>> > case when this is done seems to be with the port_specific.bind_connect
>> > test).
>> 
>>   OK. I will use 2 fds for bind and connect sockets.
>> > 
>> > > 
>> > > > > > +
>> > > > > +	/* Sets binded port for both protocol families. */
>> > > > > +	set_port(&self->srv0,
>> > > > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
>> > > > > Ditto, these two endianess translations are useless.
>> > > 
>> > >   Updated. Thanks.
>> > > > > You can also add this to make sure the returned port is not 0:
>> > > > port = get_binded_port(socket_fd, &variant->prot);
>> > > > EXPECT_NE(0, port);
>> > > > set_port(&self->srv0, port);
>> > > 
>> > >   Ok. Thanks for the tip.
>> > > > > > +
>> > > > > +	/* Connects on the binded port. */
>> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		/* Denied by Landlock. */
>> > > > > +		EXPECT_EQ(-EACCES, ret);
>> > > > > +	} else {
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	}
>> > > > > +
>> > > > > +	EXPECT_EQ(0, close(socket_fd));
>> > > > > +
>> > > > > > > > +	/* Adds the second rule layer with just bind action. */
>> > > > > There is not only bind actions here.
>> > > 
>> > >   Right.
>> > > > > This second part of the tests should be in a dedicated
>> > > > TEST_F(port_specific, bind_1023).
>> > > 
>> > >   Got it.
>> > > > > > +	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_port_attr tcp_bind_zero = {
>> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> > > > > +			.port = htons(0),
>> > > > > +		};
>> > > > > +
>> > > > > Useless new lines.
>> > > 
>> > >   Got it.
>> > > > > > +		/* A rule with port value less than 1024. */
>> > > > > +		const struct landlock_net_port_attr tcp_bind_lower_range = {
>> > > > > +			.allowed_access = LANDLOCK_ACCESS_NET_BIND_TCP,
>> > > > > +			.port = htons(1023),
>> > > > > +		};
>> > > > > +
>> > > > > Useless new line.
>> > > 
>> > >   Got it.
>> > > > > > +		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_PORT,
>> > > > > +					    &tcp_bind_lower_range, 0));
>> > > > > +		ASSERT_EQ(0,
>> > > > > +			  landlock_add_rule(ruleset_fd, LANDLOCK_RULE_NET_PORT,
>> > > > > +					    &tcp_bind_zero, 0));
>> > > > > +
>> > > > > +		enforce_ruleset(_metadata, ruleset_fd);
>> > > > > +		EXPECT_EQ(0, close(ruleset_fd));
>> > > > > +	}
>> > > > > +
>> > > > > +	socket_fd = socket_variant(&self->srv0);
>> > > > > We must have one socket FD dedicated to bind an another
>> > > dedicated to
>> > > > connect, e.g. bind_fd and connect_fd, an close them after each use,
>> > > > otherwise tests might be inconsistent.
>> > > 
>> > >   Why can't we use self-connected sockets here? Why tests might be
>> > > inconsistent? Tests will be working the same way as if we have 2 sockets,
>> > > plus the code is simpler.
>> > 
>> > AFAIK it doesn't reflect real use cases of sockets, and I find it really
>> > confusing. Where did you see this kind of usage?
>> > 
>> > Test might be inconsistent for instance if you change the port from 1023
>> > to 1024 and you adjust the (denied by system) EXPECT_EQ(-EACCES, ret),
>> > you'll get a new error in the following block, which doesn't make sense
>> > at first, but then you realize it is because the socket is already
>> > binded.  To avoid this kind of issues, and avoid leaking FDs, please use
>> > a socket per usage and close them before testing something else.
>> 
>>  Ok. Got it.
>> > 
>> > > > > > +	ASSERT_LE(0, socket_fd);
>> > > > > +
>> > > > > +	/* Sets address port to 1023 for both protocol families. */
>> > > > > +	set_port(&self->srv0, htons(1023));
>> > > > > +
>> > > > > +	/* Binds on port 1023. */
>> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > No need to add this check if the result is the same for
>> > > sandboxed and
>> > > > not sandboxed tests.
>> > > 
>> > >  Ok. Thanks.
>> > > > > Instead, use set_cap(_metadata, CAP_NET_BIND_SERVICE) and
>> > > clear_cap()
>> > > > around this bind_variant() to make this test useful.
>> > > > > You will also need to patch common.h like this:
>> > > > @@ -112,10 +112,13 @@ static void _init_caps(struct __test_metadata *const _metadata, bool drop_all)
>> > > >          cap_t cap_p;
>> > > >          /* Only these three capabilities are useful for the tests. */
>> > > >          const cap_value_t caps[] = {
>> > > > +               /* clang-format off */
>> > > >                  CAP_DAC_OVERRIDE,
>> > > >                  CAP_MKNOD,
>> > > >                  CAP_SYS_ADMIN,
>> > > >                  CAP_SYS_CHROOT,
>> > > > +               CAP_NET_BIND_SERVICE,
>> > > > +               /* clang-format on */
>> > > >          };
>> > > 
>> > >  OK. Thanks.
>> > > > > > +		/* Denied by the system. */
>> > > > > +		EXPECT_EQ(-EACCES, ret);
>> > > > > +	} else {
>> > > > > +		/* Denied by the system. */
>> > > > > +		EXPECT_EQ(-EACCES, ret);
>> > > > > +	}
>> > > > > +
>> > > > > I don't see why the following part is useful. Why did you add
>> > > it?
>> > >   Binding to ports < 1024 are forbidden by the system, not by Landlock.
>> > >   I added a rule with port 1023 to make sure it works as expected.
>> > 
>> > Landlock, as any LSM, can only add more restrictions. That's why it
>> > would make more sense to test with CAP_NET_BIND_SERVICE, to make sure
>> > Landlock rules work the same with this kind of privileged ports, but you
>> > can test both cases (all within the same TEST_F though, and without
>> > other tests).
>> 
>>  Do you mean during the test to set CAP_NET_BIND_SERVICE, check it with
>> landlock (it will success), then switch  CAP_NET_BIND_SERVICE cap off and
>> bind it again ( will be refused by the system)?
>> Am I correct?
> 
> Yes, you can use something like this:
> 
> set_cap(_metadata, CAP_NET_BIND_SERVICE);
> ret = bind_variant(socket_fd, &self->srv0);
> clear_cap(_metadata, CAP_NET_BIND_SERVICE);
> 
  yep. I think the same way. Thanks.
> 
>> > 
>> > > 
>> > > > Why tcp_bind_zero?
>> > >    Beacause it's a bind action with port zero rule.
>> > 
>> > Yes but I don't see why it's relevant to test that here, because it was
>> > tested just before.
>> > 
>>   OK. I just leave binding to 1023 port here.
>>   I'm thinking to add binding to 1024 port then to show that this port is
>> allowed by the system but denied by landlock ( we have just rule with 1023
>> port).
>>   What do you think?
> 
> Yes, you can do that. I guess you could use test_bind_and_connect() for
> this test. You can group these checks (ports 1023 and 1024) in the same
> dedicated TEST_F.

   Don't think that test_bind_and_connect() has all functionality for 
this test - it will fail without set_cap(_metadata, 
CAP_NET_BIND_SERVICE) on.  I prefer to use bind_variant/connect_variant 
functions here.
> 
>> > > 
>> > > > > The other parts are good though!
>> > > > > > +	/* Sets address port to 0 for both protocol families. */
>> > > > > +	set_port(&self->srv0, htons(0));
>> > > > > +
>> > > > > +	/* Binds on port 0. */
>> > > > > +	ret = bind_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	} else {
>> > > > > +		/* Binds to a random port within ip_local_port_range. */
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	}
>> > > > > +
>> > > > > +	/* Sets binded port for both protocol families. */
>> > > > > +	set_port(&self->srv0,
>> > > > > +		 htons(get_binded_port(socket_fd, &variant->prot)));
>> > > > > +
>> > > > > +	/* Connects on the binded port. */
>> > > > > +	ret = connect_variant(socket_fd, &self->srv0);
>> > > > > +	if (is_restricted(&variant->prot, variant->sandbox)) {
>> > > > > +		/* Denied by Landlock. */
>> > > > > +		EXPECT_EQ(-EACCES, ret);
>> > > > > +	} else {
>> > > > > +		EXPECT_EQ(0, ret);
>> > > > > +	}
>> > > > > +
>> > > > > +	EXPECT_EQ(0, close(socket_fd));
>> > > > > +}
>> > > > > +
>> > > > > +TEST_HARNESS_MAIN
>> > > > > --
>> > > > > 2.25.1
>> > > > > > .
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20  9:49       ` Mickaël Salaün
  2023-10-20 11:58         ` Konstantin Meskhidze (A)
@ 2023-10-24  2:51         ` Konstantin Meskhidze (A)
  2023-10-24  3:18         ` Konstantin Meskhidze (A)
  2 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-24  2:51 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/20/2023 12:49 PM, Mickaël Salaün пишет:
> On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> > > This commit adds network rules support in the ruleset management
>> > 
>> > Here are some advices to better write commit messages:
>> > https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
>> > The "Describe your changes in imperative mood" part is important for
>> > this commit and others. Most of this patch series' commit messages need
>> > small updates.
>> 
>>  Ok. I will refactor commit messages with "imperative mood". Thanks.
>> > 
>> > > 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
>> > 
>> > Please explain the "why" (when it makes sense) instead of just listing
>> > the "what".
>> 
>>   Ok.
>> 
>> > 
>> > > rights can be stored. Implement socket_bind() and socket_connect()
>> > > LSM hooks, which enables to restrict TCP socket binding and connection
>> > > to specific ports.
>> > 
>> > I reworded and moved this part in last:
>> > > For the file system, a file descriptor is a direct access to a file/data.
>> > > But for the network, it's impossible to identify for which data/peer a
>> > > newly created socket will give access to, it's needed to wait for a
>> > > connect or bind request to identify the use case for this socket.
>> > > That's why the access rights (related to ports) are tied to an opened
>> > > socket, but this would not align with the way Landlock access control
>> > > works for the filesystem [2].
>> 
>>    Thanks.
>> > 
>> > Please add empty line to split paragraphs.
>> 
>>   Got it.
>> > 
>> > > 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 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).
>> > 
>> > For the file system, a file descriptor is a direct access to a file/data.
>> > However, for network sockets, we cannot identify for which data or peer a newly
>> > created socket will give access to. Indeed, we need to wait for a connect or
>> > bind request to identify the use case for this socket.
>> > 
>> > Access rights are not tied to socket file descriptors. Instead, bind and
>> > connect actions are controlled by the task's domain.  As for the filesystem, a
>> > directory file descriptor may enable to open another file (i.e. a new data
>> > item), but this opening is restricted by the task's domain, not the file
>> > descriptor's access rights [2].
>> > 
>> > > 
>> > > [1]
>> > > https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > > [2]
>> > > https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>> > 
>> > [1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > [2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>>   Thanks.
>> > 
>> > > 
>> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> > > Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
>> > > [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
>> > > to add_rule_net_service()]
>> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> > > ---
>> > > 
>> > > Changes since v12:
>> > > * Moves add_rule_net_port() back in syscalls.c and makes it static.
>> > > * Deletes bind_access_mask allowing bind action rule on port 0.
>> > > * Adds comment about port 0 in landlock_net_port_attr structure.
>> > > * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > Changes since v11:
>> > > * Replaces dates with "2022-2023" in net.c/h files headers.
>> > > * Removes WARN_ON_ONCE(!domain) in check_socket_access().
>> > > * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
>> > > * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
>> > > * Renames landlock_net_service_attr to landlock_net_port_attr.
>> > > * Defines two add_rule_net_service() functions according to
>> > >   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>> > >   function.
>> > > * Adds af_family consistency check while handling AF_UNSPEC specifically.
>> > > * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>> > >   action on port zero.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > 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() and
>> > >   get_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                |  56 ++++++
>> > >  security/landlock/Kconfig                    |   1 +
>> > >  security/landlock/Makefile                   |   2 +
>> > >  security/landlock/limits.h                   |   5 +
>> > >  security/landlock/net.c                      | 198 +++++++++++++++++++
>> > >  security/landlock/net.h                      |  33 ++++
>> > >  security/landlock/ruleset.c                  |  62 +++++-
>> > >  security/landlock/ruleset.h                  |  59 +++++-
>> > >  security/landlock/setup.c                    |   2 +
>> > >  security/landlock/syscalls.c                 |  69 ++++++-
>> > >  tools/testing/selftests/landlock/base_test.c |   2 +-
>> > >  11 files changed, 466 insertions(+), 23 deletions(-)
>> > >  create mode 100644 security/landlock/net.c
>> > >  create mode 100644 security/landlock/net.h
>> > > 
>> > > diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
>> > > index 81d09ef9aa50..25349666b19e 100644
>> > > --- a/include/uapi/linux/landlock.h
>> > > +++ b/include/uapi/linux/landlock.h
>> > > @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>> > >  	 * landlock_path_beneath_attr .
>> > >  	 */
>> > >  	LANDLOCK_RULE_PATH_BENEATH = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
>> > > +	 * landlock_net_port_attr .
>> > > +	 */
>> > > +	LANDLOCK_RULE_NET_PORT = 2,
>> > 
>> > We don't need the explicit " = 2".
>> 
>>   Fixed. Thanks.
>> > 
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>> > >  	 */
>> > >  } __attribute__((packed));
>> > > 
>> > > +/**
>> > > + * struct landlock_net_port_attr - Network port definition
>> > > + *
>> > > + * Argument of sys_landlock_add_rule().
>> > > + */
>> > > +struct landlock_net_port_attr {
>> > > +	/**
>> > > +	 * @allowed_access: Bitmask of allowed access network for a port
>> > > +	 * (cf. `Network flags`_).
>> > > +	 */
>> > > +	__u64 allowed_access;
>> > > +	/**
>> > > +	 * @port: Network port. Landlock does not forbid rules with port 0,
>> > > +	 * since some network services use it. Port 0 is a reserved one in
>> > > +	 * TCP/IP networking, meaning that it should not be used in TCP or
>> > > +	 * UDP messages. To allocate its source port number, services call
>> > > +	 * TCP/IP network functions like bind() to request one. With port 0
>> > > +	 * it triggers the operating system to automatically search for
>> > > +	 * and return a suitable available port in the TCP/IP dynamic
>> > > +	 * port number range. This port range can be controlled by a
>> > > +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
>> > > +	 * which is also used by IPv6.
>> > 
>> > This looks too inspired from
>> > https://www.lifewire.com/port-0-in-tcp-and-udp-818145
>> 
>>   Yep. You are right.
>> > 
>> > Let's make it simpler:
>> > 
>> >   * @port: Network port.
>> >   *
>> >   * It should be noted that port 0 passed to :manpage:`bind(2)` will
>> >   * bind to an available port from a specific port range. This can be
>> >   * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
>> >   * sysctl (also used for IPv6). A Landlock rule with port 0 and the
>> >   * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
>> >   * on port 0 is allowed and it will automatically translate to binding
>> >   * on the related port range.
>>     Thanks.
>> > 
>> > > +	 */
>> > > +	__u64 port;
>> > > +};
>> > > +
>> > >  /**
>> > >   * DOC: fs_access
>> > >   *
>> > > @@ -189,4 +226,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.
>> > 
>> > You can add:
>> > "This is supported since ABI 4."
>> 
>>    Updated.
>> > 
>> > > + *
>> > > + * 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 */
>> > > diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
>> > > index c1e862a38410..c4bf0d5eff39 100644
>> > > --- a/security/landlock/Kconfig
>> > > +++ b/security/landlock/Kconfig
>> > > @@ -3,6 +3,7 @@
>> > >  config SECURITY_LANDLOCK
>> > >  	bool "Landlock support"
>> > >  	depends on SECURITY
>> > > +	select SECURITY_NETWORK
>> > >  	select SECURITY_PATH
>> > >  	help
>> > >  	  Landlock is a sandboxing mechanism that enables processes to restrict
>> > > diff --git a/security/landlock/Makefile b/security/landlock/Makefile
>> > > index 7bbd2f413b3e..53d3c92ae22e 100644
>> > > --- a/security/landlock/Makefile
>> > > +++ b/security/landlock/Makefile
>> > > @@ -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
>> > > \ No newline at end of file
>> > > diff --git a/security/landlock/limits.h b/security/landlock/limits.h
>> > > index bafb3b8dc677..93c9c6f91556 100644
>> > > --- a/security/landlock/limits.h
>> > > +++ b/security/landlock/limits.h
>> > > @@ -23,6 +23,11 @@
>> > >  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>> > >  #define LANDLOCK_SHIFT_ACCESS_FS	0
>> > > 
>> > > +#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 */
>> > > diff --git a/security/landlock/net.c b/security/landlock/net.c
>> > > new file mode 100644
>> > > index 000000000000..1bf26cf3c41b
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.c
>> > > @@ -0,0 +1,198 @@
>> > > +// SPDX-License-Identifier: GPL-2.0-only
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
>> > > + * Copyright © 2022-2023 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,
>> > 
>> > To be consistent with current_check_access_path(), please rename to
>> > current_check_access_socket().
>> 
>>   Done. Thanks.
>> > 
>> > > +			       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();
>> > 
>> > For consistency with other functions, s/domain/dom/g
>> 
>>  Ok. Fixed.
>> > 
>> > > +
>> > > +	if (!domain)
>> > > +		return 0;
>> > > +	if (WARN_ON_ONCE(domain->num_layers < 1))
>> > > +		return -EACCES;
>> > > +
>> > > +	/* Checks if it's a (potential) TCP socket. */
>> > > +	if (sock->type != SOCK_STREAM)
>> > > +		return 0;
>> > > +
>> > > +	/* Checks for minimal header length to safely read sa_family. */
>> > > +	if (addrlen < offsetofend(typeof(*address), sa_family))
>> > > +		return -EINVAL;
>> > > +
>> > > +	switch (address->sa_family) {
>> > > +	case AF_UNSPEC:
>> > > +	case AF_INET:
>> > > +		if (addrlen < sizeof(struct sockaddr_in))
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in *)address)->sin_port;
>> > > +		break;
>> > > +#if IS_ENABLED(CONFIG_IPV6)
>> > > +	case AF_INET6:
>> > > +		if (addrlen < SIN6_LEN_RFC2133)
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in6 *)address)->sin6_port;
>> > > +		break;
>> > > +#endif
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>   #endif /* IS_ENABLED(CONFIG_IPV6) */ I suppose.
> 
> Indeed
> 
>> > 
>> > > +	default:
>> > > +		return 0;
>> > > +	}
>> > > +
>> > > +	/* Specific AF_UNSPEC handling. */
>> > > +	if (address->sa_family == AF_UNSPEC) {
>> > > +		/*
>> > > +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
>> > > +		 * association, which have the same effect as closing the
>> > > +		 * 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;
>> > > +
>> > > +		/*
>> > > +		 * For compatibility reason, accept AF_UNSPEC for bind
>> > > +		 * accesses (mapped to AF_INET) only if the address is
>> > > +		 * 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;
>> > > +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
>> > > +
>> > > +	rule = landlock_find_rule(domain, id);
>> > > +	handled_access = landlock_init_layer_masks(
>> > > +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
>> > > +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
>> > > +				   ARRAY_SIZE(layer_masks)))
>> > > +		return 0;
>> > > +
>> > > +	return -EACCES;
>> > > +}
>> > > +
>> > > +static int hook_socket_bind(struct socket *const sock,
>> > > +			    struct sockaddr *const address, const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_BIND_TCP);
>> > > +}
>> > > +
>> > > +static int hook_socket_connect(struct socket *const sock,
>> > > +			       struct sockaddr *const address,
>> > > +			       const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
>> > > +}
>> > > +
>> > > +static struct security_hook_list landlock_hooks[] __ro_after_init = {
>> > > +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
>> > > +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
>> > > +};
>> > > +
>> > > +__init void landlock_add_net_hooks(void)
>> > > +{
>> > > +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
>> > > +			   LANDLOCK_NAME);
>> > > +}
>> > > diff --git a/security/landlock/net.h b/security/landlock/net.h
>> > > new file mode 100644
>> > > index 000000000000..588a49fd6907
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.h
>> > > @@ -0,0 +1,33 @@
>> > > +/* SPDX-License-Identifier: GPL-2.0-only */
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 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)
>> > > +{
>> > > +}
>> > > +
>> > > +static inline int
>> > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> > > +			 access_mask_t access_rights);
>> > > +{
>> > > +	return -EAFNOSUPPORT;
>> > > +}
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > > +#endif /* _SECURITY_LANDLOCK_NET_H */
>> > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > --- a/security/landlock/ruleset.c
>> > > +++ b/security/landlock/ruleset.c
>> > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  	refcount_set(&new_ruleset->usage, 1);
>> > >  	mutex_init(&new_ruleset->lock);
>> > >  	new_ruleset->root_inode = RB_ROOT;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	new_ruleset->num_layers = num_layers;
>> > >  	/*
>> > >  	 * hierarchy = NULL
>> > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  }
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > +			const access_mask_t net_access_mask)
>> > >  {
>> > >  	struct landlock_ruleset *new_ruleset;
>> > > 
>> > >  	/* Informs about useless ruleset. */
>> > > -	if (!fs_access_mask)
>> > > +	if (!fs_access_mask && !net_access_mask)
>> > >  		return ERR_PTR(-ENOMSG);
>> > >  	new_ruleset = create_ruleset(1);
>> > > -	if (!IS_ERR(new_ruleset))
>> > > +	if (IS_ERR(new_ruleset))
>> > > +		return new_ruleset;
>> > > +	if (fs_access_mask)
>> > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > +	if (net_access_mask)
>> > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > 
>> > This is good, but it is not tested: we need to add a test that both
>> > handle FS and net restrictions. You can add one in net.c, just handling
>> > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > that open("/") is denied, open("/dev") is allowed, and and only the
>> > allowed port is allowed with bind(). This test should be simple and can
>> > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > after port_endianness. fcntl.h should then be included by net.c
>> 
>>   Ok.
>> > 
>> > I guess that was the purpose of layout1.with_net (in fs_test.c) but it
>> 
>>   Yep. I added this kind of nest in fs_test.c to test both fs and network
>> rules together.
>> > is not complete. You can revamp this test and move it to net.c
>> > following the above suggestions, keeping it consistent with other tests
>> > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > 
>> > This test must failed if we change "ruleset->access_masks[layer_level] |="
>> > to "ruleset->access_masks[layer_level] =" in
>> > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> 
>>   Do you want to change it? Why?
> 
> The kernel code is correct and must not be changed. However, if by
> mistake we change it and remove the OR, a test should catch that. We
> need a test to assert this assumption.
> 
>>   Fs and network masks are ORed to not intersect with each other.
> 
> Yes, they are ORed, and we need a test to check that. Noting is
> currently testing this OR (and the different rule type consistency).
> I'm suggesting to revamp the layout1.with_net test into
> ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> addition of different types.

Thinking about this test. We don't need to add any additional ASSERT 
here. Anyway if we accidentally change 
"ruleset->access_masks[layer_level] |=" to 
"ruleset->access_masks[layer_level] =" we will fail either in opening 
directory or in port binding, cause adding a second rule (fs or net) 
will overwrite a first one's mask. it does not matter which one goes 
first. I will check it and send you a message.
What do you think?

> 
>> > 
>> > >  	return new_ruleset;
>> > >  }
>> > > 
>> > > @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return true;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return false;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return false;
>> > > @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return &ruleset->root_inode;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return &ruleset->root_net_port;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > > +		WARN_ON_ONCE(1);
>> > 
>> > Please move this WARN to the patch that added the previous and next
>> > lines.
>> 
>>   OK. Will be moved.
>> > 
>> > >  		return ERR_PTR(-EINVAL);
>> > >  	}
>> > >  }
>> > > @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>> > >  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>> > >  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>> > >  	BUILD_BUG_ON(access_masks <
>> > > -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
>> > > +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
>> > > +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>> > >  }
>> > > 
>> > >  /**
>> > > @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Merges the @src network port tree. */
>> > > +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  out_unlock:
>> > >  	mutex_unlock(&src->lock);
>> > >  	mutex_unlock(&dst->lock);
>> > > @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Copies the @parent network port tree. */
>> > > +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>> > >  		err = -EINVAL;
>> > >  		goto out_unlock;
>> > > @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>> > >  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>> > >  					     node)
>> > >  		free_rule(freeme, LANDLOCK_KEY_INODE);
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	rbtree_postorder_for_each_entry_safe(freeme, next,
>> > > +					     &ruleset->root_net_port, node)
>> > > +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	put_hierarchy(ruleset->hierarchy);
>> > >  	kfree(ruleset);
>> > >  }
>> > > @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>> > >   *
>> > >   * @domain: The domain that defines the current restrictions.
>> > >   * @access_request: The requested access rights to check.
>> > > - * @layer_masks: The layer masks to populate.
>> > > + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
>> > 
>> > "%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"
>> 
>>   Done.
>> > 
>> > > + * elements according to @key_type.
>> > >   * @key_type: The key type to switch between access masks of different types.
>> > >   *
>> > >   * Returns: An access mask where each access right bit is set which is handled
>> > > @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>> > >  		get_access_mask = landlock_get_fs_access_mask;
>> > >  		num_access = LANDLOCK_NUM_ACCESS_FS;
>> > >  		break;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		get_access_mask = landlock_get_net_access_mask;
>> > > +		num_access = LANDLOCK_NUM_ACCESS_NET;
>> > > +		break;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return 0;
>> > > diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
>> > > index 1ede2b9a79b7..ba4a06035599 100644
>> > > --- a/security/landlock/ruleset.h
>> > > +++ b/security/landlock/ruleset.h
>> > > @@ -33,13 +33,16 @@
>> > >  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);
>> > > +/* Makes sure all network access rights can be stored. */
>> > > +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>> > >  /* 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;
>> > > +typedef u32 access_masks_t;
>> > >  /* Makes sure all ruleset access rights can be stored. */
>> > > -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
>> > > +static_assert(BITS_PER_TYPE(access_masks_t) >=
>> > > +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
>> > > 
>> > >  typedef u16 layer_mask_t;
>> > >  /* Makes sure all layers can be checked. */
>> > > @@ -84,6 +87,11 @@ enum landlock_key_type {
>> > >  	 * keys.
>> > >  	 */
>> > >  	LANDLOCK_KEY_INODE = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
>> > > +	 * node keys.
>> > > +	 */
>> > > +	LANDLOCK_KEY_NET_PORT,
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -158,6 +166,13 @@ struct landlock_ruleset {
>> > >  	 * reaches zero.
>> > >  	 */
>> > >  	struct rb_root root_inode;
>> > 
>> > #if IS_ENABLED(CONFIG_INET)
>>   OK. Done.
>> > > +	/**
>> > > +	 * @root_net_port: Root of a red-black tree containing &struct
>> > > +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
>> > > +	 * process (i.e. as a domain), this tree is immutable until @usage
>> > > +	 * reaches zero.
>> > > +	 */
>> > > +	struct rb_root root_net_port;
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>  Done.
>> > 
>> > >  	/**
>> > >  	 * @hierarchy: Enables hierarchy identification even when a parent
>> > >  	 * domain vanishes.  This is needed for the ptrace protection.
>> > > @@ -196,13 +211,13 @@ struct landlock_ruleset {
>> > >  			 */
>> > >  			u32 num_layers;
>> > >  			/**
>> > > -			 * @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
>> > > -			 * one.  These layers are used when merging rulesets,
>> > > -			 * for user space backward compatibility (i.e.
>> > > -			 * future-proof), and to properly handle merged
>> > > +			 * @access_masks: Contains the subset of filesystem and
>> > > +			 * network actions that are restricted by a ruleset.
>> > > +			 * A domain saves all layers of merged rulesets in a
>> > > +			 * stack (FAM), starting from the first layer to the
>> > > +			 * last one.  These layers are used when merging
>> > > +			 * rulesets, for user space backward compatibility
>> > > +			 * (i.e. future-proof), and to properly handle merged
>> > >  			 * rulesets without overlapping access rights.  These
>> > >  			 * layers are set once and never changed for the
>> > >  			 * lifetime of the ruleset.
>> > > @@ -213,7 +228,8 @@ struct landlock_ruleset {
>> > >  };
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t access_mask);
>> > > +landlock_create_ruleset(const access_mask_t access_mask_fs,
>> > > +			const access_mask_t access_mask_net);
>> > > 
>> > >  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>> > >  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
>> > > @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>> > >  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>> > >  }
>> > > 
>> > > +static inline void
>> > > +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
>> > > +			     const access_mask_t net_access_mask,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
>> > > +
>> > > +	/* Should already be checked in sys_landlock_create_ruleset(). */
>> > > +	WARN_ON_ONCE(net_access_mask != net_mask);
>> > > +	ruleset->access_masks[layer_level] |=
>> > > +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
>> > > +}
>> > > +
>> > >  static inline access_mask_t
>> > >  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>> > >  				const u16 layer_level)
>> > > @@ -266,6 +295,16 @@ 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;
>> > >  }
>> > > +
>> > > +static inline access_mask_t
>> > > +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	return (ruleset->access_masks[layer_level] >>
>> > > +		LANDLOCK_SHIFT_ACCESS_NET) &
>> > > +	       LANDLOCK_MASK_ACCESS_NET;
>> > > +}
>> > > +
>> > >  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>> > >  			    const access_mask_t access_request,
>> > >  			    layer_mask_t (*const layer_masks)[],
>> > > diff --git a/security/landlock/setup.c b/security/landlock/setup.c
>> > > index 0f6113528fa4..df81612811bf 100644
>> > > --- a/security/landlock/setup.c
>> > > +++ b/security/landlock/setup.c
>> > > @@ -14,6 +14,7 @@
>> > >  #include "fs.h"
>> > >  #include "ptrace.h"
>> > >  #include "setup.h"
>> > > +#include "net.h"
>> > > 
>> > >  bool landlock_initialized __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;
>> > > diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> > > index 8a54e87dbb17..3ad652d9a146 100644
>> > > --- a/security/landlock/syscalls.c
>> > > +++ b/security/landlock/syscalls.c
>> > > @@ -29,6 +29,7 @@
>> > >  #include "cred.h"
>> > >  #include "fs.h"
>> > >  #include "limits.h"
>> > > +#include "net.h"
>> > >  #include "ruleset.h"
>> > >  #include "setup.h"
>> > > 
>> > > @@ -74,7 +75,8 @@ static void build_check_abi(void)
>> > >  {
>> > >  	struct landlock_ruleset_attr ruleset_attr;
>> > >  	struct landlock_path_beneath_attr path_beneath_attr;
>> > > -	size_t ruleset_size, path_beneath_size;
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	size_t ruleset_size, path_beneath_size, net_port_size;
>> > > 
>> > >  	/*
>> > >  	 * For each user space ABI structures, first checks that there is no
>> > > @@ -82,13 +84,19 @@ static void build_check_abi(void)
>> > >  	 * struct size.
>> > >  	 */
>> > >  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
>> > > +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>> > >  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
>> > > -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
>> > > +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
>> > > 
>> > >  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>> > >  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
>> > > +
>> > > +	net_port_size = sizeof(net_port_attr.allowed_access);
>> > > +	net_port_size += sizeof(net_port_attr.port);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>> > >  }
>> > > 
>> > >  /* Ruleset handling */
>> > > @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>> > >  	.write = fop_dummy_write,
>> > >  };
>> > > 
>> > > -#define LANDLOCK_ABI_VERSION 3
>> > > +#define LANDLOCK_ABI_VERSION 4
>> > > 
>> > >  /**
>> > >   * sys_landlock_create_ruleset - Create a new ruleset
>> > > @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>> > >  	    LANDLOCK_MASK_ACCESS_FS)
>> > >  		return -EINVAL;
>> > > 
>> > > +	/* Checks network content (and 32-bits cast). */
>> > > +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
>> > > +	    LANDLOCK_MASK_ACCESS_NET)
>> > > +		return -EINVAL;
>> > > +
>> > >  	/* Checks arguments and transforms to kernel struct. */
>> > > -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
>> > > +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
>> > > +					  ruleset_attr.handled_access_net);
>> > >  	if (IS_ERR(ruleset))
>> > >  		return PTR_ERR(ruleset);
>> > > 
>> > > @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	int res, err;
>> > >  	access_mask_t mask;
>> > > 
>> > > -	/* Copies raw user space buffer, only one type for now. */
>> > > +	/* Copies raw user space buffer. */
>> > 
>> > Shouldn't this be part of a previous patch?
>> 
>> I did it according Gunter's suggestion
>> https://lore.kernel.org/netdev/20230627.82cde73b1efe@gnoack.org/
> 
> Ok, that indeed makes more sense in this patch, please keep it.
> 
>> > 
>> > >  	res = copy_from_user(&path_beneath_attr, rule_attr,
>> > >  			     sizeof(path_beneath_attr));
>> > >  	if (res)
>> > > @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	return err;
>> > >  }
>> > > 
>> > > +static int add_rule_net_port(struct landlock_ruleset *ruleset,
>> > > +			     const void __user *const rule_attr)
>> > > +{
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	int res;
>> > > +	access_mask_t mask;
>> > > +
>> > > +	/* Copies raw user space buffer. */
>> > > +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
>> > > +	if (res)
>> > > +		return -EFAULT;
>> > > +
>> > > +	/*
>> > > +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
>> > > +	 * are ignored by network actions.
>> > > +	 */
>> > > +	if (!net_port_attr.allowed_access)
>> > > +		return -ENOMSG;
>> > > +
>> > > +	/*
>> > > +	 * Checks that allowed_access matches the @ruleset constraints
>> > > +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
>> > > +	 */
>> > > +	mask = landlock_get_net_access_mask(ruleset, 0);
>> > > +	if ((net_port_attr.allowed_access | mask) != mask)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Denies inserting a rule with port higher than 65535. */
>> > 
>> > For consistency with the following comment:
>> > "Denies inserting a rule with port greater than 65535."
>> > 
>>   Done. Thanks.
>> > 
>> > > +	if (net_port_attr.port > U16_MAX)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Imports the new rule. */
>> > > +	return landlock_append_net_rule(ruleset, net_port_attr.port,
>> > > +					net_port_attr.allowed_access);
>> > > +}
>> > > +
>> > >  /**
>> > >   * sys_landlock_add_rule - Add a new rule to a ruleset
>> > >   *
>> > >   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
>> > >   *		with the new rule.
>> > > - * @rule_type: Identify the structure type pointed to by @rule_attr (only
>> > > - *             %LANDLOCK_RULE_PATH_BENEATH for now).
>> > > + * @rule_type: Identify the structure type pointed to by @rule_attr:
>> > > + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
>> > >   * @rule_attr: Pointer to a rule (only of type &struct
>> > >   *             landlock_path_beneath_attr for now).
>> > >   * @flags: Must be 0.
>> > > @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >   * Possible returned errors are:
>> > >   *
>> > >   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
>> > > + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
>> > 
>> > %LANDLOCK_RULE_NET_PORT
>> 
>>  Done.
>> > 
>> > > + *   supported by the running kernel;
>> > >   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
>> > >   *   &landlock_path_beneath_attr.allowed_access is not a subset of the
>> > 
>> > &landlock_path_beneath_attr.allowed_access or
>> > &landlock_net_port_attr.allowed_access is not a subset of the
>> 
>>   Fixed. Thanks.
>> > 
>> > >   *   ruleset handled accesses);
>> > 
>> > EINVAL description needs to be updated, especially for port > U16_MAX:
>> > - *   ruleset handled accesses);
>> > + *   ruleset handled accesses), or &landlock_net_port_attr.port is
>> > +     greater than 65535;
>> 
>>  Done. Thanks.
>> > 
>> > 
>> > > @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
>> > >  	case LANDLOCK_RULE_PATH_BENEATH:
>> > >  		err = add_rule_path_beneath(ruleset, rule_attr);
>> > >  		break;
>> > > +	case LANDLOCK_RULE_NET_PORT:
>> > > +		err = add_rule_net_port(ruleset, rule_attr);
>> > > +		break;
>> > >  	default:
>> > >  		err = -EINVAL;
>> > >  		break;
>> > > diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
>> > > index 792c3f0a59b4..646f778dfb1e 100644
>> > > --- a/tools/testing/selftests/landlock/base_test.c
>> > > +++ b/tools/testing/selftests/landlock/base_test.c
>> > > @@ -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
>> > > 
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-20  9:49       ` Mickaël Salaün
  2023-10-20 11:58         ` Konstantin Meskhidze (A)
  2023-10-24  2:51         ` Konstantin Meskhidze (A)
@ 2023-10-24  3:18         ` Konstantin Meskhidze (A)
  2023-10-24  9:03           ` Mickaël Salaün
  2 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-24  3:18 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/20/2023 12:49 PM, Mickaël Salaün пишет:
> On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> > > This commit adds network rules support in the ruleset management
>> > 
>> > Here are some advices to better write commit messages:
>> > https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
>> > The "Describe your changes in imperative mood" part is important for
>> > this commit and others. Most of this patch series' commit messages need
>> > small updates.
>> 
>>  Ok. I will refactor commit messages with "imperative mood". Thanks.
>> > 
>> > > 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
>> > 
>> > Please explain the "why" (when it makes sense) instead of just listing
>> > the "what".
>> 
>>   Ok.
>> 
>> > 
>> > > rights can be stored. Implement socket_bind() and socket_connect()
>> > > LSM hooks, which enables to restrict TCP socket binding and connection
>> > > to specific ports.
>> > 
>> > I reworded and moved this part in last:
>> > > For the file system, a file descriptor is a direct access to a file/data.
>> > > But for the network, it's impossible to identify for which data/peer a
>> > > newly created socket will give access to, it's needed to wait for a
>> > > connect or bind request to identify the use case for this socket.
>> > > That's why the access rights (related to ports) are tied to an opened
>> > > socket, but this would not align with the way Landlock access control
>> > > works for the filesystem [2].
>> 
>>    Thanks.
>> > 
>> > Please add empty line to split paragraphs.
>> 
>>   Got it.
>> > 
>> > > 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 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).
>> > 
>> > For the file system, a file descriptor is a direct access to a file/data.
>> > However, for network sockets, we cannot identify for which data or peer a newly
>> > created socket will give access to. Indeed, we need to wait for a connect or
>> > bind request to identify the use case for this socket.
>> > 
>> > Access rights are not tied to socket file descriptors. Instead, bind and
>> > connect actions are controlled by the task's domain.  As for the filesystem, a
>> > directory file descriptor may enable to open another file (i.e. a new data
>> > item), but this opening is restricted by the task's domain, not the file
>> > descriptor's access rights [2].
>> > 
>> > > 
>> > > [1]
>> > > https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > > [2]
>> > > https://lore.kernel.org/all/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>> > 
>> > [1] https://lore.kernel.org/r/278ab07f-7583-a4e0-3d37-1bacd091531d@digikod.net
>> > [2] https://lore.kernel.org/r/263c1eb3-602f-57fe-8450-3f138581bee7@digikod.net
>>   Thanks.
>> > 
>> > > 
>> > > Signed-off-by: Konstantin Meskhidze <konstantin.meskhidze@huawei.com>
>> > > Link: https://lore.kernel.org/r/20230920092641.832134-9-konstantin.meskhidze@huawei.com
>> > > [mic: Remove !ARCH_EPHEMERAL_INODES in Kconfig, and add landlock_ prefix
>> > > to add_rule_net_service()]
>> > > Signed-off-by: Mickaël Salaün <mic@digikod.net>
>> > > ---
>> > > 
>> > > Changes since v12:
>> > > * Moves add_rule_net_port() back in syscalls.c and makes it static.
>> > > * Deletes bind_access_mask allowing bind action rule on port 0.
>> > > * Adds comment about port 0 in landlock_net_port_attr structure.
>> > > * Removes !ARCH_EPHEMERAL_INODES from Kconfig.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > Changes since v11:
>> > > * Replaces dates with "2022-2023" in net.c/h files headers.
>> > > * Removes WARN_ON_ONCE(!domain) in check_socket_access().
>> > > * Using "typeof(*address)" instead of offsetofend(struct sockaddr, sa_family).
>> > > * Renames LANDLOCK_RULE_NET_SERVICE to LANDLOCK_RULE_NET_PORT.
>> > > * Renames landlock_net_service_attr to landlock_net_port_attr.
>> > > * Defines two add_rule_net_service() functions according to
>> > >   IS_ENABLED(CONFIG_INET) instead of changing the body of the only
>> > >   function.
>> > > * Adds af_family consistency check while handling AF_UNSPEC specifically.
>> > > * Adds bind_access_mask in add_rule_net_service() to deny all rules with bind
>> > >   action on port zero.
>> > > * Minor fixes.
>> > > * Refactors commit message.
>> > > 
>> > > 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() and
>> > >   get_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                |  56 ++++++
>> > >  security/landlock/Kconfig                    |   1 +
>> > >  security/landlock/Makefile                   |   2 +
>> > >  security/landlock/limits.h                   |   5 +
>> > >  security/landlock/net.c                      | 198 +++++++++++++++++++
>> > >  security/landlock/net.h                      |  33 ++++
>> > >  security/landlock/ruleset.c                  |  62 +++++-
>> > >  security/landlock/ruleset.h                  |  59 +++++-
>> > >  security/landlock/setup.c                    |   2 +
>> > >  security/landlock/syscalls.c                 |  69 ++++++-
>> > >  tools/testing/selftests/landlock/base_test.c |   2 +-
>> > >  11 files changed, 466 insertions(+), 23 deletions(-)
>> > >  create mode 100644 security/landlock/net.c
>> > >  create mode 100644 security/landlock/net.h
>> > > 
>> > > diff --git a/include/uapi/linux/landlock.h b/include/uapi/linux/landlock.h
>> > > index 81d09ef9aa50..25349666b19e 100644
>> > > --- a/include/uapi/linux/landlock.h
>> > > +++ b/include/uapi/linux/landlock.h
>> > > @@ -31,6 +31,12 @@ 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 +60,11 @@ enum landlock_rule_type {
>> > >  	 * landlock_path_beneath_attr .
>> > >  	 */
>> > >  	LANDLOCK_RULE_PATH_BENEATH = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_RULE_NET_PORT: Type of a &struct
>> > > +	 * landlock_net_port_attr .
>> > > +	 */
>> > > +	LANDLOCK_RULE_NET_PORT = 2,
>> > 
>> > We don't need the explicit " = 2".
>> 
>>   Fixed. Thanks.
>> > 
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -79,6 +90,32 @@ struct landlock_path_beneath_attr {
>> > >  	 */
>> > >  } __attribute__((packed));
>> > > 
>> > > +/**
>> > > + * struct landlock_net_port_attr - Network port definition
>> > > + *
>> > > + * Argument of sys_landlock_add_rule().
>> > > + */
>> > > +struct landlock_net_port_attr {
>> > > +	/**
>> > > +	 * @allowed_access: Bitmask of allowed access network for a port
>> > > +	 * (cf. `Network flags`_).
>> > > +	 */
>> > > +	__u64 allowed_access;
>> > > +	/**
>> > > +	 * @port: Network port. Landlock does not forbid rules with port 0,
>> > > +	 * since some network services use it. Port 0 is a reserved one in
>> > > +	 * TCP/IP networking, meaning that it should not be used in TCP or
>> > > +	 * UDP messages. To allocate its source port number, services call
>> > > +	 * TCP/IP network functions like bind() to request one. With port 0
>> > > +	 * it triggers the operating system to automatically search for
>> > > +	 * and return a suitable available port in the TCP/IP dynamic
>> > > +	 * port number range. This port range can be controlled by a
>> > > +	 * sysadmin with /proc/sys/net/ipv4/ip_local_port_range sysctl,
>> > > +	 * which is also used by IPv6.
>> > 
>> > This looks too inspired from
>> > https://www.lifewire.com/port-0-in-tcp-and-udp-818145
>> 
>>   Yep. You are right.
>> > 
>> > Let's make it simpler:
>> > 
>> >   * @port: Network port.
>> >   *
>> >   * It should be noted that port 0 passed to :manpage:`bind(2)` will
>> >   * bind to an available port from a specific port range. This can be
>> >   * configured thanks to the ``/proc/sys/net/ipv4/ip_local_port_range``
>> >   * sysctl (also used for IPv6). A Landlock rule with port 0 and the
>> >   * ``LANDLOCK_ACCESS_NET_BIND_TCP`` right means that requesting to bind
>> >   * on port 0 is allowed and it will automatically translate to binding
>> >   * on the related port range.
>>     Thanks.
>> > 
>> > > +	 */
>> > > +	__u64 port;
>> > > +};
>> > > +
>> > >  /**
>> > >   * DOC: fs_access
>> > >   *
>> > > @@ -189,4 +226,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.
>> > 
>> > You can add:
>> > "This is supported since ABI 4."
>> 
>>    Updated.
>> > 
>> > > + *
>> > > + * 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 */
>> > > diff --git a/security/landlock/Kconfig b/security/landlock/Kconfig
>> > > index c1e862a38410..c4bf0d5eff39 100644
>> > > --- a/security/landlock/Kconfig
>> > > +++ b/security/landlock/Kconfig
>> > > @@ -3,6 +3,7 @@
>> > >  config SECURITY_LANDLOCK
>> > >  	bool "Landlock support"
>> > >  	depends on SECURITY
>> > > +	select SECURITY_NETWORK
>> > >  	select SECURITY_PATH
>> > >  	help
>> > >  	  Landlock is a sandboxing mechanism that enables processes to restrict
>> > > diff --git a/security/landlock/Makefile b/security/landlock/Makefile
>> > > index 7bbd2f413b3e..53d3c92ae22e 100644
>> > > --- a/security/landlock/Makefile
>> > > +++ b/security/landlock/Makefile
>> > > @@ -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
>> > > \ No newline at end of file
>> > > diff --git a/security/landlock/limits.h b/security/landlock/limits.h
>> > > index bafb3b8dc677..93c9c6f91556 100644
>> > > --- a/security/landlock/limits.h
>> > > +++ b/security/landlock/limits.h
>> > > @@ -23,6 +23,11 @@
>> > >  #define LANDLOCK_NUM_ACCESS_FS		__const_hweight64(LANDLOCK_MASK_ACCESS_FS)
>> > >  #define LANDLOCK_SHIFT_ACCESS_FS	0
>> > > 
>> > > +#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 */
>> > > diff --git a/security/landlock/net.c b/security/landlock/net.c
>> > > new file mode 100644
>> > > index 000000000000..1bf26cf3c41b
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.c
>> > > @@ -0,0 +1,198 @@
>> > > +// SPDX-License-Identifier: GPL-2.0-only
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 Huawei Tech. Co., Ltd.
>> > > + * Copyright © 2022-2023 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,
>> > 
>> > To be consistent with current_check_access_path(), please rename to
>> > current_check_access_socket().
>> 
>>   Done. Thanks.
>> > 
>> > > +			       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();
>> > 
>> > For consistency with other functions, s/domain/dom/g
>> 
>>  Ok. Fixed.
>> > 
>> > > +
>> > > +	if (!domain)
>> > > +		return 0;
>> > > +	if (WARN_ON_ONCE(domain->num_layers < 1))
>> > > +		return -EACCES;
>> > > +
>> > > +	/* Checks if it's a (potential) TCP socket. */
>> > > +	if (sock->type != SOCK_STREAM)
>> > > +		return 0;
>> > > +
>> > > +	/* Checks for minimal header length to safely read sa_family. */
>> > > +	if (addrlen < offsetofend(typeof(*address), sa_family))
>> > > +		return -EINVAL;
>> > > +
>> > > +	switch (address->sa_family) {
>> > > +	case AF_UNSPEC:
>> > > +	case AF_INET:
>> > > +		if (addrlen < sizeof(struct sockaddr_in))
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in *)address)->sin_port;
>> > > +		break;
>> > > +#if IS_ENABLED(CONFIG_IPV6)
>> > > +	case AF_INET6:
>> > > +		if (addrlen < SIN6_LEN_RFC2133)
>> > > +			return -EINVAL;
>> > > +		port = ((struct sockaddr_in6 *)address)->sin6_port;
>> > > +		break;
>> > > +#endif
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>   #endif /* IS_ENABLED(CONFIG_IPV6) */ I suppose.
> 
> Indeed
> 
>> > 
>> > > +	default:
>> > > +		return 0;
>> > > +	}
>> > > +
>> > > +	/* Specific AF_UNSPEC handling. */
>> > > +	if (address->sa_family == AF_UNSPEC) {
>> > > +		/*
>> > > +		 * Connecting to an address with AF_UNSPEC dissolves the TCP
>> > > +		 * association, which have the same effect as closing the
>> > > +		 * 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;
>> > > +
>> > > +		/*
>> > > +		 * For compatibility reason, accept AF_UNSPEC for bind
>> > > +		 * accesses (mapped to AF_INET) only if the address is
>> > > +		 * 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;
>> > > +	BUILD_BUG_ON(sizeof(port) > sizeof(id.key.data));
>> > > +
>> > > +	rule = landlock_find_rule(domain, id);
>> > > +	handled_access = landlock_init_layer_masks(
>> > > +		domain, access_request, &layer_masks, LANDLOCK_KEY_NET_PORT);
>> > > +	if (landlock_unmask_layers(rule, handled_access, &layer_masks,
>> > > +				   ARRAY_SIZE(layer_masks)))
>> > > +		return 0;
>> > > +
>> > > +	return -EACCES;
>> > > +}
>> > > +
>> > > +static int hook_socket_bind(struct socket *const sock,
>> > > +			    struct sockaddr *const address, const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_BIND_TCP);
>> > > +}
>> > > +
>> > > +static int hook_socket_connect(struct socket *const sock,
>> > > +			       struct sockaddr *const address,
>> > > +			       const int addrlen)
>> > > +{
>> > > +	return check_socket_access(sock, address, addrlen,
>> > > +				   LANDLOCK_ACCESS_NET_CONNECT_TCP);
>> > > +}
>> > > +
>> > > +static struct security_hook_list landlock_hooks[] __ro_after_init = {
>> > > +	LSM_HOOK_INIT(socket_bind, hook_socket_bind),
>> > > +	LSM_HOOK_INIT(socket_connect, hook_socket_connect),
>> > > +};
>> > > +
>> > > +__init void landlock_add_net_hooks(void)
>> > > +{
>> > > +	security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
>> > > +			   LANDLOCK_NAME);
>> > > +}
>> > > diff --git a/security/landlock/net.h b/security/landlock/net.h
>> > > new file mode 100644
>> > > index 000000000000..588a49fd6907
>> > > --- /dev/null
>> > > +++ b/security/landlock/net.h
>> > > @@ -0,0 +1,33 @@
>> > > +/* SPDX-License-Identifier: GPL-2.0-only */
>> > > +/*
>> > > + * Landlock LSM - Network management and hooks
>> > > + *
>> > > + * Copyright © 2022-2023 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)
>> > > +{
>> > > +}
>> > > +
>> > > +static inline int
>> > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> > > +			 access_mask_t access_rights);
>> > > +{
>> > > +	return -EAFNOSUPPORT;
>> > > +}
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > > +#endif /* _SECURITY_LANDLOCK_NET_H */
>> > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > --- a/security/landlock/ruleset.c
>> > > +++ b/security/landlock/ruleset.c
>> > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  	refcount_set(&new_ruleset->usage, 1);
>> > >  	mutex_init(&new_ruleset->lock);
>> > >  	new_ruleset->root_inode = RB_ROOT;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	new_ruleset->num_layers = num_layers;
>> > >  	/*
>> > >  	 * hierarchy = NULL
>> > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > >  }
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > +			const access_mask_t net_access_mask)
>> > >  {
>> > >  	struct landlock_ruleset *new_ruleset;
>> > > 
>> > >  	/* Informs about useless ruleset. */
>> > > -	if (!fs_access_mask)
>> > > +	if (!fs_access_mask && !net_access_mask)
>> > >  		return ERR_PTR(-ENOMSG);
>> > >  	new_ruleset = create_ruleset(1);
>> > > -	if (!IS_ERR(new_ruleset))
>> > > +	if (IS_ERR(new_ruleset))
>> > > +		return new_ruleset;
>> > > +	if (fs_access_mask)
>> > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > +	if (net_access_mask)
>> > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > 
>> > This is good, but it is not tested: we need to add a test that both
>> > handle FS and net restrictions. You can add one in net.c, just handling
>> > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > that open("/") is denied, open("/dev") is allowed, and and only the
>> > allowed port is allowed with bind(). This test should be simple and can
>> > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > after port_endianness. fcntl.h should then be included by net.c
>> 
>>   Ok.
>> > 
>> > I guess that was the purpose of layout1.with_net (in fs_test.c) but it
>> 
>>   Yep. I added this kind of nest in fs_test.c to test both fs and network
>> rules together.
>> > is not complete. You can revamp this test and move it to net.c
>> > following the above suggestions, keeping it consistent with other tests
>> > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > 
>> > This test must failed if we change "ruleset->access_masks[layer_level] |="
>> > to "ruleset->access_masks[layer_level] =" in
>> > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> 
>>   Do you want to change it? Why?
> 
> The kernel code is correct and must not be changed. However, if by
> mistake we change it and remove the OR, a test should catch that. We
> need a test to assert this assumption.
> 
>>   Fs and network masks are ORed to not intersect with each other.
> 
> Yes, they are ORed, and we need a test to check that. Noting is
> currently testing this OR (and the different rule type consistency).
> I'm suggesting to revamp the layout1.with_net test into
> ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> addition of different types.

   About my previous comment.

   Checking the code we can  notice that adding fs mask goes first:

...
if (fs_access_mask)
		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
if (net_access_mask)
		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
....

So with we change "ruleset->access_masks[layer_level] |="
 >> > to "ruleset->access_masks[layer_level] =" in 
landlock_add_fs_access_mask() nothing bad will happen.
But if we do that in landlock_add_net_access_mask()
fs mask will be overwritten and adding fs rule will fail
(as unhandled allowed_accesss).

> 
>> > 
>> > >  	return new_ruleset;
>> > >  }
>> > > 
>> > > @@ -74,6 +84,11 @@ static bool is_object_pointer(const enum landlock_key_type key_type)
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return true;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return false;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return false;
>> > > @@ -126,7 +141,13 @@ static struct rb_root *get_root(struct landlock_ruleset *const ruleset,
>> > >  	case LANDLOCK_KEY_INODE:
>> > >  		return &ruleset->root_inode;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		return &ruleset->root_net_port;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > > +		WARN_ON_ONCE(1);
>> > 
>> > Please move this WARN to the patch that added the previous and next
>> > lines.
>> 
>>   OK. Will be moved.
>> > 
>> > >  		return ERR_PTR(-EINVAL);
>> > >  	}
>> > >  }
>> > > @@ -153,7 +174,8 @@ static void build_check_ruleset(void)
>> > >  	BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
>> > >  	BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
>> > >  	BUILD_BUG_ON(access_masks <
>> > > -		     (LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS));
>> > > +		     ((LANDLOCK_MASK_ACCESS_FS << LANDLOCK_SHIFT_ACCESS_FS) |
>> > > +		      (LANDLOCK_MASK_ACCESS_NET << LANDLOCK_SHIFT_ACCESS_NET)));
>> > >  }
>> > > 
>> > >  /**
>> > > @@ -370,6 +392,13 @@ static int merge_ruleset(struct landlock_ruleset *const dst,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Merges the @src network port tree. */
>> > > +	err = merge_tree(dst, src, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  out_unlock:
>> > >  	mutex_unlock(&src->lock);
>> > >  	mutex_unlock(&dst->lock);
>> > > @@ -426,6 +455,13 @@ static int inherit_ruleset(struct landlock_ruleset *const parent,
>> > >  	if (err)
>> > >  		goto out_unlock;
>> > > 
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	/* Copies the @parent network port tree. */
>> > > +	err = inherit_tree(parent, child, LANDLOCK_KEY_NET_PORT);
>> > > +	if (err)
>> > > +		goto out_unlock;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
>> > >  		err = -EINVAL;
>> > >  		goto out_unlock;
>> > > @@ -455,6 +491,13 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
>> > >  	rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root_inode,
>> > >  					     node)
>> > >  		free_rule(freeme, LANDLOCK_KEY_INODE);
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	rbtree_postorder_for_each_entry_safe(freeme, next,
>> > > +					     &ruleset->root_net_port, node)
>> > > +		free_rule(freeme, LANDLOCK_KEY_NET_PORT);
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	put_hierarchy(ruleset->hierarchy);
>> > >  	kfree(ruleset);
>> > >  }
>> > > @@ -635,7 +678,8 @@ get_access_mask_t(const struct landlock_ruleset *const ruleset,
>> > >   *
>> > >   * @domain: The domain that defines the current restrictions.
>> > >   * @access_request: The requested access rights to check.
>> > > - * @layer_masks: The layer masks to populate.
>> > > + * @layer_masks: It must contain LANDLOCK_NUM_ACCESS_FS or LANDLOCK_NUM_ACCESS_NET
>> > 
>> > "%LANDLOCK_NUM_ACCESS_FS or %LANDLOCK_NUM_ACCESS_NET"
>> 
>>   Done.
>> > 
>> > > + * elements according to @key_type.
>> > >   * @key_type: The key type to switch between access masks of different types.
>> > >   *
>> > >   * Returns: An access mask where each access right bit is set which is handled
>> > > @@ -656,6 +700,14 @@ landlock_init_layer_masks(const struct landlock_ruleset *const domain,
>> > >  		get_access_mask = landlock_get_fs_access_mask;
>> > >  		num_access = LANDLOCK_NUM_ACCESS_FS;
>> > >  		break;
>> > > +
>> > > +#if IS_ENABLED(CONFIG_INET)
>> > > +	case LANDLOCK_KEY_NET_PORT:
>> > > +		get_access_mask = landlock_get_net_access_mask;
>> > > +		num_access = LANDLOCK_NUM_ACCESS_NET;
>> > > +		break;
>> > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > +
>> > >  	default:
>> > >  		WARN_ON_ONCE(1);
>> > >  		return 0;
>> > > diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
>> > > index 1ede2b9a79b7..ba4a06035599 100644
>> > > --- a/security/landlock/ruleset.h
>> > > +++ b/security/landlock/ruleset.h
>> > > @@ -33,13 +33,16 @@
>> > >  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);
>> > > +/* Makes sure all network access rights can be stored. */
>> > > +static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
>> > >  /* 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;
>> > > +typedef u32 access_masks_t;
>> > >  /* Makes sure all ruleset access rights can be stored. */
>> > > -static_assert(BITS_PER_TYPE(access_masks_t) >= LANDLOCK_NUM_ACCESS_FS);
>> > > +static_assert(BITS_PER_TYPE(access_masks_t) >=
>> > > +	      LANDLOCK_NUM_ACCESS_FS + LANDLOCK_NUM_ACCESS_NET);
>> > > 
>> > >  typedef u16 layer_mask_t;
>> > >  /* Makes sure all layers can be checked. */
>> > > @@ -84,6 +87,11 @@ enum landlock_key_type {
>> > >  	 * keys.
>> > >  	 */
>> > >  	LANDLOCK_KEY_INODE = 1,
>> > > +	/**
>> > > +	 * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
>> > > +	 * node keys.
>> > > +	 */
>> > > +	LANDLOCK_KEY_NET_PORT,
>> > >  };
>> > > 
>> > >  /**
>> > > @@ -158,6 +166,13 @@ struct landlock_ruleset {
>> > >  	 * reaches zero.
>> > >  	 */
>> > >  	struct rb_root root_inode;
>> > 
>> > #if IS_ENABLED(CONFIG_INET)
>>   OK. Done.
>> > > +	/**
>> > > +	 * @root_net_port: Root of a red-black tree containing &struct
>> > > +	 * landlock_rule nodes with network port. Once a ruleset is tied to a
>> > > +	 * process (i.e. as a domain), this tree is immutable until @usage
>> > > +	 * reaches zero.
>> > > +	 */
>> > > +	struct rb_root root_net_port;
>> > 
>> > #endif /* IS_ENABLED(CONFIG_INET) */
>> 
>>  Done.
>> > 
>> > >  	/**
>> > >  	 * @hierarchy: Enables hierarchy identification even when a parent
>> > >  	 * domain vanishes.  This is needed for the ptrace protection.
>> > > @@ -196,13 +211,13 @@ struct landlock_ruleset {
>> > >  			 */
>> > >  			u32 num_layers;
>> > >  			/**
>> > > -			 * @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
>> > > -			 * one.  These layers are used when merging rulesets,
>> > > -			 * for user space backward compatibility (i.e.
>> > > -			 * future-proof), and to properly handle merged
>> > > +			 * @access_masks: Contains the subset of filesystem and
>> > > +			 * network actions that are restricted by a ruleset.
>> > > +			 * A domain saves all layers of merged rulesets in a
>> > > +			 * stack (FAM), starting from the first layer to the
>> > > +			 * last one.  These layers are used when merging
>> > > +			 * rulesets, for user space backward compatibility
>> > > +			 * (i.e. future-proof), and to properly handle merged
>> > >  			 * rulesets without overlapping access rights.  These
>> > >  			 * layers are set once and never changed for the
>> > >  			 * lifetime of the ruleset.
>> > > @@ -213,7 +228,8 @@ struct landlock_ruleset {
>> > >  };
>> > > 
>> > >  struct landlock_ruleset *
>> > > -landlock_create_ruleset(const access_mask_t access_mask);
>> > > +landlock_create_ruleset(const access_mask_t access_mask_fs,
>> > > +			const access_mask_t access_mask_net);
>> > > 
>> > >  void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
>> > >  void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
>> > > @@ -249,6 +265,19 @@ landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
>> > >  		(fs_mask << LANDLOCK_SHIFT_ACCESS_FS);
>> > >  }
>> > > 
>> > > +static inline void
>> > > +landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
>> > > +			     const access_mask_t net_access_mask,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
>> > > +
>> > > +	/* Should already be checked in sys_landlock_create_ruleset(). */
>> > > +	WARN_ON_ONCE(net_access_mask != net_mask);
>> > > +	ruleset->access_masks[layer_level] |=
>> > > +		(net_mask << LANDLOCK_SHIFT_ACCESS_NET);
>> > > +}
>> > > +
>> > >  static inline access_mask_t
>> > >  landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
>> > >  				const u16 layer_level)
>> > > @@ -266,6 +295,16 @@ 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;
>> > >  }
>> > > +
>> > > +static inline access_mask_t
>> > > +landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
>> > > +			     const u16 layer_level)
>> > > +{
>> > > +	return (ruleset->access_masks[layer_level] >>
>> > > +		LANDLOCK_SHIFT_ACCESS_NET) &
>> > > +	       LANDLOCK_MASK_ACCESS_NET;
>> > > +}
>> > > +
>> > >  bool landlock_unmask_layers(const struct landlock_rule *const rule,
>> > >  			    const access_mask_t access_request,
>> > >  			    layer_mask_t (*const layer_masks)[],
>> > > diff --git a/security/landlock/setup.c b/security/landlock/setup.c
>> > > index 0f6113528fa4..df81612811bf 100644
>> > > --- a/security/landlock/setup.c
>> > > +++ b/security/landlock/setup.c
>> > > @@ -14,6 +14,7 @@
>> > >  #include "fs.h"
>> > >  #include "ptrace.h"
>> > >  #include "setup.h"
>> > > +#include "net.h"
>> > > 
>> > >  bool landlock_initialized __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;
>> > > diff --git a/security/landlock/syscalls.c b/security/landlock/syscalls.c
>> > > index 8a54e87dbb17..3ad652d9a146 100644
>> > > --- a/security/landlock/syscalls.c
>> > > +++ b/security/landlock/syscalls.c
>> > > @@ -29,6 +29,7 @@
>> > >  #include "cred.h"
>> > >  #include "fs.h"
>> > >  #include "limits.h"
>> > > +#include "net.h"
>> > >  #include "ruleset.h"
>> > >  #include "setup.h"
>> > > 
>> > > @@ -74,7 +75,8 @@ static void build_check_abi(void)
>> > >  {
>> > >  	struct landlock_ruleset_attr ruleset_attr;
>> > >  	struct landlock_path_beneath_attr path_beneath_attr;
>> > > -	size_t ruleset_size, path_beneath_size;
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	size_t ruleset_size, path_beneath_size, net_port_size;
>> > > 
>> > >  	/*
>> > >  	 * For each user space ABI structures, first checks that there is no
>> > > @@ -82,13 +84,19 @@ static void build_check_abi(void)
>> > >  	 * struct size.
>> > >  	 */
>> > >  	ruleset_size = sizeof(ruleset_attr.handled_access_fs);
>> > > +	ruleset_size += sizeof(ruleset_attr.handled_access_net);
>> > >  	BUILD_BUG_ON(sizeof(ruleset_attr) != ruleset_size);
>> > > -	BUILD_BUG_ON(sizeof(ruleset_attr) != 8);
>> > > +	BUILD_BUG_ON(sizeof(ruleset_attr) != 16);
>> > > 
>> > >  	path_beneath_size = sizeof(path_beneath_attr.allowed_access);
>> > >  	path_beneath_size += sizeof(path_beneath_attr.parent_fd);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != path_beneath_size);
>> > >  	BUILD_BUG_ON(sizeof(path_beneath_attr) != 12);
>> > > +
>> > > +	net_port_size = sizeof(net_port_attr.allowed_access);
>> > > +	net_port_size += sizeof(net_port_attr.port);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != net_port_size);
>> > > +	BUILD_BUG_ON(sizeof(net_port_attr) != 16);
>> > >  }
>> > > 
>> > >  /* Ruleset handling */
>> > > @@ -129,7 +137,7 @@ static const struct file_operations ruleset_fops = {
>> > >  	.write = fop_dummy_write,
>> > >  };
>> > > 
>> > > -#define LANDLOCK_ABI_VERSION 3
>> > > +#define LANDLOCK_ABI_VERSION 4
>> > > 
>> > >  /**
>> > >   * sys_landlock_create_ruleset - Create a new ruleset
>> > > @@ -188,8 +196,14 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
>> > >  	    LANDLOCK_MASK_ACCESS_FS)
>> > >  		return -EINVAL;
>> > > 
>> > > +	/* Checks network content (and 32-bits cast). */
>> > > +	if ((ruleset_attr.handled_access_net | LANDLOCK_MASK_ACCESS_NET) !=
>> > > +	    LANDLOCK_MASK_ACCESS_NET)
>> > > +		return -EINVAL;
>> > > +
>> > >  	/* Checks arguments and transforms to kernel struct. */
>> > > -	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs);
>> > > +	ruleset = landlock_create_ruleset(ruleset_attr.handled_access_fs,
>> > > +					  ruleset_attr.handled_access_net);
>> > >  	if (IS_ERR(ruleset))
>> > >  		return PTR_ERR(ruleset);
>> > > 
>> > > @@ -282,7 +296,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	int res, err;
>> > >  	access_mask_t mask;
>> > > 
>> > > -	/* Copies raw user space buffer, only one type for now. */
>> > > +	/* Copies raw user space buffer. */
>> > 
>> > Shouldn't this be part of a previous patch?
>> 
>> I did it according Gunter's suggestion
>> https://lore.kernel.org/netdev/20230627.82cde73b1efe@gnoack.org/
> 
> Ok, that indeed makes more sense in this patch, please keep it.
> 
>> > 
>> > >  	res = copy_from_user(&path_beneath_attr, rule_attr,
>> > >  			     sizeof(path_beneath_attr));
>> > >  	if (res)
>> > > @@ -315,13 +329,49 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >  	return err;
>> > >  }
>> > > 
>> > > +static int add_rule_net_port(struct landlock_ruleset *ruleset,
>> > > +			     const void __user *const rule_attr)
>> > > +{
>> > > +	struct landlock_net_port_attr net_port_attr;
>> > > +	int res;
>> > > +	access_mask_t mask;
>> > > +
>> > > +	/* Copies raw user space buffer. */
>> > > +	res = copy_from_user(&net_port_attr, rule_attr, sizeof(net_port_attr));
>> > > +	if (res)
>> > > +		return -EFAULT;
>> > > +
>> > > +	/*
>> > > +	 * Informs about useless rule: empty allowed_access (i.e. deny rules)
>> > > +	 * are ignored by network actions.
>> > > +	 */
>> > > +	if (!net_port_attr.allowed_access)
>> > > +		return -ENOMSG;
>> > > +
>> > > +	/*
>> > > +	 * Checks that allowed_access matches the @ruleset constraints
>> > > +	 * (ruleset->access_masks[0] is automatically upgraded to 64-bits).
>> > > +	 */
>> > > +	mask = landlock_get_net_access_mask(ruleset, 0);
>> > > +	if ((net_port_attr.allowed_access | mask) != mask)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Denies inserting a rule with port higher than 65535. */
>> > 
>> > For consistency with the following comment:
>> > "Denies inserting a rule with port greater than 65535."
>> > 
>>   Done. Thanks.
>> > 
>> > > +	if (net_port_attr.port > U16_MAX)
>> > > +		return -EINVAL;
>> > > +
>> > > +	/* Imports the new rule. */
>> > > +	return landlock_append_net_rule(ruleset, net_port_attr.port,
>> > > +					net_port_attr.allowed_access);
>> > > +}
>> > > +
>> > >  /**
>> > >   * sys_landlock_add_rule - Add a new rule to a ruleset
>> > >   *
>> > >   * @ruleset_fd: File descriptor tied to the ruleset that should be extended
>> > >   *		with the new rule.
>> > > - * @rule_type: Identify the structure type pointed to by @rule_attr (only
>> > > - *             %LANDLOCK_RULE_PATH_BENEATH for now).
>> > > + * @rule_type: Identify the structure type pointed to by @rule_attr:
>> > > + *             %LANDLOCK_RULE_PATH_BENEATH or %LANDLOCK_RULE_NET_PORT.
>> > >   * @rule_attr: Pointer to a rule (only of type &struct
>> > >   *             landlock_path_beneath_attr for now).
>> > >   * @flags: Must be 0.
>> > > @@ -332,6 +382,8 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
>> > >   * Possible returned errors are:
>> > >   *
>> > >   * - %EOPNOTSUPP: Landlock is supported by the kernel but disabled at boot time;
>> > > + * - %EAFNOSUPPORT: @rule_type is LANDLOCK_RULE_NET_PORT but TCP/IP is not
>> > 
>> > %LANDLOCK_RULE_NET_PORT
>> 
>>  Done.
>> > 
>> > > + *   supported by the running kernel;
>> > >   * - %EINVAL: @flags is not 0, or inconsistent access in the rule (i.e.
>> > >   *   &landlock_path_beneath_attr.allowed_access is not a subset of the
>> > 
>> > &landlock_path_beneath_attr.allowed_access or
>> > &landlock_net_port_attr.allowed_access is not a subset of the
>> 
>>   Fixed. Thanks.
>> > 
>> > >   *   ruleset handled accesses);
>> > 
>> > EINVAL description needs to be updated, especially for port > U16_MAX:
>> > - *   ruleset handled accesses);
>> > + *   ruleset handled accesses), or &landlock_net_port_attr.port is
>> > +     greater than 65535;
>> 
>>  Done. Thanks.
>> > 
>> > 
>> > > @@ -366,6 +418,9 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
>> > >  	case LANDLOCK_RULE_PATH_BENEATH:
>> > >  		err = add_rule_path_beneath(ruleset, rule_attr);
>> > >  		break;
>> > > +	case LANDLOCK_RULE_NET_PORT:
>> > > +		err = add_rule_net_port(ruleset, rule_attr);
>> > > +		break;
>> > >  	default:
>> > >  		err = -EINVAL;
>> > >  		break;
>> > > diff --git a/tools/testing/selftests/landlock/base_test.c b/tools/testing/selftests/landlock/base_test.c
>> > > index 792c3f0a59b4..646f778dfb1e 100644
>> > > --- a/tools/testing/selftests/landlock/base_test.c
>> > > +++ b/tools/testing/selftests/landlock/base_test.c
>> > > @@ -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
>> > > 
>> > .
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-24  3:18         ` Konstantin Meskhidze (A)
@ 2023-10-24  9:03           ` Mickaël Salaün
  2023-10-24  9:12             ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-24  9:03 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Tue, Oct 24, 2023 at 06:18:54AM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/20/2023 12:49 PM, Mickaël Salaün пишет:
> > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
> > > 
> > > 
> > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
> > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:

> > > > > diff --git a/security/landlock/net.h b/security/landlock/net.h
> > > > > new file mode 100644
> > > > > index 000000000000..588a49fd6907
> > > > > --- /dev/null
> > > > > +++ b/security/landlock/net.h
> > > > > @@ -0,0 +1,33 @@
> > > > > +/* SPDX-License-Identifier: GPL-2.0-only */
> > > > > +/*
> > > > > + * Landlock LSM - Network management and hooks
> > > > > + *
> > > > > + * Copyright © 2022-2023 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)
> > > > > +{
> > > > > +}
> > > > > +
> > > > > +static inline int
> > > > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
> > > > > +			 access_mask_t access_rights);
> > > > > +{
> > > > > +	return -EAFNOSUPPORT;
> > > > > +}
> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > +
> > > > > +#endif /* _SECURITY_LANDLOCK_NET_H */
> > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> > > > > index 4c209acee01e..1fe4298ff4a7 100644
> > > > > --- a/security/landlock/ruleset.c
> > > > > +++ b/security/landlock/ruleset.c
> > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > >  	refcount_set(&new_ruleset->usage, 1);
> > > > >  	mutex_init(&new_ruleset->lock);
> > > > >  	new_ruleset->root_inode = RB_ROOT;
> > > > > +
> > > > > +#if IS_ENABLED(CONFIG_INET)
> > > > > +	new_ruleset->root_net_port = RB_ROOT;
> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > +
> > > > >  	new_ruleset->num_layers = num_layers;
> > > > >  	/*
> > > > >  	 * hierarchy = NULL
> > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > >  }
> > > > > > >  struct landlock_ruleset *
> > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
> > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
> > > > > +			const access_mask_t net_access_mask)
> > > > >  {
> > > > >  	struct landlock_ruleset *new_ruleset;
> > > > > > >  	/* Informs about useless ruleset. */
> > > > > -	if (!fs_access_mask)
> > > > > +	if (!fs_access_mask && !net_access_mask)
> > > > >  		return ERR_PTR(-ENOMSG);
> > > > >  	new_ruleset = create_ruleset(1);
> > > > > -	if (!IS_ERR(new_ruleset))
> > > > > +	if (IS_ERR(new_ruleset))
> > > > > +		return new_ruleset;
> > > > > +	if (fs_access_mask)
> > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > > > +	if (net_access_mask)
> > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > > > > This is good, but it is not tested: we need to add a test that
> > > both
> > > > handle FS and net restrictions. You can add one in net.c, just handling
> > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
> > > > that open("/") is denied, open("/dev") is allowed, and and only the
> > > > allowed port is allowed with bind(). This test should be simple and can
> > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> > > > after port_endianness. fcntl.h should then be included by net.c
> > > 
> > >   Ok.
> > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
> > > but it
> > > 
> > >   Yep. I added this kind of nest in fs_test.c to test both fs and network
> > > rules together.
> > > > is not complete. You can revamp this test and move it to net.c
> > > > following the above suggestions, keeping it consistent with other tests
> > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
> > > > > This test must failed if we change
> > > "ruleset->access_masks[layer_level] |="
> > > > to "ruleset->access_masks[layer_level] =" in
> > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
> > > 
> > >   Do you want to change it? Why?
> > 
> > The kernel code is correct and must not be changed. However, if by
> > mistake we change it and remove the OR, a test should catch that. We
> > need a test to assert this assumption.
> > 
> > >   Fs and network masks are ORed to not intersect with each other.
> > 
> > Yes, they are ORed, and we need a test to check that. Noting is
> > currently testing this OR (and the different rule type consistency).
> > I'm suggesting to revamp the layout1.with_net test into
> > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> > addition of different types.

From the other email:
> Thinking about this test. We don't need to add any additional ASSERT here.
> Anyway if we accidentally change "ruleset->access_masks[layer_level] |=" to
> "ruleset->access_masks[layer_level] =" we will fail either in opening
> directory or in port binding, cause adding a second rule (fs or net) will
> overwrite a first one's mask. it does not matter which one goes first. I
> will check it and send you a message.
> What do you think?

> 
>   About my previous comment.
> 
>   Checking the code we can  notice that adding fs mask goes first:
> 
> ...
> if (fs_access_mask)
> 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> if (net_access_mask)
> 		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> ....
> 
> So with we change "ruleset->access_masks[layer_level] |="
> >> > to "ruleset->access_masks[layer_level] =" in
> landlock_add_fs_access_mask() nothing bad will happen.
> But if we do that in landlock_add_net_access_mask()
> fs mask will be overwritten and adding fs rule will fail
> (as unhandled allowed_accesss).

Right. What is the conclusion here? Are you OK with my test proposal?

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-24  9:03           ` Mickaël Salaün
@ 2023-10-24  9:12             ` Konstantin Meskhidze (A)
  2023-10-25 11:29               ` Mickaël Salaün
  0 siblings, 1 reply; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-24  9:12 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/24/2023 12:03 PM, Mickaël Salaün пишет:
> On Tue, Oct 24, 2023 at 06:18:54AM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/20/2023 12:49 PM, Mickaël Salaün пишет:
>> > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> 
>> > > > > diff --git a/security/landlock/net.h b/security/landlock/net.h
>> > > > > new file mode 100644
>> > > > > index 000000000000..588a49fd6907
>> > > > > --- /dev/null
>> > > > > +++ b/security/landlock/net.h
>> > > > > @@ -0,0 +1,33 @@
>> > > > > +/* SPDX-License-Identifier: GPL-2.0-only */
>> > > > > +/*
>> > > > > + * Landlock LSM - Network management and hooks
>> > > > > + *
>> > > > > + * Copyright © 2022-2023 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)
>> > > > > +{
>> > > > > +}
>> > > > > +
>> > > > > +static inline int
>> > > > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> > > > > +			 access_mask_t access_rights);
>> > > > > +{
>> > > > > +	return -EAFNOSUPPORT;
>> > > > > +}
>> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > +
>> > > > > +#endif /* _SECURITY_LANDLOCK_NET_H */
>> > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > > > --- a/security/landlock/ruleset.c
>> > > > > +++ b/security/landlock/ruleset.c
>> > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > >  	refcount_set(&new_ruleset->usage, 1);
>> > > > >  	mutex_init(&new_ruleset->lock);
>> > > > >  	new_ruleset->root_inode = RB_ROOT;
>> > > > > +
>> > > > > +#if IS_ENABLED(CONFIG_INET)
>> > > > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > +
>> > > > >  	new_ruleset->num_layers = num_layers;
>> > > > >  	/*
>> > > > >  	 * hierarchy = NULL
>> > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > >  }
>> > > > > > >  struct landlock_ruleset *
>> > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > > > +			const access_mask_t net_access_mask)
>> > > > >  {
>> > > > >  	struct landlock_ruleset *new_ruleset;
>> > > > > > >  	/* Informs about useless ruleset. */
>> > > > > -	if (!fs_access_mask)
>> > > > > +	if (!fs_access_mask && !net_access_mask)
>> > > > >  		return ERR_PTR(-ENOMSG);
>> > > > >  	new_ruleset = create_ruleset(1);
>> > > > > -	if (!IS_ERR(new_ruleset))
>> > > > > +	if (IS_ERR(new_ruleset))
>> > > > > +		return new_ruleset;
>> > > > > +	if (fs_access_mask)
>> > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > > > +	if (net_access_mask)
>> > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > > > > This is good, but it is not tested: we need to add a test that
>> > > both
>> > > > handle FS and net restrictions. You can add one in net.c, just handling
>> > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > > > that open("/") is denied, open("/dev") is allowed, and and only the
>> > > > allowed port is allowed with bind(). This test should be simple and can
>> > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > > > after port_endianness. fcntl.h should then be included by net.c
>> > > 
>> > >   Ok.
>> > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
>> > > but it
>> > > 
>> > >   Yep. I added this kind of nest in fs_test.c to test both fs and network
>> > > rules together.
>> > > > is not complete. You can revamp this test and move it to net.c
>> > > > following the above suggestions, keeping it consistent with other tests
>> > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > > > > This test must failed if we change
>> > > "ruleset->access_masks[layer_level] |="
>> > > > to "ruleset->access_masks[layer_level] =" in
>> > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> > > 
>> > >   Do you want to change it? Why?
>> > 
>> > The kernel code is correct and must not be changed. However, if by
>> > mistake we change it and remove the OR, a test should catch that. We
>> > need a test to assert this assumption.
>> > 
>> > >   Fs and network masks are ORed to not intersect with each other.
>> > 
>> > Yes, they are ORed, and we need a test to check that. Noting is
>> > currently testing this OR (and the different rule type consistency).
>> > I'm suggesting to revamp the layout1.with_net test into
>> > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
>> > addition of different types.
> 
>>From the other email:
>> Thinking about this test. We don't need to add any additional ASSERT here.
>> Anyway if we accidentally change "ruleset->access_masks[layer_level] |=" to
>> "ruleset->access_masks[layer_level] =" we will fail either in opening
>> directory or in port binding, cause adding a second rule (fs or net) will
>> overwrite a first one's mask. it does not matter which one goes first. I
>> will check it and send you a message.
>> What do you think?
> 
>> 
>>   About my previous comment.
>> 
>>   Checking the code we can  notice that adding fs mask goes first:
>> 
>> ...
>> if (fs_access_mask)
>> 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> if (net_access_mask)
>> 		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> ....
>> 
>> So with we change "ruleset->access_masks[layer_level] |="
>> >> > to "ruleset->access_masks[layer_level] =" in
>> landlock_add_fs_access_mask() nothing bad will happen.
>> But if we do that in landlock_add_net_access_mask()
>> fs mask will be overwritten and adding fs rule will fail
>> (as unhandled allowed_accesss).
> 
> Right. What is the conclusion here? Are you OK with my test proposal?

   So we just check if landlock_add_net_access_mask() would be changed 
by mistake? Changing landlock_add_fs_access_mask() does not break the 
logic. Am I correct here?
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-24  9:12             ` Konstantin Meskhidze (A)
@ 2023-10-25 11:29               ` Mickaël Salaün
  2023-10-26  2:02                 ` Konstantin Meskhidze (A)
  0 siblings, 1 reply; 45+ messages in thread
From: Mickaël Salaün @ 2023-10-25 11:29 UTC (permalink / raw)
  To: Konstantin Meskhidze (A)
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin

On Tue, Oct 24, 2023 at 12:12:01PM +0300, Konstantin Meskhidze (A) wrote:
> 
> 
> 10/24/2023 12:03 PM, Mickaël Salaün пишет:
> > On Tue, Oct 24, 2023 at 06:18:54AM +0300, Konstantin Meskhidze (A) wrote:
> > > 
> > > 
> > > 10/20/2023 12:49 PM, Mickaël Salaün пишет:
> > > > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
> > > > > > > > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
> > > > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
> > 
> > > > > > > diff --git a/security/landlock/net.h b/security/landlock/net.h
> > > > > > > new file mode 100644
> > > > > > > index 000000000000..588a49fd6907
> > > > > > > --- /dev/null
> > > > > > > +++ b/security/landlock/net.h
> > > > > > > @@ -0,0 +1,33 @@
> > > > > > > +/* SPDX-License-Identifier: GPL-2.0-only */
> > > > > > > +/*
> > > > > > > + * Landlock LSM - Network management and hooks
> > > > > > > + *
> > > > > > > + * Copyright © 2022-2023 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)
> > > > > > > +{
> > > > > > > +}
> > > > > > > +
> > > > > > > +static inline int
> > > > > > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
> > > > > > > +			 access_mask_t access_rights);
> > > > > > > +{
> > > > > > > +	return -EAFNOSUPPORT;
> > > > > > > +}
> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > > > +
> > > > > > > +#endif /* _SECURITY_LANDLOCK_NET_H */
> > > > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
> > > > > > > index 4c209acee01e..1fe4298ff4a7 100644
> > > > > > > --- a/security/landlock/ruleset.c
> > > > > > > +++ b/security/landlock/ruleset.c
> > > > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > > > >  	refcount_set(&new_ruleset->usage, 1);
> > > > > > >  	mutex_init(&new_ruleset->lock);
> > > > > > >  	new_ruleset->root_inode = RB_ROOT;
> > > > > > > +
> > > > > > > +#if IS_ENABLED(CONFIG_INET)
> > > > > > > +	new_ruleset->root_net_port = RB_ROOT;
> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
> > > > > > > +
> > > > > > >  	new_ruleset->num_layers = num_layers;
> > > > > > >  	/*
> > > > > > >  	 * hierarchy = NULL
> > > > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
> > > > > > >  }
> > > > > > > > >  struct landlock_ruleset *
> > > > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
> > > > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
> > > > > > > +			const access_mask_t net_access_mask)
> > > > > > >  {
> > > > > > >  	struct landlock_ruleset *new_ruleset;
> > > > > > > > >  	/* Informs about useless ruleset. */
> > > > > > > -	if (!fs_access_mask)
> > > > > > > +	if (!fs_access_mask && !net_access_mask)
> > > > > > >  		return ERR_PTR(-ENOMSG);
> > > > > > >  	new_ruleset = create_ruleset(1);
> > > > > > > -	if (!IS_ERR(new_ruleset))
> > > > > > > +	if (IS_ERR(new_ruleset))
> > > > > > > +		return new_ruleset;
> > > > > > > +	if (fs_access_mask)
> > > > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > > > > > +	if (net_access_mask)
> > > > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > > > > > > This is good, but it is not tested: we need to add a test that
> > > > > both
> > > > > > handle FS and net restrictions. You can add one in net.c, just handling
> > > > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
> > > > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
> > > > > > that open("/") is denied, open("/dev") is allowed, and and only the
> > > > > > allowed port is allowed with bind(). This test should be simple and can
> > > > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
> > > > > > after port_endianness. fcntl.h should then be included by net.c
> > > > > > >   Ok.
> > > > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
> > > > > but it
> > > > > > >   Yep. I added this kind of nest in fs_test.c to test both
> > > fs and network
> > > > > rules together.
> > > > > > is not complete. You can revamp this test and move it to net.c
> > > > > > following the above suggestions, keeping it consistent with other tests
> > > > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
> > > > > > > This test must failed if we change
> > > > > "ruleset->access_masks[layer_level] |="
> > > > > > to "ruleset->access_masks[layer_level] =" in
> > > > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
> > > > > > >   Do you want to change it? Why?
> > > > > The kernel code is correct and must not be changed. However, if
> > > by
> > > > mistake we change it and remove the OR, a test should catch that. We
> > > > need a test to assert this assumption.
> > > > > >   Fs and network masks are ORed to not intersect with each
> > > other.
> > > > > Yes, they are ORed, and we need a test to check that. Noting is
> > > > currently testing this OR (and the different rule type consistency).
> > > > I'm suggesting to revamp the layout1.with_net test into
> > > > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
> > > > addition of different types.
> > 
> > > From the other email:
> > > Thinking about this test. We don't need to add any additional ASSERT here.
> > > Anyway if we accidentally change "ruleset->access_masks[layer_level] |=" to
> > > "ruleset->access_masks[layer_level] =" we will fail either in opening
> > > directory or in port binding, cause adding a second rule (fs or net) will
> > > overwrite a first one's mask. it does not matter which one goes first. I
> > > will check it and send you a message.
> > > What do you think?
> > 
> > > 
> > >   About my previous comment.
> > > 
> > >   Checking the code we can  notice that adding fs mask goes first:
> > > 
> > > ...
> > > if (fs_access_mask)
> > > 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
> > > if (net_access_mask)
> > > 		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> > > ....
> > > 
> > > So with we change "ruleset->access_masks[layer_level] |="
> > > >> > to "ruleset->access_masks[layer_level] =" in
> > > landlock_add_fs_access_mask() nothing bad will happen.
> > > But if we do that in landlock_add_net_access_mask()
> > > fs mask will be overwritten and adding fs rule will fail
> > > (as unhandled allowed_accesss).
> > 
> > Right. What is the conclusion here? Are you OK with my test proposal?
> 
>   So we just check if landlock_add_net_access_mask() would be changed by
> mistake?

With the current kernel code, yes.

> Changing landlock_add_fs_access_mask() does not break the logic. Am
> I correct here?

Yes, only landlock_add_net_access_mask() changes would be detected with
the current kernel code, but the test checks the whole semantic, so even
the following code with a buggy landlock_add_fs_access_mask() would be
detected:

if (net_access_mask)
	landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
if (fs_access_mask)
	landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);

^ permalink raw reply	[flat|nested] 45+ messages in thread

* Re: [PATCH v13 08/12] landlock: Add network rules and TCP hooks support
  2023-10-25 11:29               ` Mickaël Salaün
@ 2023-10-26  2:02                 ` Konstantin Meskhidze (A)
  0 siblings, 0 replies; 45+ messages in thread
From: Konstantin Meskhidze (A) @ 2023-10-26  2:02 UTC (permalink / raw)
  To: Mickaël Salaün
  Cc: willemdebruijn.kernel, gnoack3000, linux-security-module, netdev,
	netfilter-devel, yusongping, artem.kuzin



10/25/2023 2:29 PM, Mickaël Salaün пишет:
> On Tue, Oct 24, 2023 at 12:12:01PM +0300, Konstantin Meskhidze (A) wrote:
>> 
>> 
>> 10/24/2023 12:03 PM, Mickaël Salaün пишет:
>> > On Tue, Oct 24, 2023 at 06:18:54AM +0300, Konstantin Meskhidze (A) wrote:
>> > > 
>> > > 
>> > > 10/20/2023 12:49 PM, Mickaël Salaün пишет:
>> > > > On Fri, Oct 20, 2023 at 07:08:33AM +0300, Konstantin Meskhidze (A) wrote:
>> > > > > > > > > 10/18/2023 3:29 PM, Mickaël Salaün пишет:
>> > > > > > On Mon, Oct 16, 2023 at 09:50:26AM +0800, Konstantin Meskhidze wrote:
>> > 
>> > > > > > > diff --git a/security/landlock/net.h b/security/landlock/net.h
>> > > > > > > new file mode 100644
>> > > > > > > index 000000000000..588a49fd6907
>> > > > > > > --- /dev/null
>> > > > > > > +++ b/security/landlock/net.h
>> > > > > > > @@ -0,0 +1,33 @@
>> > > > > > > +/* SPDX-License-Identifier: GPL-2.0-only */
>> > > > > > > +/*
>> > > > > > > + * Landlock LSM - Network management and hooks
>> > > > > > > + *
>> > > > > > > + * Copyright © 2022-2023 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)
>> > > > > > > +{
>> > > > > > > +}
>> > > > > > > +
>> > > > > > > +static inline int
>> > > > > > > +landlock_append_net_rule(struct landlock_ruleset *const ruleset, const u16 port,
>> > > > > > > +			 access_mask_t access_rights);
>> > > > > > > +{
>> > > > > > > +	return -EAFNOSUPPORT;
>> > > > > > > +}
>> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > > > +
>> > > > > > > +#endif /* _SECURITY_LANDLOCK_NET_H */
>> > > > > > > diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
>> > > > > > > index 4c209acee01e..1fe4298ff4a7 100644
>> > > > > > > --- a/security/landlock/ruleset.c
>> > > > > > > +++ b/security/landlock/ruleset.c
>> > > > > > > @@ -36,6 +36,11 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > > > >  	refcount_set(&new_ruleset->usage, 1);
>> > > > > > >  	mutex_init(&new_ruleset->lock);
>> > > > > > >  	new_ruleset->root_inode = RB_ROOT;
>> > > > > > > +
>> > > > > > > +#if IS_ENABLED(CONFIG_INET)
>> > > > > > > +	new_ruleset->root_net_port = RB_ROOT;
>> > > > > > > +#endif /* IS_ENABLED(CONFIG_INET) */
>> > > > > > > +
>> > > > > > >  	new_ruleset->num_layers = num_layers;
>> > > > > > >  	/*
>> > > > > > >  	 * hierarchy = NULL
>> > > > > > > @@ -46,16 +51,21 @@ static struct landlock_ruleset *create_ruleset(const u32 num_layers)
>> > > > > > >  }
>> > > > > > > > >  struct landlock_ruleset *
>> > > > > > > -landlock_create_ruleset(const access_mask_t fs_access_mask)
>> > > > > > > +landlock_create_ruleset(const access_mask_t fs_access_mask,
>> > > > > > > +			const access_mask_t net_access_mask)
>> > > > > > >  {
>> > > > > > >  	struct landlock_ruleset *new_ruleset;
>> > > > > > > > >  	/* Informs about useless ruleset. */
>> > > > > > > -	if (!fs_access_mask)
>> > > > > > > +	if (!fs_access_mask && !net_access_mask)
>> > > > > > >  		return ERR_PTR(-ENOMSG);
>> > > > > > >  	new_ruleset = create_ruleset(1);
>> > > > > > > -	if (!IS_ERR(new_ruleset))
>> > > > > > > +	if (IS_ERR(new_ruleset))
>> > > > > > > +		return new_ruleset;
>> > > > > > > +	if (fs_access_mask)
>> > > > > > >  		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > > > > > +	if (net_access_mask)
>> > > > > > > +		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > > > > > > This is good, but it is not tested: we need to add a test that
>> > > > > both
>> > > > > > handle FS and net restrictions. You can add one in net.c, just handling
>> > > > > > LANDLOCK_ACCESS_FS_READ_DIR and LANDLOCK_ACCESS_NET_BIND_TCP, add one
>> > > > > > rule with path_beneath (e.g. /dev) and another with net_port, and check
>> > > > > > that open("/") is denied, open("/dev") is allowed, and and only the
>> > > > > > allowed port is allowed with bind(). This test should be simple and can
>> > > > > > only check against an IPv4 socket, i.e. using ipv4_tcp fixture, just
>> > > > > > after port_endianness. fcntl.h should then be included by net.c
>> > > > > > >   Ok.
>> > > > > > > I guess that was the purpose of layout1.with_net (in fs_test.c)
>> > > > > but it
>> > > > > > >   Yep. I added this kind of nest in fs_test.c to test both
>> > > fs and network
>> > > > > rules together.
>> > > > > > is not complete. You can revamp this test and move it to net.c
>> > > > > > following the above suggestions, keeping it consistent with other tests
>> > > > > > in net.c . You don't need the test_open() nor create_ruleset() helpers.
>> > > > > > > This test must failed if we change
>> > > > > "ruleset->access_masks[layer_level] |="
>> > > > > > to "ruleset->access_masks[layer_level] =" in
>> > > > > > landlock_add_fs_access_mask() or landlock_add_net_access_mask().
>> > > > > > >   Do you want to change it? Why?
>> > > > > The kernel code is correct and must not be changed. However, if
>> > > by
>> > > > mistake we change it and remove the OR, a test should catch that. We
>> > > > need a test to assert this assumption.
>> > > > > >   Fs and network masks are ORed to not intersect with each
>> > > other.
>> > > > > Yes, they are ORed, and we need a test to check that. Noting is
>> > > > currently testing this OR (and the different rule type consistency).
>> > > > I'm suggesting to revamp the layout1.with_net test into
>> > > > ipv4_tcp.with_fs and make it check ruleset->access_masks[] and rule
>> > > > addition of different types.
>> > 
>> > > From the other email:
>> > > Thinking about this test. We don't need to add any additional ASSERT here.
>> > > Anyway if we accidentally change "ruleset->access_masks[layer_level] |=" to
>> > > "ruleset->access_masks[layer_level] =" we will fail either in opening
>> > > directory or in port binding, cause adding a second rule (fs or net) will
>> > > overwrite a first one's mask. it does not matter which one goes first. I
>> > > will check it and send you a message.
>> > > What do you think?
>> > 
>> > > 
>> > >   About my previous comment.
>> > > 
>> > >   Checking the code we can  notice that adding fs mask goes first:
>> > > 
>> > > ...
>> > > if (fs_access_mask)
>> > > 		landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);
>> > > if (net_access_mask)
>> > > 		landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
>> > > ....
>> > > 
>> > > So with we change "ruleset->access_masks[layer_level] |="
>> > > >> > to "ruleset->access_masks[layer_level] =" in
>> > > landlock_add_fs_access_mask() nothing bad will happen.
>> > > But if we do that in landlock_add_net_access_mask()
>> > > fs mask will be overwritten and adding fs rule will fail
>> > > (as unhandled allowed_accesss).
>> > 
>> > Right. What is the conclusion here? Are you OK with my test proposal?
>> 
>>   So we just check if landlock_add_net_access_mask() would be changed by
>> mistake?
> 
> With the current kernel code, yes.
> 
>> Changing landlock_add_fs_access_mask() does not break the logic. Am
>> I correct here?
> 
> Yes, only landlock_add_net_access_mask() changes would be detected with
> the current kernel code, but the test checks the whole semantic, so even
> the following code with a buggy landlock_add_fs_access_mask() would be
> detected:
> 
> if (net_access_mask)
> 	landlock_add_net_access_mask(new_ruleset, net_access_mask, 0);
> if (fs_access_mask)
> 	landlock_add_fs_access_mask(new_ruleset, fs_access_mask, 0);

  I agree. Thanks for the explanation.
> .

^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2023-10-26  2:02 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-16  1:50 [PATCH v13 00/12] Network support for Landlock Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 01/12] landlock: Make ruleset's access masks more generic Konstantin Meskhidze
2023-10-18 12:28   ` Mickaël Salaün
2023-10-19  1:45     ` Konstantin Meskhidze (A)
2023-10-16  1:50 ` [PATCH v13 02/12] landlock: Allow FS topology changes for domains without such rule type Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 03/12] landlock: Refactor landlock_find_rule/insert_rule Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 04/12] landlock: Refactor merge/inherit_ruleset functions Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 05/12] landlock: Move and rename layer helpers Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 06/12] landlock: Refactor " Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 07/12] landlock: Refactor landlock_add_rule() syscall Konstantin Meskhidze
2023-10-18 12:28   ` Mickaël Salaün
2023-10-19 11:59     ` Konstantin Meskhidze (A)
2023-10-18 16:34   ` Mickaël Salaün
2023-10-19 11:57     ` Konstantin Meskhidze (A)
2023-10-16  1:50 ` [PATCH v13 08/12] landlock: Add network rules and TCP hooks support Konstantin Meskhidze
2023-10-18 12:29   ` Mickaël Salaün
2023-10-20  4:08     ` Konstantin Meskhidze (A)
2023-10-20  9:49       ` Mickaël Salaün
2023-10-20 11:58         ` Konstantin Meskhidze (A)
2023-10-20 15:41           ` Mickaël Salaün
2023-10-23  7:23             ` Konstantin Meskhidze (A)
2023-10-23  8:30               ` Mickaël Salaün
2023-10-23  8:56                 ` Konstantin Meskhidze (A)
2023-10-24  2:51         ` Konstantin Meskhidze (A)
2023-10-24  3:18         ` Konstantin Meskhidze (A)
2023-10-24  9:03           ` Mickaël Salaün
2023-10-24  9:12             ` Konstantin Meskhidze (A)
2023-10-25 11:29               ` Mickaël Salaün
2023-10-26  2:02                 ` Konstantin Meskhidze (A)
2023-10-18 16:34   ` Mickaël Salaün
2023-10-20  9:40     ` Konstantin Meskhidze (A)
2023-10-16  1:50 ` [PATCH v13 09/12] selftests/landlock: Share enforce_ruleset() Konstantin Meskhidze
2023-10-16  1:50 ` [PATCH v13 10/12] selftests/landlock: Add 7 new test variants dedicated to network Konstantin Meskhidze
2023-10-18 12:32   ` Mickaël Salaün
2023-10-20 11:41     ` Konstantin Meskhidze (A)
2023-10-20 15:40       ` Mickaël Salaün
2023-10-23  7:09         ` Konstantin Meskhidze (A)
2023-10-23  8:44           ` Mickaël Salaün
2023-10-23  9:15             ` Konstantin Meskhidze (A)
2023-10-16  1:50 ` [PATCH v13 11/12] samples/landlock: Add network demo Konstantin Meskhidze
2023-10-18 12:33   ` Mickaël Salaün
2023-10-20 11:59     ` Konstantin Meskhidze (A)
2023-10-16  1:50 ` [PATCH v13 12/12] landlock: Document Landlock's network support Konstantin Meskhidze
2023-10-18 12:34   ` Mickaël Salaün
2023-10-20 12:17     ` Konstantin Meskhidze (A)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.