linux-security-module.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Roberto Sassu <roberto.sassu@huaweicloud.com>
To: viro@zeniv.linux.org.uk, brauner@kernel.org,
	chuck.lever@oracle.com, jlayton@kernel.org, neilb@suse.de,
	kolga@netapp.com, Dai.Ngo@oracle.com, tom@talpey.com,
	paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com,
	zohar@linux.ibm.com, dmitry.kasatkin@gmail.com,
	dhowells@redhat.com, jarkko@kernel.org,
	stephen.smalley.work@gmail.com, eparis@parisplace.org,
	casey@schaufler-ca.com, mic@digikod.net
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-security-module@vger.kernel.org,
	linux-integrity@vger.kernel.org, keyrings@vger.kernel.org,
	selinux@vger.kernel.org, Roberto Sassu <roberto.sassu@huawei.com>
Subject: [PATCH v6 23/25] evm: Remove dependency on 'integrity' LSM
Date: Mon, 20 Nov 2023 18:33:16 +0100	[thread overview]
Message-ID: <20231120173318.1132868-24-roberto.sassu@huaweicloud.com> (raw)
In-Reply-To: <20231120173318.1132868-1-roberto.sassu@huaweicloud.com>

From: Roberto Sassu <roberto.sassu@huawei.com>

Similarly to IMA, introduce EVM own integrity metadata (evm_iint_cache,
with EVM-specific fields from integrity_iint_cache), and reserve them from
the 'evm' LSM.

First, replace the integrity_iint_cache structure with evm_iint_cache in
various places of the EVM code.

Then, reserve space in the security blob for the evm_iint_cache structure,
so that retrieval always succeeds. Replace integrity_inode_get() and
integrity_iint_find() with evm_inode_get_iint(), to retrieve the
evm_iint_cache structure.

Initialize the new evm_iint_cache structure by registering
evm_inode_alloc_security() as implementation of the inode_alloc_security
LSM hook.

Since now IMA and EVM integrity metadata are disjoint, and always
available, remove the iint parameter from evm_verifyxattr() and always
retrieve the evm_iint_cache structure in evm_verify_hmac(), called by
evm_verifyxattr() and evm_verify_current_integrity().

Signed-off-by: Roberto Sassu <roberto.sassu@huawei.com>
---
 include/linux/evm.h                   |  8 +---
 security/integrity/evm/evm.h          | 17 ++++++++
 security/integrity/evm/evm_crypto.c   |  5 +--
 security/integrity/evm/evm_main.c     | 63 ++++++++++++++-------------
 security/integrity/ima/ima_appraise.c |  2 +-
 5 files changed, 54 insertions(+), 41 deletions(-)

diff --git a/include/linux/evm.h b/include/linux/evm.h
index cb481eccc967..d48d6da32315 100644
--- a/include/linux/evm.h
+++ b/include/linux/evm.h
@@ -12,15 +12,12 @@
 #include <linux/integrity.h>
 #include <linux/xattr.h>
 
-struct integrity_iint_cache;
-
 #ifdef CONFIG_EVM
 extern int evm_set_key(void *key, size_t keylen);
 extern enum integrity_status evm_verifyxattr(struct dentry *dentry,
 					     const char *xattr_name,
 					     void *xattr_value,
-					     size_t xattr_value_len,
-					     struct integrity_iint_cache *iint);
+					     size_t xattr_value_len);
 int evm_inode_init_security(struct inode *inode, struct inode *dir,
 			    const struct qstr *qstr, struct xattr *xattrs,
 			    int *xattr_count);
@@ -48,8 +45,7 @@ static inline int evm_set_key(void *key, size_t keylen)
 static inline enum integrity_status evm_verifyxattr(struct dentry *dentry,
 						    const char *xattr_name,
 						    void *xattr_value,
-						    size_t xattr_value_len,
-					struct integrity_iint_cache *iint)
+						    size_t xattr_value_len)
 {
 	return INTEGRITY_UNKNOWN;
 }
diff --git a/security/integrity/evm/evm.h b/security/integrity/evm/evm.h
index 53bd7fec93fa..478b6fbca699 100644
--- a/security/integrity/evm/evm.h
+++ b/security/integrity/evm/evm.h
@@ -32,6 +32,23 @@ struct xattr_list {
 	bool enabled;
 };
 
+/* EVM integrity metadata associated with an inode */
+struct evm_iint_cache {
+	unsigned long flags;
+	enum integrity_status evm_status:4;
+};
+
+extern struct lsm_blob_sizes evm_blob_sizes;
+
+static inline struct evm_iint_cache *
+evm_inode_get_iint(const struct inode *inode)
+{
+	struct evm_iint_cache *evm_iint_sec;
+
+	evm_iint_sec = inode->i_security + evm_blob_sizes.lbs_inode;
+	return evm_iint_sec;
+}
+
 extern int evm_initialized;
 
 #define EVM_ATTR_FSUUID		0x0001
diff --git a/security/integrity/evm/evm_crypto.c b/security/integrity/evm/evm_crypto.c
index b1ffd4cc0b44..c69422cc4a52 100644
--- a/security/integrity/evm/evm_crypto.c
+++ b/security/integrity/evm/evm_crypto.c
@@ -322,11 +322,10 @@ int evm_calc_hash(struct dentry *dentry, const char *req_xattr_name,
 static int evm_is_immutable(struct dentry *dentry, struct inode *inode)
 {
 	const struct evm_ima_xattr_data *xattr_data = NULL;
-	struct integrity_iint_cache *iint;
+	struct evm_iint_cache *iint = evm_inode_get_iint(inode);
 	int rc = 0;
 
-	iint = integrity_iint_find(inode);
-	if (iint && (iint->flags & EVM_IMMUTABLE_DIGSIG))
+	if (iint->flags & EVM_IMMUTABLE_DIGSIG)
 		return 1;
 
 	/* Do this the hard way */
diff --git a/security/integrity/evm/evm_main.c b/security/integrity/evm/evm_main.c
index 1e59a985b845..5aa5207a75e1 100644
--- a/security/integrity/evm/evm_main.c
+++ b/security/integrity/evm/evm_main.c
@@ -167,18 +167,20 @@ static int evm_find_protected_xattrs(struct dentry *dentry)
 static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 					     const char *xattr_name,
 					     char *xattr_value,
-					     size_t xattr_value_len,
-					     struct integrity_iint_cache *iint)
+					     size_t xattr_value_len)
 {
 	struct evm_ima_xattr_data *xattr_data = NULL;
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
 	struct inode *inode;
+	struct evm_iint_cache *iint;
 	int rc, xattr_len, evm_immutable = 0;
 
-	if (iint && (iint->evm_status == INTEGRITY_PASS ||
-		     iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
+	iint = evm_inode_get_iint(d_backing_inode(dentry));
+
+	if ((iint->evm_status == INTEGRITY_PASS ||
+	     iint->evm_status == INTEGRITY_PASS_IMMUTABLE))
 		return iint->evm_status;
 
 	/* if status is not PASS, try to check again - against -ENOMEM */
@@ -243,8 +245,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 			inode = d_backing_inode(dentry);
 
 			if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
-				if (iint)
-					iint->flags |= EVM_IMMUTABLE_DIGSIG;
+				iint->flags |= EVM_IMMUTABLE_DIGSIG;
 				evm_status = INTEGRITY_PASS_IMMUTABLE;
 			} else if (!IS_RDONLY(inode) &&
 				   !(inode->i_sb->s_readonly_remount) &&
@@ -271,8 +272,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
 		  digest.digest);
 out:
-	if (iint)
-		iint->evm_status = evm_status;
+	iint->evm_status = evm_status;
 	kfree(xattr_data);
 	return evm_status;
 }
@@ -389,7 +389,6 @@ int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
  * @xattr_name: requested xattr
  * @xattr_value: requested xattr value
  * @xattr_value_len: requested xattr value length
- * @iint: inode integrity metadata
  *
  * Calculate the HMAC for the given dentry and verify it against the stored
  * security.evm xattr. For performance, use the xattr value and length
@@ -402,19 +401,13 @@ int evm_read_protected_xattrs(struct dentry *dentry, u8 *buffer,
  */
 enum integrity_status evm_verifyxattr(struct dentry *dentry,
 				      const char *xattr_name,
-				      void *xattr_value, size_t xattr_value_len,
-				      struct integrity_iint_cache *iint)
+				      void *xattr_value, size_t xattr_value_len)
 {
 	if (!evm_key_loaded() || !evm_protected_xattr(xattr_name))
 		return INTEGRITY_UNKNOWN;
 
-	if (!iint) {
-		iint = integrity_inode_get(d_backing_inode(dentry));
-		if (!iint)
-			return INTEGRITY_UNKNOWN;
-	}
 	return evm_verify_hmac(dentry, xattr_name, xattr_value,
-				 xattr_value_len, iint);
+				 xattr_value_len);
 }
 EXPORT_SYMBOL_GPL(evm_verifyxattr);
 
@@ -431,7 +424,7 @@ static enum integrity_status evm_verify_current_integrity(struct dentry *dentry)
 
 	if (!evm_key_loaded() || !S_ISREG(inode->i_mode) || evm_fixmode)
 		return INTEGRITY_PASS;
-	return evm_verify_hmac(dentry, NULL, NULL, 0, NULL);
+	return evm_verify_hmac(dentry, NULL, NULL, 0);
 }
 
 /*
@@ -503,14 +496,14 @@ static int evm_protect_xattr(struct mnt_idmap *idmap,
 
 	evm_status = evm_verify_current_integrity(dentry);
 	if (evm_status == INTEGRITY_NOXATTRS) {
-		struct integrity_iint_cache *iint;
+		struct evm_iint_cache *iint;
 
 		/* Exception if the HMAC is not going to be calculated. */
 		if (evm_hmac_disabled())
 			return 0;
 
-		iint = integrity_iint_find(d_backing_inode(dentry));
-		if (iint && (iint->flags & IMA_NEW_FILE))
+		iint = evm_inode_get_iint(d_backing_inode(dentry));
+		if ((iint->flags & IMA_NEW_FILE))
 			return 0;
 
 		/* exception for pseudo filesystems */
@@ -712,11 +705,9 @@ static int evm_inode_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
 
 static void evm_reset_status(struct inode *inode)
 {
-	struct integrity_iint_cache *iint;
+	struct evm_iint_cache *iint = evm_inode_get_iint(inode);
 
-	iint = integrity_iint_find(inode);
-	if (iint)
-		iint->evm_status = INTEGRITY_UNKNOWN;
+	iint->evm_status = INTEGRITY_UNKNOWN;
 }
 
 /**
@@ -982,12 +973,11 @@ EXPORT_SYMBOL_GPL(evm_inode_init_security);
 static void __maybe_unused
 evm_post_path_mknod(struct mnt_idmap *idmap, struct dentry *dentry)
 {
-	struct integrity_iint_cache *iint;
+	struct evm_iint_cache *iint;
 
-	iint = integrity_inode_get(d_backing_inode(dentry));
-	if (iint)
-		/* needed for successful verification of empty files */
-		iint->flags |= IMA_NEW_FILE;
+	iint = evm_inode_get_iint(d_backing_inode(dentry));
+	/* needed for successful verification of empty files */
+	iint->flags |= IMA_NEW_FILE;
 }
 
 #ifdef CONFIG_EVM_LOAD_X509
@@ -1029,6 +1019,15 @@ static int __init init_evm(void)
 	return error;
 }
 
+static int evm_inode_alloc_security(struct inode *inode)
+{
+	struct evm_iint_cache *evm_iint = evm_inode_get_iint(inode);
+
+	evm_iint->flags = 0UL;
+	evm_iint->evm_status = INTEGRITY_UNKNOWN;
+	return 0;
+}
+
 static struct security_hook_list evm_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(inode_setattr, evm_inode_setattr),
 	LSM_HOOK_INIT(inode_post_setattr, evm_inode_post_setattr),
@@ -1041,6 +1040,7 @@ static struct security_hook_list evm_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(inode_removexattr, evm_inode_removexattr),
 	LSM_HOOK_INIT(inode_post_removexattr, evm_inode_post_removexattr),
 	LSM_HOOK_INIT(inode_init_security, evm_inode_init_security),
+	LSM_HOOK_INIT(inode_alloc_security, evm_inode_alloc_security),
 #ifdef CONFIG_SECURITY_PATH
 	LSM_HOOK_INIT(path_post_mknod, evm_post_path_mknod),
 #endif
@@ -1064,7 +1064,8 @@ int __init init_evm_lsm(void)
 	return 0;
 }
 
-static struct lsm_blob_sizes evm_blob_sizes __ro_after_init = {
+struct lsm_blob_sizes evm_blob_sizes __ro_after_init = {
+	.lbs_inode = sizeof(struct evm_iint_cache),
 	.lbs_xattr_count = 1,
 };
 
diff --git a/security/integrity/ima/ima_appraise.c b/security/integrity/ima/ima_appraise.c
index b0b96c263961..89125efb7e06 100644
--- a/security/integrity/ima/ima_appraise.c
+++ b/security/integrity/ima/ima_appraise.c
@@ -519,7 +519,7 @@ int ima_appraise_measurement(enum ima_hooks func,
 	}
 
 	status = evm_verifyxattr(dentry, XATTR_NAME_IMA, xattr_value,
-				 rc < 0 ? 0 : rc, NULL);
+				 rc < 0 ? 0 : rc);
 	switch (status) {
 	case INTEGRITY_PASS:
 	case INTEGRITY_PASS_IMMUTABLE:
-- 
2.34.1


  parent reply	other threads:[~2023-11-20 17:41 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 17:32 [PATCH v6 00/25] security: Move IMA and EVM to the LSM infrastructure Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 01/25] ima: Align ima_inode_post_setattr() definition with " Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 02/25] ima: Align ima_file_mprotect() " Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 03/25] ima: Align ima_inode_setxattr() " Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 04/25] ima: Align ima_inode_removexattr() " Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 05/25] ima: Align ima_post_read_file() " Roberto Sassu
2023-11-20 17:32 ` [PATCH v6 06/25] evm: Align evm_inode_post_setattr() " Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 07/25] evm: Align evm_inode_setxattr() " Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 08/25] evm: Align evm_inode_post_setxattr() " Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 09/25] security: Align inode_setattr hook definition with EVM Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 10/25] security: Introduce inode_post_setattr hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 11/25] security: Introduce inode_post_removexattr hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 12/25] security: Introduce file_post_open hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 13/25] security: Introduce file_release hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 14/25] security: Introduce path_post_mknod hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 15/25] security: Introduce inode_post_create_tmpfile hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 16/25] security: Introduce inode_post_set_acl hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 17/25] security: Introduce inode_post_remove_acl hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 18/25] security: Introduce key_post_create_or_update hook Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 19/25] ima: Move to LSM infrastructure Roberto Sassu
2023-11-21  8:02   ` Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 20/25] ima: Move IMA-Appraisal " Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 21/25] evm: Move " Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 22/25] ima: Remove dependency on 'integrity' LSM Roberto Sassu
2023-11-20 17:33 ` Roberto Sassu [this message]
2023-11-20 17:33 ` [PATCH v6 24/25] integrity: Remove LSM Roberto Sassu
2023-11-20 17:33 ` [PATCH v6 25/25] security: Enforce ordering of 'ima' and 'evm' LSMs Roberto Sassu
2023-11-21  0:50   ` Casey Schaufler
2023-11-21  7:57     ` Roberto Sassu

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=20231120173318.1132868-24-roberto.sassu@huaweicloud.com \
    --to=roberto.sassu@huaweicloud.com \
    --cc=Dai.Ngo@oracle.com \
    --cc=brauner@kernel.org \
    --cc=casey@schaufler-ca.com \
    --cc=chuck.lever@oracle.com \
    --cc=dhowells@redhat.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=eparis@parisplace.org \
    --cc=jarkko@kernel.org \
    --cc=jlayton@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=kolga@netapp.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=mic@digikod.net \
    --cc=neilb@suse.de \
    --cc=paul@paul-moore.com \
    --cc=roberto.sassu@huawei.com \
    --cc=selinux@vger.kernel.org \
    --cc=serge@hallyn.com \
    --cc=stephen.smalley.work@gmail.com \
    --cc=tom@talpey.com \
    --cc=viro@zeniv.linux.org.uk \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).