From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E640C3A5AA for ; Wed, 4 Sep 2019 18:25:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 418FB208E4 for ; Wed, 4 Sep 2019 18:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567621506; bh=8Z6HD5HY8h6D4M2p5+ETPaKm+ftuV/xNmNbAIFxiHrg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=yZNuLkgoari1io3j2102Gj/bDblPjkEoTkwLfu98kYI8VjSsZtiZEnbbPZcb/1YEg k+tODRNoIVjNlVUAeLna1Gr2cGn/WzNia9rdviRh4vXKW8h9IQEwP/bXAQaZePqYqe MAmG0QHgEBQCVXhedLTbOfENqwfN1Kbsy0KjXWTM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388069AbfIDSAa (ORCPT ); Wed, 4 Sep 2019 14:00:30 -0400 Received: from mail.kernel.org ([198.145.29.99]:39740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388048AbfIDSA3 (ORCPT ); Wed, 4 Sep 2019 14:00:29 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 101E42339D; Wed, 4 Sep 2019 18:00:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620028; bh=8Z6HD5HY8h6D4M2p5+ETPaKm+ftuV/xNmNbAIFxiHrg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lfH+VmUx2R9z00DFh9YPCMz90jBL2z/dYNp8k4T96w76YiTCyB6Yu0oZOYmsc0uIS gaM9d7reTlXvSmhdNKWNdT79/+LJ1ISohe1EHt5BiyGm71DMxaZnRDQ4BtuJWPqJql DAZl3boqiTShF3i8GziCpl37lFCJADkzmY3JVe58= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nicolin Chen , Robin Murphy , Joerg Roedel , Sasha Levin Subject: [PATCH 4.9 47/83] iommu/dma: Handle SG length overflow better Date: Wed, 4 Sep 2019 19:53:39 +0200 Message-Id: <20190904175307.951889740@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175303.488266791@linuxfoundation.org> References: <20190904175303.488266791@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org [ Upstream commit ab2cbeb0ed301a9f0460078e91b09f39958212ef ] Since scatterlist dimensions are all unsigned ints, in the relatively rare cases where a device's max_segment_size is set to UINT_MAX, then the "cur_len + s_length <= max_len" check in __finalise_sg() will always return true. As a result, the corner case of such a device mapping an excessively large scatterlist which is mergeable to or beyond a total length of 4GB can lead to overflow and a bogus truncated dma_length in the resulting segment. As we already assume that any single segment must be no longer than max_len to begin with, this can easily be addressed by reshuffling the comparison. Fixes: 809eac54cdd6 ("iommu/dma: Implement scatterlist segment merging") Reported-by: Nicolin Chen Tested-by: Nicolin Chen Signed-off-by: Robin Murphy Signed-off-by: Joerg Roedel Signed-off-by: Sasha Levin --- drivers/iommu/dma-iommu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 1520e7f02c2f1..89d191b6a0e0f 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -493,7 +493,7 @@ static int __finalise_sg(struct device *dev, struct scatterlist *sg, int nents, * - and wouldn't make the resulting output segment too long */ if (cur_len && !s_iova_off && (dma_addr & seg_mask) && - (cur_len + s_length <= max_len)) { + (max_len - cur_len >= s_length)) { /* ...then concatenate it with the previous one */ cur_len += s_length; } else { -- 2.20.1