Skip to content

Commit 8fef083

Browse files
authored
Fix xdg folder permission check for cases when privileges were dropped (#4619)
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
1 parent e16a18e commit 8fef083

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

scapy/main.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,18 @@
6868
def _probe_xdg_folder(var, default, *cf):
6969
# type: (str, str, *str) -> Optional[pathlib.Path]
7070
path = pathlib.Path(os.environ.get(var, default))
71-
if not path.exists():
72-
# ~ folder doesn't exist. Create according to spec
73-
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
74-
# "If, when attempting to write a file, the destination directory is
75-
# non-existent an attempt should be made to create it with permission 0700."
76-
try:
71+
try:
72+
if not path.exists():
73+
# ~ folder doesn't exist. Create according to spec
74+
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
75+
# "If, when attempting to write a file, the destination directory is
76+
# non-existent an attempt should be made to create it with permission 0700."
7777
path.mkdir(mode=0o700, exist_ok=True)
78-
except Exception:
79-
# There is a gazillion ways this can fail. Most notably,
80-
# a read-only fs.
81-
return None
78+
except Exception:
79+
# There is a gazillion ways this can fail. Most notably, a read-only fs or no
80+
# permissions to even check for folder to exist (e.x. privileges were dropped
81+
# before scapy was started).
82+
return None
8283
return path.joinpath(*cf).resolve()
8384

8485

0 commit comments

Comments
 (0)