s390/zcrypt: rework length information for dqap

The inline ap_dqap function does not return the number of
bytes actually written into the message buffer. The calling
code inspects the AP message header to figure out what kind
of AP message has been received and pulls the length
information from this header. This processing may not work
correctly in cases where only a fragment of the reply is
received.

With this patch the ap_dqap inline function now returns
the number of actually written bytes in the *length parameter.
So the calling function has a chance to compare the number of
received bytes against what the AP message header length
field states. This is especially useful in cases where a
message could only get partially received.

The low level reply processing functions needed some rework
to be able to catch this new length information and compare
it the right way. The rework also deals with some situations
where until now the reply length was not correctly calculated
and/or set.

All this has been heavily tested as the modifications on
the reply length information may affect crypto load.

Signed-off-by: Harald Freudenberger <freude@linux.ibm.com>
Reviewed-by: Holger Dengler <dengler@linux.ibm.com>
Signed-off-by: Heiko Carstens <hca@linux.ibm.com>
This commit is contained in:
Harald Freudenberger 2023-02-14 17:13:18 +01:00 committed by Heiko Carstens
parent 003d248fee
commit 8794c59613
6 changed files with 60 additions and 37 deletions

View File

@ -359,10 +359,11 @@ static inline struct ap_queue_status ap_nqap(ap_qid_t qid,
* ap_dqap(): Receive message from adjunct processor queue. * ap_dqap(): Receive message from adjunct processor queue.
* @qid: The AP queue number * @qid: The AP queue number
* @psmid: Pointer to program supplied message identifier * @psmid: Pointer to program supplied message identifier
* @msg: The message text * @msg: Pointer to message buffer
* @length: The message length * @msglen: Message buffer size
* @reslength: Resitual length on return * @length: Pointer to length of actually written bytes
* @resgr0: input: gr0 value (only used if != 0), output: resitual gr0 content * @reslength: Residual length on return
* @resgr0: input: gr0 value (only used if != 0), output: residual gr0 content
* *
* Returns AP queue status structure. * Returns AP queue status structure.
* Condition code 1 on DQAP means the receive has taken place * Condition code 1 on DQAP means the receive has taken place
@ -387,7 +388,8 @@ static inline struct ap_queue_status ap_nqap(ap_qid_t qid,
*/ */
static inline struct ap_queue_status ap_dqap(ap_qid_t qid, static inline struct ap_queue_status ap_dqap(ap_qid_t qid,
unsigned long *psmid, unsigned long *psmid,
void *msg, size_t length, void *msg, size_t msglen,
size_t *length,
size_t *reslength, size_t *reslength,
unsigned long *resgr0) unsigned long *resgr0)
{ {
@ -399,7 +401,7 @@ static inline struct ap_queue_status ap_dqap(ap_qid_t qid,
rp1.even = 0UL; rp1.even = 0UL;
rp1.odd = 0UL; rp1.odd = 0UL;
rp2.even = (unsigned long)msg; rp2.even = (unsigned long)msg;
rp2.odd = (unsigned long)length; rp2.odd = (unsigned long)msglen;
asm volatile( asm volatile(
" lgr 0,%[reg0]\n" /* qid param into gr0 */ " lgr 0,%[reg0]\n" /* qid param into gr0 */
@ -434,6 +436,10 @@ static inline struct ap_queue_status ap_dqap(ap_qid_t qid,
*resgr0 = 0; *resgr0 = 0;
} }
/* update *length with the nr of bytes stored into the msg buffer */
if (length)
*length = msglen - rp2.odd;
return reg1.status; return reg1.status;
} }

View File

@ -244,8 +244,8 @@ struct ap_message {
struct list_head list; /* Request queueing. */ struct list_head list; /* Request queueing. */
unsigned long psmid; /* Message id. */ unsigned long psmid; /* Message id. */
void *msg; /* Pointer to message buffer. */ void *msg; /* Pointer to message buffer. */
unsigned int len; /* actual msg len in msg buffer */ size_t len; /* actual msg len in msg buffer */
unsigned int bufsize; /* allocated msg buffer size */ size_t bufsize; /* allocated msg buffer size */
u16 flags; /* Flags, see AP_MSG_FLAG_xxx */ u16 flags; /* Flags, see AP_MSG_FLAG_xxx */
struct ap_fi fi; /* Failure Injection cmd */ struct ap_fi fi; /* Failure Injection cmd */
int rc; /* Return code for this message */ int rc; /* Return code for this message */
@ -285,8 +285,8 @@ static inline void ap_release_message(struct ap_message *ap_msg)
* for the first time. Otherwise the ap message queue will get * for the first time. Otherwise the ap message queue will get
* confused. * confused.
*/ */
int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t length); int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen);
int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t length); int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t msglen);
enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event); enum ap_sm_wait ap_sm_event(struct ap_queue *aq, enum ap_sm_event event);
enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event); enum ap_sm_wait ap_sm_event_loop(struct ap_queue *aq, enum ap_sm_event event);

View File

@ -59,7 +59,7 @@ static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
* @qid: The AP queue number * @qid: The AP queue number
* @psmid: The program supplied message identifier * @psmid: The program supplied message identifier
* @msg: The message text * @msg: The message text
* @length: The message length * @msglen: The message length
* @special: Special Bit * @special: Special Bit
* *
* Returns AP queue status structure. * Returns AP queue status structure.
@ -68,19 +68,19 @@ static int ap_queue_enable_irq(struct ap_queue *aq, void *ind)
* because a segment boundary was reached. The NQAP is repeated. * because a segment boundary was reached. The NQAP is repeated.
*/ */
static inline struct ap_queue_status static inline struct ap_queue_status
__ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t length, __ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen,
int special) int special)
{ {
if (special) if (special)
qid |= 0x400000UL; qid |= 0x400000UL;
return ap_nqap(qid, psmid, msg, length); return ap_nqap(qid, psmid, msg, msglen);
} }
int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t length) int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t msglen)
{ {
struct ap_queue_status status; struct ap_queue_status status;
status = __ap_send(qid, psmid, msg, length, 0); status = __ap_send(qid, psmid, msg, msglen, 0);
switch (status.response_code) { switch (status.response_code) {
case AP_RESPONSE_NORMAL: case AP_RESPONSE_NORMAL:
return 0; return 0;
@ -95,13 +95,13 @@ int ap_send(ap_qid_t qid, unsigned long psmid, void *msg, size_t length)
} }
EXPORT_SYMBOL(ap_send); EXPORT_SYMBOL(ap_send);
int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t length) int ap_recv(ap_qid_t qid, unsigned long *psmid, void *msg, size_t msglen)
{ {
struct ap_queue_status status; struct ap_queue_status status;
if (!msg) if (!msg)
return -EINVAL; return -EINVAL;
status = ap_dqap(qid, psmid, msg, length, NULL, NULL); status = ap_dqap(qid, psmid, msg, msglen, NULL, NULL, NULL);
switch (status.response_code) { switch (status.response_code) {
case AP_RESPONSE_NORMAL: case AP_RESPONSE_NORMAL:
return 0; return 0;
@ -150,7 +150,7 @@ static struct ap_queue_status ap_sm_recv(struct ap_queue *aq)
do { do {
status = ap_dqap(aq->qid, &aq->reply->psmid, status = ap_dqap(aq->qid, &aq->reply->psmid,
aq->reply->msg, aq->reply->bufsize, aq->reply->msg, aq->reply->bufsize,
&reslen, &resgr0); &aq->reply->len, &reslen, &resgr0);
parts++; parts++;
} while (status.response_code == 0xFF && resgr0 != 0); } while (status.response_code == 0xFF && resgr0 != 0);

View File

@ -203,6 +203,7 @@ static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
ap_msg.msg = (void *)get_zeroed_page(GFP_KERNEL); ap_msg.msg = (void *)get_zeroed_page(GFP_KERNEL);
if (!ap_msg.msg) if (!ap_msg.msg)
return -ENOMEM; return -ENOMEM;
ap_msg.bufsize = PAGE_SIZE;
rng_type6cprb_msgx(&ap_msg, 4, &domain); rng_type6cprb_msgx(&ap_msg, 4, &domain);
@ -216,7 +217,7 @@ static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
/* Wait for the test message to complete. */ /* Wait for the test message to complete. */
for (i = 0; i < 2 * HZ; i++) { for (i = 0; i < 2 * HZ; i++) {
msleep(1000 / HZ); msleep(1000 / HZ);
rc = ap_recv(aq->qid, &psmid, ap_msg.msg, 4096); rc = ap_recv(aq->qid, &psmid, ap_msg.msg, ap_msg.bufsize);
if (rc == 0 && psmid == 0x0102030405060708UL) if (rc == 0 && psmid == 0x0102030405060708UL)
break; break;
} }

View File

@ -441,14 +441,17 @@ static void zcrypt_cex2a_receive(struct ap_queue *aq,
t80h = reply->msg; t80h = reply->msg;
if (t80h->type == TYPE80_RSP_CODE) { if (t80h->type == TYPE80_RSP_CODE) {
len = t80h->len; len = t80h->len;
if (len > reply->bufsize || len > msg->bufsize) { if (len > reply->bufsize || len > msg->bufsize ||
len != reply->len) {
ZCRYPT_DBF_DBG("%s len mismatch => EMSGSIZE\n", __func__);
msg->rc = -EMSGSIZE; msg->rc = -EMSGSIZE;
} else { goto out;
memcpy(msg->msg, reply->msg, len);
msg->len = len;
} }
memcpy(msg->msg, reply->msg, len);
msg->len = len;
} else { } else {
memcpy(msg->msg, reply->msg, sizeof(error_reply)); memcpy(msg->msg, reply->msg, sizeof(error_reply));
msg->len = sizeof(error_reply);
} }
out: out:
complete((struct completion *)msg->private); complete((struct completion *)msg->private);

View File

@ -938,28 +938,37 @@ static void zcrypt_msgtype6_receive(struct ap_queue *aq,
t86r->cprbx.cprb_ver_id == 0x02) { t86r->cprbx.cprb_ver_id == 0x02) {
switch (resp_type->type) { switch (resp_type->type) {
case CEXXC_RESPONSE_TYPE_ICA: case CEXXC_RESPONSE_TYPE_ICA:
len = sizeof(struct type86x_reply) + t86r->length - 2; len = sizeof(struct type86x_reply) + t86r->length;
if (len > reply->bufsize || len > msg->bufsize) { if (len > reply->bufsize || len > msg->bufsize ||
len != reply->len) {
ZCRYPT_DBF_DBG("%s len mismatch => EMSGSIZE\n", __func__);
msg->rc = -EMSGSIZE; msg->rc = -EMSGSIZE;
} else { goto out;
memcpy(msg->msg, reply->msg, len);
msg->len = len;
} }
memcpy(msg->msg, reply->msg, len);
msg->len = len;
break; break;
case CEXXC_RESPONSE_TYPE_XCRB: case CEXXC_RESPONSE_TYPE_XCRB:
len = t86r->fmt2.offset2 + t86r->fmt2.count2; if (t86r->fmt2.count2)
if (len > reply->bufsize || len > msg->bufsize) { len = t86r->fmt2.offset2 + t86r->fmt2.count2;
else
len = t86r->fmt2.offset1 + t86r->fmt2.count1;
if (len > reply->bufsize || len > msg->bufsize ||
len != reply->len) {
ZCRYPT_DBF_DBG("%s len mismatch => EMSGSIZE\n", __func__);
msg->rc = -EMSGSIZE; msg->rc = -EMSGSIZE;
} else { goto out;
memcpy(msg->msg, reply->msg, len);
msg->len = len;
} }
memcpy(msg->msg, reply->msg, len);
msg->len = len;
break; break;
default: default:
memcpy(msg->msg, &error_reply, sizeof(error_reply)); memcpy(msg->msg, &error_reply, sizeof(error_reply));
msg->len = sizeof(error_reply);
} }
} else { } else {
memcpy(msg->msg, reply->msg, sizeof(error_reply)); memcpy(msg->msg, reply->msg, sizeof(error_reply));
msg->len = sizeof(error_reply);
} }
out: out:
complete(&resp_type->work); complete(&resp_type->work);
@ -994,18 +1003,22 @@ static void zcrypt_msgtype6_receive_ep11(struct ap_queue *aq,
switch (resp_type->type) { switch (resp_type->type) {
case CEXXC_RESPONSE_TYPE_EP11: case CEXXC_RESPONSE_TYPE_EP11:
len = t86r->fmt2.offset1 + t86r->fmt2.count1; len = t86r->fmt2.offset1 + t86r->fmt2.count1;
if (len > reply->bufsize || len > msg->bufsize) { if (len > reply->bufsize || len > msg->bufsize ||
len != reply->len) {
ZCRYPT_DBF_DBG("%s len mismatch => EMSGSIZE\n", __func__);
msg->rc = -EMSGSIZE; msg->rc = -EMSGSIZE;
} else { goto out;
memcpy(msg->msg, reply->msg, len);
msg->len = len;
} }
memcpy(msg->msg, reply->msg, len);
msg->len = len;
break; break;
default: default:
memcpy(msg->msg, &error_reply, sizeof(error_reply)); memcpy(msg->msg, &error_reply, sizeof(error_reply));
msg->len = sizeof(error_reply);
} }
} else { } else {
memcpy(msg->msg, reply->msg, sizeof(error_reply)); memcpy(msg->msg, reply->msg, sizeof(error_reply));
msg->len = sizeof(error_reply);
} }
out: out:
complete(&resp_type->work); complete(&resp_type->work);