8000 fix(util): close received descriptors on invalid messages by FranprzDev · Pull Request #388 · google/nsjail · GitHub
[go: up one dir, main page]

Skip to content

fix(util): close received descriptors on invalid messages - #388

Open
FranprzDev wants to merge 1 commit into
google:masterfrom
FranprzDev:fix/recvmsg-fd-lifetime
Open

FranprzDev wants to merge 1 commit into
google:masterfrom
FranprzDev:fix/recvmsg-fd-lifetime

Conversation

@FranprzDev
Copy link
Copy Markdown

Summary

Make descriptor ownership explicit at the recvMsg() boundary:

  • Receive SCM_RIGHTS descriptors with MSG_CMSG_CLOEXEC, so they do not become inheritable just because they were passed over a socket.
  • Close every received descriptor before rejecting a short payload or a message with truncated data/control information.
  • Preserve the existing behavior of returning the first descriptor and closing additional descriptors for a valid message.
  • Leave the message output unchanged on failure and set the descriptor output to -1.

recvmsg() can install descriptors even when its payload is too short. The old early return loses those descriptors. It also accepts a truncated control message as successful. This is descriptor-lifetime hardening, not a claim of a demonstrated sandbox escape.

Tests

Added tests/util_msg_test.cc, wired into make test. It uses local Unix socketpairs, without starting nsjail or creating namespaces. Eleven cases cover stream and sequenced-packet sockets, CLOEXEC, short payloads, truncated control data, extra descriptors, no descriptor output, and an oversized packet. Each case checks the process's FD count after cleanup.

Against the original util.cc, isolated checks fail for missing CLOEXEC, leaked descriptors after a short payload, acceptance of truncated control data, and acceptance of an oversized sequenced packet. Valid extra-descriptor and no-output controls pass. With the patch, all eleven cases pass.

Validation run locally:

  • All five Makefile unit-test binaries built and passed: util_msg_test, nstun_buffer_budget_test, nstun_policy_test, nstun_ip_test, nstun_udp_peer_test.
  • New test passed with ASan and UBSan.
  • clang-format --dry-run --Werror util.cc tests/util_msg_test.cc and git diff --check passed.
  • Full make all passed with the existing compatibility value USER_DEFINES=-DPR_SCHED_CORE_SCOPE_THREAD_GROUP=1. The local Ubuntu 22.04 headers lack that constant while defining PR_SCHED_CORE, so the existing fallback in missing_defs.h does not supply it. No source workaround is included in this PR. Optional libnl support was not enabled in this local build.

The privileged/network integration suite was not run.

AI tooling assisted this contribution. The code and tests were reviewed, and the listed commands were run locally.

@google-cla
google-cla Bot commented Nov 10, 2026
Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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