Skip to contentSkip to conversation

cmd: nvedit_efi: fix default GUID selection for signature databases

PATCH Superseded Aristo Chen 2 participants 8 messages Jul 14 – Jul 20

lore
Share & tools
lore
#0 lore raw

cover cmd: nvedit_efi: fix default GUID selection for signature databases

"env set -e" selects a default vendor GUID when no -guid option is given, using a hand rolled name check in do_env_set_efi(). That check has drifted from the canonical name_type[] table in lib/efi_loader/efi_var_common.c: it knows db, dbx and dbt but not dbr, so "env set -e dbr" silently operates on a variable under EFI_GLOBAL_VARIABLE_GUID instead of the image security database GUID, creating a variable that no consumer will ever look up. The command also unmapped the -i value buffer twice, which triggers a spurious "Address not mapped" warning on sandbox for addresses that get a tagged mapping.

Patch 1 replaces the hand rolled check with a call to efi_auth_var_get_guid() so that the shell command and the variable service share a single table, and consolidates the value and name cleanup on the common exit path. Behaviour is unchanged for db, dbx, dbt and for non-authenticated variable names.

Patch 2 adds a test case to the sandbox secure boot suite that enrolls each of db, dbx, dbt and dbr in setup mode without an explicit -guid option and checks that every variable is created under the image security database GUID and not under the global variable GUID. This is the first coverage of dbt and dbr under test/. The enrollment happens in setup mode because once secure boot is enabled, efi_variable_authenticate() only accepts writes to PK, KEK, db and dbx. The new test fails at the dbr step without patch 1.

Aristo Chen (2):
cmd: nvedit_efi: use efi_auth_var_get_guid() for the default GUID
test: check default GUID selection of signature database variables

 cmd/nvedit_efi.c                              | 22 +++++---------
 test/py/tests/test_efi_secboot/conftest.py    |  8 +++++
 .../py/tests/test_efi_secboot/test_authvar.py | 30 +++++++++++++++++++
 3 files changed, 46 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 cmd: nvedit_efi: use efi_auth_var_get_guid() for the default GUID

do_env_set_efi() hand rolls the mapping from well known variable names to their default vendor GUID and has already drifted from the canonical name_type[] table in efi_var_common.c: it does not know "dbr", so "env set -e dbr" operates on the variable under EFI_GLOBAL_VARIABLE_GUID instead of the image security database GUID, silently creating a variable that nothing will ever consume.

Convert the variable name to UTF-16 before selecting the GUID and let efi_auth_var_get_guid() do the lookup. That function knows all authenticated variables including "dbr" and falls back to EFI_GLOBAL_VARIABLE_GUID for any other name, so behaviour is unchanged for "db", "dbx", "dbt" and non-authenticated variables. Future additions to the table now apply to the shell command automatically.

Since the conversion now happens before the value parsing loop, free the UTF-16 name at the common exit label so the error path there does not leak it.

Also drop the unmap_sysmem() call right after efi_set_variable_int():
the common exit path already unmaps the value for the -i case and frees it otherwise, so the value was unmapped twice. On sandbox the second call triggers a spurious "Address not mapped" warning for addresses that get a tagged mapping.

---
 cmd/nvedit_efi.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
cmd/nvedit_efi.c +8 -14
index 3a36f94775b..f38802e4f4d 100644
@@ -448,13 +448,14 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	var_name = argv[0];
-	if (default_guid) {
-		if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
-		    !strcmp(var_name, "dbt"))
-			guid = efi_guid_image_security_database;
-		else
-			guid = efi_global_variable_guid;
+	var_name16 = efi_convert_string(var_name);
+	if (!var_name16) {
+		printf("## Out of memory\n");
+		ret = CMD_RET_FAILURE;
+		goto out;
 	}
+	if (default_guid)
+		guid = *efi_auth_var_get_guid(var_name16);
 
 	if (verbose) {
 		printf("GUID: %pUl (%pUs)\n", &guid, &guid);
@@ -479,16 +480,8 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 			       16, 1, value, size, true);
 	}
 
-	var_name16 = efi_convert_string(var_name);
-	if (!var_name16) {
-		printf("## Out of memory\n");
-		ret = CMD_RET_FAILURE;
-		goto out;
-	}
 	ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
 				   true);
-	free(var_name16);
-	unmap_sysmem(value);
 	if (ret == EFI_SUCCESS) {
 		ret = CMD_RET_SUCCESS;
 	} else {
@@ -518,6 +511,7 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 		ret = CMD_RET_FAILURE;
 	}
 out:
+	free(var_name16);
 	if (value_on_memory)
 		unmap_sysmem(value);
 	else
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH v1 1/2] cmd: nvedit_efi: use efi_auth_var_get_guid() for the default GUID

Quoted 18 lines · Aristo Chen

do_env_set_efi() hand rolls the mapping from well known variable names to their default vendor GUID and has already drifted from the canonical name_type[] table in efi_var_common.c: it does not know "dbr", so "env set -e dbr" operates on the variable under EFI_GLOBAL_VARIABLE_GUID instead of the image security database GUID, silently creating a variable that nothing will ever consume.

Convert the variable name to UTF-16 before selecting the GUID and let efi_auth_var_get_guid() do the lookup. That function knows all authenticated variables including "dbr" and falls back to EFI_GLOBAL_VARIABLE_GUID for any other name, so behaviour is unchanged for "db", "dbx", "dbt" and non-authenticated variables. Future additions to the table now apply to the shell command automatically.

Since the conversion now happens before the value parsing loop, free the UTF-16 name at the common exit label so the error path there does not leak it.

Quoted 7 lines

Also drop the unmap_sysmem() call right after efi_set_variable_int():
the common exit path already unmaps the value for the -i case and frees it otherwise, so the value was unmapped twice. On sandbox the second call triggers a spurious "Address not mapped" warning for addresses that get a tagged mapping.

Quoted 54 lines
---
  cmd/nvedit_efi.c | 22 ++++++++--------------
  1 file changed, 8 insertions(+), 14 deletions(-)
cmd/nvedit_efi.c +8 -14
index 3a36f94775b..f38802e4f4d 100644
@@ -448,13 +448,14 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  		return CMD_RET_USAGE;
 
  	var_name = argv[0];
-	if (default_guid) {
-		if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
-		    !strcmp(var_name, "dbt"))
-			guid = efi_guid_image_security_database;
-		else
-			guid = efi_global_variable_guid;
+	var_name16 = efi_convert_string(var_name);
+	if (!var_name16) {
+		printf("## Out of memory\n");
+		ret = CMD_RET_FAILURE;
+		goto out;
  	}
+	if (default_guid)
+		guid = *efi_auth_var_get_guid(var_name16);
 
  	if (verbose) {
  		printf("GUID: %pUl (%pUs)\n", &guid, &guid);
@@ -479,16 +480,8 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  			       16, 1, value, size, true);
  	}
 
-	var_name16 = efi_convert_string(var_name);
-	if (!var_name16) {
-		printf("## Out of memory\n");
-		ret = CMD_RET_FAILURE;
-		goto out;
-	}
  	ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
  				   true);
-	free(var_name16);
-	unmap_sysmem(value);
  	if (ret == EFI_SUCCESS) {
  		ret = CMD_RET_SUCCESS;
  	} else {
@@ -518,6 +511,7 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
  		ret = CMD_RET_FAILURE;
  	}
  out:
+	free(var_name16);
  	if (value_on_memory)
  		unmap_sysmem(value);
  	else
#3 lore raw

PATCH 2/2 test: check default GUID selection of signature database variables

In reply to Aristo Chen

The name to GUID mapping that "env set -e" applies when no -guid option is given was not covered by any test, which is how the wrong default GUID for "dbr" went unnoticed until the previous commit.

Add a test case that enrolls each of db, dbx, dbt and dbr in setup mode without an explicit -guid option and checks that every variable is created under the image security database GUID and not under the global variable GUID. This is also the first coverage of dbt and dbr anywhere under test/. The enrollment happens in setup mode because once secure boot is enabled, efi_variable_authenticate() only accepts writes to PK, KEK, db and dbx.

---
 test/py/tests/test_efi_secboot/conftest.py    |  8 +++++
 .../py/tests/test_efi_secboot/test_authvar.py | 30 +++++++++++++++++++
 2 files changed, 38 insertions(+)
test/py/tests/test_efi_secboot/conftest.py +8 -0
index 76b8f9fa0a3..0755497d46d 100644
@@ -96,6 +96,14 @@ def efi_boot_env(request, ubman):
         check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
                    % (mnt_point, EFITOOLS_PATH),
                    shell=True)
+        # dbt (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbt db.esl dbt.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
+        # dbr (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbr db.esl dbr.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
 
         # Copy image
         check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
test/py/tests/test_efi_secboot/test_authvar.py +30 -0
index 7b45f8fb814..45350b19b06 100644
@@ -279,3 +279,33 @@ class TestEfiAuthVar(object):
             output = ubman.run_command(
                 'printenv -e SetupMode')
             assert '00000000: 01' in output
+
+    def test_efi_var_auth6(self, ubman, efi_boot_env):
+        """
+        Test Case 6 - Default GUID of signature database variables
+        """
+        ubman.restart_uboot()
+        disk_img = efi_boot_env
+        with ubman.log.section('Test Case 6a'):
+            # Test Case 6a, install signature database variables in setup
+            # mode without -guid
+            output = ubman.run_command_list([
+                'host bind 0 %s' % disk_img,
+                'printenv -e SetupMode'])
+            assert '00000000: 01' in ''.join(output)
+
+            for var in ('db', 'dbx', 'dbt', 'dbr'):
+                output = ubman.run_command_list([
+                    'fatload host 0:1 4000000 %s.auth' % var,
+                    'setenv -e -nv -bs -rt -at -i 4000000:$filesize %s' % var,
+                    'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f %s' % var])
+                assert 'Failed to set EFI variable' not in ''.join(output)
+                assert '%s:' % var in ''.join(output)
+
+        with ubman.log.section('Test Case 6b'):
+            # Test Case 6b, variables must not exist under the global
+            # variable GUID
+            for var in ('db', 'dbx', 'dbt', 'dbr'):
+                output = ubman.run_command(
+                    'printenv -e -n -guid 8be4df61-93ca-11d2-aa0d-00e098032b8c %s' % var)
+                assert '\"%s\" not defined' % var in output
Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH v1 2/2] test: check default GUID selection of signature database variables

Quoted 51 lines · Aristo Chen

The name to GUID mapping that "env set -e" applies when no -guid option is given was not covered by any test, which is how the wrong default GUID for "dbr" went unnoticed until the previous commit.

Add a test case that enrolls each of db, dbx, dbt and dbr in setup mode without an explicit -guid option and checks that every variable is created under the image security database GUID and not under the global variable GUID. This is also the first coverage of dbt and dbr anywhere under test/. The enrollment happens in setup mode because once secure boot is enabled, efi_variable_authenticate() only accepts writes to PK, KEK, db and dbx.

---
  test/py/tests/test_efi_secboot/conftest.py    |  8 +++++
  .../py/tests/test_efi_secboot/test_authvar.py | 30 +++++++++++++++++++
  2 files changed, 38 insertions(+)
test/py/tests/test_efi_secboot/conftest.py +8 -0
index 76b8f9fa0a3..0755497d46d 100644
@@ -96,6 +96,14 @@ def efi_boot_env(request, ubman):
          check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
                     % (mnt_point, EFITOOLS_PATH),
                     shell=True)
+        # dbt (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbt db.esl dbt.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
+        # dbr (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbr db.esl dbr.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
 
          # Copy image
          check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
test/py/tests/test_efi_secboot/test_authvar.py +6 -0
index 7b45f8fb814..45350b19b06 100644
@@ -279,3 +279,33 @@ class TestEfiAuthVar(object):
              output = ubman.run_command(
                  'printenv -e SetupMode')
              assert '00000000: 01' in output
+
+    def test_efi_var_auth6(self, ubman, efi_boot_env):
+        """
+        Test Case 6 - Default GUID of signature database variables
+        """
+        ubman.restart_uboot()

test/py/tests/test_efi_secboot/test_authvar.py class TestEfiAuthVar(object): · line 297

Quoted 10 lines
test/py/tests/test_efi_secboot/test_authvar.py +10 -0
+        disk_img = efi_boot_env
+        with ubman.log.section('Test Case 6a'):
+            # Test Case 6a, install signature database variables in setup
+            # mode without -guid
+            output = ubman.run_command_list([
+                'host bind 0 %s' % disk_img,
+                'printenv -e SetupMode'])
+            assert '00000000: 01' in ''.join(output)
+
+            for var in ('db', 'dbx', 'dbt', 'dbr'):

Thank you Aristo for updating the test.

Should we check the GUID of PK and KEK too?

Best regards

Heinrich

Quoted 14 lines
test/py/tests/test_efi_secboot/test_authvar.py +14 -0
+                output = ubman.run_command_list([
+                    'fatload host 0:1 4000000 %s.auth' % var,
+                    'setenv -e -nv -bs -rt -at -i 4000000:$filesize %s' % var,
+                    'printenv -e -n -guid d719b2cb-3d3a-4596-a3bc-dad00e67656f %s' % var])
+                assert 'Failed to set EFI variable' not in ''.join(output)
+                assert '%s:' % var in ''.join(output)
+
+        with ubman.log.section('Test Case 6b'):
+            # Test Case 6b, variables must not exist under the global
+            # variable GUID
+            for var in ('db', 'dbx', 'dbt', 'dbr'):
+                output = ubman.run_command(
+                    'printenv -e -n -guid 8be4df61-93ca-11d2-aa0d-00e098032b8c %s' % var)
+                assert '\"%s\" not defined' % var in output
#5 lore raw

v2 cover cmd: nvedit_efi: fix default GUID selection for signature databases

In reply to Aristo Chen

"env set -e" selects a default vendor GUID when no -guid option is given, using a hand rolled name check in do_env_set_efi(). That check has drifted from the canonical name_type[] table in lib/efi_loader/efi_var_common.c: it knows db, dbx and dbt but not dbr, so "env set -e dbr" silently operates on a variable under EFI_GLOBAL_VARIABLE_GUID instead of the image security database GUID, creating a variable that no consumer will ever look up. The command also unmapped the -i value buffer twice, which triggers a spurious "Address not mapped" warning on sandbox for addresses that get a tagged mapping.

Patch 1 replaces the hand rolled check with a call to efi_auth_var_get_guid() so that the shell command and the variable service share a single table, and consolidates the value and name cleanup on the common exit path. Behaviour is unchanged for db, dbx, dbt and for non-authenticated variable names.

Patch 2 adds a test case to the sandbox secure boot suite that enrolls each of db, dbx, dbt and dbr in setup mode without an explicit -guid option and checks that every variable is created under the image security database GUID and not under the global variable GUID, then enrolls PK and KEK the same way and checks that both land under the global variable GUID. This is the first coverage of dbt and dbr under test/. The signature database enrollment happens in setup mode because once secure boot is enabled, efi_variable_authenticate() only accepts writes to PK, KEK, db and dbx. The new test fails at the dbr step without patch 1.

Changes in v2:
- also check that PK and KEK are created under the global variable GUID and not under the image security database GUID (suggested by Heinrich Schuchardt)
- patch 1: collect Reviewed-by tag, no code changes

Aristo Chen (2):
cmd: nvedit_efi: use efi_auth_var_get_guid() for the default GUID
test: check default GUID selection of signature database variables

 cmd/nvedit_efi.c                              | 22 ++++-----
 test/py/tests/test_efi_secboot/conftest.py    |  8 ++++
 .../py/tests/test_efi_secboot/test_authvar.py | 47 +++++++++++++++++++
 3 files changed, 63 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
#6 lore raw

PATCH v2 1/2 cmd: nvedit_efi: use efi_auth_var_get_guid() for the default GUID

do_env_set_efi() hand rolls the mapping from well known variable names to their default vendor GUID and has already drifted from the canonical name_type[] table in efi_var_common.c: it does not know "dbr", so "env set -e dbr" operates on the variable under EFI_GLOBAL_VARIABLE_GUID instead of the image security database GUID, silently creating a variable that nothing will ever consume.

Convert the variable name to UTF-16 before selecting the GUID and let efi_auth_var_get_guid() do the lookup. That function knows all authenticated variables including "dbr" and falls back to EFI_GLOBAL_VARIABLE_GUID for any other name, so behaviour is unchanged for "db", "dbx", "dbt" and non-authenticated variables. Future additions to the table now apply to the shell command automatically.

Since the conversion now happens before the value parsing loop, free the UTF-16 name at the common exit label so the error path there does not leak it.

Also drop the unmap_sysmem() call right after efi_set_variable_int():
the common exit path already unmaps the value for the -i case and frees it otherwise, so the value was unmapped twice. On sandbox the second call triggers a spurious "Address not mapped" warning for addresses that get a tagged mapping.

---
Changes in v2:
- no code changes, collected Reviewed-by tag

 cmd/nvedit_efi.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
cmd/nvedit_efi.c +8 -14
index 3a36f94775b..f38802e4f4d 100644
@@ -448,13 +448,14 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 		return CMD_RET_USAGE;
 
 	var_name = argv[0];
-	if (default_guid) {
-		if (!strcmp(var_name, "db") || !strcmp(var_name, "dbx") ||
-		    !strcmp(var_name, "dbt"))
-			guid = efi_guid_image_security_database;
-		else
-			guid = efi_global_variable_guid;
+	var_name16 = efi_convert_string(var_name);
+	if (!var_name16) {
+		printf("## Out of memory\n");
+		ret = CMD_RET_FAILURE;
+		goto out;
 	}
+	if (default_guid)
+		guid = *efi_auth_var_get_guid(var_name16);
 
 	if (verbose) {
 		printf("GUID: %pUl (%pUs)\n", &guid, &guid);
@@ -479,16 +480,8 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 			       16, 1, value, size, true);
 	}
 
-	var_name16 = efi_convert_string(var_name);
-	if (!var_name16) {
-		printf("## Out of memory\n");
-		ret = CMD_RET_FAILURE;
-		goto out;
-	}
 	ret = efi_set_variable_int(var_name16, &guid, attributes, size, value,
 				   true);
-	free(var_name16);
-	unmap_sysmem(value);
 	if (ret == EFI_SUCCESS) {
 		ret = CMD_RET_SUCCESS;
 	} else {
@@ -518,6 +511,7 @@ int do_env_set_efi(struct cmd_tbl *cmdtp, int flag, int argc,
 		ret = CMD_RET_FAILURE;
 	}
 out:
+	free(var_name16);
 	if (value_on_memory)
 		unmap_sysmem(value);
 	else
Signature · 1 line
2.43.0
#7 lore raw

PATCH v2 2/2 test: check default GUID selection of signature database variables

In reply to Aristo Chen

The name to GUID mapping that "env set -e" applies when no -guid option is given was not covered by any test, which is how the wrong default GUID for "dbr" went unnoticed until the previous commit.

Add a test case that enrolls each of db, dbx, dbt and dbr in setup mode without an explicit -guid option and checks that every variable is created under the image security database GUID and not under the global variable GUID. Then enroll PK and KEK, also without -guid, and check that both are created under the global variable GUID and not under the image security database GUID. This is also the first coverage of dbt and dbr anywhere under test/.

The signature database enrollment happens in setup mode because once secure boot is enabled, efi_variable_authenticate() only accepts writes to PK, KEK, db and dbx; PK and KEK are enrolled last because installing PK leaves setup mode.

---
Changes in v2:
- also check that PK and KEK are created under the global variable GUID and not under the image security database GUID (suggested by Heinrich Schuchardt)

 test/py/tests/test_efi_secboot/conftest.py    |  8 ++++
 .../py/tests/test_efi_secboot/test_authvar.py | 47 +++++++++++++++++++
 2 files changed, 55 insertions(+)
test/py/tests/test_efi_secboot/conftest.py +8 -0
index 76b8f9fa0a3..0755497d46d 100644
@@ -96,6 +96,14 @@ def efi_boot_env(request, ubman):
         check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbx db.esl dbx_db.auth'
                    % (mnt_point, EFITOOLS_PATH),
                    shell=True)
+        # dbt (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbt db.esl dbt.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
+        # dbr (with TEST_db certificate)
+        check_call('cd %s; %ssign-efi-sig-list -t "2020-04-05" -c KEK.crt -k KEK.key dbr db.esl dbr.auth'
+                   % (mnt_point, EFITOOLS_PATH),
+                   shell=True)
 
         # Copy image
         check_call('cp %s/lib/efi_loader/helloworld.efi %s' %
test/py/tests/test_efi_secboot/test_authvar.py +47 -0

50 lines not on this page · raw message

Signature · 1 line
2.43.0