Skip to content

Reject a closed --userns-block-fd while parsing arguments - #831

Open
sarankumar1325 wants to merge 1 commit into
containers:mainfrom
sarankumar1325:userns-block-fd-check
Open

sarankumar1325 wants to merge 1 commit into
containers:mainfrom
sarankumar1325:userns-block-fd-check

Conversation

@sarankumar1325

Copy link
Copy Markdown

Fixes #826.

If the descriptor passed to --userns-block-fd is closed, its number can be reused for bwrap's own child_wait_fd eventfd or, with --json-status-fd, the read end of setup_finished_pipe. The parent then reads from or closes its own descriptor (bubblewrap.c, the opt_userns_block_fd block after the clone), and parent and child wait for each other forever.

This checks the descriptor with fcntl (fd, F_GETFD) when the option is parsed. That's before main opens /proc or any eventfd/pipe, so a closed number can't be confused with one of ours. A closed descriptor now fails with bwrap: Invalid fd: N: Bad file descriptor, the check suggested in the issue.

Testing (built with meson on Linux, unprivileged user namespaces):

  • Both reproducers from the issue: on main each one hangs until timeout kills it (rc 124). With this change each exits 1 with Invalid fd: 4: Bad file descriptor / Invalid fd: 5: Bad file descriptor.
  • New test-run.sh case: --userns-block-fd 42 with fd 42 closed must fail with that message. It fails on main (bwrap fails with a different error) and passes here.
  • meson test -C _build: test-utils, test-run.sh, test-specifying-pidns.sh, test-specifying-userns.sh and test-sandbox.py pass. test-seccomp.py is skipped (not available on this box).

I only changed --userns-block-fd, because that's where a reused descriptor makes bwrap wait on itself. The other descriptor options (--block-fd, --sync-fd, --info-fd, ...) aren't validated at parse time either. I can add the same check there if you'd like that.

I used an AI assistant for this change and reviewed the code and tests myself.

If the descriptor given to --userns-block-fd isn't open, its number can
be reused for child_wait_fd or, with --json-status-fd, the read end of
setup_finished_pipe. The parent then closes or blocks on its own
descriptor and the parent and child wait for each other forever.

Check the descriptor with fcntl(F_GETFD) when the option is parsed,
before bwrap opens any descriptors of its own, and fail with
"Invalid fd: N: Bad file descriptor".

Fixes: containers#826
Signed-off-by: sarankumar1325 <120080287+sarankumar1325@users.noreply.github.com>
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.

--userns-block-fd can hang when given a closed descriptor

1 participant