summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorKees Cook <kees@kernel.org>2026-02-21 10:49:23 +0300
committerKees Cook <kees@kernel.org>2026-02-21 12:02:28 +0300
commit69050f8d6d075dc01af7a5f2f550a8067510366f (patch)
treebb265f94d9dfa7876c06a5d9f88673d496a15341 /crypto
parentd39a1d7486d98668dd34aaa6732aad7977c45f5a (diff)
downloadlinux-69050f8d6d075dc01af7a5f2f550a8067510366f.tar.xz
treewide: Replace kmalloc with kmalloc_obj for non-scalar types
This is the result of running the Coccinelle script from scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to avoid scalar types (which need careful case-by-case checking), and instead replace kmalloc-family calls that allocate struct or union object instances: Single allocations: kmalloc(sizeof(TYPE), ...) are replaced with: kmalloc_obj(TYPE, ...) Array allocations: kmalloc_array(COUNT, sizeof(TYPE), ...) are replaced with: kmalloc_objs(TYPE, COUNT, ...) Flex array allocations: kmalloc(struct_size(PTR, FAM, COUNT), ...) are replaced with: kmalloc_flex(*PTR, FAM, COUNT, ...) (where TYPE may also be *VAR) The resulting allocations no longer return "void *", instead returning "TYPE *". Signed-off-by: Kees Cook <kees@kernel.org>
Diffstat (limited to 'crypto')
-rw-r--r--crypto/af_alg.c2
-rw-r--r--crypto/algboss.c4
-rw-r--r--crypto/algif_rng.c2
-rw-r--r--crypto/api.c2
-rw-r--r--crypto/asymmetric_keys/asymmetric_type.c2
-rw-r--r--crypto/asymmetric_keys/pkcs7_parser.c14
-rw-r--r--crypto/asymmetric_keys/pkcs8_parser.c2
-rw-r--r--crypto/asymmetric_keys/x509_cert_parser.c8
-rw-r--r--crypto/asymmetric_keys/x509_public_key.c2
-rw-r--r--crypto/deflate.c2
-rw-r--r--crypto/drbg.c4
-rw-r--r--crypto/ecc.c2
-rw-r--r--crypto/gcm.c2
-rw-r--r--crypto/simd.c4
-rw-r--r--crypto/tcrypt.c12
-rw-r--r--crypto/testmgr.c10
-rw-r--r--crypto/zstd.c2
17 files changed, 37 insertions, 39 deletions
diff --git a/crypto/af_alg.c b/crypto/af_alg.c
index e468714f539d..d6f14649bd13 100644
--- a/crypto/af_alg.c
+++ b/crypto/af_alg.c
@@ -70,7 +70,7 @@ int af_alg_register_type(const struct af_alg_type *type)
goto unlock;
}
- node = kmalloc(sizeof(*node), GFP_KERNEL);
+ node = kmalloc_obj(*node, GFP_KERNEL);
err = -ENOMEM;
if (!node)
goto unlock;
diff --git a/crypto/algboss.c b/crypto/algboss.c
index 846f586889ee..09ceeb0db431 100644
--- a/crypto/algboss.c
+++ b/crypto/algboss.c
@@ -84,7 +84,7 @@ static int cryptomgr_schedule_probe(struct crypto_larval *larval)
if (!try_module_get(THIS_MODULE))
goto err;
- param = kzalloc(sizeof(*param), GFP_KERNEL);
+ param = kzalloc_obj(*param, GFP_KERNEL);
if (!param)
goto err_put_module;
@@ -195,7 +195,7 @@ static int cryptomgr_schedule_test(struct crypto_alg *alg)
if (!try_module_get(THIS_MODULE))
goto err;
- param = kzalloc(sizeof(*param), GFP_KERNEL);
+ param = kzalloc_obj(*param, GFP_KERNEL);
if (!param)
goto err_put_module;
diff --git a/crypto/algif_rng.c b/crypto/algif_rng.c
index 1a86e40c8372..8578b9d1c569 100644
--- a/crypto/algif_rng.c
+++ b/crypto/algif_rng.c
@@ -202,7 +202,7 @@ static void *rng_bind(const char *name, u32 type, u32 mask)
struct rng_parent_ctx *pctx;
struct crypto_rng *rng;
- pctx = kzalloc(sizeof(*pctx), GFP_KERNEL);
+ pctx = kzalloc_obj(*pctx, GFP_KERNEL);
if (!pctx)
return ERR_PTR(-ENOMEM);
diff --git a/crypto/api.c b/crypto/api.c
index 05629644a688..b0b69ca789f3 100644
--- a/crypto/api.c
+++ b/crypto/api.c
@@ -105,7 +105,7 @@ struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
{
struct crypto_larval *larval;
- larval = kzalloc(sizeof(*larval), GFP_KERNEL);
+ larval = kzalloc_obj(*larval, GFP_KERNEL);
if (!larval)
return ERR_PTR(-ENOMEM);
diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
index 1f1d92547dfc..33c9e68ab28d 100644
--- a/crypto/asymmetric_keys/asymmetric_type.c
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -486,7 +486,7 @@ static struct key_restriction *asymmetric_restriction_alloc(
struct key *key)
{
struct key_restriction *keyres =
- kzalloc(sizeof(struct key_restriction), GFP_KERNEL);
+ kzalloc_obj(struct key_restriction, GFP_KERNEL);
if (!keyres)
return ERR_PTR(-ENOMEM);
diff --git a/crypto/asymmetric_keys/pkcs7_parser.c b/crypto/asymmetric_keys/pkcs7_parser.c
index db1c90ca6fc1..ab400d62c587 100644
--- a/crypto/asymmetric_keys/pkcs7_parser.c
+++ b/crypto/asymmetric_keys/pkcs7_parser.c
@@ -133,17 +133,16 @@ struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
int ret;
- ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
+ ctx = kzalloc_obj(struct pkcs7_parse_context, GFP_KERNEL);
if (!ctx)
goto out_no_ctx;
- ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
+ ctx->msg = kzalloc_obj(struct pkcs7_message, GFP_KERNEL);
if (!ctx->msg)
goto out_no_msg;
- ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
+ ctx->sinfo = kzalloc_obj(struct pkcs7_signed_info, GFP_KERNEL);
if (!ctx->sinfo)
goto out_no_sinfo;
- ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
- GFP_KERNEL);
+ ctx->sinfo->sig = kzalloc_obj(struct public_key_signature, GFP_KERNEL);
if (!ctx->sinfo->sig)
goto out_no_sig;
@@ -729,11 +728,10 @@ int pkcs7_note_signed_info(void *context, size_t hdrlen,
sinfo->index = ++ctx->sinfo_index;
*ctx->ppsinfo = sinfo;
ctx->ppsinfo = &sinfo->next;
- ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
+ ctx->sinfo = kzalloc_obj(struct pkcs7_signed_info, GFP_KERNEL);
if (!ctx->sinfo)
return -ENOMEM;
- ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
- GFP_KERNEL);
+ ctx->sinfo->sig = kzalloc_obj(struct public_key_signature, GFP_KERNEL);
if (!ctx->sinfo->sig)
return -ENOMEM;
return 0;
diff --git a/crypto/asymmetric_keys/pkcs8_parser.c b/crypto/asymmetric_keys/pkcs8_parser.c
index 105dcce27f71..c7f04dbadbd1 100644
--- a/crypto/asymmetric_keys/pkcs8_parser.c
+++ b/crypto/asymmetric_keys/pkcs8_parser.c
@@ -103,7 +103,7 @@ static struct public_key *pkcs8_parse(const void *data, size_t datalen)
memset(&ctx, 0, sizeof(ctx));
ret = -ENOMEM;
- ctx.pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
+ ctx.pub = kzalloc_obj(struct public_key, GFP_KERNEL);
if (!ctx.pub)
goto error;
diff --git a/crypto/asymmetric_keys/x509_cert_parser.c b/crypto/asymmetric_keys/x509_cert_parser.c
index 2fe094f5caf3..ba446683358a 100644
--- a/crypto/asymmetric_keys/x509_cert_parser.c
+++ b/crypto/asymmetric_keys/x509_cert_parser.c
@@ -65,16 +65,16 @@ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
struct asymmetric_key_id *kid;
long ret;
- cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
+ cert = kzalloc_obj(struct x509_certificate, GFP_KERNEL);
if (!cert)
return ERR_PTR(-ENOMEM);
- cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
+ cert->pub = kzalloc_obj(struct public_key, GFP_KERNEL);
if (!cert->pub)
return ERR_PTR(-ENOMEM);
- cert->sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL);
+ cert->sig = kzalloc_obj(struct public_key_signature, GFP_KERNEL);
if (!cert->sig)
return ERR_PTR(-ENOMEM);
- ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);
+ ctx = kzalloc_obj(struct x509_parse_context, GFP_KERNEL);
if (!ctx)
return ERR_PTR(-ENOMEM);
diff --git a/crypto/asymmetric_keys/x509_public_key.c b/crypto/asymmetric_keys/x509_public_key.c
index b695e59fd9e4..9a936e255397 100644
--- a/crypto/asymmetric_keys/x509_public_key.c
+++ b/crypto/asymmetric_keys/x509_public_key.c
@@ -210,7 +210,7 @@ static int x509_key_preparse(struct key_preparsed_payload *prep)
p = bin2hex(p, q, srlen);
*p = 0;
- kids = kmalloc(sizeof(struct asymmetric_key_ids), GFP_KERNEL);
+ kids = kmalloc_obj(struct asymmetric_key_ids, GFP_KERNEL);
if (!kids)
return -ENOMEM;
kids->id[0] = cert->id;
diff --git a/crypto/deflate.c b/crypto/deflate.c
index a3e1fff55661..644daf558ad0 100644
--- a/crypto/deflate.c
+++ b/crypto/deflate.c
@@ -40,7 +40,7 @@ static void *deflate_alloc_stream(void)
DEFLATE_DEF_MEMLEVEL));
struct deflate_stream *ctx;
- ctx = kvmalloc(struct_size(ctx, workspace, size), GFP_KERNEL);
+ ctx = kvmalloc_flex(*ctx, workspace, size, GFP_KERNEL);
if (!ctx)
return ERR_PTR(-ENOMEM);
diff --git a/crypto/drbg.c b/crypto/drbg.c
index 5e7ed5f5c192..8a915da08fa9 100644
--- a/crypto/drbg.c
+++ b/crypto/drbg.c
@@ -1522,7 +1522,7 @@ static int drbg_init_sym_kernel(struct drbg_state *drbg)
unsigned int alignmask;
char ctr_name[CRYPTO_MAX_ALG_NAME];
- aeskey = kzalloc(sizeof(*aeskey), GFP_KERNEL);
+ aeskey = kzalloc_obj(*aeskey, GFP_KERNEL);
if (!aeskey)
return -ENOMEM;
drbg->priv_data = aeskey;
@@ -1761,7 +1761,7 @@ static inline int __init drbg_healthcheck_sanity(void)
drbg_convert_tfm_core("drbg_nopr_hmac_sha512", &coreref, &pr);
#endif
- drbg = kzalloc(sizeof(struct drbg_state), GFP_KERNEL);
+ drbg = kzalloc_obj(struct drbg_state, GFP_KERNEL);
if (!drbg)
return -ENOMEM;
diff --git a/crypto/ecc.c b/crypto/ecc.c
index 2808b3d5f483..3d07383a26f4 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -98,7 +98,7 @@ struct ecc_point *ecc_alloc_point(unsigned int ndigits)
if (!ndigits)
return NULL;
- p = kmalloc(sizeof(*p), GFP_KERNEL);
+ p = kmalloc_obj(*p, GFP_KERNEL);
if (!p)
return NULL;
diff --git a/crypto/gcm.c b/crypto/gcm.c
index 97716482bed0..154138dbf19a 100644
--- a/crypto/gcm.c
+++ b/crypto/gcm.c
@@ -1098,7 +1098,7 @@ static int __init crypto_gcm_module_init(void)
{
int err;
- gcm_zeroes = kzalloc(sizeof(*gcm_zeroes), GFP_KERNEL);
+ gcm_zeroes = kzalloc_obj(*gcm_zeroes, GFP_KERNEL);
if (!gcm_zeroes)
return -ENOMEM;
diff --git a/crypto/simd.c b/crypto/simd.c
index 2a7549e280ca..e343bda70fc8 100644
--- a/crypto/simd.c
+++ b/crypto/simd.c
@@ -145,7 +145,7 @@ struct simd_skcipher_alg *simd_skcipher_create_compat(struct skcipher_alg *ialg,
struct skcipher_alg *alg;
int err;
- salg = kzalloc(sizeof(*salg), GFP_KERNEL);
+ salg = kzalloc_obj(*salg, GFP_KERNEL);
if (!salg) {
salg = ERR_PTR(-ENOMEM);
goto out;
@@ -370,7 +370,7 @@ static struct simd_aead_alg *simd_aead_create_compat(struct aead_alg *ialg,
struct aead_alg *alg;
int err;
- salg = kzalloc(sizeof(*salg), GFP_KERNEL);
+ salg = kzalloc_obj(*salg, GFP_KERNEL);
if (!salg) {
salg = ERR_PTR(-ENOMEM);
goto out;
diff --git a/crypto/tcrypt.c b/crypto/tcrypt.c
index 62fef100e599..4217d3d1ca41 100644
--- a/crypto/tcrypt.c
+++ b/crypto/tcrypt.c
@@ -180,7 +180,7 @@ static int test_mb_aead_jiffies(struct test_mb_aead_data *data, int enc,
int ret = 0;
int *rc;
- rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
+ rc = kzalloc_objs(*rc, num_mb, GFP_KERNEL);
if (!rc)
return -ENOMEM;
@@ -207,7 +207,7 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
int i;
int *rc;
- rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
+ rc = kzalloc_objs(*rc, num_mb, GFP_KERNEL);
if (!rc)
return -ENOMEM;
@@ -270,7 +270,7 @@ static void test_mb_aead_speed(const char *algo, int enc, int secs,
else
e = "decryption";
- data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
+ data = kzalloc_objs(*data, num_mb, GFP_KERNEL);
if (!data)
goto out_free_iv;
@@ -997,7 +997,7 @@ static int test_mb_acipher_jiffies(struct test_mb_skcipher_data *data, int enc,
int ret = 0;
int *rc;
- rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
+ rc = kzalloc_objs(*rc, num_mb, GFP_KERNEL);
if (!rc)
return -ENOMEM;
@@ -1024,7 +1024,7 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
int i;
int *rc;
- rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
+ rc = kzalloc_objs(*rc, num_mb, GFP_KERNEL);
if (!rc)
return -ENOMEM;
@@ -1075,7 +1075,7 @@ static void test_mb_skcipher_speed(const char *algo, int enc, int secs,
else
e = "decryption";
- data = kcalloc(num_mb, sizeof(*data), GFP_KERNEL);
+ data = kzalloc_objs(*data, num_mb, GFP_KERNEL);
if (!data)
return;
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index b940721447fa..c13aa351898d 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -739,7 +739,7 @@ static struct cipher_test_sglists *alloc_cipher_test_sglists(void)
{
struct cipher_test_sglists *tsgls;
- tsgls = kmalloc(sizeof(*tsgls), GFP_KERNEL);
+ tsgls = kmalloc_obj(*tsgls, GFP_KERNEL);
if (!tsgls)
return NULL;
@@ -1796,7 +1796,7 @@ static int test_hash_vs_generic_impl(const char *generic_driver,
return err;
}
- cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
+ cfg = kzalloc_obj(*cfg, GFP_KERNEL);
if (!cfg) {
err = -ENOMEM;
goto out;
@@ -1941,7 +1941,7 @@ static int __alg_test_hash(const struct hash_testvec *vecs,
if (err)
goto out;
- tsgl = kmalloc(sizeof(*tsgl), GFP_KERNEL);
+ tsgl = kmalloc_obj(*tsgl, GFP_KERNEL);
if (!tsgl || init_test_sglist(tsgl) != 0) {
pr_err("alg: hash: failed to allocate test buffers for %s\n",
driver);
@@ -2598,7 +2598,7 @@ static int test_aead_slow(const struct alg_test_desc *test_desc,
if (noslowtests)
return 0;
- ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
+ ctx = kzalloc_obj(*ctx, GFP_KERNEL);
if (!ctx)
return -ENOMEM;
init_rnd_state(&ctx->rng);
@@ -3106,7 +3106,7 @@ static int test_skcipher_vs_generic_impl(const char *generic_driver,
return err;
}
- cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
+ cfg = kzalloc_obj(*cfg, GFP_KERNEL);
if (!cfg) {
err = -ENOMEM;
goto out;
diff --git a/crypto/zstd.c b/crypto/zstd.c
index cbbd0413751a..f0c5c4ee7d29 100644
--- a/crypto/zstd.c
+++ b/crypto/zstd.c
@@ -44,7 +44,7 @@ static void *zstd_alloc_stream(void)
if (!wksp_size)
return ERR_PTR(-EINVAL);
- ctx = kvmalloc(struct_size(ctx, wksp, wksp_size), GFP_KERNEL);
+ ctx = kvmalloc_flex(*ctx, wksp, wksp_size, GFP_KERNEL);
if (!ctx)
return ERR_PTR(-ENOMEM);