From patchwork Tue Jul 11 06:12:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Carpenter X-Patchwork-Id: 13308169 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 70F0CC0015E for ; Tue, 11 Jul 2023 06:13:07 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id C764F10E310; Tue, 11 Jul 2023 06:13:06 +0000 (UTC) Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2724810E311 for ; Tue, 11 Jul 2023 06:13:04 +0000 (UTC) Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-3fbf1b82dc7so54808215e9.2 for ; Mon, 10 Jul 2023 23:13:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1689055982; x=1691647982; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=JxKFYbOeCPJfNoXrqBvsNS7aUlyz8/ToZjQNETQXPms=; b=FV97CtS+b2ivv2YRoRawAvvKMw4aLDa7Rm7BXE7rE3Rx9oKkP0hKVpGnlanhx9W4Md UNeGoquhfb66TgF0NvxLwG3gU1FDIa0WerFojuDRIocUcjdhpj1vTd0fPWnlilKDfdSY /di9GEWJlzXRPiTucNo2ocij0y/7vgoWuSqI1HAFtC7y5NqX/awQYh/ttw+QyI256Kz9 1CqUVF0EdRS2WUj/HgHJ706fE8dFJPrrVSDb1OoNeQzRIxkFDn86T4JLJ372TGBBeXaa nNfik6T6DJ80R9Tx0TlnbzsIk2rVCMI0Apym+bBILOB4qR9VZZerFRyqa5+ZLbw9WmH9 bi9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689055982; x=1691647982; h=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=JxKFYbOeCPJfNoXrqBvsNS7aUlyz8/ToZjQNETQXPms=; b=DUZoDqe44DuMMJ4yT/Rv+kPfbDKoTc3BXkMHKAtEhAVSmkgY39kTlcVvTwHYXMl3k4 Alwsjr8Bdox+3BVOWvlyORQ3xr+Z46r8Jh77qaL19hKjSA4aFukEOtKu1+X5HaZRHo1W LeHGeJJCbvIEIFN6bbyDvGNFyC6UW9SgQdV62rf2R7Y+Z5naDkQwcILaAAOXuINsXNoI aA6HwGhvYCDYFQkUMf7Cp9HaCnrIkm2Ovc/4tDEzsxJHHQC4/5SFxm8+rp8+D62BF3G5 b/toWAvVP0GSpV3uhcnKAbsB8g2/g8jUblxscfiDmuI8brdAVuVoYxihp6S3PIkTrJe8 gRLQ== X-Gm-Message-State: ABy/qLYGVtMU7JkbQAP0RYIkhwLp6VkD+n9Sj1VoohBwUfVa9ZymteI1 PLkuejRBoBsbqmb3C24PM3RByQ== X-Google-Smtp-Source: APBJJlFf+n6TNNYgruOps7eyqyNXpCn3snxsTrK5DfRFZuDu9k9jIoV4xxu6En2qffZVVMji6acx6g== X-Received: by 2002:a1c:ed07:0:b0:3fb:d68d:4c6f with SMTP id l7-20020a1ced07000000b003fbd68d4c6fmr15023175wmh.14.1689055982600; Mon, 10 Jul 2023 23:13:02 -0700 (PDT) Received: from localhost ([102.36.222.112]) by smtp.gmail.com with ESMTPSA id k6-20020a05600c0b4600b003fc00702f65sm10890231wmr.46.2023.07.10.23.13.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 23:13:01 -0700 (PDT) Date: Tue, 11 Jul 2023 09:12:58 +0300 From: Dan Carpenter To: Jeffrey Hugo Subject: [PATCH 4/5 v3] accel/qaic: move and expand integer overflow checks for map_user_pages() Message-ID: <2efbbfb0-ca61-49eb-9347-04d8d00f87e1@moroto.mountain> MIME-Version: 1.0 Content-Disposition: inline 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" 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 --- no change 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 23680f5f1902..d5ce36cb351f 100644 --- a/drivers/accel/qaic/qaic_control.c +++ b/drivers/accel/qaic/qaic_control.c @@ -402,6 +402,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); @@ -564,9 +570,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;