From c512b138e43a1f2914d9e91bd68f074efb9ce35c Mon Sep 17 00:00:00 2001 From: Renan Rodrigo Date: Tue, 17 Oct 2023 18:57:45 -0300 Subject: [PATCH] files: change implementation of write to avoid race conditions try/excep instead of checking for existence with an if is safer Signed-off-by: Renan Rodrigo --- uaclient/files/files.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/uaclient/files/files.py b/uaclient/files/files.py index 62353117bc..a35c065f5a 100644 --- a/uaclient/files/files.py +++ b/uaclient/files/files.py @@ -41,11 +41,15 @@ def write(self, content: str): if self.is_private else defaults.WORLD_READABLE_MODE ) - if not os.path.exists(self._directory): + # try/except-ing here avoids race conditions the best + try: if os.path.basename(self._directory) == defaults.PRIVATE_SUBDIR: os.makedirs(self._directory, mode=0o700) else: os.makedirs(self._directory) + except OSError: + pass + system.write_file(self.path, content, file_mode) def read(self) -> Optional[str]: