Skip to contentSkip to conversation

IMA: set a default value for unknown digsig algorithms

PATCH In review THOBY Simon 3 participants 3 messages Aug 20, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 IMA: set a default value for unknown digsig algorithms

When adding new protections against writing invalid data in the security.ima xattr, I erroneously expected ima_get_hash_algo() to always return a valid 'enum hash_algo', but it turns out it trusts the user-supplied digital signatures and return it without any checks. It didn't affect process_measurement() because that function (indirectly) calls into ima_alloc_atfm() that fallback silently on the default hash algorithm, but it did affect ima_inode_setxattr as that new function didn't perform a bounds check.

Update ima_get_hash_algo() to always return a valid hash algorithm, defaulting on 'ima_hash_algo' when the user-supplied value inside the xattr is invalid.

This patch was successfully tested by syszbot, see
https://syzkaller.appspot.com/bug?extid=e8bafe7b82c739eaf153.

---
 security/integrity/ima/ima_appraise.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
security/integrity/ima/ima_appraise.c +2 -1
index 8f1eb7ef041e..dbba51583e7c 100644
@@ -185,7 +185,8 @@ enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
 	switch (xattr_value->type) {
 	case EVM_IMA_XATTR_DIGSIG:
 		sig = (typeof(sig))xattr_value;
-		if (sig->version != 2 || xattr_len <= sizeof(*sig))
+		if (sig->version != 2 || xattr_len <= sizeof(*sig)
+		    || sig->hash_algo >= HASH_ALGO__LAST)
 			return ima_hash_algo;
 		return sig->hash_algo;
 		break;
Signature · 1 line
2.31.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 13 lines · THOBY Simon

When adding new protections against writing invalid data in the security.ima xattr, I erroneously expected ima_get_hash_algo() to always return a valid 'enum hash_algo', but it turns out it trusts the user-supplied digital signatures and return it without any checks. It didn't affect process_measurement() because that function (indirectly) calls into ima_alloc_atfm() that fallback silently on the default hash algorithm, but it did affect ima_inode_setxattr as that new function didn't perform a bounds check.

Update ima_get_hash_algo() to always return a valid hash algorithm, defaulting on 'ima_hash_algo' when the user-supplied value inside the xattr is invalid.

Quoted 8 lines

This patch was successfully tested by syszbot, see
https://syzkaller.appspot.com/bug?extid=e8bafe7b82c739eaf153.

---
  security/integrity/ima/ima_appraise.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)

-lakshmi

Quoted 16 lines
security/integrity/ima/ima_appraise.c +2 -1
index 8f1eb7ef041e..dbba51583e7c 100644
@@ -185,7 +185,8 @@ enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
  	switch (xattr_value->type) {
  	case EVM_IMA_XATTR_DIGSIG:
  		sig = (typeof(sig))xattr_value;
-		if (sig->version != 2 || xattr_len <= sizeof(*sig))
+		if (sig->version != 2 || xattr_len <= sizeof(*sig)
+		    || sig->hash_algo >= HASH_ALGO__LAST)
  			return ima_hash_algo;
  		return sig->hash_algo;
  		break;
maintainer
#2 lore raw
In reply to THOBY Simon

Hi Simon,

Quoted 8 lines · THOBY Simon

When adding new protections against writing invalid data in the security.ima xattr, I erroneously expected ima_get_hash_algo() to always return a valid 'enum hash_algo', but it turns out it trusts the user-supplied digital signatures and return it without any checks. It didn't affect process_measurement() because that function (indirectly) calls into ima_alloc_atfm() that fallback silently on the default hash algorithm, but it did affect ima_inode_setxattr as that new function didn't perform a bounds check.

Please update patch description something like:

The new function validate_hash_algo() assumed that ima_get_hash_algo()
always return a valid 'enum hash_algo', but returned the user-supplied hash algorithm from the digital signature without any checks.

Quoted 7 lines

Update ima_get_hash_algo() to always return a valid hash algorithm, defaulting on 'ima_hash_algo' when the user-supplied value inside the xattr is invalid.

This patch was successfully tested by syszbot, see
https://syzkaller.appspot.com/bug?extid=e8bafe7b82c739eaf153.

Thank you for the pointer.

Quoted 3 lines
Fixes 50f742dd9147 ("IMA: block writes of the security.ima xattr with

unsupported algorithms")

thanks,

Mimi

Quoted 18 lines
---
 security/integrity/ima/ima_appraise.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
security/integrity/ima/ima_appraise.c +2 -1
index 8f1eb7ef041e..dbba51583e7c 100644
@@ -185,7 +185,8 @@ enum hash_algo ima_get_hash_algo(const struct evm_ima_xattr_data *xattr_value,
 	switch (xattr_value->type) {
 	case EVM_IMA_XATTR_DIGSIG:
 		sig = (typeof(sig))xattr_value;
-		if (sig->version != 2 || xattr_len <= sizeof(*sig))
+		if (sig->version != 2 || xattr_len <= sizeof(*sig)
+		    || sig->hash_algo >= HASH_ALGO__LAST)
 			return ima_hash_algo;
 		return sig->hash_algo;
 		break;