Skip to contentSkip to conversation

Fix and improve zcrypt reply message checks

PATCH Superseded Harald Freudenberger 2 participants 4 messages Sep 10 – Sep 11

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Harald Freudenberger

author maintainer
#0 lore raw

v3 cover Fix and improve zcrypt reply message checks

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

Changelog:
v1: Initial version
v2: Sashiko complained about not checking offset1/count1 at all if
count2 for an CCA type6 reply is given. And a check like this was in place before the rework. So now offset1/count1 is always checked and only if count2 is non zero offset2/count2 is additionally checked.
v3: Again Sashiko had some complains which let to a broader review of
the reply processing functions. Especially the zcrypt_type50.c functions convert_type80() and convert_response() got slight but significant improvements - and also a mysterious BUG_ON has been removed.

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

 drivers/s390/crypto/zcrypt_msgtype50.c |  95 ++++++++----
 drivers/s390/crypto/zcrypt_msgtype6.c  | 196 ++++++++++++++++++-------
 2 files changed, 214 insertions(+), 77 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH v3 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.

Additional improve some of the later invoked evaluation functions which further check the payload. Add comments about length assumptions and for the type50 processing rework the payload processing completely with removing a misplaced BUG_ON().

Fixes 3b6245fd303f ("s390/zcrypt: Separate msgtype implementation from card modules.")
---
 drivers/s390/crypto/zcrypt_msgtype50.c |  95 ++++++++----
 drivers/s390/crypto/zcrypt_msgtype6.c  | 196 ++++++++++++++++++-------
 2 files changed, 214 insertions(+), 77 deletions(-)
drivers/s390/crypto/zcrypt_msgtype50.c +68 -27
index d6fc2d8e7fad..5907fb2a6920 100644
@@ -152,6 +152,11 @@ struct type80_hdr {
 	unsigned char	reserved3[8];
 } __packed;
 
+struct type80_reply {
+	struct type80_hdr hdr;
+	char data[];
+} __packed;
+
 int get_rsa_modex_fc(struct ica_rsa_modexpo *mex, int *fcode)
 {
 	if (!mex->inputdatalength)
@@ -340,32 +345,43 @@ static int ICACRT_msg_to_type50CRT_msg(struct zcrypt_queue *zq,
  * @data: pointer to user output data
  * @length: size of user output data
  *
- * Returns 0 on success or -EFAULT.
+ * Returns 0 on success or neg. errno value on failure.
  */
 static int convert_type80(struct zcrypt_queue *zq,
 			  struct ap_message *reply,
 			  char __user *outputdata,
 			  unsigned int outputdatalength)
 {
-	struct type80_hdr *t80h = reply->msg;
-	unsigned char *data;
+	struct type80_reply *msg = reply->msg;
+	size_t payload_len;
+
+	/*
+	 * reply->len is always >= sizeof(struct type80_reply) here and
+	 * outputdatalength (the modulus size) is guaranteed to be equal to
+	 * inputdatalength and 0 < outputdatalength <= CEX3A_MAX_MOD_SIZE,
+	 * also payload size may be >= outputdatalength.
+	 */
+
+	payload_len = reply->len - sizeof(msg->hdr);
 
-	if (t80h->len < sizeof(*t80h) + outputdatalength) {
-		/* The result is too short, the CEXxA card may not do that.. */
+	if (outputdatalength > payload_len) {
+		/* We expect at least outputdatalength bytes, broken card ? */
 		zq->online = 0;
 		pr_err("Crypto dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
 		       AP_QID_CARD(zq->queue->qid),
-		       AP_QID_QUEUE(zq->queue->qid), t80h->code);
+		       AP_QID_QUEUE(zq->queue->qid), msg->hdr.code);
 		ZCRYPT_DBF_ERR("%s dev=%02x.%04x code=0x%02x => online=0 rc=EAGAIN\n",
 			       __func__, AP_QID_CARD(zq->queue->qid),
-			       AP_QID_QUEUE(zq->queue->qid), t80h->code);
+			       AP_QID_QUEUE(zq->queue->qid), msg->hdr.code);
 		ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
 		return -EAGAIN;
 	}
-	BUG_ON(t80h->len > CEX3A_MAX_RESPONSE_SIZE);
-	data = reply->msg + t80h->len - outputdatalength;
-	if (copy_to_user(outputdata, data, outputdatalength))
+
+	if (copy_to_user(outputdata,
+			 msg->data + payload_len - outputdatalength,
+			 outputdatalength))
 		return -EFAULT;
+
 	return 0;
 }
 
@@ -374,14 +390,19 @@ static int convert_response(struct zcrypt_queue *zq,
 			    char __user *outputdata,
 			    unsigned int outputdatalength)
 {
-	/* Response type byte is the second byte in the response. */
-	unsigned char rtype = ((unsigned char *)reply->msg)[1];
+	struct type80_reply *msg = reply->msg;
+
+	/* reply->len is always >= sizeof(struct error_hdr) here */
 
-	switch (rtype) {
+	switch (msg->hdr.type) {
 	case TYPE82_RSP_CODE:
 	case TYPE88_RSP_CODE:
 		return convert_error(zq, reply);
 	case TYPE80_RSP_CODE:
+		if (msg->hdr.code)
+			return convert_error(zq, reply);
+		if (reply->len < sizeof(struct type80_reply))
+			return -EINVAL;
 		return convert_type80(zq, reply,
 				      outputdata, outputdatalength);
 	default: /* Unknown response type, this should NEVER EVER happen */
@@ -389,11 +410,11 @@ static int convert_response(struct zcrypt_queue *zq,
 		pr_err("Crypto dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
 		       AP_QID_CARD(zq->queue->qid),
 		       AP_QID_QUEUE(zq->queue->qid),
-		       (int)rtype);
+		       (int)msg->hdr.type);
 		ZCRYPT_DBF_ERR(
 			"%s dev=%02x.%04x unknown response type 0x%02x => online=0 rc=EAGAIN\n",
 			__func__, AP_QID_CARD(zq->queue->qid),
-			AP_QID_QUEUE(zq->queue->qid), (int)rtype);
+			AP_QID_QUEUE(zq->queue->qid), (int)msg->hdr.type);
 		ap_send_online_uevent(&zq->queue->ap_dev, zq->online);
 		return -EAGAIN;
 	}
@@ -416,26 +437,46 @@ 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;
+
+	if (reply->len < sizeof(*t80h) ||
+	    t80h->type != TYPE80_RSP_CODE) {
+		if (reply->len < sizeof(error_reply)) {
+			/* total broken reply, use static error reply instead */
+			memcpy(msg->msg, &error_reply, sizeof(error_reply));
+			msg->len = sizeof(error_reply);
 			goto out;
+		} else {
+			/* malformed reply, convert function will handle this */
+			len = reply->len;
+			goto copy_len_and_out;
 		}
-		memcpy(msg->msg, reply->msg, len);
-		msg->len = len;
-	} else {
-		memcpy(msg->msg, reply->msg, sizeof(error_reply));
-		msg->len = sizeof(error_reply);
 	}
+
+	len = t80h->len;
+
+copy_len_and_out:
+	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 +146 -50

283 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 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: - [High] Legitimate XCRB and RNG responses will be spuriously rejected due to enforcing an incorrect…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Legitimate XCRB and RNG responses will be spuriously rejected due to enforcing an incorrect minimum length of sizeof(struct type86x_reply). --

commit 991141a9c29feb41fbfdd37e1610a682977e72b6
Author: Harald Freudenberger <[email protected]>

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

This commit adds and improves checks related to buffer sizes and reply sizes for CCA, EP11, and ICA messages in the zcrypt driver. It adds length validation before accessing reply message structures to prevent out-of-bounds reads. It also reworks payload evaluation functions.

Quoted 4 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
index 3df1d676de5d8..a7d392272cfa2 100644

[ ... ]

Quoted 12 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -820,6 +833,8 @@ static int convert_response_xcrb(bool userspace, struct zcrypt_queue *zq,
 {
 	struct type86x_reply *msg = reply->msg;
 
+	/* reply->len is always >= sizeof(struct error_hdr) here */
+
 	switch (msg->hdr.type) {
 	case TYPE82_RSP_CODE:
 	case TYPE88_RSP_CODE:
@@ -827,9 +842,14 @@ static int convert_response_xcrb(bool userspace, struct zcrypt_queue *zq,
 		return convert_error(zq, reply);
 	case TYPE86_RSP_CODE:

drivers/s390/crypto/zcrypt_msgtype6.c static int convert_response_xcrb(bool userspace, struct zcrypt_queue *zq, · line 852

Quoted 10 lines
drivers/s390/crypto/zcrypt_msgtype6.c +6 -1
 		if (msg->hdr.reply_code) {
-			xcrb->status = msg->fmt2.apfs;
+			if (reply->len < sizeof(struct type86_fmt2_msg))
+				xcrb->status = 0x0008044DL;
+			else
+				xcrb->status = msg->fmt2.apfs;
 			return convert_error(zq, reply);
 		}
+		if (reply->len < sizeof(struct type86x_reply))
+			return -EINVAL;

[Severity: High]
Does this code incorrectly reject legitimate XCRB responses?

It appears that this check assumes XCRB replies use the large ICA-specific type86x_reply struct. If a userspace application sends a valid XCRB request without a data payload, the hardware replies successfully without padding.

While zcrypt_msgtype6_receive() correctly computes len as offset1 + count1 and verifies it exactly matches the hardware reply length, this check evaluates reply->len < sizeof(struct type86x_reply). Since type86x_reply includes 6 additional bytes (pad[4] and length) intended only for ICA responses, its size is larger than an empty-payload XCRB response.

Could this result in valid XCRB responses being rejected with -EINVAL, causing a regression for userspace applications using the ICAXCRB ioctl?

Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 		if (msg->cprbx.cprb_ver_id == 0x02)
 			return convert_type86_xcrb(userspace, zq, reply, xcrb);
 		fallthrough;	/* wrong cprb version is an unknown response */

[ ... ]

Quoted 10 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -885,6 +909,8 @@ static int convert_response_rng(struct zcrypt_queue *zq,
 {
 	struct type86x_reply *msg = reply->msg;
 
+	/* reply->len is always >= sizeof(struct error_hdr) here */
+
 	switch (msg->hdr.type) {
 	case TYPE82_RSP_CODE:
 	case TYPE88_RSP_CODE:

drivers/s390/crypto/zcrypt_msgtype6.c static int convert_response_rng(struct zcrypt_queue *zq, · line 922

Quoted 5 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -892,6 +918,8 @@ static int convert_response_rng(struct zcrypt_queue *zq,
 	case TYPE86_RSP_CODE:
 		if (msg->hdr.reply_code)
 			return -EINVAL;
+		if (reply->len < sizeof(struct type86x_reply))
+			return -EINVAL;

[Severity: High]
Does this check similarly apply a logically flawed minimum-size limit to RNG hardware responses?

It seems this could fail if the hardware RNG response buffer size configuration ever drops below the 6-byte delta caused by the ICA-specific padding in type86x_reply.

Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 		if (msg->cprbx.cprb_ver_id == 0x02)
 			return convert_type86_rng(zq, reply, data);
 		fallthrough;	/* wrong cprb version is an unknown response */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

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

Quoted 54 lines · On 2026-09-10 18:35, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Legitimate XCRB and RNG responses will be spuriously rejected due to enforcing an incorrect minimum length of sizeof(struct type86x_reply).
--

commit 991141a9c29feb41fbfdd37e1610a682977e72b6
Author: Harald Freudenberger <[email protected]>

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

This commit adds and improves checks related to buffer sizes and reply sizes for CCA, EP11, and ICA messages in the zcrypt driver. It adds length validation before accessing reply message structures to prevent out-of-bounds reads. It also reworks payload evaluation functions.

Quoted 4 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
index 3df1d676de5d8..a7d392272cfa2 100644

[ ... ]

Quoted 14 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -820,6 +833,8 @@ static int convert_response_xcrb(bool userspace, 
 struct zcrypt_queue *zq,
 {
 	struct type86x_reply *msg = reply->msg;
 
+	/* reply->len is always >= sizeof(struct error_hdr) here */
+
 	switch (msg->hdr.type) {
 	case TYPE82_RSP_CODE:
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 	case TYPE88_RSP_CODE:
@@ -827,9 +842,14 @@ static int convert_response_xcrb(bool userspace, 
 struct zcrypt_queue *zq,
 		return convert_error(zq, reply);
 	case TYPE86_RSP_CODE:

drivers/s390/crypto/zcrypt_msgtype6.c static int convert_response_xcrb(bool userspace, · line 853

Quoted 10 lines
drivers/s390/crypto/zcrypt_msgtype6.c +6 -1
 		if (msg->hdr.reply_code) {
-			xcrb->status = msg->fmt2.apfs;
+			if (reply->len < sizeof(struct type86_fmt2_msg))
+				xcrb->status = 0x0008044DL;
+			else
+				xcrb->status = msg->fmt2.apfs;
 			return convert_error(zq, reply);
 		}
+		if (reply->len < sizeof(struct type86x_reply))
+			return -EINVAL;

[Severity: High]
Does this code incorrectly reject legitimate XCRB responses?

It appears that this check assumes XCRB replies use the large ICA-specific type86x_reply struct. If a userspace application sends a valid XCRB request without a data payload, the hardware replies successfully without padding.

Quoted 9 lines

While zcrypt_msgtype6_receive() correctly computes len as offset1 + count1 and verifies it exactly matches the hardware reply length, this check evaluates reply->len < sizeof(struct type86x_reply). Since type86x_reply includes 6 additional bytes (pad[4] and length) intended only for ICA responses, its size is larger than an empty-payload XCRB response.

Could this result in valid XCRB responses being rejected with -EINVAL, causing a regression for userspace applications using the ICAXCRB ioctl?

Will fix this in the next version.

Quoted 26 lines
Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 		if (msg->cprbx.cprb_ver_id == 0x02)
 			return convert_type86_xcrb(userspace, zq, reply, xcrb);

fallthrough; /* wrong cprb version is an unknown response */

[ ... ]

Quoted 11 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -885,6 +909,8 @@ static int convert_response_rng(struct 
 zcrypt_queue *zq,
 {
 	struct type86x_reply *msg = reply->msg;
 
+	/* reply->len is always >= sizeof(struct error_hdr) here */
+
 	switch (msg->hdr.type) {
 	case TYPE82_RSP_CODE:
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 	case TYPE88_RSP_CODE:

drivers/s390/crypto/zcrypt_msgtype6.c static int convert_response_rng(struct · line 923

Quoted 6 lines
drivers/s390/crypto/zcrypt_msgtype6.c +2 -0
@@ -892,6 +918,8 @@ static int convert_response_rng(struct 
 zcrypt_queue *zq,
 	case TYPE86_RSP_CODE:
 		if (msg->hdr.reply_code)
 			return -EINVAL;
+		if (reply->len < sizeof(struct type86x_reply))
+			return -EINVAL;

[Severity: High]
Does this check similarly apply a logically flawed minimum-size limit to RNG hardware responses?

Quoted 4 lines

It seems this could fail if the hardware RNG response buffer size configuration ever drops below the 6-byte delta caused by the ICA-specific padding in type86x_reply.

Does not apply. A valid RNG reply always need to be >= sizeof(struct type86x_reply) and even more. However to access the fields cprbx.ccp_rtcode and cprbx.ccp_rscode in function convert_type86_rng() this check is a precondition.

Quoted 3 lines
Quoted 3 lines
drivers/s390/crypto/zcrypt_msgtype6.c +0 -0
 		if (msg->cprbx.cprb_ver_id == 0x02)
 			return convert_type86_rng(zq, reply, data);

fallthrough; /* wrong cprb version is an unknown response */