From 83adf2bf773a0b75d71d78c970859b0a4c8b393c Mon Sep 17 00:00:00 2001 From: Vladimir Smirnov Date: Wed, 25 Dec 2024 23:58:02 +0100 Subject: [PATCH] Fix xdg folder permission check for cases when privileges were dropped If privileges for scapy were dropped, but username remain unchanged, path.exist() would trigger an exception. Fix that by moving whole if statement under try-except. Fixes #4618 --- scapy/main.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/scapy/main.py b/scapy/main.py index c5ad0c74d99..414bd46c1a2 100644 --- a/scapy/main.py +++ b/scapy/main.py @@ -68,17 +68,18 @@ def _probe_xdg_folder(var, default, *cf): # type: (str, str, *str) -> Optional[pathlib.Path] path = pathlib.Path(os.environ.get(var, default)) - if not path.exists(): - # ~ folder doesn't exist. Create according to spec - # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html - # "If, when attempting to write a file, the destination directory is - # non-existent an attempt should be made to create it with permission 0700." - try: + try: + if not path.exists(): + # ~ folder doesn't exist. Create according to spec + # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html + # "If, when attempting to write a file, the destination directory is + # non-existent an attempt should be made to create it with permission 0700." path.mkdir(mode=0o700, exist_ok=True) - except Exception: - # There is a gazillion ways this can fail. Most notably, - # a read-only fs. - return None + except Exception: + # There is a gazillion ways this can fail. Most notably, a read-only fs or no + # permissions to even check for folder to exist (e.x. privileges were dropped + # before scapy was started). + return None return path.joinpath(*cf).resolve()