Skip to contentSkip to conversation

Refactor Landlock access mask management

PATCH Superseded Mickaël Salaün 1 participant 4 messages Oct 14, 2024

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Mickaël Salaün

author maintainer
#0 lore raw

v2 cover Refactor Landlock access mask management

Hi,

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

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:
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       | 21 +++---------
 security/landlock/net.c      | 21 +++---------
 security/landlock/ruleset.h  | 66 +++++++++++++++++++++++++++++-------
 security/landlock/syscalls.c |  2 +-
 security/landlock/task.c     | 22 ++++++++++--
 5 files changed, 82 insertions(+), 50 deletions(-)

base-commit: 8cf0b93919e13d1e8d4466eb4080a4c4d9d66d7b

Signature · 1 line
2.47.0
author maintainer
#1 lore raw

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

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

Replace struct access_masks with union access_masks that includes a new "all" field to simplify mask filtering.

---

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       | 21 +++---------
 security/landlock/ruleset.h  | 66 +++++++++++++++++++++++++++++-------
 security/landlock/syscalls.c |  2 +-
 3 files changed, 59 insertions(+), 30 deletions(-)
security/landlock/fs.c +5 -16
index 7d79fc8abe21..5ab770442702 100644
@@ -388,33 +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_merge_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;
+	const union access_masks any_fs = {
+		.fs = ~0,
+	};
 
-	return domain;
+	return landlock_match_ruleset(domain, any_fs);
 }
 
 static const struct landlock_ruleset *get_current_fs_domain(void)
security/landlock/ruleset.h +53 -13

97 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

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

In reply to Mickaël Salaün

Replace the get_raw_handled_net_accesses() implementation with a call to landlock_match_ruleset().

---

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

---
 security/landlock/net.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
security/landlock/net.c +4 -17
index c8bcd29bde09..fdc1bb0a9c5d 100644
@@ -39,26 +39,13 @@ 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;
+	const union access_masks any_net = {
+		.net = ~0,
+	};
 
-	return dom;
+	return landlock_match_ruleset(landlock_get_current_domain(), any_net);
 }
 
 static int current_check_access_socket(struct socket *const sock,
Signature · 1 line
2.47.0
author maintainer
#3 lore raw

PATCH v2 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.

---
 security/landlock/task.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
security/landlock/task.c +19 -3
index 4acbd7c40eee..9725e0fd36d2 100644
@@ -204,12 +204,22 @@ static bool is_abstract_socket(struct sock *const sock)
 	return false;
 }
 
+static const struct landlock_ruleset *get_current_unix_scope_domain(void)
+{
+	const union access_masks unix_scope = {
+		.scope = LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET,
+	};
+
+	return landlock_match_ruleset(landlock_get_current_domain(),
+				      unix_scope);
+}
+
 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();
+		get_current_unix_scope_domain();
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -225,7 +235,7 @@ static int hook_unix_may_send(struct socket *const sock,
 			      struct socket *const other)
 {
 	const struct landlock_ruleset *const dom =
-		landlock_get_current_domain();
+		get_current_unix_scope_domain();
 
 	if (!dom)
 		return 0;
@@ -243,6 +253,10 @@ static int hook_unix_may_send(struct socket *const sock,
 	return 0;
 }
 
+static const union 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 +270,7 @@ static int hook_task_kill(struct task_struct *const p,
 	} else {
 		dom = landlock_get_current_domain();
 	}
+	dom = landlock_match_ruleset(dom, signal_scope);
 
 	/* Quick return for non-landlocked tasks. */
 	if (!dom)
@@ -279,7 +294,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_match_ruleset(landlock_file(fown->file)->fown_domain,
+				     signal_scope);
 
 	/* Quick return for unowned socket. */
 	if (!dom)
Signature · 1 line
2.47.0