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>
This commit is contained in:
Kees Cook
2026-02-20 23:49:23 -08:00
parent d39a1d7486
commit 69050f8d6d
8016 changed files with 20055 additions and 20913 deletions

View File

@@ -592,7 +592,7 @@ smk_import_allocated_label(char *smack, gfp_t gfp)
if (skp != NULL)
goto freeout;
skp = kzalloc(sizeof(*skp), gfp);
skp = kzalloc_obj(*skp, gfp);
if (skp == NULL) {
skp = ERR_PTR(-ENOMEM);
goto freeout;

View File

@@ -372,7 +372,7 @@ static int smk_copy_relabel(struct list_head *nhead, struct list_head *ohead,
struct smack_known_list_elem *oklep;
list_for_each_entry(oklep, ohead, list) {
nklep = kzalloc(sizeof(struct smack_known_list_elem), gfp);
nklep = kzalloc_obj(struct smack_known_list_elem, gfp);
if (nklep == NULL) {
smk_destroy_label_list(nhead);
return -ENOMEM;
@@ -562,7 +562,7 @@ static int smack_add_opt(int token, const char *s, void **mnt_opts)
struct smack_known *skp;
if (!opts) {
opts = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
opts = kzalloc_obj(struct smack_mnt_opts, GFP_KERNEL);
if (!opts)
return -ENOMEM;
*mnt_opts = opts;
@@ -622,7 +622,7 @@ static int smack_fs_context_submount(struct fs_context *fc,
struct smack_mnt_opts *ctx;
struct inode_smack *isp;
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
ctx = kzalloc_obj(*ctx, GFP_KERNEL);
if (!ctx)
return -ENOMEM;
fc->security = ctx;
@@ -673,7 +673,7 @@ static int smack_fs_context_dup(struct fs_context *fc,
if (!src)
return 0;
fc->security = kzalloc(sizeof(struct smack_mnt_opts), GFP_KERNEL);
fc->security = kzalloc_obj(struct smack_mnt_opts, GFP_KERNEL);
if (!fc->security)
return -ENOMEM;
@@ -2817,7 +2817,7 @@ static void smk_ipv6_port_label(struct socket *sock, struct sockaddr *address)
/*
* A new port entry is required.
*/
spp = kzalloc(sizeof(*spp), GFP_KERNEL);
spp = kzalloc_obj(*spp, GFP_KERNEL);
if (spp == NULL)
return;

View File

@@ -682,7 +682,7 @@ smk_cipso_doi(u32 ndoi, gfp_t gfp_flags)
smk_netlabel_audit_set(&nai);
doip = kmalloc(sizeof(struct cipso_v4_doi), gfp_flags);
doip = kmalloc_obj(struct cipso_v4_doi, gfp_flags);
if (!doip) {
rc = -ENOMEM;
goto clr_doi_lock;
@@ -1249,7 +1249,7 @@ static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
smk_netlabel_audit_set(&audit_info);
if (found == 0) {
snp = kzalloc(sizeof(*snp), GFP_KERNEL);
snp = kzalloc_obj(*snp, GFP_KERNEL);
if (snp == NULL)
rc = -ENOMEM;
else {
@@ -1526,7 +1526,7 @@ static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
break;
}
if (found == 0) {
snp = kzalloc(sizeof(*snp), GFP_KERNEL);
snp = kzalloc_obj(*snp, GFP_KERNEL);
if (snp == NULL)
rc = -ENOMEM;
else {
@@ -1970,7 +1970,7 @@ static int smk_parse_label_list(char *data, struct list_head *list)
if (IS_ERR(skp))
return PTR_ERR(skp);
sklep = kzalloc(sizeof(*sklep), GFP_KERNEL);
sklep = kzalloc_obj(*sklep, GFP_KERNEL);
if (sklep == NULL)
return -ENOMEM;