`IO.FS.createTempFile` / `createTempDir` abort with a `PATH_MAX` assertion when `TMPDIR` is within 13 bytes of `PATH_MAX`

Aperta
#16,819 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
63/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

The affected functions are lean_io_create_tempfile and lean_io_create_tempdir in src/runtime/io.cpp; the report identifies the PATH_MAX assertions at lines 1343, 1350, 1398, and 1405. Read those functions and run the supplied Lean reproducer for both operations with a long TMPDIR. Done means each call returns a catchable IO error instead of aborting; the report also names tests/async/elab/io_enoent_without_file_name.lean as a related test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
Prerequisites
  • Check that your issue is not already filed:
    https://github.com/leanprover/lean4/issues
  • Reduce the issue to a minimal, self-contained, reproducible test case.
    Avoid dependencies to Mathlib or Batteries.
  • Test your test case against the latest nightly release, for example on
    https://live.lean-lang.org/#project=lean-nightly
    (You can also use the settings there to switch to “Lean nightly”)
  • When reporting a perceived soundness bug: Does it affect lake check --paranoid?
    If in doubt, please reach out on [email protected] for responsible disclosure.
    (Not a soundness bug.)
Description

lean_io_create_tempdir copies the uv_os_tmpdir result into char path[PATH_MAX], checks only that one '/' fits (io.cpp:1398), then calls lean_always_assert(PATH_MAX >= strlen(path) + 12 + 1) (io.cpp:1405). lean_io_create_tempfile contains the same code (io.cpp:1343 and io.cpp:1350). With a TMPDIR whose length is between PATH_MAX - 13 and PATH_MAX - 1 (4083-4095 bytes on Linux, 1011-1023 bytes on macOS), that assert fails and the process aborts (at exactly PATH_MAX - 1 it is the preceding PATH_MAX >= base_len + 1 + 1 assert that fires). A slightly shorter TMPDIR makes mkdtemp/mkstemp return ENAMETOOLONG, so the assert is the only reason this case aborts instead of returning an error. TMPDIR is user-controlled input, so the functions should return an IO error. A TMPDIR this close to PATH_MAX is far outside real use. Both functions would be fixed in one patch.

The program sets TMPDIR to "/nonexistent-lean-test-dir" and calls IO.FS.createTempFile (resp. IO.FS.createTempDir). That call returns the error "no such file or directory (error code: 2)", as the repo's tests/async/elab/io_enoent_without_file_name.lean test requires. It then sets TMPDIR to "/" followed by PATH_MAX - 10 'a' characters (a path that also does not exist, and leaves no room for the appended "/tmp.XXXXXXXX") and calls the function again. Instead of returning an IO error result, the process is killed by SIGABRT with a LEAN ASSERTION VIOLATION at io.cpp:1350 (createTempFile) / io.cpp:1405 (createTempDir). The same program with a TMPDIR about 75 bytes shorter returns "invalid argument (error code: 63, name too long)" (macOS; 36 on Linux). So the abort is the runtime's own buffer arithmetic, not an OS limit.

IO.FS.createTempFile (lean_io_create_tempfile)

Aborts at io.cpp:1350, PATH_MAX >= strlen(path) + file_pattern_size + 1.

IO.FS.createTempDir (lean_io_create_tempdir)

Aborts at io.cpp:1405, the same assertion.

Context
  • Affected functions: lean_io_create_tempfile, lean_io_create_tempdir (src/runtime/io.cpp)
  • Upstream: no existing issue found. #16202 (closed by #15175) covered the missing-TMPDIR segfault in the same functions, not this length assert. The cited code is unchanged in nightly-2026-11-26.
Steps to Reproduce
  1. Save the following as input.lean (imports only Init/Std). The length 1014 is for macOS (PATH_MAX = 1024); on Linux (PATH_MAX = 4096) use 4082.
import Std.Internal.UV

open Std.Internal.UV

def tryFile (label : String) : IO Unit := do
  try
    let (_h, p) ← IO.FS.createTempFile
    IO.println s!"{label}: ok {p}"
  catch e =>
    IO.println s!"{label}: error {e}"

def main : IO Unit := do
  -- the situation of tests/async/elab/io_enoent_without_file_name.lean
  System.osSetenv "TMPDIR" "/nonexistent-lean-test-dir"
  tryFile "short"
  -- the same, with a TMPDIR whose path leaves no room for the appended "tmp.XXXXXXXX"
  -- (PATH_MAX - 10 characters: 1014 on macOS, 4082 on Linux)
  let long := "/" ++ String.ofList (List.replicate 1014 'a')
  IO.println s!"len={long.length}"
  System.osSetenv "TMPDIR" long
  tryFile "long"

For createTempDir, replace tryFile with:

def tryDir (label : String) : IO Unit := do
  try
    let d ← IO.FS.createTempDir
    IO.println s!"{label}: ok {d}"
  catch e =>
    IO.println s!"{label}: error {e}"
  1. Compile: lean -c input.c input.lean && leanc -o prog input.c
  2. Run: ./prog

Expected behavior: both calls return a catchable IO error, e.g.

short: error no such file or directory (error code: 2)
  file: 
len=1015
long: error invalid argument (error code: 63, name too long)

Actual behavior (createTempFile; exit status 134, SIGABRT):

short: error no such file or directory (error code: 2)
  file: 
len=1015
LEAN ASSERTION VIOLATION
File: .../src/runtime/io.cpp
Line: 1350
PATH_MAX >= strlen(path) + file_pattern_size + 1
libc++abi: terminating due to uncaught exception of type lean::unreachable_reached: 'unreachable' code was reached

createTempDir gives the same output with Line: 1405.

On macOS a TMPDIR of length 1011 aborts in the same way, and 1023 aborts on the earlier PATH_MAX >= base_len + 1 + 1 assert (line 1343 / 1398). Lengths 1010 or 941 make both functions return long: error invalid argument (error code: 63, name too long) and exit 0.

Versions

Lean (version 4.38.0-nightly-2026-11-26, arm64-apple-darwin24.6.0, commit e1228b8b45918ece1a00d68e5fc5e6a685ca59ee, Release)
macOS 26.6.2 (arm64)

Additional Information

Root cause analysis. The functions take no arguments; their input is the process's TMPDIR, which any caller may set (here with System.osSetenv, as tests/async/elab/io_enoent_without_file_name.lean does). libuv's uv_os_tmpdir accepts that base and returns 0 with the correct NUL-terminated base path and base_len = PATH_MAX - 9. Since the base does not end in '/', the code appends one (the guard PATH_MAX >= base_len + 1 + 1 holds). Then the check PATH_MAX >= strlen(path) + strlen("tmp.XXXXXXXX") + 1 evaluates to PATH_MAX >= PATH_MAX + 1 and fails. lean_always_assert throws lean::unreachable_reached out of an extern "C" runtime function, which reaches std::terminate and SIGABRT. The function should have returned an IO error result, as it does for the same nonexistent TMPDIR at 25 bytes and at slightly shorter long lengths. The bytes it still needs (the '/', "tmp.XXXXXXXX" and the terminator) beyond what is left of char path[PATH_MAX], and the decision to assert rather than return an error, are in these functions' own code (io.cpp:1340-1351 and 1397-1406).

Suggested fix: replace the lean_always_asserts on PATH_MAX >= strlen(path) + file_pattern_size + 1 (and on PATH_MAX >= base_len + 1 + 1) in both lean_io_create_tempfile and lean_io_create_tempdir with an IO error result, e.g. io_result_mk_error(decode_uv_error(UV_ENAMETOOLONG, nullptr)), when the appended pattern does not fit, the way the following uv_fs_mkstemp/uv_fs_mkdtemp failure path already reports ENAMETOOLONG.

Impact

Only a process whose TMPDIR is within 13 bytes of PATH_MAX (4083-4095 bytes on Linux, 1011-1023 on macOS) is affected: it aborts on IO.FS.createTempFile/withTempFile and IO.FS.createTempDir/withTempDir instead of getting a catchable error.

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Lingua principale
Lean
Stelle
10.3k
Fork
1.1k
Merge medio
16h 45m
PR unite (30g)
361

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su Backend & API Design

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.