add support for reading per bank TPM 2.0 PCRs via sysfs
PATCH 1/1 add support for reading per bank TPM 2.0 PCRs via sysfs
Until Linux kernel version 5.11, a TSS was required to read TPM 2.0 PCR values. A feature which exposed the per bank TPM 2.0 PCRs directly via sysfs was upstreamed in newer Kernel versions.
Use this recent feature in IMA-EVM-UTILS to remove TSS dependency.
--- src/evmctl.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
src/evmctl.c +53 -0
@@ -1899,6 +1899,7 @@ static int read_one_bank(struct tpm_bank_info *tpm_bank, FILE *fp)static char *pcrs ="/sys/class/tpm/tpm0/device/pcrs" ;/* Kernels >= 4.0 */ static char *misc_pcrs ="/sys/class/misc/tpm0/device/pcrs" ; +static char tpm2_pcr_path[28 ] ="/sys/class/tpm/tpm0/pcr-sha" ;/* Read one of the TPM 1.2 sysfs files if present */ static int read_sysfs_pcrs (int num_banks,struct tpm_bank_info *tpm_banks)
@@ -1922,7 +1923,55 @@ static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks)for (i =1 ; i < num_banks; i++) tpm_banks[i].supported =0 ;return 0 ; +} + +static int read_tpm2_one_bank (struct tpm_bank_info *tpm_bank,int bank) +{ +FILE *fp; +char file_name[NAME_MAX ]; +char digest[MAX_DIGEST_SIZE +1 ]; +char *p; +int i; + +for (i =0 ; i <NUM_PCRS ; i++) { +sprintf (file_name,"%s%d/%d" , tpm2_pcr_path, bank, i); + fp =fopen (file_name,"r" ); +if (!fp) +return -1 ; + + p =fgets (digest, (tpm_bank->digest_size *2 +1 ), fp); +if (!p) +return -1 ; + +hex2bin (tpm_bank->pcr[i], digest, tpm_bank->digest_size); +fclose (fp); + } +return 0 ; +} + +static int read_sysfs_tpm2_pcrs (int num_banks,struct tpm_bank_info *tpm_banks) +{ +int banks[2 ] = {1 ,256 }; +int rt, j; +int tpm_enabled =0 ; +if (imaevm_params.verbose >LOG_INFO ) +log_info ("Trying to read TPM 2.0 PCRs via sysfs.\n" ); + +for (j =0 ; j < num_banks; j++) { + rt =read_tpm2_one_bank (&tpm_banks[j], banks[j]); + tpm_banks[j].supported =0 ; + +if (rt <0 ) +continue ; + + tpm_enabled =1 ; + tpm_banks[j].supported =1 ; + } + +if (tpm_enabled ==0 ) +return -1 ; +return 0 ; }/* Read PCRs from per-bank file(s) specified via --pcrs */
@@ -2008,6 +2057,9 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank)if (read_sysfs_pcrs (num_banks, bank) ==0 )return 0 ; +if (read_sysfs_tpm2_pcrs (num_banks, bank) ==0 ) +return 0 ; +/* Any userspace applications available for reading TPM 2.0 PCRs? */ if (!tpm2_pcr_supported ()) {log_debug ("Failed to read TPM 2.0 PCRs\n" );
@@ -2024,6 +2076,7 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank) bank[i].pcr[pcr_handle], bank[i].digest_size, &errmsg); + bank[i].supported =1 ;if (err) {log_debug ("Failed to read %s PCRs: (%s)\n" , bank[i].algo_name, errmsg);
Signature · 1 line
2.32.1 (Apple Git-133)
Quoted 16 lines · Tergel Myanganbayar
Until Linux kernel version 5.11, a TSS was required to read TPM 2.0 PCR values. A feature which exposed the per bank TPM 2.0 PCRs directly via sysfs was upstreamed in newer Kernel versions.
Quoted 1 line
Use this recent feature in IMA-EVM-UTILS to remove TSS dependency.
Signed-off-by Tergel Myanganbayar <[email protected]>--- src/evmctl.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)src/evmctl.c +0
-0
src/evmctl.c static int read_one_bank(struct tpm_bank_info *tpm_bank, FILE *fp) · line 1902
Quoted 4 lines
src/evmctl.c +1
-0@@ -1899,6 +1899,7 @@ static int read_one_bank(struct tpm_bank_info *tpm_bank, FILE *fp)static char *pcrs ="/sys/class/tpm/tpm0/device/pcrs" ;/* Kernels >= 4.0 */ static char *misc_pcrs ="/sys/class/misc/tpm0/device/pcrs" ; +static char tpm2_pcr_path[28 ] ="/sys/class/tpm/tpm0/pcr-sha" ;
I don't think this 'constant' is necessary since it's only used once though the same may be true for the other two. If you want to keep it it should probably be written like this
static const char *tpm2_pcr_path = "/sys/class/tpm/tpm0/pcr-sha";
The others should also have a 'const'.
Quoted 9 lines
src/evmctl.c +2
-0/* Read one of the TPM 1.2 sysfs files if present */ static int read_sysfs_pcrs (int num_banks,struct tpm_bank_info *tpm_banks)@@ -1922,7 +1923,55 @@ static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks)for (i =1 ; i < num_banks; i++) tpm_banks[i].supported =0 ;return 0 ; +} +
src/evmctl.c static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks) · line 1937
Quoted 10 lines
src/evmctl.c +10
-0+static int read_tpm2_one_bank (struct tpm_bank_info *tpm_bank,int bank) +{ +FILE *fp; +char file_name[NAME_MAX ]; +char digest[MAX_DIGEST_SIZE +1 ]; +char *p; +int i; + +for (i =0 ; i <NUM_PCRS ; i++) { +sprintf (file_name,"%s%d/%d" , tpm2_pcr_path, bank, i);
... and just write "/sys/class/tpm/tpm0/pcr-sha%d/%d" here ?
src/evmctl.c static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks) · line 1942
Quoted 5 lines
src/evmctl.c +5
-0+ fp =fopen (file_name,"r" ); +if (!fp) +return -1 ; + + p =fgets (digest, (tpm_bank->digest_size *2 +1 ), fp);
no need for parenthesis
src/evmctl.c static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks) · line 1944
Quoted 2 lines
src/evmctl.c +2
-0+if (!p) +return -1 ;
fclose(fp) before the return
Quoted 20 lines
src/evmctl.c +19
-0+ +hex2bin (tpm_bank->pcr[i], digest, tpm_bank->digest_size); +fclose (fp); + } +return 0 ; +} + +static int read_sysfs_tpm2_pcrs (int num_banks,struct tpm_bank_info *tpm_banks) +{ +int banks[2 ] = {1 ,256 }; +int rt, j; +int tpm_enabled =0 ; +if (imaevm_params.verbose >LOG_INFO ) +log_info ("Trying to read TPM 2.0 PCRs via sysfs.\n" ); + +for (j =0 ; j < num_banks; j++) { + rt =read_tpm2_one_bank (&tpm_banks[j], banks[j]); + tpm_banks[j].supported =0 ; +
src/evmctl.c static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks) · line 1974
Quoted 10 lines
src/evmctl.c +10
-0+if (rt <0 ) +continue ; + + tpm_enabled =1 ; + tpm_banks[j].supported =1 ; + } + +if (tpm_enabled ==0 ) +return -1 ; +return 0 ;
You could have just copied and pasted from read_tpm_banks():
return tpm_enabled ? 0 : 1;
Quoted 14 lines
src/evmctl.c +3
-0}/* Read PCRs from per-bank file(s) specified via --pcrs */ @@ -2008,6 +2057,9 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank)if (read_sysfs_pcrs (num_banks, bank) ==0 )return 0 ; +if (read_sysfs_tpm2_pcrs (num_banks, bank) ==0 ) +return 0 ; +/* Any userspace applications available for reading TPM 2.0 PCRs? */ if (!tpm2_pcr_supported ()) {log_debug ("Failed to read TPM 2.0 PCRs\n" );
src/evmctl.c static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank) · line 2079
Quoted 4 lines
src/evmctl.c +1
-0@@ -2024,6 +2076,7 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank) bank[i].pcr[pcr_handle], bank[i].digest_size, &errmsg); + bank[i].supported =1 ;
Is this a bugfix?
Quoted 3 lines
src/evmctl.c +0
-0if (err) {log_debug ("Failed to read %s PCRs: (%s)\n" , bank[i].algo_name, errmsg);
Mimi Zohar
maintainerQuoted 4 lines · Stefan Berger
Quoted 4 lines
src/evmctl.c +4
-0+static int read_tpm2_one_bank (struct tpm_bank_info *tpm_bank,int bank) +{ +FILE *fp; +char file_name[NAME_MAX ];
Quoted 8 lines
src/evmctl.c static int read_sysfs_pcrs(int num_banks, struct tpm_bank_info *tpm_banks) · line 1937
Quoted 6 lines
src/evmctl.c +6
-0+char digest[MAX_DIGEST_SIZE +1 ]; +char *p; +int i; + +for (i =0 ; i <NUM_PCRS ; i++) { +sprintf (file_name,"%s%d/%d" , tpm2_pcr_path, bank, i);... and just write "/sys/class/tpm/tpm0/pcr-sha%d/%d" here ?
Thanks, Stefan. Although the only banks currently supported are sha1
and sha256, let's not hard code the hash algorithm. The tpm_bank_info
struct contains the algo_name. With this change, there's no need for
the "banks[]" definition in read_sysfs_tpm2_pcrs() or passing it to read_tpm2_one_bank().
Signature · 3 lines
thanks, Mimi