Skip to contentSkip to conversation

bootstd: Fix efi_mgr usage in bootmeths env var

PATCH In review Sam Protsenko 4 participants 18 messages Jan 12, 2025 – Jun 20, 2025

lore
Share & tools
lore
#0 lore raw

cover bootstd: Fix efi_mgr usage in bootmeths env var

Defining the 'bootmeths' environment variable with efi_mgr causes NULL pointer dereference when running 'bootflow scan' on the E850-96 board. This patch series fixes that, and cleans up the surrounding code a little while at it.

Sam Protsenko (3):
bootstd: Fix memleak on errors in bootmeth_setup_iter_order()
bootstd: Probe bootmeth devices for bootmeths env var
bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()

 boot/bootmeth-uclass.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/3 bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
boot/bootmeth-uclass.c +12 -4
index 5b5fea39b3b3..ff36da78d5a1 100644
@@ -133,8 +133,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
 		 * We don't support skipping global bootmeths. Instead, the user
 		 * should omit them from the ordering
 		 */
-		if (!include_global)
-			return log_msg_ret("glob", -EPERM);
+		if (!include_global) {
+			ret = log_msg_ret("glob", -EPERM);
+			goto err_order;
+		}
 		memcpy(order, std->bootmeth_order,
 		       count * sizeof(struct bootmeth *));
 
@@ -188,8 +190,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
 		}
 		count = upto;
 	}
-	if (!count)
-		return log_msg_ret("count2", -ENOENT);
+	if (!count) {
+		ret = log_msg_ret("count2", -ENOENT);
+		goto err_order;
+	}
 
 	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
 	    iter->first_glob_method != -1 && iter->first_glob_method != count) {
@@ -200,6 +204,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
 	iter->num_methods = count;
 
 	return 0;
+
+err_order:
+	free(order);
+	return ret;
 }
 
 int bootmeth_set_order(const char *order_str)
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 41 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
  boot/bootmeth-uclass.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
boot/bootmeth-uclass.c +8 -4
index 5b5fea39b3b3..ff36da78d5a1 100644
@@ -133,8 +133,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
  		 * We don't support skipping global bootmeths. Instead, the user
  		 * should omit them from the ordering
  		 */
-		if (!include_global)
-			return log_msg_ret("glob", -EPERM);
+		if (!include_global) {
+			ret = log_msg_ret("glob", -EPERM);
+			goto err_order;
+		}
  		memcpy(order, std->bootmeth_order,
  		       count * sizeof(struct bootmeth *));
 
@@ -188,8 +190,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
  		}
  		count = upto;
  	}
-	if (!count)
-		return log_msg_ret("count2", -ENOENT);
+	if (!count) {
+		ret = log_msg_ret("count2", -ENOENT);
+		goto err_order;
+	}
 
  	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
  	    iter->first_glob_method != -1 && iter->first_glob_method != count) {

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 213

Quoted 10 lines
boot/bootmeth-uclass.c +4 -0
@@ -200,6 +204,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
  	iter->num_methods = count;
 
  	return 0;
+
+err_order:
+	free(order);
+	return ret;
  }
 
  int bootmeth_set_order(const char *order_str)

bootmeth_setup_iter_order() is called when the boot scan command is executed. The command can be executed multiple times, shouldn't we free iter->method_order before reassigning it? Hopefully the field is NULL if not initialized.

Best regards

Heinrich

#3 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 52 lines · Heinrich Schuchardt
Quoted 50 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
  boot/bootmeth-uclass.c | 16 ++++++++++++----
  1 file changed, 12 insertions(+), 4 deletions(-)
boot/bootmeth-uclass.c +12 -4
index 5b5fea39b3b3..ff36da78d5a1 100644
@@ -133,8 +133,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
               * We don't support skipping global bootmeths. Instead, the user
               * should omit them from the ordering
               */
-             if (!include_global)
-                     return log_msg_ret("glob", -EPERM);
+             if (!include_global) {
+                     ret = log_msg_ret("glob", -EPERM);
+                     goto err_order;
+             }
              memcpy(order, std->bootmeth_order,
                     count * sizeof(struct bootmeth *));
 
@@ -188,8 +190,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
              }
              count = upto;
      }
-     if (!count)
-             return log_msg_ret("count2", -ENOENT);
+     if (!count) {
+             ret = log_msg_ret("count2", -ENOENT);
+             goto err_order;
+     }
 
      if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && include_global &&
          iter->first_glob_method != -1 && iter->first_glob_method != count) {
@@ -200,6 +204,10 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
      iter->num_methods = count;
 
      return 0;
+
+err_order:
+     free(order);
+     return ret;
  }
 
Quoted 7 lines

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 213

Quoted 1 line
boot/bootmeth-uclass.c +0 -0
  int bootmeth_set_order(const char *order_str)

bootmeth_setup_iter_order() is called when the boot scan command is executed. The command can be executed multiple times, shouldn't we free iter->method_order before reassigning it? Hopefully the field is NULL if not initialized.

I think it's already taken care of in do_bootflow_scan(), when it's running bootflow_iter_uninit() in the end of the loop? The relevant call chain looks like this:

do_bootflow_scan()

    bootflow_scan_first()
        bootflow_iter_init()
            memset(iter, 0)
        bootmeth_setup_iter_order()
            order = calloc()
            iter->method_order = order
    bootflow_scan_next()
    bootflow_iter_uninit()
        free(iter->method_order)

So bootmeth_setup_iter_order() is only called once for each 'bootflow scan' execution, and it's always called with iter->method_order freed.

Quoted 4 lines

Best regards

Heinrich

Reviewed-by on v1 1/3
#4 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

In reply to Sam Protsenko
Quoted 11 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

How about adding a test to check there is no leak? You can see test/lib/abuf.c for similar tests.

#5 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 14 lines · Simon Glass
Quoted 11 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
Quoted 4 lines

How about adding a test to check there is no leak? You can see test/lib/abuf.c for similar tests.

That might be a good idea, and I already have the test code handy. But frankly I wasn't able to successfully run the current bootstd test in sandbox, it fails before it can get to memleak check I added. Firstly, when I do:

    $ make sandbox_defconfig
    $ make -j32
    $ ./u-boot -T -c "ut bootstd"

it says:

MMC: Can't map file 'mmc1.img': Invalid argument

and I can see only empty (zero size) mmc1.img in my U-Boot source code root dir. Then I was able to trigger its creation (I guess) by running Python suite somehow, so it generated 20 MiB mmc1.img. But even with that added, I hit this error:

    test/boot/bootdev.c:218, bootdev_test_order():
        0 == bootflow_scan_first(((void *)0), ((void *)0), &iter, 0, &bflow):
        Expected 0x0 (0), got 0xffffffed (-19)

I've read corresponding doc [1] ("Tests" section), but still don't understand how can I successfully run the bootstd test locally. If you can help me out, I can send the memleak test you mentioned in v2.

Thanks!

[1] doc/develop/bootstd/overview.rst

#6 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Hi Sam,

Quoted 39 lines · Sam Protsenko
Quoted 14 lines · Simon Glass
Quoted 11 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
Quoted 4 lines

How about adding a test to check there is no leak? You can see test/lib/abuf.c for similar tests.

That might be a good idea, and I already have the test code handy. But frankly I wasn't able to successfully run the current bootstd test in sandbox, it fails before it can get to memleak check I added. Firstly, when I do:

    $ make sandbox_defconfig
    $ make -j32
    $ ./u-boot -T -c "ut bootstd"

it says:

MMC: Can't map file 'mmc1.img': Invalid argument

and I can see only empty (zero size) mmc1.img in my U-Boot source code root dir. Then I was able to trigger its creation (I guess) by running Python suite somehow, so it generated 20 MiB mmc1.img. But even with that added, I hit this error:

Quoted 7 lines
    test/boot/bootdev.c:218, bootdev_test_order():
        0 == bootflow_scan_first(((void *)0), ((void *)0), &iter, 0, &bflow):
        Expected 0x0 (0), got 0xffffffed (-19)

I've read corresponding doc [1] ("Tests" section), but still don't understand how can I successfully run the bootstd test locally. If you can help me out, I can send the memleak test you mentioned in v2.

You can run the bootstd tests with something like:

test/py/test.py -B sandbox --build-dir /tmp/b/sandbox -k bootstd

although it sounds like you have already figured that out. After you do it once (to create the images) you can run the C tests directly as you did above, but yes, sadly, some of the tests are affected by others (with bootstd). You could take a look if you like.

Regards,
Simon

Quoted 4 lines

Thanks!

[1] doc/develop/bootstd/overview.rst

#7 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 51 lines · Simon Glass

Hi Sam,

Quoted 39 lines · Sam Protsenko
Quoted 14 lines · Simon Glass
Quoted 11 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
Quoted 4 lines

How about adding a test to check there is no leak? You can see test/lib/abuf.c for similar tests.

That might be a good idea, and I already have the test code handy. But frankly I wasn't able to successfully run the current bootstd test in sandbox, it fails before it can get to memleak check I added. Firstly, when I do:

    $ make sandbox_defconfig
    $ make -j32
    $ ./u-boot -T -c "ut bootstd"

it says:

MMC: Can't map file 'mmc1.img': Invalid argument

and I can see only empty (zero size) mmc1.img in my U-Boot source code root dir. Then I was able to trigger its creation (I guess) by running Python suite somehow, so it generated 20 MiB mmc1.img. But even with that added, I hit this error:

Quoted 7 lines
    test/boot/bootdev.c:218, bootdev_test_order():
        0 == bootflow_scan_first(((void *)0), ((void *)0), &iter, 0, &bflow):
        Expected 0x0 (0), got 0xffffffed (-19)

I've read corresponding doc [1] ("Tests" section), but still don't understand how can I successfully run the bootstd test locally. If you can help me out, I can send the memleak test you mentioned in v2.

Quoted 9 lines

You can run the bootstd tests with something like:

test/py/test.py -B sandbox --build-dir /tmp/b/sandbox -k bootstd

although it sounds like you have already figured that out. After you do it once (to create the images) you can run the C tests directly as you did above, but yes, sadly, some of the tests are affected by others (with bootstd). You could take a look if you like.

Is it possible to just apply this patch without a test? It's clearly fixing a memleak. Not that I'm lazy or anything, I came up with a test like below, but I don't want to send something that is quite hard to test. If you think this test is really essential, I can send it once bootstd tests are fixed and can be run in one command without any extra non-documented work.

8<---------------------------------------------------------------------->8

test/boot/bootdev.c +6 -0
index 9af947868707..f4746ee4fb61 100644
@@ -194,6 +194,7 @@ static int bootdev_test_order(struct unit_test_state *uts)
 {
        struct bootflow_iter iter;
        struct bootflow bflow;
+       ulong start;
 
        test_set_skip_delays(true);
 
@@ -201,6 +202,8 @@ static int bootdev_test_order(struct unit_test_state *uts)
        bootstd_reset_usb();
        ut_assertok(run_command("usb start", 0));
 
+       start = ut_check_free();
+
        /*
         * First try the order set by the bootdev-order property
         * Like all sandbox unit tests this relies on the devicetree setting up
@@ -277,6 +280,9 @@ static int bootdev_test_order(struct unit_test_state *uts)
                        iter.dev_used[3]->name);
        bootflow_iter_uninit(&iter);
 
+       /* Check for memory leaks */
+       ut_assertok(ut_check_delta(start));
+
        return 0;
 }
 BOOTSTD_TEST(bootdev_test_order, UTF_DM | UTF_SCAN_FDT);

8<---------------------------------------------------------------------->8

Quoted 7 lines

Regards,
Simon

Quoted 4 lines

Thanks!

[1] doc/develop/bootstd/overview.rst

#8 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 101 lines · Sam Protsenko
Quoted 51 lines · Simon Glass

Hi Sam,

Quoted 39 lines · Sam Protsenko
Quoted 14 lines · Simon Glass
Quoted 11 lines · Sam Protsenko

Free memory allocated for 'order' (array of bootmeths) on error paths in bootmeth_setup_iter_order() function.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
Fixes 10d16faa436c ("bootstd: Detect empty bootmeth")
---
 boot/bootmeth-uclass.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
Quoted 4 lines

How about adding a test to check there is no leak? You can see test/lib/abuf.c for similar tests.

That might be a good idea, and I already have the test code handy. But frankly I wasn't able to successfully run the current bootstd test in sandbox, it fails before it can get to memleak check I added. Firstly, when I do:

    $ make sandbox_defconfig
    $ make -j32
    $ ./u-boot -T -c "ut bootstd"

it says:

MMC: Can't map file 'mmc1.img': Invalid argument

and I can see only empty (zero size) mmc1.img in my U-Boot source code root dir. Then I was able to trigger its creation (I guess) by running Python suite somehow, so it generated 20 MiB mmc1.img. But even with that added, I hit this error:

Quoted 7 lines
    test/boot/bootdev.c:218, bootdev_test_order():
        0 == bootflow_scan_first(((void *)0), ((void *)0), &iter, 0, &bflow):
        Expected 0x0 (0), got 0xffffffed (-19)

I've read corresponding doc [1] ("Tests" section), but still don't understand how can I successfully run the bootstd test locally. If you can help me out, I can send the memleak test you mentioned in v2.

Quoted 9 lines

You can run the bootstd tests with something like:

test/py/test.py -B sandbox --build-dir /tmp/b/sandbox -k bootstd

although it sounds like you have already figured that out. After you do it once (to create the images) you can run the C tests directly as you did above, but yes, sadly, some of the tests are affected by others (with bootstd). You could take a look if you like.

Is it possible to just apply this patch without a test? It's clearly fixing a memleak. Not that I'm lazy or anything, I came up with a test like below, but I don't want to send something that is quite hard to test. If you think this test is really essential, I can send it once bootstd tests are fixed and can be run in one command without any extra non-documented work.

8<---------------------------------------------------------------------->8

test/boot/bootdev.c +6 -0
index 9af947868707..f4746ee4fb61 100644
@@ -194,6 +194,7 @@ static int bootdev_test_order(struct unit_test_state *uts)
 {
        struct bootflow_iter iter;
        struct bootflow bflow;
+       ulong start;
 
        test_set_skip_delays(true);
 
@@ -201,6 +202,8 @@ static int bootdev_test_order(struct unit_test_state *uts)
        bootstd_reset_usb();
        ut_assertok(run_command("usb start", 0));
 
+       start = ut_check_free();
+
        /*
         * First try the order set by the bootdev-order property
         * Like all sandbox unit tests this relies on the devicetree setting up
@@ -277,6 +280,9 @@ static int bootdev_test_order(struct unit_test_state *uts)
                        iter.dev_used[3]->name);
        bootflow_iter_uninit(&iter);
 
+       /* Check for memory leaks */
+       ut_assertok(ut_check_delta(start));
+
        return 0;
 }
 BOOTSTD_TEST(bootdev_test_order, UTF_DM | UTF_SCAN_FDT);
Quoted 1 line

8<---------------------------------------------------------------------->8

Yes, we can do this in a follow-up I think. I'll take this series to -next soon.

Signature · 1 line
Tom
#9 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

On Thu, Jun 19, 2025 at 5:35 PM Tom Rini <[email protected]> wrote:

[snip]

Quoted 8 lines
Quoted 8 lines

Is it possible to just apply this patch without a test? It's clearly fixing a memleak. Not that I'm lazy or anything, I came up with a test like below, but I don't want to send something that is quite hard to test. If you think this test is really essential, I can send it once bootstd tests are fixed and can be run in one command without any extra non-documented work.

[snip]

Quoted 4 lines

Yes, we can do this in a follow-up I think. I'll take this series to -next soon.

Thanks Tom! It's not very recent though, should I rebase it and re-submit?

Quoted 2 lines

--
Tom

#10 lore raw

Re: [PATCH 1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

Quoted 13 lines · Sam Protsenko

On Thu, Jun 19, 2025 at 5:35 PM Tom Rini <[email protected]> wrote:

[snip]

Quoted 8 lines
Quoted 8 lines

Is it possible to just apply this patch without a test? It's clearly fixing a memleak. Not that I'm lazy or anything, I came up with a test like below, but I don't want to send something that is quite hard to test. If you think this test is really essential, I can send it once bootstd tests are fixed and can be run in one command without any extra non-documented work.

Quoted 8 lines

[snip]

Quoted 4 lines

Yes, we can do this in a follow-up I think. I'll take this series to -next soon.

Thanks Tom! It's not very recent though, should I rebase it and re-submit?

It still applies fine, thanks.

Signature · 1 line
Tom
#11 lore raw

PATCH 2/3 bootstd: Probe bootmeth devices for bootmeths env var

In reply to Sam Protsenko

Specifying efi_mgr in 'bootmeths' environment variable leads to NULL pointer dereference when 'bootflow scan' is executed, with call trace like this:

priv->fake_dev // NULL pointer dereference
.read_bootflow = efi_mgr_read_bootflow()
bootmeth_get_bootflow()
bootflow_check()
bootflow_scan_first()
do_bootflow_scan()
'bootflow scan -l'

That happens because in case when 'bootmeths' env var is defined the bootmeth_efi_mgr driver is not probed, and the memory for its private data isn't allocated by .priv_auto. In case when 'bootmeths' env var is not defined, the std->bootmeth_count is 0, and the execution flow in bootmeth_setup_iter_order() takes "no ordering" path, which in turn runs uclass_get_device_by_seq() -> ... -> device_probe(), so issue isn't present there. But when 'bootmeths' is defined and contains efi_mgr, the std->bootmeth_count > 0, so bootmeth_setup_iter_order() follows the "we have an ordering" path, where devices are not probed. In other words:

'bootmeths' defined 'bootmeths' not defined
--------------------------------------------------------
priv == NULL priv != NULL
^ ^
| device_alloc_priv()
no probe device_of_to_plat()
^ device_probe()
| uclass_get_device_tail()
dev = order[i] uclass_get_device_by_seq()
^ ^
| have an ordering | no ordering
+----------------+---------------+
|
bootmeth_setup_iter_order()
bootflow_scan_first()
do_bootflow_scan()

Add an explicit device_probe() call in "we have an ordering" case to fix the issue.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
---
 boot/bootmeth-uclass.c | 7 +++++++
 1 file changed, 7 insertions(+)
boot/bootmeth-uclass.c +7 -0
index ff36da78d5a1..049389403191 100644
@@ -11,6 +11,7 @@
 #include <bootmeth.h>
 #include <bootstd.h>
 #include <dm.h>
+#include <dm/device-internal.h>
 #include <env_internal.h>
 #include <fs.h>
 #include <malloc.h>
@@ -146,6 +147,12 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
 				struct bootmeth_uc_plat *ucp;
 				bool is_global;
 
+				ret = device_probe(dev);
+				if (ret) {
+					ret = log_msg_ret("probe", ret);
+					goto err_order;
+				}
+
 				ucp = dev_get_uclass_plat(dev);
 				is_global = ucp->flags &
 					BOOTMETHF_GLOBAL;
Signature · 1 line
2.39.5
#12 lore raw

Re: [PATCH 2/3] bootstd: Probe bootmeth devices for bootmeths env var

Quoted 61 lines · Sam Protsenko

Specifying efi_mgr in 'bootmeths' environment variable leads to NULL pointer dereference when 'bootflow scan' is executed, with call trace like this:

priv->fake_dev // NULL pointer dereference
.read_bootflow = efi_mgr_read_bootflow()
bootmeth_get_bootflow()
bootflow_check()
bootflow_scan_first()
do_bootflow_scan()
'bootflow scan -l'

That happens because in case when 'bootmeths' env var is defined the bootmeth_efi_mgr driver is not probed, and the memory for its private data isn't allocated by .priv_auto. In case when 'bootmeths' env var is not defined, the std->bootmeth_count is 0, and the execution flow in bootmeth_setup_iter_order() takes "no ordering" path, which in turn runs uclass_get_device_by_seq() -> ... -> device_probe(), so issue isn't present there. But when 'bootmeths' is defined and contains efi_mgr, the std->bootmeth_count > 0, so bootmeth_setup_iter_order() follows the "we have an ordering" path, where devices are not probed. In other words:

'bootmeths' defined 'bootmeths' not defined
--------------------------------------------------------
priv == NULL priv != NULL
^ ^
| device_alloc_priv()
no probe device_of_to_plat()
^ device_probe()
| uclass_get_device_tail()
dev = order[i] uclass_get_device_by_seq()
^ ^
| have an ordering | no ordering
+----------------+---------------+
|
bootmeth_setup_iter_order()
bootflow_scan_first()
do_bootflow_scan()

Add an explicit device_probe() call in "we have an ordering" case to fix the issue.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
---
  boot/bootmeth-uclass.c | 7 +++++++
  1 file changed, 7 insertions(+)
boot/bootmeth-uclass.c +1 -0
index ff36da78d5a1..049389403191 100644
@@ -11,6 +11,7 @@
  #include <bootmeth.h>
  #include <bootstd.h>
  #include <dm.h>
+#include <dm/device-internal.h>
  #include <env_internal.h>
  #include <fs.h>
  #include <malloc.h>

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 154

Quoted 8 lines
boot/bootmeth-uclass.c +5 -0
@@ -146,6 +147,12 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
  				struct bootmeth_uc_plat *ucp;
  				bool is_global;
 
+				ret = device_probe(dev);
+				if (ret) {
+					ret = log_msg_ret("probe", ret);
+					goto err_order;
+				}

This would mean that we fail boot if probing one of the boot devices fails. Shouldn't we remove the device from the order instead?

Best regards

Heinrich

Quoted 4 lines
boot/bootmeth-uclass.c +1 -0
+
  				ucp = dev_get_uclass_plat(dev);
  				is_global = ucp->flags &
  					BOOTMETHF_GLOBAL;
Reviewed-by
#13 lore raw

Re: [PATCH 2/3] bootstd: Probe bootmeth devices for bootmeths env var

Hi Heinrich, Sam,

Quoted 41 lines · Heinrich Schuchardt
Quoted 39 lines · Sam Protsenko

Specifying efi_mgr in 'bootmeths' environment variable leads to NULL pointer dereference when 'bootflow scan' is executed, with call trace like this:

priv->fake_dev // NULL pointer dereference
.read_bootflow = efi_mgr_read_bootflow()
bootmeth_get_bootflow()
bootflow_check()
bootflow_scan_first()
do_bootflow_scan()
'bootflow scan -l'

That happens because in case when 'bootmeths' env var is defined the bootmeth_efi_mgr driver is not probed, and the memory for its private data isn't allocated by .priv_auto. In case when 'bootmeths' env var is not defined, the std->bootmeth_count is 0, and the execution flow in bootmeth_setup_iter_order() takes "no ordering" path, which in turn runs uclass_get_device_by_seq() -> ... -> device_probe(), so issue isn't present there. But when 'bootmeths' is defined and contains efi_mgr, the std->bootmeth_count > 0, so bootmeth_setup_iter_order() follows the "we have an ordering" path, where devices are not probed. In other words:

'bootmeths' defined 'bootmeths' not defined
--------------------------------------------------------
priv == NULL priv != NULL
^ ^
| device_alloc_priv()
no probe device_of_to_plat()
^ device_probe()
| uclass_get_device_tail()
dev = order[i] uclass_get_device_by_seq()
^ ^
| have an ordering | no ordering
+----------------+---------------+
|
bootmeth_setup_iter_order()
bootflow_scan_first()
do_bootflow_scan()

Quoted 8 lines
Quoted 8 lines

Add an explicit device_probe() call in "we have an ordering" case to fix the issue.

Fixes c627cfc14c08 ("bootstd: Allow scanning for global bootmeths separately")
---
  boot/bootmeth-uclass.c | 7 +++++++
  1 file changed, 7 insertions(+)
Quoted 17 lines
Quoted 17 lines
boot/bootmeth-uclass.c +1 -0
index ff36da78d5a1..049389403191 100644
@@ -11,6 +11,7 @@
  #include <bootmeth.h>
  #include <bootstd.h>
  #include <dm.h>
+#include <dm/device-internal.h>
  #include <env_internal.h>
  #include <fs.h>
  #include <malloc.h>
@@ -146,6 +147,12 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
                              struct bootmeth_uc_plat *ucp;
                              bool is_global;
 
Quoted 8 lines

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 154

Quoted 5 lines
boot/bootmeth-uclass.c +5 -0
+                             ret = device_probe(dev);
+                             if (ret) {
+                                     ret = log_msg_ret("probe", ret);
+                                     goto err_order;
+                             }

This would mean that we fail boot if probing one of the boot devices fails. Shouldn't we remove the device from the order instead?

It is actually a bootmeth failing to probe which causes problems, not a bootdev. I think that is catastrophic enough that failing is fine, at least for now.

Strictly speaking we should not probe the bootmeth until it is actually used. One way to do this is to probe it in bootflow_iter_set_dev(), or in its callers, where we already probe the bootdev.

Quoted 5 lines

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 158

Quoted 4 lines
boot/bootmeth-uclass.c +1 -0
+
                              ucp = dev_get_uclass_plat(dev);
                              is_global = ucp->flags &
                                      BOOTMETHF_GLOBAL;

Regards,
Simon

#14 lore raw

PATCH 3/3 bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()

In reply to Sam Protsenko

There is no such thing as struct bootmeth, it's probably a typo. This issue doesn't affect the execution as it's a pointer, and pointer sizes are the same for all data types. But it can be confusing, so make it struct udevice, as it should be.

Fixes a950d31abe98 ("bootstd: Add the bootmeth uclass and helpers")
---
 boot/bootmeth-uclass.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth-uclass.c +1 -1
index 049389403191..2496e8c1d8a8 100644
@@ -139,7 +139,7 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
 			goto err_order;
 		}
 		memcpy(order, std->bootmeth_order,
-		       count * sizeof(struct bootmeth *));
+		       count * sizeof(struct udevice *));
 
 		if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
 			for (i = 0; i < count; i++) {
Signature · 1 line
2.39.5
#15 lore raw

Re: [PATCH 3/3] bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()

Quoted 16 lines · Sam Protsenko

There is no such thing as struct bootmeth, it's probably a typo. This issue doesn't affect the execution as it's a pointer, and pointer sizes are the same for all data types. But it can be confusing, so make it struct udevice, as it should be.

Fixes a950d31abe98 ("bootstd: Add the bootmeth uclass and helpers")
---
  boot/bootmeth-uclass.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth-uclass.c +0 -0
index 049389403191..2496e8c1d8a8 100644

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 142

Quoted 5 lines
boot/bootmeth-uclass.c +1 -1
@@ -139,7 +139,7 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
  			goto err_order;
  		}
  		memcpy(order, std->bootmeth_order,
-		       count * sizeof(struct bootmeth *));
+		       count * sizeof(struct udevice *));

I found this description of the field:
@bootmeth_order: List of bootmeth devices to use, in order, NULL-terminated

As the list is NULL terminated, shouldn't we copy the NULL value, i.e.

(count + 1) * sizeof(struct udevice *)

so that we can still identify the end of the list?

Best regards

Heinrich

Quoted 3 lines
boot/bootmeth-uclass.c +0 -0
  		if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
  			for (i = 0; i < count; i++) {
#16 lore raw

Re: [PATCH 3/3] bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()

Hi Heinrich,

Quoted 27 lines · Heinrich Schuchardt
Quoted 16 lines · Sam Protsenko

There is no such thing as struct bootmeth, it's probably a typo. This issue doesn't affect the execution as it's a pointer, and pointer sizes are the same for all data types. But it can be confusing, so make it struct udevice, as it should be.

Fixes a950d31abe98 ("bootstd: Add the bootmeth uclass and helpers")
---
  boot/bootmeth-uclass.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
boot/bootmeth-uclass.c +0 -0
index 049389403191..2496e8c1d8a8 100644

boot/bootmeth-uclass.c int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global) · line 142

Quoted 5 lines
boot/bootmeth-uclass.c +1 -1
@@ -139,7 +139,7 @@ int bootmeth_setup_iter_order(struct bootflow_iter *iter, bool include_global)
                      goto err_order;
              }
              memcpy(order, std->bootmeth_order,
-                    count * sizeof(struct bootmeth *));
+                    count * sizeof(struct udevice *));

I found this description of the field:
@bootmeth_order: List of bootmeth devices to use, in order, NULL-terminated

Quoted 6 lines

As the list is NULL terminated, shouldn't we copy the NULL value, i.e.

(count + 1) * sizeof(struct udevice *)

so that we can still identify the end of the list?

Unfortunately that comment is out of date. The bootmeth_count member holds the count now.

And probably it would be better as an alist now that we have that.

[..]

Regards,
Simom

#17 lore raw
In reply to Sam Protsenko
Quoted 11 lines · Sam Protsenko

Defining the 'bootmeths' environment variable with efi_mgr causes NULL pointer dereference when running 'bootflow scan' on the E850-96 board. This patch series fixes that, and cleans up the surrounding code a little while at it.

Sam Protsenko (3):
bootstd: Fix memleak on errors in bootmeth_setup_iter_order()
bootstd: Probe bootmeth devices for bootmeths env var
bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()

[...]

Applied to u-boot/next, thanks!

[1/3] bootstd: Fix memleak on errors in bootmeth_setup_iter_order()

      commit: 11319e0e2bbf7b3892658d45017cd80c41ad961b
[2/3] bootstd: Probe bootmeth devices for bootmeths env var
      commit: 8da358c0a15a4f538d561602f600b2695fedeb56
[3/3] bootstd: Fix incorrect struct name in bootmeth_setup_iter_order()
      commit: 8c61fc082e9ae045534dc2f18ed50493adf35e6d
Signature · 1 line
Tom