Repository navigation
Conversation
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
force-pushed
the
fix/logs-dup-above-stderr
branch
from
December 5, 2026 14:17
f531f85 to
228d8b2
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
logs.cc, bothsetDupLogFdOr()andsetLogStderrLevel()(added in #345) calledfcntl(..., F_DUPFD_CLOEXEC, 0)with minimum descriptor0instead ofSTDERR_FILENO + 1. Becausecmdline::parseArgs()adds{0, 1, 2}tonsj->openfds,contain::containMakeFdsCOE()clearsFD_CLOEXECon fds0..2, andsetupFD()maps stdio/connection pipes onto0..2.When
nsjailis launched with closed standard descriptors (for example under a service supervisor or custom process spawner):-l/log_file/--log_fd): with fds0and2(or0,1, and2) closed,logFile()duplicates the newly opened host log file onto fd1or2. BecauselogFile()also opened log files withO_RDWR | O_APPENDrather thanO_WRONLY | O_APPEND, the sandboxed child inherited a readable and appendable descriptor to the host log file outsidepivot_root.LISTENmode (--log_stderr_level): with fds0and1closed and2open,log_init()placed_log_fdon0(also skipped byif (_log_fd > STDERR_FILENO) close(_log_fd)inlogFile()) andsetLogStderrLevel()placed_log_stderr_fdon1. InLISTENmode,setupFD()callsdup2(out[1], 1), redirecting subsequent child diagnostic logs directly to the connected TCP client socket.fdandorfdfailed withEBADF,setDupLogFdOr()retained the closedorfd(typicallySTDERR_FILENO), allowing laterdup2()onto fd2to receive internal log output.Precondition:
nsjailinvoked with one or more of fds0/1/2closed at startup.Fix
logs.cc:TEMP_FAILURE_RETRY(fcntl(..., F_DUPFD_CLOEXEC, STDERR_FILENO + 1))in bothsetDupLogFdOr()andsetLogStderrLevel()._log_fd = -1when duplicating the fallbackorfdfails withEBADF(skippingwriteLogMsg()when_log_fd < 0while preservingLOG_F_exit(0xff)and treating_log_stderr_fdas distinct inupdateStderrFdDistinct()).O_WRONLYinstead ofO_RDWR(logs.cconly writes to_log_fd).tests/logs_fd_test.cc&Makefile:log_init()with closed0/1,logFile()with closed0/2and all stdio closed (verifyinglogFd() > STDERR_FILENO,FD_CLOEXEC,O_WRONLY | O_APPEND, and closed stdio fds untouched),--log_fdwith closed0/1,setLogStderrLevel()in both call orders withdup2()onto stdout, repeatedlogFile()calls without fd leaks, closed fallback fd non-reuse, andLOG_Fexit behavior when_log_fd == -1.Verification
Tested on Linux 6.8.0 (
g++ 11.4,-std=c++20 -Wall -Wextra -Werror):./tests/logs_fd_test(ASan+UBSan) failed atctor_closed_stdin_stdout(Assertion 'logs::logFd() > STDERR_FILENO' failed).-Mo -l <host.log>with fds0and2closed passed<host.log>(flags: 0502002,O_RDWR | O_APPEND) as child fd2, allowing the child to append to the host log file.-Ml -l <listen.log> --log_stderr_level debug -- /bin/echo APP_OKwith fds0and1closed sent 2,363 bytes of internal[D]child logs to the TCP client../tests/logs_fd_test— PASS (standard build and-fsanitize=address,undefined, 0 leaks / 0 UB)./tests/util_msg_test— PASSmake test-cmdline— PASS-Mo -l <host.log>with fds0and2closed:<host.log>is absent from child/proc/self/fd/*and child stderr writes do not reach<host.log>.-Ml -l <listen.log> --log_stderr_level debug -- /bin/echo APP_OKwith fds0and1closed: TCP client receives onlyAPP_OK\n(7 bytes), whiledebuglogs go to stderr andinfologs go to<listen.log>.-Ml -v -- /bin/echo APP_OKwith fd2closed (no-l): TCP client receives onlyAPP_OK\n(7 bytes).