Skip to contentSkip to conversation

init: Use strscpy() in initcall_blacklist()

PATCH In review Thorsten Blum 2 participants 3 messages Nov 15

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 init: Use strscpy() in initcall_blacklist()

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.

---
 init/main.c | 21 +++++++++------------
 1 file changed, 9 insertions(+), 12 deletions(-)
init/main.c +9 -12
index 500584688744..db8fb6ebe830 100644
@@ -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;
 }
#1 lore raw

PATCH 2/2 init: Combine blacklist entry allocations in initcall_blacklist()

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.

---
 init/main.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
init/main.c +4 -3
index db8fb6ebe830..da10f08037ef 100644
@@ -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);
 	}
#2 lore raw

Re: [PATCH 2/2] init: Combine blacklist entry allocations in initcall_blacklist()

Quoted 4 lines · On Sun, 15 Nov 2026 15:23:21 +0100 Thorsten Blum <[email protected]> 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.

init/main.c static void __init do_ctors(void) · line 1271

Quoted 9 lines
init/main.c +1 -1
@@ -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.