audit: use struct_size() helper in kmalloc()
authorXiu Jianfeng <xiujianfeng@huawei.com>
Tue, 14 Dec 2021 11:48:54 +0000 (19:48 +0800)
committerPaul Moore <paul@paul-moore.com>
Tue, 14 Dec 2021 22:39:42 +0000 (17:39 -0500)
Make use of struct_size() helper instead of an open-coded calucation.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Paul Moore <paul@paul-moore.com>
kernel/audit.c
kernel/audit_tree.c
kernel/auditfilter.c

index 121d37e700a62b53854c06199d9a89850ec39dd4..0117e7d947fdead2b57e38b3747700a7a6411fe2 100644 (file)
@@ -1446,7 +1446,7 @@ static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
                        if (err)
                                return err;
                }
-               sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
+               sig_data = kmalloc(struct_size(sig_data, ctx, len), GFP_KERNEL);
                if (!sig_data) {
                        if (audit_sig_sid)
                                security_release_secctx(ctx, len);
index 72324afcffef5c831b88ce9d00acafa4a6cdbe72..e7315d4871635debb47737817216d7decc1a55ff 100644 (file)
@@ -94,7 +94,7 @@ static struct audit_tree *alloc_tree(const char *s)
 {
        struct audit_tree *tree;
 
-       tree = kmalloc(sizeof(struct audit_tree) + strlen(s) + 1, GFP_KERNEL);
+       tree = kmalloc(struct_size(tree, pathname, strlen(s) + 1), GFP_KERNEL);
        if (tree) {
                refcount_set(&tree->count, 1);
                tree->goner = 0;
index d75acb014ccdcba2a0bed0646323f5427757e493..398b4c57e92147ce51b14777c8f23f4d557f1717 100644 (file)
@@ -637,7 +637,7 @@ static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
        void *bufp;
        int i;
 
-       data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
+       data = kmalloc(struct_size(data, buf, krule->buflen), GFP_KERNEL);
        if (unlikely(!data))
                return NULL;
        memset(data, 0, sizeof(*data));