From e90bf526743b5324d4f2c67d83586db973ccedcd Mon Sep 17 00:00:00 2001 From: Shreya Date: Tue, 15 Jun 2021 15:51:02 +0530 Subject: [PATCH] island: Use `Path().touch()` instead of `os.open()` when securely creating a file on Linux --- monkey/monkey_island/cc/server_utils/file_utils.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/monkey/monkey_island/cc/server_utils/file_utils.py b/monkey/monkey_island/cc/server_utils/file_utils.py index 368f49e95..cb08be10f 100644 --- a/monkey/monkey_island/cc/server_utils/file_utils.py +++ b/monkey/monkey_island/cc/server_utils/file_utils.py @@ -2,6 +2,7 @@ import logging import os import platform import stat +from pathlib import Path LOG = logging.getLogger(__name__) @@ -64,11 +65,8 @@ def create_secure_file(path: str): def _create_secure_file_linux(path: str): try: - flags = ( - os.O_RDWR | os.O_CREAT | os.O_EXCL - ) # read/write, create new, throw error if file exists mode = stat.S_IRWXU # read/write/execute permissions to owner - os.close(os.open(path, flags, mode)) + Path(path).touch(mode=mode, exist_ok=False) except Exception as ex: LOG.error(f'Could not create a file at "{path}": {str(ex)}')