Skip to contentSkip to conversation

env: Make _init() expect _INVALID when _IS_NOWHERE

PATCH In review Pierre-Clément Tosi 3 participants 5 messages Aug 12, 2021 – Sep 24, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 env: Make _init() expect _INVALID when _IS_NOWHERE

Avoid applying the "fix" introduced by commit 5557eec01cbf ("env: Fix invalid env handling in env_init()") to the environment "nowhere".

This is necessary as that commit, by setting the return value of env_init() to -ENOENT if gd->env_valid is ENV_INVALID, forces that function to reset gd->env_valid to ENV_VALID. By doing so, it breaks the assumption (required by ENV_IS_NOWHERE) that gd->env_valid must be ENV_INVALID.

This, in turn, results in env_relocate() calling env_load() (it should not), which itself, calls U_BOOT_ENV_LOCATION(nowhere).load() i.e. env_nowhere_load(). That function, being implemented under the assumption mentioned above, calls env_set_default(), which in turn, seeing that gd->env_valid is ENV_VALID (it should not), tries to dereference whatever lies in gd->env_addr (most likely garbage), leading to a faulty memory access.

Note that other env_locations might be concerned by this bug but that this commit only intends to fix it for when ENV_IS_NOWHERE.

Fixes 5557eec01cbf ("env: Fix invalid env handling in env_init()")
---
 env/env.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
env/env.c +1 -1
index e534008006..0a0f234747 100644
@@ -336,7 +336,7 @@ int env_init(void)
 		debug("%s: Environment %s init done (ret=%d)\n", __func__,
 		      drv->name, ret);
 
-		if (gd->env_valid == ENV_INVALID)
+		if (gd->env_valid == ENV_INVALID && drv->location != ENVL_NOWHERE)
 			ret = -ENOENT;
 	}
 
Signature · 5 lines
2.32.0.605.g8dce9f2422-goog


-- 
Pierre
#1 lore raw
Quoted 17 lines · Pierre-Clément Tosi

Avoid applying the "fix" introduced by commit 5557eec01cbf ("env: Fix invalid env handling in env_init()") to the environment "nowhere".

This is necessary as that commit, by setting the return value of env_init() to -ENOENT if gd->env_valid is ENV_INVALID, forces that function to reset gd->env_valid to ENV_VALID. By doing so, it breaks the assumption (required by ENV_IS_NOWHERE) that gd->env_valid must be ENV_INVALID.

This, in turn, results in env_relocate() calling env_load() (it should not), which itself, calls U_BOOT_ENV_LOCATION(nowhere).load() i.e. env_nowhere_load(). That function, being implemented under the assumption mentioned above, calls env_set_default(), which in turn, seeing that gd->env_valid is ENV_VALID (it should not), tries to dereference whatever lies in gd->env_addr (most likely garbage), leading to a faulty memory access.

Quoted 5 lines

Note that other env_locations might be concerned by this bug but that this commit only intends to fix it for when ENV_IS_NOWHERE.

Fixes 5557eec01cbf ("env: Fix invalid env handling in env_init()")

Such patch was already posted, see the patch and the discussion, in the end there was a different fix for the relocation code:

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

#2 lore raw
In reply to Pierre-Clément Tosi
Quoted 17 lines · Pierre-Clément Tosi

Avoid applying the "fix" introduced by commit 5557eec01cbf ("env: Fix invalid env handling in env_init()") to the environment "nowhere".

This is necessary as that commit, by setting the return value of env_init() to -ENOENT if gd->env_valid is ENV_INVALID, forces that function to reset gd->env_valid to ENV_VALID. By doing so, it breaks the assumption (required by ENV_IS_NOWHERE) that gd->env_valid must be ENV_INVALID.

This, in turn, results in env_relocate() calling env_load() (it should not), which itself, calls U_BOOT_ENV_LOCATION(nowhere).load() i.e. env_nowhere_load(). That function, being implemented under the assumption mentioned above, calls env_set_default(), which in turn, seeing that gd->env_valid is ENV_VALID (it should not), tries to dereference whatever lies in gd->env_addr (most likely garbage), leading to a faulty memory access.

Quoted 5 lines

Note that other env_locations might be concerned by this bug but that this commit only intends to fix it for when ENV_IS_NOWHERE.

Fixes 5557eec01cbf ("env: Fix invalid env handling in env_init()")

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#3 lore raw
Quoted 19 lines · Tom Rini
Quoted 17 lines · Pierre-Clément Tosi

Avoid applying the "fix" introduced by commit 5557eec01cbf ("env: Fix invalid env handling in env_init()") to the environment "nowhere".

This is necessary as that commit, by setting the return value of env_init() to -ENOENT if gd->env_valid is ENV_INVALID, forces that function to reset gd->env_valid to ENV_VALID. By doing so, it breaks the assumption (required by ENV_IS_NOWHERE) that gd->env_valid must be ENV_INVALID.

This, in turn, results in env_relocate() calling env_load() (it should not), which itself, calls U_BOOT_ENV_LOCATION(nowhere).load() i.e. env_nowhere_load(). That function, being implemented under the assumption mentioned above, calls env_set_default(), which in turn, seeing that gd->env_valid is ENV_VALID (it should not), tries to dereference whatever lies in gd->env_addr (most likely garbage), leading to a faulty memory access.

Quoted 7 lines
Quoted 5 lines

Note that other env_locations might be concerned by this bug but that this commit only intends to fix it for when ENV_IS_NOWHERE.

Fixes 5557eec01cbf ("env: Fix invalid env handling in env_init()")

Applied to u-boot/next, thanks!

Please drop this, quote from my previous reply to this patch:

"
Such patch was already posted, see the patch and the discussion, in the end there was a different fix for the relocation code:

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
"

#4 lore raw
Quoted 28 lines · Marek Vasut
Quoted 19 lines · Tom Rini
Quoted 17 lines · Pierre-Clément Tosi

Avoid applying the "fix" introduced by commit 5557eec01cbf ("env: Fix invalid env handling in env_init()") to the environment "nowhere".

This is necessary as that commit, by setting the return value of env_init() to -ENOENT if gd->env_valid is ENV_INVALID, forces that function to reset gd->env_valid to ENV_VALID. By doing so, it breaks the assumption (required by ENV_IS_NOWHERE) that gd->env_valid must be ENV_INVALID.

This, in turn, results in env_relocate() calling env_load() (it should not), which itself, calls U_BOOT_ENV_LOCATION(nowhere).load() i.e. env_nowhere_load(). That function, being implemented under the assumption mentioned above, calls env_set_default(), which in turn, seeing that gd->env_valid is ENV_VALID (it should not), tries to dereference whatever lies in gd->env_addr (most likely garbage), leading to a faulty memory access.

Quoted 7 lines
Quoted 5 lines

Note that other env_locations might be concerned by this bug but that this commit only intends to fix it for when ENV_IS_NOWHERE.

Fixes 5557eec01cbf ("env: Fix invalid env handling in env_init()")

Applied to u-boot/next, thanks!

Quoted 8 lines

Please drop this, quote from my previous reply to this patch:

"
Such patch was already posted, see the patch and the discussion, in the end there was a different fix for the relocation code:

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
"

I'll drop this. FWIW, my reading of the previous patch, and then this being posted afterwards is why I had applied this patch.

Signature · 1 line
Tom