Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

2.4 KB
Raw Blame
"""A symlink escape that non-strict ``Path.resolve()`` cannot see.

``build_path_max_escape(base, outside)`` plants, inside ``base``:

* a chain of 16 nested directories with 254-character names, created
  through ``dir_fd`` so that no syscall ever receives a path longer than
  PATH_MAX;
* ``base/L1``, a relative symlink to the deepest directory of that chain
  (its target is 4079 characters, under the 4095-character limit);
* ``<deepest>/esc``, a symlink to ``outside``.

``base/L1/esc/<name>`` is short, and the kernel opens it as
``outside/<name>``. Resolving it in user space instead builds a prefix
longer than PATH_MAX, so every later ``lstat`` fails with ENAMETOOLONG.
Non-strict ``resolve()`` ignores that error and appends ``esc/<name>``
without following ``esc``. The result is lexically inside ``base``,
which is how a containment check built on it was bypassed.
"""

from __future__ import annotations

import os
from pathlib import Path

import pytest

_DEPTH = 16
_NAME_LENGTH = 254
_PATH_MAX_STRING = 4095


def build_path_max_escape(base: Path, outside: Path) -> str:
    """Plant the chain under ``base`` and return ``"L1/esc"``.

    ``base/L1/esc`` reaches ``outside`` through the kernel. Skips the
    calling test where the platform cannot build the layout, or where
    ``base`` is too short for the resolved prefix to outgrow PATH_MAX.
    """
    names = [chr(ord("a") + i) * _NAME_LENGTH for i in range(_DEPTH)]
    target = "/".join(names)
    resolved_prefix = len(str(base.resolve())) + 1 + len(target)
    if resolved_prefix + len("/esc") <= _PATH_MAX_STRING:
        pytest.skip("base path too short to outgrow PATH_MAX")
    try:
        fd = os.open(base, os.O_RDONLY | os.O_DIRECTORY)
    except (OSError, AttributeError):
        pytest.skip("directory file descriptors are not available")
    try:
        for name in names:
            os.mkdir(name, dir_fd=fd)
            child = os.open(name, os.O_RDONLY | os.O_DIRECTORY, dir_fd=fd)
            os.close(fd)
            fd = child
        os.symlink(str(outside), "esc", dir_fd=fd)
    except (OSError, NotImplementedError):
        pytest.skip("cannot build a PATH_MAX symlink chain here")
    finally:
        os.close(fd)
    try:
        (base / "L1").symlink_to(target, target_is_directory=True)
    except (OSError, NotImplementedError):
        pytest.skip("cannot create symlinks in this environment")
    return "L1/esc"