Skip to contentSkip to conversation

ima-evm-utils: use tsspcrread to read the TPM 2.0 PCRs

PATCH In review Mimi Zohar 3 participants 4 messages Jul 22, 2019 Keys-trusted

lore
Share & tools
lore

Mimi Zohar

author maintainer
#0 lore raw

PATCH 1/1 ima-evm-utils: use tsspcrread to read the TPM 2.0 PCRs

The kernel does not expose the crypto agile TPM 2.0 PCR banks to userspace like it exposes PCRs for TPM 1.2. As a result, a userspace application is required to read PCRs.

This patch adds tsspcrread support for reading the TPM 2.0 PCRs. tsspcrread is one application included in the ibmtss package.

---
 configure.ac    |  3 +++
 src/Makefile.am |  3 +++
 src/evmctl.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 54 insertions(+), 5 deletions(-)
configure.ac +3 -0
index 9beb4b6c2377..40fea93fef2f 100644
@@ -28,6 +28,8 @@ PKG_CHECK_MODULES(LIBCRYPTO, [libcrypto >= 0.9.8 ])
 AC_SUBST(KERNEL_HEADERS)
 AC_CHECK_HEADER(unistd.h)
 AC_CHECK_HEADERS(openssl/conf.h)
+AC_SEARCH_LIBS(TSS_Transmit, ibmtss, [have_ibmtss=yes], [have_ibmtss=no])
+AM_CONDITIONAL([CONFIG_IBMTSS], [test "x$have_ibmtss" = "xyes"])
 
 AC_CHECK_HEADERS(sys/xattr.h, , [AC_MSG_ERROR([sys/xattr.h header not found. You need the c-library development package.])])
 AC_CHECK_HEADERS(keyutils.h, , [AC_MSG_ERROR([keyutils.h header not found. You need the libkeyutils development package.])])
@@ -71,4 +73,5 @@ echo
 echo	"Configuration:"
 echo	"          debug: $pkg_cv_enable_debug"
 echo	"   openssl-conf: $enable_openssl_conf"
+echo	"         ibmtss: $have_ibmtss"
 echo
src/Makefile.am +3 -0
index 9c037e21dc4f..f0990fb01210 100644
@@ -21,6 +21,9 @@ evmctl_SOURCES = evmctl.c
 evmctl_CPPFLAGS = $(AM_CPPFLAGS) $(LIBCRYPTO_CFLAGS)
 evmctl_LDFLAGS = $(LDFLAGS_READLINE)
 evmctl_LDADD =  $(LIBCRYPTO_LIBS) -lkeyutils libimaevm.la
+if CONFIG_IBMTSS
+evmctl_CFLAGS = -DIBMTSS
+endif
 
 AM_CPPFLAGS = -I$(top_srcdir) -include config.h
 
src/evmctl.c +48 -5

72 lines not on this page · raw message

Signature · 1 line
2.7.5
#1 lore raw

Mimi,

Quoted 27 lines · Mimi Zohar

The kernel does not expose the crypto agile TPM 2.0 PCR banks to userspace like it exposes PCRs for TPM 1.2. As a result, a userspace application is required to read PCRs.

This patch adds tsspcrread support for reading the TPM 2.0 PCRs. tsspcrread is one application included in the ibmtss package.

---
 configure.ac    |  3 +++
 src/Makefile.am |  3 +++
 src/evmctl.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 54 insertions(+), 5 deletions(-)
src/evmctl.c +5 -0
index 9e0926f10404..cbb9397be138 100644
@@ -1402,6 +1400,38 @@ static int tpm_pcr_read(int idx, uint8_t *pcr, int len)
 	return result;
 }
 
+#ifdef IBMTSS
+static int tpm_pcr_read2(int idx, uint8_t *hwpcr, int len, char **errmsg)
+{
+	FILE *fp;
+	char pcr[100];	/* may contain an error */

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1417

Quoted 10 lines
src/evmctl.c +10 -0
+	char cmd[36];
+	int ret = 0;
+	int i;
+
+	sprintf(cmd, "tsspcrread -halg sha1 -ha %d -ns", idx);
+	fp = popen(cmd, "r");
+	if (!fp)
+		return -1;
+
+	fgets(pcr, 100, fp);

Should it be sizeof(pcr)?

I don't know convention of tsspcrread' but maybe fgets() return value should be checked too in case of error of executing tsspcrread' or error inside of `tsspcrread' (like pcr read error).

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1422

Quoted 5 lines
src/evmctl.c +5 -0
+
+	/* pcr might contain an error message */
+	for (i = 0; i < strlen(pcr) - 1 && !ret; i++) {
+		if (isspace(pcr[i]))
+			ret = -1;

Probably strlen(pcr)' should be without - 1'.

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1428

Quoted 6 lines
src/evmctl.c +6 -0
+	}
+
+	if (!ret)
+		hex2bin(hwpcr, pcr, SHA_DIGEST_LENGTH);
+	else
+		*errmsg = pcr;

pcr isn't static nor malloc'ed.

Quoted 35 lines
src/evmctl.c +21 -2
+
+	pclose(fp);
+	return ret;
+}
+#endif
+
 #define TCG_EVENT_NAME_LEN_MAX	255
 
 struct template_entry {
@@ -1658,8 +1688,21 @@ static int ima_measurement(const char *file)
 		log_info("PCRAgg %.2d: ", i);
 		log_dump(pcr[i], SHA_DIGEST_LENGTH);
 
-		if (tpm_pcr_read(i, hwpcr, sizeof(hwpcr)))
-			exit(1);
+		if (tpm_pcr_read(i, hwpcr, sizeof(hwpcr))) {
+#ifdef IBMTSS
+			char *errmsg = NULL;
+
+			err = tpm_pcr_read2(i, hwpcr, sizeof(hwpcr), &errmsg);
+			if (err) {
+				log_info("Failed to read either TPM 1.2 or TPM 2.0 PCRs.\n\t %s", errmsg);
+				exit(-1);
+			}
+#else
+			log_info("Failed to read TPM 1.2 PCRs.\n");
+			exit(-1);
+#endif
+		}
+
 		log_info("HW PCR-%d: ", i);
 		log_dump(hwpcr, sizeof(hwpcr));
 
Signature · 1 line
2.7.5
#2 lore raw

Hi Mirian,

Quoted 77 lines · Vitaly Chikunov

Mimi,

Quoted 27 lines · Mimi Zohar

The kernel does not expose the crypto agile TPM 2.0 PCR banks to userspace like it exposes PCRs for TPM 1.2. As a result, a userspace application is required to read PCRs.

This patch adds tsspcrread support for reading the TPM 2.0 PCRs. tsspcrread is one application included in the ibmtss package.

---
 configure.ac    |  3 +++
 src/Makefile.am |  3 +++
 src/evmctl.c    | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 54 insertions(+), 5 deletions(-)
src/evmctl.c +5 -0
index 9e0926f10404..cbb9397be138 100644
@@ -1402,6 +1400,38 @@ static int tpm_pcr_read(int idx, uint8_t *pcr, int len)
 	return result;
 }
 
+#ifdef IBMTSS
+static int tpm_pcr_read2(int idx, uint8_t *hwpcr, int len, char **errmsg)
+{
+	FILE *fp;
+	char pcr[100];	/* may contain an error */

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1417

Quoted 10 lines
src/evmctl.c +10 -0
+	char cmd[36];
+	int ret = 0;
+	int i;
+
+	sprintf(cmd, "tsspcrread -halg sha1 -ha %d -ns", idx);
+	fp = popen(cmd, "r");
+	if (!fp)
+		return -1;
+
+	fgets(pcr, 100, fp);

Should it be sizeof(pcr)?

I don't know convention of tsspcrread' but maybe fgets() return value should be checked too in case of error of executing tsspcrread' or error inside of `tsspcrread' (like pcr read error).

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1422

Quoted 5 lines
src/evmctl.c +5 -0
+
+	/* pcr might contain an error message */
+	for (i = 0; i < strlen(pcr) - 1 && !ret; i++) {
+		if (isspace(pcr[i]))
+			ret = -1;

Probably strlen(pcr)' should be without - 1'.

src/evmctl.c static int tpm_pcr_read(int idx, uint8_t *pcr, int len) · line 1428

Quoted 6 lines
src/evmctl.c +6 -0
+	}
+
+	if (!ret)
+		hex2bin(hwpcr, pcr, SHA_DIGEST_LENGTH);
+	else
+		*errmsg = pcr;

pcr isn't static nor malloc'ed.

Quoted 13 lines
src/evmctl.c +6 -0
+
+	pclose(fp);
+	return ret;
+}
+#endif
+
 #define TCG_EVENT_NAME_LEN_MAX	255
 
 struct template_entry {
@@ -1658,8 +1688,21 @@ static int ima_measurement(const char *file)
 		log_info("PCRAgg %.2d: ", i);
 		log_dump(pcr[i], SHA_DIGEST_LENGTH);
 
Quoted 10 lines

src/evmctl.c static int ima_measurement(const char *file) · line 1698

Quoted 10 lines
src/evmctl.c +8 -2
-		if (tpm_pcr_read(i, hwpcr, sizeof(hwpcr)))
-			exit(1);
+		if (tpm_pcr_read(i, hwpcr, sizeof(hwpcr))) {
+#ifdef IBMTSS
+			char *errmsg = NULL;
+
+			err = tpm_pcr_read2(i, hwpcr, sizeof(hwpcr), &errmsg);
+			if (err) {
+				log_info("Failed to read either TPM 1.2 or TPM 2.0 PCRs.\n\t %s", errmsg);
+				exit(-1);
                                ^^^^^^^^
Quoted 4 lines

src/evmctl.c static int ima_measurement(const char *file) · line 1702

Quoted 4 lines
src/evmctl.c +4 -0
+			}
+#else
+			log_info("Failed to read TPM 1.2 PCRs.\n");
+			exit(-1);
                        ^^^^^^^^
Quoted 8 lines
Quoted 8 lines
src/evmctl.c +3 -0
+#endif
+		}
+
 		log_info("HW PCR-%d: ", i);
 		log_dump(hwpcr, sizeof(hwpcr));
 
Signature · 1 line
2.7.5

Besides to what Vitaly have pointed...

exit(1) has been the standard exit code in case of failure, wouldn't that be better to keep it instead of change it to -1? (points highlighted above)

author maintainer
#3 lore raw

Vitaly, Bruno,

Quoted 4 lines · Bruno E. O. Meneguele
Quoted 3 lines · Vitaly Chikunov

Mimi,

On Mon, Jul 22, 2019 at 09:32:48AM -0400, Mimi Zohar wrote:

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

The kernel does not expose the crypto agile TPM 2.0 PCR banks to userspace like it exposes PCRs for TPM 1.2. As a result, a userspace application is required to read PCRs.

This patch adds tsspcrread support for reading the TPM 2.0 PCRs. tsspcrread is one application included in the ibmtss package.

Thank you for reviewing this patch so quickly!  Your comments are much appreciated and will be addressed in the version.

Mimi