My TPM chip got out of whack (I'm going to try a reboot), but in the
mean time here's a patch to make tpm_version not fail silently.

-- 
typedef struct me_s {
 char name[]      = { "Thomas Habets" };
 char email[]     = { "[email protected]" };
 char kernel[]    = { "Linux" };
 char *pgpKey[]   = { "http://www.habets.pp.se/pubkey.txt"; };
 char pgp[] = { "A8A3 D1DD 4AE0 8467 7FDE  0945 286A E90A AD48 E854" };
 char coolcmd[]   = { "echo '. ./_&. ./_'>_;. ./_" };
} me_t;
commit 6441c7aaa5c3913c9dc158c7631afd7b9d0ad1e4
Author: Thomas Habets <[email protected]>
Date:   Thu Aug 14 09:38:38 2014 +0100

    tpm_version: Don't suppress communication errors with TPM chip.
    
    Signed-off-by: Thomas Habets <[email protected]>

diff --git a/src/tpm_mgmt/tpm_version.c b/src/tpm_mgmt/tpm_version.c
index 2378dba..6883c65 100644
--- a/src/tpm_mgmt/tpm_version.c
+++ b/src/tpm_mgmt/tpm_version.c
@@ -18,14 +18,73 @@
  * along with this program; if not, a copy can be viewed at
  * http://www.opensource.org/licenses/cpl1.0.php.
  */
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <stdlib.h>		//for def. exit
+#include <unistd.h>
 #include "tpm_tspi.h"
 
 
 TSS_HCONTEXT hContext = 0;
 
+int capture_stderr()
+{
+  int olderr = dup(STDERR_FILENO);
+  if (olderr < 0) {
+    perror("dup(STDERR_FILENO)");
+    return -1;
+  }
+  FILE* f = tmpfile();
+  if (f == NULL) {
+    perror("tmpfile()");
+    return -1;
+  }
+  if (0 > dup2(fileno(f), STDERR_FILENO)) {
+    perror("dup2(, STDERR_FILENO)");
+    return -1;
+  }
+  fclose(f);
+  return olderr;
+}
+
+char* end_capture_stderr(int olderr)
+{
+  char* buf = NULL;
+  struct stat st;
+  int memsize;
+  if (0 > fstat(STDERR_FILENO, &st)) {
+    perror("fstat()");
+    goto errout;
+  }
+  if (0 > lseek(STDERR_FILENO, 0, SEEK_SET)) {
+    perror("lseek()");
+    goto errout;
+  }
+  memsize = st.st_size + 1;
+  if (memsize <= st.st_size) {
+    perror("int overflow");
+    memsize = 100;
+    st.st_size = memsize - 1;
+  }
+  if (!(buf = malloc(memsize))) {
+    perror("malloc()");
+    goto errout;
+  }
+  if (st.st_size != read(STDERR_FILENO, buf, st.st_size)) {
+    perror("read()");
+  }
+ errout:
+  if (0 > dup2(olderr, STDERR_FILENO)) {
+    perror("dup2()");
+    return NULL;
+  }
+  if (0 > close(olderr)) {
+    perror("close()");
+  }
+  return buf;
+}
+
 int cmdVersion(const char *a_szCmd)
 {
 	TSS_HTPM hTpm;
@@ -43,26 +102,33 @@ int cmdVersion(const char *a_szCmd)
 
 	if (contextGetTpm(hContext, &hTpm) != TSS_SUCCESS)
 		goto out_close;
-
 #ifdef TSS_LIB_IS_12
 	{
 		UINT64 offset;
 		TSS_RESULT uiResult;
 		TPM_CAP_VERSION_INFO versionInfo;
-		int tmpLogLevel = iLogLevel;
-
-		/* disable logging during this call. If we're on a 1.1 TPM, it'd throw an error */
-		iLogLevel = LOG_LEVEL_NONE;
-
-		if ((uiResult = getCapability(hTpm, TSS_TPMCAP_VERSION_VAL, 0, NULL, &uiResultLen,
-					      &pResult)) != TSS_SUCCESS) {
-			iLogLevel = tmpLogLevel;
-			if (uiResult == TPM_E_BAD_MODE)
+		char buf[4096] = {0};
+		int olderr;
+
+		/* Disable logging of "Bad Mode" during this call. If we're on a 1.1 TPM, it'd throw an error */
+		olderr = capture_stderr(buf, sizeof(buf));
+		uiResult = getCapability(hTpm, TSS_TPMCAP_VERSION_VAL, 0, NULL, &uiResultLen,
+					 &pResult);
+		char* errbuf = end_capture_stderr(olderr);
+
+		if (uiResult != TSS_SUCCESS) {
+			if (uiResult == TPM_E_BAD_MODE) {
+			        free(errbuf);
 				goto print_cap_version;
-			else
+			} else {
+			        if (iLogLevel >= LOG_LEVEL_ERROR) {
+				        fprintf(stderr, "%s", errbuf);
+				}
+			        free(errbuf);
 				goto out_close;
+			}
 		}
-		iLogLevel = tmpLogLevel;
+		free(errbuf);
 
 		offset = 0;
 		if ((uiResult = unloadVersionInfo(&offset, pResult, &versionInfo))) {
------------------------------------------------------------------------------
_______________________________________________
TrouSerS-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/trousers-users

Reply via email to