Skip to contentSkip to conversation

ima/evm: Ensure digest to verify is in linear mapping area

PATCH In review Roberto Sassu 3 participants 12 messages Dec 1, 2022 – Dec 8, 2022

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

v2 cover ima/evm: Ensure digest to verify is in linear mapping area

From: Roberto Sassu <[email protected]>

As sg_set_buf() requires the buffer for a crypto operation to be in the linear mapping area, so that it is always in adjacent pages, ensure that this requirement is met for IMA/EVM.

Currently, evm_verify_hmac() and xattr_verify() put the evm_digest and ima_max_digest_data structures in the stack. As normally the stack is in the linear mapping area, passing them to sg_set_buf() would not be a problem.

However, if CONFIG_VMAP_STACK is enabled, these structures will reside in the vmalloc area instead. If CONFIG_DEBUG_SG is enabled, the kernel will panic:

[  467.077359] kernel BUG at include/linux/scatterlist.h:163!
[  467.077939] invalid opcode: 0000 [#1] PREEMPT SMP NOPTI

[...]

[  467.095225] Call Trace:
[  467.096088]  <TASK>
[  467.096928]  ? rcu_read_lock_held_common+0xe/0x50
[  467.097569]  ? rcu_read_lock_sched_held+0x13/0x70
[  467.098123]  ? trace_hardirqs_on+0x2c/0xd0
[  467.098647]  ? public_key_verify_signature+0x470/0x470
[  467.099237]  asymmetric_verify+0x14c/0x300
[  467.099869]  evm_verify_hmac+0x245/0x360
[  467.100391]  evm_inode_setattr+0x43/0x190

To overcome this problem, dynamically allocate the structures with kmalloc() if CONFIG_VMAP_STACK is enabled, so that they are placed in the linear mapping area, and use them instead of the in-stack counterparts.

A test report is available here:

https://github.com/robertosassu/ima-evm-utils/actions/runs/3590837109/jobs/6045608579

which contains the following test (include tests for EVM portable signatures and IMA verity signatures):

https://github.com/robertosassu/ima-evm-utils/commit/41cf11d299e9fc2d13a60dce4b275c2675d9cc23

Changelog:

v1:
- Dynamically allocate the data structures in IMA and EVM, when necessary, instead of always making a copy in asymmetric_verify() (suggested by Mimi)

Roberto Sassu (2):
evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y
ima: Alloc ima_max_digest_data in xattr_verify() if
CONFIG_VMAP_STACK=y

 security/integrity/evm/evm_main.c     | 26 +++++++++++++++++++++-----
 security/integrity/ima/ima_appraise.c | 19 ++++++++++++++++---
 2 files changed, 37 insertions(+), 8 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/2 evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +21 -5
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);
 out:
 	if (iint)
 		iint->evm_status = evm_status;
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 64 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 10 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

- Eric

maintainer
#3 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack cbounterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Enabling CONFIG_VMAP_STACK is the culprit, which triggers the BUG_ON only when CONFIG_DEBUG_SG is enabled as well.

Refer to commit ba14a194a434 ("fork: Add generic vmalloced stack support").

Signature · 3 lines
thanks,

Mimi
#4 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 80 lines · Mimi Zohar
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack cbounterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Quoted 5 lines

Enabling CONFIG_VMAP_STACK is the culprit, which triggers the BUG_ON only when CONFIG_DEBUG_SG is enabled as well.

Refer to commit ba14a194a434 ("fork: Add generic vmalloced stack support").

I'm asking about where the actual bug is. Where is a scatterlist being created to represent an on-disk buffer...

- Eric

#5 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

In reply to Eric Biggers
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Hi Eric

it is in public_key_verify_signature(), called by asymmetric_verify() and integrity_digsig_verify().

Roberto

#6 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 80 lines · Roberto Sassu
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Quoted 5 lines

Hi Eric

it is in public_key_verify_signature(), called by asymmetric_verify() and integrity_digsig_verify().

Hmm, that's several steps down the stack then. And not something I had expected.

Perhaps this should be fixed in public_key_verify_signature() instead? It already does a kmalloc(), so that allocation size just could be made a bit larger to get space for a temporary copy of 's' and 'digest'.

Or at the very least, struct public_key_signature should have a *very* clear comment saying that the 's' and 'digest' fields must be located in physically contiguous memory...

- Eric

#7 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 87 lines · Eric Biggers
Quoted 80 lines · Roberto Sassu
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Quoted 5 lines

Hi Eric

it is in public_key_verify_signature(), called by asymmetric_verify() and integrity_digsig_verify().

Quoted 6 lines

Hmm, that's several steps down the stack then. And not something I had expected.

Perhaps this should be fixed in public_key_verify_signature() instead? It already does a kmalloc(), so that allocation size just could be made a bit larger to get space for a temporary copy of 's' and 'digest'.

Mimi asked to fix it in both IMA and EVM.

Quoted 3 lines

Or at the very least, struct public_key_signature should have a *very* clear comment saying that the 's' and 'digest' fields must be located in physically contiguous memory...

That I could add as an additional patch.

Thanks

Roberto

maintainer
#8 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 88 lines · Roberto Sassu
Quoted 87 lines · Eric Biggers
Quoted 85 lines · Roberto Sassu
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Hi Eric

it is in public_key_verify_signature(), called by asymmetric_verify() and integrity_digsig_verify().

Quoted 8 lines
Quoted 6 lines

Hmm, that's several steps down the stack then. And not something I had expected.

Perhaps this should be fixed in public_key_verify_signature() instead? It already does a kmalloc(), so that allocation size just could be made a bit larger to get space for a temporary copy of 's' and 'digest'.

Mimi asked to fix it in both IMA and EVM.

At the time I thought the problem was limited to
integrity_digsig_verify() and just to the digest.

I'll leave it up to you and Eric to decide what is the preferable solution.

Quoted 6 lines
Quoted 4 lines

Or at the very least, struct public_key_signature should have a *very* clear comment saying that the 's' and 'digest' fields must be located in physically contiguous memory...

That I could add as an additional patch.

Thanks, the new patch containing the comment looks fine.

Signature · 3 lines
thanks,

Mimi
#9 lore raw

Re: [PATCH v2 1/2] evm: Alloc evm_digest in evm_verify_hmac() if CONFIG_VMAP_STACK=y

Quoted 96 lines · Mimi Zohar
Quoted 95 lines · Roberto Sassu
Quoted 93 lines · Eric Biggers
Quoted 80 lines · Roberto Sassu
Quoted 64 lines · Eric Biggers
Quoted 63 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Commit ac4e97abce9b8 ("scatterlist: sg_set_buf() argument must be in linear mapping") checks that both the signature and the digest reside in the linear mapping area.

However, more recently commit ba14a194a434c ("fork: Add generic vmalloced stack support"), made it possible to move the stack in the vmalloc area, which is not contiguous, and thus not suitable for sg_set_buf() which needs adjacent pages.

Fix this by checking if CONFIG_VMAP_STACK is enabled. If yes, allocate an evm_digest structure, and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/evm/evm_main.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)
security/integrity/evm/evm_main.c +16 -3
index 23d484e05e6f..7f76d6103f2e 100644
@@ -174,6 +174,7 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 	struct signature_v2_hdr *hdr;
 	enum integrity_status evm_status = INTEGRITY_PASS;
 	struct evm_digest digest;
+	struct evm_digest *digest_ptr = &digest;
 	struct inode *inode;
 	int rc, xattr_len, evm_immutable = 0;
 
@@ -231,14 +232,26 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		}
 
 		hdr = (struct signature_v2_hdr *)xattr_data;
-		digest.hdr.algo = hdr->hash_algo;
+
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			digest_ptr = kmalloc(sizeof(*digest_ptr), GFP_NOFS);
+			if (!digest_ptr) {
+				rc = -ENOMEM;
+				break;
+			}
+		}
+
+		digest_ptr->hdr.algo = hdr->hash_algo;
+
 		rc = evm_calc_hash(dentry, xattr_name, xattr_value,
-				   xattr_value_len, xattr_data->type, &digest);
+				   xattr_value_len, xattr_data->type,
+				   digest_ptr);
 		if (rc)
 			break;
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_EVM,
 					(const char *)xattr_data, xattr_len,
-					digest.digest, digest.hdr.length);
+					digest_ptr->digest,
+					digest_ptr->hdr.length);
 		if (!rc) {
 			inode = d_backing_inode(dentry);
 
Quoted 14 lines

security/integrity/evm/evm_main.c static enum integrity_status evm_verify_hmac(struct dentry *dentry, · line 288

Quoted 11 lines
security/integrity/evm/evm_main.c +5 -2
@@ -268,8 +281,11 @@ static enum integrity_status evm_verify_hmac(struct dentry *dentry,
 		else
 			evm_status = INTEGRITY_FAIL;
 	}
-	pr_debug("digest: (%d) [%*phN]\n", digest.hdr.length, digest.hdr.length,
-		  digest.digest);
+	pr_debug("digest: (%d) [%*phN]\n", digest_ptr->hdr.length,
+		 digest_ptr->hdr.length, digest_ptr->digest);
+
+	if (digest_ptr && digest_ptr != &digest)
+		kfree(digest_ptr);

What is the actual problem here? Where is a scatterlist being created from this buffer? AFAICS it never happens.

Quoted 5 lines

Hi Eric

it is in public_key_verify_signature(), called by asymmetric_verify() and integrity_digsig_verify().

Hmm, that's several steps down the stack then. And not something I had expected.

Perhaps this should be fixed in public_key_verify_signature() instead? It already does a kmalloc(), so that allocation size just could be made a bit larger to get space for a temporary copy of 's' and 'digest'.

Quoted 7 lines
Quoted 1 line

Mimi asked to fix it in both IMA and EVM.

At the time I thought the problem was limited to
integrity_digsig_verify() and just to the digest.

I'll leave it up to you and Eric to decide what is the preferable solution.

Ok, yes. I think Eric's suggestion of making a copy in public_key_verify_signature() is better. Will do it.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Or at the very least, struct public_key_signature should have a *very* clear comment saying that the 's' and 'digest' fields must be located in physically contiguous memory...

That I could add as an additional patch.

Thanks, the new patch containing the comment looks fine.

Thanks, not sure if I need to keep it with the new patch (probably not).

Roberto

#10 lore raw

PATCH v2 2/2 ima: Alloc ima_max_digest_data in xattr_verify() if CONFIG_VMAP_STACK=y

In reply to Roberto Sassu

From: Roberto Sassu <[email protected]>

Similarly to evm_verify_hmac(), which allocates an evm_digest structure to satisfy the linear mapping requirement if CONFIG_VMAP_STACK is enabled, do the same in xattr_verify(). Allocate an ima_max_digest_data structure and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")
---
 security/integrity/ima/ima_appraise.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)
security/integrity/ima/ima_appraise.c +16 -3
index 3e0fbbd99534..ed8f05340fe8 100644
@@ -278,6 +278,7 @@ static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
 			enum integrity_status *status, const char **cause)
 {
 	struct ima_max_digest_data hash;
+	struct ima_max_digest_data *hash_ptr = &hash;
 	struct signature_v2_hdr *sig;
 	int rc = -EINVAL, hash_start = 0;
 	int mask;
@@ -376,8 +377,17 @@ static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
 			break;
 		}
 
+		if (IS_ENABLED(CONFIG_VMAP_STACK)) {
+			hash_ptr = kmalloc(sizeof(*hash_ptr), GFP_KERNEL);
+			if (!hash_ptr) {
+				*cause = "out-of-memory";
+				*status = INTEGRITY_FAIL;
+				break;
+			}
+		}
+
 		rc = calc_file_id_hash(IMA_VERITY_DIGSIG, iint->ima_hash->algo,
-				       iint->ima_hash->digest, &hash.hdr);
+				       iint->ima_hash->digest, &hash_ptr->hdr);
 		if (rc) {
 			*cause = "sigv3-hashing-error";
 			*status = INTEGRITY_FAIL;
@@ -386,8 +396,8 @@ static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
 
 		rc = integrity_digsig_verify(INTEGRITY_KEYRING_IMA,
 					     (const char *)xattr_value,
-					     xattr_len, hash.digest,
-					     hash.hdr.length);
+					     xattr_len, hash_ptr->digest,
+					     hash_ptr->hdr.length);
 		if (rc) {
 			*cause = "invalid-verity-signature";
 			*status = INTEGRITY_FAIL;
@@ -402,6 +412,9 @@ static int xattr_verify(enum ima_hooks func, struct integrity_iint_cache *iint,
 		break;
 	}
 
+	if (hash_ptr && hash_ptr != &hash)
+		kfree(hash_ptr);
+
 	return rc;
 }
 
Signature · 1 line
2.25.1
#11 lore raw

Re: [PATCH v2 2/2] ima: Alloc ima_max_digest_data in xattr_verify() if CONFIG_VMAP_STACK=y

Quoted 10 lines · Roberto Sassu

From: Roberto Sassu <[email protected]>

Similarly to evm_verify_hmac(), which allocates an evm_digest structure to satisfy the linear mapping requirement if CONFIG_VMAP_STACK is enabled, do the same in xattr_verify(). Allocate an ima_max_digest_data structure and use that instead of the in-stack counterpart.

Fixes ba14a194a434 ("fork: Add generic vmalloced stack support")

Likewise, what is the actual problem here? Where specifically is a scatterlist being used to represent an on-stack buffer?

- Eric