Skip to content

Commit

Permalink
Fix xdg folder permission check for cases when privileges were dropped
Browse files Browse the repository at this point in the history
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 secdev#4618
  • Loading branch information
Civil committed Dec 25, 2024
1 parent cb4a95b commit 83adf2b
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions scapy/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down

0 comments on commit 83adf2b

Please sign in to comment.