From patchwork Wed Jun 21 07:21:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13286785 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7CF83EB64DD for ; Wed, 21 Jun 2023 07:21:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 98A1610E3D3; Wed, 21 Jun 2023 07:21:41 +0000 (UTC) Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by gabe.freedesktop.org (Postfix) with ESMTPS id E64B810E3D2 for ; Wed, 21 Jun 2023 07:21:38 +0000 (UTC) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-311183ef595so5486321f8f.0 for ; Wed, 21 Jun 2023 00:21:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687332097; x=1689924097; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=W89qn4/C8LoZMhk8A7/gIp60EbovNS/dN8Aiz2Yljxw=; b=gwX3I3y3JXGrO1fvvhPpZ2E3xlM4/joWTGZoQufhuS1+pPp2RUQ/kSFoMiwJJnceDX uiwyIYTj/8dw1EADck2P0Wv78wiXl2JCfd8UaAb3Sb6Bw5KnCIAJmGkEoTuzRe4itxbn ZuzrG3JukXgGl5rjPqtosS/XigPt+G+VA4r5yAygLs78F+y2V5a5d6B/P+HmFO+IBO6O nHjNb24iEOUUTwZffHQtqmecPCypsQesaFCIXjn96Hdu39OgqWvhWSK/GcLTVAsbCUAY i7bYMSjrKg7UwaxtwcCas54se3kJaakK4Qt6xEDoEiZ1i4Ljit5uxdEfnjeHlFH0qyHG dB4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687332097; x=1689924097; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=W89qn4/C8LoZMhk8A7/gIp60EbovNS/dN8Aiz2Yljxw=; b=IUMQCATE0evr+mLAPfgJWjDNXg1wH45amLcmM2skkDYLWaO3xOIali2Ffwj8rW++4Z lV9Tr2iY+LBDCN08djVQzWklucq+EXKH5UvUgbKDJpvK5cYZlKR4+JmVoF7uEfipLjgk bAt8lN6MlnOzEYv8BZ+RvWE64PQY30Kdbm5YC9BGRWCaumhcAurz5eIlsz0r1i7Mtp8N nodJ/KH12tfFy9boLDgvHHDZdMX2SeaWBkwzF8wvYswWuYwgcyj/tBCsbkvjOWTaOjqt WEvM4bUC+glMsDVHqvfCLWX7cXNJQYSMRYULx1WnLrEi1o6Q9ONYiIutM7BCuc1nbL3y Oo0g== X-Gm-Message-State: AC+VfDzkEiVALpES3H/sMpeM+k1aQtqRmyhs6bR3qRitVVB1TsNj6iOw 3oQrbWXfdPqlWnEaNFrKhtSCrg== X-Google-Smtp-Source: ACHHUZ60HKjNzSz8LdbwOSE2Cw7gl9ezizd0yDDWDAe3HI8NT9eurE9ffYF6AYJtLl+48w22r4UjUw== X-Received: by 2002:a5d:63c5:0:b0:30f:cf93:4bb8 with SMTP id c5-20020a5d63c5000000b0030fcf934bb8mr10906719wrw.57.1687332096781; Wed, 21 Jun 2023 00:21:36 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id f12-20020a5d58ec000000b00309382eb047sm3626326wrd.112.2023.06.21.00.21.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 00:21:35 -0700 (PDT) Date: Wed, 21 Jun 2023 10:21:31 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 1/5] accel/qaic: tighten bounds checking in encode_message() Message-ID: <8dc35a68-7257-41ac-9057-7c89b9ad6e18@moroto.mountain> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email haha only kidding X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-msm@vger.kernel.org, Oded Gabbay , kernel-janitors@vger.kernel.org, dri-devel@lists.freedesktop.org, Pranjal Ramajor Asha Kanojiya , Stanislaw Gruszka , Carl Vanderlip , Jacek Lawrynowicz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" There are several issues in this code. The check at the start of the loop: if (user_len >= user_msg->len) { This check does not ensure that we have enough space for the trans_hdr (8 bytes). Instead the check needs to be: if (user_len >= user_msg->len - sizeof(*trans_hdr)) { That subtraction is done as an unsigned long we want to avoid negatives. Add a lower bound to the start of the function. if (user_msg->len < sizeof(*trans_hdr)) There is a second integer underflow which can happen if trans_hdr->len is zero inside the encode_passthrough() function. memcpy(out_trans->data, in_trans->data, in_trans->hdr.len - sizeof(in_trans->hdr)); Instead of adding a check to encode_passthrough() it's better to check in this central place. Add that check: if (trans_hdr->len < sizeof(trans_hdr) The final concern is that the "user_len + trans_hdr->len" might have an integer overflow bug. Use size_add() to prevent that. - if (user_len + trans_hdr->len > user_msg->len) { + if (size_add(user_len, trans_hdr->len) > user_msg->len) { Fixes: 129776ac2e38 ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter --- This is based on code review and not tested. drivers/accel/qaic/qaic_control.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/accel/qaic/qaic_control.c b/drivers/accel/qaic/qaic_control.c index 5c57f7b4494e..a51b1594dcfa 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -748,7 +748,8 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg, int ret; int i; - if (!user_msg->count) { + if (!user_msg->count || + user_msg->len < sizeof(*trans_hdr)) { ret = -EINVAL; goto out; } @@ -765,12 +766,13 @@ static int encode_message(struct qaic_device *qdev, struct manage_msg *user_msg, } for (i = 0; i < user_msg->count; ++i) { - if (user_len >= user_msg->len) { + if (user_len >= user_msg->len - sizeof(*trans_hdr)) { ret = -EINVAL; break; } trans_hdr = (struct qaic_manage_trans_hdr *)(user_msg->data + user_len); - if (user_len + trans_hdr->len > user_msg->len) { + if (trans_hdr->len < sizeof(trans_hdr) || + size_add(user_len, trans_hdr->len) > user_msg->len) { ret = -EINVAL; break; } From patchwork Wed Jun 21 07:21:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13286786 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 37068EB64D7 for ; Wed, 21 Jun 2023 07:21:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8CF6D10E3D5; Wed, 21 Jun 2023 07:21:56 +0000 (UTC) Received: from mail-lf1-x12c.google.com (mail-lf1-x12c.google.com [IPv6:2a00:1450:4864:20::12c]) by gabe.freedesktop.org (Postfix) with ESMTPS id 6132010E3D5 for ; Wed, 21 Jun 2023 07:21:54 +0000 (UTC) Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-4f870247d6aso4572206e87.0 for ; Wed, 21 Jun 2023 00:21:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687332112; x=1689924112; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=CZmvVILTRUYuuna4kyNwmHz+05y6e9HA767/dDY6Eos=; b=j4sioODyzUl2U16EBi+muzNb7LYcgJA6zqvzuGHn+vI7biXRHaBxjp/KvIY5JGYSg1 V93xDmp5YKIUTvcxnsVUhGgG0O8B3tYnuy4qH3/FXPcYRdlT1lSZgsmssFhLpTwE84Ok WBAibyM4ulyOyRGMKglLCtJvNULodeek/M1GGq/CWvA1s0O1qezOrquDPGL5ifyeiqoD oLP+RFptBUgGSCZWhn6n/s9zBPo1as5FzLKJjepV0GpHUwx7IX9RYvV8QU2zY/IraA5X +RB3O+kMVFcbpy9ptWU4cpYRPtBNhzlg/U2+sKbhDxNhwYmFSaXzh0VkNQIBp92AulL8 tDyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687332112; x=1689924112; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=CZmvVILTRUYuuna4kyNwmHz+05y6e9HA767/dDY6Eos=; b=IJnZ09Kap6EBvgolW0u5G6sVnAs3AEjZkqvF+m2+JGlmyPxTskrJncFmIY3gf1+tL+ 3fPHwowDzZHaQC57UqZES8NB0p1c7gxHcjE5ROiodcQog6oio92bkOYMFmTm+fRMaHmo p74kCG9pfRJcIvfC1GCHqZCxBRh5MM8ZARejMM5qZ0EC7kLmyMC0EofxoV1/p3tBxLU+ FMao60HqkEYcyDLiS/iOD7HVBesHYikqMHAYcotojc5EKT1amwQNxF9hKf+TdggGVdqK GNBigQAFW7fugBXvp/Imyn60GdPZ9bTOuYOXS15ZKrPjNOCU3yXSLZpzOTPlH16+RTM+ Hx9Q== X-Gm-Message-State: AC+VfDxSerztAsqKy2tciAvwMu4lV/vG/kuiWg0hEDg0eYJCR+69WENW dG9r7yyNxgAv8tlsNjPbHaulDQ== X-Google-Smtp-Source: ACHHUZ5i3hbZ+EY2u3cWMm7/VxqrPuRBkipz14YmZjC2dPxHjO1DIo3PL18tiL8tunWtmNyDMhgh3g== X-Received: by 2002:a19:5f4f:0:b0:4f9:5718:70b0 with SMTP id a15-20020a195f4f000000b004f9571870b0mr1039020lfj.41.1687332112158; Wed, 21 Jun 2023 00:21:52 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id j10-20020a5d604a000000b0031272fced4dsm3671556wrt.52.2023.06.21.00.21.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 00:21:50 -0700 (PDT) Date: Wed, 21 Jun 2023 10:21:47 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 2/5] accel/qaic: tighten bounds checking in decode_message() Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email haha only kidding X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-msm@vger.kernel.org, Oded Gabbay , kernel-janitors@vger.kernel.org, dri-devel@lists.freedesktop.org, Pranjal Ramajor Asha Kanojiya , Carl Vanderlip , Jacek Lawrynowicz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Copy the bounds checking from encode_message() to decode_message(). This patch addresses the following concerns. Ensure that there is enough space for at least one header so that we don't have a negative size later. if (msg_hdr_len < sizeof(*trans_hdr)) Ensure that we have enough space to read the next header from the msg->data. if (msg_len >= msg_hdr_len - sizeof(*trans_hdr)) return -EINVAL; Check that the trans_hdr->len is not below the minimum size: if (hdr_len < sizeof(*trans_hdr)) This minimum check ensures that we don't corrupt memory in decode_passthrough() when we do. memcpy(out_trans->data, in_trans->data, len - sizeof(in_trans->hdr)); And finally, use size_add() to prevent an integer overflow: if (size_add(msg_len, hdr_len) > msg_hdr_len) Fixes: 129776ac2e38 ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter --- drivers/accel/qaic/qaic_control.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/accel/qaic/qaic_control.c b/drivers/accel/qaic/qaic_control.c index a51b1594dcfa..78f6c3d6380d 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -955,15 +955,23 @@ static int decode_message(struct qaic_device *qdev, struct manage_msg *user_msg, int ret; int i; - if (msg_hdr_len > QAIC_MANAGE_MAX_MSG_LENGTH) + if (msg_hdr_len < sizeof(*trans_hdr) || + msg_hdr_len > QAIC_MANAGE_MAX_MSG_LENGTH) return -EINVAL; user_msg->len = 0; user_msg->count = le32_to_cpu(msg->hdr.count); for (i = 0; i < user_msg->count; ++i) { + u32 hdr_len; + + if (msg_len >= msg_hdr_len - sizeof(*trans_hdr)) + return -EINVAL; + trans_hdr = (struct wire_trans_hdr *)(msg->data + msg_len); - if (msg_len + le32_to_cpu(trans_hdr->len) > msg_hdr_len) + hdr_len = le32_to_cpu(trans_hdr->len); + if (hdr_len < sizeof(*trans_hdr) || + size_add(msg_len, hdr_len) > msg_hdr_len) return -EINVAL; switch (le32_to_cpu(trans_hdr->type)) { From patchwork Wed Jun 21 07:22:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13286787 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 823E0EB64D7 for ; Wed, 21 Jun 2023 07:22:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id E280D10E3D4; Wed, 21 Jun 2023 07:22:11 +0000 (UTC) Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by gabe.freedesktop.org (Postfix) with ESMTPS id EE26810E3D4 for ; Wed, 21 Jun 2023 07:22:09 +0000 (UTC) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3f9b0f139feso35621075e9.3 for ; Wed, 21 Jun 2023 00:22:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687332128; x=1689924128; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=+b4GexZhYGtFJ8457FP9Uyc3mQnN692CXOs2Lb8cwgE=; b=aa4DKZw2bQK9zS+DjaaGo5gMxSO32Ta7cm5CQh2RCsjj7hGF1y1tbA/iJpqtbb9Boj 0TPd6+O//mPvdUgmcs1euxuikN4j8opHMVLsK6l5PrqR8vjlb7bRpiyT7PvGB8wra0MI wcUSMrnbJdhqhXlhVvQKeJrKd9kYs4BJLJXzBB+0Q+QSsoS65z+Njd5teoo5vMmVQbKO 0Kp6OLBrDJqeTk+j/QbIOIg+HFJA7ZrNzexR3mCJOdAbV8sAAbNVLSr4tzYUwcMSEnYA /pWDSiVN22JHOVnJkyIxKdqwLmeYLGCQhD3adetc18073RlU9i2w8FFwSJddurgqTZko 6xQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687332128; x=1689924128; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+b4GexZhYGtFJ8457FP9Uyc3mQnN692CXOs2Lb8cwgE=; b=YWF+GhyTEjoy9wOMsydYSfV4SP8y/sWDsd9m+n5R+DX60Zbq710u3VaDQIDN5XK/yy bU2xd0aiGkgBSTH3YPsTOObE42LBJ7UrElTGjXQ5ZXOKQIn9QYPhwYXY/TsKU1qB2Unk KmUcMQTiHt13vlf6un3+M9YKjuEpYRnUxen8Wy/0kuaLkc8Q9AbKilMrEHwqpYc1iFeJ Ndsv5EERDzKXUGDCJFYjOhJs5uMAlbgCmKSOCobMkbO/qHLmpeHEAJs+utf4+cHuoby7 E+jOycAS5gdIw/7a00QHLQx7hF/3mwdEvoC2m1TsNsF1iOTFHAwYETbpwDoABIj6R6HF zzag== X-Gm-Message-State: AC+VfDw6JHud9LB1WAvLSvZrojzaDXF/AB+QjphQWoVDEsufG8nlxfmG EngM1KuP8tuMu06aClTQepXuEQ== X-Google-Smtp-Source: ACHHUZ53CCuxV6ZTQUPnw/p+BKD5sESTMplQpXucrjx+ujYgMjcEMKjr5ub7OUGfbA3GewP+jnCn6Q== X-Received: by 2002:a1c:7906:0:b0:3f9:3853:9d6a with SMTP id l6-20020a1c7906000000b003f938539d6amr7616012wme.12.1687332128199; Wed, 21 Jun 2023 00:22:08 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id t20-20020a1c7714000000b003f900678815sm4109190wmi.39.2023.06.21.00.22.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 00:22:06 -0700 (PDT) Date: Wed, 21 Jun 2023 10:22:03 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 3/5] accel/qaic: Add consistent integer overflow checks Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email haha only kidding X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-msm@vger.kernel.org, Oded Gabbay , kernel-janitors@vger.kernel.org, dri-devel@lists.freedesktop.org, Pranjal Ramajor Asha Kanojiya , Stanislaw Gruszka , Carl Vanderlip , Jacek Lawrynowicz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The encode_dma() function has integer overflow checks. The encode_passthrough(), encode_activate() and encode_status() functions did not. I added integer overflow checking everywhere. I also updated the integer overflow checking in encode_dma() to use size_add() so everything is consistent. Fixes: 129776ac2e38 ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter Reviewed-by: Jeffrey Hugo Reviewed-by: Pranjal Ramajor Asha Kanojiya --- drivers/accel/qaic/qaic_control.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/accel/qaic/qaic_control.c b/drivers/accel/qaic/qaic_control.c index 78f6c3d6380d..96a26539df18 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -366,7 +366,7 @@ static int encode_passthrough(struct qaic_device *qdev, void *trans, struct wrap if (in_trans->hdr.len % 8 != 0) return -EINVAL; - if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_EXT_MSG_LENGTH) + if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_EXT_MSG_LENGTH) return -ENOSPC; trans_wrapper = add_wrapper(wrappers, @@ -557,12 +557,10 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list msg = &wrapper->msg; msg_hdr_len = le32_to_cpu(msg->hdr.len); - if (msg_hdr_len > (UINT_MAX - QAIC_MANAGE_EXT_MSG_LENGTH)) - return -EINVAL; - /* There should be enough space to hold at least one ASP entry. */ - if (msg_hdr_len + sizeof(*out_trans) + sizeof(struct wire_addr_size_pair) > - QAIC_MANAGE_EXT_MSG_LENGTH) + if (size_add(msg_hdr_len, + sizeof(*out_trans) + sizeof(struct wire_addr_size_pair)) > + QAIC_MANAGE_EXT_MSG_LENGTH) return -ENOMEM; if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size) @@ -634,7 +632,7 @@ static int encode_activate(struct qaic_device *qdev, void *trans, struct wrapper msg = &wrapper->msg; msg_hdr_len = le32_to_cpu(msg->hdr.len); - if (msg_hdr_len + sizeof(*out_trans) > QAIC_MANAGE_MAX_MSG_LENGTH) + if (size_add(msg_hdr_len, sizeof(*out_trans)) > QAIC_MANAGE_MAX_MSG_LENGTH) return -ENOSPC; if (!in_trans->queue_size) @@ -718,7 +716,7 @@ static int encode_status(struct qaic_device *qdev, void *trans, struct wrapper_l msg = &wrapper->msg; msg_hdr_len = le32_to_cpu(msg->hdr.len); - if (msg_hdr_len + in_trans->hdr.len > QAIC_MANAGE_MAX_MSG_LENGTH) + if (size_add(msg_hdr_len, in_trans->hdr.len) > QAIC_MANAGE_MAX_MSG_LENGTH) return -ENOSPC; trans_wrapper = add_wrapper(wrappers, sizeof(*trans_wrapper)); From patchwork Wed Jun 21 07:22:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13286788 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 18324EB64D7 for ; Wed, 21 Jun 2023 07:22:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6135B10E3D2; Wed, 21 Jun 2023 07:22:27 +0000 (UTC) Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by gabe.freedesktop.org (Postfix) with ESMTPS id C79E810E3D2 for ; Wed, 21 Jun 2023 07:22:25 +0000 (UTC) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-3f90b8acefdso32879305e9.1 for ; Wed, 21 Jun 2023 00:22:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687332144; x=1689924144; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=6LvoAnY92IhuYKxUcIFqiF0B336ukPa8jJhL/uMvwzE=; b=fqb+Lk5qFDdI8tsOo384wsahnjd07gLKH1f6HgdRNIZpDEu+zO/YTZuQ+XhyUm9pmz 4wK6gc/xZRmxT+Km9H9lXKMZT2or6+NA5NV/jRz9cP6EmBe9LuOdxICsvgPUs2qXU9Uv C5lFzWsrnwKKWtNg0XXySJ/s4z5KGXWz9pXGESAOmufwh5ZplHXxaOjS4oO1KlEXpOy5 RmhJ+ZatkFLeblkVSgBQwQc7S81DhxysHCXiI/Cs/wGlRSNMsNUaSRRM4/1YeJmJYQrr 3noxF2mMjzVStM7K7wELhUlk6eCqHCXBXl2RNoiI1v4ZHKChof/0ugZk2t7Ue+bsE2N+ 6YIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687332144; x=1689924144; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6LvoAnY92IhuYKxUcIFqiF0B336ukPa8jJhL/uMvwzE=; b=JlsA8BhzWKciWYBpdh4hziND47ym0Rc2hVj8FhQZT9FDFRH5145UXRmc+hiYQcoyHn HI/oIQAhe7XuHrDlSk9EnNO47U+E7HuFCEErbGq1NP9hQnScLzZ0JUtfmMDmbEc0SDmg tkzIGjDFvTDj0Yza2AXpzvpRTfE9m9vvvGuNSAx74h6UG0xAqwESLiUrTogoZJwnpsF2 AnPvhOWVEmgVKSS7Z1U/IDkdP8TRFAky3eH6gMGqqUxtEHpy3VL27uaBVGl5CVOl5Xi0 Z+9uD7Kl8RC0gqWMHQH42nPPRHjMz5/00NoC74gkuGSewBpCmrYtiukSCyJUoj9Sren8 DCtQ== X-Gm-Message-State: AC+VfDxQGUjKdA8/up+ytPNHBVTSkGYgAk33Im9RXQq4hof1pczU1j+7 k2Pflz8VfXHz/+ee9koKsvY0bw== X-Google-Smtp-Source: ACHHUZ4OLioLcbMLtIFKz0PjiB4ajmxMhMKaALqVWaK0zxD//PQ2N3/uhDiKEOVC0pJwRkD5gwXahA== X-Received: by 2002:a05:600c:211a:b0:3f6:e59:c04c with SMTP id u26-20020a05600c211a00b003f60e59c04cmr10030649wml.24.1687332143848; Wed, 21 Jun 2023 00:22:23 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id l5-20020a1ced05000000b003f70a7b4537sm15211480wmh.36.2023.06.21.00.22.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 00:22:22 -0700 (PDT) Date: Wed, 21 Jun 2023 10:22:18 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 4/5] accel/qaic: move and expand integer overflow checks for map_user_pages() Message-ID: <435abccc-9251-4c27-9b35-8fdf4bbd2433@moroto.mountain> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email haha only kidding X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-msm@vger.kernel.org, Oded Gabbay , kernel-janitors@vger.kernel.org, dri-devel@lists.freedesktop.org, Pranjal Ramajor Asha Kanojiya , Stanislaw Gruszka , Carl Vanderlip , Jacek Lawrynowicz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The integer overflow checking for find_and_map_user_pages() was done in encode_dma(). Presumably this was to do it before the allocation. But it's not super important that the failure path is a fast path and it hurts readability to put the check so far from the where the variable is used. Move the check to find_and_map_user_pages() instead and add some more additional potential integer overflow checks. Fixes: 129776ac2e38 ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter --- I kind of went to town adding integer overflow checks here. Please, review this extra carefully. drivers/accel/qaic/qaic_control.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/accel/qaic/qaic_control.c b/drivers/accel/qaic/qaic_control.c index 96a26539df18..03932197f1ac 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -401,6 +401,12 @@ static int find_and_map_user_pages(struct qaic_device *qdev, xfer_start_addr = in_trans->addr + resources->xferred_dma_size; + if (in_trans->size == 0 || + in_trans->addr + in_trans->size < in_trans->addr || + in_trans->addr + resources->xferred_dma_size < in_trans->addr || + in_trans->size + offset_in_page(xfer_start_addr) < resources->xferred_dma_size) + return -EINVAL; + need_pages = DIV_ROUND_UP(in_trans->size + offset_in_page(xfer_start_addr) - resources->xferred_dma_size, PAGE_SIZE); @@ -563,9 +569,6 @@ static int encode_dma(struct qaic_device *qdev, void *trans, struct wrapper_list QAIC_MANAGE_EXT_MSG_LENGTH) return -ENOMEM; - if (in_trans->addr + in_trans->size < in_trans->addr || !in_trans->size) - return -EINVAL; - xfer = kmalloc(sizeof(*xfer), GFP_KERNEL); if (!xfer) return -ENOMEM; From patchwork Wed Jun 21 07:22:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13286789 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A7F7CEB64D8 for ; Wed, 21 Jun 2023 07:22:47 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 9B46710E3D6; Wed, 21 Jun 2023 07:22:46 +0000 (UTC) Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by gabe.freedesktop.org (Postfix) with ESMTPS id 46A6D10E3D6 for ; Wed, 21 Jun 2023 07:22:44 +0000 (UTC) Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-3f9b4a715d9so21246485e9.0 for ; Wed, 21 Jun 2023 00:22:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687332162; x=1689924162; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=Vz2evvxynfo3GpNLQFS4FwB3difDSTskdd0+liW6jTE=; b=DWjFbl9ja+2c9aIXWX3Oh9IqIpBzGQDkRm4zFFdPNcFa8siG6zB+EGrWIJ/7OTxOU+ hfshTn5wArKKPiOka5N5SqyErZkho7Wb/PY27cS4+26IdiqLBQxEYGb9JeRkQ5rwuUr5 POi1K2WtPHVwn0DTPek3aPCkF7gNV131a0W/3dAk54eHga883QzJZXegEasc2AaI1UII 5Sd/Pzl8LWtTfPUNK2eYTxRuq+SXEji6uQBRILIAA8MiX6MTTjQKLfRdrzEyLf0sVRA3 pNaGgYBwQmIfmIzeYo2PLV2zf1syWNu9FD5Q/V1vUne4VspEv2ud3hntSbFrnvLGWy01 h+oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687332162; x=1689924162; h=in-reply-to:content-disposition:mime-version:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Vz2evvxynfo3GpNLQFS4FwB3difDSTskdd0+liW6jTE=; b=AEfaBoAAsP49kYQhKnroC5CFwdPNdp684Mha3ZUnM0577NFe691gPIbDtzs22BVgVT ZDmgLLspN3jMIAGpStiJCNlNwSODpfmHbiiDzksddlI7wmeFiY8R0DUkSYW0tHXxRlre 00jTnenEr8mqQj2ZpthAQ95pywLwS6gHITFqCGz1C4x1yceT3tnCg0atnJy9lv1n90A/ Y5yyr0z32Wc/sqswegHytG1jbRvfZAnVWCmT0nNxfcX9KXjFNNE9BR6JxOHR+pVh+M9h aoBrndnzwAtEmJzKpByy5J5RclLXm7uPgWcEOwY2Pd+YFUch321Ggquj/Xb7eLNn+43Z wl2g== X-Gm-Message-State: AC+VfDx5x9UmUCAwKjQx4T2EOfuE9VQ2pBWOj+uKhpGgH+MV8s9/JqNu HlGqKXmKIuxv3oNhhmF9Ngva4w== X-Google-Smtp-Source: ACHHUZ53ONPx1oNStMjM74vkhEhb3eMLUz1J+VK37q5osvP2XR/IJPwufysddaekoV0Ufnicb76YQg== X-Received: by 2002:a1c:cc07:0:b0:3f7:c92:57a0 with SMTP id h7-20020a1ccc07000000b003f70c9257a0mr13018372wmb.14.1687332162699; Wed, 21 Jun 2023 00:22:42 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id y10-20020a5d4aca000000b0030ae6432504sm3687548wrs.38.2023.06.21.00.22.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Jun 2023 00:22:41 -0700 (PDT) Date: Wed, 21 Jun 2023 10:22:38 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 5/5] accel/qaic: Fix a leak in map_user_pages() Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Mailer: git-send-email haha only kidding X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-msm@vger.kernel.org, Oded Gabbay , kernel-janitors@vger.kernel.org, dri-devel@lists.freedesktop.org, Pranjal Ramajor Asha Kanojiya , Stanislaw Gruszka , Carl Vanderlip , Jacek Lawrynowicz Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" If get_user_pages_fast() allocates some pages but not as many as we wanted, then the current code leaks those pages. Call put_page() on the pages before returning. Fixes: 129776ac2e38 ("accel/qaic: Add control path") Signed-off-by: Dan Carpenter Reviewed-by: Jeffrey Hugo Reviewed-by: Pranjal Ramajor Asha Kanojiya --- drivers/accel/qaic/qaic_control.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/accel/qaic/qaic_control.c b/drivers/accel/qaic/qaic_control.c index 03932197f1ac..7c3f9009617f 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -424,9 +424,12 @@ static int find_and_map_user_pages(struct qaic_device *qdev, } ret = get_user_pages_fast(xfer_start_addr, nr_pages, 0, page_list); - if (ret < 0 || ret != nr_pages) { - ret = -EFAULT; + if (ret < 0) goto free_page_list; + if (ret != nr_pages) { + nr_pages = ret; + ret = -EFAULT; + goto put_pages; } sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);