Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1027230ybe; Wed, 4 Sep 2019 11:21:39 -0700 (PDT) X-Google-Smtp-Source: APXvYqzCuNKWC/rG8z65Rc3sU47URXL4QPCh+KIQ0LAj83DPXGIGWs7SMzZyQg0tnnayvLVqXQN6 X-Received: by 2002:a17:90a:c501:: with SMTP id k1mr6429141pjt.37.1567621299085; Wed, 04 Sep 2019 11:21:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567621299; cv=none; d=google.com; s=arc-20160816; b=s+YojS9Coh1dfEa7srJwcmSHZLRSbed7d9K7LscUebwavg3qFOsa/c0W34BPvBJslJ ToWTP0iw78MQEnlQOb7+wh7xGVBj7u/AIX4J3ij7fwctjD/D9qqErO1NxIU9veXBpp4n P0ySop9NucqX0Azpc4l9WYPiv5SpIcLuSSAkptqqbQQuvhW4aL9x91Yk0IqoX9Bj7Mec Kt8Cse+2g2kislclGK1eVP8BER5sDb8FGhlRLCMKsJwH4wgA4fZaA7ee46ugnYNww8jy 4a3yqoALpjFBs4fPSJtIFGk1KkrcFm8uobU1HPX7nDVYtGBi70YiKmqeIB3xgJM3Eh9a gFWQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=p0HURn0BonnJFWQHe8ZSl2rgLWKSqubrGoD+rhSZ/co=; b=PYsAfqsWuHOmt3sBX0wjr1Bj7PTjOOAnC7qzvPNjNHlwcufMd5OFmpTGjq9i+eul3I L53/py2O8i3qCVAc60wPgNjmKInub8xKCc+9un+nzly2HxFlpHrOhGyIfK6lgClvseAB 63/PKuCVJQbi26i7Qk6mqrXyRr/w6F6TvEGvgDRiYbBWf9sA8tlqppNZlnCq9HXodnnD 8nlXiP7vWZK64LKZY4DfRTE1EEQXnaEZ7NgUcorggn8fngGPh+HxMaXCMQxy1l0zwrBa oTs01hNYcdOyc4xdGSW/aTu+FJF/Bep+WYYlEjwja6QzAmjFGVxGA0gD9Cef1Dn1hcaj 2mTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Sbxst4u1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n10si17969670plk.433.2019.09.04.11.21.23; Wed, 04 Sep 2019 11:21:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Sbxst4u1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390029AbfIDSKB (ORCPT + 99 others); Wed, 4 Sep 2019 14:10:01 -0400 Received: from mail.kernel.org ([198.145.29.99]:53326 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389505AbfIDSJ5 (ORCPT ); Wed, 4 Sep 2019 14:09:57 -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 7B8CA22DBF; Wed, 4 Sep 2019 18:09:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620596; bh=bR2+xtafuPeElELYnLwr7/P4WCwNsYJIMMT6xdDju34=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sbxst4u1Y/5CfYK2aOgWW7ayWKjCmeFPp5Nm0u07/DT2FG8tiU3j22VcTFkrykGXn mEYzfSaCKmbyXtNpFM8nU++QsHNgrgQsJPVJMUK8/OgC+g6w+zMl9jv8xdaUq4R8Q2 gJIZGbaPDPVM9ybAJmarBcWMvG/bqBTrjlcR90jA= 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 5.2 024/143] iommu/dma: Handle SG length overflow better Date: Wed, 4 Sep 2019 19:52:47 +0200 Message-Id: <20190904175315.013334397@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175314.206239922@linuxfoundation.org> References: <20190904175314.206239922@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 379318266468c..8c02d2283d647 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -710,7 +710,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