Skip to contentSkip to conversation

Automatic Disk Unlock with TPM2

PATCH Superseded Gary Lin via Grub-devel 3 participants 72 messages Jun 28, 2024 – Sep 3, 2024 Kbuild Page 2 of 2

v18 of 23 Superseded Latest is v23 →

lore
Share & tools
lore
#61 lore raw

Re: [PATCH v18 23/25] diskfilter: look up cryptodisk devices first

In reply to Gary Lin via Grub-devel
Quoted 62 lines · Gary Lin via Grub-devel

When using disk auto-unlocking with TPM 2.0, the typical grub.cfg may look like this:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
  cryptomount -u <PART-UUID> -P tpm2
  search --fs-uuid --set=root <FS-UUID>

Since the disk search order is based on the order of module loading, the attacker could insert a malicious disk with the same FS-UUID root to trick grub2 to boot into the malicious root and further dump memory to steal the unsealed key.

Do defend against such an attack, we can specify the hint provided by 'grub-probe' to search the encrypted partition first:

search --fs-uuid --set=root --hint='cryptouuid/<PART-UUID>' <FS-UUID>

However, for LVM on an encrypted partition, the search hint provided by 'grub-probe' is:

--hint='lvmid/<VG-UUID>/<LV-UUID>'

It doesn't guarantee to look up the logical volume from the encrypted partition, so the attacker may have the chance to fool grub2 to boot into the malicious disk.

To minimize the attack surface, this commit tweaks the disk device search in diskfilter to look up cryptodisk devices first and then others, so that the auto-unlocked disk will be found first, not the attacker's disk.

---
 grub-core/disk/diskfilter.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
grub-core/disk/diskfilter.c +8 -9
index 21e239511..df1992305 100644
@@ -226,15 +226,32 @@ scan_devices (const char *arname)
   int need_rescan;
 
   for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
-    for (p = grub_disk_dev_list; p; p = p->next)
-      if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
-	  && p->disk_iterate)
-	{
-	  if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
-	    return;
-	  if (arname && is_lv_readable (find_lv (arname), 1))
-	    return;
-	}
+    {
+      /* look up the crytodisk devices first */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;

grub-core/disk/diskfilter.c scan_devices (const char *arname) · line 246

Quoted 10 lines
grub-core/disk/diskfilter.c +10 -0
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	    break;
+	  }
+
+      /* check the devices other than crytodisk */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
+	  continue;
+	else if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID

I think you can drop "if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)"...

grub-core/disk/diskfilter.c scan_devices (const char *arname) · line 253

Quoted 8 lines
grub-core/disk/diskfilter.c +8 -0
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	  }
+    }

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#62 lore raw

Re: [PATCH v18 23/25] diskfilter: look up cryptodisk devices first

Quoted 67 lines · Daniel Kiper
Quoted 66 lines · Gary Lin via Grub-devel

When using disk auto-unlocking with TPM 2.0, the typical grub.cfg may look like this:

  tpm2_key_protector_init --tpm2key=(hd0,gpt1)/boot/grub2/sealed.tpm
  cryptomount -u <PART-UUID> -P tpm2
  search --fs-uuid --set=root <FS-UUID>

Since the disk search order is based on the order of module loading, the attacker could insert a malicious disk with the same FS-UUID root to trick grub2 to boot into the malicious root and further dump memory to steal the unsealed key.

Do defend against such an attack, we can specify the hint provided by 'grub-probe' to search the encrypted partition first:

search --fs-uuid --set=root --hint='cryptouuid/<PART-UUID>' <FS-UUID>

However, for LVM on an encrypted partition, the search hint provided by 'grub-probe' is:

--hint='lvmid/<VG-UUID>/<LV-UUID>'

It doesn't guarantee to look up the logical volume from the encrypted partition, so the attacker may have the chance to fool grub2 to boot into the malicious disk.

To minimize the attack surface, this commit tweaks the disk device search in diskfilter to look up cryptodisk devices first and then others, so that the auto-unlocked disk will be found first, not the attacker's disk.

---
 grub-core/disk/diskfilter.c | 35 ++++++++++++++++++++++++++---------
 1 file changed, 26 insertions(+), 9 deletions(-)
grub-core/disk/diskfilter.c +12 -9
index 21e239511..df1992305 100644
@@ -226,15 +226,32 @@ scan_devices (const char *arname)
   int need_rescan;
 
   for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
-    for (p = grub_disk_dev_list; p; p = p->next)
-      if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID
-	  && p->disk_iterate)
-	{
-	  if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
-	    return;
-	  if (arname && is_lv_readable (find_lv (arname), 1))
-	    return;
-	}
+    {
+      /* look up the crytodisk devices first */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	    break;
+	  }
Quoted 9 lines

grub-core/disk/diskfilter.c scan_devices (const char *arname) · line 246

Quoted 6 lines
grub-core/disk/diskfilter.c +6 -0
+
+      /* check the devices other than crytodisk */
+      for (p = grub_disk_dev_list; p; p = p->next)
+	if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)
+	  continue;
+	else if (p->id != GRUB_DISK_DEVICE_DISKFILTER_ID

I think you can drop "if (p->id == GRUB_DISK_DEVICE_CRYPTODISK_ID)"...

Ok, I'll drop the if statement.

Gary Lin

Quoted 10 lines

grub-core/disk/diskfilter.c scan_devices (const char *arname) · line 253

Quoted 8 lines
grub-core/disk/diskfilter.c +8 -0
+	    && p->disk_iterate)
+	  {
+	    if ((p->disk_iterate) (scan_disk_hook, NULL, pull))
+	      return;
+	    if (arname && is_lv_readable (find_lv (arname), 1))
+	      return;
+	  }
+    }

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#63 lore raw

PATCH v18 24/25 tpm2_key_protector: Add grub-emu support

In reply to Gary Lin via Grub-devel

As a preparation to test tpm2_key_protector with grub-emu, the new option, --tpm-device, is introduced to specify the TPM device for grub-emu so that grub-emu can share the emulated TPM device with the host.

Since grub-emu can directly access the device node on host, it's easy to implement the essential TCG2 command submission function with the read/write functions and enable tpm2_key_protector module for grub-emu, so that we can further test TPM2 key unsealing with grub-emu.

---
 grub-core/Makefile.core.def   |  3 ++
 grub-core/kern/emu/main.c     | 11 ++++++-
 grub-core/kern/emu/misc.c     | 51 +++++++++++++++++++++++++++++++++
 grub-core/lib/tss2/tcg2_emu.c | 54 +++++++++++++++++++++++++++++++++++
 include/grub/emu/misc.h       |  5 ++++
 5 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/tss2/tcg2_emu.c
grub-core/Makefile.core.def +3 -0
index d54c12574..df07c0f0e 100644
@@ -2574,7 +2574,9 @@ module = {
   common = lib/tss2/tpm2_cmd.c;
   common = lib/tss2/tss2.c;
   efi = lib/efi/tcg2.c;
+  emu = lib/tss2/tcg2_emu.c;
   enable = efi;
+  enable = emu;
   cppflags = '-I$(srcdir)/lib/tss2';
 };
 
@@ -2586,6 +2588,7 @@ module = {
   common = commands/tpm2_key_protector/tpm2key_asn1_tab.c;
   /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */
   enable = efi;
+  enable = emu;
   cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub';
 };
 
grub-core/kern/emu/main.c +10 -1
index 855b11c3d..c10838613 100644
@@ -55,7 +55,7 @@
 static jmp_buf main_env;
 
 /* Store the prefix specified by an argument.  */
-static char *root_dev = NULL, *dir = NULL;
+static char *root_dev = NULL, *dir = NULL, *tpm_dev = NULL;
 
 grub_addr_t grub_modbase = 0;
 
@@ -108,6 +108,7 @@ static struct argp_option options[] = {
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
   {"kexec",       'X', 0,      0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0},
+  {"tpm-device",  't', N_("DEV"), 0, N_("Set TPM device."), 0},
   { 0, 0, 0, 0, 0, 0 }
 };
 
@@ -168,6 +169,10 @@ argp_parser (int key, char *arg, struct argp_state *state)
     case 'X':
       grub_util_set_kexecute ();
       break;
+    case 't':
+      free (tpm_dev);
+      tpm_dev = xstrdup (arg);
+      break;
 
     case ARGP_KEY_ARG:
       {
@@ -276,6 +281,9 @@ main (int argc, char *argv[])
 
   dir = xstrdup (dir);
 
+  if (tpm_dev)
+    grub_util_tpm_open (tpm_dev);
+
   /* Start GRUB!  */
   if (setjmp (main_env) == 0)
     grub_main ();
@@ -283,6 +291,7 @@ main (int argc, char *argv[])
   grub_fini_all ();
   grub_hostfs_fini ();
   grub_host_fini ();
+  grub_util_tpm_close ();
 
   grub_machine_fini (GRUB_LOADER_FLAG_NORETURN);
 
grub-core/kern/emu/misc.c +51 -0

66 lines not on this page · raw message

grub-core/lib/tss2/tcg2_emu.c new file +54 -0

54 lines not on this page · raw message

include/grub/emu/misc.h +5 -0
index fa959ebe7..fefbec499 100644
@@ -75,4 +75,9 @@ grub_util_fopen (const char *path, const char *mode);
 
 int grub_util_file_sync (FILE *f);
 
+grub_err_t grub_util_tpm_open (const char *tpm_dev);
+grub_err_t grub_util_tpm_close (void);
+grub_size_t EXPORT_FUNC(grub_util_tpm_read) (void *output, grub_size_t size);
+grub_size_t EXPORT_FUNC(grub_util_tpm_write) (const void *input, grub_size_t size);
+
 #endif /* GRUB_EMU_MISC_H */
Signature · 7 lines
2.35.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#64 lore raw

Re: [PATCH v18 24/25] tpm2_key_protector: Add grub-emu support

Quoted 57 lines · Gary Lin via Grub-devel

As a preparation to test tpm2_key_protector with grub-emu, the new option, --tpm-device, is introduced to specify the TPM device for grub-emu so that grub-emu can share the emulated TPM device with the host.

Since grub-emu can directly access the device node on host, it's easy to implement the essential TCG2 command submission function with the read/write functions and enable tpm2_key_protector module for grub-emu, so that we can further test TPM2 key unsealing with grub-emu.

---
 grub-core/Makefile.core.def   |  3 ++
 grub-core/kern/emu/main.c     | 11 ++++++-
 grub-core/kern/emu/misc.c     | 51 +++++++++++++++++++++++++++++++++
 grub-core/lib/tss2/tcg2_emu.c | 54 +++++++++++++++++++++++++++++++++++
 include/grub/emu/misc.h       |  5 ++++
 5 files changed, 123 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/lib/tss2/tcg2_emu.c
grub-core/Makefile.core.def +3 -0
index d54c12574..df07c0f0e 100644
@@ -2574,7 +2574,9 @@ module = {
   common = lib/tss2/tpm2_cmd.c;
   common = lib/tss2/tss2.c;
   efi = lib/efi/tcg2.c;
+  emu = lib/tss2/tcg2_emu.c;
   enable = efi;
+  enable = emu;
   cppflags = '-I$(srcdir)/lib/tss2';
 };
 
@@ -2586,6 +2588,7 @@ module = {
   common = commands/tpm2_key_protector/tpm2key_asn1_tab.c;
   /* The plaform support of tpm2_key_protector depends on the tcg2 implementation in tss2. */
   enable = efi;
+  enable = emu;
   cppflags = '-I$(srcdir)/lib/tss2 -I$(srcdir)/lib/libtasn1-grub';
 };
 
grub-core/kern/emu/main.c +1 -1
index 855b11c3d..c10838613 100644
@@ -55,7 +55,7 @@
 static jmp_buf main_env;
 
 /* Store the prefix specified by an argument.  */
-static char *root_dev = NULL, *dir = NULL;
+static char *root_dev = NULL, *dir = NULL, *tpm_dev = NULL;
 
 grub_addr_t grub_modbase = 0;
 

grub-core/kern/emu/main.c static struct argp_option options[] = { · line 111

Quoted 4 lines
grub-core/kern/emu/main.c +1 -0
@@ -108,6 +108,7 @@ static struct argp_option options[] = {
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
   {"kexec",       'X', 0,      0, N_("use kexec to boot Linux kernels via systemctl (pass twice to enable dangerous fallback to non-systemctl)."), 0},
+  {"tpm-device",  't', N_("DEV"), 0, N_("Set TPM device."), 0},

Again, this has to be documented.

Otherwise patch LGTM...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#65 lore raw

PATCH v18 25/25 tests: Add tpm2_key_protector_test

In reply to Gary Lin via Grub-devel

For the tpm2_key_protector module, the TCG2 command submission function is the only difference between a QEMU instance and grub-emu. To test TPM2 key unsealing with a QEMU instance, it requires an extra OS image to invoke grub-protect to seal the LUKS key, rather than a simple grub-shell rescue CD image. On the other hand, grub-emu can share the emulated TPM2 device with the host, so that we can seal the LUKS key on host and test key unsealing with grub-emu.

This test script firstly creates a simple LUKS image to be loaded as a loopback device in grub-emu. Then an emulated TPM2 device is created by "swtpm chardev" and PCR 0 and 1 are extended.

There are several test cases in the script to test various settings. Each test case uses grub-protect or tpm2-tools to seal the LUKS password with PCR 0 and PCR 1. Then grub-emu is launched to load the LUKS image, try to mount the image with tpm2_key_protector_init and cryptomount, and verify the result.

Based on the idea from Michael Chang.

Cc Michael Chang <[email protected]>
Cc Stefan Berger <[email protected]>
Cc Glenn Washburn <[email protected]>
---
 Makefile.util.def                |   6 +
 tests/tpm2_key_protector_test.in | 389 +++++++++++++++++++++++++++++++
 tests/util/grub-shell.in         |   6 +-
 3 files changed, 400 insertions(+), 1 deletion(-)
 create mode 100644 tests/tpm2_key_protector_test.in
Makefile.util.def +6 -0
index 074c0aff7..038253b37 100644
@@ -1290,6 +1290,12 @@ script = {
   common = tests/asn1_test.in;
 };
 
+script = {
+  testcase = native;
+  name = tpm2_key_protector_test;
+  common = tests/tpm2_key_protector_test.in;
+};
+
 program = {
   testcase = native;
   name = example_unit_test;
tests/tpm2_key_protector_test.in new file +389 -0

389 lines not on this page · raw message

tests/util/grub-shell.in +5 -1
index ae5f711fe..15c5f45a5 100644
@@ -75,6 +75,7 @@ work_directory=${WORKDIR:-`mktemp -d "${TMPDIR:-/tmp}/grub-shell.XXXXXXXXXX"`} |
 
 . "${builddir}/grub-core/modinfo.sh"
 qemuopts=
+emuopts=
 serial_port=com0
 serial_null=
 halt_cmd=halt
@@ -376,6 +377,9 @@ for option in "$@"; do
     --qemu-opts=*)
 	qs=`echo "$option" | sed -e 's/--qemu-opts=//'`
 	qemuopts="$qemuopts $qs" ;;
+    --emu-opts=*)
+	qs=`echo "$option" | sed -e 's/--emu-opts=//'`
+	emuopts="$emuopts $qs" ;;
     --disk=*)
 	dsk=`echo "$option" | sed -e 's/--disk=//'`
 	if [ ${grub_modinfo_platform} = emu ]; then
@@ -674,7 +678,7 @@ elif [ x$boot = xemu ]; then
     cat >"$work_directory/run.sh" <<EOF
 #! @BUILD_SHEBANG@
 SDIR=\$(realpath -e \${0%/*})
-exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub"
+exec "$(realpath -e "${builddir}")/grub-core/grub-emu" -m "\$SDIR/${device_map##*/}" --memdisk "\$SDIR/${roottar##*/}" -r memdisk -d "/boot/grub" ${emuopts}
 EOF
 else
     cat >"$work_directory/run.sh" <<EOF
Signature · 7 lines
2.35.3


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Reviewed-by on v18 25/25
#66 lore raw

Re: [PATCH v18 25/25] tests: Add tpm2_key_protector_test

Quoted 18 lines · Gary Lin via Grub-devel

For the tpm2_key_protector module, the TCG2 command submission function is the only difference between a QEMU instance and grub-emu. To test TPM2 key unsealing with a QEMU instance, it requires an extra OS image to invoke grub-protect to seal the LUKS key, rather than a simple grub-shell rescue CD image. On the other hand, grub-emu can share the emulated TPM2 device with the host, so that we can seal the LUKS key on host and test key unsealing with grub-emu.

This test script firstly creates a simple LUKS image to be loaded as a loopback device in grub-emu. Then an emulated TPM2 device is created by "swtpm chardev" and PCR 0 and 1 are extended.

There are several test cases in the script to test various settings. Each test case uses grub-protect or tpm2-tools to seal the LUKS password with PCR 0 and PCR 1. Then grub-emu is launched to load the LUKS image, try to mount the image with tpm2_key_protector_init and cryptomount, and verify the result.

Quoted 6 lines

Based on the idea from Michael Chang.

Cc Michael Chang <[email protected]>
Cc Stefan Berger <[email protected]>
Cc Glenn Washburn <[email protected]>

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#67 lore raw
In reply to Gary Lin via Grub-devel
Quoted 11 lines · Gary Lin via Grub-devel

GIT repo for v18: https://github.com/lcp/grub2/tree/tpm2-unlock-v18

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~6,8-10 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.

Build of this version fails in the following way:

arm-linux-gnueabihf-gcc -DHAVE_CONFIG_H -I. -I.. -Wall -W -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM_EFI -mword-relocations -nostdinc -isystem /usr/lib/gcc-cross/arm-linux-gnueabihf/6/include -I../include -I../include -DGRUB_FILE=\"commands/tpm2_key_protector/args.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/ -I./lib/tss2 -I./lib/libtasn1-grub -D_FILE_OFFSET_BITS=64 -std=gnu99 -fno-common -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -msoft-float -fno-omit-frame-pointer -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-ident -mthumb-interwork -fno-PIE -fno-pie -fno-stack-protector -mno-unaligned-access -Wtrampolines -Werror -ffreestanding -MT commands/tpm2_key_protector/tpm2_key_protector_module-args.o -MD -MP -MF commands/tpm2_key_protector/.deps-core/tpm2_key_protector_module-args.Tpo -c -o commands/tpm2_key_protector/tpm2_key_protector_module-args.o test -f 'commands/tpm2_key_protector/args.c' || echo './'commands/tpm2_key_protector/args.c
In file included from ../include/grub/misc.h:27:0,
from commands/tpm2_key_protector/args.c:22:
commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_pcrs’:
commands/tpm2_key_protector/args.c:54:47: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]

    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Entry %lu in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u"), pcr, TPM_MAX_PCRS);
                                                 ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~
  commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_tpm_handle’:
  commands/tpm2_key_protector/args.c:125:50: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]
       return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Value %lu is too large to be a TPM handle, TPM handles are unsigned 32-bit integers"), num);
                                                    ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~

I think you can drop N_() macros here and use PRIuGRUB_UINT64_T instead of "%lu".

[...]

Quoted 99 lines

==
7 Integer handling issues:
CID 435774, CID 435773, CID 435772, CID 435768, CID 435765, CID 435764, CID 435763

Those are related to Gnulib macros and false positive. Here are the analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

/grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW.

'(a) < (min) >> (b)'
=> '(val) < _GL_INT_MINIMUM (val) >> (7)'

=> '(val) < \
     (EXPR_SIGNED (val) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
      : ((1 ? 0 : (val)) + (0))) \
     >> (7)'
Quoted 4 lines

'_GL_INT_MINIMUM' returns the minimum value of the given type. Since 'val' is 'uint64_t', '_GL_INT_MINIMUM (val)' is 0, thus '(val) < 0 >> (7)' is always false. Besides, '(val) < 0' is false, so "(max) >> (b) < (a))" becomes the only valid statement to check if 'a' overflows after left shift.

... but this final statement does not explain why the CID can be ignored. AIUI simply we do not care about second operand of "?:" in this case...

Quoted 53 lines

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))

The statement in question is the expanded 'EXPR_SIGNED (val0)' from either '_GL_INT_MAXIMUM' or '_GL_INT_MINIMUM'

Quoted 8 lines

'EXPR_SIGNED (val0)'
=> '(_GL_INT_NEGATE_CONVERT (val0, 1) < 0)'
=> '(((1 ? 0 : (val0)) - (1)) < 0)'

Since 'EXPR_SIGNED' is to test if the given expression is signed or not. For 'uint64_t val0', it's expected to be false, and '_GL_INT_MINIMUM' returns the minimum value of 'uint64_t' while '_GL_INT_MAXIMUM' returns the maximum value of 'uint64_t'.

This explanation is missing final statement why this CID can be ignored. Please fix this.

Quoted 74 lines

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max)     \
  ((b) < 0                                              \
   ? ((a) < 0                                           \
      ? (a) < (max) / (b)                               \
      : (b) == -1                                       \
      ? 0                                               \
      : (min) / (b) < (a))                              \
   : (b) == 0                                           \
   ? 0                                                  \
   : ((a) < 0                                           \
      ? (a) < (min) / (b)                               \
      : (max) / (b) < (a)))
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                           \
   (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a))))       \
    || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))

#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \

  op_result_overflow (a, b,                                     \
                      _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
                      _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))

#define INT_MULTIPLY_OVERFLOW(a, b) \
_GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)

The statement in question is the expanded 'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.

'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))'
=> 'EXPR_SIGNED ((1 ? 0 : (ris)) + (128))'
=> '(_GL_INT_NEGATE_CONVERT (((1 ? 0 : (ris)) + (128), 1) < 0))' => '((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0)'

Quoted 5 lines

Since 'ris' is 'unsigned int', the compiler treats 128 as 'unsigned int' too. 'EXPR_SIGNED' is designed to check if the given expression is signed or not. Thus, the result is expected to be false and '_GL_INT_MINIMUM' returns the minimum value of 'unsigned int' while '_GL_INT_MAXIMUM' returns the maximum value of 'unsigned int'.

Ditto. In general I have a feeling that your descriptions require some rephrasing to make them clear. Same comment applies to descriptions below...

Quoted 11 lines

________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

[...]

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#68 lore raw
Quoted 33 lines · Daniel Kiper
Quoted 11 lines · Gary Lin via Grub-devel

GIT repo for v18: https://github.com/lcp/grub2/tree/tpm2-unlock-v18

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~6,8-10 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.

Build of this version fails in the following way:

arm-linux-gnueabihf-gcc -DHAVE_CONFIG_H -I. -I.. -Wall -W -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM_EFI -mword-relocations -nostdinc -isystem /usr/lib/gcc-cross/arm-linux-gnueabihf/6/include -I../include -I../include -DGRUB_FILE=\"commands/tpm2_key_protector/args.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/ -I./lib/tss2 -I./lib/libtasn1-grub -D_FILE_OFFSET_BITS=64 -std=gnu99 -fno-common -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -msoft-float -fno-omit-frame-pointer -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-ident -mthumb-interwork -fno-PIE -fno-pie -fno-stack-protector -mno-unaligned-access -Wtrampolines -Werror -ffreestanding -MT commands/tpm2_key_protector/tpm2_key_protector_module-args.o -MD -MP -MF commands/tpm2_key_protector/.deps-core/tpm2_key_protector_module-args.Tpo -c -o commands/tpm2_key_protector/tpm2_key_protector_module-args.o test -f 'commands/tpm2_key_protector/args.c' || echo './'commands/tpm2_key_protector/args.c
In file included from ../include/grub/misc.h:27:0,
from commands/tpm2_key_protector/args.c:22:
commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_pcrs’:
commands/tpm2_key_protector/args.c:54:47: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]

    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Entry %lu in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u"), pcr, TPM_MAX_PCRS);
                                                 ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~
  commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_tpm_handle’:
  commands/tpm2_key_protector/args.c:125:50: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]
       return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Value %lu is too large to be a TPM handle, TPM handles are unsigned 32-bit integers"), num);
                                                    ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~
Quoted 2 lines

I think you can drop N_() macros here and use PRIuGRUB_UINT64_T instead of "%lu".

The N_() marcors are dropped in my WIP branch per Vladimir's comment. I'll fix the type error in the next version.

Quoted 101 lines

[...]

Quoted 99 lines

==
7 Integer handling issues:
CID 435774, CID 435773, CID 435772, CID 435768, CID 435765, CID 435764, CID 435763

Those are related to Gnulib macros and false positive. Here are the analyses:

________________________________________________________________________________________________________
*** CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT) /grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()
475 */
476 if (leading != 0 && der[len_len + k] == 0x80)
477 return ASN1_DER_ERROR;
478 leading = 0;
479
480 /* check for wrap around */

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435774: Integer handling issues (CONSTANT_EXPRESSION_RESULT)
"val < ((((1 ? 0 : val) - 1 < 0) ? ~((((1 ? 0 : val) + 1 << 62UL /* sizeof (+val) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : val) + 0)) >> 7)" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

481 if (INT_LEFT_SHIFT_OVERFLOW (val, 7))
482 return ASN1_DER_ERROR;
483
484 val = val << 7;
485 val |= der[len_len + k] & 0x7F;
486

/grub-core/lib/libtasn1/lib/decoding.c: 481 in asn1_get_object_id_der()

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))

The statement in question is expanded "(a) < (min) >> (b)" of INT_LEFT_SHIFT_RANGE_OVERFLOW.

'(a) < (min) >> (b)'
=> '(val) < _GL_INT_MINIMUM (val) >> (7)'

=> '(val) < \
     (EXPR_SIGNED (val) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((_GL_INT_NEGATE_CONVERT (val, 1) < 0) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ _GL_SIGNED_INT_MAXIMUM (val) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((_GL_INT_CONVERT (val, 1) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << (TYPE_WIDTH (+ (val)) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
      : _GL_INT_CONVERT (val, 0)) \
     >> (7)'
=> '(val) < \
     ((((1 ? 0 : (val)) - (1)) < 0) \
      ? ~ (((((1 ? 0 : (val)) + (1)) << ((sizeof (val) * CHAR_BIT) - 2)) - 1) * 2 + 1) \
      : ((1 ? 0 : (val)) + (0))) \
     >> (7)'
Quoted 8 lines
Quoted 4 lines

'_GL_INT_MINIMUM' returns the minimum value of the given type. Since 'val' is 'uint64_t', '_GL_INT_MINIMUM (val)' is 0, thus '(val) < 0 >> (7)' is always false. Besides, '(val) < 0' is false, so "(max) >> (b) < (a))" becomes the only valid statement to check if 'a' overflows after left shift.

... but this final statement does not explain why the CID can be ignored. AIUI simply we do not care about second operand of "?:" in this case...

I'll updatet the statement for all CIDs in the next version.

Gary Lin

Quoted 158 lines
Quoted 53 lines

________________________________________________________________________________________________________
*** CID 435773: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 439 in asn1_get_object_id_der()
433 return ASN1_DER_ERROR;
434
435 val0 = 0;
436
437 for (k = 0; k < len; k++)
438 {

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435773: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0UL : val0) - 1UL < 0UL".

439 if (INT_LEFT_SHIFT_OVERFLOW (val0, 7))
440 return ASN1_DER_ERROR;
441

442           val0 <<= 7;
443           val0 |= der[len_len + k] & 0x7F;
444           if (!(der[len_len + k] & 0x80))

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_LEFT_SHIFT_RANGE_OVERFLOW(a, b, min, max)   \
  ((a) < 0                                              \
   ? (a) < (min) >> (b)                                 \

: (max) >> (b) < (a))

#define INT_LEFT_SHIFT_OVERFLOW(a, b) \

  INT_LEFT_SHIFT_RANGE_OVERFLOW (a, b, \
                                 _GL_INT_MINIMUM (a), _GL_INT_MAXIMUM (a))

The statement in question is the expanded 'EXPR_SIGNED (val0)' from either '_GL_INT_MAXIMUM' or '_GL_INT_MINIMUM'

Quoted 8 lines

'EXPR_SIGNED (val0)'
=> '(_GL_INT_NEGATE_CONVERT (val0, 1) < 0)'
=> '(((1 ? 0 : (val0)) - (1)) < 0)'

Since 'EXPR_SIGNED' is to test if the given expression is signed or not. For 'uint64_t val0', it's expected to be false, and '_GL_INT_MINIMUM' returns the minimum value of 'uint64_t' while '_GL_INT_MAXIMUM' returns the maximum value of 'uint64_t'.

This explanation is missing final statement why this CID can be ignored. Please fix this.

Quoted 74 lines

________________________________________________________________________________________________________
*** CID 435772: Integer handling issues (NO_EFFECT) /grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

CID 435772: Integer handling issues (NO_EFFECT)
This less-than-zero comparison of an unsigned value is never true. "(1 ? 0U : ((1 ? 0U : ris) + 128U)) - 1U < 0U".

204     	  if (INT_MULTIPLY_OVERFLOW (ris, 128))
205     	    return ASN1_DER_ERROR;
206     	  ris *= 128;

207
208 if (INT_ADD_OVERFLOW (ris, ((unsigned) (der[punt] & 0x7F))))
209 return ASN1_DER_ERROR;

Here are the related macros:

#define _GL_INT_NEGATE_CONVERT(e, v) ((1 ? 0 : (e)) - (v))

#define _GL_INT_CONVERT(e, v) ((1 ? 0 : (e)) + (v))

#define EXPR_SIGNED(e) (_GL_INT_NEGATE_CONVERT (e, 1) < 0)

#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)

#define _GL_SIGNED_INT_MAXIMUM(e) \
(((_GL_INT_CONVERT (e, 1) << (TYPE_WIDTH (+ (e)) - 2)) - 1) * 2 + 1)

#define _GL_INT_MINIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? ~ _GL_SIGNED_INT_MAXIMUM (e)                                       \

: _GL_INT_CONVERT (e, 0))

#define _GL_INT_MAXIMUM(e)                                              \
  (EXPR_SIGNED (e)                                                      \
   ? _GL_SIGNED_INT_MAXIMUM (e)                                         \

: _GL_INT_NEGATE_CONVERT (e, 1))

#define INT_MULTIPLY_RANGE_OVERFLOW(a, b, min, max)     \
  ((b) < 0                                              \
   ? ((a) < 0                                           \
      ? (a) < (max) / (b)                               \
      : (b) == -1                                       \
      ? 0                                               \
      : (min) / (b) < (a))                              \
   : (b) == 0                                           \
   ? 0                                                  \
   : ((a) < 0                                           \
      ? (a) < (min) / (b)                               \
      : (max) / (b) < (a)))
# define _GL_MULTIPLY_OVERFLOW(a, b, min, max)                           \
   (((min) == 0 && (((a) < 0 && 0 < (b)) || ((b) < 0 && 0 < (a))))       \
    || INT_MULTIPLY_RANGE_OVERFLOW (a, b, min, max))

#define _GL_BINARY_OP_OVERFLOW(a, b, op_result_overflow) \

  op_result_overflow (a, b,                                     \
                      _GL_INT_MINIMUM (_GL_INT_CONVERT (a, b)), \
                      _GL_INT_MAXIMUM (_GL_INT_CONVERT (a, b)))

#define INT_MULTIPLY_OVERFLOW(a, b) \
_GL_BINARY_OP_OVERFLOW (a, b, _GL_MULTIPLY_OVERFLOW)

The statement in question is the expanded 'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))' from either '_GL_INT_MINIMUM' or '_GL_INT_MAXIMUM'.

'EXPR_SIGNED (_GL_INT_CONVERT (ris, 128))'
=> 'EXPR_SIGNED ((1 ? 0 : (ris)) + (128))'
=> '(_GL_INT_NEGATE_CONVERT (((1 ? 0 : (ris)) + (128), 1) < 0))' => '((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0)'

Quoted 5 lines

Since 'ris' is 'unsigned int', the compiler treats 128 as 'unsigned int' too. 'EXPR_SIGNED' is designed to check if the given expression is signed or not. Thus, the result is expected to be false and '_GL_INT_MINIMUM' returns the minimum value of 'unsigned int' while '_GL_INT_MAXIMUM' returns the maximum value of 'unsigned int'.

Ditto. In general I have a feeling that your descriptions require some rephrasing to make them clear. Same comment applies to descriptions below...

Quoted 9 lines

________________________________________________________________________________________________________
*** CID 435768: (CONSTANT_EXPRESSION_RESULT)
/grub-core/lib/libtasn1/lib/decoding.c: 204 in asn1_get_tag_der()

198           /* Long form */
199           punt = 1;
200           ris = 0;
201           while (punt < der_len && der[punt] & 128)

202 {
203

Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    CID 435768:    (CONSTANT_EXPRESSION_RESULT)
    "ris < (((1 ? 0 : ((1 ? 0 : ris) + 128)) - 1 < 0) ? ~((((1 ? 0 : ((1 ? 0 : ris) + 128)) + 1 << 30UL /* sizeof (+((1 ? 0 : ris) + 128)) * 8 - 2 */) - 1) * 2 + 1) : ((1 ? 0 : ((1 ? 0 : ris) + 128)) + 0)) / 128" is always false regardless of the values of its operands. This occurs as the second operand of "?:".

[...]

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#69 lore raw
Quoted 34 lines · Gary Lin
Quoted 33 lines · Daniel Kiper
Quoted 11 lines · Gary Lin via Grub-devel

GIT repo for v18: https://github.com/lcp/grub2/tree/tpm2-unlock-v18

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~6,8-10 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.

Build of this version fails in the following way:

arm-linux-gnueabihf-gcc -DHAVE_CONFIG_H -I. -I.. -Wall -W -DGRUB_MACHINE_EFI=1 -DGRUB_MACHINE=ARM_EFI -mword-relocations -nostdinc -isystem /usr/lib/gcc-cross/arm-linux-gnueabihf/6/include -I../include -I../include -DGRUB_FILE=\"commands/tpm2_key_protector/args.c\" -I. -I. -I.. -I.. -I../include -I../include -I../grub-core/lib/libgcrypt-grub/src/ -I./lib/tss2 -I./lib/libtasn1-grub -D_FILE_OFFSET_BITS=64 -std=gnu99 -fno-common -Os -Wall -W -Wshadow -Wpointer-arith -Wundef -Wchar-subscripts -Wcomment -Wdeprecated-declarations -Wdisabled-optimization -Wdiv-by-zero -Wfloat-equal -Wformat-extra-args -Wformat-security -Wformat-y2k -Wimplicit -Wimplicit-function-declaration -Wimplicit-int -Wmain -Wmissing-braces -Wmissing-format-attribute -Wmultichar -Wparentheses -Wreturn-type -Wsequence-point -Wshadow -Wsign-compare -Wswitch -Wtrigraphs -Wunknown-pragmas -Wunused -Wunused-function -Wunused-label -Wunused-parameter -Wunused-value -Wunused-variable -Wwrite-strings -Wnested-externs -Wstrict-prototypes -g -Wredundant-decls -Wmissing-prototypes -Wmissing-declarations -Wcast-align -Wextra -Wattributes -Wendif-labels -Winit-self -Wint-to-pointer-cast -Winvalid-pch -Wmissing-field-initializers -Wnonnull -Woverflow -Wvla -Wpointer-to-int-cast -Wstrict-aliasing -Wvariadic-macros -Wvolatile-register-var -Wpointer-sign -Wmissing-include-dirs -Wmissing-prototypes -Wmissing-declarations -Wformat=2 -freg-struct-return -msoft-float -fno-omit-frame-pointer -fno-dwarf2-cfi-asm -fno-asynchronous-unwind-tables -fno-unwind-tables -fno-ident -mthumb-interwork -fno-PIE -fno-pie -fno-stack-protector -mno-unaligned-access -Wtrampolines -Werror -ffreestanding -MT commands/tpm2_key_protector/tpm2_key_protector_module-args.o -MD -MP -MF commands/tpm2_key_protector/.deps-core/tpm2_key_protector_module-args.Tpo -c -o commands/tpm2_key_protector/tpm2_key_protector_module-args.o test -f 'commands/tpm2_key_protector/args.c' || echo './'commands/tpm2_key_protector/args.c
In file included from ../include/grub/misc.h:27:0,
from commands/tpm2_key_protector/args.c:22:
commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_pcrs’:
commands/tpm2_key_protector/args.c:54:47: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]

    return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Entry %lu in PCR list is too large to be a PCR number, PCR numbers range from 0 to %u"), pcr, TPM_MAX_PCRS);
                                                 ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~
  commands/tpm2_key_protector/args.c: In function ‘grub_tpm2_protector_parse_tpm_handle’:
  commands/tpm2_key_protector/args.c:125:50: error: format ‘%lu’ expects argument of type ‘long unsigned int’, but argument 3 has type ‘grub_uint64_t {aka long long unsigned int}’ [-Werror=format=]
       return grub_error (GRUB_ERR_OUT_OF_RANGE, N_("Value %lu is too large to be a TPM handle, TPM handles are unsigned 32-bit integers"), num);
                                                    ^
  ../include/grub/i18n.h:66:17: note: in definition of macro ‘N_’
   #define N_(str) str
                   ^~~
Quoted 4 lines
Quoted 1 line

I think you can drop N_() macros here and use PRIuGRUB_UINT64_T instead of "%lu".

The N_() marcors are dropped in my WIP branch per Vladimir's comment. I'll fix the type error in the next version.

One thing, you should drop N_()/_() macros only from very technical messages which can be difficult to translate and to understand by average user.

Daniel

PS Please do not post next version of patches until I finish reviewing
this version. I hope I will be able to do it by the end of next week.

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#70 lore raw
In reply to Gary Lin via Grub-devel
Quoted 48 lines · Gary Lin via Grub-devel

GIT repo for v18: https://github.com/lcp/grub2/tree/tpm2-unlock-v18

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~6,8-10 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.

Patch 7 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)

Patch 11 adds the document for libtasn1 and the steps to upgrade the library.

Patch 12~18 are based on Hernan Gatta's patches with the follow-up fixes and improvements:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based on the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters of TPM2_EvictControl to match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands on TPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command for TPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL so that we don't have to declare empty variables
- Using grub_cpu_to_be*() in the TPM2 stack instead of grub_swap_bytes*() which may cause problems in big-indian machines
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Supporting TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency to grub-protect to write the TPM 2.0 Key files
- Extending the TPM2 TSS stack to support authorized policy

Patch 19 implements the authorized policy support.

Patch 20 implements the missing NV index mode. (Thanks to Patrick Colp)

Quoted 7 lines

Patch 21 improves the 'cryptomount' command to fall back to the passphrase mode when the key protector fails to unlock the encrypted partition. (Another patch from Patrick Colp)

Patch 22 and 23 fix the potential security issues spotted by Fabian Vogt.

Patch 24 and 25 implement the TPM2 key unsealing testcases.

It seems to me this patch set misses usage documentation with examples. Could you add it to the docs/grub.texi?

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#71 lore raw
Quoted 53 lines · Daniel Kiper
Quoted 52 lines · Gary Lin via Grub-devel

GIT repo for v18: https://github.com/lcp/grub2/tree/tpm2-unlock-v18

This patch series is based on "Automatic TPM Disk Unlock"(*1) posted by Hernan Gatta to introduce the key protector framework and TPM2 stack to GRUB2, and this could be a useful feature for the systems to implement full disk encryption.

To support TPM 2.0 Key File format(*2), patch 1~6,8-10 are grabbed from Daniel Axtens's "appended signature secure boot support" (*3) to import libtasn1 into grub2. Besides, the libtasn1 version is upgraded to 4.19.0 instead of 4.16.0 in the original patch.

Patch 7 fixes a potential buffer overrun in libtasn1.
(https://gitlab.com/gnutls/libtasn1/-/issues/49)

Patch 11 adds the document for libtasn1 and the steps to upgrade the library.

Patch 12~18 are based on Hernan Gatta's patches with the follow-up fixes and improvements:
- Converting 8 spaces into 1 tab
- Merging the minor build fix from Michael Chang
- Replacing "lu" with "PRIuGRUB_SIZE" for grub_dprintf
- Adding "enable = efi" to the tpm2 module in grub-core/Makefile.core.def
- Rebasing "cryptodisk: Support key protectors" to the git master
- Removing the measurement on the sealed key
- Based on the patch from Olaf Kirch <[email protected]>
- Adjusting the input parameters of TPM2_EvictControl to match the order in "TCG TPM2 Part3 Commands"
- Declaring the input arguments of TPM2 functions as const
- Resending TPM2 commands on TPM_RC_RETRY
- Adding checks for the parameters of TPM2 commands
- Packing the missing authorization command for TPM2_PCR_Read
- Tweaking the TPM2 command functions to allow some parameters to be NULL so that we don't have to declare empty variables
- Using grub_cpu_to_be*() in the TPM2 stack instead of grub_swap_bytes*() which may cause problems in big-indian machines
- Changing the short name of "--protector" of "cryptomount" from "-k" to "-P" to avoid the conflict with "--key-file"
- Supporting TPM 2.0 Key File Format besides the raw sealed key
- Adding the external libtasn1 dependency to grub-protect to write the TPM 2.0 Key files
- Extending the TPM2 TSS stack to support authorized policy

Patch 19 implements the authorized policy support.

Patch 20 implements the missing NV index mode. (Thanks to Patrick Colp)

Patch 21 improves the 'cryptomount' command to fall back to the passphrase mode when the key protector fails to unlock the encrypted partition. (Another patch from Patrick Colp)

Quoted 7 lines
Quoted 3 lines

Patch 22 and 23 fix the potential security issues spotted by Fabian Vogt.

Patch 24 and 25 implement the TPM2 key unsealing testcases.

It seems to me this patch set misses usage documentation with examples. Could you add it to the docs/grub.texi?

Ok. I can add a few examples including how to set up swtpm instance for grub-emu.

Gary Lin

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel