Skip to contentSkip to conversation

initialize errno in cmd_sign_hash()

PATCH In review Mimi Zohar 3 participants 9 messages May 12, 2022 – May 17, 2022 Keys-trusted

lore
Share & tools
lore

Mimi Zohar

author maintainer
#0 lore raw

PATCH v2 1/3 initialize errno in cmd_sign_hash()

Prevent "errno: No such file or directory (2)" message.

---
 src/evmctl.c | 1 +
 1 file changed, 1 insertion(+)
src/evmctl.c +1 -0
index 8bdd34817408..ca9449498321 100644
@@ -737,6 +737,7 @@ static int cmd_sign_hash(struct command *cmd)
 	unsigned char sig[MAX_SIGNATURE_SIZE] = "\x03";
 	int siglen;
 
+	errno = 0;
 	key = imaevm_params.keyfile ? : "/etc/keys/privkey_evm.pem";
 
 	/* support reading hash (eg. output of shasum) */
Signature · 1 line
2.27.0
author maintainer
#1 lore raw

PATCH v2 2/3 Sign an fs-verity file digest

Sign fs-verity file digests provided in the format as produced by "fsverity measure". The output is of the same format as the input, but with the file signature appended. Use setfattr to write the signature as security.ima xattr.

fsverity measure format: <algo>:<hash> <pathname> output format: <algo>:<hash> <pathname> <signature>

Instead of directly signing the fsverity hash, to disambiguate the original IMA signatures from the fs-verity signatures stored in the security.ima xattr a new signature format version 3 (sigv3) was defined as the hash of the xattr type (enum evm_ima_xattr_type), the hash algorithm (enum hash_algo), and the hash.

Example:
fsverity measure <pathname> | evmctl sign_hash --veritysig \
--key <pem encoded private key>

---
 README          |   3 +-
 src/evmctl.c    | 103 +++++++++++++++++++++++++++++++++++++++++-------
 src/imaevm.h    |   5 ++-
 src/libimaevm.c |  85 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 179 insertions(+), 17 deletions(-)
README +2 -1
index 5b5ecb52a74b..ffe46ad75728 100644
@@ -34,7 +34,7 @@ COMMANDS
  ima_hash file
  ima_measurement [--ignore-violations] [--verify-sig [--key "key1, key2, ..."]]  [--pcrs [hash-algorithm,]file [--pcrs hash-algorithm,file] ...] file
  ima_fix [-t fdsxm] path
- sign_hash [--key key] [--pass password]
+ sign_hash [--veritysig] [--key key] [--pass password]
  hmac [--imahash | --imasig ] file
 
 
@@ -43,6 +43,7 @@ OPTIONS
 
   -a, --hashalgo     sha1, sha224, sha256, sha384, sha512
   -s, --imasig       make IMA signature
+      --veritysig    sign an fs-verity file digest hash
   -d, --imahash      make IMA hash
   -f, --sigfile      store IMA signature in .sig file instead of xattr
       --xattr-user   store xattrs in user namespace (for testing purposes)
src/evmctl.c +88 -15

147 lines not on this page · raw message

src/imaevm.h +4 -1
index 0d53a0232ae4..afcf1e042014 100644
@@ -93,6 +93,7 @@ enum evm_ima_xattr_type {
 	EVM_IMA_XATTR_DIGSIG,
 	IMA_XATTR_DIGEST_NG,
 	EVM_XATTR_PORTABLE_DIGSIG,
+	IMA_VERITY_DIGSIG,
 };
 
 struct h_misc {
@@ -138,7 +139,8 @@ enum digest_algo {
 
 enum digsig_version {
 	DIGSIG_VERSION_1 = 1,
-	DIGSIG_VERSION_2
+	DIGSIG_VERSION_2,
+	DIGSIG_VERSION_3	/* hash of ima_file_id struct (portion used) */
 };
 
 struct pubkey_hdr {
@@ -233,5 +235,6 @@ int ima_verify_signature(const char *file, unsigned char *sig, int siglen, unsig
 void init_public_keys(const char *keyfiles);
 int imaevm_hash_algo_from_sig(unsigned char *sig);
 const char *imaevm_hash_algo_by_id(int algo);
+int calc_hash_sigv3(enum evm_ima_xattr_type type, const char *algo, const unsigned char *in_hash, unsigned char *out_hash);
 
 #endif
src/libimaevm.c +85 -0

91 lines not on this page · raw message

Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH v2 ima-evm-utils 2/3] Sign an fs-verity file digest

Quoted 117 lines · Mimi Zohar

Sign fs-verity file digests provided in the format as produced by "fsverity measure". The output is of the same format as the input, but with the file signature appended. Use setfattr to write the signature as security.ima xattr.

fsverity measure format: <algo>:<hash> <pathname> output format: <algo>:<hash> <pathname> <signature>

Instead of directly signing the fsverity hash, to disambiguate the original IMA signatures from the fs-verity signatures stored in the security.ima xattr a new signature format version 3 (sigv3) was defined as the hash of the xattr type (enum evm_ima_xattr_type), the hash algorithm (enum hash_algo), and the hash.

Example:
fsverity measure <pathname> | evmctl sign_hash --veritysig \
--key <pem encoded private key>

---
  README          |   3 +-
  src/evmctl.c    | 103 +++++++++++++++++++++++++++++++++++++++++-------
  src/imaevm.h    |   5 ++-
  src/libimaevm.c |  85 +++++++++++++++++++++++++++++++++++++++
  4 files changed, 179 insertions(+), 17 deletions(-)
README +2 -1
index 5b5ecb52a74b..ffe46ad75728 100644
@@ -34,7 +34,7 @@ COMMANDS
   ima_hash file
   ima_measurement [--ignore-violations] [--verify-sig [--key "key1, key2, ..."]]  [--pcrs [hash-algorithm,]file [--pcrs hash-algorithm,file] ...] file
   ima_fix [-t fdsxm] path
- sign_hash [--key key] [--pass password]
+ sign_hash [--veritysig] [--key key] [--pass password]
   hmac [--imahash | --imasig ] file
 
 
@@ -43,6 +43,7 @@ OPTIONS
 
    -a, --hashalgo     sha1, sha224, sha256, sha384, sha512
    -s, --imasig       make IMA signature
+      --veritysig    sign an fs-verity file digest hash
    -d, --imahash      make IMA hash
    -f, --sigfile      store IMA signature in .sig file instead of xattr
        --xattr-user   store xattrs in user namespace (for testing purposes)
src/evmctl.c +32 -10
index ca9449498321..9152b0a5c7c2 100644
@@ -135,6 +135,7 @@ static int msize;
  static dev_t fs_dev;
  static bool evm_immutable;
  static bool evm_portable;
+static bool veritysig;
 
  #define HMAC_FLAG_NO_UUID	0x0001
  #define HMAC_FLAG_CAPS_SET	0x0002
@@ -726,34 +727,102 @@ static int cmd_sign_ima(struct command *cmd)
  	return do_cmd(cmd, sign_ima_file);
  }
 
+/*
+ * Sign file hash(es) provided in the format as produced by either
+ * sha*sum or "fsverity measure".
+ *
+ * sha*sum format: <hash> <pathname>
+ * fsverity measure format: <algo>:<hash> <pathname>
+ *
+ * To disambiguate the resulting file signatures, a new signature format
+ * version 3 (sigv3) was defined as the hash of the xattr type (enum
+ * evm_ima_xattr_type), the hash algorithm (enum hash_algo), and the hash.
+ *
+ * Either directly sign the sha*sum hash or indirectly sign the fsverity
+ * hash (sigv3).
+ *
+ * The output is the same format as the input with the resulting file
+ * signature appended.
+ */
  static int cmd_sign_hash(struct command *cmd)
  {
+	unsigned char sigv3_hash[MAX_DIGEST_SIZE];
+	unsigned char sig[MAX_SIGNATURE_SIZE];
+	unsigned char hash[MAX_DIGEST_SIZE];
+	int siglen, algolen, hashlen = 0;
+	char *line = NULL, *token, *hashp;
+	size_t line_len = 0;
  	const char *key;
-	char *token, *line = NULL;
-	int hashlen = 0;
-	size_t line_len;
+	char algo[12];
  	ssize_t len;
-	unsigned char hash[MAX_DIGEST_SIZE];
-	unsigned char sig[MAX_SIGNATURE_SIZE] = "\x03";
-	int siglen;
+	int ret;
 
  	errno = 0;
  	key = imaevm_params.keyfile ? : "/etc/keys/privkey_evm.pem";
 
-	/* support reading hash (eg. output of shasum) */
  	while ((len = getline(&line, &line_len, stdin)) > 0) {
  		/* remove end of line */
  		if (line[len - 1] == '\n')
  			line[--len] = '\0';
 
-		/* find the end of the hash */
-		token = strpbrk(line, ", \t");
-		hashlen = token ? token - line : strlen(line);
+		/*
+		 * Before either directly or indirectly signing the hash,
+		 * convert the hex-ascii hash representation to binary.
+		 */
+		if (veritysig) {
+

src/evmctl.c static int cmd_sign_ima(struct command *cmd) · line 783

Quoted 10 lines
src/evmctl.c +10 -0
+			/* split the algorithm from the hash */
+			hashp = strpbrk(line, ":");
+			if (!hashp) {	/* pointer to the delimiter */
+				log_err("Missing fsverity hash algorithm\n");
+				continue;
+			}
+
+			algolen = hashp - line;
+			if (algolen > sizeof(algo))
+				algolen = sizeof(algo);

I think a hash name exceeding the max buffer size and needing to be truncating it should be an error since the name to index conversion will fail later on.

src/evmctl.c static int cmd_sign_ima(struct command *cmd) · line 790

Quoted 7 lines
src/evmctl.c +7 -0
+			if (algolen <= 0) {
+				log_err("Missing fsverity hash algorithm\n");
+				continue;
+			}
+
+			strncpy(algo, line, algolen);
+			algo[algolen] = 0;	/* Nul terminate algorithm */

per above: if algolen == sizeof(algo) then you have an out-of-bounds access here...

author maintainer
#3 lore raw

Re: [PATCH v2 ima-evm-utils 2/3] Sign an fs-verity file digest

Quoted 15 lines · Stefan Berger
Quoted 15 lines
src/evmctl.c +12 -3
-             /* find the end of the hash */
-             token = strpbrk(line, ", \t");
-             hashlen = token ? token - line : strlen(line);
+             /*
+              * Before either directly or indirectly signing the hash,
+              * convert the hex-ascii hash representation to binary.
+              */
+             if (veritysig) {
+
+                     /* split the algorithm from the hash */
+                     hashp = strpbrk(line, ":");
+                     if (!hashp) {   /* pointer to the delimiter */
+                             log_err("Missing fsverity hash algorithm\n");
+                             continue;
+                     }
Quoted 8 lines

src/evmctl.c static int cmd_sign_ima(struct command *cmd) · line 783

Quoted 4 lines
src/evmctl.c +4 -0
+
+                     algolen = hashp - line;
+                     if (algolen > sizeof(algo))
+                             algolen = sizeof(algo);

I think a hash name exceeding the max buffer size and needing to be truncating it should be an error since the name to index conversion will fail later on.

Sure. In fact, the hash algorithm name size should be limited to the maximum size of those algorithms that fs-verity actually supports.

thanks,

Mimi

#4 lore raw

Re: [PATCH v2 ima-evm-utils 2/3] Sign an fs-verity file digest

In reply to Mimi Zohar
Quoted 2 lines · Mimi Zohar

Sign fs-verity file digests provided in the format as produced by "fsverity measure".

This is correct, but it probably would be more useful to give "fsverity digest" as the example, since "fsverity digest" doesn't depend on the file already having verity enabled.

- Eric

author maintainer
#5 lore raw

PATCH v2 3/3 Verify an fs-verity file digest based signature

In reply to Mimi Zohar

ima-evm-utils does not attempt to calculate or even read the fs-verity file hash, but can verify the fs-verity signature based on the fsverity file hash, both contained in the measurement list record.

For the time being only fs-verity supports signature format v3 (sigv3). To differentiate between the existing IMA and fs-verity file signatures, modify the verify_hash() 'sig' argument to be a pointer to the entire xattr, including the xattr type.

Example:
evmctl ima_measurement --key <DER encoded public key> \
--verify-sig /sys/kernel/security/ima/binary_runtime_measurements

---
 src/evmctl.c    | 11 +++++++++--
 src/libimaevm.c | 50 +++++++++++++++++++++++++++++++++++++------------
 2 files changed, 47 insertions(+), 14 deletions(-)
src/evmctl.c +9 -2
index 9152b0a5c7c2..593eed80f96a 100644
@@ -909,7 +909,7 @@ static int verify_evm(const char *file)
 		return mdlen;
 	assert(mdlen <= sizeof(hash));
 
-	return verify_hash(file, hash, mdlen, sig + 1, len - 1);
+	return verify_hash(file, hash, mdlen, sig, len);
 }
 
 static int cmd_verify_evm(struct command *cmd)
@@ -1572,7 +1572,8 @@ void ima_ng_show(struct template_entry *entry)
 	fieldp += field_len;
 	total_len -= field_len;
 
-	if (!strcmp(entry->name, "ima-sig")) {
+	if (!strcmp(entry->name, "ima-sig") ||
+	    !strcmp(entry->name, "ima-sigv2")) {
 		/* get signature */
 		field_len = *(uint32_t *)fieldp;
 		fieldp += sizeof(field_len);
@@ -1618,11 +1619,17 @@ void ima_ng_show(struct template_entry *entry)
 			log_info(" ");
 			log_dump(sig, sig_len);
 		}
+
+		/*
+		 * Either verify the signature against the hash contained in
+		 * the measurement list or calculate the hash.
+		 */
 		if (verify_list_sig)
 			err = ima_verify_signature(path, sig, sig_len,
 						   digest, digest_len);
 		else
 			err = ima_verify_signature(path, sig, sig_len, NULL, 0);
+
 		if (!err && imaevm_params.verbose > LOG_INFO)
 			log_info("%s: verification is OK\n", path);
 	} else {
src/libimaevm.c +38 -12

98 lines not on this page · raw message

Signature · 1 line
2.27.0
#6 lore raw

Re: [PATCH v2 ima-evm-utils 3/3] Verify an fs-verity file digest based signature

Quoted 75 lines · Mimi Zohar

ima-evm-utils does not attempt to calculate or even read the fs-verity file hash, but can verify the fs-verity signature based on the fsverity file hash, both contained in the measurement list record.

For the time being only fs-verity supports signature format v3 (sigv3). To differentiate between the existing IMA and fs-verity file signatures, modify the verify_hash() 'sig' argument to be a pointer to the entire xattr, including the xattr type.

Example:
evmctl ima_measurement --key <DER encoded public key> \
--verify-sig /sys/kernel/security/ima/binary_runtime_measurements

---
  src/evmctl.c    | 11 +++++++++--
  src/libimaevm.c | 50 +++++++++++++++++++++++++++++++++++++------------
  2 files changed, 47 insertions(+), 14 deletions(-)
src/evmctl.c +9 -2
index 9152b0a5c7c2..593eed80f96a 100644
@@ -909,7 +909,7 @@ static int verify_evm(const char *file)
  		return mdlen;
  	assert(mdlen <= sizeof(hash));
 
-	return verify_hash(file, hash, mdlen, sig + 1, len - 1);
+	return verify_hash(file, hash, mdlen, sig, len);
  }
 
  static int cmd_verify_evm(struct command *cmd)
@@ -1572,7 +1572,8 @@ void ima_ng_show(struct template_entry *entry)
  	fieldp += field_len;
  	total_len -= field_len;
 
-	if (!strcmp(entry->name, "ima-sig")) {
+	if (!strcmp(entry->name, "ima-sig") ||
+	    !strcmp(entry->name, "ima-sigv2")) {
  		/* get signature */
  		field_len = *(uint32_t *)fieldp;
  		fieldp += sizeof(field_len);
@@ -1618,11 +1619,17 @@ void ima_ng_show(struct template_entry *entry)
  			log_info(" ");
  			log_dump(sig, sig_len);
  		}
+
+		/*
+		 * Either verify the signature against the hash contained in
+		 * the measurement list or calculate the hash.
+		 */
  		if (verify_list_sig)
  			err = ima_verify_signature(path, sig, sig_len,
  						   digest, digest_len);
  		else
  			err = ima_verify_signature(path, sig, sig_len, NULL, 0);
+
  		if (!err && imaevm_params.verbose > LOG_INFO)
  			log_info("%s: verification is OK\n", path);
  	} else {
src/libimaevm.c +4 -3
index 7c2ed5fb0556..8a37551132d6 100644
@@ -534,11 +534,21 @@ int calc_hash_sigv3(enum evm_ima_xattr_type type, const char *algo,
  		return -EINVAL;
  	}
 
-	if ((hash_algo = imaevm_get_hash_algo(algo)) < 0) {
-		log_err("Hash algorithm %s not supported\n", algo);
-		return -EINVAL;
+	/*
+	 * Calculating the hash is based on the fsverity hash algorithm,
+	 * while verifying the signature is based on the hash algorithm
+	 * contained in the signature header.

src/libimaevm.c int calc_hash_sigv3(enum evm_ima_xattr_type type, const char *algo, · line 550

Quoted 10 lines
src/libimaevm.c +10 -0
+	 */
+	if (algo) {
+		if ((hash_algo = imaevm_get_hash_algo(algo)) < 0) {
+			log_err("Hash algorithm %s not supported\n", algo);
+			return -EINVAL;
+		}
+		file_id.hash_algorithm = hash_algo;
+	} else {
+		algo = imaevm_params.hash_algo;
+		file_id.hash_algorithm = imaevm_get_hash_algo(algo);

You should probably compare this also against '< 0' as it was before. If you do it outside the if-else the two cases can share this:

	if (!algo)
		algo = imaevm_params.hash_algo;

	if ((hash_algo = imaevm_get_hash_algo(algo)) < 0) {
		log_err("Hash algorithm %s not supported\n", algo);
		return -EINVAL;
	}
	file_id.hash_algorithm = hash_algo;
#7 lore raw
In reply to Mimi Zohar
Quoted 1 line · Mimi Zohar

Prevent "errno: No such file or directory (2)" message.

I guess there's an error being returned from call_command() but this errno is unrelated or confusing. Maybe errno should be initialized at the beginning of main() or beginning of call_command() to clear it before any command is run that can return an error and where the errno may be confusing the user otherwise.

Quoted 18 lines
---
  src/evmctl.c | 1 +
  1 file changed, 1 insertion(+)
src/evmctl.c +1 -0
index 8bdd34817408..ca9449498321 100644
@@ -737,6 +737,7 @@ static int cmd_sign_hash(struct command *cmd)
  	unsigned char sig[MAX_SIGNATURE_SIZE] = "\x03";
  	int siglen;
 
+	errno = 0;
  	key = imaevm_params.keyfile ? : "/etc/keys/privkey_evm.pem";
 
  	/* support reading hash (eg. output of shasum) */
author maintainer
#8 lore raw
Quoted 9 lines · Stefan Berger
Quoted 1 line · Mimi Zohar

Prevent "errno: No such file or directory (2)" message.

I guess there's an error being returned from call_command() but this errno is unrelated or confusing. Maybe errno should be initialized at the beginning of main() or beginning of call_command() to clear it before any command is run that can return an error and where the errno may be confusing the user otherwise.

'errno' is being set on failure to open/fopen and access() in file_exist().

thanks,

Mimi