Skip to contentSkip to conversation

Fix and improve zcrypt reply message checks

PATCH Superseded Harald Freudenberger 2 participants 3 messages Sep 8

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Harald Freudenberger

author maintainer
#0 lore raw

cover Fix and improve zcrypt reply message checks

Fix and improve zcrypt reply messages of type 6 - CCA and EP11 - and type 50 - ICA.

Harald Freudenberger (1):
s390/zcrypt: Fix and improve zcrypt reply message verification checks

 drivers/s390/crypto/zcrypt_msgtype50.c |  40 ++++--
 drivers/s390/crypto/zcrypt_msgtype6.c  | 162 +++++++++++++++++--------
 2 files changed, 138 insertions(+), 64 deletions(-)

--
2.43.0

author maintainer
#1 lore raw

PATCH 1/1 s390/zcrypt: Fix and improve zcrypt reply message verification checks

Add or improve checks related to buffer sizes and reply sizes to the handling of replies from the crypto cards for CCA, EP11 (AP message type 6) and ICA (AP type 50) messages. The verification code related to reply field length was not designed well and thus firmware deficiencies could lead to unexpected behavior in the zcrypt device driver. Thus improve the code to more closely inspect especially length fields at message replies.

Add length validation before accessing reply message structures in zcrypt_msgtype6_receive(), zcrypt_msgtype6_receive_ep11(), and zcrypt_msgtype50_receive() to prevent out-of-bounds reads and potential kernel memory disclosure.

Also rework these three functions to validate reply lengths more carefully before copying data back into the request buffer. Use size_t for length calculations, reject inconsistent reply sizes, and add defensive handling for short invalid replies. For XCRB replies, validate both reply segments and derive the effective message length from the covered range instead of trusting only the second segment.

Fixes 3b6245fd303f ("s390/zcrypt: Separate msgtype implementation from card modules.")
---
 drivers/s390/crypto/zcrypt_msgtype50.c |  40 ++++--
 drivers/s390/crypto/zcrypt_msgtype6.c  | 162 +++++++++++++++++--------
 2 files changed, 138 insertions(+), 64 deletions(-)
drivers/s390/crypto/zcrypt_msgtype50.c +27 -13
index d6fc2d8e7fad..80c447ea2d83 100644
@@ -416,26 +416,40 @@ static void zcrypt_msgtype50_receive(struct ap_queue *aq,
 		.reply_code = REP82_ERROR_MACHINE_FAILURE,
 	};
 	struct type80_hdr *t80h;
-	int len;
+	size_t len;
 
 	/* Copy the reply message to the request message buffer. */
 	if (!reply)
 		goto out;	/* ap_msg->rc indicates the error */
+
 	t80h = reply->msg;
-	if (t80h->type == TYPE80_RSP_CODE) {
-		len = t80h->len;
-		if (len > reply->bufsize || len > msg->bufsize ||
-		    len != reply->len) {
-			pr_debug("len mismatch => EMSGSIZE\n");
-			msg->rc = -EMSGSIZE;
-			goto out;
-		}
-		memcpy(msg->msg, reply->msg, len);
-		msg->len = len;
-	} else {
-		memcpy(msg->msg, reply->msg, sizeof(error_reply));
+
+	if (reply->len < sizeof(*t80h) ||
+	    t80h->type != TYPE80_RSP_CODE) {
+		if (reply->len < sizeof(error_reply))
+			memcpy(msg->msg, &error_reply, sizeof(error_reply));
+		else
+			memcpy(msg->msg, reply->msg, sizeof(error_reply));
 		msg->len = sizeof(error_reply);
+		goto out;
+	}
+
+	len = t80h->len;
+	if (len != reply->len) {
+		msg->rc = -EMSGSIZE;
+		pr_debug("len %zu rpl.len %zu mismatch, msg.rc=%d\n",
+			 len, reply->len, msg->rc);
+		goto out;
 	}
+	if (len > reply->bufsize || len > msg->bufsize) {
+		msg->rc = -EMSGSIZE;
+		pr_debug("len %zu exceeds buf %zu/%zu, msg.rc=%d\n",
+			 len, reply->bufsize, msg->bufsize, msg->rc);
+		goto out;
+	}
+	memcpy(msg->msg, reply->msg, len);
+	msg->len = len;
+
 out:
 	complete(&msg->response.work);
 }
drivers/s390/crypto/zcrypt_msgtype6.c +111 -51

199 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v1 1/1] s390/zcrypt: Fix and improve zcrypt reply message verification checks

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] The length validation for XCRB replies with a secondary data segment (count2 > 0) fails to verify that the primary segment's boundaries (offset1 + count1) fit within the message buffer, allowing an out-of-bounds read and uninitialized kernel heap memory disclosure to userspace. --

commit e0149d402b0c0e1ac4ba64b32211aa5a4effb09f
Author: Harald Freudenberger <[email protected]>

s390/zcrypt: Fix and improve zcrypt reply message verification checks

This commit reworks length validation for crypto card replies in the zcrypt device driver. It adds bounds checks to prevent out-of-bounds reads when handling CCA, EP11, and ICA messages.

Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0

[ ... ]

Quoted 1 line
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
@@ -928,48 +935,82 @@ static void zcrypt_msgtype6_receive(struct ap_queue *aq,

[ ... ]

drivers/s390/crypto/zcrypt_msgtype6.c static void zcrypt_msgtype6_receive(struct ap_queue *aq, · line 982

Quoted 13 lines
drivers/s390/crypto/zcrypt_msgtype6.c +13 -0
+	case CEXXC_RESPONSE_TYPE_XCRB:
+		len = (size_t)t86r->fmt2.offset1 + (size_t)t86r->fmt2.count1;
+		if (t86r->fmt2.offset1 > reply->len ||
+		    t86r->fmt2.count1 > reply->len) {
+			msg->rc = -EMSGSIZE;
+			pr_debug("offset1 %u count1 %u rpl.len %zu mismatch, msg.rc=%d\n",
+				 t86r->fmt2.offset1, t86r->fmt2.count1,
+				 reply->len, msg->rc);
+			goto out;
+		}
+		if (t86r->fmt2.count2) {
+			len = (size_t)t86r->fmt2.offset2 +
+				(size_t)t86r->fmt2.count2;

[Severity: High]
This is a pre-existing issue, but does this overwrite of len bypass the bounds checking for offset1 and count1 against reply->len?

When count2 is greater than 0, len is overwritten with the secondary segment's length (offset2 + count2). Later in zcrypt_msgtype6_receive(), the length validation check only verifies:

if (len != reply->len) {
        ...
}
if (len > reply->bufsize || len > msg->bufsize) {
        ...
}

Since len is now based entirely on offset2 and count2, the primary segment's boundaries (offset1 + count1) are never verified to ensure they actually fit within the message buffer (offset1 + count1 <= reply->len).

Could this lead to an out-of-bounds read later when convert_type86_xcrb() handles the reply?

If a malformed reply has count2 > 0 but crafted offset1 and count1 values, convert_type86_xcrb() will use the unvalidated offset1 and count1 to copy kernel data to userspace:

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))
                return -EFAULT;
        ...
}

Since the message buffer is allocated without zeroing, could this copy uninitialized heap memory past the end of the valid firmware reply back to userspace?

Quoted 8 lines
drivers/s390/crypto/zcrypt_msgtype6.c +5 -0
+			if (t86r->fmt2.offset2 > reply->len ||
+			    t86r->fmt2.count2 > reply->len) {
 				msg->rc = -EMSGSIZE;
+				pr_debug("offset2 %u count2 %u rpl.len %zu mismatch, msg.rc=%d\n",
+					 t86r->fmt2.offset2, t86r->fmt2.count2,
+					 reply->len, msg->rc);
 				goto out;
 			}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1