From patchwork Tue Oct 25 09:00:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernard Metzler X-Patchwork-Id: 10601 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp891474wru; Tue, 25 Oct 2022 02:06:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7RcMYBl0eFmGRBHwxsBVyjHvlYRMARuyL9LT6/Fvu312LK6eft+IGM3uTT4KK8cRnCXEZb X-Received: by 2002:a17:906:4795:b0:794:8b93:2e43 with SMTP id cw21-20020a170906479500b007948b932e43mr26137879ejc.184.1666688818857; Tue, 25 Oct 2022 02:06:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666688818; cv=none; d=google.com; s=arc-20160816; b=n69RwONwhD+Q/iy+I5u7sgDfns1ziH3YFXMF0Fw9fEdqTzVmBgIbwZU1UqsAas7han 6YHGrt0jguwWOkJgngtIYTx40xFOV88voCW07MozL/m7TRTzTEuaPJ20/1HYN0iyA0mz Ds5IxmfybraUJ9lKyHj7Za+SCUKHhiISu6cvt9hfG/rrqD5LkLISNKwA4Pc5nXYxYsxR AWR9oRklHCwiMRwljAIvSz+L01tMAXnVbUeFfwrr0Vkam3/m3PyFbou3L+BYf0XDuFcB id0QA567AuBrUHIphWrTt32eVDWYzg+l0KNiWrff3xgSCnWH3xLjNhe2RVmFx7097KZ3 +/ZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=z7t7LiEHuB86A6W1GOTWH/4DZl9q2t+SzqDgKw5rej4=; b=rMW2KLs19rFYQJrPRz2mHc5N4irpe1Zv06e/x5iO96zl5nFz+LuUY2Fguk7CXz5OcD 5ZzJGLKSyG1viSE97UyxX8t/xcdvN3iSz4nde/6fxzfNdvFaogbcZDQopuCIemeYXtN4 OWQdPAX4J+rGQeUR6Vg0t+oAvc4+O4n+nMhz7w/Izicv+rqmCbYiVFB+MCTRkUvWF+i4 de59BBruO7x2NAyWsvyjbhQxpxgcUCq6jEONRQPaiKrEmV1ke1H7lSpOphmbnmlOCIsD wKfnmjgWsoBevb4Ws9gV5u4OKlb3rki/HAlwUHmheClioA7mzBPHgmmnV/BJanB4Zrlo /wLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ZNdIY1O2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ht22-20020a170907609600b00783ac0b4d32si2520272ejc.941.2022.10.25.02.06.34; Tue, 25 Oct 2022 02:06:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=ZNdIY1O2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231899AbiJYJB3 (ORCPT + 99 others); Tue, 25 Oct 2022 05:01:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231807AbiJYJBU (ORCPT ); Tue, 25 Oct 2022 05:01:20 -0400 Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D86715626D for ; Tue, 25 Oct 2022 02:01:15 -0700 (PDT) Received: from pps.filterd (m0098419.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.17.1.5/8.17.1.5) with ESMTP id 29P7bPRn015825; Tue, 25 Oct 2022 09:01:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding; s=pp1; bh=z7t7LiEHuB86A6W1GOTWH/4DZl9q2t+SzqDgKw5rej4=; b=ZNdIY1O2oCkkbEj8sFwVxfllCC2BgZft4HyM6fBvRhlzQWbcShQD+4rNEXurCl5TZM2j ZbxPj8qc+5/w1SYFbGeIyQEA/6rROQ0KO1lE4D0H6WoP/KJl/aYX3M7Ajwt7y8lfm8Pg m3+LvLV4BonJn4LTzrs3tTckC+6ZA3Qy25oEUpW2btCY40N5RI1Fio1ee34cQz6+qFbO FlNAzpy9rMSwJBYU+b1EsrBWhS8USCIrslx+Hrj2SypCaj9j7hSq3Eazn4aDsHyIeHJU gtczsIMAQTNsVYZ46NuxeE20nai/PLjfInGIZjStzjY5FWlKo5mZqPfGcsHmXeeL0ZIv Aw== Received: from ppma03ams.nl.ibm.com (62.31.33a9.ip4.static.sl-reverse.com [169.51.49.98]) by mx0b-001b2d01.pphosted.com (PPS) with ESMTPS id 3kebjt33sc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 25 Oct 2022 09:01:10 +0000 Received: from pps.filterd (ppma03ams.nl.ibm.com [127.0.0.1]) by ppma03ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 29P8oZi8012771; Tue, 25 Oct 2022 09:01:08 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by ppma03ams.nl.ibm.com with ESMTP id 3kdugasw33-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 25 Oct 2022 09:01:08 +0000 Received: from d06av25.portsmouth.uk.ibm.com (d06av25.portsmouth.uk.ibm.com [9.149.105.61]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 29P916ZW55443772 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 25 Oct 2022 09:01:06 GMT Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3FCF011C064; Tue, 25 Oct 2022 09:01:06 +0000 (GMT) Received: from d06av25.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 02F2A11C050; Tue, 25 Oct 2022 09:01:06 +0000 (GMT) Received: from rims.zurich.ibm.com (unknown [9.4.69.56]) by d06av25.portsmouth.uk.ibm.com (Postfix) with ESMTP; Tue, 25 Oct 2022 09:01:05 +0000 (GMT) From: Bernard Metzler To: linux-kernel@vger.kernel.org Cc: jgg@nvidia.com, leonro@nvidia.com, Bernard Metzler , Olga Kornievskaia Subject: [PATCH] RDMA/siw: Fix immediate work request flush to completion queue. Date: Tue, 25 Oct 2022 11:00:47 +0200 Message-Id: <20221025090047.299004-1-bmt@zurich.ibm.com> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: N5LeHUcJb8GoeCcqbFXY1kx5Y_a-DioU X-Proofpoint-ORIG-GUID: N5LeHUcJb8GoeCcqbFXY1kx5Y_a-DioU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-25_03,2022-10-21_01,2022-06-22_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 malwarescore=0 suspectscore=0 spamscore=0 clxscore=1011 mlxscore=0 adultscore=0 lowpriorityscore=0 mlxlogscore=903 priorityscore=1501 impostorscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2209130000 definitions=main-2210250049 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747649895219872585?= X-GMAIL-MSGID: =?utf-8?q?1747649895219872585?= Correctly set send queue element opcode during immediate work request flushing in post sendqueue operation, if the QP is in ERROR state. An undefined ocode value results in out-of-bounds access to an array for mapping the opcode between siw internal and RDMA core representation in work completion generation. It resulted in a KASAN BUG report of type 'global-out-of-bounds' during NFSoRDMA testing. This patch further fixes a potential case of a malicious user which may write undefined values for completion queue elements status or opcode, if the CQ is memory mapped to user land. It avoids the same out-of-bounds access to arrays for status and opcode mapping as described above. Fixes: 303ae1cdfdf7 ("rdma/siw: application interface") Fixes: b0fff7317bb4 ("rdma/siw: completion queue methods") Reported-by: Olga Kornievskaia Signed-off-by: Bernard Metzler --- drivers/infiniband/sw/siw/siw_cq.c | 24 ++++++++++++++-- drivers/infiniband/sw/siw/siw_verbs.c | 40 ++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/drivers/infiniband/sw/siw/siw_cq.c b/drivers/infiniband/sw/siw/siw_cq.c index d68e37859e73..acc7bcd538b5 100644 --- a/drivers/infiniband/sw/siw/siw_cq.c +++ b/drivers/infiniband/sw/siw/siw_cq.c @@ -56,8 +56,6 @@ int siw_reap_cqe(struct siw_cq *cq, struct ib_wc *wc) if (READ_ONCE(cqe->flags) & SIW_WQE_VALID) { memset(wc, 0, sizeof(*wc)); wc->wr_id = cqe->id; - wc->status = map_cqe_status[cqe->status].ib; - wc->opcode = map_wc_opcode[cqe->opcode]; wc->byte_len = cqe->bytes; /* @@ -71,10 +69,32 @@ int siw_reap_cqe(struct siw_cq *cq, struct ib_wc *wc) wc->wc_flags = IB_WC_WITH_INVALIDATE; } wc->qp = cqe->base_qp; + wc->opcode = map_wc_opcode[cqe->opcode]; + wc->status = map_cqe_status[cqe->status].ib; siw_dbg_cq(cq, "idx %u, type %d, flags %2x, id 0x%pK\n", cq->cq_get % cq->num_cqe, cqe->opcode, cqe->flags, (void *)(uintptr_t)cqe->id); + } else { + /* + * A malicious user may set invalid opcode or + * status in the user mmapped CQE array. + * Sanity check and correct values in that case + * to avoid out-of-bounds access to global arrays + * for opcode and status mapping. + */ + u8 opcode = cqe->opcode; + u16 status = cqe->status; + + if (opcode >= SIW_NUM_OPCODES) { + opcode = 0; + status = IB_WC_GENERAL_ERR; + } else if (status >= SIW_NUM_WC_STATUS) { + status = IB_WC_GENERAL_ERR; + } + wc->opcode = map_wc_opcode[opcode]; + wc->status = map_cqe_status[status].ib; + } WRITE_ONCE(cqe->flags, 0); cq->cq_get++; diff --git a/drivers/infiniband/sw/siw/siw_verbs.c b/drivers/infiniband/sw/siw/siw_verbs.c index 3e814cfb298c..8021fbd004b0 100644 --- a/drivers/infiniband/sw/siw/siw_verbs.c +++ b/drivers/infiniband/sw/siw/siw_verbs.c @@ -676,13 +676,45 @@ static int siw_copy_inline_sgl(const struct ib_send_wr *core_wr, static int siw_sq_flush_wr(struct siw_qp *qp, const struct ib_send_wr *wr, const struct ib_send_wr **bad_wr) { - struct siw_sqe sqe = {}; int rv = 0; while (wr) { - sqe.id = wr->wr_id; - sqe.opcode = wr->opcode; - rv = siw_sqe_complete(qp, &sqe, 0, SIW_WC_WR_FLUSH_ERR); + struct siw_sqe sqe = {}; + + switch (wr->opcode) { + case IB_WR_RDMA_WRITE: + sqe.opcode = SIW_OP_WRITE; + break; + case IB_WR_RDMA_READ: + sqe.opcode = SIW_OP_READ; + break; + case IB_WR_RDMA_READ_WITH_INV: + sqe.opcode = SIW_OP_READ_LOCAL_INV; + break; + case IB_WR_SEND: + sqe.opcode = SIW_OP_SEND; + break; + case IB_WR_SEND_WITH_IMM: + sqe.opcode = SIW_OP_SEND_WITH_IMM; + break; + case IB_WR_SEND_WITH_INV: + sqe.opcode = SIW_OP_SEND_REMOTE_INV; + break; + case IB_WR_LOCAL_INV: + sqe.opcode = SIW_OP_INVAL_STAG; + break; + case IB_WR_REG_MR: + sqe.opcode = SIW_OP_REG_MR; + break; + default: + rv = -EOPNOTSUPP; + break; + } + if (!rv) { + sqe.id = wr->wr_id; + rv = siw_sqe_complete(qp, &sqe, 0, + SIW_WC_WR_FLUSH_ERR); + } if (rv) { if (bad_wr) *bad_wr = wr;