mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Su Hui <suhui@nfschina.com>
To: herbert@gondor.apana.org.au, davem@davemloft.net
Cc: Su Hui <suhui@nfschina.com>,
	linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-janitors@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: [PATCH] crypto: using size_add() for kmalloc()
Date: Mon, 21 Apr 2025 13:51:06 +0800	[thread overview]
Message-ID: <20250421055104.663552-1-suhui@nfschina.com> (raw)

It's safer to use size_add() to replace open-coded aithmetic in allocator
arguments, because size_add() can prevent possible overflow problem.

Signed-off-by: Su Hui <suhui@nfschina.com>
---
 include/crypto/aead.h     | 3 ++-
 include/crypto/akcipher.h | 4 +++-
 include/crypto/kpp.h      | 3 ++-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/crypto/aead.h b/include/crypto/aead.h
index 0e8a41638678..cf212d28fe18 100644
--- a/include/crypto/aead.h
+++ b/include/crypto/aead.h
@@ -10,6 +10,7 @@
 
 #include <linux/atomic.h>
 #include <linux/container_of.h>
+#include <linux/overflow.h>
 #include <linux/crypto.h>
 #include <linux/slab.h>
 #include <linux/types.h>
@@ -433,7 +434,7 @@ static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm,
 {
 	struct aead_request *req;
 
-	req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
+	req = kmalloc(size_add(sizeof(*req), crypto_aead_reqsize(tfm)), gfp);
 
 	if (likely(req))
 		aead_request_set_tfm(req, tfm);
diff --git a/include/crypto/akcipher.h b/include/crypto/akcipher.h
index cdf7da74bf2f..4c37a602cce5 100644
--- a/include/crypto/akcipher.h
+++ b/include/crypto/akcipher.h
@@ -10,6 +10,7 @@
 
 #include <linux/atomic.h>
 #include <linux/crypto.h>
+#include <linux/overflow.h>
 
 /**
  * struct akcipher_request - public key cipher request
@@ -184,7 +185,8 @@ static inline struct akcipher_request *akcipher_request_alloc(
 {
 	struct akcipher_request *req;
 
-	req = kmalloc(sizeof(*req) + crypto_akcipher_reqsize(tfm), gfp);
+	req = kmalloc(size_add(sizeof(*req),
+			       crypto_akcipher_reqsize(tfm)), gfp);
 	if (likely(req))
 		akcipher_request_set_tfm(req, tfm);
 
diff --git a/include/crypto/kpp.h b/include/crypto/kpp.h
index 2d9c4de57b69..11ae1ad41d2a 100644
--- a/include/crypto/kpp.h
+++ b/include/crypto/kpp.h
@@ -11,6 +11,7 @@
 
 #include <linux/atomic.h>
 #include <linux/container_of.h>
+#include <linux/overflow.h>
 #include <linux/crypto.h>
 #include <linux/slab.h>
 
@@ -182,7 +183,7 @@ static inline struct kpp_request *kpp_request_alloc(struct crypto_kpp *tfm,
 {
 	struct kpp_request *req;
 
-	req = kmalloc(sizeof(*req) + crypto_kpp_reqsize(tfm), gfp);
+	req = kmalloc(size_add(sizeof(*req), crypto_kpp_reqsize(tfm)), gfp);
 	if (likely(req))
 		kpp_request_set_tfm(req, tfm);
 
-- 
2.30.2


             reply	other threads:[~2025-04-21  5:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-21  5:51 Su Hui [this message]
2025-04-21  6:27 ` Su Hui
2025-04-21  7:10 ` Christophe JAILLET
2025-04-21  7:43   ` Su Hui
2025-04-21  8:32     ` Christophe JAILLET
2025-04-21  8:46       ` Su Hui
2025-04-21  8:50         ` Su Hui
2025-04-21 12:05 ` Herbert Xu
2025-04-22  1:30   ` Su Hui
2025-04-22 10:24   ` Dan Carpenter
2025-04-22 14:16     ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250421055104.663552-1-suhui@nfschina.com \
    --to=suhui@nfschina.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®