cmd: nvedit: add whitelist option for env import
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
While the env export can take as parameters variables to be exported, env import does not have such a mechanism of variable selection.
Let's add a -w option that asks env import to look for the whitelisted_vars env variable for a space-separated list of variables that are whitelisted.
Every env variable present in env at addr and in whitelisted_vars env variable will override the value of the variable in the current env. All the remaining variables are left untouched.
One of its use case could be to load a secure environment from the signed U-Boot binary and load only a handful of variables from an other, unsecure, environment without completely losing control of U-Boot.
---
v2:
- use strdup instead of malloc + strcpy,
- NULL-check the result of strdup,
- add common exit path for freeing memory in one unique place,
- store token pointer from strtok within the char** array instead of strdup-ing token within elements of array,
cmd/nvedit.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 11 deletions(-)
cmd/nvedit.c +68 -11
@@ -971,7 +971,7 @@ sep_err:#ifdef CONFIG_CMD_IMPORTENV /* -* env import [-d] [-t [-r] | -b | -c] addr [size] +* env import [-d] [-t [-r] | -b | -c] [-w] addr [size] * -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the
@@ -982,6 +982,10 @@ sep_err:* for line endings. Only effective in addition to -t. * -b: assume binary format ('\0' separated, "\0\0" terminated) * -c: assume checksum protected environment format +* -w: specify that whitelisting of variables should be used when +* importing environment. The space-separated list of variables +* that should override the ones in current environment is stored +* in `whitelisted_vars`. * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory
@@ -990,18 +994,22 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { ulong addr; -char *cmd, *ptr; +char *cmd, *ptr, *tmp; +char **array =NULL ;char sep ='\n' ;int chk =0 ;int fmt =0 ;int del =0 ;int crlf_is_lf =0 ; +int wl =0 ; +int wl_count =0 ; +int ret =0 ;size_t size; cmd = *argv;while (--argc >0 && **++argv =='-' ) { -char *arg = *argv; +char *arg = *argv, *str, *token;while (*++arg) {switch (*arg) {case 'b' :/* raw binary format */
@@ -1025,6 +1033,43 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,break ;case 'd' : del =1 ; +break ; +case 'w' : + wl =1 ; + wl_count =1 ; + + str =env_get ("whitelisted_vars" ); +if (!str) { +puts ("## Error: whitelisted_vars is not set.\n" ); +return CMD_RET_USAGE ; + } + + tmp =strdup (str); +if (!tmp) +return CMD_RET_FAILURE ; + + token =strchr (tmp,' ' ); +while (!token) { + wl_count++; + token =strchr (token +1 ,' ' ); + } + +strcpy (tmp, str); + + wl_count =0 ; + array =malloc (sizeof (char *) * wl_count); +if (!array) { +free (tmp); +return CMD_RET_FAILURE ; + } + + token =strtok (tmp," " ); +while (token) { + array[wl_count] = token; + wl_count++; + token =strtok (NULL ," " ); + } +break ;default :return CMD_RET_USAGE ;
@@ -1032,8 +1077,10 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, } } -if (argc <1 ) -return CMD_RET_USAGE ; +if (argc <1 ) { + ret =CMD_RET_USAGE ; + goto exit; + }if (!fmt)printf ("## Warning: defaulting to text format\n" );
@@ -1048,7 +1095,8 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, size =simple_strtoul (argv[1 ],NULL ,16 ); }else if (argc ==1 && chk) {puts ("## Error: external checksum format must pass size\n" ); -return CMD_RET_FAILURE ; + ret =CMD_RET_FAILURE ; + goto exit; }else {char *s = ptr;
@@ -1077,19 +1125,28 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,if (crc32 (0 , ep->data, size) != crc) {puts ("## Error: bad CRC, import failed\n" ); -return 1 ; + ret =1 ; + goto exit; } ptr = (char *)ep->data; }if (himport_r (&env_htab, ptr, size, sep, del ?0 :H_NOCLEAR , - crlf_is_lf,0 ,NULL ) ==0 ) { + crlf_is_lf, wl ? wl_count :0 , wl ? array :NULL ) ==0 ) {pr_err ("Environment import failed: errno = %d\n" , errno); -return 1 ; + + ret =1 ; + goto exit; } gd->flags |=GD_FLG_ENV_READY ; -return 0 ; +exit: +if (wl) { +free (tmp); +free (array); + } + +return ret; sep_err: printf("## %s: only one of \"-b\", \"-c\" or \"-t\" allowed\n",
@@ -1212,7 +1269,7 @@ static char env_help_text[] =#endif #endif #if defined(CONFIG_CMD_IMPORTENV ) -"env import [-d] [-t [-r] | -b | -c] addr [size] - import environment\n" +"env import [-d] [-t [-r] | -b | -c] [-w] addr [size] - import environment\n" #endif "env print [-a | name ...] - print environment\n"#if defined(CONFIG_CMD_RUN )
Signature · 1 line
2.14.1
PATCH v2 2/2 test/py: add test for whitelist of variables while importing environment
This tests that the importing of an environment with a specified whitelist works as intended.
If the variable whitelisted_vars is not set, the env importing should fail with a given message.
For each variable separated by spaces in whitelisted_vars, if
- foo is bar in current env and bar2 in exported env, after importing exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env, after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is in the whitelisted_vars), after importing exported env, foo shall be empty,
Any variable not in whitelisted_vars should be left untouched.
---
added in v2
test/py/tests/test_env.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
test/py/tests/test_env.py +40 -0
@@ -6,6 +6,7 @@ # Test operation of shell commands relating to environment variables. import pytest +import u_boot_utils # FIXME: This might be useful for other tests; # perhaps refactor it into ConsoleBase or some other state object?
@@ -231,3 +232,42 @@ def test_env_expansion_spaces(state_test_env): unset_var(state_test_env, var_space) if var_test: unset_var(state_test_env, var_test) + +def test_env_import_whitelist_empty(state_test_env): + """Test trying to import 0 variables from an environment""" + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) + addr = '%08x' % ram_base + + set_var(state_test_env, "foo1", "bar1") + + state_test_env.u_boot_console.run_command('env export %s' % addr) + + c = state_test_env.u_boot_console + with c.disable_check('error_notification'): + response = c.run_command('env import -w %s' % addr) + assert(response.startswith('## Error: whitelisted_vars is not set')) + +def test_env_import_whitelist(state_test_env): + """Test importing only a handful of env variables from an environment""" + ram_base = u_boot_utils.find_ram_base(state_test_env.u_boot_console) + addr = '%08x' % ram_base + + #no foo1 in current env, foo2 overridden, foo3 should be of the value + #before exporting and foo4 should be deleted + set_var(state_test_env, "whitelisted_vars", "foo1 foo2 foo4") + set_var(state_test_env, "foo1", "bar1") + set_var(state_test_env, "foo2", "bar2") + set_var(state_test_env, "foo3", "bar3") + + state_test_env.u_boot_console.run_command('env export %s' % addr) + + unset_var(state_test_env, "foo1") + set_var(state_test_env, "foo2", "test2") + set_var(state_test_env, "foo4", "bar4") + + state_test_env.u_boot_console.run_command('env import -w %s' % addr) + + validate_set(state_test_env, "foo1", "bar1") + validate_set(state_test_env, "foo2", "bar2") + validate_set(state_test_env, "foo3", "bar3") + validate_empty(state_test_env, "foo4")
Signature · 1 line
2.14.1
PATCH v2 2/2 test/py: add test for whitelist of variables while importing environment
Quoted 15 lines · Quentin Schulz
This tests that the importing of an environment with a specified whitelist works as intended.
If the variable
whitelisted_varsis not set, the env importing should fail with a given message.For each variable separated by spaces in
whitelisted_vars, if
- foo is bar in current env and bar2 in exported env, after importing exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env, after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is in thewhitelisted_vars), after importing exported env, foo shall be empty,
Quoted 1 line
Any variable not in
whitelisted_varsshould be left untouched.
test/py/tests/test_env.py def test_env_expansion_spaces(state_test_env): · line 273
Quoted 10 lines
test/py/tests/test_env.py +7
-0+def test_env_import_whitelist(state_test_env): + state_test_env.u_boot_console.run_command('env import -w %s' % addr) + + validate_set(state_test_env, "foo1", "bar1") + validate_set(state_test_env, "foo2", "bar2") + validate_set(state_test_env, "foo3", "bar3") + validate_empty(state_test_env, "foo4")
It might make sense to iterate over *all* variables and make sure that they have the same value before/after the import. But, the current code seems to cover the basic cases, so it's probably not strictly necessary to do that.
PATCH v2 2/2 test/py: add test for whitelist of variables while importing environment
Hi Stephen,
Quoted 31 lines · Stephen Warren
Quoted 15 lines · Quentin Schulz
This tests that the importing of an environment with a specified whitelist works as intended.
If the variable
whitelisted_varsis not set, the env importing should fail with a given message.For each variable separated by spaces in
whitelisted_vars, if
- foo is bar in current env and bar2 in exported env, after importing exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env, after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is in thewhitelisted_vars), after importing exported env, foo shall be empty,Quoted 1 line
Any variable not in
whitelisted_varsshould be left untouched.Acked-by Stephen Warren <[email protected]>test/py/tests/test_env.py def test_env_expansion_spaces(state_test_env): · line 273
Quoted 10 lines
test/py/tests/test_env.py +7
-0+def test_env_import_whitelist(state_test_env): + state_test_env.u_boot_console.run_command('env import -w %s' % addr) + + validate_set(state_test_env, "foo1", "bar1") + validate_set(state_test_env, "foo2", "bar2") + validate_set(state_test_env, "foo3", "bar3") + validate_empty(state_test_env, "foo4")
Quoted 3 lines
It might make sense to iterate over *all* variables and make sure that they have the same value before/after the import. But, the current code seems to cover the basic cases, so it's probably not strictly necessary to do that.
I would advocate for as many test sets as possible, each one testing a very specific scenario. That way, it's easy to know which scenario failed instead of having to find within a big scenario what failed and why. Here, I just want to test that importing some env variables work.
It would make sense to have a small test that tests that setting an environment variable works (that what's done with test_env_set* test functions I think) and others to test the exporting of variables and the importing of some others with all the possible/impossible arguments of these cli commands. I wish I could test this importing without manually exporting an environnement before to reduce "dependencies" of the test and thus keep the test scenario to the actual command we're testing.
However the more the arguments of the commands, the more functions to write ("exponentially") as we have to test each and every case. The best would be to also have tests for expected and wanted failures. I think it's a good start for now but more needs to be done in the env testing regarding (at least) env exporting and importing.
Quentin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/92f4b525/attachment.sig>
PATCH v2 2/2 test/py: add test for whitelist of variables while importing environment
In reply to Quentin SchulzQuoted 17 lines · Quentin Schulz
This tests that the importing of an environment with a specified whitelist works as intended.
If the variable
whitelisted_varsis not set, the env importing should fail with a given message.For each variable separated by spaces in
whitelisted_vars, if
- foo is bar in current env and bar2 in exported env, after importing exported env, foo shall be bar2,
- foo does not exist in current env and foo is bar2 in exported env, after importing exported env, foo shall be bar2,
- foo is bar in current env and does not exist in exported env (but is in thewhitelisted_vars), after importing exported env, foo shall be empty,Any variable not in
whitelisted_varsshould be left untouched.
Quoted 7 lines
Signed-off-by Quentin Schulz <[email protected]>---
added in v2
test/py/tests/test_env.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
In reply to Quentin SchulzQuoted 6 lines · Quentin Schulz
While the
env exportcan take as parameters variables to be exported,env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list of variables that are whitelisted.
Would it be better for the -w option to accept a variable name rather than hard-coding it as whitelisted_vars? That way, people could import/export different sets of variables at different times, and also could choose a more use-case-specific variable name than whitelisted_vars in order to describe why those variables are "whitelisted".
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Hi Stephen,
Quoted 8 lines · Stephen Warren
Quoted 6 lines · Quentin Schulz
While the
env exportcan take as parameters variables to be exported,env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list of variables that are whitelisted.
Quoted 5 lines
Would it be better for the -w option to accept a variable name rather than hard-coding it as
whitelisted_vars? That way, people could import/export different sets of variables at different times, and also could choose a more use-case-specific variable name thanwhitelisted_varsin order to describe why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by Alex Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_vars in the environnement,
Proposition 2: Have -w followed by one string-word which is the name of the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for,
-: a bit of complexity added to the parsing of the parameters of the env
import function,
Proposition 3: Have -w followed by the list of whitelisted env variable, +: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with size being optional => how to know if size was passed or not?), what char can be used to separate env variables in the list?
how does it perform with a very long list of whitelisted variables?
I don't know how many features and their complexity we can add to the env import/export functions, but keeping their arguments simple is a way to keep features hopefully easy to implement in the future.
I'm not convinced by any of the propositions but proposition 1 is the one I implemented for now. I just want a consensus towards a solution (be it among the given 3 or another one) and I'm good with it.
Thanks,
Quentin
[1] https://www.mail-archive.com/u-boot at lists.denx.de/msg286266.html
[2] http://patchwork.ozlabs.org/patch/891422/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/963238a5/attachment.sig>
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Quoted 5 lines · Quentin Schulz
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to be
exported,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list of
variables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name rather
than
Quoted 2 lines
Quoted 2 lines
hard-coding it as
whitelisted_vars? That way, people could import/export different sets of variables at different times, and also could choose a
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order to
describe
Quoted 21 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by Alex Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name of the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for,
-: a bit of complexity added to the parsing of the parameters of the env
import function,
Quoted 8 lines
Proposition 3: Have -w followed by the list of whitelisted env variable, +: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with size being optional => how to know if size was passed or not?), what char can be used to separate env variables in the list?
how does it perform with a very long list of whitelisted variables?
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
- specify multiple -w options each specifying a whitelisted env variable
- use the remaining arguments approach and eat all the trailing arguments as the names of env vars you import - needs a sentinel value for the size argument
Signature · 1 line
Alex Kiernan
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Hi Alex,
Quoted 54 lines · Alex Kiernan
Quoted 5 lines · Quentin Schulz
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name rather
than
Quoted 2 lines
Quoted 2 lines
hard-coding it as
whitelisted_vars? That way, people could import/export different sets of variables at different times, and also could choose amore
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order todescribe
Quoted 21 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by Alex Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name of the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for,
-: a bit of complexity added to the parsing of the parameters of the env
import function,Quoted 8 lines
Proposition 3: Have -w followed by the list of whitelisted env variable, +: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with size being optional => how to know if size was passed or not?), what char can be used to separate env variables in the list?
how does it perform with a very long list of whitelisted variables?
Quoted 4 lines
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
- specify multiple -w options each specifying a whitelisted env variable
You'll hit the maximum number of arguments/length of the command quickly with this method. Quicker than with the other propositions.
Moreover, this can make the command painfully long, painful to read and thus cumbersome to find the small typo in your command.
Quoted 4 lines
- use the remaining arguments approach and eat all the trailing arguments as the names of env vars you import - needs a sentinel value for the size argument
That can't work I think.
How do you know if the size argument was passed or not? How'd you know what string is addr, size or the whitelist (if there is even any)?
env import foo1 foo2 foo3 foo4 addr size
env import foo1 foo2 foo3 addr
env import addr size
env import addr
Quentin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/3b140572/attachment.sig>
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Quoted 4 lines · Quentin Schulz
Hi Alex,
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 9:02 AM Quentin Schulz <
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines · wrote:
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name
rather
Quoted 2 lines
Quoted 2 lines
than
Quoted 1 line
Quoted 1 line
hard-coding it as
whitelisted_vars? That way, people could
import/export
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
different sets of variables at different times, and also could
choose a
Quoted 7 lines
Quoted 7 lines
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order todescribe
Quoted 4 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by
Alex
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name
of
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for, -: a bit of complexity added to the parsing of the parameters of the
env
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
import function,
Proposition 3: Have -w followed by the list of whitelisted env
variable,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with size
being
Quoted 1 line
Quoted 1 line
Quoted 1 line
optional => how to know if size was passed or not?), what char
can be
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
used to separate env variables in the list? how does it perform with a very long list of whitelisted
variables?
Quoted 5 lines
Quoted 5 lines
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
Quoted 9 lines
Quoted 1 line
- specify multiple -w options each specifying a whitelisted env variable
You'll hit the maximum number of arguments/length of the command quickly with this method. Quicker than with the other propositions.
Moreover, this can make the command painfully long, painful to read and thus cumbersome to find the small typo in your command.
Quoted 1 line
- use the remaining arguments approach and eat all the trailing
arguments
Quoted 1 line
Quoted 1 line
as the names of env vars you import - needs a sentinel value for the
size
Quoted 5 lines
Quoted 2 lines
argument
That can't work I think.
Quoted 7 lines
How do you know if the size argument was passed or not? How'd you know what string is addr, size or the whitelist (if there is even any)?
env import foo1 foo2 foo3 foo4 addr size
env import foo1 foo2 foo3 addr
env import addr size
env import addr
That's why you need a sentinel for the size:
* env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
* -d: delete existing environment before importing;
* otherwise overwrite / append to existing definitions
* -t: assume text format; either "size" must be given or the
* text data must be '\0' terminated
* -r: handle CRLF like LF, that means exported variables with
* a content which ends with \r won't get imported. Used
* to import text files created with editors which are using
CRLF
* for line endings. Only effective in addition to -t.
* -b: assume binary format ('\0' separated, "\0\0" terminated)
* -c: assume checksum protected environment format
* addr: memory address to read from
* size: length of input data; if missing, proper '\0'
* termination is mandatory. If not required and passing
* variables to import use '-' * var...: List of variable names that get imported. Without arguments,
* all variables are imported
Which for your examples translates to:
env import addr size foo1 foo2 foo3 foo4
env import addr - foo1 foo2 foo3
env import addr size
env import addr
Signature · 1 line
Alex Kiernan
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Quoted 121 lines · Alex Kiernan
Quoted 4 lines · Quentin Schulz
Hi Alex,
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 9:02 AM Quentin Schulz <
quentin.schulz at bootlin.com>
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines · wrote:
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name
rather
Quoted 2 lines
Quoted 2 lines
than
Quoted 1 line
Quoted 1 line
hard-coding it as
whitelisted_vars? That way, people couldimport/export
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
different sets of variables at different times, and also could
choose a
Quoted 7 lines
Quoted 7 lines
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order todescribe
Quoted 4 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by
Alex
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name
of
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for, -: a bit of complexity added to the parsing of the parameters of theenv
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
import function,
Proposition 3: Have -w followed by the list of whitelisted env
variable,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with sizebeing
Quoted 1 line
Quoted 1 line
Quoted 1 line
optional => how to know if size was passed or not?), what char
can be
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
used to separate env variables in the list? how does it perform with a very long list of whitelisted
variables?
Quoted 5 lines
Quoted 5 lines
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
Quoted 9 lines
Quoted 1 line
- specify multiple -w options each specifying a whitelisted env variable
You'll hit the maximum number of arguments/length of the command quickly with this method. Quicker than with the other propositions.
Moreover, this can make the command painfully long, painful to read and thus cumbersome to find the small typo in your command.
Quoted 1 line
- use the remaining arguments approach and eat all the trailing
arguments
Quoted 1 line
Quoted 1 line
as the names of env vars you import - needs a sentinel value for the
size
Quoted 5 lines
Quoted 2 lines
argument
That can't work I think.
Quoted 7 lines
How do you know if the size argument was passed or not? How'd you know what string is addr, size or the whitelist (if there is even any)?
env import foo1 foo2 foo3 foo4 addr size
env import foo1 foo2 foo3 addr
env import addr size
env import addrThat's why you need a sentinel for the size:
* env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
* -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the * text data must be '\0' terminated * -r: handle CRLF like LF, that means exported variables with * a content which ends with \r won't get imported. Used * to import text files created with editors which are using CRLF * for line endings. Only effective in addition to -t. * -b: assume binary format ('\0' separated, "\0\0" terminated) * -c: assume checksum protected environment format * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory. If not required and passing * variables to import use '-'* var...: List of variable names that get imported. Without arguments,
* all variables are imported
Quoted 7 lines
Which for your examples translates to:
env import addr size foo1 foo2 foo3 foo4
env import addr - foo1 foo2 foo3
env import addr size
env import addr
Ah, I misunderstood the word sentinel then :)
Would have been an even better idea if we had some consistency between env export and env import. For specifying the env export size, we have to use the -s argument but it's a positional argument for env export. We then have a list of variables to export, so it would make sense to have the same for env import I guess?
Quentin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/83f30249/attachment.sig>
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Quoted 2 lines · Quentin Schulz
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 1:06 PM Quentin Schulz <
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines · wrote:
Hi Alex,
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 9:02 AM Quentin Schulz <
quentin.schulz at bootlin.com>
Quoted 2 lines
Quoted 2 lines
wrote:
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 1 line
Quoted 1 line
Quoted 1 line
env importdoes not have such a mechanism of variable
selection.
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name
Quoted 8 lines
Quoted 8 lines
rather
Quoted 2 lines
Quoted 2 lines
than
Quoted 1 line
Quoted 1 line
hard-coding it as
whitelisted_vars? That way, people couldimport/export
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
different sets of variables at different times, and also could
choose a
Quoted 2 lines
Quoted 2 lines
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order
to
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
describe
Quoted 3 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of
which
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
you weren't in the mail recipients) and a similar patch[2] made by
Alex
Quoted 1 line
Quoted 1 line
Quoted 1 line
Kiernan (Cc of this patch series). I'd say it's an ongoing
discussion,
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the
name
Quoted 5 lines
Quoted 5 lines
of
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for, -: a bit of complexity added to the parsing of the parameters of
the
Quoted 7 lines
Quoted 7 lines
env
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
import function,
Proposition 3: Have -w followed by the list of whitelisted env
variable,
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
+: explicit list
-: the list cannot be separated by comma (valid character for an
env
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
variable) or a space (would not be able to distinguish the
last
Quoted 10 lines
Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
arguments of the commands which are address and size with size
being
Quoted 1 line
Quoted 1 line
Quoted 1 line
optional => how to know if size was passed or not?), what char
can be
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
used to separate env variables in the list? how does it perform with a very long list of whitelisted
variables?
Quoted 3 lines
Quoted 3 lines
Two more thoughts, both of which delegate the separator problem to
the
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
caller (the second being the one I implemented as it's almost no
code)
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
- specify multiple -w options each specifying a whitelisted env
variable
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
You'll hit the maximum number of arguments/length of the command
quickly
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
with this method. Quicker than with the other propositions.
Moreover, this can make the command painfully long, painful to read
and
Quoted 22 lines
Quoted 22 lines
Quoted 3 lines
thus cumbersome to find the small typo in your command.
Quoted 1 line
- use the remaining arguments approach and eat all the trailing
arguments
Quoted 1 line
Quoted 1 line
as the names of env vars you import - needs a sentinel value for the
size
Quoted 5 lines
Quoted 2 lines
argument
That can't work I think.
Quoted 7 lines
How do you know if the size argument was passed or not? How'd you know what string is addr, size or the whitelist (if there is even any)?
env import foo1 foo2 foo3 foo4 addr size
env import foo1 foo2 foo3 addr
env import addr size
env import addrThat's why you need a sentinel for the size:
Quoted 8 lines
Quoted 8 lines
* env import [-d] [-t [-r] | -b | -c] addr [size] [var ...]
* -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the * text data must be '\0' terminated * -r: handle CRLF like LF, that means exported variables with * a content which ends with \r won't get imported. Used * to import text files created with editors which are
using
Quoted 3 lines
Quoted 3 lines
CRLF
* for line endings. Only effective in addition to -t.
* -b: assume binary format ('\0' separated, "\0\0"
terminated)
Quoted 6 lines
Quoted 6 lines
* -c: assume checksum protected environment format * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory. If not required and passing * variables to import use '-'* var...: List of variable names that get imported. Without
arguments,
Quoted 10 lines
Quoted 9 lines
* all variables are imported
Which for your examples translates to:
env import addr size foo1 foo2 foo3 foo4
env import addr - foo1 foo2 foo3
env import addr size
env import addr
Quoted 7 lines
Ah, I misunderstood the word sentinel then :)
Would have been an even better idea if we had some consistency between env export and env import. For specifying the env export size, we have to use the -s argument but it's a positional argument for env export. We then have a list of variables to export, so it would make sense to have the same for env import I guess?
I agree, but I can't immediately think of a good way of doing that which is backward compatible.
Signature · 1 line
Alex Kiernan
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
In reply to Quentin SchulzQuoted 4 lines · Quentin Schulz
Hi Alex,
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 9:02 AM Quentin Schulz <
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines · wrote:
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name
rather
Quoted 2 lines
Quoted 2 lines
than
Quoted 1 line
Quoted 1 line
hard-coding it as
whitelisted_vars? That way, people could
import/export
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
different sets of variables at different times, and also could
choose a
Quoted 7 lines
Quoted 7 lines
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order todescribe
Quoted 4 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by
Alex
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name
of
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for, -: a bit of complexity added to the parsing of the parameters of the
env
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
import function,
Proposition 3: Have -w followed by the list of whitelisted env
variable,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with size
being
Quoted 1 line
Quoted 1 line
Quoted 1 line
optional => how to know if size was passed or not?), what char
can be
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
used to separate env variables in the list? how does it perform with a very long list of whitelisted
variables?
Quoted 9 lines
Quoted 6 lines
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
- specify multiple -w options each specifying a whitelisted env variable
You'll hit the maximum number of arguments/length of the command quickly with this method. Quicker than with the other propositions.
So you just made me go and read the definition for CONFIG_SYS_MAXARGS, the default's clearly pretty small (16). I guess I don't have a feeling for how many args you want to import - my use case is for 4, so even with max args at
16 I'm within that limit.
--
Alex Kiernan
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Quoted 80 lines · Alex Kiernan
Quoted 4 lines · Quentin Schulz
Hi Alex,
Quoted 1 line · Alex Kiernan
On Mon, May 21, 2018 at 9:02 AM Quentin Schulz <
quentin.schulz at bootlin.com>
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines · wrote:
Hi Stephen,
Quoted 2 lines · Stephen Warren
Quoted 1 line · Quentin Schulz
While the
env exportcan take as parameters variables to beexported,
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list ofvariables
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
that are whitelisted.
Would it be better for the -w option to accept a variable name
rather
Quoted 2 lines
Quoted 2 lines
than
Quoted 1 line
Quoted 1 line
hard-coding it as
whitelisted_vars? That way, people couldimport/export
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
different sets of variables at different times, and also could
choose a
Quoted 7 lines
Quoted 7 lines
more
Quoted 1 line
Quoted 1 line
use-case-specific variable name than
whitelisted_varsin order todescribe
Quoted 4 lines
Quoted 1 line
why those variables are "whitelisted".
This has been raised in the previous version of the patch[1] (of which you weren't in the mail recipients) and a similar patch[2] made by
Alex
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Kiernan (Cc of this patch series). I'd say it's an ongoing discussion, though I should have mentioned it in the comments of the patch?
TL;DR:
Proposition 1: Have -w only which "hardcodedly" checks for "whitelisting_vars",
+: straightforward implementation of the argument parsing, -: implicit declaration of the list: you have to know to set
whitelisted_varsin the environnement,Proposition 2: Have -w followed by one string-word which is the name
of
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
the env variable where to look for the list of whitelisted env variables,
+: explicit var to check where whitelist is looked for, -: a bit of complexity added to the parsing of the parameters of theenv
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
import function,
Proposition 3: Have -w followed by the list of whitelisted env
variable,
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+: explicit list
-: the list cannot be separated by comma (valid character for an env
variable) or a space (would not be able to distinguish the last arguments of the commands which are address and size with sizebeing
Quoted 1 line
Quoted 1 line
Quoted 1 line
optional => how to know if size was passed or not?), what char
can be
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
used to separate env variables in the list? how does it perform with a very long list of whitelisted
variables?
Quoted 9 lines
Quoted 6 lines
Two more thoughts, both of which delegate the separator problem to the caller (the second being the one I implemented as it's almost no code)
- specify multiple -w options each specifying a whitelisted env variable
You'll hit the maximum number of arguments/length of the command quickly with this method. Quicker than with the other propositions.
Quoted 6 lines
So you just made me go and read the definition for
CONFIG_SYS_MAXARGS, the default's clearly pretty small (16). I guess I don't have a feeling for how many args you want to import - my use case is for 4, so even with max args at
16 I'm within that limit.
Same for me, a few variables and that's it. I'm trying not to have a solution that's working only for me/us with a pretty low limitation though. Once we've settled for a solution, IMHO, we've to stick with it and thus if the solution can't be used by others, that's bad design from us.
I guess at worst, we could call multiple times the env import command with different whitelists but that's not really user-friendly.
Quentin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/4cf6c844/attachment.sig>
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
In reply to Quentin SchulzHi,
Quoted 95 lines · On Fri, 18 May 2018 16:44:59 +0200 Quentin Schulz wrote:
While the
env exportcan take as parameters variables to be exported,env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list of variables that are whitelisted.Every env variable present in env at
addrand inwhitelisted_varsenv variable will override the value of the variable in the current env. All the remaining variables are left untouched.One of its use case could be to load a secure environment from the signed U-Boot binary and load only a handful of variables from an other, unsecure, environment without completely losing control of U-Boot.
Signed-off-by Quentin Schulz <[email protected]>---
v2:
- use strdup instead of malloc + strcpy,
- NULL-check the result of strdup,
- add common exit path for freeing memory in one unique place,
- store token pointer from strtok within the char** array instead of strdup-ing token within elements of array,cmd/nvedit.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 11 deletions(-)cmd/nvedit.c +24
-3@@ -971,7 +971,7 @@ sep_err:#ifdef CONFIG_CMD_IMPORTENV /* -* env import [-d] [-t [-r] | -b | -c] addr [size] +* env import [-d] [-t [-r] | -b | -c] [-w] addr [size] * -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the @@ -982,6 +982,10 @@ sep_err:* for line endings. Only effective in addition to -t. * -b: assume binary format ('\0' separated, "\0\0" terminated) * -c: assume checksum protected environment format +* -w: specify that whitelisting of variables should be used when +* importing environment. The space-separated list of variables +* that should override the ones in current environment is stored +* in `whitelisted_vars`. * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory @@ -990,18 +994,22 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { ulong addr; -char *cmd, *ptr; +char *cmd, *ptr, *tmp; +char **array =NULL ;char sep ='\n' ;int chk =0 ;int fmt =0 ;int del =0 ;int crlf_is_lf =0 ; +int wl =0 ; +int wl_count =0 ; +int ret =0 ;size_t size; cmd = *argv;while (--argc >0 && **++argv =='-' ) { -char *arg = *argv; +char *arg = *argv, *str, *token;while (*++arg) {switch (*arg) {case 'b' :/* raw binary format */ @@ -1025,6 +1033,43 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,break ;case 'd' : del =1 ; +break ; +case 'w' : + wl =1 ; + wl_count =1 ; + + str =env_get ("whitelisted_vars" ); +if (!str) { +puts ("## Error: whitelisted_vars is not set.\n" ); +return CMD_RET_USAGE ; + } + + tmp =strdup (str); +if (!tmp)
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag, · line 1058
Quoted 10 lines
cmd/nvedit.c +9
-0+return CMD_RET_FAILURE ; + + token =strchr (tmp,' ' ); +while (!token) { + wl_count++; + token =strchr (token +1 ,' ' ); + } + +strcpy (tmp, str);
This is redundant. strchr() doesn't modify the array.
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag,
Quoted 3 lines
cmd/nvedit.c +2
-0+ wl_count =0 ; + array =malloc (sizeof (char *) * wl_count);
You have juste before reset wl_count to 0 are mallocing a zero sized array here!
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag,
Quoted 6 lines
cmd/nvedit.c +5
-0+if (!array) { +free (tmp); +return CMD_RET_FAILURE ; + } +
wl_count should probably be zeroed here...
But I would keep the predetermined vlaue of wl_count and check against it in the following loop to be sure not to overflow the allocated array, even in the improbable case, that strtok() should happen to return more tokens, than you tested before with the strchr() loop.
Lothar Waßmann
PATCH v2 1/2 cmd: nvedit: add whitelist option for env import
Hi Lothar,
Quoted 101 lines · Lothar Waßmann
Hi,
Quoted 98 lines · On Fri, 18 May 2018 16:44:59 +0200 Quentin Schulz wrote:
While the
env exportcan take as parameters variables to be exported,env importdoes not have such a mechanism of variable selection.Let's add a
-woption that asksenv importto look for thewhitelisted_varsenv variable for a space-separated list of variables that are whitelisted.Every env variable present in env at
addrand inwhitelisted_varsenv variable will override the value of the variable in the current env. All the remaining variables are left untouched.One of its use case could be to load a secure environment from the signed U-Boot binary and load only a handful of variables from an other, unsecure, environment without completely losing control of U-Boot.
Signed-off-by Quentin Schulz <[email protected]>---
v2:
- use strdup instead of malloc + strcpy,
- NULL-check the result of strdup,
- add common exit path for freeing memory in one unique place,
- store token pointer from strtok within the char** array instead of strdup-ing token within elements of array,cmd/nvedit.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 11 deletions(-)cmd/nvedit.c +27
-3@@ -971,7 +971,7 @@ sep_err:#ifdef CONFIG_CMD_IMPORTENV /* -* env import [-d] [-t [-r] | -b | -c] addr [size] +* env import [-d] [-t [-r] | -b | -c] [-w] addr [size] * -d: delete existing environment before importing; * otherwise overwrite / append to existing definitions * -t: assume text format; either "size" must be given or the @@ -982,6 +982,10 @@ sep_err:* for line endings. Only effective in addition to -t. * -b: assume binary format ('\0' separated, "\0\0" terminated) * -c: assume checksum protected environment format +* -w: specify that whitelisting of variables should be used when +* importing environment. The space-separated list of variables +* that should override the ones in current environment is stored +* in `whitelisted_vars`. * addr: memory address to read from * size: length of input data; if missing, proper '\0' * termination is mandatory @@ -990,18 +994,22 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { ulong addr; -char *cmd, *ptr; +char *cmd, *ptr, *tmp; +char **array =NULL ;char sep ='\n' ;int chk =0 ;int fmt =0 ;int del =0 ;int crlf_is_lf =0 ; +int wl =0 ; +int wl_count =0 ; +int ret =0 ;size_t size; cmd = *argv;while (--argc >0 && **++argv =='-' ) { -char *arg = *argv; +char *arg = *argv, *str, *token;while (*++arg) {switch (*arg) {case 'b' :/* raw binary format */ @@ -1025,6 +1033,43 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag,break ;case 'd' : del =1 ; +break ; +case 'w' : + wl =1 ; + wl_count =1 ; + + str =env_get ("whitelisted_vars" ); +if (!str) { +puts ("## Error: whitelisted_vars is not set.\n" ); +return CMD_RET_USAGE ; + } + + tmp =strdup (str); +if (!tmp) +return CMD_RET_FAILURE ; + + token =strchr (tmp,' ' );
Quoted 9 lines
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag,
Quoted 7 lines
cmd/nvedit.c +6
-0+while (!token) { + wl_count++; + token =strchr (token +1 ,' ' ); + } + +strcpy (tmp, str);This is redundant.
strchr()doesn't modify the array.
ACK.
Quoted 6 lines
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag,
Quoted 3 lines
cmd/nvedit.c +2
-0+ wl_count =0 ; + array =malloc (sizeof (char *) * wl_count);You have juste before reset
wl_countto 0 are mallocing a zero sized array here!
Don't know how I could get the sandbox test to pass with this error, thanks.
Quoted 7 lines
cmd/nvedit.c static int do_env_import(cmd_tbl_t *cmdtp, int flag,
Quoted 6 lines
cmd/nvedit.c +5
-0+if (!array) { +free (tmp); +return CMD_RET_FAILURE ; + } +
wl_countshould probably be zeroed here...
Indeed.
Quoted 5 lines
But I would keep the predetermined vlaue of
wl_countand check against it in the following loop to be sure not to overflow the allocated array, even in the improbable case, thatstrtok()should happen to return more tokens, than you tested before with thestrchr()loop.
Good point, never too safe to double check.
Thanks,
Quentin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20180521/e3325d53/attachment.sig>