[PATCH v7 08/17] printk: Ensure consistent name and devname when updating preferred console
From: Petr Mladek
Date: Wed Nov 11 2026 - 11:11:58 EST
Make sure that add_or_update_preferred_console() updates the right entry.
The current caller does the right thing. The new paranoid checks prevent
a potential misuse of the API in the future.
Signed-off-by: Petr Mladek <pmladek@xxxxxxxx>
---
kernel/printk/printk.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index a76f37c1b431..e9b189eae548 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -2564,6 +2564,8 @@ asmlinkage __visible void early_printk(const char *fmt, ...)
*
* 2. Either @name and valid @idx OR @devname and @idx=-1 are allowed. [*]
*
+ * 3. Only matching entries can be updated.
+ *
* [*] A valid @name and @idx will get assigned for a devname later when
* @devname matches during the device initialization, see
* match_devname_and_update_preferred_console().
@@ -2623,6 +2625,19 @@ static int add_or_update_preferred_console(unsigned int i,
}
pc->index = idx;
+ } else {
+ /* Make sure that only matching entries are updated. */
+ if (name && (strcmp(pc->name, name) || (pc->index != idx))) {
+ pr_err("Updating a preferred console with an invalid name or index: %s%d vs. %s%d\n",
+ pc->name, pc->index, name, idx);
+ return -EINVAL;
+ }
+
+ if (devname && strcmp(pc->devname, devname)) {
+ pr_err("Updating a preferred console with an invalid devname: %s vs. %s\n",
+ pc->devname, devname);
+ return -EINVAL;
+ }
}
/*
--
2.56.0