Skip to contentSkip to conversation

init/main.c: Fix potential static_command_line memory overflow

PATCH Superseded Yuntao Wang 4 participants 14 messages Apr 11, 2024 – Apr 12, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 init/main.c: Fix potential static_command_line memory overflow

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +5 -3
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
 	if (extra_init_args)
 		ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-	len = xlen + strlen(boot_command_line) + 1;
+	len = xlen + strlen(boot_command_line) + ilen + 1;
 
-	saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+	saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
 	if (!saved_command_line)
-		panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+	len = xlen + strlen(command_line) + 1;
 
 	static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
 	if (!static_command_line)
Signature · 1 line
2.44.0
#1 lore raw

CC Hiramatsu-san

Quoted 25 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 10 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;
 
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
#2 lore raw
Quoted 10 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 7 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Quoted 37 lines
Quoted 16 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 10 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;
 
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

I didn't observe the overflow, it's just a theoretical bug.

Quoted 4 lines
Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Before commit f5c7310ac73e, the memory size allocated for static_command_line was 'strlen(command_line) + 1', but commit f5c7310ac73e changed this size to 'strlen(boot_command_line) + 1'. I think this should be wrong.

Quoted 41 lines
Quoted 37 lines
Quoted 16 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 10 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;
 
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
#4 lore raw
Quoted 18 lines · On Thu, 11 Apr 2024 22:27:23 +0800 Yuntao Wang <[email protected]> wrote:
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

I didn't observe the overflow, it's just a theoretical bug.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Before commit f5c7310ac73e, the memory size allocated for static_command_line was 'strlen(command_line) + 1', but commit f5c7310ac73e changed this size to 'strlen(boot_command_line) + 1'. I think this should be wrong.

Ah, OK. that sounds reasonable.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

init/main.c static void __init setup_command_line(char *command_line) · line 634

Quoted 7 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;

Ah, I missed this line. Sorry. So this looks good to me but you don't need any other lines, because those are not related to the bug you want to fix. Please just focus on 1 fix.

Thank you,

Quoted 18 lines
Quoted 18 lines
Quoted 14 lines

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 3 lines
init/main.c +0 -0
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#5 lore raw
Quoted 23 lines · Masami Hiramatsu (Google)
Quoted 20 lines · On Thu, 11 Apr 2024 22:27:23 +0800 Yuntao Wang <[email protected]> wrote:
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

I didn't observe the overflow, it's just a theoretical bug.

Quoted 2 lines
Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Before commit f5c7310ac73e, the memory size allocated for static_command_line was 'strlen(command_line) + 1', but commit f5c7310ac73e changed this size to 'strlen(boot_command_line) + 1'. I think this should be wrong.

Ah, OK. that sounds reasonable.

:-)

Quoted 29 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

init/main.c static void __init setup_command_line(char *command_line) · line 634

Quoted 7 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;

Ah, I missed this line. Sorry. So this looks good to me but you don't need any other lines, because those are not related to the bug you want to fix. Please just focus on 1 fix.

Quoted 1 line

Thank you,

The other few lines of modification are some very small optimization. I thought they were not worth posting a separate patch for at the time, so I post them together. If necessary, I can separate them.

Thanks.

Quoted 22 lines
Quoted 18 lines
Quoted 18 lines
Quoted 14 lines

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 3 lines
init/main.c +0 -0
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#6 lore raw
In reply to Masami Hiramatsu (Google)
Quoted 49 lines · Masami Hiramatsu (Google)
Quoted 18 lines · On Thu, 11 Apr 2024 22:27:23 +0800 Yuntao Wang <[email protected]> wrote:
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

I didn't observe the overflow, it's just a theoretical bug.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Before commit f5c7310ac73e, the memory size allocated for static_command_line was 'strlen(command_line) + 1', but commit f5c7310ac73e changed this size to 'strlen(boot_command_line) + 1'. I think this should be wrong.

Ah, OK. that sounds reasonable.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

init/main.c static void __init setup_command_line(char *command_line) · line 634

Quoted 7 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;

Ah, I missed this line. Sorry. So this looks good to me but you don't need any other lines, because those are not related to the bug you want to fix. Please just focus on 1 fix.

Hi Masami,

Do I need to split this patch into two? Or should I just repost this patch with any other lines not related to this bug removed?

Actually, I think these lines are still necessary as they make the code look a bit cleaner.

Thanks,
Yuntao

Quoted 24 lines

Thank you,

Quoted 18 lines
Quoted 18 lines
Quoted 14 lines

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 3 lines
init/main.c +0 -0
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#7 lore raw
Quoted 63 lines · On Fri, 12 Apr 2024 11:51:07 +0800 Yuntao Wang <[email protected]> wrote:
Quoted 49 lines · Masami Hiramatsu (Google)
Quoted 18 lines · On Thu, 11 Apr 2024 22:27:23 +0800 Yuntao Wang <[email protected]> wrote:
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Geert Uytterhoeven

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Can this ever happen?
Did you observe the overflow or is this a theoretical bug?

I didn't observe the overflow, it's just a theoretical bug.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Quoted 1 line
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

f5c7310ac73e didn't have the logic for calculating allocation size, we surely don't want to go back that far wiht Fixes.

Before commit f5c7310ac73e, the memory size allocated for static_command_line was 'strlen(command_line) + 1', but commit f5c7310ac73e changed this size to 'strlen(boot_command_line) + 1'. I think this should be wrong.

Ah, OK. that sounds reasonable.

Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 
Quoted 11 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

init/main.c static void __init setup_command_line(char *command_line) · line 634

Quoted 7 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;

Ah, I missed this line. Sorry. So this looks good to me but you don't need any other lines, because those are not related to the bug you want to fix. Please just focus on 1 fix.

Quoted 4 lines

Hi Masami,

Do I need to split this patch into two? Or should I just repost this patch with any other lines not related to this bug removed?

Latter one should be easier. Only add above one line and just explain that this recovers strlen(command_line) which was miss-consolidated with strlen(boot_command_line) in the commit f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()"). Simple fix does not confuse reviewers.

Quoted 3 lines

Actually, I think these lines are still necessary as they make the code look a bit cleaner.

That is a cleanup, and should be separated from bugfix, because bugfix must be backported but the cleanup doesn't. As far as I can see, the cleanup part can not apply to the commit f5c7310ac73e.

Thank you,

Quoted 28 lines

Thanks,
Yuntao

Quoted 24 lines

Thank you,

Quoted 18 lines
Quoted 18 lines
Quoted 14 lines

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 3 lines
init/main.c +0 -0
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 2 lines
Sincerely yours,
Mike.
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#8 lore raw

v2 cover Fix potential static_command_line memory overflow

v1 -> v2: Split the v1 version patch into a bugfix patch and a cleanup patch

Yuntao Wang (2):
init/main.c: Fix potential static_command_line memory overflow init/main.c: Minor cleanup for the setup_command_line() function

 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Signature · 1 line
2.44.0
#9 lore raw

PATCH v2 1/2 init/main.c: Fix potential static_command_line memory overflow

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

This patch just recovers strlen(command_line) which was miss-consolidated with strlen(boot_command_line) in the commit f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")
---
 init/main.c | 2 ++
 1 file changed, 2 insertions(+)
init/main.c +2 -0
index 881f6230ee59..5dcf5274c09c 100644
@@ -636,6 +636,8 @@ static void __init setup_command_line(char *command_line)
 	if (!saved_command_line)
 		panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
 
+	len = xlen + strlen(command_line) + 1;
+
 	static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
 	if (!static_command_line)
 		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
Signature · 1 line
2.44.0
#10 lore raw

PATCH v2 2/2 init/main.c: Minor cleanup for the setup_command_line() function

In reply to Yuntao Wang

This is just a minor cleanup to make the code look a bit cleaner.

---
 init/main.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
init/main.c +3 -3
index 5dcf5274c09c..d6383e397e9f 100644
@@ -630,11 +630,11 @@ static void __init setup_command_line(char *command_line)
 	if (extra_init_args)
 		ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-	len = xlen + strlen(boot_command_line) + 1;
+	len = xlen + strlen(boot_command_line) + ilen + 1;
 
-	saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+	saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
 	if (!saved_command_line)
-		panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+		panic("%s: Failed to allocate %zu bytes\n", __func__, len);
 
 	len = xlen + strlen(command_line) + 1;
 
Signature · 1 line
2.44.0
#11 lore raw

Re: [PATCH v2 0/2] Fix potential static_command_line memory overflow

In reply to Yuntao Wang
Quoted 8 lines · On Fri, 12 Apr 2024 16:17:31 +0800 Yuntao Wang <[email protected]> wrote:

v1 -> v2: Split the v1 version patch into a bugfix patch and a cleanup patch

Yuntao Wang (2):
init/main.c: Fix potential static_command_line memory overflow init/main.c: Minor cleanup for the setup_command_line() function

 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

OK, both looks good to me. Let me pick it.

Thanks,

Quoted 4 lines
Signature · 2 lines
-- 
2.44.0
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#12 lore raw
In reply to Geert Uytterhoeven
Quoted 10 lines · On Thu, 11 Apr 2024 09:23:47 +0200 Geert Uytterhoeven <[email protected]>…

CC Hiramatsu-san

Quoted 7 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Hi Yuntao,

OK, but this is not a good way to fix.
We should introduce "slen = strlen(command_line) + xlen + 1" and use it for allocating static_command_line.

Quoted 2 lines
Quoted 2 lines
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

As Mike pointed, this is not the best commit.

Fixes 51887d03aca1 ("bootconfig: init: Allow admin to use bootconfig for kernel command line")

Thank you,

Quoted 38 lines
Quoted 16 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 10 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;
 
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68korg

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
#13 lore raw
Quoted 11 lines · Masami Hiramatsu (Google)
Quoted 8 lines · On Thu, 11 Apr 2024 09:23:47 +0200 Geert Uytterhoeven <[email protected]>…

CC Hiramatsu-san

Quoted 5 lines · Yuntao Wang

We allocate memory of size 'xlen + strlen(boot_command_line) + 1' for static_command_line, but the strings copied into static_command_line are extra_command_line and command_line, rather than extra_command_line and boot_command_line.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

When strlen(command_line) > strlen(boot_command_line), static_command_line will overflow.

Hi Yuntao,

OK, but this is not a good way to fix.
We should introduce "slen = strlen(command_line) + xlen + 1" and use it for allocating static_command_line.

Hi Masami,

But this would introduce an additional variable 'slen', which seems unnecessary.

In fact, we can use 'len' directly, this makes the code more concise.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Fixes f5c7310ac73e ("init/main: add checks for the return value of memblock_alloc*()")

As Mike pointed, this is not the best commit.

Fixes 51887d03aca1 ("bootconfig: init: Allow admin to use bootconfig for kernel command line")

Thank you,

I have reviewed the commit 51887d03aca1, but I don't think this commit introduced the issue. I still think it was introduced by the f5c7310ac73e commit. Or perhaps I missed something?

Quoted 42 lines
Quoted 38 lines
Quoted 16 lines
---
 init/main.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
init/main.c +1 -1
index 2ca52474d0c3..a7b1f5f3e3b6 100644
@@ -625,11 +625,13 @@ static void __init setup_command_line(char *command_line)
        if (extra_init_args)
                ilen = strlen(extra_init_args) + 4; /* for " -- " */
 
-       len = xlen + strlen(boot_command_line) + 1;
+       len = xlen + strlen(boot_command_line) + ilen + 1;
 

init/main.c static void __init setup_command_line(char *command_line) · line 637

Quoted 10 lines
init/main.c +4 -2
-       saved_command_line = memblock_alloc(len + ilen, SMP_CACHE_BYTES);
+       saved_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!saved_command_line)
-               panic("%s: Failed to allocate %zu bytes\n", __func__, len + ilen);
+               panic("%s: Failed to allocate %zu bytes\n", __func__, len);
+
+       len = xlen + strlen(command_line) + 1;
 
        static_command_line = memblock_alloc(len, SMP_CACHE_BYTES);
        if (!static_command_line)

Gr{oetje,eeting}s,

Geert

Signature · 5 lines
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68korg

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>