mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] init: Use strscpy() in initcall_blacklist()
@ 2026-11-15 14:23 Thorsten Blum
  2026-11-15 14:23 ` [PATCH 2/2] init: Combine blacklist entry allocations " Thorsten Blum
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2026-11-15 14:23 UTC (permalink / raw)
  To: Andrew Morton, Masami Hiramatsu (Google),
	Christian Brauner, Breno Leitao, Sang-Heon Jeon, Ryan Roberts,
	Wilson Felipe Pereira
  Cc: Thorsten Blum, Mike Rapoport (Microsoft), linux-kernel

Replace the deprecated strcpy() [1] with strscpy() to bound the copy to
the allocated buffer size.

Also use a simple while loop to reduce code indentation.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strcpy [1]
Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 init/main.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)

diff --git a/init/main.c b/init/main.c
index 500584688744..db8fb6ebe830 100644
--- a/init/main.c
+++ b/init/main.c
@@ -1277,18 +1277,15 @@ static int __init initcall_blacklist(char *str)
 	struct blacklist_entry *entry;
 
 	/* str argument is a comma-separated list of functions */
-	do {
-		str_entry = strsep(&str, ",");
-		if (str_entry) {
-			pr_debug("blacklisting initcall %s\n", str_entry);
-			entry = memblock_alloc_or_panic(sizeof(*entry),
-					       SMP_CACHE_BYTES);
-			entry->buf = memblock_alloc_or_panic(strlen(str_entry) + 1,
-						    SMP_CACHE_BYTES);
-			strcpy(entry->buf, str_entry);
-			list_add(&entry->next, &blacklisted_initcalls);
-		}
-	} while (str_entry);
+	while ((str_entry = strsep(&str, ","))) {
+		size_t buf_size = strlen(str_entry) + 1;
+
+		pr_debug("blacklisting initcall %s\n", str_entry);
+		entry = memblock_alloc_or_panic(sizeof(*entry), SMP_CACHE_BYTES);
+		entry->buf = memblock_alloc_or_panic(buf_size, SMP_CACHE_BYTES);
+		strscpy(entry->buf, str_entry, buf_size);
+		list_add(&entry->next, &blacklisted_initcalls);
+	}
 
 	return 1;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/2] init: Combine blacklist entry allocations in initcall_blacklist()
  2026-11-15 14:23 [PATCH 1/2] init: Use strscpy() in initcall_blacklist() Thorsten Blum
@ 2026-11-15 14:23 ` Thorsten Blum
  2026-11-15 15:17   ` Andrew Morton
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2026-11-15 14:23 UTC (permalink / raw)
  To: Masami Hiramatsu (Google),
	Andrew Morton, Breno Leitao, Christian Brauner, Sang-Heon Jeon,
	Wilson Felipe Pereira, Ryan Roberts, Mike Rapoport (Microsoft)
  Cc: Thorsten Blum, linux-kernel

Use a flexible array member for the function name and allocate it
together with the blacklist entry. This removes the pointer and saves
one allocation call per blacklist entry.

Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 init/main.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/init/main.c b/init/main.c
index db8fb6ebe830..da10f08037ef 100644
--- a/init/main.c
+++ b/init/main.c
@@ -28,6 +28,7 @@
 #include <linux/init.h>
 #include <linux/initrd.h>
 #include <linux/memblock.h>
+#include <linux/overflow.h>
 #include <linux/acpi.h>
 #include <linux/bootconfig.h>
 #include <linux/console.h>
@@ -1266,7 +1267,7 @@ static void __init do_ctors(void)
 #ifdef CONFIG_KALLSYMS
 struct blacklist_entry {
 	struct list_head next;
-	char *buf;
+	char buf[];
 };
 
 static __initdata_or_module LIST_HEAD(blacklisted_initcalls);
@@ -1281,8 +1282,8 @@ static int __init initcall_blacklist(char *str)
 		size_t buf_size = strlen(str_entry) + 1;
 
 		pr_debug("blacklisting initcall %s\n", str_entry);
-		entry = memblock_alloc_or_panic(sizeof(*entry), SMP_CACHE_BYTES);
-		entry->buf = memblock_alloc_or_panic(buf_size, SMP_CACHE_BYTES);
+		entry = memblock_alloc_or_panic(struct_size(entry, buf, buf_size),
+						SMP_CACHE_BYTES);
 		strscpy(entry->buf, str_entry, buf_size);
 		list_add(&entry->next, &blacklisted_initcalls);
 	}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 2/2] init: Combine blacklist entry allocations in initcall_blacklist()
  2026-11-15 14:23 ` [PATCH 2/2] init: Combine blacklist entry allocations " Thorsten Blum
@ 2026-11-15 15:17   ` Andrew Morton
  0 siblings, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-11-15 15:17 UTC (permalink / raw)
  To: Thorsten Blum
  Cc: Masami Hiramatsu (Google),
	Breno Leitao, Christian Brauner, Sang-Heon Jeon,
	Wilson Felipe Pereira, Ryan Roberts, Mike Rapoport (Microsoft),
	linux-kernel

On Sun, 15 Nov 2026 15:23:21 +0100 Thorsten Blum <blum@kernel.org> wrote:

> Use a flexible array member for the function name and allocate it
> together with the blacklist entry. This removes the pointer and saves
> one allocation call per blacklist entry.
> 

Looks OK, thanks.

> --- a/init/main.c
> +++ b/init/main.c
> @@ -1266,7 +1267,7 @@ static void __init do_ctors(void)
>  #ifdef CONFIG_KALLSYMS
>  struct blacklist_entry {
>  	struct list_head next;
> -	char *buf;
> +	char buf[];
>  };

We need some care with the flexible arrays.  If some code does a struct
copy:

	struct blacklist_entry bl1 = bl2;

then the old code would have worked but after this change bl1 has
garbage in its buf[].

afaict none of our static checking tools (gcc, clang, smatch, sparse)
will catch this.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-15 15:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 14:23 [PATCH 1/2] init: Use strscpy() in initcall_blacklist() Thorsten Blum
2026-11-15 14:23 ` [PATCH 2/2] init: Combine blacklist entry allocations " Thorsten Blum
2026-11-15 15:17   ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®