Oops, the newest version of the patch was sent to the wrong address.
Newer version attached.

Wrt 100: If there's too much output, it's probably garbage padded. 100
bytes should be enough to show any error message that may be in the
beginning of the output. As to whether it should be 80 or 500: It's
going to be wrong no matter what it is.

On 18 November 2014 16:41, Hon Ching Lo <[email protected]> wrote:
>>+ char buf[4096] = {0};
>>+ int olderr;
>>+
>>+ /* Disable logging to of "Bad Mode" during this call. If we're on a 1.1
>> TPM, it'd throw an >error
>>+ * Save data sent to stderr in case it was any other type of error. */
>>+ olderr = capture_stderr(buf, sizeof(buf));
>
> capture_stderr() was defined to have no input arguments, which you passed in
> the buf and sizeof(buf) there.  Did you forget to get rid of them?
>
> Another question is the handling of int overflow.  Just wondering what's the
> reason behind the chosen number '100'?
>
>
> Vicky
>
>
>
>
>
>
>
>
>
>
> On Wed, Aug 20, 2014 at 10:32 AM, Thomas Habets <[email protected]> wrote:
>>
>> The library writes logs to stderr. Instead of rewriting the logging
>> framework to be able to redirect, this will redirect stderr
>> temporarily, and will swallow the error only if it's the type of error
>> we want to swallow.
>>
>> Attached is a patch with a couple of more comments.
>>
>> On 20 August 2014 13:14, Ken Goldman <[email protected]> wrote:
>> > There's evidently something clever going on with stderr.  My sense is
>> > that it's something you've used before, so it's familiar to you.  How
>> > about some comments on the clever parts, for future maintainers?
>> >
>> > On 8/14/2014 4:42 AM, Thomas Habets wrote:
>> >> 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.
>> >
>> >
>> >
>> >
>> > ------------------------------------------------------------------------------
>> > Slashdot TV.
>> > Video for Nerds.  Stuff that matters.
>> > http://tv.slashdot.org/
>> > _______________________________________________
>> > TrouSerS-users mailing list
>> > [email protected]
>> > https://lists.sourceforge.net/lists/listinfo/trousers-users
>>
>>
>>
>> --
>> 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;
>>
>>
>> ------------------------------------------------------------------------------
>> Slashdot TV.
>> Video for Nerds.  Stuff that matters.
>> http://tv.slashdot.org/
>> _______________________________________________
>> TrouSerS-users mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/trousers-users
>>
>



-- 
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 6e96893da9a32a5353dce120bc07bb5edeaae162
Author: Thomas Habets <[email protected]>
Date:   Wed Aug 20 15:27:35 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..01358bb 100644
--- a/src/tpm_mgmt/tpm_version.c
+++ b/src/tpm_mgmt/tpm_version.c
@@ -18,14 +18,99 @@
  * 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;
 
+// Redirect all stderr output to a temporary file.
+// Returns a cloned version of the original stderr, for use with end_capture_stderr().
+int capture_stderr()
+{
+  // Save a copy of stderr.
+  int olderr = dup(STDERR_FILENO);
+  if (olderr < 0) {
+    perror("dup(STDERR_FILENO)");
+    return -1;
+  }
+
+  // Open file that will become new stderr.
+  FILE* f = tmpfile();
+  if (f == NULL) {
+    close(olderr);
+    perror("tmpfile()");
+    return -1;
+  }
+
+  // Override stderr with temp file.
+  if (0 > dup2(fileno(f), STDERR_FILENO)) {
+    fclose(f);
+    close(olderr);
+    perror("dup2(, STDERR_FILENO)");
+    return -1;
+  }
+
+  // Old handle for temp file no longer needed.
+  fclose(f);
+  return olderr;
+}
+
+// Restore stderr and return a newly allocated buffer with the captured stderr output.
+// Caller frees the returned buffer.
+char* end_capture_stderr(int olderr)
+{
+  char* buf = NULL;
+  struct stat st;
+  int memsize;
+
+  if (olderr == -1) {
+    // capture_stderr() must have failed. Nothing to restore.
+    return strdup("");
+  }
+
+  // Find out how much to read.
+  if (0 > fstat(STDERR_FILENO, &st)) {
+    perror("fstat()");
+    goto errout;
+  }
+  memsize = st.st_size + 1;
+  if (memsize <= st.st_size) {
+    // Something fishy is going on. Fall back to getting 100 bytes (arbitrary).
+    perror("int overflow");
+    memsize = 100;
+    st.st_size = memsize - 1;
+  }
+  if (!(buf = malloc(memsize))) {
+    perror("malloc()");
+    goto errout;
+  }
+
+  // Read file content.
+  if (0 > lseek(STDERR_FILENO, 0, SEEK_SET)) {
+    perror("lseek()");
+    goto errout;
+  }
+  if (st.st_size != read(STDERR_FILENO, buf, st.st_size)) {
+    perror("read()");
+  }
+
+  // Restore stderr.
+ errout:
+  if (0 > dup2(olderr, STDERR_FILENO)) {
+    perror("dup2()");
+    return buf;
+  }
+  if (0 > close(olderr)) {
+    perror("close()");
+  }
+  return buf;
+}
+
 int cmdVersion(const char *a_szCmd)
 {
 	TSS_HTPM hTpm;
@@ -43,26 +128,39 @@ 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)
-				goto print_cap_version;
-			else
-				goto out_close;
+		char *errbuf = NULL; // Buffer containing what was sent to stderr during getCapability.
+
+		/* Disable logging to of "Bad Mode" during this call. If we're on a 1.1 TPM, it'd throw an error.
+		 * Save data sent to stderr in case it was any other type of error. */
+		{
+		        int olderr;   // Saved copy of stderr.
+
+			// Redirect stderr to tempfile.
+			olderr = capture_stderr();
+
+			// Get capabilities. Stderr output is captured.
+			uiResult = getCapability(hTpm, TSS_TPMCAP_VERSION_VAL, 0, NULL, &uiResultLen,
+						 &pResult);
+
+			// Restore output to stderr.
+			errbuf = end_capture_stderr(olderr);
+		}
+
+		// Don't print errors due to "Bad Mode", that just means we're on a 1.1 TPM.
+		if (uiResult == TPM_E_BAD_MODE) {
+		        free(errbuf);
+			goto print_cap_version;
+		}
+		fprintf(stderr, "%s", errbuf);
+		free(errbuf);
+		if (uiResult != TSS_SUCCESS) {
+			goto out_close;
 		}
-		iLogLevel = tmpLogLevel;
 
 		offset = 0;
 		if ((uiResult = unloadVersionInfo(&offset, pResult, &versionInfo))) {
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=157005751&iu=/4140/ostg.clktrk
_______________________________________________
TrouSerS-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/trousers-users

Reply via email to