Skip to contentSkip to conversation

Security and correctness issues around captured FDT pointers

DISCUSSION Bo Gan 5 participants 8 messages Nov 13, 2023 – Nov 17, 2023

lore
Share & tools
lore

Bo Gan

author
#0 lore raw

Hi OpenSBI community again,

It's a little bit unclear to me from design point of view how are we going to protect the flattened device tree shared with lower privileged modes. I ask because in several places we have the following

1. generic/platform.c:

Quoted 9 lines

static void fw_platform_lookup_special(void *fdt, int root_offset) {
...

	match = fdt_match_node(fdt, root_offset, plat->match_table);
	if (!match)
		continue;

	generic_plat = plat;
	generic_plat_match = match;

generic_plat_match captures a data blob inside fdt.

2. generic/starfive/jh7110.c

Quoted 14 lines

static int starfive_jh7110_final_init(bool cold_boot,
const struct fdt_match *match)

{
	void *fdt = fdt_get_address();

	if (cold_boot) {
		fdt_reset_driver_init(fdt, &fdt_reset_pmic);
		return starfive_jh7110_inst_init(fdt);
	}

static int pm_reset_init(void *fdt, int nodeoff,
const struct fdt_match *match)
{
...

Quoted 6 lines
	rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
	if (rc)
		return rc;

	pmic_inst.dev_addr = addr;
	pmic_inst.compatible = match->compatible;

fdt_reset_pmic will capture a compatible string in pmic.compatible

For 1, it's probably safe unless we are using the same fdt from previous boot- loader and pass it to the next stage. (Could happen if using fw_jump and not defining FW_JUMP_FDT_ADDR). For 2, it's very likely unsafe. fdt_get_address will return root.next_arg1, which IS the fdt we pass to next stage. I don't think we can ever made the fdt to next stage as read-only, because they, such as U-boot or Linux kernel running in S-mode, usually will relocate it and reuse the memory for something else, or at least probably modify it in-place. When it happens, the harts, at runtime, regardless of cold/warm booted, will access the corrupted data. This can also be exploited by malicious actors.

In my opinion, the only place we access fdt should be from code boot hart, and we should stop accessing it and zero-out pointers captured before it kicks warm boot harts. If the fdt data is still need afterwards, either parse it or make a copy (perhaps on the heap). fdt_get_address() can also be enhanced to prevent it from being called in the wrong places. Please let me know your thoughts, and I'm looking forward to your ideas. Thanks!

Bo

#1 lore raw
Quoted 44 lines · On Mon, Nov 13, 2023 at 6:02?PM Bo Gan <[email protected]> wrote:

Hi OpenSBI community again,

It's a little bit unclear to me from design point of view how are we going to protect the flattened device tree shared with lower privileged modes. I ask because in several places we have the following

1. generic/platform.c:

Quoted 9 lines

static void fw_platform_lookup_special(void *fdt, int root_offset) {
...

      match = fdt_match_node(fdt, root_offset, plat->match_table);
      if (!match)
              continue;

      generic_plat = plat;
      generic_plat_match = match;

generic_plat_match captures a data blob inside fdt.

2. generic/starfive/jh7110.c

Quoted 14 lines

static int starfive_jh7110_final_init(bool cold_boot,
const struct fdt_match *match)

{
      void *fdt = fdt_get_address();

      if (cold_boot) {
              fdt_reset_driver_init(fdt, &fdt_reset_pmic);
              return starfive_jh7110_inst_init(fdt);
      }

static int pm_reset_init(void *fdt, int nodeoff,
const struct fdt_match *match)
{
...

Quoted 6 lines
      rc = fdt_get_node_addr_size(fdt, nodeoff, 0, &addr, NULL);
      if (rc)
              return rc;

      pmic_inst.dev_addr = addr;
      pmic_inst.compatible = match->compatible;

fdt_reset_pmic will capture a compatible string in pmic.compatible

Quoted 9 lines

For 1, it's probably safe unless we are using the same fdt from previous boot- loader and pass it to the next stage. (Could happen if using fw_jump and not defining FW_JUMP_FDT_ADDR). For 2, it's very likely unsafe. fdt_get_address will return root.next_arg1, which IS the fdt we pass to next stage. I don't think we can ever made the fdt to next stage as read-only, because they, such as U-boot or Linux kernel running in S-mode, usually will relocate it and reuse the memory for something else, or at least probably modify it in-place. When it happens, the harts, at runtime, regardless of cold/warm booted, will access the corrupted data. This can also be exploited by malicious actors.

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

Quoted 7 lines

In my opinion, the only place we access fdt should be from code boot hart, and we should stop accessing it and zero-out pointers captured before it kicks warm boot harts. If the fdt data is still need afterwards, either parse it or make a copy (perhaps on the heap). fdt_get_address() can also be enhanced to prevent it from being called in the wrong places. Please let me know your thoughts, and I'm looking forward to your ideas. Thanks!

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Regards,
Anup

Quoted 9 lines

Bo

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

author
#2 lore raw
Quoted 9 lines · Anup Patel

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

For #2, can @Minda Chen or @Wei Liang help take a look? I see no reason to have the const char *compatible field in struct pmic. The compatible string is always going to be "stf,axp15060-regulator" in the current implementation, so what's point of strcmp it again in pmic_ops? If it's for future extension, can you strcmp it inside pm_reset_init, and use an enum to indicate the model?

Bo

#3 lore raw

Hi Bo,

Quoted 16 lines · Bo Gan
Quoted 9 lines · Anup Patel

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Quoted 5 lines

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

If the FDT comes from U-Boot (the common case) or another prior stage (e.g. QEMU), that means the FDT address was provided in a register, and FW_FDT_PATH is *not* defined. So we should not assume FW_FDT_PATH is set.

Regards,
Samuel

author
#4 lore raw
Quoted 25 lines · Samuel Holland

Hi Bo,

Quoted 16 lines · Bo Gan
Quoted 9 lines · Anup Patel

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Quoted 5 lines

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

Quoted 7 lines

If the FDT comes from U-Boot (the common case) or another prior stage (e.g. QEMU), that means the FDT address was provided in a register, and FW_FDT_PATH is *not* defined. So we should not assume FW_FDT_PATH is set.

Regards,
Samuel

Hi Samuel, thanks for the reply. Yes, we should not assume FW_FDT_PATH, and if it's pretty common for real world/production setup to use the FDT from previous boot stage, I'd suggest we ensure there's never global variables or heap variables that can capture any pointer to FDT, and all access to FDT must happen before cold- boot done. Otherwise, the memory region contains FDT passed from previous bootstage must be protected, but surely it'll complicate things and could potentially involve platform specific handling.

Bo

#5 lore raw
In reply to Bo Gan
Quoted 17 lines · On Thu, Nov 16, 2023 at 5:56?AM Bo Gan <[email protected]> wrote:
Quoted 9 lines · Anup Patel

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Quoted 5 lines

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

That's an incorrect understanding of the FDT match table in OpenSBI.

The "generic_plat_match" points to a matching entry in a FDT match table provided by one of the platform override instances. It does not point to any part of the FDT. Please look at the fdt_match_node() implementation.

Quoted 7 lines

For #2, can @Minda Chen or @Wei Liang help take a look? I see no reason to have the const char *compatible field in struct pmic. The compatible string is always going to be "stf,axp15060-regulator" in the current implementation, so what's point of strcmp it again in pmic_ops? If it's for future extension, can you strcmp it inside pm_reset_init, and use an enum to indicate the model?

Quoted 6 lines

Bo

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

Regards,
Anup

author
#6 lore raw
Quoted 13 lines · Anup Patel
Quoted 11 lines · On Thu, Nov 16, 2023 at 5:56?AM Bo Gan <[email protected]> wrote:

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

Quoted 6 lines

That's an incorrect understanding of the FDT match table in OpenSBI.

The "generic_plat_match" points to a matching entry in a FDT match table provided by one of the platform override instances. It does not point to any part of the FDT. Please look at the fdt_match_node() implementation.

Hi Anup, you are right, generic_plat_match won't capture the FDT. Thanks for clarifying this.

Bo

#7 lore raw
In reply to Bo Gan
Quoted 22 lines · Bo Gan
Quoted 9 lines · Anup Patel

Yes, #1 is mostly safe.

The #2 needs to be fixed in the generic/starfive/jh7110.c and all other places where it exists.

We should certainly stop accessing FDT after cold boot hard is done but I don't think we should zero-out pointers as well.

Thanks for the reply, Anup. I did a little bit more digging on this and I think #1 is only safe when OpenSBI's carrying the FDT itself (through FW_FDT_PATH). Otherwise, we have to make sure FDT will never be accessed after cold boot done. By zeroing-out the pointers, we can avoid speculative access as well. This helps to defense against speculative timing attacks. (Maybe I'm too paranoid?).

Given OpenSBI starts at very early stage, it's probably uncommon to not have FW_FDT_PATH defined. I have a JH7110 on hand, and the OpenSBI (loaded by U-Boot SPL) will carry the FDT built from U-Boot. I'd imagine other boards/vendors do it similarly. Just in case folks really don't have FW_FDT_PATH, can we emit a warning, and suggest the FDT to be protected through other mechanism?

Quoted 7 lines

For #2, can @Minda Chen or @Wei Liang help take a look? I see no reason to have the const char *compatible field in struct pmic. The compatible string is always going to be "stf,axp15060-regulator" in the current implementation, so what's point of strcmp it again in pmic_ops? If it's for future extension, can you strcmp it inside pm_reset_init, and use an enum to indicate the model?

Bo

Now just one pmic now. the compatible can be deleted.

But I don't know what are you changed and 1# 2# mean. I can not find your patch in opensbi mail website.