Skip to content

Commit 5017826

Browse files
authored
Merge commit from fork
Harden path-traversal / file-I/O sandbox: write-side symlink TOCTOU + shared-temp squat + living audit (GHSA-8mgp umbrella; f794, p4gq/CVE-2026-54293, q5h6/CVE-2026-12243)
2 parents 722778f + 40d0f06 commit 5017826

5 files changed

Lines changed: 515 additions & 29 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ jobs:
198198
# drop it; also collected by "Run pytest".
199199
pytest -rsx nltk/test/unit/test_quadratic_dos.py nltk/test/unit/test_distance.py
200200
201+
- name: Run path-traversal / file-I/O security regression tests
202+
shell: bash
203+
env:
204+
PYTHONSAFEPATH: "1"
205+
run: |
206+
# Security regression suite for the path-traversal / file-I/O sandbox
207+
# cluster (GHSA-8mgp-746c-j5xp umbrella: CVE-2026-54293 / GHSA-p4gq and
208+
# CVE-2026-12243 / GHSA-q5h6 encoded-traversal in nltk.data; GHSA-f794
209+
# pathsec symlink/hardlink TOCTOU -- read and the newly-closed write
210+
# side; the perceptron shared-temp squat. CWE-22/59/377). Own fast,
211+
# early step so a change to the main pytest invocation can't silently
212+
# drop it; also collected by "Run pytest".
213+
pytest -rsx nltk/test/unit/test_path_traversal_security.py
214+
201215
- name: Run pytest
202216
shell: bash
203217
env:

‎nltk/corpus/reader/bcp47.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import re
99
from warnings import warn
10-
from xml.etree import ElementTree as et
1110

1211
from nltk.corpus.reader import CorpusReader
12+
from nltk.xmlsec import parse as safe_parse
1313

1414

1515
class BCP47CorpusReader(CorpusReader):
@@ -37,7 +37,7 @@ def __init__(self, root, fileids):
3737
self.db = self.data_dict(fp.read().split("%%\n"))
3838
with self.open("cldr/common-subdivisions-en.xml") as fp:
3939
self.subdiv = self.subdiv_dict(
40-
et.parse(fp).iterfind("localeDisplayNames/subdivisions/subdivision")
40+
safe_parse(fp).iterfind("localeDisplayNames/subdivisions/subdivision")
4141
)
4242
self.morphology()
4343

‎nltk/pathsec.py‎

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -648,31 +648,66 @@ def _fd_realpath(fd):
648648
return None
649649

650650

651-
def _hardened_read_open(raw_path, mode, context, required_root, **kwargs):
652-
"""Open ``raw_path`` for reading, closing the symlink-swap TOCTOU and the
653-
hardlink escape that a path-only ``validate_path`` cannot.
651+
def _os_open_flags(mode):
652+
"""Translate a :func:`builtins.open` *mode* string into ``os.open()`` flags.
653+
654+
Mirrors the read/write/create/append/truncate intent of *mode* so the
655+
hardened opener can add ``O_NOFOLLOW`` on top of exactly what the caller
656+
asked for. POSIX only.
657+
"""
658+
chars = set(mode)
659+
if "x" in chars:
660+
creat = os.O_CREAT | os.O_EXCL
661+
elif "w" in chars:
662+
creat = os.O_CREAT | os.O_TRUNC
663+
elif "a" in chars:
664+
creat = os.O_CREAT | os.O_APPEND
665+
else: # "r" -- open existing, no create
666+
creat = 0
667+
if "+" in chars:
668+
access = os.O_RDWR
669+
elif chars & {"w", "a", "x"}:
670+
access = os.O_WRONLY
671+
else:
672+
access = os.O_RDONLY
673+
return access | creat
674+
675+
676+
def _hardened_open(raw_path, mode, context, required_root, **kwargs):
677+
"""Open ``raw_path`` for read *or* write, closing the symlink-swap TOCTOU and
678+
the hardlink escape that a path-only ``validate_path`` cannot.
654679
655680
1. Open ``raw_path`` with ``O_NOFOLLOW`` so its final component is never
656681
followed as a symlink. Unlike opening a re-resolved path, this is atomic:
657682
the kernel either opens the real inode at that path or fails, so a symlink
658683
swapped in after the caller's ``validate_path`` cannot redirect the open
659-
(TOCTOU). Corpora contain no symlinked files, so rejecting a final-
660-
component symlink outright is safe and fail-closed.
684+
(TOCTOU). On the write side this stops an attacker who plants/swaps
685+
``<root>/pkg`` -> ``/etc/...`` between the check and a ``retrieve()`` /
686+
model write from redirecting the write outside the root -- the symmetric
687+
counterpart of the read-side content leak (CWE-59, GHSA-f794 class).
688+
Corpora/data files are never symlinks, so refusing a final-component
689+
symlink outright is fail-closed.
661690
2. ``fstat`` the descriptor and refuse ``st_nlink > 1``: a hardlink is another
662691
name for the same inode with no symlink to resolve, so an in-root hardlink
663-
to an outside inode would otherwise leak its contents (CWE-59, GHSA-f794
664-
class). Multiply-linked corpus files are not expected.
692+
to an outside inode would otherwise be read or written through.
665693
3. Re-validate the *opened descriptor's* real path (from the kernel, not by
666694
re-resolving the string) so a swapped intermediate directory symlink that
667695
redirected the open to an outside inode is still caught.
668696
669-
POSIX only; callers fall back to :func:`builtins.open` elsewhere.
697+
A newly created file is given ``0600`` permissions so a write into a shared
698+
temp dir is not left group-/world-readable (CWE-377/378). POSIX only;
699+
callers fall back to :func:`builtins.open` elsewhere.
670700
"""
671701
import errno
672702

673-
flags = os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0) | getattr(os, "O_CLOEXEC", 0)
703+
flags = (
704+
_os_open_flags(mode)
705+
| getattr(os, "O_NOFOLLOW", 0)
706+
| getattr(os, "O_CLOEXEC", 0)
707+
)
674708
try:
675-
fd = os.open(raw_path, flags)
709+
# 0o600 is only consulted when O_CREAT is in flags (write/append/x modes).
710+
fd = os.open(raw_path, flags, 0o600)
676711
except OSError as e:
677712
if e.errno in (errno.ELOOP, errno.EMLINK):
678713
raise PermissionError(
@@ -699,7 +734,19 @@ def _hardened_read_open(raw_path, mode, context, required_root, **kwargs):
699734
except BaseException:
700735
os.close(fd)
701736
raise
702-
return os.fdopen(fd, mode, **kwargs)
737+
try:
738+
return os.fdopen(fd, mode, **kwargs)
739+
except BaseException:
740+
# os.open() accepts some mode strings that os.fdopen() rejects (e.g. an
741+
# empty or malformed mode); without this the descriptor would leak and
742+
# repeated calls could exhaust the fd table (DoS).
743+
os.close(fd)
744+
raise
745+
746+
747+
# Back-compat alias: the opener now handles write modes too, but external
748+
# callers/tests may still import the original read-only name.
749+
_hardened_read_open = _hardened_open
703750

704751

705752
def open(file, mode="r", *, context="pathsec.open", required_root=None, **kwargs):
@@ -734,17 +781,19 @@ def open(file, mode="r", *, context="pathsec.open", required_root=None, **kwargs
734781

735782
# 4. Execution Substitution: validate and open the pure primitive, discarding the original object
736783
validate_path(raw_path, context=context, required_root=required_root)
737-
# 5. For read modes under enforcement on POSIX, additionally close the
738-
# validate-then-open symlink TOCTOU and the hardlink escape that a path-based
739-
# check cannot see. Write/append/update flows and non-POSIX keep the plain
740-
# open (their containment is unchanged from before).
741-
if ENFORCE and os.name == "posix" and _is_readonly_mode(mode):
784+
# 5. Under enforcement on POSIX, additionally close the validate-then-open
785+
# symlink TOCTOU and the hardlink escape that a path-based check cannot see.
786+
# This now covers read *and* write/append/update modes: the write side is the
787+
# symmetric counterpart of the read-side leak -- a symlink swapped in at the
788+
# destination after validate_path would otherwise let a retrieve()/model write
789+
# land outside the root (CWE-59). Non-POSIX keeps the plain open.
790+
if ENFORCE and os.name == "posix":
742791
# The hardened path owns this open entirely: it raises PermissionError /
743792
# ValueError on a security violation and the usual OSError (e.g.
744793
# FileNotFoundError) on a genuine open failure. We must NOT fall back to
745794
# builtins.open on OSError -- retrying the raw path would follow a symlink
746795
# the hardened open deliberately refused, reopening the TOCTOU leak.
747-
return _hardened_read_open(raw_path, mode, context, required_root, **kwargs)
796+
return _hardened_open(raw_path, mode, context, required_root, **kwargs)
748797
return builtins.open(raw_path, mode=mode, **kwargs)
749798

750799

‎nltk/tag/perceptron.py‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,49 @@ def _authorize_private_dir(directory):
4848
pathsec._LAST_DATA_PATHS = None
4949

5050

51+
def _open_private_model_dir(loc):
52+
"""Create/reuse *loc* as a private directory and return a pinned O_NOFOLLOW
53+
directory descriptor, closing the shared-temp symlink squat / TOCTOU race.
54+
55+
The leaf is created with ``os.mkdir`` (atomic: it either creates the real
56+
directory or fails with ``FileExistsError``) and then re-opened with
57+
``O_NOFOLLOW|O_DIRECTORY`` -- so a symlink pre-planted or raced in at the
58+
guessable default name is refused (``ELOOP``/``ENOTDIR``) instead of being
59+
followed. The descriptor is rejected unless it is a real directory owned by
60+
the current user and not group-/world-writable (CWE-59/377/378).
61+
"""
62+
import errno
63+
64+
parent = os.path.dirname(loc) or "."
65+
os.makedirs(parent, exist_ok=True)
66+
try:
67+
os.mkdir(loc, 0o700)
68+
except FileExistsError:
69+
pass
70+
flags = os.O_RDONLY | getattr(os, "O_DIRECTORY", 0) | getattr(os, "O_NOFOLLOW", 0)
71+
try:
72+
fd = os.open(loc, flags)
73+
except OSError as e:
74+
if e.errno in (errno.ELOOP, errno.ENOTDIR):
75+
raise PermissionError(
76+
f"Refusing trained-model dir {loc!r}: not a real directory "
77+
"(symlink / TOCTOU squat) (CWE-59)"
78+
) from e
79+
raise
80+
try:
81+
st = os.fstat(fd)
82+
# POSIX only: on Windows st_uid is not meaningful and NTFS ACLs govern.
83+
if os.name == "posix" and (st.st_uid != os.getuid() or (st.st_mode & 0o022)):
84+
raise PermissionError(
85+
f"Refusing non-private trained-model dir {loc!r}: not owned by "
86+
"you or group-/world-writable (CWE-377/378)"
87+
)
88+
except BaseException:
89+
os.close(fd)
90+
raise
91+
return fd
92+
93+
5194
try:
5295
import numpy as np
5396
except ImportError:
@@ -289,16 +332,35 @@ def train(self, sentences, save_loc=None, nr_iter=5):
289332
def save_to_json(self, lang="xxx", loc=None):
290333
if not loc:
291334
loc = self.save_dir
292-
# Create the trained-model dir private to the current user (mode 0700).
293-
# The default TRAINED_TAGGER_PATH is the system temp dir, which is shared
294-
# and world-writable on Linux; a private dir cannot be tampered with by
295-
# another local user, and lets it be authorized for reading back below.
296-
os.makedirs(loc, mode=0o700, exist_ok=True)
297-
_authorize_private_dir(loc)
298-
299-
for param, json_file in zip(self.encode_json_obj(), self.param_files(lang)):
300-
with open(path_join(loc, json_file), "w") as fout:
301-
json.dump(param, fout)
335+
# The default TRAINED_TAGGER_PATH is the system temp dir -- shared and
336+
# world-writable on Linux -- and the save dir is a *guessable* name in
337+
# it, so a local attacker can pre-plant or race a symlink at ``loc``. A
338+
# plain ``islink`` pre-check is non-atomic (TOCTOU) and misses it once
339+
# created; ``_open_private_model_dir`` instead creates/re-opens the leaf
340+
# atomically with O_NOFOLLOW|O_DIRECTORY and verifies it is a real,
341+
# user-owned, non-world-writable directory, returning a pinned fd
342+
# (CWE-59/377/378).
343+
dir_fd = _open_private_model_dir(loc)
344+
try:
345+
_authorize_private_dir(loc)
346+
347+
# Write each model file relative to the pinned directory fd (where
348+
# supported) with O_NOFOLLOW (0600), so neither the dir nor the file
349+
# can be redirected outside the verified directory after the check.
350+
use_dir_fd = os.open in os.supports_dir_fd
351+
352+
def _no_follow_opener(path, flags):
353+
extra = {"dir_fd": dir_fd} if use_dir_fd else {}
354+
return os.open(
355+
path, flags | getattr(os, "O_NOFOLLOW", 0), 0o600, **extra
356+
)
357+
358+
for param, json_file in zip(self.encode_json_obj(), self.param_files(lang)):
359+
target = json_file if use_dir_fd else path_join(loc, json_file)
360+
with open(target, "w", opener=_no_follow_opener) as fout:
361+
json.dump(param, fout)
362+
finally:
363+
os.close(dir_fd)
302364

303365
def load_from_json(self, lang="eng", loc=None):
304366
# Automatically find path to the tagger if location is not specified.

0 commit comments

Comments
 (0)