Skip to contentSkip to conversation

Refactor Landlock access mask management

PATCH In review Mickaël Salaün 2 participants 10 messages Nov 9, 2024

Subsystems (2)
lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

v4 cover Refactor Landlock access mask management

Hi,

To simplify code for new access types [1], add 2 new helpers:
- landlock_union_access_masks()
- landlock_get_applicable_domain()

This fourth version mainly rename these helpers.

The last patch uses these helpers to optimize Landlock scope management like with filesystem and network access checks.

[1] https://lore.kernel.org/r/[email protected]

Previous version:
v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Regards,

Mickaël Salaün (3):
landlock: Refactor filesystem access mask management
landlock: Refactor network access mask management
landlock: Optimize scope enforcement

 security/landlock/fs.c       | 31 ++++-----------
 security/landlock/net.c      | 28 +++-----------
 security/landlock/ruleset.h  | 73 ++++++++++++++++++++++++++++++++----
 security/landlock/syscalls.c |  2 +-
 security/landlock/task.c     | 18 +++++++--
 5 files changed, 95 insertions(+), 57 deletions(-)

base-commit: dad2f20715163e80aab284fb092efc8c18bf97c7

Signature · 1 line
2.47.0
author maintainer
#1 lore raw

PATCH v4 1/3 landlock: Refactor filesystem access mask management

Replace get_raw_handled_fs_accesses() with a generic landlock_merge_access_masks(), and replace get_fs_domain() with a generic landlock_match_ruleset(). These helpers will also be useful for other types of access.

---

Changes since v3:
* Rename landlock_merge_access_masks() to landlock_union_access_masks(), suggested by Günther.
* Rename landlock_match_ruleset() to landlock_get_applicable_domain(), suggested by Günther.
* Rename the "ruleset" arguments to "domain" for consistency with these new helpers.
* Use typeof_member(), suggested by Günther. Also include kernel.h (instead of the new container_of.h to ease backports).

Changes since v2:
* Create a new type union access_masks_all instead of changing struct acces_masks.
* Replace get_fs_domain() with an explicit call to landlock_match_ruleset().

Changes since v1:
* Rename landlock_filter_access_masks() to landlock_match_ruleset().
* Rename local variables from domain to ruleset to mach helpers' semantic. We'll rename and move these helpers when we'll have a dedicated domain struct type.
* Rename the all_fs mask to any_fs.
* Add documentation, as suggested by Günther.

---
 security/landlock/fs.c       | 31 ++++-----------
 security/landlock/ruleset.h  | 73 ++++++++++++++++++++++++++++++++----
 security/landlock/syscalls.c |  2 +-
 3 files changed, 74 insertions(+), 32 deletions(-)
security/landlock/fs.c +8 -23
index 7d79fc8abe21..e31b97a9f175 100644
@@ -388,38 +388,22 @@ static bool is_nouser_or_private(const struct dentry *dentry)
 		unlikely(IS_PRIVATE(d_backing_inode(dentry))));
 }
 
-static access_mask_t
-get_raw_handled_fs_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_raw_fs_access_mask(domain, layer_level);
-	return access_dom;
-}
-
 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) |
+	return landlock_union_access_masks(domain).fs |
 	       LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
 }
 
-static const struct landlock_ruleset *
-get_fs_domain(const struct landlock_ruleset *const domain)
-{
-	if (!domain || !get_raw_handled_fs_accesses(domain))
-		return NULL;
-
-	return domain;
-}
+static const struct access_masks any_fs = {
+	.fs = ~0,
+};
 
 static const struct landlock_ruleset *get_current_fs_domain(void)
 {
-	return get_fs_domain(landlock_get_current_domain());
+	return landlock_get_applicable_domain(landlock_get_current_domain(),
+					      any_fs);
 }
 
 /*
@@ -1517,7 +1501,8 @@ static int hook_file_open(struct file *const file)
 	access_mask_t open_access_request, full_access_request, allowed_access,
 		optional_access;
 	const struct landlock_ruleset *const dom =
-		get_fs_domain(landlock_cred(file->f_cred)->domain);
+		landlock_get_applicable_domain(
+			landlock_cred(file->f_cred)->domain, any_fs);
 
 	if (!dom)
 		return 0;
security/landlock/ruleset.h +65 -8

102 lines not on this page · raw message

security/landlock/syscalls.c +1 -1
index f5a0e7182ec0..c097d356fa45 100644
@@ -329,7 +329,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
 		return -ENOMSG;
 
 	/* Checks that allowed_access matches the @ruleset constraints. */
-	mask = landlock_get_raw_fs_access_mask(ruleset, 0);
+	mask = ruleset->access_masks[0].fs;
 	if ((path_beneath_attr.allowed_access | mask) != mask)
 		return -EINVAL;
 
Signature · 1 line
2.47.0
author maintainer
#2 lore raw

Re: [PATCH v4 1/3] landlock: Refactor filesystem access mask management

Quoted 4 lines · Mickaël Salaün

Replace get_raw_handled_fs_accesses() with a generic landlock_merge_access_masks(), and replace get_fs_domain() with a generic landlock_match_ruleset(). These helpers will also be useful for other types of access.

Of course I need to update the commit messages with the new names...

Quoted 5 lines
#3 lore raw

Re: [PATCH v4 1/3] landlock: Refactor filesystem access mask management

Quoted 7 lines · Mickaël Salaün
Quoted 4 lines · Mickaël Salaün

Replace get_raw_handled_fs_accesses() with a generic landlock_merge_access_masks(), and replace get_fs_domain() with a generic landlock_match_ruleset(). These helpers will also be useful for other types of access.

Of course I need to update the commit messages with the new names...

Yes, those are still using the old function names.

At this point my other two remaining remarks are so minor, I think we can skip another version of this patch set. 👍

Thanks for simplifying the code!

—Günther

reviewer Reviewed-by on v4 1/3
#4 lore raw

Re: [PATCH v4 1/3] landlock: Refactor filesystem access mask management

In reply to Mickaël Salaün
Quoted 149 lines · Mickaël Salaün

Replace get_raw_handled_fs_accesses() with a generic landlock_merge_access_masks(), and replace get_fs_domain() with a generic landlock_match_ruleset(). These helpers will also be useful for other types of access.

---

Changes since v3:
* Rename landlock_merge_access_masks() to landlock_union_access_masks(), suggested by Günther.
* Rename landlock_match_ruleset() to landlock_get_applicable_domain(), suggested by Günther.
* Rename the "ruleset" arguments to "domain" for consistency with these new helpers.
* Use typeof_member(), suggested by Günther. Also include kernel.h (instead of the new container_of.h to ease backports).

Changes since v2:
* Create a new type union access_masks_all instead of changing struct acces_masks.
* Replace get_fs_domain() with an explicit call to landlock_match_ruleset().

Changes since v1:
* Rename landlock_filter_access_masks() to landlock_match_ruleset().
* Rename local variables from domain to ruleset to mach helpers' semantic. We'll rename and move these helpers when we'll have a dedicated domain struct type.
* Rename the all_fs mask to any_fs.
* Add documentation, as suggested by Günther.

---
 security/landlock/fs.c       | 31 ++++-----------
 security/landlock/ruleset.h  | 73 ++++++++++++++++++++++++++++++++----
 security/landlock/syscalls.c |  2 +-
 3 files changed, 74 insertions(+), 32 deletions(-)
security/landlock/fs.c +8 -23
index 7d79fc8abe21..e31b97a9f175 100644
@@ -388,38 +388,22 @@ static bool is_nouser_or_private(const struct dentry *dentry)
 		unlikely(IS_PRIVATE(d_backing_inode(dentry))));
 }
 
-static access_mask_t
-get_raw_handled_fs_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_raw_fs_access_mask(domain, layer_level);
-	return access_dom;
-}
-
 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) |
+	return landlock_union_access_masks(domain).fs |
 	       LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
 }
 
-static const struct landlock_ruleset *
-get_fs_domain(const struct landlock_ruleset *const domain)
-{
-	if (!domain || !get_raw_handled_fs_accesses(domain))
-		return NULL;
-
-	return domain;
-}
+static const struct access_masks any_fs = {
+	.fs = ~0,
+};
 
 static const struct landlock_ruleset *get_current_fs_domain(void)
 {
-	return get_fs_domain(landlock_get_current_domain());
+	return landlock_get_applicable_domain(landlock_get_current_domain(),
+					      any_fs);
 }
 
 /*
@@ -1517,7 +1501,8 @@ static int hook_file_open(struct file *const file)
 	access_mask_t open_access_request, full_access_request, allowed_access,
 		optional_access;
 	const struct landlock_ruleset *const dom =
-		get_fs_domain(landlock_cred(file->f_cred)->domain);
+		landlock_get_applicable_domain(
+			landlock_cred(file->f_cred)->domain, any_fs);
 
 	if (!dom)
 		return 0;
security/landlock/ruleset.h +28 -0
index 61bdbc550172..698fa3114cf4 100644
@@ -11,6 +11,7 @@
 
 #include <linux/bitops.h>
 #include <linux/build_bug.h>
+#include <linux/kernel.h>
 #include <linux/mutex.h>
 #include <linux/rbtree.h>
 #include <linux/refcount.h>
@@ -47,6 +48,15 @@ struct access_masks {
 	access_mask_t scope : LANDLOCK_NUM_SCOPE;
 };
 
+union access_masks_all {
+	struct access_masks masks;
+	u32 all;
+};
+
+/* Makes sure all fields are covered. */
+static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
+	      sizeof(typeof_member(union access_masks_all, all)));
+
 typedef u16 layer_mask_t;
 /* Makes sure all layers can be checked. */
 static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
@@ -260,6 +270,60 @@ static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
 		refcount_inc(&ruleset->usage);
 }
 
+/**
+ * landlock_union_access_masks - Return all access rights handled in the
+ *				 domain
+ *
+ * @domain: Landlock ruleset (used as a domain)
+ *
+ * Returns: an access_masks result of the OR of all the domain's access masks.
+ */
+static inline struct access_masks
+landlock_union_access_masks(const struct landlock_ruleset *const domain)
+{
+	union access_masks_all matches = {};
+	size_t layer_level;
+
+	for (layer_level = 0; layer_level < domain->num_layers; layer_level++) {
+		union access_masks_all layer = {
+			.masks = domain->access_masks[layer_level],
+		};

security/landlock/ruleset.h static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset) · line 300

Quoted 10 lines
security/landlock/ruleset.h +10 -0
+
+		matches.all |= layer.all;
+	}
+
+	return matches.masks;
+}
+
+/**
+ * landlock_get_applicable_domain - Return @domain if it applies to (handles)
+ *				    the access rights specified in @masks

Very minor wording suggestion: For clarity, I would suggest "at least one of":

Return @domain if it applies to (handles)
at least one of the access rights specified in @masks

Otherwise this can be interpreted as "...all of the access rights specified in @masks".

Quoted 66 lines
security/landlock/ruleset.h +27 -8
+ *
+ * @domain: Landlock ruleset (used as a domain)
+ * @masks: access masks
+ *
+ * Returns: @domain if any access rights specified in @masks is handled, or
+ * NULL otherwise.
+ */
+static inline const struct landlock_ruleset *
+landlock_get_applicable_domain(const struct landlock_ruleset *const domain,
+			       const struct access_masks masks)
+{
+	const union access_masks_all masks_all = {
+		.masks = masks,
+	};
+	union access_masks_all merge = {};
+
+	if (!domain)
+		return NULL;
+
+	merge.masks = landlock_union_access_masks(domain);
+	if (merge.all & masks_all.all)
+		return domain;
+
+	return NULL;
+}
+
 static inline void
 landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
 			    const access_mask_t fs_access_mask,
@@ -295,19 +359,12 @@ landlock_add_scope_mask(struct landlock_ruleset *const ruleset,
 	ruleset->access_masks[layer_level].scope |= mask;
 }
 
-static inline access_mask_t
-landlock_get_raw_fs_access_mask(const struct landlock_ruleset *const ruleset,
-				const u16 layer_level)
-{
-	return ruleset->access_masks[layer_level].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) |
+	return ruleset->access_masks[layer_level].fs |
 	       LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
 }
 
security/landlock/syscalls.c +1 -1
index f5a0e7182ec0..c097d356fa45 100644
@@ -329,7 +329,7 @@ static int add_rule_path_beneath(struct landlock_ruleset *const ruleset,
 		return -ENOMSG;
 
 	/* Checks that allowed_access matches the @ruleset constraints. */
-	mask = landlock_get_raw_fs_access_mask(ruleset, 0);
+	mask = ruleset->access_masks[0].fs;
 	if ((path_beneath_attr.allowed_access | mask) != mask)
 		return -EINVAL;
 
Signature · 1 line
2.47.0

Looks good, thanks! Names are much clearer now, IMHO. 👍

—Günther

author maintainer
#5 lore raw

PATCH v4 2/3 landlock: Refactor network access mask management

In reply to Mickaël Salaün

Replace get_raw_handled_net_accesses() and get_current_net_domain() with a call to landlock_match_ruleset().

---

Changes since v2:
* Replace get_current_net_domain() with explicit call to landlock_match_ruleset().

Changes since v1:
* Rename the all_net mask to any_net.

---
 security/landlock/net.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)
security/landlock/net.c +6 -22
index c8bcd29bde09..d5dcc4407a19 100644
@@ -39,27 +39,9 @@ int landlock_append_net_rule(struct landlock_ruleset *const ruleset,
 	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 const struct access_masks any_net = {
+	.net = ~0,
+};
 
 static int current_check_access_socket(struct socket *const sock,
 				       struct sockaddr *const address,
@@ -72,7 +54,9 @@ static int current_check_access_socket(struct socket *const sock,
 	struct landlock_id id = {
 		.type = LANDLOCK_KEY_NET_PORT,
 	};
-	const struct landlock_ruleset *const dom = get_current_net_domain();
+	const struct landlock_ruleset *const dom =
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       any_net);
 
 	if (!dom)
 		return 0;
Signature · 1 line
2.47.0
reviewer Reviewed-by on v4 2/3
#6 lore raw

Re: [PATCH v4 2/3] landlock: Refactor network access mask management

Quoted 69 lines · Mickaël Salaün

Replace get_raw_handled_net_accesses() and get_current_net_domain() with a call to landlock_match_ruleset().

---

Changes since v2:
* Replace get_current_net_domain() with explicit call to landlock_match_ruleset().

Changes since v1:
* Rename the all_net mask to any_net.

---
 security/landlock/net.c | 28 ++++++----------------------
 1 file changed, 6 insertions(+), 22 deletions(-)
security/landlock/net.c +6 -22
index c8bcd29bde09..d5dcc4407a19 100644
@@ -39,27 +39,9 @@ int landlock_append_net_rule(struct landlock_ruleset *const ruleset,
 	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 const struct access_masks any_net = {
+	.net = ~0,
+};
 
 static int current_check_access_socket(struct socket *const sock,
 				       struct sockaddr *const address,
@@ -72,7 +54,9 @@ static int current_check_access_socket(struct socket *const sock,
 	struct landlock_id id = {
 		.type = LANDLOCK_KEY_NET_PORT,
 	};
-	const struct landlock_ruleset *const dom = get_current_net_domain();
+	const struct landlock_ruleset *const dom =
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       any_net);
 
 	if (!dom)
 		return 0;
Signature · 1 line
2.47.0

—Günther

author maintainer
#7 lore raw

PATCH v4 3/3 landlock: Optimize scope enforcement

In reply to Mickaël Salaün

Do not walk through the domain hierarchy when the required scope is not supported by this domain. This is the same approach as for filesystem and network restrictions.

---

Changes since v2:
* Make the unix_scope variable global to the file and remove previous get_current_unix_scope_domain().

---
 security/landlock/task.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/task.c +15 -3
index 4acbd7c40eee..dc7dab78392e 100644
@@ -204,12 +204,17 @@ static bool is_abstract_socket(struct sock *const sock)
 	return false;
 }
 
+static const struct access_masks unix_scope = {
+	.scope = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
+};
+
 static int hook_unix_stream_connect(struct sock *const sock,
 				    struct sock *const other,
 				    struct sock *const newsk)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -225,7 +230,8 @@ static int hook_unix_may_send(struct socket *const sock,
 			      struct socket *const other)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	if (!dom)
 		return 0;
@@ -243,6 +249,10 @@ static int hook_unix_may_send(struct socket *const sock,
 	return 0;
 }
 
+static const struct access_masks signal_scope = {
+	.scope = LANDLOCK_SCOPE_SIGNAL,
+};
+
 static int hook_task_kill(struct task_struct *const p,
 			  struct kernel_siginfo *const info, const int sig,
 			  const struct cred *const cred)
@@ -256,6 +266,7 @@ static int hook_task_kill(struct task_struct *const p,
 	} else {
 		dom = landlock_get_current_domain();
 	}
+	dom = landlock_get_applicable_domain(dom, signal_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -279,7 +290,8 @@ static int hook_file_send_sigiotask(struct task_struct *tsk,
 
 	/* Lock already held by send_sigio() and send_sigurg(). */
 	lockdep_assert_held(&fown->lock);
-	dom = landlock_file(fown->file)->fown_domain;
+	dom = landlock_get_applicable_domain(
+		landlock_file(fown->file)->fown_domain, signal_scope);
 
 	/* Quick return for unowned socket. */
 	if (!dom)
Signature · 1 line
2.47.0
reviewer Reviewed-by on v4 3/3
#8 lore raw

Re: [PATCH v4 3/3] landlock: Optimize scope enforcement

Quoted 23 lines · Mickaël Salaün

Do not walk through the domain hierarchy when the required scope is not supported by this domain. This is the same approach as for filesystem and network restrictions.

---

Changes since v2:
* Make the unix_scope variable global to the file and remove previous get_current_unix_scope_domain().

---
 security/landlock/task.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/task.c +0 -0
index 4acbd7c40eee..dc7dab78392e 100644

security/landlock/task.c static bool is_abstract_socket(struct sock *const sock) · line 209

Quoted 6 lines
security/landlock/task.c +3 -0
@@ -204,12 +204,17 @@ static bool is_abstract_socket(struct sock *const sock)
 	return false;
 }
 
+static const struct access_masks unix_scope = {
+	.scope = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
+};

Optional nit: You could probably inline these two struct access_masks values into the respective functions where they are used. (But it's just a minor code style matter IMHO; both ways are fine.)

Quoted 54 lines
security/landlock/task.c +12 -3
+
 static int hook_unix_stream_connect(struct sock *const sock,
 				    struct sock *const other,
 				    struct sock *const newsk)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -225,7 +230,8 @@ static int hook_unix_may_send(struct socket *const sock,
 			      struct socket *const other)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	if (!dom)
 		return 0;
@@ -243,6 +249,10 @@ static int hook_unix_may_send(struct socket *const sock,
 	return 0;
 }
 
+static const struct access_masks signal_scope = {
+	.scope = LANDLOCK_SCOPE_SIGNAL,
+};
+
 static int hook_task_kill(struct task_struct *const p,
 			  struct kernel_siginfo *const info, const int sig,
 			  const struct cred *const cred)
@@ -256,6 +266,7 @@ static int hook_task_kill(struct task_struct *const p,
 	} else {
 		dom = landlock_get_current_domain();
 	}
+	dom = landlock_get_applicable_domain(dom, signal_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -279,7 +290,8 @@ static int hook_file_send_sigiotask(struct task_struct *tsk,
 
 	/* Lock already held by send_sigio() and send_sigurg(). */
 	lockdep_assert_held(&fown->lock);
-	dom = landlock_file(fown->file)->fown_domain;
+	dom = landlock_get_applicable_domain(
+		landlock_file(fown->file)->fown_domain, signal_scope);
 
 	/* Quick return for unowned socket. */
 	if (!dom)
Signature · 1 line
2.47.0

Looks good!

—Günther

author maintainer
#9 lore raw

Re: [PATCH v4 3/3] landlock: Optimize scope enforcement

Quoted 27 lines · Günther Noack
Quoted 26 lines · Mickaël Salaün

Do not walk through the domain hierarchy when the required scope is not supported by this domain. This is the same approach as for filesystem and network restrictions.

---

Changes since v2:
* Make the unix_scope variable global to the file and remove previous get_current_unix_scope_domain().

---
 security/landlock/task.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
security/landlock/task.c +0 -0
index 4acbd7c40eee..dc7dab78392e 100644
@@ -204,12 +204,17 @@ static bool is_abstract_socket(struct sock *const sock)
 	return false;
 }
 
Quoted 7 lines

security/landlock/task.c static bool is_abstract_socket(struct sock *const sock) · line 209

Quoted 3 lines
security/landlock/task.c +3 -0
+static const struct access_masks unix_scope = {
+	.scope = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
+};

Optional nit: You could probably inline these two struct access_masks values into the respective functions where they are used. (But it's just a minor code style matter IMHO; both ways are fine.)

That was the case in a previous version, but because we'll need to have it global for a next patch series, I already put it there to avoid useless code change:
https://lore.kernel.org/all/[email protected]/

Quoted 58 lines
Quoted 55 lines
security/landlock/task.c +12 -3
+
 static int hook_unix_stream_connect(struct sock *const sock,
 				    struct sock *const other,
 				    struct sock *const newsk)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -225,7 +230,8 @@ static int hook_unix_may_send(struct socket *const sock,
 			      struct socket *const other)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		landlock_get_applicable_domain(landlock_get_current_domain(),
+					       unix_scope);
 
 	if (!dom)
 		return 0;
@@ -243,6 +249,10 @@ static int hook_unix_may_send(struct socket *const sock,
 	return 0;
 }
 
+static const struct access_masks signal_scope = {
+	.scope = LANDLOCK_SCOPE_SIGNAL,
+};
+
 static int hook_task_kill(struct task_struct *const p,
 			  struct kernel_siginfo *const info, const int sig,
 			  const struct cred *const cred)
@@ -256,6 +266,7 @@ static int hook_task_kill(struct task_struct *const p,
 	} else {
 		dom = landlock_get_current_domain();
 	}
+	dom = landlock_get_applicable_domain(dom, signal_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -279,7 +290,8 @@ static int hook_file_send_sigiotask(struct task_struct *tsk,
 
 	/* Lock already held by send_sigio() and send_sigurg(). */
 	lockdep_assert_held(&fown->lock);
-	dom = landlock_file(fown->file)->fown_domain;
+	dom = landlock_get_applicable_domain(
+		landlock_file(fown->file)->fown_domain, signal_scope);
 
 	/* Quick return for unowned socket. */
 	if (!dom)
Signature · 1 line
2.47.0
Quoted 1 line

Looks good!

Thanks!

Quoted 3 lines

—Günther