bootstd: rauc: Fix stack overflow with over-long slot names
cover bootstd: rauc: Fix stack overflow with over-long slot names
This series fixes a stack buffer overflow in the RAUC bootmeth. The "BOOT_<slot>_LEFT" environment variable names are built with sprintf() into fixed 42-byte stack buffers, so a slot name longer than 32 characters from BOOT_ORDER or CONFIG_BOOTMETH_RAUC_BOOT_ORDER writes past the end of the buffer, and even a 32-character name overflows it by the NUL terminator.
Patch 1 builds the names through a helper that rejects slot names which do not fit. Patch 2 adds a regression test covering both sides of the new length limit.
This series is based on next, since it builds on the recently merged "bootstd: Fix crashes and memory leaks on scan/boot error paths" series and its bootflow_rauc test.
Aristo Chen (2):
bootstd: rauc: Reject slot names that do not fit the env-var name
buffer
test: boot: Check that rauc rejects over-long slot names
boot/bootmeth_rauc.c | 42 +++++++++++++++++++++++++++++++++++------- test/boot/bootflow.c | 23 +++++++++++++++++++++++ 2 files changed, 58 insertions(+), 7 deletions(-)
base-commit: 856a00aeb61c1d4e7825257e54219599f8e34a6c
Signature · 1 line
2.43.0
PATCH 1/2 bootstd: rauc: Reject slot names that do not fit the env-var name buffer
The names of the "BOOT_<slot>_LEFT" environment variables are built with sprintf() into fixed 42-byte stack buffers, and the "rauc.slot=<slot>" kernel argument into a 64-byte one. The slot names come from the BOOT_ORDER environment variable in find_active_slot() and distro_rauc_boot(), and from CONFIG_BOOTMETH_RAUC_BOOT_ORDER in distro_rauc_read_bootflow() and the reset-all-tries path. Nothing limits their length, so a slot name longer than 32 characters writes past the end of the buffer, and even a 32-character one overflows it by the NUL terminator.
The BOOT_ORDER entries are checked against the configured slots when the bootflow is scanned, but the variable is read again when the bootflow is booted, so changing it in between (e.g. from the command line) reaches the unchecked sprintf() calls.
Build the variable names through a small helper that uses snprintf() and rejects slot names which do not fit, size the buffer for a 32-character slot name plus NUL, and use snprintf() for the kernel argument as well. Its slot name has passed the same check by then, so it always fits.
--- boot/bootmeth_rauc.c | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-)
boot/bootmeth_rauc.c +35 -7
@@ -22,8 +22,8 @@#include <linux/stringify.h> #include <asm/cache.h> -/* Length of env var "BOOT_*_LEFT" */ -#define BOOT_LEFT_LEN (5 + 32 + 5) +/* Size of the name of env var "BOOT_<slot>_LEFT", for a 32-char slot name */ +#define BOOT_LEFT_LEN (5 + 32 + 5 + 1)static const char *const script_names[] = {"boot.scr" ,"boot.scr.uimg" ,NULL };
@@ -79,6 +79,21 @@ static struct distro_rauc_slot *get_slot(struct distro_rauc_priv *priv,return NULL ; } +/** +* boot_left_name() - Build the name of the "BOOT_<slot>_LEFT" env var +* +* @buf: Buffer of BOOT_LEFT_LEN bytes to write the name to +* @slot: Name of the slot +* Return: 0 if OK, -ENAMETOOLONG if @slot does not fit +*/ +static int boot_left_name (char *buf,const char *slot) +{ +if (snprintf (buf,BOOT_LEFT_LEN ,"BOOT_%s_LEFT" , slot) >=BOOT_LEFT_LEN ) +return -ENAMETOOLONG ; + +return 0 ; +} +static int distro_rauc_check (struct udevice *dev,struct bootflow_iter *iter) {/*
@@ -167,7 +182,11 @@ static int distro_rauc_read_bootflow(struct udevice *dev, struct bootflow *bflow default_boot_order =CONFIG_BOOTMETH_RAUC_BOOT_ORDER ; default_boot_order_list =str_to_list (default_boot_order);for (i =0 ; default_boot_order_list[i]; i++) { -sprintf (boot_left,"BOOT_%s_LEFT" , default_boot_order_list[i]); + ret =boot_left_name (boot_left, default_boot_order_list[i]); +if (ret) { +str_free_list (default_boot_order_list); +return log_msg_ret ("name" , ret); + }if (!env_get (boot_left)) {log_debug ("%s did not exist yet, setting default value\n" , boot_left);
@@ -333,7 +352,11 @@ static int find_active_slot(char **slot_name, ulong *slot_tries)return log_msg_ret ("env" , -ENOENT ); boot_order_list =str_to_list (boot_order);for (i =0 ; boot_order_list[i] && !slot_found; i++) { -sprintf (boot_left,"BOOT_%s_LEFT" , boot_order_list[i]); + ret =boot_left_name (boot_left, boot_order_list[i]); +if (ret) { +str_free_list (boot_order_list); +return log_msg_ret ("name" , ret); + } tries =env_get_ulong (boot_left,10 ,ULONG_MAX );if (tries ==ULONG_MAX ) {str_free_list (boot_order_list);
@@ -359,7 +382,11 @@ static int find_active_slot(char **slot_name, ulong *slot_tries) log_info("INFO: Resetting all slot tries to " __stringify(CONFIG_BOOTMETH_RAUC_DEFAULT_TRIES) "\n"); boot_order_list =str_to_list (CONFIG_BOOTMETH_RAUC_BOOT_ORDER );for (i =0 ; boot_order_list[i]; i++) { -sprintf (boot_left,"BOOT_%s_LEFT" , boot_order_list[i]); + ret =boot_left_name (boot_left, boot_order_list[i]); +if (ret) { +str_free_list (boot_order_list); +return log_msg_ret ("name" , ret); + } ret =env_set_ulong (boot_left,CONFIG_BOOTMETH_RAUC_DEFAULT_TRIES );if (ret) {str_free_list (boot_order_list);
@@ -416,7 +443,7 @@ static int distro_rauc_boot(struct udevice *dev, struct bootflow *bflow)return log_msg_ret ("env" , ret);/* Kernel command line arguments */ -sprintf (raucargs,"rauc.slot=%s" , active_slot); +snprintf (raucargs,sizeof (raucargs),"rauc.slot=%s" , active_slot); ret =env_set ("raucargs" , raucargs);if (ret)return log_msg_ret ("env" , ret);
@@ -454,7 +481,8 @@ static int distro_rauc_boot(struct udevice *dev, struct bootflow *bflow)log_debug ("BOOT_ORDER: %s\n" , boot_order); boot_order_list =str_to_list (boot_order);for (i =0 ; boot_order_list[i]; i++) { -sprintf (boot_left,"BOOT_%s_LEFT" , boot_order_list[i]); +if (boot_left_name (boot_left, boot_order_list[i])) +continue ;log_debug ("%s: %s\n" , boot_left,env_get (boot_left)); }str_free_list (boot_order_list);
Signature · 1 line
2.43.0
Re: [PATCH 1/2] bootstd: rauc: Reject slot names that do not fit the env-var name buffer
Quoted 22 lines · Aristo Chen
bootstd: rauc: Reject slot names that do not fit the env-var name buffer
The names of the "BOOT_<slot>_LEFT" environment variables are built with
sprintf()into fixed 42-byte stack buffers, and the "rauc.slot=<slot>" kernel argument into a 64-byte one. The slot names come from theBOOT_ORDERenvironment variable infind_active_slot()anddistro_rauc_boot(), and fromCONFIG_BOOTMETH_RAUC_BOOT_ORDERindistro_rauc_read_bootflow()and the reset-all-tries path. Nothing limits their length, so a slot name longer than 32 characters writes past the end of the buffer, and even a 32-character one overflows it by the NUL terminator.The
BOOT_ORDERentries are checked against the configured slots when the bootflow is scanned, but the variable is read again when the bootflow is booted, so changing it in between (e.g. from the command line) reaches the uncheckedsprintf()calls.Build the variable names through a small helper that uses
snprintf()and rejects slot names which do not fit, size the buffer for a 32-character slot name plus NUL, and usesnprintf()for the kernel argument as well. Its slot name has passed the same check by then, so it always fits.
Quoted 5 lines
Fixes 7e5c2c782fb9 ("bootstd: Add implementation for bootmeth rauc")Signed-off-by Aristo Chen <[email protected]>
boot/bootmeth_rauc.c| 42 +++++++++++++++++++++++++++++++++++-------1 file changed, 35 insertions(+), 7 deletions(-)
PATCH 2/2 test: boot: Check that rauc rejects over-long slot names
In reply to Aristo ChenBoot the RAUC bootflow after setting BOOT_ORDER to a 33-character slot name, one more than fits the "BOOT_<slot>_LEFT" env-var name buffer. The boot must now fail with -ENAMETOOLONG.
Before the previous commit the name overflowed the stack buffer; as no such variable exists the boot then failed with -ENOENT, hiding the overflow.
Also check the accepted side of the boundary: a 32-character name, the longest that fits, must pass the length check and fail only with -ENOENT because its BOOT_<slot>_LEFT variable does not exist.
--- test/boot/bootflow.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+)
test/boot/bootflow.c +23 -0
@@ -1445,6 +1445,29 @@ static int bootflow_rauc(struct unit_test_state *uts)ut_assert_console_end (); +/* +* Boot with a BOOT_ORDER slot name (33 chars) that is too long for the +* "BOOT_<slot>_LEFT" env-var name buffer. It must be rejected rather +* than overflow the stack. +*/ +ut_assertok (run_command ("bootflow select 0" ,0 )); +ut_assert_console_end (); +ut_assertok (env_set ("BOOT_ORDER" ,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA B" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENAMETOOLONG ); +ut_assert_console_end (); + +/* +* A 32-char slot name is the longest that fits. It must pass the +* length check and fail only because BOOT_<slot>_LEFT does not exist. +*/ +ut_assertok (env_set ("BOOT_ORDER" ,"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENOENT ); +ut_assert_console_end (); +/* * Scan with a BOOT_ORDER naming a slot that has no configured * partitions. get_slot() must not crash on the NULL array terminator;
Signature · 1 line
2.43.0
Re: [PATCH 2/2] test: boot: Check that rauc rejects over-long slot names
Hi Aristo,
Quoted 10 lines · Aristo Chen
test: boot: Check that rauc rejects over-long slot names
Boot the RAUC bootflow after setting
BOOT_ORDERto a 33-character slot name, one more than fits the "BOOT_<slot>_LEFT" env-var name buffer. The boot must now fail with-ENAMETOOLONG.Before the previous commit the name overflowed the stack buffer; as no such variable exists the boot then failed with
-ENOENT, hiding the overflow.
Quoted 8 lines
Also check the accepted side of the boundary: a 32-character name, the longest that fits, must pass the length check and fail only with
-ENOENTbecause its BOOT_<slot>_LEFT variable does not exist.Signed-off-by Aristo Chen <[email protected]>
test/boot/bootflow.c| 23 +++++++++++++++++++++++1 file changed, 23 insertions(+)
Since the boundary is boot_left_name(), please exercise the scan path too - distro_rauc_read_bootflow() calls it against CONFIG_BOOTMETH_RAUC_BOOT_ORDER, which is what a user is most likely to hit. The current test only reaches find_active_slot() via BOOT_ORDER at boot time.
Quoted 17 lines
test/boot/bootflow.c +9
-0+/* +* A 32-char slot name is the longest that fits. It must pass the +* length check and fail only because BOOT_<slot>_LEFT does not exist. +*/ +ut_assertok (env_set ("BOOT_ORDER" ,"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENOENT ); +ut_assert_console_end ();test/boot/bootflow.c +2
-0@@ -1520,6 +1520,29 @@ static int bootstd_images(struct unit_test_state *uts) ptr =map_sysmem (img->addr,0 );ut_asserteq_strn ("BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=" , ptr); +/* +* Boot with a BOOT_ORDER slot name (33 chars) that is too long for the
test/boot/bootflow.c static int bootstd_images(struct unit_test_state *uts) · line 1534
Quoted 10 lines
test/boot/bootflow.c +10
-0+* "BOOT_<slot>_LEFT" env-var name buffer. It must be rejected rather +* than overflow the stack. +*/ +ut_assertok (run_command ("bootflow select 0" ,0 )); +ut_assert_console_end (); +ut_assertok (env_set ("BOOT_ORDER" ,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA B" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENAMETOOLONG ); +ut_assert_console_end ();
This is in the wrong function. bootstd_images() scans mmc2/mmc1/mmc4/mmc5 and produces the four extlinux/script/cros bootflows listed just above; it never binds bootmeth_rauc or enables mmc10, so 'bootflow select 0' picks mmc1.bootdev.part_1 (extlinux_cfg) and the expected "** Booting bootflow 'mmc10.bootdev.whole' with rauc" line never appears. Please extend bootflow_rauc() (or add a sibling test that reuses its setup) so mmc10 is bound and rauc is in the bootmeth order before 'bootflow boot' runs. Also restore BOOT_ORDER (and the bootmeth/bootdev order) at the end so it does not leak into the next test.
Regards,
Simon
Re: [PATCH 2/2] test: boot: Check that rauc rejects over-long slot names
Hi Simon,
Quoted 20 lines · Simon Glass
Hi Aristo,
Quoted 16 lines · Aristo Chen
test: boot: Check that rauc rejects over-long slot names
Boot the RAUC bootflow after setting
BOOT_ORDERto a 33-character slot name, one more than fits the "BOOT_<slot>_LEFT" env-var name buffer. The boot must now fail with-ENAMETOOLONG.Before the previous commit the name overflowed the stack buffer; as no such variable exists the boot then failed with
-ENOENT, hiding the overflow.Also check the accepted side of the boundary: a 32-character name, the longest that fits, must pass the length check and fail only with
-ENOENTbecause its BOOT_<slot>_LEFT variable does not exist.Signed-off-by Aristo Chen <[email protected]>
Quoted 8 lines
Quoted 2 lines
test/boot/bootflow.c| 23 +++++++++++++++++++++++1 file changed, 23 insertions(+)Since the boundary is
boot_left_name(), please exercise the scan path too -distro_rauc_read_bootflow()calls it againstCONFIG_BOOTMETH_RAUC_BOOT_ORDER, which is what a user is most likely to hit. The current test only reachesfind_active_slot()viaBOOT_ORDERat boot time.
During a scan the only names that reach boot_left_name() are the entries of CONFIG_BOOTMETH_RAUC_BOOT_ORDER, a compile-time string ("A B" on sandbox). The BOOT_ORDER entries seen at scan time go through get_slot() in distro_rauc_scan_parts() instead, which marks the bootflow invalid when a name does not match a configured slot, so no env-var name is built from them. The test cannot change the Kconfig value at run time, so covering the rejecting side of the scan path would need either a dedicated sandbox configuration (which would require adapting or excluding the existing rauc test, since the bootflow becomes invalid) or a test hook that lets the scan path take an alternate default order. The scan path's normal accepted-input case is exercised by every scan in bootflow_rauc(), and it goes through the same helper the boot-time test drives across the 32/33-char boundary. If you still want the rejecting Kconfig scan path covered, I can add a dedicated test configuration, although that seems disproportionate for this fix.
Quoted 31 lines
Quoted 18 lines
test/boot/bootflow.c +9
-0+/* +* A 32-char slot name is the longest that fits. It must pass the +* length check and fail only because BOOT_<slot>_LEFT does not exist. +*/ +ut_assertok (env_set ("BOOT_ORDER" ,"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENOENT ); +ut_assert_console_end ();test/boot/bootflow.c +2
-0@@ -1520,6 +1520,29 @@ static int bootstd_images(struct unit_test_state *uts) ptr =map_sysmem (img->addr,0 );ut_asserteq_strn ("BOOT_IMAGE=/vmlinuz-5.15.0-121-generic root=" , ptr); +/* +* Boot with a BOOT_ORDER slot name (33 chars) that is too long for the test/boot/bootflow.c static int bootstd_images(struct unit_test_state *uts) · line 1534
Quoted 10 lines
test/boot/bootflow.c +10
-0+* "BOOT_<slot>_LEFT" env-var name buffer. It must be rejected rather +* than overflow the stack. +*/ +ut_assertok (run_command ("bootflow select 0" ,0 )); +ut_assert_console_end (); +ut_assertok (env_set ("BOOT_ORDER" ,"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA B" )); +ut_asserteq (1 ,run_command ("bootflow boot" ,0 )); +ut_assert_nextline ("** Booting bootflow 'mmc10.bootdev.whole' with rauc" ); +ut_assert_nextline ("Boot failed (err=%d)" , -ENAMETOOLONG ); +ut_assert_console_end ();This is in the wrong function.
bootstd_images()scans mmc2/mmc1/mmc4/mmc5 and produces the four extlinux/script/cros
Quoted 8 lines
bootflows listed just above; it never binds
bootmeth_raucor enables mmc10, so 'bootflow select 0' picks mmc1.bootdev.part_1 (extlinux_cfg) and the expected "** Booting bootflow 'mmc10.bootdev.whole' with rauc" line never appears. Please extendbootflow_rauc()(or add a sibling test that reuses its setup) so mmc10 is bound and rauc is in the bootmeth order before 'bootflow boot' runs. Also restoreBOOT_ORDER(and the bootmeth/bootdev order) at the end so it does not leak into the next test.
The hunk you quote is not the one in the patch. As sent it is
@@ -1445,6 +1445,29 @@ static int bootflow_rauc(struct unit_test_state *uts)
so it does extend bootflow_rauc(), right after the scan and list that leave the single rauc bootflow on mmc10 as index 0. The series is based on next (base-commit 856a00aeb61 in the cover letter) because it builds on the bootflow_rauc() additions from the "Fix crashes and memory leaks on scan/boot error paths" series, which is only in next. I think the hunk you saw comes from applying the patch to master: patch(1) accepts it there with fuzz 2, landing inside bootstd_images(), which matches your quote, while git am refuses it.
On the restore: bootflow_rauc() already sets BOOT_ORDER back to "A B" (the value distro_rauc_read_bootflow() seeds from CONFIG_BOOTMETH_RAUC_BOOT_ORDER) and restores the bootdev order before returning, and UTF_DM gives every test a fresh driver-model root, so the bootmeth order does not carry over either.
Looking into that last point did turn up a small pre-existing leak: the array allocated by bootmeth_set_order() is never freed when the bootstd device goes away, since bootstd_remove() frees bootdev_order but not bootmeth_order. It affects every test that sets a bootmeth order (three in bootflow.c alone) and real teardown too, so it seems better handled as a separate one-line fix to bootstd_remove() than folded into this series; I can send that patch.
I re-checked both patches on top of current next (951132236b9): they apply cleanly and bootflow_rauc passes, so I will hold off on a resend unless you or Tom prefer one.
Quoted 3 lines
Regards,
Simon
Best regards,
Aristo
Simon Glass
Reviewed-byRe: [PATCH 2/2] test: boot: Check that rauc rejects over-long slot names
Hi Aristo,
Quoted 33 lines · Aristo Chen
Hi Simon,
Quoted 20 lines · Simon Glass
Hi Aristo,
Quoted 16 lines · Aristo Chen
test: boot: Check that rauc rejects over-long slot names
Boot the RAUC bootflow after setting
BOOT_ORDERto a 33-character slot name, one more than fits the "BOOT_<slot>_LEFT" env-var name buffer. The boot must now fail with-ENAMETOOLONG.Before the previous commit the name overflowed the stack buffer; as no such variable exists the boot then failed with
-ENOENT, hiding the overflow.Also check the accepted side of the boundary: a 32-character name, the longest that fits, must pass the length check and fail only with
-ENOENTbecause its BOOT_<slot>_LEFT variable does not exist.Signed-off-by Aristo Chen <[email protected]>Quoted 8 lines
Quoted 2 lines
test/boot/bootflow.c| 23 +++++++++++++++++++++++1 file changed, 23 insertions(+)Since the boundary is
boot_left_name(), please exercise the scan path too -distro_rauc_read_bootflow()calls it againstCONFIG_BOOTMETH_RAUC_BOOT_ORDER, which is what a user is most likely to hit. The current test only reachesfind_active_slot()viaBOOT_ORDERat boot time.
Quoted 15 lines
During a scan the only names that reach
boot_left_name()are the entries ofCONFIG_BOOTMETH_RAUC_BOOT_ORDER, a compile-time string ("A B" on sandbox). TheBOOT_ORDERentries seen at scan time go throughget_slot()indistro_rauc_scan_parts()instead, which marks the bootflow invalid when a name does not match a configured slot, so no env-var name is built from them. The test cannot change theKconfigvalue at run time, so covering the rejecting side of the scan path would need either a dedicated sandbox configuration (which would require adapting or excluding the existing rauc test, since the bootflow becomes invalid) or a test hook that lets the scan path take an alternate default order. The scan path's normal accepted-input case is exercised by every scan inbootflow_rauc(), and it goes through the same helper the boot-time test drives across the 32/33-char boundary. If you still want the rejectingKconfigscan path covered, I can add a dedicated test configuration, although that seems disproportionate for this fix.
That seems OK to me...we are going to end up with more need to test internal functions, but most of the other tests don't, so it's not something you need to pioneer.
I was a bit confused perhaps because patchwork died while I was checking the patches.
Quoted 3 lines
I re-checked both patches on top of current next (
951132236b9): they apply cleanly andbootflow_raucpasses, so I will hold off on a resend unless you or Tom prefer one.
Regards,
Simon
Quoted 7 lines · Aristo Chen
This series fixes a stack buffer overflow in the RAUC bootmeth. The "BOOT_<slot>_LEFT" environment variable names are built with
sprintf()into fixed 42-byte stack buffers, so a slot name longer than 32 characters fromBOOT_ORDERorCONFIG_BOOTMETH_RAUC_BOOT_ORDERwrites past the end of the buffer, and even a 32-character name overflows it by the NUL terminator.
Quoted 7 lines
Patch 1 builds the names through a helper that rejects slot names which do not fit. Patch 2 adds a regression test covering both sides of the new length limit.
This series is based on next, since it builds on the recently merged "bootstd: Fix crashes and memory leaks on scan/boot error paths" series and its
bootflow_rauctest.
I'm hoping for some tags from the RAUC folks on this, thanks!
Signature · 1 line
Tom