All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
@ 2026-06-16 16:44 Naveen Kumar Chaudhary
  2026-06-16 20:20 ` David Laight
  2026-06-16 22:04 ` Doug Anderson
  0 siblings, 2 replies; 6+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-16 16:44 UTC (permalink / raw)
  To: jason.wessel, danielt, dianders; +Cc: kgdb-bugreport, linux-kernel

kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
always match the actual buffer size.

Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
---
 kernel/debug/kdb/kdb_main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index ddce56b47b25..571e9e61b40e 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
 
 do_full_getstr:
 		/* PROMPT can only be set if we have MEM_READ permission. */
-		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
-			 raw_smp_processor_id());
+		snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
+			 kdbgetenv("PROMPT"), raw_smp_processor_id());
 
 		/*
 		 * Fetch command from keyboard
-- 
2.43.0


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

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
@ 2026-06-16 20:20 ` David Laight
  2026-06-16 22:06   ` Doug Anderson
  2026-06-16 22:04 ` Doug Anderson
  1 sibling, 1 reply; 6+ messages in thread
From: David Laight @ 2026-06-16 20:20 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary
  Cc: jason.wessel, danielt, dianders, kgdb-bugreport, linux-kernel

On Tue, 16 Jun 2026 22:14:54 +0530
Naveen Kumar Chaudhary <naveen.osdev@gmail.com> wrote:

> kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> always match the actual buffer size.

As a matter of interest what sets the string that kdbgetenv("PROMPT")
returns?
If it is user settable, using it as a format string doesn't seem wise
(even for kdbg).

	David

> 
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_main.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..571e9e61b40e 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>  
>  do_full_getstr:
>  		/* PROMPT can only be set if we have MEM_READ permission. */
> -		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -			 raw_smp_processor_id());
> +		snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
> +			 kdbgetenv("PROMPT"), raw_smp_processor_id());
>  
>  		/*
>  		 * Fetch command from keyboard


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

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
  2026-06-16 20:20 ` David Laight
@ 2026-06-16 22:04 ` Doug Anderson
  2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
  1 sibling, 1 reply; 6+ messages in thread
From: Doug Anderson @ 2026-06-16 22:04 UTC (permalink / raw)
  To: Naveen Kumar Chaudhary
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

Hi,

On Tue, Jun 16, 2026 at 9:45 AM Naveen Kumar Chaudhary
<naveen.osdev@gmail.com> wrote:
>
> kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> always match the actual buffer size.
>
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_main.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..571e9e61b40e 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -1265,8 +1265,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>
>  do_full_getstr:
>                 /* PROMPT can only be set if we have MEM_READ permission. */
> -               snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -                        raw_smp_processor_id());
> +               snprintf(kdb_prompt_str, sizeof(kdb_prompt_str),
> +                        kdbgetenv("PROMPT"), raw_smp_processor_id());

Hmmm, I don't think so. My compiler yells at me for that:

  Invalid application of 'sizeof' to an incomplete type
'char[]'clang(sizeof_alignof_incomplete_or_sizeless_type)

...which makes sense since the variable is defined in a different
source file and the header has:

kernel/debug/kdb/kdb_private.h:extern char kdb_prompt_str[];

So there's a bug to fix, but I don't think your fix is quite right.
Instead, maybe you should have a single #define that's in a header and
used by both files?


-Doug

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

* Re: [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN
  2026-06-16 20:20 ` David Laight
@ 2026-06-16 22:06   ` Doug Anderson
  0 siblings, 0 replies; 6+ messages in thread
From: Doug Anderson @ 2026-06-16 22:06 UTC (permalink / raw)
  To: David Laight
  Cc: Naveen Kumar Chaudhary, jason.wessel, danielt, kgdb-bugreport,
	linux-kernel

Hi,

On Tue, Jun 16, 2026 at 1:20 PM David Laight
<david.laight.linux@gmail.com> wrote:
>
> On Tue, 16 Jun 2026 22:14:54 +0530
> Naveen Kumar Chaudhary <naveen.osdev@gmail.com> wrote:
>
> > kdb_main.c defines CMD_BUFLEN as 200 (for command history buffers),
> > while kdb_io.c defines it as 256 (for kdb_prompt_str). The snprintf()
> > filling kdb_prompt_str incorrectly used the local CMD_BUFLEN (200),
> > truncating the prompt unnecessarily. Use sizeof(kdb_prompt_str) to
> > always match the actual buffer size.
>
> As a matter of interest what sets the string that kdbgetenv("PROMPT")
> returns?
> If it is user settable, using it as a format string doesn't seem wise
> (even for kdbg).

For some history, see commit ad99b5105c08 ("kdb: Censor attempts to
set PROMPT without ENABLE_MEM_READ").

I have no idea how truly useful it is to be able to mess with your
prompt like this to begin with, but at least the "safety" of it has
been considered a little.

-Doug

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

* [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h
  2026-06-16 22:04 ` Doug Anderson
@ 2026-06-17  2:28   ` Naveen Kumar Chaudhary
  2026-06-17  3:00     ` Naveen Kumar Chaudhary
  0 siblings, 1 reply; 6+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-17  2:28 UTC (permalink / raw)
  To: dianders, david.laight.linux
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

CMD_BUFLEN was defined separately in kdb_io.c (256) and kdb_main.c
(200), causing kdb_main.c to use the wrong size when formatting the
prompt string into kdb_prompt_str (which is 256 bytes).

Move CMD_BUFLEN (256) into kdb_private.h so all users share a single
consistent definition, and remove the local definitions from both
files.

Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
---
 kernel/debug/kdb/kdb_io.c      | 1 -
 kernel/debug/kdb/kdb_main.c    | 6 ++----
 kernel/debug/kdb/kdb_private.h | 3 ++-
 3 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
index c399f11740ef..f5b1b7d4c9c8 100644
--- a/kernel/debug/kdb/kdb_io.c
+++ b/kernel/debug/kdb/kdb_io.c
@@ -22,7 +22,6 @@
 #include <linux/kallsyms.h>
 #include "kdb_private.h"
 
-#define CMD_BUFLEN 256
 char kdb_prompt_str[CMD_BUFLEN];
 
 int kdb_trap_printk;
diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
index ddce56b47b25..ca0126db9850 100644
--- a/kernel/debug/kdb/kdb_main.c
+++ b/kernel/debug/kdb/kdb_main.c
@@ -783,8 +783,6 @@ static int kdb_exec_defcmd(int argc, const char **argv)
 
 /* Command history */
 #define KDB_CMD_HISTORY_COUNT	32
-#define CMD_BUFLEN		200	/* kdb_printf: max printline
-					 * size == 256 */
 static unsigned int cmd_head, cmd_tail;
 static unsigned int cmdptr;
 static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
@@ -1265,8 +1263,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
 
 do_full_getstr:
 		/* PROMPT can only be set if we have MEM_READ permission. */
-		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
-			 raw_smp_processor_id());
+		snprintf(kdb_prompt_str, CMD_BUFLEN,
+			 kdbgetenv("PROMPT"), raw_smp_processor_id());
 
 		/*
 		 * Fetch command from keyboard
diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
index 92a28b8ab604..722e8aa50724 100644
--- a/kernel/debug/kdb/kdb_private.h
+++ b/kernel/debug/kdb/kdb_private.h
@@ -225,7 +225,8 @@ extern void kdb_kbd_cleanup_state(void);
 #define kdb_kbd_cleanup_state()
 #endif /* ! CONFIG_KDB_KEYBOARD */
 
-extern char kdb_prompt_str[];
+#define CMD_BUFLEN 256
+extern char kdb_prompt_str[CMD_BUFLEN];
 
 #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
 
-- 
2.43.0


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

* Re: [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h
  2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
@ 2026-06-17  3:00     ` Naveen Kumar Chaudhary
  0 siblings, 0 replies; 6+ messages in thread
From: Naveen Kumar Chaudhary @ 2026-06-17  3:00 UTC (permalink / raw)
  To: dianders, david.laight.linux
  Cc: jason.wessel, danielt, kgdb-bugreport, linux-kernel

Thanks Doug for the review. Apologies, I missed to realize that my
config for kgdb_kdb was not enabled and hence missed that compilation
error. Have taken care this time with this new patch.

One concern I should mention about `cmd_hist[32][200]` earlier which was
32 entries × 200 = 6,400 bytes of static storage. Bumping to 256 would
make it 8,192 bytes — a ~28% increase in static memory. Though this
should be ok for a debugger. Alternatively, I was thinking to rename
these two differently so that they don't clash, plus we won't have this
memory bump. But it has its own quirks.

Assuming that 1.8KB is meaningless for an optional debugger and a single
definition is impossible to get wrong, the unified approach is the better
long-term choice. Please correct me in case I am wrong.

Regards,
Naveen

On Wed 17 Jun 07:58 AM, Naveen Kumar Chaudhary wrote:
> CMD_BUFLEN was defined separately in kdb_io.c (256) and kdb_main.c
> (200), causing kdb_main.c to use the wrong size when formatting the
> prompt string into kdb_prompt_str (which is 256 bytes).
> 
> Move CMD_BUFLEN (256) into kdb_private.h so all users share a single
> consistent definition, and remove the local definitions from both
> files.
> 
> Fixes: 5d5314d6795f ("kdb: core for kgdb back end (1 of 2)")
> Signed-off-by: Naveen Kumar Chaudhary <naveen.osdev@gmail.com>
> ---
>  kernel/debug/kdb/kdb_io.c      | 1 -
>  kernel/debug/kdb/kdb_main.c    | 6 ++----
>  kernel/debug/kdb/kdb_private.h | 3 ++-
>  3 files changed, 4 insertions(+), 6 deletions(-)
> 
> diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c
> index c399f11740ef..f5b1b7d4c9c8 100644
> --- a/kernel/debug/kdb/kdb_io.c
> +++ b/kernel/debug/kdb/kdb_io.c
> @@ -22,7 +22,6 @@
>  #include <linux/kallsyms.h>
>  #include "kdb_private.h"
>  
> -#define CMD_BUFLEN 256
>  char kdb_prompt_str[CMD_BUFLEN];
>  
>  int kdb_trap_printk;
> diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c
> index ddce56b47b25..ca0126db9850 100644
> --- a/kernel/debug/kdb/kdb_main.c
> +++ b/kernel/debug/kdb/kdb_main.c
> @@ -783,8 +783,6 @@ static int kdb_exec_defcmd(int argc, const char **argv)
>  
>  /* Command history */
>  #define KDB_CMD_HISTORY_COUNT	32
> -#define CMD_BUFLEN		200	/* kdb_printf: max printline
> -					 * size == 256 */
>  static unsigned int cmd_head, cmd_tail;
>  static unsigned int cmdptr;
>  static char cmd_hist[KDB_CMD_HISTORY_COUNT][CMD_BUFLEN];
> @@ -1265,8 +1263,8 @@ static int kdb_local(kdb_reason_t reason, int error, struct pt_regs *regs,
>  
>  do_full_getstr:
>  		/* PROMPT can only be set if we have MEM_READ permission. */
> -		snprintf(kdb_prompt_str, CMD_BUFLEN, kdbgetenv("PROMPT"),
> -			 raw_smp_processor_id());
> +		snprintf(kdb_prompt_str, CMD_BUFLEN,
> +			 kdbgetenv("PROMPT"), raw_smp_processor_id());
>  
>  		/*
>  		 * Fetch command from keyboard
> diff --git a/kernel/debug/kdb/kdb_private.h b/kernel/debug/kdb/kdb_private.h
> index 92a28b8ab604..722e8aa50724 100644
> --- a/kernel/debug/kdb/kdb_private.h
> +++ b/kernel/debug/kdb/kdb_private.h
> @@ -225,7 +225,8 @@ extern void kdb_kbd_cleanup_state(void);
>  #define kdb_kbd_cleanup_state()
>  #endif /* ! CONFIG_KDB_KEYBOARD */
>  
> -extern char kdb_prompt_str[];
> +#define CMD_BUFLEN 256
> +extern char kdb_prompt_str[CMD_BUFLEN];
>  
>  #define	KDB_WORD_SIZE	((int)sizeof(unsigned long))
>  
> -- 
> 2.43.0
> 

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

end of thread, other threads:[~2026-06-17  3:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-16 16:44 [PATCH] kdb: use sizeof(kdb_prompt_str) instead of mismatched CMD_BUFLEN Naveen Kumar Chaudhary
2026-06-16 20:20 ` David Laight
2026-06-16 22:06   ` Doug Anderson
2026-06-16 22:04 ` Doug Anderson
2026-06-17  2:28   ` [PATCH v2] kdb: unify CMD_BUFLEN definition into kdb_private.h Naveen Kumar Chaudhary
2026-06-17  3:00     ` Naveen Kumar Chaudhary

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.