Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2280927iof; Wed, 8 Jun 2022 01:18:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwu2N8WYM3D3AW8ucHk4SIFmhFOWFO8TD9vUasZ4ac9cPuTfQJBNbD8dffcUzTDMX8WrosB X-Received: by 2002:a17:90b:4b82:b0:1e6:7853:c004 with SMTP id lr2-20020a17090b4b8200b001e67853c004mr37337932pjb.136.1654676283116; Wed, 08 Jun 2022 01:18:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654676283; cv=none; d=google.com; s=arc-20160816; b=J3gdpI/PY9yEb0n27H5ue2OXp4kGc/0dLeTlm6dn2HsKlW41Pkw+CR3nZxGJBDxtBJ Eccq9jm+NJTOG3Mtccs9k2tL8s6pLxN0VmD8f8JW6vBHeLcHaNUz3snDrL25uajR3SEE VewcxACV9VNXRIVDrUXePkyOHYSSK4VCb8nlu78/0qmth47sIxZlyVoTkEUXf39X1FLt kOmfaPhcIjVyVJGXAO5Ov+sjQsza4p8pQIhlzZW2ss6itnk3tatY2G0/U71ZFQYB0DbU HP9H0NMZ5MHgVxVDv3Vof8ztSUx4SzPnX60oEUszMviK2NXoNNqLTzarR/9hh2sYhDNZ XuXw== 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=SWU0ymqXJYiRkm6LDJUiL4wix7kiRpUWT42znNK0APA=; b=nHlg6RGiGg7904GYLOnsa+7VB4emfC3q5ErYxSYTVN9cwtN+oqaf9gjpJ0yOhWz/DR DIH26OrZ3CbhsWsJBm1FpjUmUKzXIEaa4aI1C48DEg7uVQHZhaSFGwJevQ/XMb/9YsqX SDVQJAmB59mQNOo2rpUd9Wkss57zQXFo36cYGJ8uJeLFd9Nb6QjAEQN5X8in9v7caccz VbTGBBEp7lvzBOg87hQQXiI+V8Agab+jbRQ6HcubXDh0XKfbq3UUPH3gD3XSPjuenpuJ HDd33BHtb8ITUtZBErto/YGSHEnEJ5vpmJjK0TGa/lhDqvzJ14zHiLDsrBgeZWuy8L5D KO8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=W3O9tML2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j6-20020a654306000000b003fa5e1daaa7si25576017pgq.197.2022.06.08.01.18.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jun 2022 01:18:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=W3O9tML2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 B02A33A81E3; Wed, 8 Jun 2022 00:48:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1391687AbiFHBGc (ORCPT + 99 others); Tue, 7 Jun 2022 21:06:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382072AbiFGW3F (ORCPT ); Tue, 7 Jun 2022 18:29:05 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ECE9227237D; Tue, 7 Jun 2022 12:23:36 -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 ams.source.kernel.org (Postfix) with ESMTPS id 377A0B823D8; Tue, 7 Jun 2022 19:23:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 881D4C385A5; Tue, 7 Jun 2022 19:23:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629813; bh=esTM9CTG9/DjUAGs0V6KSfVymumew+ALHoZY1k8oq6g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=W3O9tML2WgKQHTOpZBGYaedjhrsqRR3jtTLdxX7TwcyksNZkkH9PA7LnaLupWIDAQ ryJH+XdlNjXldooLISZ1uODgTUDuP+HvOriS9baOOR6D3rdwXJu1ne+doSLBt+If1I r5cvdEkdRNKWR4Lj+R3xN3KtBMjxKx2j9biSQLpA= 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.18 826/879] iommu/dma: Fix iova map result check bug Date: Tue, 7 Jun 2022 19:05:44 +0200 Message-Id: <20220607165026.829945432@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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 @@ -776,6 +776,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)) @@ -813,8 +814,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; @@ -1209,7 +1210,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);