public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Rosen Penev <rosenp@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Kees Cook <kees@kernel.org>, Arnd Bergmann <arnd@arndb.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	linux-hardening@vger.kernel.org (open list:KERNEL HARDENING (not
	covered by other areas):Keyword:\b__counted_by(_le|_be)?\b)
Subject: [PATCH] lkdtm/bugs: combine allocations with kzalloc_flex
Date: Mon, 16 Mar 2026 20:15:59 -0700	[thread overview]
Message-ID: <20260317031559.93544-1-rosenp@gmail.com> (raw)

Avoid a separate kzalloc_objs call and just use a flexible array member
to do so. Simplifies allocation.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/misc/lkdtm/bugs.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/misc/lkdtm/bugs.c b/drivers/misc/lkdtm/bugs.c
index e0098f314570..938916e5b53c 100644
--- a/drivers/misc/lkdtm/bugs.c
+++ b/drivers/misc/lkdtm/bugs.c
@@ -515,9 +515,9 @@ struct lkdtm_extra {
 struct lkdtm_cb_ptr {
 	int a, b, c;
 	int nr_extra;
-	char *buf __counted_by_ptr(len);
 	size_t len;
 	struct lkdtm_extra *extra __counted_by_ptr(nr_extra);
+	char buf[] __counted_by(len);
 };
 
 static noinline void check_ptr_len(struct lkdtm_cb_ptr *p, size_t len)
@@ -533,17 +533,12 @@ static void lkdtm_PTR_BOUNDS(void)
 {
 	struct lkdtm_cb_ptr *inst;
 
-	inst = kzalloc_obj(*inst);
+	inst = kzalloc_flex(*inst, buf, element_count);
 	if (!inst) {
 		pr_err("FAIL: could not allocate struct lkdtm_cb_ptr!\n");
 		return;
 	}
 
-	inst->buf = kzalloc(element_count, GFP_KERNEL);
-	if (!inst->buf) {
-		pr_err("FAIL: could not allocate inst->buf!\n");
-		return;
-	}
 	inst->len = element_count;
 
 	/* Double element_count */
@@ -564,7 +559,6 @@ static void lkdtm_PTR_BOUNDS(void)
 	ignored = inst->extra[inst->nr_extra].b;
 
 	kfree(inst->extra);
-	kfree(inst->buf);
 	kfree(inst);
 
 	pr_err("FAIL: survived access of invalid pointer member offset!\n");
-- 
2.53.0


                 reply	other threads:[~2026-03-17  3:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20260317031559.93544-1-rosenp@gmail.com \
    --to=rosenp@gmail.com \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=gustavoars@kernel.org \
    --cc=kees@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