All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: linux-integrity@vger.kernel.org
Cc: zohar@linux.ibm.com, serge@hallyn.com,
	christian.brauner@ubuntu.com, containers@lists.linux.dev,
	dmitry.kasatkin@gmail.com, ebiederm@xmission.com,
	krzysztof.struczynski@huawei.com, roberto.sassu@huawei.com,
	mpeters@redhat.com, lhinds@redhat.com, lsturman@redhat.com,
	puiterwi@redhat.com, jejb@linux.ibm.com, jamjoom@us.ibm.com,
	linux-kernel@vger.kernel.org, paul@paul-moore.com,
	rgb@redhat.com, linux-security-module@vger.kernel.org,
	jmorris@namei.org, Stefan Berger <stefanb@linux.ibm.com>
Subject: [PATCH v6 14/17] ima: Tie opened SecurityFS files to the IMA namespace it belongs to
Date: Fri, 10 Dec 2021 14:47:33 -0500	[thread overview]
Message-ID: <20211210194736.1538863-15-stefanb@linux.ibm.com> (raw)
In-Reply-To: <20211210194736.1538863-1-stefanb@linux.ibm.com>

Tie IMA's files in SecurityFS to the IMA namespace they belong to so that
also file descriptor that were passed or inherited to other user/IMA
namespaces will always access the data of the IMA namespace they originally
belonged to.

Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
---
 security/integrity/ima/ima_fs.c     | 74 ++++++++++++++++++++++++-----
 security/integrity/ima/ima_policy.c |  4 +-
 2 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/security/integrity/ima/ima_fs.c b/security/integrity/ima/ima_fs.c
index 0e582ceecc7f..a136d14f29ec 100644
--- a/security/integrity/ima/ima_fs.c
+++ b/security/integrity/ima/ima_fs.c
@@ -35,6 +35,20 @@ static int __init default_canonical_fmt_setup(char *str)
 }
 __setup("ima_canonical_fmt", default_canonical_fmt_setup);
 
+static inline struct user_namespace *ima_user_ns_from_file(struct file *filp)
+{
+	return filp->f_path.mnt->mnt_sb->s_user_ns;
+}
+
+static int ima_open(struct inode *inode, struct file *file)
+{
+	struct user_namespace *user_ns = ima_user_ns_from_file(file);
+	struct ima_namespace *ns = user_ns->ima_ns;
+
+	file->private_data = ns;
+	return 0;
+}
+
 static ssize_t ima_show_htable_value(char __user *buf, size_t count,
 				     loff_t *ppos, atomic_long_t *val)
 {
@@ -49,12 +63,13 @@ static ssize_t ima_show_htable_violations(struct file *filp,
 					  char __user *buf,
 					  size_t count, loff_t *ppos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = filp->private_data;
 
 	return ima_show_htable_value(buf, count, ppos, &ns->ima_htable.violations);
 }
 
 static const struct file_operations ima_htable_violations_ops = {
+	.open = ima_open,
 	.read = ima_show_htable_violations,
 	.llseek = generic_file_llseek,
 };
@@ -63,12 +78,13 @@ static ssize_t ima_show_measurements_count(struct file *filp,
 					   char __user *buf,
 					   size_t count, loff_t *ppos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = filp->private_data;
 
 	return ima_show_htable_value(buf, count, ppos, &ns->ima_htable.len);
 }
 
 static const struct file_operations ima_measurements_count_ops = {
+	.open = ima_open,
 	.read = ima_show_measurements_count,
 	.llseek = generic_file_llseek,
 };
@@ -76,7 +92,7 @@ static const struct file_operations ima_measurements_count_ops = {
 /* returns pointer to hlist_node */
 static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = m->private;
 	loff_t l = *pos;
 	struct ima_queue_entry *qe;
 
@@ -94,7 +110,7 @@ static void *ima_measurements_start(struct seq_file *m, loff_t *pos)
 
 static void *ima_measurements_next(struct seq_file *m, void *v, loff_t *pos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = m->private;
 	struct ima_queue_entry *qe = v;
 
 	/* lock protects when reading beyond last element
@@ -195,9 +211,26 @@ static const struct seq_operations ima_measurments_seqops = {
 	.show = ima_measurements_show
 };
 
+static int ima_seq_open(struct file *file, const struct seq_operations *seq_ops)
+{
+	struct user_namespace *user_ns = ima_user_ns_from_file(file);
+	struct ima_namespace *ns = user_ns->ima_ns;
+	struct seq_file *seq;
+	int err;
+
+	err = seq_open(file, seq_ops);
+	if (err)
+		return err;
+
+	seq = file->private_data;
+	seq->private = ns;
+
+	return 0;
+}
+
 static int ima_measurements_open(struct inode *inode, struct file *file)
 {
-	return seq_open(file, &ima_measurments_seqops);
+	return ima_seq_open(file, &ima_measurments_seqops);
 }
 
 static const struct file_operations ima_measurements_ops = {
@@ -263,7 +296,7 @@ static const struct seq_operations ima_ascii_measurements_seqops = {
 
 static int ima_ascii_measurements_open(struct inode *inode, struct file *file)
 {
-	return seq_open(file, &ima_ascii_measurements_seqops);
+	return ima_seq_open(file, &ima_ascii_measurements_seqops);
 }
 
 static const struct file_operations ima_ascii_measurements_ops = {
@@ -273,9 +306,8 @@ static const struct file_operations ima_ascii_measurements_ops = {
 	.release = seq_release,
 };
 
-static ssize_t ima_read_policy(char *path)
+static ssize_t ima_read_policy(struct ima_namespace *ns, char *path)
 {
-	struct ima_namespace *ns = get_current_ns();
 	void *data = NULL;
 	char *datap;
 	size_t size;
@@ -314,10 +346,23 @@ static ssize_t ima_read_policy(char *path)
 		return pathlen;
 }
 
+static struct ima_namespace *ima_filp_private(struct file *filp)
+{
+	if (!(filp->f_flags & O_WRONLY)) {
+#ifdef CONFIG_IMA_READ_POLICY
+		struct seq_file *seq;
+
+		seq = filp->private_data;
+		return seq->private;
+#endif
+	}
+	return filp->private_data;
+}
+
 static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 				size_t datalen, loff_t *ppos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = ima_filp_private(file);
 	char *data;
 	ssize_t result;
 
@@ -340,7 +385,7 @@ static ssize_t ima_write_policy(struct file *file, const char __user *buf,
 		goto out_free;
 
 	if (data[0] == '/') {
-		result = ima_read_policy(data);
+		result = ima_read_policy(ns, data);
 	} else if (ima_appraise & IMA_APPRAISE_POLICY) {
 		pr_err("signed policy file (specified as an absolute pathname) required\n");
 		integrity_audit_msg(AUDIT_INTEGRITY_STATUS, NULL, NULL,
@@ -386,7 +431,8 @@ static const struct seq_operations ima_policy_seqops = {
  */
 static int ima_open_policy(struct inode *inode, struct file *filp)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct user_namespace *user_ns = ima_user_ns_from_file(filp);
+	struct ima_namespace *ns = user_ns->ima_ns;
 
 	if (!(filp->f_flags & O_WRONLY)) {
 #ifndef	CONFIG_IMA_READ_POLICY
@@ -396,11 +442,13 @@ static int ima_open_policy(struct inode *inode, struct file *filp)
 			return -EACCES;
 		if (!capable(CAP_SYS_ADMIN))
 			return -EPERM;
-		return seq_open(filp, &ima_policy_seqops);
+		return ima_seq_open(filp, &ima_policy_seqops);
 #endif
 	}
 	if (test_and_set_bit(IMA_FS_BUSY, &ns->ima_fs_flags))
 		return -EBUSY;
+
+	filp->private_data = ns;
 	return 0;
 }
 
@@ -413,7 +461,7 @@ static int ima_open_policy(struct inode *inode, struct file *filp)
  */
 static int ima_release_policy(struct inode *inode, struct file *file)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = ima_filp_private(file);
 	const char *cause = ns->valid_policy ? "completed" : "failed";
 
 	if ((file->f_flags & O_ACCMODE) == O_RDONLY)
diff --git a/security/integrity/ima/ima_policy.c b/security/integrity/ima/ima_policy.c
index 747dca6131d6..c2acd4cf529d 100644
--- a/security/integrity/ima/ima_policy.c
+++ b/security/integrity/ima/ima_policy.c
@@ -1908,7 +1908,7 @@ static const char *const mask_tokens[] = {
 
 void *ima_policy_start(struct seq_file *m, loff_t *pos)
 {
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = m->private;
 	loff_t l = *pos;
 	struct ima_rule_entry *entry;
 	struct list_head *ima_rules_tmp;
@@ -1928,7 +1928,7 @@ void *ima_policy_start(struct seq_file *m, loff_t *pos)
 void *ima_policy_next(struct seq_file *m, void *v, loff_t *pos)
 {
 	struct ima_rule_entry *entry = v;
-	struct ima_namespace *ns = get_current_ns();
+	struct ima_namespace *ns = m->private;
 
 	rcu_read_lock();
 	entry = list_entry_rcu(entry->list.next, struct ima_rule_entry, list);
-- 
2.31.1


  parent reply	other threads:[~2021-12-10 19:47 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 19:47 [PATCH v6 00/17] ima: Namespace IMA with audit support in IMA-ns Stefan Berger
2021-12-10 19:47 ` [PATCH v6 01/17] ima: Add IMA namespace support Stefan Berger
2021-12-13 21:11   ` Stefan Berger
2021-12-15 19:41     ` Mimi Zohar
2021-12-15 21:10   ` Mimi Zohar
2021-12-16  2:34     ` Stefan Berger
2021-12-10 19:47 ` [PATCH v6 02/17] ima: Define ns_status for storing namespaced iint data Stefan Berger
2021-12-15 21:12   ` Mimi Zohar
2021-12-16  2:37     ` Stefan Berger
2021-12-16  3:53       ` Mimi Zohar
2021-12-16  4:00         ` Stefan Berger
2021-12-10 19:47 ` [PATCH v6 03/17] ima: Namespace audit status flags Stefan Berger
2021-12-15 21:15   ` Mimi Zohar
2021-12-16  2:38     ` Stefan Berger
2021-12-16  3:54       ` Mimi Zohar
2021-12-10 19:47 ` [PATCH v6 04/17] ima: Move delayed work queue and variables into ima_namespace Stefan Berger
2021-12-10 19:47 ` [PATCH v6 05/17] ima: Move IMA's keys queue related " Stefan Berger
2021-12-10 19:47 ` [PATCH v6 06/17] ima: Move policy " Stefan Berger
2021-12-13 21:15   ` Stefan Berger
2021-12-10 19:47 ` [PATCH v6 07/17] ima: Move ima_htable " Stefan Berger
2021-12-10 19:47 ` [PATCH v6 08/17] ima: Move measurement list related variables " Stefan Berger
2021-12-10 19:47 ` [PATCH v6 09/17] ima: Only accept AUDIT rules for IMA non-init_ima_ns namespaces for now Stefan Berger
2021-12-10 19:47 ` [PATCH v6 10/17] ima: Implement hierarchical processing of file accesses Stefan Berger
2021-12-10 19:47 ` [PATCH v6 11/17] securityfs: Only use simple_pin_fs/simple_release_fs for init_user_ns Stefan Berger
2021-12-10 19:47 ` [PATCH v6 12/17] securityfs: Extend securityfs with namespacing support Stefan Berger
2021-12-11 10:50   ` Christian Brauner
2021-12-11 22:31     ` Stefan Berger
2021-12-10 19:47 ` [PATCH v6 13/17] ima: Move some IMA policy and filesystem related variables into ima_namespace Stefan Berger
2021-12-10 19:47 ` Stefan Berger [this message]
2021-12-11 11:00   ` [PATCH v6 14/17] ima: Tie opened SecurityFS files to the IMA namespace it belongs to Christian Brauner
2021-12-11 22:33     ` Stefan Berger
2021-12-10 19:47 ` [PATCH v6 15/17] ima: Use mac_admin_ns_capable() to check corresponding capability Stefan Berger
2021-12-11 15:29   ` Serge E. Hallyn
2021-12-11 16:05     ` James Bottomley
2021-12-11 19:22       ` Serge E. Hallyn
2021-12-11 20:12         ` James Bottomley
2021-12-10 19:47 ` [PATCH v6 16/17] ima: Move dentry into ima_namespace and others onto stack Stefan Berger
2021-12-10 19:47 ` [PATCH v6 17/17] ima: Setup securityfs for IMA namespace Stefan Berger
2021-12-15 21:31   ` Mimi Zohar
2021-12-16  2:41     ` Stefan Berger

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=20211210194736.1538863-15-stefanb@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=containers@lists.linux.dev \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=ebiederm@xmission.com \
    --cc=jamjoom@us.ibm.com \
    --cc=jejb@linux.ibm.com \
    --cc=jmorris@namei.org \
    --cc=krzysztof.struczynski@huawei.com \
    --cc=lhinds@redhat.com \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=lsturman@redhat.com \
    --cc=mpeters@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=puiterwi@redhat.com \
    --cc=rgb@redhat.com \
    --cc=roberto.sassu@huawei.com \
    --cc=serge@hallyn.com \
    --cc=zohar@linux.ibm.com \
    /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.