Message ID | 20230224215019.3687-1-kunyu@nfschina.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp140944wrd; Wed, 22 Feb 2023 21:35:14 -0800 (PST) X-Google-Smtp-Source: AK7set9vBXSaNTY/kiEEtbKhB2CvEO9+lkkaEchKk5Xok1GI6tbGdgwpV/x97nOqPVZJu3a+Uw9y X-Received: by 2002:a17:906:150:b0:894:4341:77d6 with SMTP id 16-20020a170906015000b00894434177d6mr17563869ejh.1.1677130513977; Wed, 22 Feb 2023 21:35:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677130513; cv=none; d=google.com; s=arc-20160816; b=EhmOzmeL2lj2SGi2XBGyxeD4uj+9JnZ7x3b/HI/NeBEDnvjWisYwimsu1fmOxmF7ns wiTyRhgBy/uzbTBdd01hdTYC8M9AK8CoHYTrzbUw1gWe3PO95fXKmDuUuGJ8wOM1OUFY ORRfAwC/84g0yWaZxiP/+Tg7znE79qVexIZe+Wp74nu8ur0FCyGoTiFrI0M5zMKUXSqQ TOm4lldmwxmGmoRwNpHv4WIbIylS400y6ZhCVw+7oJEa2j42vjZEbMnQbJMWjXGLcOoo Nu73wtOOFfgt60Oxwu9ti8hL6cuFp0Ltr2RRCSMrseXSKMEvSQMoK9CW8ze9l5gp/qI4 Ub8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=3W+bZC1rcB1CIWF1GZC6W0oxeVkeaDhE95dqzNFokmI=; b=Mxl1+NTdQmpEAJE/r02BBiIMm9LX5LnDyUufvVHYVBcBC7YiGy2tbaoOLcK+Pmpcyl bli2N4pbtTWFYSKXhvHgxMB+Vf2KdNxmJzPDRwa81fdbRxUPr86BPJCgp6ryyn4jWupj cwNvhyjCL4Fq0QRTbJj4sKguwvr/bt1aqmDamu/JvRB/H1USrSGRjpNB5bvBUCZ0pdq8 mNBeZ+BKRScu31Gf9nRKgPZIh10ZB9mQIgs5Q6LjpiyKO+DxQ1piEJApIPowgTAixiBw nztwVxYLuK8x2YiDsBVi6LSR7YNrNnbBOGNXjdlfziEqiLCmJQOWcn25bbJuHTfENB5j I8GQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mt23-20020a170907619700b008b178b41ec1si19617470ejc.284.2023.02.22.21.34.49; Wed, 22 Feb 2023 21:35:13 -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 S233247AbjBWFT2 (ORCPT <rfc822;cambridge8321@gmail.com> + 99 others); Thu, 23 Feb 2023 00:19:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233234AbjBWFTS (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 23 Feb 2023 00:19:18 -0500 Received: from mail.nfschina.com (unknown [42.101.60.237]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FBDA47432; Wed, 22 Feb 2023 21:19:05 -0800 (PST) Received: from localhost (unknown [127.0.0.1]) by mail.nfschina.com (Postfix) with ESMTP id 5896C1A00A14; Thu, 23 Feb 2023 13:19:44 +0800 (CST) X-Virus-Scanned: amavisd-new at nfschina.com Received: from mail.nfschina.com ([127.0.0.1]) by localhost (localhost.localdomain [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yXrtiH0GntNQ; Thu, 23 Feb 2023 13:19:40 +0800 (CST) Received: from localhost.localdomain (unknown [219.141.250.2]) (Authenticated sender: kunyu@nfschina.com) by mail.nfschina.com (Postfix) with ESMTPA id 10B9E1A0092F; Thu, 23 Feb 2023 13:19:40 +0800 (CST) From: Li kunyu <kunyu@nfschina.com> To: herbert@gondor.apana.org.au, davem@davemloft.net, mcoquelin.stm32@gmail.com Cc: linux-crypto@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Li kunyu <kunyu@nfschina.com> Subject: [PATCH] stm32: stm32-hash: Add kmalloc_array allocation check Date: Sat, 25 Feb 2023 05:50:19 +0800 Message-Id: <20230224215019.3687-1-kunyu@nfschina.com> X-Mailer: git-send-email 2.18.2 X-Spam-Status: No, score=0.9 required=5.0 tests=BAYES_00,DATE_IN_FUTURE_24_48, RDNS_NONE,SPF_HELO_NONE,SPF_NONE autolearn=no 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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758598806285808630?= X-GMAIL-MSGID: =?utf-8?q?1758598806285808630?= |
Series |
stm32: stm32-hash: Add kmalloc_array allocation check
|
|
Commit Message
Li kunyu
Feb. 24, 2023, 9:50 p.m. UTC
If rctx->hw_context allocation of the context pointer failed. Returning
-ENOMEM and assigning NULL to the out pointer should improve the
robustness of the function.
Signed-off-by: Li kunyu <kunyu@nfschina.com>
---
drivers/crypto/stm32/stm32-hash.c | 4 ++++
1 file changed, 4 insertions(+)
Comments
On Sat, Feb 25, 2023 at 05:50:19AM +0800, Li kunyu wrote: > If rctx->hw_context allocation of the context pointer failed. Returning > -ENOMEM and assigning NULL to the out pointer should improve the > robustness of the function. > > Signed-off-by: Li kunyu <kunyu@nfschina.com> > --- > drivers/crypto/stm32/stm32-hash.c | 4 ++++ > 1 file changed, 4 insertions(+) > > diff --git a/drivers/crypto/stm32/stm32-hash.c b/drivers/crypto/stm32/stm32-hash.c > index d33006d43f76..18e79f62be96 100644 > --- a/drivers/crypto/stm32/stm32-hash.c > +++ b/drivers/crypto/stm32/stm32-hash.c > @@ -970,6 +970,10 @@ static int stm32_hash_export(struct ahash_request *req, void *out) > rctx->hw_context = kmalloc_array(3 + HASH_CSR_REGISTER_NUMBER, > sizeof(u32), > GFP_KERNEL); > + if (rctx->hw_context == NULL) { > + out = NULL; > + return -ENOMEM; > + } > > preg = rctx->hw_context; Shouldn't we free the hw_context at the end of the function? Why does it even need to be stored in rctx in the first place? Thanks,
On Sat, Feb 25, 2023 at 07:14:29AM +0800, Li kunyu wrote: > > hello senior: > 'rctx->hw_context' is a member of the rctx pointer array object and will pass 'memcpy(out, rctx, sizeof(*rctx)) at the end of the function; ', copied to the out pointer memory, it cannot be released, I think this patch does an allocation check on the 'rctx->hw_context' pointer should help a bit. Ugh that's completely broken. You can't export pointers. I guess this driver needs to be disabled. Thanks,
On Thu, Feb 23, 2023 at 10:33 AM Herbert Xu <herbert@gondor.apana.org.au> wrote: > On Sat, Feb 25, 2023 at 07:14:29AM +0800, Li kunyu wrote: > > > > hello senior: > > 'rctx->hw_context' is a member of the rctx pointer array object and will pass 'memcpy(out, rctx, sizeof(*rctx)) at the end of the function; ', copied to the out pointer memory, it cannot be released, I think this patch does an allocation check on the 'rctx->hw_context' pointer should help a bit. > > Ugh that's completely broken. You can't export pointers. > > I guess this driver needs to be disabled. Added Lionel to the To: line so he can look at this, I guess we should also add an entry to MAINTAINERS so that the reports come to the right address. Can we fix the actual problem? It seems to have been there since the initial submission in 2017. I guess the right fix is to export the *actual* hardware state into "out" and read it back from there instead of copying out the rctx struct. Also .statesize needs to be fixed to correspond to that. I can just use a roof:ed constant size for this. If Lionel does not have time to look at it I can try to come up with a quick fix. Yours, Linus Walleij
hello senior: 'rctx->hw_context' is a member of the rctx pointer array object and will pass 'memcpy(out, rctx, sizeof(*rctx)) at the end of the function; ', copied to the out pointer memory, it cannot be released, I think this patch does an allocation check on the 'rctx->hw_context' pointer should help a bit.
diff --git a/drivers/crypto/stm32/stm32-hash.c b/drivers/crypto/stm32/stm32-hash.c index d33006d43f76..18e79f62be96 100644 --- a/drivers/crypto/stm32/stm32-hash.c +++ b/drivers/crypto/stm32/stm32-hash.c @@ -970,6 +970,10 @@ static int stm32_hash_export(struct ahash_request *req, void *out) rctx->hw_context = kmalloc_array(3 + HASH_CSR_REGISTER_NUMBER, sizeof(u32), GFP_KERNEL); + if (rctx->hw_context == NULL) { + out = NULL; + return -ENOMEM; + } preg = rctx->hw_context;