Improve code in zcrypt msg type 6 handling
Subsystems (2)
Harald Freudenberger
maintainerv6 cover Improve code in zcrypt msg type 6 handling
Rework the limit and overflow checks in the both functions xcrb_msg_to_type6_ep11cprb_msgx() and xcrb_msg_to_type6cprb_msgx().
Changelog:
v1: initial version, one patch dealing with overflow checks around
the use of the CEIL4 macro in zcrypt_msgtype6.c
v2: Sashiko found a regression: The check
if (ap_msg->len > ap_msg->bufsize)
was missing in the rework. Added again this important check.
v3: - Split into two patches - one for CCA one for EP11
- rework upper limit/overflow check
- add lower limit check
v4: - tried to address all the remaining complains from sashiko.
v5: - Yea, sashiko still found out that the min req size needs
to take the minimal payload header into account. Fixed.
v6: - And finally add padding zeros when there is a gap between
user space provided message size and rounded message size in kernel.
Harald Freudenberger (2):
s390/zcrypt: Improve CCA CPRB length and overflow checks s390/zcrypt: Improve EP11 CPRB length and overflow checks
drivers/s390/crypto/zcrypt_msgtype6.c | 101 ++++++++++++++------------ 1 file changed, 55 insertions(+), 46 deletions(-)
Signature · 1 line
2.43.0
Harald Freudenberger
maintainerPATCH v6 1/2 s390/zcrypt: Improve CCA CPRB length and overflow checks
The xcrb_msg_to_type6cprb_msgx() function lacks proper input validation, creating security vulnerabilities:
1. Integer overflow after CEIL4 alignment: Signed int variables could overflow during 4-byte boundary alignment, causing undersized buffer allocations or incorrect bounds checking.
2. Missing minimum size validation: The CPRBX structure is copied from userspace without verifying sufficient buffer length. Undersized buffers cause uninitialized memory access when reading structure fields like cprbx.cprb_len and cprbx.domain.
3. Arithmetic overflow in sum calculations: Adding control block and data block sizes could overflow, bypassing size checks and enabling buffer overflows.
4. Potential kernel memory leak if copy_from_user() only copies xcrb->request_control_blk_length bytes but the processing of the message works with the rounded up to 4 byte boundary buffer size.
Fix by using size_t for length calculations, adding U32_MAX boundary checks after alignment, validating minimum control block size before copying from userspace, and detecting sum calculation overflows. Fix the possible kernel memory leak by zeroing the trailing bytes.
--- drivers/s390/crypto/zcrypt_msgtype6.c | 77 +++++++++++++-------------- 1 file changed, 37 insertions(+), 40 deletions(-)
drivers/s390/crypto/zcrypt_msgtype6.c +37 -40
@@ -342,49 +342,40 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, }; }__packed * msg = ap_msg->msg; -int rcblen =CEIL4 (xcrb->request_control_blk_length); -int req_sumlen, resp_sumlen; -char *req_data = ap_msg->msg +sizeof (struct type6_hdr ) + rcblen; -char *function_code; +size_t req_cblen, rep_cblen, req_sumlen, rep_sumlen; +char *function_code, *req_data; -if (CEIL4 (xcrb->request_control_blk_length) < - xcrb->request_control_blk_length) -return -EINVAL ;/* overflow after alignment*/ - -/* length checks */ +/* request length and overflow checks */ +if (xcrb->request_control_blk_length <sizeof (struct CPRBX )) +return -EINVAL ; + req_cblen =CEIL4 ((size_t )xcrb->request_control_blk_length); +if (req_cblen >U32_MAX ) +return -EINVAL ; ap_msg->len =sizeof (struct type6_hdr ) + -CEIL4 (xcrb->request_control_blk_length) + - xcrb->request_data_length; + req_cblen + xcrb->request_data_length;if (ap_msg->len > ap_msg->bufsize)return -EINVAL ; - -/* -* Overflow check -* sum must be greater (or equal) than the largest operand -*/ - req_sumlen =CEIL4 (xcrb->request_control_blk_length) + - xcrb->request_data_length; -if ((CEIL4 (xcrb->request_control_blk_length) <= - xcrb->request_data_length) ? + req_sumlen = req_cblen + xcrb->request_data_length; +if (req_sumlen >U32_MAX ) +return -EINVAL ; +if (req_cblen <= xcrb->request_data_length ? req_sumlen < xcrb->request_data_length : - req_sumlen <CEIL4 (xcrb->request_control_blk_length)) { + req_sumlen < req_cblen) {return -EINVAL ; } -if (CEIL4 (xcrb->reply_control_blk_length) < - xcrb->reply_control_blk_length) -return -EINVAL ;/* overflow after alignment*/ - -/* -* Overflow check -* sum must be greater (or equal) than the largest operand -*/ - resp_sumlen =CEIL4 (xcrb->reply_control_blk_length) + - xcrb->reply_data_length; -if ((CEIL4 (xcrb->reply_control_blk_length) <= - xcrb->reply_data_length) ? - resp_sumlen < xcrb->reply_data_length : - resp_sumlen <CEIL4 (xcrb->reply_control_blk_length)) { +/* reply length and overflow checks */ +if (xcrb->reply_control_blk_length <sizeof (struct CPRBX )) +return -EINVAL ; + rep_cblen =CEIL4 ((size_t )xcrb->reply_control_blk_length); +if (rep_cblen >U32_MAX ) +return -EINVAL ; + rep_sumlen = rep_cblen + xcrb->reply_data_length; +if (rep_sumlen >U32_MAX ) +return -EINVAL ; +if (rep_cblen <= xcrb->reply_data_length ? + rep_sumlen < xcrb->reply_data_length : + rep_sumlen < rep_cblen) {return -EINVAL ; }
@@ -393,7 +384,7 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg,memcpy (msg->hdr.agent_id, &xcrb->agent_ID,sizeof (xcrb->agent_ID)); msg->hdr.tocardlen1 = xcrb->request_control_blk_length;if (xcrb->request_data_length) { - msg->hdr.offset2 = msg->hdr.offset1 + rcblen; + msg->hdr.offset2 = msg->hdr.offset1 + req_cblen; msg->hdr.tocardlen2 = xcrb->request_data_length; } msg->hdr.fromcardlen1 = xcrb->reply_control_blk_length;
@@ -404,6 +395,9 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, xcrb->request_control_blk_addr, xcrb->request_control_blk_length))return -EFAULT ; +if (xcrb->request_control_blk_length < req_cblen) +memset (msg->userdata + xcrb->request_control_blk_length, +0 , req_cblen - xcrb->request_control_blk_length);if (msg->cprbx.cprb_len +sizeof (msg->hdr.function_code) > xcrb->request_control_blk_length)return -EINVAL ;
@@ -437,10 +431,13 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, }/* copy data block */ -if (xcrb->request_data_length && -z_copy_from_user (userspace, req_data, xcrb->request_data_address, - xcrb->request_data_length)) -return -EFAULT ; +if (xcrb->request_data_length) { + req_data = ap_msg->msg +sizeof (struct type6_hdr ) + req_cblen; +if (z_copy_from_user (userspace, req_data, + xcrb->request_data_address, + xcrb->request_data_length)) +return -EFAULT ; + }return 0 ; }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v6 1/2] s390/zcrypt: Improve CCA CPRB length and overflow checks
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Integer overflow in xcrb_msg_to_type6_ep11cprb_msgx allows bypassing message length checks, leading to an out-of-bounds memset in copy_from_user that panics the kernel.
- [High] Missing minimum size validation in xcrb_msg_to_type6_ep11cprb_msgx allows uninitialized memory access.
- [High] Kernel memory leak via uninitialized alignment padding bytes in xcrb_msg_to_type6_ep11cprb_msgx.
- [High] The driver trusts length and offset values (offset1, count1) from the hardware's type86_fmt2 message without fully checking them against the received message length, which can lead to an out-of-bounds kernel memory read. --
--- Patch [1]: [PATCH v6 1/2] s390/zcrypt: Improve CCA CPRB length and overflow checks --- commit 7784ed33ea5e9edca90b3daa3295185e1ce64459
Author: Harald Freudenberger <[email protected]>
s390/zcrypt: Improve CCA CPRB length and overflow checks
The xcrb_msg_to_type6cprb_msgx() function lacks proper input validation, creating several security vulnerabilities including integer overflows, missing minimum size validations, and a potential kernel memory leak. This patch fixes these issues by using size_t for length calculations, adding boundary checks, and zeroing trailing bytes.
Quoted 5 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0
-0@@ -342,49 +342,40 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg,
[ ... ]
drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, · line 350
Quoted 8 lines
drivers/s390/crypto/zcrypt_msgtype6.c +3
-5-if (CEIL4 (xcrb->request_control_blk_length) < - xcrb->request_control_blk_length) -return -EINVAL ;/* overflow after alignment*/ - -/* length checks */ +/* request length and overflow checks */ +if (xcrb->request_control_blk_length <sizeof (struct CPRBX )) +return -EINVAL ;
[Severity: High]
This isn't a bug introduced by this patch, but it appears a similar missing minimum size validation exists in xcrb_msg_to_type6_ep11cprb_msgx().
Can a small req_len (e.g., 0) cause reads of uninitialized kernel heap memory when extracting fields like pld_lenfmt or flags?
drivers/s390/crypto/zcrypt_msgtype6.c:xcrb_msg_to_type6_ep11cprb_msgx() {
...
if ((msg->pld_lenfmt & 0x80) == 0x80) {
...
}drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, · line 357
Quoted 9 lines
drivers/s390/crypto/zcrypt_msgtype6.c +4
-2+ req_cblen =CEIL4 ((size_t )xcrb->request_control_blk_length); +if (req_cblen >U32_MAX ) +return -EINVAL ; ap_msg->len =sizeof (struct type6_hdr ) + -CEIL4 (xcrb->request_control_blk_length) + - xcrb->request_data_length; + req_cblen + xcrb->request_data_length;if (ap_msg->len > ap_msg->bufsize)return -EINVAL ;
[Severity: Critical]
This is a pre-existing issue, but it looks like xcrb_msg_to_type6_ep11cprb_msgx() has an integer overflow similar to the one fixed in this patch.
Can a massive req_len cause an integer overflow when computing ap_msg->len, bypassing the ap_msg->bufsize bounds check?
drivers/s390/crypto/zcrypt_msgtype6.c:xcrb_msg_to_type6_ep11cprb_msgx() {
...
ap_msg->len = sizeof(struct type6_hdr) + CEIL4(xcrb->req_len);
...
if (z_copy_from_user(userspace, msg->userdata,
(char __force __user *)xcrb->req, xcrb->req_len)) {
...
}If z_copy_from_user() is called with the large length, the copy_from_user() fallback memset could write outside the allocated kernel buffer.
[ ... ]
drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, · line 400
Quoted 7 lines
drivers/s390/crypto/zcrypt_msgtype6.c +3
-0@@ -404,6 +395,9 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, xcrb->request_control_blk_addr, xcrb->request_control_blk_length))return -EFAULT ; +if (xcrb->request_control_blk_length < req_cblen) +memset (msg->userdata + xcrb->request_control_blk_length, +0 , req_cblen - xcrb->request_control_blk_length);
[Severity: High]
This is a pre-existing issue, but does xcrb_msg_to_type6_ep11cprb_msgx() leak kernel memory via uninitialized alignment padding bytes in a similar way?
The buffer length sent to the hardware incorporates alignment, but only req_len bytes are copied, leaving padding bytes uninitialized:
drivers/s390/crypto/zcrypt_msgtype6.c:xcrb_msg_to_type6_ep11cprb_msgx() {
...
if (z_copy_from_user(userspace, msg->userdata,
(char __force __user *)xcrb->req, xcrb->req_len)) {
...
}[ ... ]
Quoted 8 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0
-4@@ -437,10 +431,13 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, }/* copy data block */ -if (xcrb->request_data_length && -z_copy_from_user (userspace, req_data, xcrb->request_data_address, - xcrb->request_data_length)) -return -EFAULT ;
drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, · line 443
Quoted 10 lines
drivers/s390/crypto/zcrypt_msgtype6.c +7
-0+if (xcrb->request_data_length) { + req_data = ap_msg->msg +sizeof (struct type6_hdr ) + req_cblen; +if (z_copy_from_user (userspace, req_data, + xcrb->request_data_address, + xcrb->request_data_length)) +return -EFAULT ; + }return 0 ; }
[Severity: High]
This isn't a bug introduced by this patch, but does convert_type86_xcrb() trust length and offset values from the hardware without fully checking them?
zcrypt_msgtype6_receive() only validates offset2 and count2 if count2 is non-zero, ignoring offset1 and count1 during the bounds check:
drivers/s390/crypto/zcrypt_msgtype6.c:zcrypt_msgtype6_receive() {
...
if (t86r->fmt2.count2)
len = t86r->fmt2.offset2 + t86r->fmt2.count2;
...
}Later, convert_type86_xcrb() uses offset1 and count1 for a copy to user space without verifying they are within the initialized message buffer:
drivers/s390/crypto/zcrypt_msgtype6.c:convert_type86_xcrb() {
...
if (z_copy_to_user(userspace, xcrb->reply_control_blk_addr,
data + msg->fmt2.offset1, msg->fmt2.count1))
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Holger Dengler
maintainerRe: [PATCH v6 1/2] s390/zcrypt: Improve CCA CPRB length and overflow checks
In reply to Harald FreudenbergerQuoted 16 lines · Harald Freudenberger
The
xcrb_msg_to_type6cprb_msgx()function lacks proper input validation, creating security vulnerabilities:
1. Integer overflow after CEIL4 alignment: Signed int variables could overflow during 4-byte boundary alignment, causing undersized buffer allocations or incorrect bounds checking.
2. Missing minimum size validation: The CPRBX structure is copied from userspace without verifying sufficient buffer length. Undersized buffers cause uninitialized memory access when reading structure fields like cprbx.cprb_len and cprbx.domain.
3. Arithmetic overflow in sum calculations: Adding control block and data block sizes could overflow, bypassing size checks and enabling buffer overflows.
4. Potential kernel memory leak ifcopy_from_user()only copiesxcrb->request_control_blk_lengthbytes but the processing of the message works with the rounded up to 4 byte boundary buffer size.
Quoted 4 lines
Fix by using
size_tfor length calculations, addingU32_MAXboundary checks after alignment, validating minimum control block size before copying from userspace, and detecting sum calculation overflows. Fix the possible kernel memory leak by zeroing the trailing bytes.
The last sentence should go into a separate patch. See my comment below.
Quoted 1 line
Signed-off-by Harald Freudenberger <[email protected]>
The length check looks good so far. Only the min-length check need some rework (as discussed).
Quoted 8 lines
--- drivers/s390/crypto/zcrypt_msgtype6.c | 77 +++++++++++++-------------- 1 file changed, 37 insertions(+), 40 deletions(-)drivers/s390/crypto/zcrypt_msgtype6.c +0
-0
[...]
drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, · line 400
Quoted 7 lines
drivers/s390/crypto/zcrypt_msgtype6.c +3
-0@@ -404,6 +395,9 @@ static int xcrb_msg_to_type6cprb_msgx(bool userspace, struct ap_message *ap_msg, xcrb->request_control_blk_addr, xcrb->request_control_blk_length))return -EFAULT ; +if (xcrb->request_control_blk_length < req_cblen) +memset (msg->userdata + xcrb->request_control_blk_length, +0 , req_cblen - xcrb->request_control_blk_length);
Can you please move this wipe into a separate patch? It is not related to the input checking but addresses another class of vulnerability.
I think it is worth to describe this issue/fix in its own commit message.
Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0
-0if (msg->cprbx.cprb_len +sizeof (msg->hdr.function_code) > xcrb->request_control_blk_length) return -EINVAL;[...]
Signature · 2 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler
Harald Freudenberger
maintainerPATCH v6 2/2 s390/zcrypt: Improve EP11 CPRB length and overflow checks
In reply to Harald FreudenbergerThe xcrb_msg_to_type6_ep11cprb_msgx() function lacks proper input validation, creating security vulnerabilities:
1. Missing minimum size validation: The ep11_cprb structure and subsequent payload fields (pld_tag, pld_lenfmt) are copied from userspace without verifying sufficient buffer length.
2. Arithmetic overflow in length calculations: CEIL4 alignment could overflow, bypassing size checks and enabling buffer overflows.
3. There was a gap between the message size copied from user space and the message buffer sent down to the card. The msg buffer size is rounded up to 4 byte boundary.
Fix by using size_t for length calculations, adding U32_MAX boundary checks after alignment, and validating minimum request size and minimum reply size before copying from userspace. Also pad the gap between user space copied message and message buffer sent down to the card by zeros.
--- drivers/s390/crypto/zcrypt_msgtype6.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)
drivers/s390/crypto/zcrypt_msgtype6.c +18 -6
@@ -477,16 +477,25 @@ static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *apunsigned int dom_val;/* domain id */ }__packed * payload_hdr =NULL ; -if (CEIL4 (xcrb->req_len) < xcrb->req_len) -return -EINVAL ;/* overflow after alignment*/ +size_t req_len, rep_len; -/* length checks */ - ap_msg->len =sizeof (struct type6_hdr ) +CEIL4 (xcrb->req_len); +/* request length and overflow checks */ +if (xcrb->req_len <sizeof (struct ep11_cprb ) + +2 *sizeof (unsigned char ) +sizeof (struct pld_hdr )) +return -EINVAL ; + req_len =CEIL4 (xcrb->req_len); +if (req_len < xcrb->req_len || req_len >U32_MAX ) +return -EINVAL ; + ap_msg->len =sizeof (struct type6_hdr ) + req_len;if (ap_msg->len > ap_msg->bufsize)return -EINVAL ; -if (CEIL4 (xcrb->resp_len) < xcrb->resp_len) -return -EINVAL ;/* overflow after alignment*/ +/* reply length and overflow checks */ +if (xcrb->resp_len <sizeof (struct ep11_cprb )) +return -EINVAL ; + rep_len =CEIL4 (xcrb->resp_len); +if (rep_len < xcrb->resp_len || rep_len >U32_MAX ) +return -EINVAL ;/* prepare type6 header */ msg->hdr = static_type6_ep11_hdr;
@@ -498,6 +507,9 @@ static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *ap (char __force __user *)xcrb->req, xcrb->req_len)) {return -EFAULT ; } +if (xcrb->req_len < req_len) +memset (msg->userdata + xcrb->req_len,0 , + req_len - xcrb->req_len);if ((msg->pld_lenfmt &0x80 ) ==0x80 ) {/*ext.len.fmt 2 or 3*/ switch (msg->pld_lenfmt &0x03 ) {
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v6 2/2] s390/zcrypt: Improve EP11 CPRB length and overflow checks
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines
The
xcrb_msg_to_type6_ep11cprb_msgx()function lacks proper input validation, creating security vulnerabilities:
1. Missing minimum size validation: Theep11_cprbstructure and subsequent payload fields (pld_tag,pld_lenfmt) are copied from userspace without verifying sufficient buffer length.
2. Arithmetic overflow in length calculations: CEIL4 alignment could overflow, bypassing size checks and enabling buffer overflows.
3. There was a gap between the message size copied from user space and the message buffer sent down to the card. The msg buffer size is rounded up to 4 byte boundary.
Quoted 7 lines
Fix by using
size_tfor length calculations, addingU32_MAXboundary checks after alignment, and validating minimum request size and minimum reply size before copying from userspace. Also pad the gap between user space copied message and message buffer sent down to the card by zeros.Signed-off-by Harald Freudenberger <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Holger Dengler
maintainerRe: [PATCH v6 2/2] s390/zcrypt: Improve EP11 CPRB length and overflow checks
In reply to Harald Freudenbergerdrivers/s390/crypto/zcrypt_msgtype6.c
Quoted 26 lines · Harald Freudenberger
The
xcrb_msg_to_type6_ep11cprb_msgx()function lacks proper input validation, creating security vulnerabilities:
1. Missing minimum size validation: Theep11_cprbstructure and subsequent payload fields (pld_tag,pld_lenfmt) are copied from userspace without verifying sufficient buffer length.
2. Arithmetic overflow in length calculations: CEIL4 alignment could overflow, bypassing size checks and enabling buffer overflows.
3. There was a gap between the message size copied from user space and the message buffer sent down to the card. The msg buffer size is rounded up to 4 byte boundary.Fix by using
size_tfor length calculations, addingU32_MAXboundary checks after alignment, and validating minimum request size and minimum reply size before copying from userspace. Also pad the gap between user space copied message and message buffer sent down to the card by zeros.Signed-off-by Harald Freudenberger <[email protected]>--- drivers/s390/crypto/zcrypt_msgtype6.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-)drivers/s390/crypto/zcrypt_msgtype6.c +0
-0
[...]> @@ -498,6 +507,9 @@ static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *ap
drivers/s390/crypto/zcrypt_msgtype6.c static int xcrb_msg_to_type6_ep11cprb_msgx(bool userspace, struct ap_message *ap · line 512
Quoted 6 lines
(char __force __user *)xcrb->req, xcrb->req_len)) { return -EFAULT; }drivers/s390/crypto/zcrypt_msgtype6.c +3
-0+if (xcrb->req_len < req_len) +memset (msg->userdata + xcrb->req_len,0 , + req_len - xcrb->req_len);
Same here, please move the memset to a separate patch. (Maybe both wipes can go into one commit)
Quoted 3 lines
if ((msg->pld_lenfmt & 0x80) == 0x80) { /*ext.len.fmt 2 or 3*/
switch (msg->pld_lenfmt & 0x03) {Signature · 2 lines
Mit freundlichen Grüßen / Kind regards Holger Dengler