Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp6255733rwj; Wed, 21 Dec 2022 12:54:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXuGJpsQeqJQ0SPqWnYJM9HO96sEvaAXNTMNHOlL2oguwyOKU+Rf8Np/nz57R6V5uXskwHye X-Received: by 2002:a05:6a20:2d9f:b0:9d:efbe:205b with SMTP id bf31-20020a056a202d9f00b0009defbe205bmr3326527pzb.17.1671656075155; Wed, 21 Dec 2022 12:54:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671656075; cv=none; d=google.com; s=arc-20160816; b=YKP0jbfjYmC99NMJMIbKF6nONM2Oo/GPR+9uSEXnxs9faZo5nmES+7gjXsV26EMcNj T7XYGaRdmDdf1+mtjRu/0W0USkxXZTtRQOPV6811nCN3YlVvuXEpbB6zYM8aVA1D67Ax r2QzoN2YirpbbL4F98eMb58v+ZwJR/hNLnOZfm6hx7g+7N8CsxwBT5p0Z1NVmTh+tSv1 q772KOiK+WXiLV/qyxaYusG29uEGG3B6bi4KdBYYZJ5XISZcrFycj5Bw5GOZ2lFs9/3q K/Ft/UOFy2LPHWyeTq3Uy3j46JR1sEPVgHEUHp0z00e8dHgkekouT3aM9T/t6wO8GQ80 BAdg== 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:dkim-signature; bh=yEx9xr9cFi1LxuTg4xC7cxL0in7WBWqoRHBb2OyjLbI=; b=WfIxFCGKWueAIqYasaNE2ultHXrWTt30+FKn8w2gEfKQV+YZl4IwxnTx9oaEaZ1iJi zThr8e+qKg8VMjK16v8P5u+KuglWzPNFzfdLsyEAqnrghY/k65W875a3HeBCC6pUA3gY ONGqQ2q2Q4JH6wxlaCBVYIwFXCFdQsslL+EGNyls4pggMTtVqd+s8bkVriiN0x1ljg7t NnvX+vpiIFvxX8tCShEMr8eHeosuiYEd8/A54lQAeqj9dbq1ctH62gLH6ich7+Xp5RWR vejbDxLsz2sERRD4c2APRAcZ5aPxceJpkj5g4U2QJjDrFKZD36L3ejHVnFPunIDEtd0/ Yi+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G5jGAqyp; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a15-20020a631a0f000000b00477fbd598aasi18045462pga.346.2022.12.21.12.54.21; Wed, 21 Dec 2022 12:54:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=G5jGAqyp; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229647AbiLUUxf (ORCPT + 99 others); Wed, 21 Dec 2022 15:53:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229620AbiLUUxe (ORCPT ); Wed, 21 Dec 2022 15:53:34 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2839421E11; Wed, 21 Dec 2022 12:53:33 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id B683D60FB5; Wed, 21 Dec 2022 20:53:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A486BC433F0; Wed, 21 Dec 2022 20:53:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671656012; bh=Ei1yd0A82cwikByLCenh3iB6L944+LMrF6j1GxO8bf8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=G5jGAqypOPugBDeBfljlBfqGpIg1CtpmK5YPSiF3ulCeyCVKT1o8fHPY0+KDTUTrd 0QpiL09R+a8OR24QIM+xsNYE1vgL0WE7xahHbhzi6/zZ0UwFGrDJwBH/ZPhsAl/DK9 alY1H80WlcMsYrYAtKd/xvKFJbzX0PLJxuhEn0WOqSr1QLiEAEz27gG3VVNsFckcbf 3m8DDNmvZN1rnsXfTvWmRRG9P3MT4Ac/TTasR6IdGk7A0MgKpDXit2P+LGLbaQ0zXz xvIOraqshLgWFRbBtE0R/ZpfG31ag6KxxAHln38RnLxQyUP6LmQhPjPp21H/i8APsX ycpfem4iJSDug== Date: Wed, 21 Dec 2022 12:53:29 -0800 From: Eric Biggers To: Herbert Xu 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: Re: [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=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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-crypto@vger.kernel.org On Wed, Dec 21, 2022 at 02:53:58PM +0800, Herbert Xu wrote: > 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; That's fine, I guess. One quirk of the above approach is that if the last needed element of the scatterlist has a lot of extra pages, this will iterate through all those extra pages, processing 0 bytes from each. It could just stop when done. I suppose it's not worth worrying about that case, though. - Eric