Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp5245287rwj; Tue, 20 Dec 2022 23:19:50 -0800 (PST) X-Google-Smtp-Source: AMrXdXvKJo7QF6k40Gr6ZBaPdV7gBz1jbX5GBjgYd0yfdgCOCib3ozsu7PuIRkNT6ukRJgtaZd71 X-Received: by 2002:a62:e711:0:b0:57a:84dd:a70c with SMTP id s17-20020a62e711000000b0057a84dda70cmr1385782pfh.15.1671607189906; Tue, 20 Dec 2022 23:19:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671607189; cv=none; d=google.com; s=arc-20160816; b=EkbLLhqfLFBWUqAw4NoExoYhuGMlnCmhJ0NAmSW2UF13EtK7ruhHWvEEgl7whLhzMl PLBkT/eZghI3L21qXaSdxp1CIj/2bDjJRCKZyFxO5y3o9UfBax36dp9hem09ACoOJ+bZ vTNkcI1Lg+GB/1/FXxNnx83H28ITP+D8UlVWz8mI2G4RzyFx52ZBIeBRTuo+1PrD3OGW l8BeVxQczXu+IO6m7TjlyGAQUN8k4Bh4+yDtyZ47V9abo+MCvpAGXz2+NwC4m/XAvWKo WW4/lAUuxrjp05VrdTQx5KDu6agr8jU9wMaWvSWlN2oxrzgDeIHgPnWLQb6eZkH2JQmv psng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=tXBUWCJSW8vq3pLC3iRsSzqtTuHxkwSpE1wcwXb1XUA=; b=YeSfn+njFKgcCcl9NIih6c2PtTMcrdAXuDgizcQuYKDB0rHqwkC7XJraN21MxubMF4 rYs9iyGSf9pSDYiRJn1vPTMqf1j1PXuljaE9A6nS+Hg5ZQRlLbu+ybWK+sjRqMrDgTi6 V1VikCZaqMLUJSUSrDg8le/vILSWvQfE5eKCBFJ3uRbnFAe7T9EN279A2GX3PKG0iRkI 6Hcjzo1K7PCvQY5E5VCCCu2wReLFeLDNL2/zdYT6cKUGRzxRzb2sQ5Xg8jT6G1axT/yA h2JVYFYxjKB3Cfbnct87uIs4qH+/LEQjm/aALsLR4hXevMHRkxZq7+9eNGWqYqC4tAO1 oIzg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a20-20020aa794b4000000b00575deb1844asi14827504pfl.381.2022.12.20.23.19.41; Tue, 20 Dec 2022 23:19:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234100AbiLUGyf (ORCPT + 69 others); Wed, 21 Dec 2022 01:54:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234371AbiLUGy3 (ORCPT ); Wed, 21 Dec 2022 01:54:29 -0500 Received: from formenos.hmeau.com (helcar.hmeau.com [216.24.177.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CA2C51F2F4; Tue, 20 Dec 2022 22:54:27 -0800 (PST) Received: from loth.rohan.me.apana.org.au ([192.168.167.2]) by formenos.hmeau.com with smtp (Exim 4.94.2 #2 (Debian)) id 1p7szG-00989G-S1; Wed, 21 Dec 2022 14:53:59 +0800 Received: by loth.rohan.me.apana.org.au (sSMTP sendmail emulation); Wed, 21 Dec 2022 14:53:58 +0800 Date: Wed, 21 Dec 2022 14:53:58 +0800 From: Herbert Xu To: Eric Biggers Cc: Roberto Sassu , dhowells@redhat.com, davem@davemloft.net, zohar@linux.ibm.com, dmitry.kasatkin@gmail.com, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org, keyrings@vger.kernel.org, linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org, Roberto Sassu , Tadeusz Struk Subject: [v2 PATCH] lib/mpi: Fix buffer overrun when SG is too long Message-ID: References: <20221209150633.1033556-1-roberto.sassu@huaweicloud.com> <0f80852578436dbba7a0fce03d86c3fa2d38c571.camel@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham 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 On Tue, Dec 20, 2022 at 08:30:16PM +0000, Eric Biggers wrote: > > > Tried, could not boot the UML kernel. > > > > After looking, it seems we have to call sg_miter_stop(). Or alternatively, > > we could let sg_miter_next() be called but not writing anything inside the > > loop. > > > > With either of those fixes, the tests pass (using one scatterlist). Thanks for the quick feedback Roberto! > I think it should look like: > > while (nbytes) { > sg_miter_next(&miter); > ... > } > sg_miter_stop(&miter); You're right Eric. However, we could also do it by simply not checking nbytes since we already set nents according to nbytes at the top of the function. ---8<--- The helper mpi_read_raw_from_sgl sets the number of entries in the SG list according to nbytes. However, if the last entry in the SG list contains more data than nbytes, then it may overrun the buffer because it only allocates enough memory for nbytes. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Reported-by: Roberto Sassu Signed-off-by: Herbert Xu diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c index 39c4c6731094..157ef532a6a2 100644 --- a/lib/mpi/mpicoder.c +++ b/lib/mpi/mpicoder.c @@ -504,7 +501,8 @@ MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int nbytes) while (sg_miter_next(&miter)) { buff = miter.addr; - len = miter.length; + len = min_t(unsigned, miter.length, nbytes); + nbytes -= len; for (x = 0; x < len; x++) { a <<= 8; -- Email: Herbert Xu Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt