Skip to contentSkip to conversation

ima: add regular file data hash support for sigv3

PATCH In review Mimi Zohar 4 participants 10 messages Mar 24 – Apr 27

Subsystems (4)
lore
Share & tools
lore

Mimi Zohar

author maintainer
#0 lore raw

cover ima: add regular file data hash support for sigv3

IMA signature version 3 (sigv3) support was introduced to avoid file signature ambiguity. Instead of directly signing a raw fs-verity hash, IMA signs the hash of ima_file_id structure, containing the type of signature, the hash algorithm, and the hash.

Pure ML-DSA calculates and signs the hash directly rather than a pre-hashed digest. To avoid ML-DSA having to re-calculate the file data hash, Eric Biggers suggested signing the smaller ima_file_id structure.

This patch set adds the sigv3 support for regular file data hashes. A subsequent patch set will add the ML-DSA support.

Mimi Zohar (3):
ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures
ima: add regular file data hash signature version 3 support
ima: add support to require IMA sigv3 signatures

 Documentation/ABI/testing/ima_policy   | 10 ++--
 security/integrity/digsig.c            |  8 +--
 security/integrity/digsig_asymmetric.c | 58 +++++++++++++++++++++
 security/integrity/evm/evm_main.c      |  3 +-
 security/integrity/ima/ima.h           |  1 +
 security/integrity/ima/ima_appraise.c  | 72 ++++++++------------------
 security/integrity/ima/ima_policy.c    | 22 ++++----
 security/integrity/integrity.h         | 14 ++++-
 8 files changed, 115 insertions(+), 73 deletions(-)

--
2.53.0

author maintainer
#1 lore raw

PATCH 1/3 ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures

Define asymmetric_verify_v3() to calculate the hash of the struct ima_file_id, before calling asymmetric_verify() to verify the signature.

Move and update the existing calc_file_id_hash() function with a simpler, self contained version. In addition to the existing hash data and hash data length arguments, also pass the hash algorithm.

---
 security/integrity/digsig.c            |  8 ++--
 security/integrity/digsig_asymmetric.c | 58 ++++++++++++++++++++++++
 security/integrity/evm/evm_main.c      |  3 +-
 security/integrity/ima/ima_appraise.c  | 63 ++++++--------------------
 security/integrity/integrity.h         | 14 +++++-
 5 files changed, 90 insertions(+), 56 deletions(-)
security/integrity/digsig.c +5 -3
index 75c684cce370..1ed686154d7a 100644
@@ -59,7 +59,7 @@ static struct key *integrity_keyring_from_id(const unsigned int id)
 }
 
 int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
-			    const char *digest, int digestlen)
+			    const char *digest, int digestlen, u8 algo)
 {
 	struct key *keyring;
 
@@ -76,9 +76,11 @@ int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
 		return digsig_verify(keyring, sig + 1, siglen - 1, digest,
 				     digestlen);
 	case 2: /* regular file data hash based signature */
-	case 3: /* struct ima_file_id data based signature */
 		return asymmetric_verify(keyring, sig, siglen, digest,
-					 digestlen);
+					    digestlen);
+	case 3: /* struct ima_file_id data based signature */
+		return asymmetric_verify_v3(keyring, sig, siglen, digest,
+					    digestlen, algo);
 	}
 
 	return -EOPNOTSUPP;
security/integrity/digsig_asymmetric.c +58 -0

61 lines not on this page · raw message

security/integrity/evm/evm_main.c +2 -1
index 1b0089b4b796..b15d9d933b84 100644
@@ -266,7 +266,8 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest.digest, digest.hdr.length,
+					digest.hdr.algo);
 		if (!rc) {
 			if (xattr_data->type == EVM_XATTR_PORTABLE_DIGSIG) {
 				if (iint)
security/integrity/ima/ima_appraise.c +13 -50

95 lines not on this page · raw message

security/integrity/integrity.h +12 -2
index 4636629533af..0c581c03c5da 100644
@@ -131,7 +131,7 @@ struct modsig;
 #ifdef CONFIG_INTEGRITY_SIGNATURE
 
 int integrity_digsig_verify(const unsigned int id, const char *sig, int siglen,
-			    const char *digest, int digestlen);
+			    const char *digest, int digestlen, u8 algo);
 int integrity_modsig_verify(unsigned int id, const struct modsig *modsig);
 
 int __init integrity_init_keyring(const unsigned int id);
@@ -142,7 +142,8 @@ int __init integrity_load_cert(const unsigned int id, const char *source,
 
 static inline int integrity_digsig_verify(const unsigned int id,
 					  const char *sig, int siglen,
-					  const char *digest, int digestlen)
+					  const char *digest, int digestlen,
+					  u8 algo)
 {
 	return -EOPNOTSUPP;
 }
@@ -170,12 +171,21 @@ static inline int __init integrity_load_cert(const unsigned int id,
 #ifdef CONFIG_INTEGRITY_ASYMMETRIC_KEYS
 int asymmetric_verify(struct key *keyring, const char *sig,
 		      int siglen, const char *data, int datalen);
+int asymmetric_verify_v3(struct key *keyring, const char *sig,
+			 int siglen, const char *data, int datalen, u8 algo);
 #else
 static inline int asymmetric_verify(struct key *keyring, const char *sig,
 				    int siglen, const char *data, int datalen)
 {
 	return -EOPNOTSUPP;
 }
+
+static inline int asymmetric_verify_v3(struct key *keyring,
+				       const char *sig, int siglen,
+				       const char *data, int datalen, u8 algo)
+{
+	return -EOPNOTSUPP;
+}
 #endif
 
 #ifdef CONFIG_IMA_APPRAISE_MODSIG
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/3] ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures

Quoted 2 lines · Mimi Zohar

+ * IMA signature version 3 disambiguates the data that is signed by
+ * indirectly signing the hash of the ima_file_id structure data.

The right way to think about it is that it's the ima_file_id itself that is being signed and verified, and taking the hash of it is only a workaround for legacy algorithms that can only sign and verify hashes. With modern algorithms like Ed25519 and ML-DSA that accept arbitrary-length messages, that workaround won't be needed.

- Eric

author maintainer
#3 lore raw

Re: [PATCH 1/3] ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures

Quoted 9 lines · Eric Biggers
Quoted 2 lines · Mimi Zohar

+ * IMA signature version 3 disambiguates the data that is signed by
+ * indirectly signing the hash of the ima_file_id structure data.

The right way to think about it is that it's the ima_file_id itself that is being signed and verified, and taking the hash of it is only a workaround for legacy algorithms that can only sign and verify hashes. With modern algorithms like Ed25519 and ML-DSA that accept arbitrary-length messages, that workaround won't be needed.

I'll keep that in mind. As previously discussed, the hashes are being calculated for other purposes, like inclusion in the IMA measurement list and the audit log. Providing the potentially large, variable sized data so that the crypto signing/verifying algorithm can recalculate the hash is superfluous. Your recommendation of signing the ima_file_id works nicely.

thanks!

Mimi

author maintainer
#4 lore raw

PATCH 2/3 ima: add regular file data hash signature version 3 support

In reply to Mimi Zohar

Instead of directly verifying the signature of a file data hash, signature v3 verifies the signature of the ima_file_id structure containing the file data hash.

To disambiguate the signature usage, the ima_file_id structure also includes the hash algorithm and the type of data (e.g. regular file hash or fs-verity root hash).

---
 security/integrity/digsig_asymmetric.c | 2 +-
 security/integrity/ima/ima_appraise.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
security/integrity/digsig_asymmetric.c +1 -1
index dc5313746609..6b21b9bf829e 100644
@@ -154,7 +154,7 @@ static int calc_file_id_hash(enum evm_ima_xattr_type type,
 	size_t file_id_size;
 	int rc;
 
-	if (type != IMA_VERITY_DIGSIG)
+	if (type != IMA_VERITY_DIGSIG && type != EVM_IMA_XATTR_DIGSIG)
 		return -EINVAL;
 
 	tfm = crypto_alloc_shash(hash_algo_name[algo], 0, 0);
security/integrity/ima/ima_appraise.c +1 -1
index 5b42307ac254..8f182d808b09 100644
@@ -297,7 +297,7 @@ static int xattr_verify(enum ima_hooks func, struct ima_iint_cache *iint,
 		}
 
 		sig = (typeof(sig))xattr_value;
-		if (sig->version >= 3) {
+		if (sig->version > 3) {
 			*cause = "invalid-signature-version";
 			*status = INTEGRITY_FAIL;
 			break;
Signature · 1 line
2.53.0
author maintainer
#5 lore raw

PATCH 3/3 ima: add support to require IMA sigv3 signatures

In reply to Mimi Zohar

Defining a policy rule with the "appraise_type=imasig" option allows either v2 or v3 signatures. Defining an IMA appraise rule with the "appraise_type=sigv3" option requires a file sigv3 signature.

Define a new appraise type: IMA_SIGV3_REQUIRED

Example: appraise func=BPRM_CHECK appraise_type=sigv3

---
 Documentation/ABI/testing/ima_policy  | 10 ++++++----
 security/integrity/ima/ima.h          |  1 +
 security/integrity/ima/ima_appraise.c |  7 +++++++
 security/integrity/ima/ima_policy.c   | 22 ++++++++++------------
 4 files changed, 24 insertions(+), 16 deletions(-)
Documentation/ABI/testing/ima_policy +6 -4
index d4b3696a9efb..19258471b7b2 100644
@@ -53,10 +53,7 @@ Description:
 			    where 'imasig' is the original or the signature
 				format v2.
 			    where 'modsig' is an appended signature,
-			    where 'sigv3' is the signature format v3. (Currently
-				limited to fsverity digest based signatures
-				stored in security.ima xattr. Requires
-				specifying "digest_type=verity" first.)
+			    where 'sigv3' is the signature format v3.
 
 			appraise_flag:= [check_blacklist] (deprecated)
 			Setting the check_blacklist flag is no longer necessary.
@@ -186,6 +183,11 @@ Description:
 			appraise func=BPRM_CHECK digest_type=verity \
 				appraise_type=sigv3
 
+		Example of a regular IMA file hash 'appraise' rule requiring
+		signature version 3 format stored in security.ima xattr.
+
+			appraise func=BPRM_CHECK appraise_type=sigv3
+
 		All of these policy rules could, for example, be constrained
 		either based on a filesystem's UUID (fsuuid) or based on LSM
 		labels.
security/integrity/ima/ima.h +1 -0
index 0eea02ff04df..69e9bf0b82c6 100644
@@ -145,6 +145,7 @@ struct ima_kexec_hdr {
 #define IMA_DIGSIG_REQUIRED	0x01000000
 #define IMA_PERMIT_DIRECTIO	0x02000000
 #define IMA_NEW_FILE		0x04000000
+#define IMA_SIGV3_REQUIRED	0x08000000
 #define IMA_FAIL_UNVERIFIABLE_SIGS	0x10000000
 #define IMA_MODSIG_ALLOWED	0x20000000
 #define IMA_CHECK_BLACKLIST	0x40000000
security/integrity/ima/ima_appraise.c +7 -0
index 8f182d808b09..de963b9f3634 100644
@@ -302,6 +302,13 @@ static int xattr_verify(enum ima_hooks func, struct ima_iint_cache *iint,
 			*status = INTEGRITY_FAIL;
 			break;
 		}
+
+		if ((iint->flags & IMA_SIGV3_REQUIRED) && sig->version != 3) {
+			*cause = "IMA-sigv3-required";
+			*status = INTEGRITY_FAIL;
+			break;
+		}
+
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
 					     (const char *)xattr_value,
 					     xattr_len,
security/integrity/ima/ima_policy.c +10 -12
index bf2d7ba4c14a..f7f940a76922 100644
@@ -1298,7 +1298,8 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)
 				     IMA_GID | IMA_EGID |
 				     IMA_FGROUP | IMA_DIGSIG_REQUIRED |
 				     IMA_PERMIT_DIRECTIO | IMA_VALIDATE_ALGOS |
-				     IMA_CHECK_BLACKLIST | IMA_VERITY_REQUIRED))
+				     IMA_CHECK_BLACKLIST | IMA_VERITY_REQUIRED |
+				     IMA_SIGV3_REQUIRED))
 			return false;
 
 		break;
@@ -1833,9 +1834,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 			break;
 		case Opt_digest_type:
 			ima_log_string(ab, "digest_type", args[0].from);
-			if (entry->flags & IMA_DIGSIG_REQUIRED)
-				result = -EINVAL;
-			else if ((strcmp(args[0].from, "verity")) == 0)
+			if ((strcmp(args[0].from, "verity")) == 0)
 				entry->flags |= IMA_VERITY_REQUIRED;
 			else
 				result = -EINVAL;
@@ -1849,14 +1848,13 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				else
 					entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
 			} else if (strcmp(args[0].from, "sigv3") == 0) {
-				/* Only fsverity supports sigv3 for now */
-				if (entry->flags & IMA_VERITY_REQUIRED)
-					entry->flags |= IMA_DIGSIG_REQUIRED | IMA_CHECK_BLACKLIST;
-				else
-					result = -EINVAL;
+				entry->flags |= IMA_SIGV3_REQUIRED |
+					IMA_DIGSIG_REQUIRED |
+					IMA_CHECK_BLACKLIST;
 			} else if (IS_ENABLED(CONFIG_IMA_APPRAISE_MODSIG) &&
 				 strcmp(args[0].from, "imasig|modsig") == 0) {
-				if (entry->flags & IMA_VERITY_REQUIRED)
+				if ((entry->flags & IMA_VERITY_REQUIRED) ||
+				    (entry->flags & IMA_SIGV3_REQUIRED))
 					result = -EINVAL;
 				else
 					entry->flags |= IMA_DIGSIG_REQUIRED |
@@ -1941,7 +1939,7 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 
 	/* d-ngv2 template field recommended for unsigned fs-verity digests */
 	if (!result && entry->action == MEASURE &&
-	    entry->flags & IMA_VERITY_REQUIRED) {
+	    (entry->flags & IMA_VERITY_REQUIRED)) {
 		template_desc = entry->template ? entry->template :
 						  ima_template_desc_current();
 		check_template_field(template_desc, "d-ngv2",
@@ -2309,7 +2307,7 @@ int ima_policy_show(struct seq_file *m, void *v)
 	if (entry->template)
 		seq_printf(m, "template=%s ", entry->template->name);
 	if (entry->flags & IMA_DIGSIG_REQUIRED) {
-		if (entry->flags & IMA_VERITY_REQUIRED)
+		if (entry->flags & IMA_SIGV3_REQUIRED)
 			seq_puts(m, "appraise_type=sigv3 ");
 		else if (entry->flags & IMA_MODSIG_ALLOWED)
 			seq_puts(m, "appraise_type=imasig|modsig ");
Signature · 1 line
2.53.0
Tested-by on the v1 series
#6 lore raw
In reply to Mimi Zohar
Quoted 27 lines · Mimi Zohar

IMA signature version 3 (sigv3) support was introduced to avoid file signature ambiguity. Instead of directly signing a raw fs-verity hash, IMA signs the hash of ima_file_id structure, containing the type of signature, the hash algorithm, and the hash.

Pure ML-DSA calculates and signs the hash directly rather than a pre-hashed digest. To avoid ML-DSA having to re-calculate the file data hash, Eric Biggers suggested signing the smaller ima_file_id structure.

This patch set adds the sigv3 support for regular file data hashes. A subsequent patch set will add the ML-DSA support.

Mimi Zohar (3):
ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures
ima: add regular file data hash signature version 3 support
ima: add support to require IMA sigv3 signatures

  Documentation/ABI/testing/ima_policy   | 10 ++--
  security/integrity/digsig.c            |  8 +--
  security/integrity/digsig_asymmetric.c | 58 +++++++++++++++++++++
  security/integrity/evm/evm_main.c      |  3 +-
  security/integrity/ima/ima.h           |  1 +
  security/integrity/ima/ima_appraise.c  | 72 ++++++++------------------
  security/integrity/ima/ima_policy.c    | 22 ++++----
  security/integrity/integrity.h         | 14 ++++-
  8 files changed, 115 insertions(+), 73 deletions(-)
Quoted 3 lines

--
2.53.0

Series:

Acked-by on the v1 series
#7 lore raw
In reply to Mimi Zohar
Quoted 11 lines · Mimi Zohar

IMA signature version 3 (sigv3) support was introduced to avoid file signature ambiguity. Instead of directly signing a raw fs-verity hash, IMA signs the hash of ima_file_id structure, containing the type of signature, the hash algorithm, and the hash.

Pure ML-DSA calculates and signs the hash directly rather than a pre-hashed digest. To avoid ML-DSA having to re-calculate the file data hash, Eric Biggers suggested signing the smaller ima_file_id structure.

This patch set adds the sigv3 support for regular file data hashes. A subsequent patch set will add the ML-DSA support.

This explanation is a bit confusing, since this is actually needed regardless of ML-DSA support.

Anyway, it's still the right thing to do.

- Eric

Tested-by on the v1 series
#8 lore raw
In reply to Mimi Zohar
Quoted 27 lines · Mimi Zohar

IMA signature version 3 (sigv3) support was introduced to avoid file signature ambiguity. Instead of directly signing a raw fs-verity hash, IMA signs the hash of ima_file_id structure, containing the type of signature, the hash algorithm, and the hash.

Pure ML-DSA calculates and signs the hash directly rather than a pre-hashed digest. To avoid ML-DSA having to re-calculate the file data hash, Eric Biggers suggested signing the smaller ima_file_id structure.

This patch set adds the sigv3 support for regular file data hashes. A subsequent patch set will add the ML-DSA support.

Mimi Zohar (3):
ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures
ima: add regular file data hash signature version 3 support
ima: add support to require IMA sigv3 signatures

  Documentation/ABI/testing/ima_policy   | 10 ++--
  security/integrity/digsig.c            |  8 +--
  security/integrity/digsig_asymmetric.c | 58 +++++++++++++++++++++
  security/integrity/evm/evm_main.c      |  3 +-
  security/integrity/ima/ima.h           |  1 +
  security/integrity/ima/ima_appraise.c  | 72 ++++++++------------------
  security/integrity/ima/ima_policy.c    | 22 ++++----
  security/integrity/integrity.h         | 14 ++++-
  8 files changed, 115 insertions(+), 73 deletions(-)
Quoted 3 lines

--
2.53.0

I have tested this series along with Stefan's ML-DSA patches [1] and an additional fix [2] for ima_get_hash_algo().
With all patches applied, I am able to successfully sign files with ML-DSA-65 and verify IMA sigv3 signatures during appraisal.

[1] https://lore.kernel.org/linux-integrity/[email protected]/
[2] https://lore.kernel.org/linux-integrity/[email protected]/

author maintainer
#9 lore raw
Quoted 37 lines · Kamlesh Kumar
Quoted 27 lines · Mimi Zohar

IMA signature version 3 (sigv3) support was introduced to avoid file signature ambiguity. Instead of directly signing a raw fs-verity hash, IMA signs the hash of ima_file_id structure, containing the type of signature, the hash algorithm, and the hash.

Pure ML-DSA calculates and signs the hash directly rather than a pre-hashed digest. To avoid ML-DSA having to re-calculate the file data hash, Eric Biggers suggested signing the smaller ima_file_id structure.

This patch set adds the sigv3 support for regular file data hashes. A subsequent patch set will add the ML-DSA support.

Mimi Zohar (3):
ima: Define asymmetric_verify_v3() to verify IMA sigv3 signatures
ima: add regular file data hash signature version 3 support
ima: add support to require IMA sigv3 signatures

  Documentation/ABI/testing/ima_policy   | 10 ++--
  security/integrity/digsig.c            |  8 +--
  security/integrity/digsig_asymmetric.c | 58 +++++++++++++++++++++
  security/integrity/evm/evm_main.c      |  3 +-
  security/integrity/ima/ima.h           |  1 +
  security/integrity/ima/ima_appraise.c  | 72 ++++++++------------------
  security/integrity/ima/ima_policy.c    | 22 ++++----
  security/integrity/integrity.h         | 14 ++++-
  8 files changed, 115 insertions(+), 73 deletions(-)
Quoted 3 lines

--
2.53.0

I have tested this series along with Stefan's ML-DSA patches [1] and an additional fix [2] for ima_get_hash_algo().
With all patches applied, I am able to successfully sign files with ML-DSA-65 and verify IMA sigv3 signatures during appraisal.

Quoted 4 lines

[1] https://lore.kernel.org/linux-integrity/[email protected]/
[2] https://lore.kernel.org/linux-integrity/[email protected]/

Thanks, Kamlesh!

I'd appreciate re-testing with the v3 version now queued in next-integrity- testing
https://lore.kernel.org/linux-integrity/[email protected]
?

Mimi