Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp1030641ybe; Wed, 4 Sep 2019 11:25:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqwUx9J1zvRSQvRIaHYKITwZVqvPp2781RKsVPqt2Ykq6K3NLpVEIGPaxveBCv6NCc1gXb6e X-Received: by 2002:a63:7e1d:: with SMTP id z29mr36358634pgc.346.1567621505223; Wed, 04 Sep 2019 11:25:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567621505; cv=none; d=google.com; s=arc-20160816; b=hltsO/4yu5mIR6VeY9qpL4OuRvOZL5Qj21UEhFPGthNCzs7jHUvZCDZ87tPx+NhppC bwUVYjbCmgOfgdcvCekQ25SBchzmQIkN52k1vePyAuA8xT04obrahRFqbRzsgelPZPVX AjvCcMuv7BBk1Fi/pJ2nfWpQXslNK5F1mw6Sgnc67Sek2tFveyQMwEDLLz1AelOT27oX akDzncVW4Eu8EUxH6z1dQNFzWTYas+/MLyyM+B9St3AQ5RyCq0jORucaJG9v13wVbpq1 vI4+7NLTFFu0FGXUR6I124vix3La1BmC2oVMPYGWIporsWv3+HFgDArWMupg8jhDoEKR Zaug== 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=2fyudYvCHR6pGO06UNm97XhxN2KVgi+L8pBiSZVpois=; b=E9c6OlQZLw2eTsLzGrP95XLRQW7trQfN7xdGFhJbUm3nkiGReriTNvg72CQbJ4ywQ2 ZsZdkZ0mVB+cA5IXlS5iTw35GTvIHKPoj9QJftKYgxezw2F5Cgbp0Y4Chy+YDfR9W46L 8hKK3eiVNftwLWMt/niUz++Y7FsEBma3aVSCXvuhKqXROgMIQV4RZAD4BKLon6dIhw15 6qGWLMqwBag31KwLTDLiXEbNlgIFWYJD+4NSifgRE1B0eKLQv6d2H1X40ISY8sT72iez cwtYHDcqDnYTs9dVp9eKrcOHySMRLJl7QbBJNsee3nYxkIHtebiNnYmkXnh6RhqxVde6 HEkw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="wILye/DV"; 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 co19si18300971plb.242.2019.09.04.11.24.50; Wed, 04 Sep 2019 11:25:05 -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="wILye/DV"; 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 S2389243AbfIDSFX (ORCPT + 99 others); Wed, 4 Sep 2019 14:05:23 -0400 Received: from mail.kernel.org ([198.145.29.99]:46764 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388893AbfIDSFW (ORCPT ); Wed, 4 Sep 2019 14:05:22 -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 9DE852339E; Wed, 4 Sep 2019 18:05:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567620321; bh=XnvH6o0Rl557Wo2DardOGKkFMZFDIAy9WaKXsHFwfoc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wILye/DVJAG1X8AUL6vHj9PJTk+si7kH+0qT6LuYg3JrRU5zDx/UWGydp2D/BMtz4 LtYeDPEBumieGOwoOBYy1dP3M10noHplI9ig682dIQVC4uDdAjbCiZqKCOyPzZRDcT gD5iHR2UXB7dLs3FQilzOii8TIdAPjO7BxPHdabk= 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.19 15/93] iommu/dma: Handle SG length overflow better Date: Wed, 4 Sep 2019 19:53:17 +0200 Message-Id: <20190904175304.464031756@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190904175302.845828956@linuxfoundation.org> References: <20190904175302.845828956@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 511ff9a1d6d94..f9dbb064f9571 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -675,7 +675,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