Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2145415iof; Tue, 7 Jun 2022 21:15:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwdmWcUoJKHFjg8wPicc8w8sKOxES7KA/42W2TQK5SsV2UZFbY+koIK/QTDFYqYyK3bGNBd X-Received: by 2002:a63:474f:0:b0:3fd:6796:835f with SMTP id w15-20020a63474f000000b003fd6796835fmr17629071pgk.3.1654661747323; Tue, 07 Jun 2022 21:15:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654661747; cv=none; d=google.com; s=arc-20160816; b=mrDXKTzuD5E3B4IaQBpB/1hSV5Jq0D4mdbVFoo1EardpE3edpavPwEQGTHS1+ffaur rxIsDv67HmaWxIbmSClgoiKwSKgADKWzs/dFQ9H2sF4oD8Ue+UpVPebjZvK9hifO7V6l CuD41X1wfXK2VEKqJG/zqH+ulhwI4o5QUGtL4Uecq13Sb7zDs4BPIQwA3ZqZMquDZjKA I9ND70HMNnyaWAExBKbgRCipUqVJh5iu0t1aeC2NIi5UuACEQQqw87nG/+fi8lRZE27t P2mHHIcASksWTLyxCq6Elz9cZSdJHsZi36+dZgGm2YBEaRyB2NnBOOAIvQpPBmg335m+ r1wA== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=GAJBda7ci/gObe3ApUXjDbEL2xw0rbOOKma/ZRAw5dk=; b=AYp6KttK9mZ+EpzK6SulXWptwk8YLYsWrwNP46jLuPbfyyk8vxB7palZEstJFrdZGR 6FShSUjLhfR1wxVYKRy4pQhI6B1zykPXPuw+56Py9eTqQDqOVs1Zz1a+4P7b/hM4hB73 7vKnny2nncP6lsMpfe3NxMvQb4rzcL87ar5tMzdN0NZ8lxFyLw6ZgUNLrKOWhdS6mEwn hRD5TLicv+32ydzn57n+W9Sq9kkprxwkqIMpS8SEMBiiWoTxgTaeRgYrdA1Hs8sNCsCS HYL7lDrmP3MGDlvCnZyIOjEcLEMEZWHYAL6t1u/eEOAQ5Ls42Ar3GXmzy3Kc+HjNHNxt 9mWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ikbPEBeb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id o25-20020a637319000000b003f6298dd4e0si29008801pgc.355.2022.06.07.21.15.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 21:15:47 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=ikbPEBeb; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E609A2E93F6; Tue, 7 Jun 2022 20:47:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381728AbiFGVoq (ORCPT + 99 others); Tue, 7 Jun 2022 17:44:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378825AbiFGUwf (ORCPT ); Tue, 7 Jun 2022 16:52:35 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34D3626552; Tue, 7 Jun 2022 11:43:13 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 9E8D461696; Tue, 7 Jun 2022 18:43:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE448C385A2; Tue, 7 Jun 2022 18:43:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654627392; bh=kyppX3RtOX+tPA3S0lL6Sx+Sk8H5k9jY1fHS1FvaZCo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ikbPEBebswI6Ui7TAO3YYRZ6jBRDsVXOjAsATy8zDR3m7GExjk4mevQgs2497eUtM OjY6dMEuPscT2shiaEgasIkqVXeWY8+uLfBN7zorUfR0z5FlAKQJ0OESue3oU6e6qx N/Xk1nqC1qM9ccDYGgiLMTAIjGUDJdq10G2fcAqE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yunfei Wang , Robin Murphy , Miles Chen , Joerg Roedel Subject: [PATCH 5.17 722/772] iommu/dma: Fix iova map result check bug Date: Tue, 7 Jun 2022 19:05:14 +0200 Message-Id: <20220607165010.315631866@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607164948.980838585@linuxfoundation.org> References: <20220607164948.980838585@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 From: Yunfei Wang commit a3884774d731f03d3a3dd4fb70ec2d9341ceb39d upstream. The data type of the return value of the iommu_map_sg_atomic is ssize_t, but the data type of iova size is size_t, e.g. one is int while the other is unsigned int. When iommu_map_sg_atomic return value is compared with iova size, it will force the signed int to be converted to unsigned int, if iova map fails and iommu_map_sg_atomic return error code is less than 0, then (ret < iova_len) is false, which will to cause not do free iova, and the master can still successfully get the iova of map fail, which is not expected. Therefore, we need to check the return value of iommu_map_sg_atomic in two cases according to whether it is less than 0. Fixes: ad8f36e4b6b1 ("iommu: return full error code from iommu_map_sg[_atomic]()") Signed-off-by: Yunfei Wang Cc: # 5.15.* Reviewed-by: Robin Murphy Reviewed-by: Miles Chen Link: https://lore.kernel.org/r/20220507085204.16914-1-yf.wang@mediatek.com Signed-off-by: Joerg Roedel Signed-off-by: Greg Kroah-Hartman --- drivers/iommu/dma-iommu.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -772,6 +772,7 @@ static struct page **__iommu_dma_alloc_n unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; struct page **pages; dma_addr_t iova; + ssize_t ret; if (static_branch_unlikely(&iommu_deferred_attach_enabled) && iommu_deferred_attach(dev, domain)) @@ -809,8 +810,8 @@ static struct page **__iommu_dma_alloc_n arch_dma_prep_coherent(sg_page(sg), sg->length); } - if (iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot) - < size) + ret = iommu_map_sg_atomic(domain, iova, sgt->sgl, sgt->orig_nents, ioprot); + if (ret < 0 || ret < size) goto out_free_sg; sgt->sgl->dma_address = iova; @@ -1207,7 +1208,7 @@ static int iommu_dma_map_sg(struct devic * implementation - it knows better than we do. */ ret = iommu_map_sg_atomic(domain, iova, sg, nents, prot); - if (ret < iova_len) + if (ret < 0 || ret < iova_len) goto out_free_iova; return __finalise_sg(dev, sg, nents, iova);