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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 5/12/22 14:30, Mimi Zohar wrote:
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>

Signed-off-by: Mimi Zohar <zohar@xxxxxxxxxxxxx>
---
  README          |   3 +-
  src/evmctl.c    | 103 +++++++++++++++++++++++++++++++++++++++++-------
  src/imaevm.h    |   5 ++-
  src/libimaevm.c |  85 +++++++++++++++++++++++++++++++++++++++
  4 files changed, 179 insertions(+), 17 deletions(-)

diff --git a/README b/README
index 5b5ecb52a74b..ffe46ad75728 100644
--- a/README
+++ b/README
@@ -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)
diff --git a/src/evmctl.c b/src/evmctl.c
index ca9449498321..9152b0a5c7c2 100644
--- a/src/evmctl.c
+++ b/src/evmctl.c
@@ -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) {
+
+			/* 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.


+			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...




[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux Kernel]     [Linux Kernel Hardening]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux SCSI]

  Powered by Linux