Skip to contentSkip to conversation

Fix missing mem scrub at clear key import in cca_clr2cipherkey()

PATCH Applied Harald Freudenberger 4 participants 7 messages Jul 29 – Jul 30

Subsystems (2)
lore
Share & tools
lore

Harald Freudenberger

author maintainer
#0 lore raw

v2 cover Fix missing mem scrub at clear key import in cca_clr2cipherkey()

The helper function _ip_cprb_helper() uses internal buffer memory for building and processing CPRBs. After use this buffer was never scrubbed which could lead to leaving for example clear key material in memory which could be exposed via tricky reuse of this same memory.

Extent the _ip_cprb_helper() function with another parameter 'scrub' used to steer scrubbing of this buffer. So now the caller has the opportunity to decide if scrubbing is needed or not.

Extent the clear key to secure key token import process in function cca_clr2cipherkey() to tell the helper function from above to scrub the cprb buffer when the clear key value is part of the request data.

Add explicit scrubbing on return from function cca_clr2cipherkey() for the random EXOR buffer and the cprb buffer.

Overall this cleans the internal used buffer in case of clear key import to prevent sensitive data to get exposed.

Changelog:
v1: initial version
v2: added explicit scrubbing on return of cca_clr2cipherkey()

Harald Freudenberger (1):
s390/zcrypt: Fix missing mem scrub at clear key import in
cca_clr2cipherkey()

 drivers/s390/crypto/zcrypt_ccamisc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v2 1/1 s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

The helper function _ip_cprb_helper() uses internal buffer memory for building and processing CPRBs. After use this buffer was never scrubbed which could lead to leaving for example clear key material in memory which could be exposed via tricky reuse of this same memory.

Extent the _ip_cprb_helper() function with another parameter 'scrub' used to steer scrubbing of this buffer. So now the caller has the opportunity to decide if scrubbing is needed or not.

Extent the clear key to secure key token import process in function cca_clr2cipherkey() to tell the helper function from above to scrub the cprb buffer when the clear key value is part of the request data.

Add explicit scrubbing on return from function cca_clr2cipherkey() for the random EXOR buffer and the cprb buffer.

Overall this cleans the internal used buffer in case of clear key import to prevent sensitive data to get exposed.

---
 drivers/s390/crypto/zcrypt_ccamisc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/s390/crypto/zcrypt_ccamisc.c +13 -6
index 84936a795b95..9cf268efd4ab 100644
@@ -971,7 +971,8 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,
 			   int clr_key_bit_size,
 			   u8 *key_token,
 			   int *key_token_size,
-			   u32 xflags)
+			   u32 xflags,
+			   bool scrub)
 {
 	int rc, n;
 	u8 *mem, *ptr;
@@ -1111,7 +1112,7 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,
 	*key_token_size = t->len;
 
 out:
-	free_cprbmem(mem, PARMBSIZE, false, xflags);
+	free_cprbmem(mem, PARMBSIZE, scrub, xflags);
 	return rc;
 }
 
@@ -1162,28 +1163,32 @@ int cca_clr2cipherkey(u16 card, u16 dom, u32 keybitsize, u32 keygenflags,
 	 * 4/4 COMPLETE the secure cipher key import
 	 */
 	rc = _ip_cprb_helper(card, dom, "AES     ", "FIRST   ", "MIN3PART",
-			     exorbuf, keybitsize, token, &tokensize, xflags);
+			     exorbuf, keybitsize, token, &tokensize,
+			     xflags, true);
 	if (rc) {
 		ZCRYPT_DBF_ERR("%s clear key import 1/4 with CSNBKPI2 failed, rc=%d\n",
 			       __func__, rc);
 		goto out;
 	}
 	rc = _ip_cprb_helper(card, dom, "AES     ", "ADD-PART", NULL,
-			     clrkey, keybitsize, token, &tokensize, xflags);
+			     clrkey, keybitsize, token, &tokensize,
+			     xflags, true);
 	if (rc) {
 		ZCRYPT_DBF_ERR("%s clear key import 2/4 with CSNBKPI2 failed, rc=%d\n",
 			       __func__, rc);
 		goto out;
 	}
 	rc = _ip_cprb_helper(card, dom, "AES     ", "ADD-PART", NULL,
-			     exorbuf, keybitsize, token, &tokensize, xflags);
+			     exorbuf, keybitsize, token, &tokensize,
+			     xflags, true);
 	if (rc) {
 		ZCRYPT_DBF_ERR("%s clear key import 3/4 with CSNBKPI2 failed, rc=%d\n",
 			       __func__, rc);
 		goto out;
 	}
 	rc = _ip_cprb_helper(card, dom, "AES     ", "COMPLETE", NULL,
-			     NULL, keybitsize, token, &tokensize, xflags);
+			     NULL, keybitsize, token, &tokensize,
+			     xflags, true);
 	if (rc) {
 		ZCRYPT_DBF_ERR("%s clear key import 4/4 with CSNBKPI2 failed, rc=%d\n",
 			       __func__, rc);
@@ -1200,6 +1205,8 @@ int cca_clr2cipherkey(u16 card, u16 dom, u32 keybitsize, u32 keygenflags,
 	*keybufsize = tokensize;
 
 out:
+	memzero_explicit(exorbuf, sizeof(exorbuf));
+	memzero_explicit(mem, CPRB_MEMPOOL_ITEM_SIZE);
 	mempool_free(mem, cprb_mempool);
 	return rc;
 }
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unvalidated length field t->len from an untrusted hardware response leads to severe out-of-bounds memory accesses during memcpy(). --

--- Patch [1]: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey() --- commit 0678dd664c1b50d9a38dff946d27b4823de1e205
Author: Harald Freudenberger <[email protected]>

s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

This commit adds a parameter to _ip_cprb_helper() to scrub the CPRB buffer after processing clear key material. It also adds explicit zeroing of the buffers before freeing them in cca_clr2cipherkey() to prevent sensitive data exposure.

Quoted 4 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
index 84936a795b956..9cf268efd4abf 100644

[ ... ]

drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1112

Quoted 2 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
@@ -1111,7 +1112,7 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,
 	*key_token_size = t->len;

[Severity: High]
This is a pre-existing issue, but is it possible for a compromised or malfunctioning hardware device to return a maliciously crafted response length in t->len?

In _ip_cprb_helper(), the unvalidated t->len from the hardware response is directly used as the length for copying just before this line:

	t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token;
	memcpy(key_token, t, t->len);
	*key_token_size = t->len;

Without bounds checking against the actual buffer size, could this lead to an out-of-bounds read from the device response buffer and an out-of-bounds write into the key_token buffer?

Also, on subsequent iterations in cca_clr2cipherkey(), the poisoned *key_token_size is used to construct the next request:

	preq_key_block->tlv1.len = sizeof(preq_key_block->tlv1) + n;
	preq_key_block->tlv1.flag = 0x0030;
	memcpy(preq_key_block->tlv1.key_token, key_token, *key_token_size);

Does this cause another unbounded copy into the smaller parameter buffer during the next loop iteration?

Quoted 6 lines
drivers/s390/crypto/zcrypt_ccamisc.c +1 -1
 out:
-	free_cprbmem(mem, PARMBSIZE, false, xflags);
+	free_cprbmem(mem, PARMBSIZE, scrub, xflags);
 	return rc;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

Re: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

Quoted 57 lines · On 2026-07-29 16:12, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unvalidated length field t->len from an untrusted hardware response leads to severe out-of-bounds memory accesses during memcpy().
--

--- Patch [1]: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey() ---
commit 0678dd664c1b50d9a38dff946d27b4823de1e205
Author: Harald Freudenberger <[email protected]>

s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

This commit adds a parameter to _ip_cprb_helper() to scrub the CPRB buffer after processing clear key material. It also adds explicit zeroing of the buffers before freeing them in cca_clr2cipherkey() to prevent sensitive data exposure.

Quoted 4 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
index 84936a795b956..9cf268efd4abf 100644

[ ... ]

drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 · line 1113

Quoted 3 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
@@ -1111,7 +1112,7 @@ static int _ip_cprb_helper(u16 cardnr, u16 
 domain,
 	*key_token_size = t->len;

[Severity: High]
This is a pre-existing issue, but is it possible for a compromised or malfunctioning hardware device to return a maliciously crafted response length in t->len?

In _ip_cprb_helper(), the unvalidated t->len from the hardware response is directly used as the length for copying just before this line:

	t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token;
	memcpy(key_token, t, t->len);
	*key_token_size = t->len;

Without bounds checking against the actual buffer size, could this lead to an out-of-bounds read from the device response buffer and an out-of-bounds write into the key_token buffer?

Also, on subsequent iterations in cca_clr2cipherkey(), the poisoned *key_token_size is used to construct the next request:

	preq_key_block->tlv1.len = sizeof(preq_key_block->tlv1) + n;
	preq_key_block->tlv1.flag = 0x0030;
	memcpy(preq_key_block->tlv1.key_token, key_token, *key_token_size);

Does this cause another unbounded copy into the smaller parameter buffer during the next loop iteration?

Quoted 1 line
Quoted 5 lines

drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1117

Quoted 5 lines

out:

drivers/s390/crypto/zcrypt_ccamisc.c +1 -1
-	free_cprbmem(mem, PARMBSIZE, false, xflags);
+	free_cprbmem(mem, PARMBSIZE, scrub, xflags);
 	return rc;
 }

This is a valid finding but will be addressed with another patch series.

author maintainer
#4 lore raw

Re: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

In reply to [email protected]
Quoted 57 lines · On 2026-07-29 16:12, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unvalidated length field t->len from an untrusted hardware response leads to severe out-of-bounds memory accesses during memcpy().
--

--- Patch [1]: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey() ---
commit 0678dd664c1b50d9a38dff946d27b4823de1e205
Author: Harald Freudenberger <[email protected]>

s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

This commit adds a parameter to _ip_cprb_helper() to scrub the CPRB buffer after processing clear key material. It also adds explicit zeroing of the buffers before freeing them in cca_clr2cipherkey() to prevent sensitive data exposure.

Quoted 4 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
index 84936a795b956..9cf268efd4abf 100644

[ ... ]

drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 · line 1113

Quoted 3 lines
drivers/s390/crypto/zcrypt_ccamisc.c +0 -0
@@ -1111,7 +1112,7 @@ static int _ip_cprb_helper(u16 cardnr, u16 
 domain,
 	*key_token_size = t->len;

[Severity: High]
This is a pre-existing issue, but is it possible for a compromised or malfunctioning hardware device to return a maliciously crafted response length in t->len?

In _ip_cprb_helper(), the unvalidated t->len from the hardware response is directly used as the length for copying just before this line:

	t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token;
	memcpy(key_token, t, t->len);
	*key_token_size = t->len;

Without bounds checking against the actual buffer size, could this lead to an out-of-bounds read from the device response buffer and an out-of-bounds write into the key_token buffer?

Also, on subsequent iterations in cca_clr2cipherkey(), the poisoned *key_token_size is used to construct the next request:

	preq_key_block->tlv1.len = sizeof(preq_key_block->tlv1) + n;
	preq_key_block->tlv1.flag = 0x0030;
	memcpy(preq_key_block->tlv1.key_token, key_token, *key_token_size);

Does this cause another unbounded copy into the smaller parameter buffer during the next loop iteration?

Quoted 1 line
Quoted 5 lines

drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1117

Quoted 5 lines

out:

drivers/s390/crypto/zcrypt_ccamisc.c +1 -1
-	free_cprbmem(mem, PARMBSIZE, false, xflags);
+	free_cprbmem(mem, PARMBSIZE, scrub, xflags);
 	return rc;
 }

These issues will be addressed with another patch (series)

maintainer Reviewed-by on v2 1/1
#5 lore raw

Re: [PATCH v2 1/1] s390/zcrypt: Fix missing mem scrub at clear key import in cca_clr2cipherkey()

In reply to Harald Freudenberger
Quoted 6 lines · Harald Freudenberger

The helper function _ip_cprb_helper() uses internal buffer memory for building and processing CPRBs. After use this buffer was never scrubbed which could lead to leaving for example clear key material in memory which could be exposed via tricky reuse of this same memory.

Extent the _ip_cprb_helper() function with another parameter 'scrub'

typo: Extend

Quoted 4 lines

used to steer scrubbing of this buffer. So now the caller has the opportunity to decide if scrubbing is needed or not.

Extent the clear key to secure key token import process in function

typo: Extend

Quoted 11 lines

cca_clr2cipherkey() to tell the helper function from above to scrub the cprb buffer when the clear key value is part of the request data.

Add explicit scrubbing on return from function cca_clr2cipherkey() for the random EXOR buffer and the cprb buffer.

Overall this cleans the internal used buffer in case of clear key import to prevent sensitive data to get exposed.

Signature · 2 lines
Mit freundlichen Grüßen / Kind regards
Holger Dengler
maintainer Applied
#6 lore raw
In reply to Harald Freudenberger
Quoted 23 lines · Harald Freudenberger

The helper function _ip_cprb_helper() uses internal buffer memory for building and processing CPRBs. After use this buffer was never scrubbed which could lead to leaving for example clear key material in memory which could be exposed via tricky reuse of this same memory.

Extent the _ip_cprb_helper() function with another parameter 'scrub' used to steer scrubbing of this buffer. So now the caller has the opportunity to decide if scrubbing is needed or not.

Extent the clear key to secure key token import process in function cca_clr2cipherkey() to tell the helper function from above to scrub the cprb buffer when the clear key value is part of the request data.

Add explicit scrubbing on return from function cca_clr2cipherkey() for the random EXOR buffer and the cprb buffer.

Overall this cleans the internal used buffer in case of clear key import to prevent sensitive data to get exposed.

Changelog:
v1: initial version
v2: added explicit scrubbing on return of cca_clr2cipherkey()

Quoted 6 lines

Harald Freudenberger (1):
s390/zcrypt: Fix missing mem scrub at clear key import in
cca_clr2cipherkey()

 drivers/s390/crypto/zcrypt_ccamisc.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

Applied, thank you!