Skip to contentSkip to conversation

Check for tsspcrread in runtime

PATCH In review Petr Vorel 3 participants 8 messages Jul 15, 2020 – Jul 16, 2020 Keys-trusted

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v2 1/1 Check for tsspcrread in runtime

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

---
Hi Mimi,

changes v1->v2:
* Log used binary/library.
* Drop using **errmsg in tpm2_pcr_supported() use log_info() and log_debug() instead.
* Formatting changes in includes

Feel free to amend this patch to suit your needs.

Kind regards,
Petr

 configure.ac         |  7 -----
 src/Makefile.am      |  4 ---
 src/pcr_none.c       | 52 ------------------------------
 src/pcr_tss.c        | 19 ++++++++---
 src/pcr_tsspcrread.c | 21 +++++++++++--
 src/utils.c          | 75 ++++++++++++++++++++++++++++++++++++++++++++
 src/utils.h          |  1 +
 7 files changed, 108 insertions(+), 71 deletions(-)
 delete mode 100644 src/pcr_none.c
configure.ac +0 -7
index f246182..e7df7cd 100644
@@ -30,12 +30,6 @@ AC_SUBST(KERNEL_HEADERS)
 AC_CHECK_HEADER(unistd.h)
 AC_CHECK_HEADERS(openssl/conf.h)
 
-AC_CHECK_PROG(TSSPCRREAD, [tsspcrread], yes, no)
-if test "x$TSSPCRREAD" = "xyes"; then
-	AC_DEFINE(HAVE_TSSPCRREAD, 1, [Define to 1 if you have tsspcrread binary installed])
-fi
-AM_CONDITIONAL([USE_PCRTSSPCRREAD], [test "x$TSSPCRREAD" = "xyes"])
-
 AC_CHECK_LIB([tss2-esys], [Esys_PCR_Read])
 AC_CHECK_LIB([tss2-rc], [Tss2_RC_Decode])
 AM_CONDITIONAL([USE_PCRTSS], [test "x$ac_cv_lib_tss2_esys_Esys_PCR_Read" = "xyes"])
@@ -83,7 +77,6 @@ echo
 echo	"Configuration:"
 echo	"          debug: $pkg_cv_enable_debug"
 echo	"   openssl-conf: $enable_openssl_conf"
-echo	"     tsspcrread: $TSSPCRREAD"
 echo	"      tss2-esys: $ac_cv_lib_tss2_esys_Esys_PCR_Read"
 echo	" tss2-rc-decode: $ac_cv_lib_tss2_rc_Tss2_RC_Decode"
 echo
src/Makefile.am +0 -4
index 9bbff50..ba9719b 100644
@@ -25,11 +25,7 @@ evmctl_LDADD =  $(LIBCRYPTO_LIBS) -lkeyutils libimaevm.la
 if USE_PCRTSS
 evmctl_SOURCES += pcr_tss.c
 else
-if USE_PCRTSSPCRREAD
 evmctl_SOURCES += pcr_tsspcrread.c
-else
-evmctl_SOURCES += pcr_none.c
-endif
 endif
 
 AM_CPPFLAGS = -I$(top_srcdir) -include config.h
src/pcr_none.c deleted +0 -52

52 lines not on this page · raw message

src/pcr_tss.c +14 -5
index da7be2e..a58568f 100644
@@ -44,15 +44,24 @@
 #include <openssl/sha.h>
 
 #ifdef HAVE_LIBTSS2_ESYS
-#include <tss2/tss2_esys.h>
+# include <tss2/tss2_esys.h>
 
-#ifdef HAVE_LIBTSS2_RC
-#include <tss2/tss2_rc.h>
-#endif
-#endif
+# ifdef HAVE_LIBTSS2_RC
+#  include <tss2/tss2_rc.h>
+#  define LIB "tss2-rc-decode"
+# else
+#  define LIB "tss2-esys"
+# endif
+
+#endif /* HAVE_LIBTSS2_ESYS */
+
+#include "imaevm.h"
 
 int tpm2_pcr_supported(void)
 {
+	if (imaevm_params.verbose > LOG_INFO)
+		log_info("Using %s to read PCRs.\n", LIB);
+
 	return 1;
 }
 
src/pcr_tsspcrread.c +18 -3
index 9c58dcb..b840d49 100644
@@ -39,6 +39,7 @@
  */
 
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
@@ -46,9 +47,23 @@
 #include <openssl/sha.h>
 
 #include "utils.h"
+#include "imaevm.h"
+
+#define CMD "tsspcrread"
+
+static char path[PATH_MAX];
 
 int tpm2_pcr_supported(void)
 {
+	if (imaevm_params.verbose > LOG_INFO)
+		log_info("Using %s to read PCRs.\n", CMD);
+
+	if (get_cmd_path(CMD, path, sizeof(path))) {
+		log_debug("Couldn't find '%s' in $PATH", CMD);
+		return 0;
+	}
+
+	log_debug("Found '%s' in $PATH", CMD);
 	return 1;
 }
 
@@ -57,11 +72,11 @@ int tpm2_pcr_read(const char *algo_name, int idx, uint8_t *hwpcr,
 {
 	FILE *fp;
 	char pcr[100];	/* may contain an error */
-	char cmd[50];
+	char cmd[PATH_MAX + 50];
 	int ret;
 
-	sprintf(cmd, "tsspcrread -halg %s -ha %d -ns 2> /dev/null",
-		algo_name, idx);
+	sprintf(cmd, "%s -halg %s -ha %d -ns 2> /dev/null",
+		path, algo_name, idx);
 	fp = popen(cmd, "r");
 	if (!fp) {
 		ret = asprintf(errmsg, "popen failed: %s", strerror(errno));
src/utils.c +75 -0

82 lines not on this page · raw message

src/utils.h +1 -0
index 7c0ce15..9ea179f 100644
@@ -1,5 +1,6 @@
 #include <ctype.h>
 #include <sys/types.h>
 
+int get_cmd_path(const char *prog_name, char *buf, size_t buf_len);
 int hex_to_bin(char ch);
 int hex2bin(void *dst, const char *src, size_t count);
Signature · 1 line
2.27.0
author
#1 lore raw

Hi Mimi,

Quoted 7 lines

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

BTW these parts holds Cyril's copyright. If you ever add copyright to src/utils.[ch], please add it there.
Copyright (C) 2010 Cyril Hrubis <[email protected]>

Kind regards,
Petr

maintainer
#2 lore raw
Quoted 5 lines · Petr Vorel

Hi Mimi,

Quoted 2 lines

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

Quoted 7 lines
Quoted 4 lines

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

BTW these parts holds Cyril's copyright. If you ever add copyright to src/utils.[ch], please add it there.
Copyright (C) 2010 Cyril Hrubis <[email protected]>

Are you Ok with the following?

src/utils.c +7 -0
index 416a88c6dbe4..a6ae08fa4d84 100644
@@ -1,3 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * utils: set of common functions
+ *
+ * Copyright (C) 2020 Vitaly Chikunov <[email protected]>
+ * Copyright (C) 2010 Cyril Hrubis <[email protected]>
+ */
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>

Mimi

#3 lore raw

Mimi,

src/utils.c

Quoted 17 lines · Mimi Zohar
Quoted 5 lines · Petr Vorel

Hi Mimi,

Quoted 2 lines

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

Quoted 7 lines
Quoted 4 lines

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

BTW these parts holds Cyril's copyright. If you ever add copyright to src/utils.[ch], please add it there.
Copyright (C) 2010 Cyril Hrubis <[email protected]>

Are you Ok with the following?

src/utils.c +0 -0

I don't remember me authoring anything in src/utils.c

Quoted 16 lines

index 416a88c6dbe4..a6ae08fa4d84 100644

src/utils.c +7 -0
@@ -1,3 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * utils: set of common functions
+ *
+ * Copyright (C) 2020 Vitaly Chikunov <[email protected]>
+ * Copyright (C) 2010 Cyril Hrubis <[email protected]>
+ */
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>

Mimi

maintainer
#4 lore raw

[Cc'ing Patrick Uiterwijk]

Quoted 17 lines · Vitaly Chikunov

Mimi,

Quoted 14 lines · Mimi Zohar
Quoted 12 lines · Petr Vorel

Hi Mimi,

Quoted 7 lines

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

BTW these parts holds Cyril's copyright. If you ever add copyright to src/utils.[ch], please add it there.
Copyright (C) 2010 Cyril Hrubis <[email protected]>

Quoted 5 lines

src/utils.c

Quoted 3 lines

Are you Ok with the following?

src/utils.c +0 -0

I don't remember me authoring anything in src/utils.c

Thank you for catching that!  The new file came from Patrick.

Quoted 9 lines

src/utils.c line 5

Quoted 9 lines

index 416a88c6dbe4..a6ae08fa4d84 100644

src/utils.c +5 -0
@@ -1,3 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * utils: set of common functions
+ *
+ * Copyright (C) 2020 Vitaly Chikunov <[email protected]>

Copyright (C) 2020 Patrick Uiterwijk <[email protected]>

Quoted 5 lines
Quoted 5 lines
src/utils.c +2 -0
+ * Copyright (C) 2010 Cyril Hrubis <[email protected]>
+ */
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
author
#5 lore raw
In reply to Mimi Zohar
Quoted 9 lines · Petr Vorel
Quoted 7 lines

Hi Mimi,

Quoted 5 lines

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

file_exist(), tst_get_path() and MIN() taken from LTP project.

BTW these parts holds Cyril's copyright. If you ever add copyright to src/utils.[ch], please add it there.
Copyright (C) 2010 Cyril Hrubis <[email protected]>

Are you Ok with the following?

Yes, thanks!

Kind regards,
Petr

Quoted 17 lines
src/utils.c +7 -0
index 416a88c6dbe4..a6ae08fa4d84 100644
@@ -1,3 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * utils: set of common functions
+ *
+ * Copyright (C) 2020 Vitaly Chikunov <[email protected]>
+ * Copyright (C) 2010 Cyril Hrubis <[email protected]>
+ */
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>

Mimi

maintainer
#6 lore raw
In reply to Petr Vorel
Quoted 12 lines · Petr Vorel

instead of checking in build time as it's runtime dependency. Also log when tsspcrread not found to make debugging easier.

We search for tsspcrread unless there is tss2-esys with Esys_PCR_Read(), thus pcr_none.c was dropped as unneeded.

file_exist(), tst_get_path() and MIN() taken from LTP project.

---
Hi Mimi,

Quoted 7 lines

changes v1->v2:
* Log used binary/library.
* Drop using **errmsg in tpm2_pcr_supported() use log_info() and log_debug() instead.
* Formatting changes in includes

Feel free to amend this patch to suit your needs.

log_info() is not displaying the method of reading the PCRs.  To resolve this add the necessary USE_FPRINTF definitions.

Mimi

author
#7 lore raw

Hi Mimi,

...

Quoted 10 lines
Quoted 7 lines

changes v1->v2:
* Log used binary/library.
* Drop using **errmsg in tpm2_pcr_supported() use log_info() and log_debug() instead.
* Formatting changes in includes

Feel free to amend this patch to suit your needs.

log_info() is not displaying the method of reading the PCRs.  To resolve this add the necessary USE_FPRINTF definitions.

Thanks for fixing this!

Kind regards,
Petr