1
0
mirror of https://git.code.sf.net/p/linux-ima/ima-evm-utils synced 2025-04-28 06:33:36 +02:00

Change PCR iterator from int to uint32_t

PCR numbers are naturally unsigned values.  Further, they are
32 bits, even on 64-bit machines. This change eliminates the
need for negative value and overflow tests.

The parameter name is changed from j and idx to pcr_handle, which is
more descriptive and is similar to the parameter name used in the TPM
2.0 specification.

Signed-off-by: Ken Goldman <kgoldman@us.ibm.com>
Signed-off-by: Mimi Zohar <zohar@linux.ibm.com>
This commit is contained in:
Ken Goldman 2021-08-03 16:40:07 -04:00 committed by Mimi Zohar
parent efacc1f396
commit e52fc1d330
4 changed files with 15 additions and 10 deletions

View File

@ -1914,7 +1914,8 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank)
{ {
int tpm_enabled = 0; int tpm_enabled = 0;
char *errmsg = NULL; char *errmsg = NULL;
int i, j; int i;
uint32_t pcr_handle;
int err; int err;
/* If --pcrs was specified, read only from the specified file(s) */ /* If --pcrs was specified, read only from the specified file(s) */
@ -1934,9 +1935,12 @@ static int read_tpm_banks(int num_banks, struct tpm_bank_info *bank)
/* Read PCRs from multiple TPM 2.0 banks */ /* Read PCRs from multiple TPM 2.0 banks */
for (i = 0; i < num_banks; i++) { for (i = 0; i < num_banks; i++) {
err = 0; err = 0;
for (j = 0; j < NUM_PCRS && !err; j++) { for (pcr_handle = 0;
err = tpm2_pcr_read(bank[i].algo_name, j, pcr_handle < NUM_PCRS && !err;
bank[i].pcr[j], bank[i].digest_size, pcr_handle++) {
err = tpm2_pcr_read(bank[i].algo_name, pcr_handle,
bank[i].pcr[pcr_handle],
bank[i].digest_size,
&errmsg); &errmsg);
if (err) { if (err) {
log_debug("Failed to read %s PCRs: (%s)\n", log_debug("Failed to read %s PCRs: (%s)\n",

View File

@ -1,3 +1,3 @@
int tpm2_pcr_supported(void); int tpm2_pcr_supported(void);
int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, int tpm2_pcr_read(const char *algo_name, uint32_t pcr_handle, uint8_t *hwpcr,
int len, char **errmsg); int len, char **errmsg);

View File

@ -106,7 +106,7 @@ static TPM2_ALG_ID algo_to_tss2(const char *algo_name)
return TPM2_ALG_ERROR; return TPM2_ALG_ERROR;
} }
int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, int tpm2_pcr_read(const char *algo_name, uint32_t pcr_handle, uint8_t *hwpcr,
int len, char **errmsg) int len, char **errmsg)
{ {
TSS2_ABI_VERSION abi_version = { TSS2_ABI_VERSION abi_version = {
@ -140,7 +140,8 @@ int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr,
} }
}; };
pcr_select_in.pcrSelections[0].pcrSelect[idx / 8] = (1 << (idx % 8)); pcr_select_in.pcrSelections[0].pcrSelect[pcr_handle / 8] =
(1 << (pcr_handle % 8));
ret = Esys_Initialize(&ctx, NULL, &abi_version); ret = Esys_Initialize(&ctx, NULL, &abi_version);
if (ret != TPM2_RC_SUCCESS) { if (ret != TPM2_RC_SUCCESS) {

View File

@ -68,7 +68,7 @@ int tpm2_pcr_supported(void)
return 1; return 1;
} }
int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr, int tpm2_pcr_read(const char *algo_name, uint32_t pcr_handle, uint8_t *hwpcr,
int len, char **errmsg) int len, char **errmsg)
{ {
FILE *fp; FILE *fp;
@ -76,8 +76,8 @@ int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr,
char cmd[PATH_MAX + 50]; char cmd[PATH_MAX + 50];
int ret; int ret;
sprintf(cmd, "%s -halg %s -ha %d -ns 2> /dev/null", sprintf(cmd, "%s -halg %s -ha %u -ns 2> /dev/null",
path, algo_name, idx); path, algo_name, pcr_handle);
fp = popen(cmd, "r"); fp = popen(cmd, "r");
if (!fp) { if (!fp) {
ret = asprintf(errmsg, "popen failed: %s", strerror(errno)); ret = asprintf(errmsg, "popen failed: %s", strerror(errno));