From 41923b7337e8bc0879187120f8f9a7474e5c81f0 Mon Sep 17 00:00:00 2001 From: iadgovuser29 <33426478+iadgovuser29@users.noreply.github.com> Date: Tue, 25 May 2021 09:05:21 -0400 Subject: [PATCH] Fixed an issue that caused Process to drop data --- HIRS_ProvisionerTPM2/include/Process.h | 5 ++++ HIRS_ProvisionerTPM2/src/CommandTpm2.cpp | 9 +++++-- HIRS_ProvisionerTPM2/src/Process.cpp | 31 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/HIRS_ProvisionerTPM2/include/Process.h b/HIRS_ProvisionerTPM2/include/Process.h index f769dcc9..53ec0906 100644 --- a/HIRS_ProvisionerTPM2/include/Process.h +++ b/HIRS_ProvisionerTPM2/include/Process.h @@ -44,6 +44,11 @@ class Process { const std::string& sourceFileName, int sourceLineNumber); + static std::string runData(const std::string& executable, + const std::string& arguments, + const std::string& sourceFileName, + int sourceLineNumber); + static bool isRunning(const std::string& executable); }; diff --git a/HIRS_ProvisionerTPM2/src/CommandTpm2.cpp b/HIRS_ProvisionerTPM2/src/CommandTpm2.cpp index 4b72f129..f007a257 100644 --- a/HIRS_ProvisionerTPM2/src/CommandTpm2.cpp +++ b/HIRS_ProvisionerTPM2/src/CommandTpm2.cpp @@ -814,8 +814,13 @@ string CommandTpm2::runTpm2CommandWithRetry(const string& command, string tpmErrorCode; for (int i = 0;; ++i) { try { - return hirs::utils::Process::run(command, args, "CommandTpm2.cpp", - sourceCodeLineNumber); + if (command.compare(kTpm2ToolsNvReadCommand) == 0) { + return hirs::utils::Process::runData(command, args, + "CommandTpm2.cpp", sourceCodeLineNumber); + } else { + return hirs::utils::Process::run(command, args, + "CommandTpm2.cpp", sourceCodeLineNumber); + } } catch (HirsRuntimeException& ex) { tpmErrorCode = Tpm2ToolsOutputParser::parseTpmErrorCode(ex.what()); diff --git a/HIRS_ProvisionerTPM2/src/Process.cpp b/HIRS_ProvisionerTPM2/src/Process.cpp index 92a34372..aaac6efd 100644 --- a/HIRS_ProvisionerTPM2/src/Process.cpp +++ b/HIRS_ProvisionerTPM2/src/Process.cpp @@ -144,6 +144,37 @@ string Process::run(const string& executable, return str; } +/** + * * Static function for calling a process that must succeed or throw + * * a HirsRuntimeException. This function is meant to be used with the + * * RUN_PROCESS_OR_THROW macro in order to capture the source file name + * * and source file line number for use in the exception message. Does not + * * drop any characters. + * * @param executable the executable to be run + * * @param arguments the arguments including options to be passed to the + * * @param sourceFileName source file from which this method was called + * * @param sourceLineNumber line number of source file from which this method + * * was called + * * executable (defaults to empty string) + * */ +string Process::runData(const string& executable, + const string& arguments, + const string& sourceFileName, + int sourceLineNumber) { + stringstream errorStream; + Process p(executable, arguments); + if (p.run(errorStream) != 0) { + errorStream << "\n\n" + << "Process Output: " + << p.getOutputString(); + throw HirsRuntimeException(errorStream.str(), + sourceFileName + ": " + to_string(sourceLineNumber)); + } + + string str = p.getOutputString(); + return str; +} + /** * Static function to check if a specified process is currently running in the * local environment.