From 44501877cfca75e655b24e0d08b9a957c0463d32 Mon Sep 17 00:00:00 2001 From: Ruoyu Ying Date: Mon, 22 Apr 2024 13:57:15 +0800 Subject: [PATCH] vmsdk: add exception handling in get_cc_report * add exception handling for python impl of get_cc_report Signed-off-by: Ruoyu Ying --- src/python/cctrusted_vm/cvm.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/python/cctrusted_vm/cvm.py b/src/python/cctrusted_vm/cvm.py index 4334923..50d8aa1 100644 --- a/src/python/cctrusted_vm/cvm.py +++ b/src/python/cctrusted_vm/cvm.py @@ -122,6 +122,7 @@ def process_eventlog(self) -> bool: raise NotImplementedError("Should be implemented by inherited class") @abstractmethod + # pylint: disable-next=R0911 def get_cc_report(self, nonce: bytearray, data: bytearray, extraArgs) -> CcReport: """Get the CcReport (i.e. quote) for given nonce and data. @@ -180,9 +181,16 @@ def get_cc_report(self, nonce: bytearray, data: bytearray, extraArgs) -> CcRepor with open(os.path.join(tempdir, "inblob"), 'wb') as inblob_file: inblob_file.write(input_data) - # Read the output of report - with open(os.path.join(tempdir, "outblob"), 'rb') as outblob_file: - td_report = outblob_file.read() + # Read the output of report and prevent case of resource busy + try: + with open(os.path.join(tempdir, "outblob"), 'rb') as outblob_file: + td_report = outblob_file.read() + except OSError: + LOG.error("Read outblob failed with OSError") + return None + except: + LOG.error("Error in opening outblob file.") + return None # Read provider info with open(os.path.join(tempdir, "provider"), 'r', encoding='utf-8') as provider_file: @@ -192,6 +200,10 @@ def get_cc_report(self, nonce: bytearray, data: bytearray, extraArgs) -> CcRepor with open(os.path.join(tempdir, "generation"), 'r', encoding='utf-8') \ as generation_file: generation = generation_file.read() + # Check if the outblob has been corrupted during file open + if int(generation) > 1: + LOG.error("Found corrupted generation. Skipping attestation report fetching...") + return None if os.path.exists(os.path.join(tempdir, "auxblob")): with open(os.path.join(tempdir, "auxblob"), 'rb') as auxblob_file: