From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2CE22C433F5 for ; Tue, 14 Dec 2021 18:23:55 +0000 (UTC) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-325-fJzVw1pxN8mG7zhVTvro0g-1; Tue, 14 Dec 2021 13:23:50 -0500 X-MC-Unique: fJzVw1pxN8mG7zhVTvro0g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4C522100C633; Tue, 14 Dec 2021 18:23:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 37C82429A0; Tue, 14 Dec 2021 18:23:47 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 08C171802E27; Tue, 14 Dec 2021 18:23:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1BEHvSJG027123 for ; Tue, 14 Dec 2021 12:57:28 -0500 Received: by smtp.corp.redhat.com (Postfix) id 86108141DED3; Tue, 14 Dec 2021 17:57:28 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 81E031400AFB for ; Tue, 14 Dec 2021 17:57:28 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 673C7811E76 for ; Tue, 14 Dec 2021 17:57:28 +0000 (UTC) Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-453-OXrS9yEMPXqzWCUfbvFrHQ-1; Tue, 14 Dec 2021 12:57:08 -0500 X-MC-Unique: OXrS9yEMPXqzWCUfbvFrHQ-1 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8F1AC61665; Tue, 14 Dec 2021 17:49:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57007C34600; Tue, 14 Dec 2021 17:49:07 +0000 (UTC) Date: Tue, 14 Dec 2021 11:54:48 -0600 From: "Gustavo A. R. Silva" To: Xiu Jianfeng Subject: Re: [PATCH -next] audit: use struct_size() helper in kmalloc() Message-ID: <20211214175448.GA13107@embeddedor> References: <20211214114854.133328-1-xiujianfeng@huawei.com> MIME-Version: 1.0 In-Reply-To: <20211214114854.133328-1-xiujianfeng@huawei.com> X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.85 on 10.11.54.7 X-loop: linux-audit@redhat.com X-Mailman-Approved-At: Tue, 14 Dec 2021 13:23:34 -0500 Cc: linux-kernel@vger.kernel.org, eparis@redhat.com, "Gustavo A. R. Silva" , wangweiyang2@huawei.com, linux-audit@redhat.com, linux-hardening@vger.kernel.org X-BeenThere: linux-audit@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Linux Audit Discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-audit-bounces@redhat.com Errors-To: linux-audit-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=linux-audit-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit On Tue, Dec 14, 2021 at 07:48:54PM +0800, Xiu Jianfeng wrote: > 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 > --- > kernel/audit.c | 2 +- > kernel/audit_tree.c | 2 +- > kernel/auditfilter.c | 2 +- > 3 files changed, 3 insertions(+), 3 deletions(-) > > diff --git a/kernel/audit.c b/kernel/audit.c > index d4084751cfe6..f33028578c60 100644 > --- a/kernel/audit.c > +++ b/kernel/audit.c > @@ -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); > diff --git a/kernel/audit_tree.c b/kernel/audit_tree.c > index 72324afcffef..e7315d487163 100644 > --- a/kernel/audit_tree.c > +++ b/kernel/audit_tree.c > @@ -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; > diff --git a/kernel/auditfilter.c b/kernel/auditfilter.c > index 4173e771650c..19352820b274 100644 > --- a/kernel/auditfilter.c > +++ b/kernel/auditfilter.c > @@ -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); Why don't you also transform the zero-length array in struct audit_rule_data into a flexible-array member: 508 struct audit_rule_data { 509 __u32 flags; /* AUDIT_PER_{TASK,CALL}, AUDIT_PREPEND */ 510 __u32 action; /* AUDIT_NEVER, AUDIT_POSSIBLE, AUDIT_ALWAYS */ 511 __u32 field_count; 512 __u32 mask[AUDIT_BITMASK_SIZE]; /* syscall(s) affected */ 513 __u32 fields[AUDIT_MAX_FIELDS]; 514 __u32 values[AUDIT_MAX_FIELDS]; 515 __u32 fieldflags[AUDIT_MAX_FIELDS]; 516 __u32 buflen; /* total length of string fields */ 517 char buf[0]; /* string fields buffer */ 518 }; Thanks -- Gustavo > if (unlikely(!data)) > return NULL; > memset(data, 0, sizeof(*data)); > -- > 2.17.1 > > > > -- Linux-audit mailing list Linux-audit@redhat.com https://listman.redhat.com/mailman/listinfo/linux-audit