390/zcrypt: Validate length information in reply
Subsystems (2)
Holger Dengler
maintainercover 390/zcrypt: Validate length information in reply
This series contains adds validation of a length information in a hardware reply, to prevent buffer over-read/over-write in case of a broken/modified replies.
This series applies on-top of Linus' master branch.
Holger Dengler (1):
s390/zcrypt: Validate length in reply before using it
drivers/s390/crypto/zcrypt_ccamisc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
base-commit: 59e6295fac26b8e85c1ea859cdd89fa1e47519d7
Signature · 1 line
2.55.0
Holger Dengler
maintainerPATCH 1/1 s390/zcrypt: Validate length in reply before using it
The length information in the reply is used to copy the key token to the target buffer. An invalid information in t->len of the reply may cause an over-read of the target buffer and also a over-write of the target buffer. To prevent that, check t->len before using it.
As the available space in destination and source buffer is always larger than the valid length value in the parameter block in the reply, compare t->len with this (already validated) length information. As a side effect, this check also prevents buffer over-read and over-write.
--- drivers/s390/crypto/zcrypt_ccamisc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/s390/crypto/zcrypt_ccamisc.c +14 -1
@@ -1158,8 +1158,21 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,/* do not check the key here, it may be incomplete */ -/* copy the vlsc key token back */ +/* +* Copy the vlsc key token back. +* The available space in the destination (key_token) and the source +* (t) buffer is always larger as the valid range of prepparm->kb.len. +* Validate t->len ba comparing it with the length information in the +* param block of the request (prepparm->kb.len) +* The value range of prepparm->kb.len has been checked above. +*/ t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token; +if (t->len != prepparm->kb.len -3 *sizeof (uint16_t )) { +ZCRYPT_DBF_ERR ("%s reply with invalid key_token length %u\n" , + __func__, t->len); + rc = -EIO ; + goto out; + }memcpy (key_token, t, t->len); *key_token_size = t->len;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fixes an out-of-bounds read in _ip_cprb_helper() but misses identical vulnerabilities in cca_gencipherkey() and _ep11_genaeskey().
- [High] Unvalidated length fields in the CPRBX response payload are used to advance a memory pointer, causing severe out-of-bounds reads in cca_query_crypto_facility(). --
--- Patch [1]: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it --- commit 3fcaad86f04a2a9419691d5f1a54cc873f326675
Author: Holger Dengler <[email protected]>
s390/zcrypt: Validate length in reply before using it
The length information in the reply is used to copy the key token to the target buffer. An invalid information in t->len of the reply may cause an over-read of the target buffer and also a over-write of the target buffer. To prevent that, check t->len before using it.
Quoted 16 lines
drivers/s390/crypto/zcrypt_ccamisc.c +7
-1@@ -1158,8 +1158,21 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,/* do not check the key here, it may be incomplete */ -/* copy the vlsc key token back */ +/* +* Copy the vlsc key token back. +* The available space in the destination (key_token) and the source +* (t) buffer is always larger as the valid range of prepparm->kb.len. +* Validate t->len ba comparing it with the length information in the +* param block of the request (prepparm->kb.len) +* The value range of prepparm->kb.len has been checked above.
drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1177
Quoted 10 lines
drivers/s390/crypto/zcrypt_ccamisc.c +7
-0+*/ t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token; +if (t->len != prepparm->kb.len -3 *sizeof (uint16_t )) { +ZCRYPT_DBF_ERR ("%s reply with invalid key_token length %u\n" , + __func__, t->len); + rc = -EIO ; + goto out; + }memcpy (key_token, t, t->len); *key_token_size = t->len;
[Severity: High]
This isn't a bug introduced by this patch, but since you are fixing the unvalidated length field from the device reply here, should similar validation be added to cca_gencipherkey() and _ep11_genaeskey()?
In drivers/s390/crypto/zcrypt_ccamisc.c:cca_gencipherkey(), the code reads the generated token length directly from the untrusted reply and calculates the copy length without checking bounds against the structure size:
keybuflen = prepparm->kb.tlv1.len - 2 * sizeof(uint16_t);
...
if (keybuf) {
if (*keybufsize >= keybuflen)
memcpy(keybuf, t, keybuflen);Could a malicious or compromised device return an inflated length, causing memcpy() to read thousands of bytes out-of-bounds from the cprbmem allocation and potentially leak kernel memory to userspace?
A similar unvalidated copy appears in drivers/s390/crypto/zcrypt_ep11misc.c:_ep11_genaeskey(), which relies entirely on the untrusted device data length:
memcpy(keybuf, rep_pl->data, rep_pl->data_len);
[Severity: High]
This is a pre-existing issue, but does a similar vulnerability exist in drivers/s390/crypto/zcrypt_ccamisc.c:cca_query_crypto_facility() where the unvalidated length field is used to advance a memory pointer?
In that function, the code blindly advances ptr using an unchecked length provided by the device payload:
len = *((u16 *)ptr);
if (len > sizeof(u16)) {
...
ptr += len;
}If the hardware returns a corrupt or malicious length, could ptr advance far beyond the cprbmem buffer bounds, leading to an out-of-bounds dereference on the next iteration or exposing arbitrary kernel memory during the subsequent memcpy()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Holger Dengler
maintainerRe: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it
Quoted 60 lines · On 8/20/26 16:20, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fixes an out-of-bounds read in_ip_cprb_helper()but misses identical vulnerabilities incca_gencipherkey()and_ep11_genaeskey().
- [High] Unvalidated length fields in the CPRBX response payload are used to advance a memory pointer, causing severe out-of-bounds reads incca_query_crypto_facility(). ----- Patch [1]: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it --- commit
3fcaad86f04a2a9419691d5f1a54cc873f326675
Author: Holger Dengler <[email protected]>s390/zcrypt: Validate length in reply before using it
The length information in the reply is used to copy the key token to the target buffer. An invalid information in
t->lenof the reply may cause an over-read of the target buffer and also a over-write of the target buffer. To prevent that, checkt->lenbefore using it.Quoted 16 lines
drivers/s390/crypto/zcrypt_ccamisc.c +7
-1@@ -1158,8 +1158,21 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,/* do not check the key here, it may be incomplete */ -/* copy the vlsc key token back */ +/* +* Copy the vlsc key token back. +* The available space in the destination (key_token) and the source +* (t) buffer is always larger as the valid range of prepparm->kb.len. +* Validate t->len ba comparing it with the length information in the +* param block of the request (prepparm->kb.len) +* The value range of prepparm->kb.len has been checked above. drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1177
Quoted 10 lines
drivers/s390/crypto/zcrypt_ccamisc.c +7
-0+*/ t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token; +if (t->len != prepparm->kb.len -3 *sizeof (uint16_t )) { +ZCRYPT_DBF_ERR ("%s reply with invalid key_token length %u\n" , + __func__, t->len); + rc = -EIO ; + goto out; + }memcpy (key_token, t, t->len); *key_token_size = t->len;[Severity: High]
This isn't a bug introduced by this patch, but since you are fixing the unvalidated length field from the device reply here, should similar validation be added tocca_gencipherkey()and_ep11_genaeskey()?In drivers/s390/crypto/zcrypt_ccamisc.c:cca_gencipherkey(), the code reads the generated token length directly from the untrusted reply and calculates the copy length without checking bounds against the structure size:
keybuflen = prepparm->kb.tlv1.len - 2 * sizeof(uint16_t); ... if (keybuf) { if (*keybufsize >= keybuflen) memcpy(keybuf, t, keybuflen);
Quoted 3 lines
Could a malicious or compromised device return an inflated length, causing
memcpy()to read thousands of bytes out-of-bounds from the cprbmem allocation and potentially leak kernel memory to userspace?
The memcpy is only called, if keylen is less equal *keysize (which is the available space in keybuf). As far as I can see, this prevents any over-writes of the keybuf buffer, even with a malicious response from the hardware.
This objection is not relevant.
Quoted 5 lines
A similar unvalidated copy appears in drivers/s390/crypto/zcrypt_ep11misc.c:_ep11_genaeskey(), which relies entirely on the untrusted device data length:
memcpy(keybuf, rep_pl->data, rep_pl->data_len);
In _ep11_genaeskey(), the rep_pl->data_len must be less equal *keybufsize, otherwise the function returns with an error.
This objection is false.
Quoted 14 lines
[Severity: High]
This is a pre-existing issue, but does a similar vulnerability exist in drivers/s390/crypto/zcrypt_ccamisc.c:cca_query_crypto_facility() where the unvalidated length field is used to advance a memory pointer?In that function, the code blindly advances ptr using an unchecked length provided by the device payload:
len = *((u16 *)ptr); if (len > sizeof(u16)) { ... ptr += len; }
Quoted 4 lines
If the hardware returns a corrupt or malicious length, could ptr advance far beyond the cprbmem buffer bounds, leading to an out-of-bounds dereference on the next iteration or exposing arbitrary kernel memory during the subsequent
memcpy()?
This objection is under investigation and will be fixed (if required) in another series.
Signature · 2 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler
Re: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it
In reply to Holger DenglerQuoted 32 lines · Holger Dengler
The length information in the reply is used to copy the key token to the target buffer. An invalid information in
t->lenof the reply may cause an over-read of the target buffer and also a over-write of the target buffer. To prevent that, checkt->lenbefore using it.As the available space in destination and source buffer is always larger than the valid length value in the parameter block in the reply, compare
t->lenwith this (already validated) length information. As a side effect, this check also prevents buffer over-read and over-write.Signed-off-by Holger Dengler <[email protected]>--- drivers/s390/crypto/zcrypt_ccamisc.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)drivers/s390/crypto/zcrypt_ccamisc.c +4
-1@@ -1158,8 +1158,21 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain,/* do not check the key here, it may be incomplete */ -/* copy the vlsc key token back */ +/* +* Copy the vlsc key token back. +* The available space in the destination (key_token) and the source +* (t) buffer is always larger as the valid range of
prepparm->kb.len.
drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1165
Quoted 1 line
drivers/s390/crypto/zcrypt_ccamisc.c +1
-0+* Validate t->len ba comparing it with the length information in the
what is this funny "ba" in the previous sentence ?
drivers/s390/crypto/zcrypt_ccamisc.c static int _ip_cprb_helper(u16 cardnr, u16 domain, · line 1177
Quoted 12 lines
drivers/s390/crypto/zcrypt_ccamisc.c +9
-0+* param block of the request (prepparm->kb.len) +* The value range of prepparm->kb.len has been checked above. +*/ t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token; +if (t->len != prepparm->kb.len -3 *sizeof (uint16_t )) { +ZCRYPT_DBF_ERR ("%s reply with invalid key_token length %u\n" , + __func__, t->len); + rc = -EIO ; + goto out; + }memcpy (key_token, t, t->len); *key_token_size = t->len;
Holger Dengler
maintainerRe: [PATCH v1 1/1] s390/zcrypt: Validate length in reply before using it
Quoted 15 lines · Harald Freudenberger
Quoted 14 lines · Holger Dengler
drivers/s390/crypto/zcrypt_ccamisc.c +4
-1@@ -1158,8 +1158,21 @@ static int _ip_cprb_helper(u16 cardnr, u16 domain, /* do not check the key here, it may be incomplete */ - /* copy the vlsc key token back */ + /* +* Copy the vlsc key token back. +* The available space in the destination (key_token) and the source +* (t) buffer is always larger as the valid range of prepparm-
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
kb.len.
+ * Validate
t->lenba comparing it with the length information in thewhat is this funny "ba" in the previous sentence ?
A typo. Should be "by". Will be fixed in v2.
Quoted 14 lines
Quoted 13 lines
+ * param block of the request (
prepparm->kb.len)
+ * The value range ofprepparm->kb.lenhas been checked above. + */
t = (struct cipherkeytoken *)prepparm->kb.tlv1.key_token; + if (t->len!=prepparm->kb.len- 3 *sizeof(uint16_t)) { + ZCRYPT_DBF_ERR("%s reply with invalidkey_tokenlength %u\n", +__func__,t->len);
+ rc =-EIO;
+ goto out;
+ }
memcpy(key_token, t, t->len);
*key_token_size=t->len;
Quoted 1 line
Reviewed-by Harald Freudenberger <[email protected]>
Thx, I'll pick it for the v2.
Signature · 2 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler