Skip to contentSkip to conversation

env: Enhancements for establishing variable protection

PATCH In review Jan Kiszka 3 participants 9 messages Feb 3, 2023 – Feb 10, 2023

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

cover env: Enhancements for establishing variable protection

This was factored out of [1] upon request in the hope of easing the merge.

Jan

[1] https://lore.kernel.org/u-boot/[email protected]

CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>

Jan Kiszka (2):
env: Complete generic support for writable list
env: Couple networking-related variable flags to CONFIG_NET

 env/Kconfig         |  1 +
 env/env.c           |  8 ++++++++
 env/flags.c         | 10 +++++-----
 include/env_flags.h |  4 ++--
 4 files changed, 16 insertions(+), 7 deletions(-)
Signature · 1 line
2.35.3
author
#1 lore raw

PATCH 1/2 env: Complete generic support for writable list

From: Jan Kiszka <[email protected]>

This completes what 890feecaab72 started by selecting ENV_APPEND and loading the default env before any other sources. This ensures that load operations pick up all non-writable vars from the default env and only permitted parts from other locations according to the regular priorities.

With this change, boards only need to define the list of writable variables but no longer have to provide a custom env_get_location implementation.

CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>
---
 env/Kconfig | 1 +
 env/env.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
env/Kconfig +1 -0
index c409ea71fe5..6e24eee55f2 100644
@@ -733,6 +733,7 @@ config ENV_APPEND
 
 config ENV_WRITEABLE_LIST
 	bool "Permit write access only to listed variables"
+	select ENV_APPEND
 	help
 	  If defined, only environment variables which explicitly set the 'w'
 	  writeable flag can be written and modified at runtime. No variables
env/env.c +8 -0
index 06078c7f374..45e638fcd1f 100644
@@ -195,6 +195,14 @@ int env_load(void)
 	int best_prio = -1;
 	int prio;
 
+	if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
+		/*
+		 * When using a list of writeable variables, the baseline comes
+		 * from the built-in default env. So load this first.
+		 */
+		env_set_default(NULL, 0);
+	}
+
 	for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
 		int ret;
 
Signature · 1 line
2.35.3
author
#6 lore raw

PATCH 2/2 env: Couple networking-related variable flags to CONFIG_NET

In reply to Jan Kiszka

From: Jan Kiszka <[email protected]>

Boards may set networking variables programmatically, thus may have CONFIG_NET on but CONFIG_CMD_NET off. The IOT2050 is an example.

CC Joe Hershberger <[email protected]>
---
 env/flags.c         | 10 +++++-----
 include/env_flags.h |  4 ++--
 2 files changed, 7 insertions(+), 7 deletions(-)
env/flags.c +5 -5
index e3e833c4333..e2866361dfe 100644
@@ -22,7 +22,7 @@
 #include <env_internal.h>
 #endif
 
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 #define ENV_FLAGS_NET_VARTYPE_REPS "im"
 #else
 #define ENV_FLAGS_NET_VARTYPE_REPS ""
@@ -57,7 +57,7 @@ static const char * const env_flags_vartype_names[] = {
 	"decimal",
 	"hexadecimal",
 	"boolean",
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 	"IP address",
 	"MAC address",
 #endif
@@ -211,7 +211,7 @@ static void skip_num(int hex, const char *value, const char **end,
 		*end = value;
 }
 
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 int eth_validate_ethaddr_str(const char *addr)
 {
 	const char *end;
@@ -244,7 +244,7 @@ static int _env_flags_validate_type(const char *value,
 	enum env_flags_vartype type)
 {
 	const char *end;
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 	const char *cur;
 	int i;
 #endif
@@ -273,7 +273,7 @@ static int _env_flags_validate_type(const char *value,
 		if (value[1] != '\0')
 			return -1;
 		break;
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 	case env_flags_vartype_ipaddr:
 		cur = value;
 		for (i = 0; i < 4; i++) {
include/env_flags.h +2 -2
index 6bd574c2bdb..7de58cc57c3 100644
@@ -12,7 +12,7 @@ enum env_flags_vartype {
 	env_flags_vartype_decimal,
 	env_flags_vartype_hex,
 	env_flags_vartype_bool,
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 	env_flags_vartype_ipaddr,
 	env_flags_vartype_macaddr,
 #endif
@@ -121,7 +121,7 @@ enum env_flags_varaccess env_flags_parse_varaccess(const char *flags);
  */
 enum env_flags_varaccess env_flags_parse_varaccess_from_binflags(int binflags);
 
-#ifdef CONFIG_CMD_NET
+#ifdef CONFIG_NET
 /*
  * Check if a string has the format of an Ethernet MAC address
  */
Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH 1/2] env: Complete generic support for writable list

In reply to Jan Kiszka

Hi Jan,

Quoted 55 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This completes what 890feecaab72 started by selecting ENV_APPEND and loading the default env before any other sources. This ensures that load operations pick up all non-writable vars from the default env and only permitted parts from other locations according to the regular priorities.

With this change, boards only need to define the list of writable variables but no longer have to provide a custom env_get_location implementation.

CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>
---
 env/Kconfig | 1 +
 env/env.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
env/Kconfig +1 -0
index c409ea71fe5..6e24eee55f2 100644
@@ -733,6 +733,7 @@ config ENV_APPEND
 
 config ENV_WRITEABLE_LIST
        bool "Permit write access only to listed variables"
+       select ENV_APPEND
        help
          If defined, only environment variables which explicitly set the 'w'
          writeable flag can be written and modified at runtime. No variables
env/env.c +8 -1
index 06078c7f374..45e638fcd1f 100644
@@ -195,6 +195,14 @@ int env_load(void)
        int best_prio = -1;
        int prio;
 
+       if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
+               /*
+                * When using a list of writeable variables, the baseline comes
+                * from the built-in default env. So load this first.
+                */
+               env_set_default(NULL, 0);
+       }
+
        for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
                int ret;
 
--
Quoted 2 lines

2.35.3

This looks OK, but please can you write some tests in test/env ?

Regards,
SImon

author
#3 lore raw

Re: [PATCH 1/2] env: Complete generic support for writable list

Quoted 57 lines · Simon Glass

Hi Jan,

Quoted 54 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This completes what 890feecaab72 started by selecting ENV_APPEND and loading the default env before any other sources. This ensures that load operations pick up all non-writable vars from the default env and only permitted parts from other locations according to the regular priorities.

With this change, boards only need to define the list of writable variables but no longer have to provide a custom env_get_location implementation.

CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>
---
 env/Kconfig | 1 +
 env/env.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
env/Kconfig +1 -0
index c409ea71fe5..6e24eee55f2 100644
@@ -733,6 +733,7 @@ config ENV_APPEND
 
 config ENV_WRITEABLE_LIST
        bool "Permit write access only to listed variables"
+       select ENV_APPEND
        help
          If defined, only environment variables which explicitly set the 'w'
          writeable flag can be written and modified at runtime. No variables
env/env.c +8 -0
index 06078c7f374..45e638fcd1f 100644
@@ -195,6 +195,14 @@ int env_load(void)
        int best_prio = -1;
        int prio;
 
+       if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
+               /*
+                * When using a list of writeable variables, the baseline comes
+                * from the built-in default env. So load this first.
+                */
+               env_set_default(NULL, 0);
+       }
+
        for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
                int ret;
 
Quoted 5 lines
Quoted 3 lines

--
2.35.3

This looks OK, but please can you write some tests in test/env ?

Looking at those, it seems there is nothing at all regarding access flags yet. Any suggestions how to first of all build up that infrastructure are welcome. Then I could add this aspect here on top.

Jan

Signature · 2 lines
Siemens AG, Technology
Competence Center Embedded Linux
#4 lore raw

Re: [PATCH 1/2] env: Complete generic support for writable list

()Hi Jan,

Quoted 65 lines · Jan Kiszka
Quoted 57 lines · Simon Glass

Hi Jan,

Quoted 54 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This completes what 890feecaab72 started by selecting ENV_APPEND and loading the default env before any other sources. This ensures that load operations pick up all non-writable vars from the default env and only permitted parts from other locations according to the regular priorities.

With this change, boards only need to define the list of writable variables but no longer have to provide a custom env_get_location implementation.

CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>
---
 env/Kconfig | 1 +
 env/env.c   | 8 ++++++++
 2 files changed, 9 insertions(+)
env/Kconfig +1 -0
index c409ea71fe5..6e24eee55f2 100644
@@ -733,6 +733,7 @@ config ENV_APPEND
 
 config ENV_WRITEABLE_LIST
        bool "Permit write access only to listed variables"
+       select ENV_APPEND
        help
          If defined, only environment variables which explicitly set the 'w'
          writeable flag can be written and modified at runtime. No variables
env/env.c +8 -0
index 06078c7f374..45e638fcd1f 100644
@@ -195,6 +195,14 @@ int env_load(void)
        int best_prio = -1;
        int prio;
 
+       if (CONFIG_IS_ENABLED(ENV_WRITEABLE_LIST)) {
+               /*
+                * When using a list of writeable variables, the baseline comes
+                * from the built-in default env. So load this first.
+                */
+               env_set_default(NULL, 0);
+       }
+
        for (prio = 0; (drv = env_driver_lookup(ENVOP_LOAD, prio)); prio++) {
                int ret;
 
Quoted 5 lines
Quoted 3 lines

--
2.35.3

This looks OK, but please can you write some tests in test/env ?

Quoted 3 lines

Looking at those, it seems there is nothing at all regarding access flags yet. Any suggestions how to first of all build up that infrastructure are welcome. Then I could add this aspect here on top.

Yes, starting something a bit new is always harder. My approach is normally to just start small. Anything is better than nothing, and it provides something for others to build on.

There are a few tests for hashtable which could be a way to start this.

I suggest starting a new env.c file in there with a few env_get() and env_set() calls. You could also use himport_r() to test out different flags.

I'm not sure how easy it would be to call env_load() in a test, but it might work.

The trickier thing (perhaps to worry about later) is that you are (correctly) using a CONFIG option to enable the feature. For sandbox tests we typically want all features to be enabled at build time (e.g. default y if SANDBOX), then select them at runtime, so we can test them. See test_eth_enabled() for an example of how this is done.

Regards,
Simon

#5 lore raw

Re: [PATCH 1/2] env: Complete generic support for writable list

In reply to Jan Kiszka
Quoted 12 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

This completes what 890feecaab72 started by selecting ENV_APPEND and loading the default env before any other sources. This ensures that load operations pick up all non-writable vars from the default env and only permitted parts from other locations according to the regular priorities.

With this change, boards only need to define the list of writable variables but no longer have to provide a custom env_get_location implementation.

Quoted 5 lines
CC Joe Hershberger <[email protected]>
CC Marek Vasut <[email protected]>
CC Stefan Herbrechtsmeier <[email protected]>

Applied to u-boot/master, thanks!

Signature · 1 line
Tom
#8 lore raw

Re: [PATCH 2/2] env: Couple networking-related variable flags to CONFIG_NET

In reply to Jan Kiszka
Quoted 8 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

Boards may set networking variables programmatically, thus may have CONFIG_NET on but CONFIG_CMD_NET off. The IOT2050 is an example.

Applied to u-boot/master, thanks!

Signature · 1 line
Tom