8000 logs: keep duplicated log fds above stderr and open log files write-only by hyc-alt · Pull Request #414 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content

logs: keep duplicated log fds above stderr and open log files write-only - #414

Open
hyc-alt wants to merge 1 commit into
google:masterfrom
hyc-alt:fix/logs-dup-above-stderr
Open

hyc-alt wants to merge 1 commit into
google:masterfrom
hyc-alt:fix/logs-dup-above-stderr

Conversation

@hyc-alt
@hyc-alt hyc-alt commented Dec 5, 2026
Copy link
Copy Markdown
Contributor

Problem

In logs.cc, both setDupLogFdOr() and setLogStderrLevel() (added in #345) called fcntl(..., F_DUPFD_CLOEXEC, 0) with minimum descriptor 0 instead of STDERR_FILENO + 1. Because cmdline::parseArgs() adds {0, 1, 2} to nsj->openfds, contain::containMakeFdsCOE() clears FD_CLOEXEC on fds 0..2, and setupFD() maps stdio/connection pipes onto 0..2.

When nsjail is launched with closed standard descriptors (for example under a service supervisor or custom process spawner):

  1. Host log file fd leak into the sandboxed child (-l / log_file / --log_fd): with fds 0 and 2 (or 0, 1, and 2) closed, logFile() duplicates the newly opened host log file onto fd 1 or 2. Because logFile() also opened log files with O_RDWR | O_APPEND rather than O_WRONLY | O_APPEND, the sandboxed child inherited a readable and appendable descriptor to the host log file outside pivot_root.
  2. Diagnostic log leakage to remote TCP clients in LISTEN mode (--log_stderr_level): with fds 0 and 1 closed and 2 open, log_init() placed _log_fd on 0 (also skipped by if (_log_fd > STDERR_FILENO) close(_log_fd) in logFile()) and setLogStderrLevel() placed _log_stderr_fd on 1. In LISTEN mode, setupFD() calls dup2(out[1], 1), redirecting subsequent child diagnostic logs directly to the connected TCP client socket.
  3. Closed fallback fd reuse: when duplicating both fd and orfd failed with EBADF, setDupLogFdOr() retained the closed orfd (typically STDERR_FILENO), allowing later dup2() onto fd 2 to receive internal log output.

Precondition: nsjail invoked with one or more of fds 0/1/2 closed at startup.

Fix

  • logs.cc:
    • Use TEMP_FAILURE_RETRY(fcntl(..., F_DUPFD_CLOEXEC, STDERR_FILENO + 1)) in both setDupLogFdOr() and setLogStderrLevel().
    • Keep _log_fd = -1 when duplicating the fallback orfd fails with EBADF (skipping writeLogMsg() when _log_fd < 0 while preserving LOG_F _exit(0xff) and treating _log_stderr_fd as distinct in updateStderrFdDistinct()).
    • Open log files with O_WRONLY instead of O_RDWR (logs.cc only writes to _log_fd).
  • tests/logs_fd_test.cc & Makefile:
    • Add unit test covering constructor log_init() with closed 0/1, logFile() with closed 0/2 and all stdio closed (verifying logFd() > STDERR_FILENO, FD_CLOEXEC, O_WRONLY | O_APPEND, and closed stdio fds untouched), --log_fd with closed 0/1, setLogStderrLevel() in both call orders with dup2() onto stdout, repeated logFile() calls without fd leaks, closed fallback fd non-reuse, and LOG_F exit behavior when _log_fd == -1.

Verification

Tested on Linux 6.8.0 (g++ 11.4, -std=c++20 -Wall -Wextra -Werror):

  • Before fix:
    • ./tests/logs_fd_test (ASan+UBSan) failed at ctor_closed_stdin_stdout (Assertion 'logs::logFd() > STDERR_FILENO' failed).
    • -Mo -l <host.log> with fds 0 and 2 closed passed <host.log> (flags: 0502002, O_RDWR | O_APPEND) as child fd 2, allowing the child to append to the host log file.
    • -Ml -l <listen.log> --log_stderr_level debug -- /bin/echo APP_OK with fds 0 and 1 closed sent 2,363 bytes of internal [D] child logs to the TCP client.
  • After fix:
    • ./tests/logs_fd_test — PASS (standard build and -fsanitize=address,undefined, 0 leaks / 0 UB)
    • ./tests/util_msg_test — PASS
    • make test-cmdline — PASS
    • End-to-end -Mo -l <host.log> with fds 0 and 2 closed: <host.log> is absent from child /proc/self/fd/* and child stderr writes do not reach <host.log>.
    • End-to-end -Ml -l <listen.log> --log_stderr_level debug -- /bin/echo APP_OK with fds 0 and 1 closed: TCP client receives only APP_OK\n (7 bytes), while debug logs go to stderr and info logs go to <listen.log>.
    • End-to-end -Ml -v -- /bin/echo APP_OK with fd 2 closed (no -l): TCP client receives only APP_OK\n (7 bytes).

Use F_DUPFD_CLOEXEC with minimum fd STDERR_FILENO + 1 in setDupLogFdOr() and setLogStderrLevel(), avoid retaining a closed fallback fd on EBADF, and open log files O_WRONLY instead of O_RDWR.
@hyc-alt
hyc-alt force-pushed the fix/logs-dup-above-stderr branch from f531f85 to 228d8b2 Compare December 5, 2026 14:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0