All of lore.kernel.org
 help / color / mirror / Atom feed
From: Shervin Oloumi <enlightened@chromium.org>
To: mic@digikod.net
Cc: linux-security-module@vger.kernel.org, jorgelo@chromium.org,
	keescook@chromium.org, groeck@chromium.org, jeffxu@chromium.org,
	allenwebb@chromium.org, gnoack3000@gmail.com, areber@redhat.com,
	criu@openvz.org, linux-api@vger.kernel.org, jannh@google.com,
	brauner@kernel.org, Shervin Oloumi <enlightened@chromium.org>
Subject: [PATCH v2] lsm: adds process attribute getter for Landlock
Date: Thu, 18 May 2023 13:45:28 -0700	[thread overview]
Message-ID: <20230518204549.3139044-1-enlightened@chromium.org> (raw)
In-Reply-To: <ce44fc98-1234-fa53-5067-cd624866f44a@digikod.net>

Adds a new getprocattr hook function to the Landlock LSM, which tracks
the landlocked state of the process. This is invoked when user-space
reads /proc/[pid]/attr/domain to determine whether a given process is
sand-boxed using Landlock. When the target process is not sand-boxed,
the result is "none", otherwise the result is empty, as we still need to
decide what kind of domain information is best to provide in "domain".

The hook function also performs an access check. The request is rejected
if the tracing process is the same as the target process, or if the
tracing process domain is not an ancestor to the target process domain.

Adds a new directory for landlock under the process attribute
filesystem, and defines "domain" as a read-only process attribute entry
for landlock.

Signed-off-by: Shervin Oloumi <enlightened@chromium.org>
---
 fs/proc/base.c             | 11 +++++++++++
 security/landlock/fs.c     | 38 ++++++++++++++++++++++++++++++++++++++
 security/landlock/fs.h     |  1 +
 security/landlock/ptrace.c |  4 ++--
 security/landlock/ptrace.h |  3 +++
 5 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 9e479d7d202b..b257ea704666 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -2851,6 +2851,13 @@ static const struct pid_entry apparmor_attr_dir_stuff[] = {
 LSM_DIR_OPS(apparmor);
 #endif
 
+#ifdef CONFIG_SECURITY_LANDLOCK
+static const struct pid_entry landlock_attr_dir_stuff[] = {
+	ATTR("landlock", "domain", 0444),
+};
+LSM_DIR_OPS(landlock);
+#endif
+
 static const struct pid_entry attr_dir_stuff[] = {
 	ATTR(NULL, "current",		0666),
 	ATTR(NULL, "prev",		0444),
@@ -2866,6 +2873,10 @@ static const struct pid_entry attr_dir_stuff[] = {
 	DIR("apparmor",			0555,
 	    proc_apparmor_attr_dir_inode_ops, proc_apparmor_attr_dir_ops),
 #endif
+#ifdef CONFIG_SECURITY_LANDLOCK
+	DIR("landlock",                  0555,
+	    proc_landlock_attr_dir_inode_ops, proc_landlock_attr_dir_ops),
+#endif
 };
 
 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
diff --git a/security/landlock/fs.c b/security/landlock/fs.c
index adcea0fe7e68..2f8b0837a0fd 100644
--- a/security/landlock/fs.c
+++ b/security/landlock/fs.c
@@ -1280,6 +1280,42 @@ static int hook_file_truncate(struct file *const file)
 	return -EACCES;
 }
 
+/* process attribute interfaces */
+
+/**
+ * landlock_getprocattr - Landlock process attribute getter
+ * @task: the object task
+ * @name: the name of the attribute in /proc/.../attr
+ * @value: where to put the result
+ *
+ * Performs access checks and writes any applicable results to value
+ *
+ * Returns the length of the result inside value or an error code
+ */
+static int landlock_getprocattr(struct task_struct *task, const char *name,
+				char **value)
+{
+	char *val = "";
+	int slen;
+
+	// If the tracing process is landlocked, ensure its domain is an
+	// ancestor to the target process domain.
+	if (landlocked(current))
+		if (current == task || !task_is_scoped(current, task))
+			return -EACCES;
+
+	// The only supported attribute is "domain".
+	if (strcmp(name, "domain") != 0)
+		return -EINVAL;
+
+	if (!landlocked(task))
+		val = "none";
+
+	slen = strlen(val);
+	*value = val;
+	return slen;
+}
+
 static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(inode_free_security, hook_inode_free_security),
 
@@ -1302,6 +1338,8 @@ static struct security_hook_list landlock_hooks[] __lsm_ro_after_init = {
 	LSM_HOOK_INIT(file_alloc_security, hook_file_alloc_security),
 	LSM_HOOK_INIT(file_open, hook_file_open),
 	LSM_HOOK_INIT(file_truncate, hook_file_truncate),
+
+	LSM_HOOK_INIT(getprocattr, landlock_getprocattr),
 };
 
 __init void landlock_add_fs_hooks(void)
diff --git a/security/landlock/fs.h b/security/landlock/fs.h
index 488e4813680a..64145e8b5537 100644
--- a/security/landlock/fs.h
+++ b/security/landlock/fs.h
@@ -13,6 +13,7 @@
 #include <linux/init.h>
 #include <linux/rcupdate.h>
 
+#include "ptrace.h"
 #include "ruleset.h"
 #include "setup.h"
 
diff --git a/security/landlock/ptrace.c b/security/landlock/ptrace.c
index 4c5b9cd71286..de943f0f3899 100644
--- a/security/landlock/ptrace.c
+++ b/security/landlock/ptrace.c
@@ -47,8 +47,8 @@ static bool domain_scope_le(const struct landlock_ruleset *const parent,
 	return false;
 }
 
-static bool task_is_scoped(const struct task_struct *const parent,
-			   const struct task_struct *const child)
+const bool task_is_scoped(const struct task_struct *const parent,
+			  const struct task_struct *const child)
 {
 	bool is_scoped;
 	const struct landlock_ruleset *dom_parent, *dom_child;
diff --git a/security/landlock/ptrace.h b/security/landlock/ptrace.h
index 265b220ae3bf..c6eb08951fc1 100644
--- a/security/landlock/ptrace.h
+++ b/security/landlock/ptrace.h
@@ -11,4 +11,7 @@
 
 __init void landlock_add_ptrace_hooks(void);
 
+const bool task_is_scoped(const struct task_struct *const parent,
+			  const struct task_struct *const child);
+
 #endif /* _SECURITY_LANDLOCK_PTRACE_H */
-- 
2.40.1.698.g37aff9b760-goog


  parent reply	other threads:[~2023-05-18 20:46 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-02 18:52 [PATCH 0/1] process attribute support for Landlock enlightened
2023-03-02 18:52 ` [PATCH 1/1] lsm: adds process attribute getter " enlightened
2023-03-02 20:24   ` Casey Schaufler
2023-03-03 16:39   ` Günther Noack
2023-03-02 20:22 ` [PATCH 0/1] process attribute support " Casey Schaufler
2023-03-06 22:40   ` Shervin Oloumi
2023-03-07 17:51     ` Casey Schaufler
2023-03-06 19:18 ` Mickaël Salaün
2023-03-07 14:16   ` Mickaël Salaün
2023-03-08 22:25   ` Shervin Oloumi
2023-03-15  9:56     ` Mickaël Salaün
2023-03-16  6:19       ` Günther Noack
2023-03-17  8:38         ` Mickaël Salaün
2023-05-18 20:44       ` Shervin Oloumi
2023-05-24 16:09         ` Mickaël Salaün
2023-05-24 16:21         ` Mickaël Salaün
2023-05-18 20:45       ` Shervin Oloumi [this message]
2023-05-18 21:26         ` [PATCH v2] lsm: adds process attribute getter " Casey Schaufler
2023-05-22 19:56           ` Paul Moore
2023-05-23  6:13             ` Jeff Xu
2023-05-23 15:32               ` Casey Schaufler
2023-05-30 18:02                 ` Jeff Xu
2023-05-30 19:05                   ` Casey Schaufler
2023-05-31 13:01                   ` Mickaël Salaün
2023-06-01 20:45                     ` Jeff Xu
2023-06-01 21:30                       ` Casey Schaufler
2023-05-23 21:12               ` Paul Moore
2023-05-24 15:38                 ` Mickaël Salaün
2023-05-24 16:02                   ` Mickaël Salaün
2023-05-25 16:28                     ` Casey Schaufler
2023-05-30 18:05                       ` Jeff Xu
2023-05-30 19:19                         ` Casey Schaufler
2023-05-31 13:26                           ` Mickaël Salaün
2023-06-01 20:48                             ` Jeff Xu
2023-06-01 21:34                               ` Casey Schaufler
2023-06-01 22:08                                 ` Mickaël Salaün
2023-05-24 16:05           ` Mickaël Salaün
2023-05-19  0:11         ` kernel test robot
2023-05-19  5:22         ` kernel test robot
2023-05-24 16:48         ` Mickaël Salaün

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230518204549.3139044-1-enlightened@chromium.org \
    --to=enlightened@chromium.org \
    --cc=allenwebb@chromium.org \
    --cc=areber@redhat.com \
    --cc=brauner@kernel.org \
    --cc=criu@openvz.org \
    --cc=gnoack3000@gmail.com \
    --cc=groeck@chromium.org \
    --cc=jannh@google.com \
    --cc=jeffxu@chromium.org \
    --cc=jorgelo@chromium.org \
    --cc=keescook@chromium.org \
    --cc=linux-api@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.