Skip to contentSkip to conversation

ima_evm_utils: erroneous "verification failed: 0 (invalid padding)" message

PATCH Superseded Mimi Zohar 2 participants 7 messages Jul 18, 2019 – Jul 24, 2019 Keys-trusted

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH v3 2/2 ima-evm-utils: log unknown keyid's as errors

In reply to Mimi Zohar

Each tima a new unknown key is encountered, emit a message of the format "key #: <keyid> unknown". The individual files using unknown keys are then only logged in verbose mode.

---
 src/libimaevm.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
src/libimaevm.c +17 -4
index 43eb4ef2412c..d2194a6ca0f8 100644
@@ -402,13 +402,26 @@ static struct public_key_entry *public_keys = NULL;
 
 static EVP_PKEY *find_keyid(uint32_t keyid)
 {
-	struct public_key_entry *entry;
+	struct public_key_entry *entry, *tail = public_keys;
+	int i = 1;
 
 	for (entry = public_keys; entry != NULL; entry = entry->next) {
 		if (entry->keyid == keyid)
 			return entry->key;
+		i++;
+		tail = entry;
 	}
-	return NULL;
+
+	/* add unknown keys to list */
+	entry = calloc(1, sizeof(struct public_key_entry));
+	if (!entry) {
+		perror("calloc");
+		return 0;
+	}
+	entry->keyid = keyid;
+	tail->next = entry;
+	log_err("key %d: %x unknown\n", i,  __be32_to_cpup(&keyid));
+	return 0;
 }
 
 void init_public_keys(const char *keyfiles)
@@ -470,8 +483,8 @@ static int verify_hash_v2(const char *file, const unsigned char *hash, int size,
 
 	pkey = find_keyid(keyid);
 	if (!pkey) {
-		log_err("%s: unknown keyid: %x\n",
-			file, __be32_to_cpup(&keyid));
+		log_info("%s: unknown keyid: %x\n",
+			 file, __be32_to_cpup(&keyid));
 		return -1;
 	}
 
Signature · 1 line
2.7.5

Mimi Zohar

author maintainer
#0 lore raw

PATCH v3 1/2 ima_evm_utils: erroneous "verification failed: 0 (invalid padding)" message

When public keys are specified on the boot command line (--key <public key file>, [<public key file>, ...]), the appropriate public key is used to verify EVM or file signatures. If no keys are specified, the default x509_evm.der or x509_evm.pem file is used to verify the DIGSIG_VERSION_2 or DIGSIG_VERSION_1 signatures respectively, without first checking the keyids. Instead of emitting a "verification failed: 0 (invalid padding)" message, an "unknown keyid" message would be clearer.

To address this problem, when no public keys are specified, this patch loads the x509_evm.der default public key onto the "public_keys" list, while the x509_evm.pem continues to be passed to verify_hash_v1(). As a result of this change, the verify_hash_v2() "key" parameter is unnecssary and is removed.

---
 src/evmctl.c    | 14 +++++++++++---
 src/libimaevm.c | 56 ++++++++++++++++++++++----------------------------------
 2 files changed, 33 insertions(+), 37 deletions(-)
src/evmctl.c +11 -3
index 61808d276419..9e0926f10404 100644
@@ -838,6 +838,11 @@ static int cmd_verify_evm(struct command *cmd)
 		return -1;
 	}
 
+	if (params.keyfile)	/* Support multiple public keys */
+		init_public_keys(params.keyfile);
+	else			/* assume read pubkey from x509 cert */
+		init_public_keys("/etc/keys/x509_evm.der");
+
 	err = verify_evm(file);
 	if (!err && params.verbose >= LOG_INFO)
 		log_info("%s: verification is OK\n", file);
@@ -879,8 +884,10 @@ static int cmd_verify_ima(struct command *cmd)
 	char *file = g_argv[optind++];
 	int err;
 
-	if (params.keyfile)
+	if (params.keyfile)	/* Support multiple public keys */
 		init_public_keys(params.keyfile);
+	else			/* assume read pubkey from x509 cert */
+		init_public_keys("/etc/keys/x509_evm.der");
 
 	errno = 0;
 	if (!file) {
@@ -1602,9 +1609,10 @@ static int ima_measurement(const char *file)
 		return -1;
 	}
 
-	/* Support multiple public keys */
-	if (params.keyfile)
+	if (params.keyfile)	/* Support multiple public keys */
 		init_public_keys(params.keyfile);
+	else			/* assume read pubkey from x509 cert */
+		init_public_keys("/etc/keys/x509_evm.der");
 
 	while (fread(&entry.header, sizeof(entry.header), 1, fp)) {
 		ima_extend_pcr(pcr[entry.header.pcr], entry.header.digest,
src/libimaevm.c +22 -34

96 lines not on this page · raw message

Signature · 1 line
2.7.5
#2 lore raw

Re: [PATCH v3 2/2] ima-evm-utils: log unknown keyid's as errors

In reply to Mimi Zohar

Mimi,

Quoted 14 lines · Mimi Zohar

Each tima a new unknown key is encountered, emit a message of the format "key #: <keyid> unknown". The individual files using unknown keys are then only logged in verbose mode.

---
 src/libimaevm.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
src/libimaevm.c +0 -0
index 43eb4ef2412c..d2194a6ca0f8 100644

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 405

Quoted 5 lines
src/libimaevm.c +1 -1
@@ -402,13 +402,26 @@ static struct public_key_entry *public_keys = NULL;
 
 static EVP_PKEY *find_keyid(uint32_t keyid)
 {
-	struct public_key_entry *entry;
+	struct public_key_entry *entry, *tail = public_keys;

If user specified in -k filename that does not exist no key is added into public_keys and it remains NULL.

Quoted 8 lines
src/libimaevm.c +3 -0
+	int i = 1;
 
 	for (entry = public_keys; entry != NULL; entry = entry->next) {
 		if (entry->keyid == keyid)
 			return entry->key;
+		i++;
+		tail = entry;
 	}

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 422

Quoted 10 lines
src/libimaevm.c +9 -1
-	return NULL;
+
+	/* add unknown keys to list */
+	entry = calloc(1, sizeof(struct public_key_entry));
+	if (!entry) {
+		perror("calloc");
+		return 0;
+	}
+	entry->keyid = keyid;
+	tail->next = entry;

In that case here is SIGSEGV when user try to ima_verify.

Quoted 16 lines
src/libimaevm.c +4 -2
+	log_err("key %d: %x unknown\n", i,  __be32_to_cpup(&keyid));
+	return 0;
 }
 
 void init_public_keys(const char *keyfiles)
@@ -470,8 +483,8 @@ static int verify_hash_v2(const char *file, const unsigned char *hash, int size,
 
 	pkey = find_keyid(keyid);
 	if (!pkey) {
-		log_err("%s: unknown keyid: %x\n",
-			file, __be32_to_cpup(&keyid));
+		log_info("%s: unknown keyid: %x\n",
+			 file, __be32_to_cpup(&keyid));
 		return -1;
 	}
 
author maintainer
#3 lore raw

Re: [PATCH v3 2/2] ima-evm-utils: log unknown keyid's as errors

Quoted 18 lines · Vitaly Chikunov

Mimi,

Quoted 15 lines · Mimi Zohar

Each tima a new unknown key is encountered, emit a message of the format "key #: <keyid> unknown". The individual files using unknown keys are then only logged in verbose mode.

---
 src/libimaevm.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
src/libimaevm.c +0 -0
index 43eb4ef2412c..d2194a6ca0f8 100644
@@ -402,13 +402,26 @@ static struct public_key_entry *public_keys = NULL;
 
Quoted 7 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 405

Quoted 4 lines
src/libimaevm.c +1 -1
 static EVP_PKEY *find_keyid(uint32_t keyid)
 {
-	struct public_key_entry *entry;
+	struct public_key_entry *entry, *tail = public_keys;

If user specified in -k filename that does not exist no key is added into public_keys and it remains NULL.

Right, just the keyid is added and an error message is emitted.

Quoted 13 lines
Quoted 13 lines
src/libimaevm.c +6 -1
+	int i = 1;
 
 	for (entry = public_keys; entry != NULL; entry = entry->next) {
 		if (entry->keyid == keyid)
 			return entry->key;
+		i++;
+		tail = entry;
 	}
-	return NULL;
+
+	/* add unknown keys to list */
+	entry = calloc(1, sizeof(struct public_key_entry));
Quoted 8 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 422

Quoted 6 lines
src/libimaevm.c +6 -0
+	if (!entry) {
+		perror("calloc");
+		return 0;
+	}
+	entry->keyid = keyid;
+	tail->next = entry;

In that case here is SIGSEGV when user try to ima_verify.

find_keyid() returns NULL, which is checked before being used. There's only one caller of this function.

Do you have a test case to reproduce this bug?

Mimi

Quoted 18 lines
Quoted 17 lines
src/libimaevm.c +4 -2
+	log_err("key %d: %x unknown\n", i,  __be32_to_cpup(&keyid));
+	return 0;
 }
 
 void init_public_keys(const char *keyfiles)
@@ -470,8 +483,8 @@ static int verify_hash_v2(const char *file, const unsigned char *hash, int size,
 
 	pkey = find_keyid(keyid);
 	if (!pkey) {
-		log_err("%s: unknown keyid: %x\n",
-			file, __be32_to_cpup(&keyid));
+		log_info("%s: unknown keyid: %x\n",
+			 file, __be32_to_cpup(&keyid));
 		return -1;
 	}
 
#4 lore raw

Re: [PATCH v3 2/2] ima-evm-utils: log unknown keyid's as errors

Mimi,

Quoted 49 lines · Mimi Zohar
Quoted 16 lines · Vitaly Chikunov
Quoted 15 lines · Mimi Zohar

Each tima a new unknown key is encountered, emit a message of the format "key #: <keyid> unknown". The individual files using unknown keys are then only logged in verbose mode.

---
 src/libimaevm.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
src/libimaevm.c +0 -0
index 43eb4ef2412c..d2194a6ca0f8 100644
@@ -402,13 +402,26 @@ static struct public_key_entry *public_keys = NULL;
 
Quoted 7 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 405

Quoted 4 lines
src/libimaevm.c +1 -1
 static EVP_PKEY *find_keyid(uint32_t keyid)
 {
-	struct public_key_entry *entry;
+	struct public_key_entry *entry, *tail = public_keys;

If user specified in -k filename that does not exist no key is added into public_keys and it remains NULL.

Right, just the keyid is added and an error message is emitted.

Quoted 13 lines
Quoted 13 lines
src/libimaevm.c +6 -1
+	int i = 1;
 
 	for (entry = public_keys; entry != NULL; entry = entry->next) {
 		if (entry->keyid == keyid)
 			return entry->key;
+		i++;
+		tail = entry;
 	}
-	return NULL;
+
+	/* add unknown keys to list */
+	entry = calloc(1, sizeof(struct public_key_entry));
Quoted 8 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 422

Quoted 6 lines
src/libimaevm.c +6 -0
+	if (!entry) {
+		perror("calloc");
+		return 0;
+	}
+	entry->keyid = keyid;
+	tail->next = entry;

In that case here is SIGSEGV when user try to ima_verify.

Quoted 2 lines

find_keyid() returns NULL, which is checked before being used. There's only one caller of this function.

find_keyid does not return since tail is NULL, thus there is null dereference in tail->next.

Quoted 1 line

Do you have a test case to reproduce this bug?

  ima-evm-utils/tests ((31b5f50...))$ rm sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ touch sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_sign -k test-rsa1024.key -a sha1 --xattr-user sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_verify -k /absent --xattr-user sha1.txt
  Failed to open keyfile: /absent
  Segmentation fault

31b5f50 is head of your git next branch.

I temporary fixed like this:

- tail->next = entry;
+ if (tail)
+ tail->next = entry;
+ else
+ public_keys = entry;

author maintainer
#5 lore raw

Re: [PATCH v3 2/2] ima-evm-utils: log unknown keyid's as errors

Quoted 71 lines · Vitaly Chikunov

Mimi,

Quoted 49 lines · Mimi Zohar
Quoted 16 lines · Vitaly Chikunov
Quoted 15 lines · Mimi Zohar

Each tima a new unknown key is encountered, emit a message of the format "key #: <keyid> unknown". The individual files using unknown keys are then only logged in verbose mode.

---
 src/libimaevm.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
src/libimaevm.c +0 -0
index 43eb4ef2412c..d2194a6ca0f8 100644
@@ -402,13 +402,26 @@ static struct public_key_entry *public_keys = NULL;
 
Quoted 7 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 405

Quoted 4 lines
src/libimaevm.c +1 -1
 static EVP_PKEY *find_keyid(uint32_t keyid)
 {
-	struct public_key_entry *entry;
+	struct public_key_entry *entry, *tail = public_keys;

If user specified in -k filename that does not exist no key is added into public_keys and it remains NULL.

Right, just the keyid is added and an error message is emitted.

Quoted 13 lines
Quoted 13 lines
src/libimaevm.c +6 -1
+	int i = 1;
 
 	for (entry = public_keys; entry != NULL; entry = entry->next) {
 		if (entry->keyid == keyid)
 			return entry->key;
+		i++;
+		tail = entry;
 	}
-	return NULL;
+
+	/* add unknown keys to list */
+	entry = calloc(1, sizeof(struct public_key_entry));
Quoted 8 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 422

Quoted 6 lines
src/libimaevm.c +6 -0
+	if (!entry) {
+		perror("calloc");
+		return 0;
+	}
+	entry->keyid = keyid;
+	tail->next = entry;

In that case here is SIGSEGV when user try to ima_verify.

Quoted 2 lines

find_keyid() returns NULL, which is checked before being used. There's only one caller of this function.

find_keyid does not return since tail is NULL, thus there is null dereference in tail->next.

Quoted 1 line

Do you have a test case to reproduce this bug?

  ima-evm-utils/tests ((31b5f50...))$ rm sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ touch sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_sign -k test-rsa1024.key -a sha1 --xattr-user sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_verify -k /absent --xattr-user sha1.txt
  Failed to open keyfile: /absent
  Segmentation fault

31b5f50 is head of your git next branch.

I temporary fixed like this:

Review

Quoted 5 lines
(quoted diff) +4 -1
-       tail->next = entry;
+       if (tail)
+               tail->next = entry;
+       else
+               public_keys = entry;

I see. If you don't object, I'll fold this into the original patch.

Mimi

#6 lore raw

Re: [PATCH v3 2/2] ima-evm-utils: log unknown keyid's as errors

Mimi,

Quoted 24 lines · Mimi Zohar
Quoted 23 lines · Vitaly Chikunov
Quoted 6 lines
Quoted 3 lines

src/libimaevm.c static struct public_key_entry *public_keys = NULL; · line 422

Quoted 1 line
src/libimaevm.c +1 -0
+	tail->next = entry;

In that case here is SIGSEGV when user try to ima_verify.

find_keyid() returns NULL, which is checked before being used. There's only one caller of this function.

find_keyid does not return since tail is NULL, thus there is null dereference in tail->next.

Quoted 1 line

Do you have a test case to reproduce this bug?

  ima-evm-utils/tests ((31b5f50...))$ rm sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ touch sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_sign -k test-rsa1024.key -a sha1 --xattr-user sha1.txt
  ima-evm-utils/tests ((31b5f50...))$ evmctl ima_verify -k /absent --xattr-user sha1.txt
  Failed to open keyfile: /absent
  Segmentation fault

31b5f50 is head of your git next branch.

I temporary fixed like this:

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +4 -1
-       tail->next = entry;
+       if (tail)
+               tail->next = entry;
+       else
+               public_keys = entry;

I see. If you don't object, I'll fold this into the original patch.

Of course.

Thanks,