Skip to content

nstun: drain TCP listener at flow capacity - #364

Open
elnadym165 wants to merge 1 commit into
google:masterfrom
elnadym165:fix/nstun-tcp-flow-capacity-spin
Open

elnadym165 wants to merge 1 commit into
google:masterfrom
elnadym165:fix/nstun-tcp-flow-capacity-spin

Conversation

@elnadym165

Copy link
Copy Markdown

Summary

Fix an NSTUN TCP listener busy-loop condition when the aggregate TCP flow limit is reached.

Previously, handle_host_tcp_accept() checked the IPv4/IPv6 TCP flow count before calling accept4(). At NSTUN_MAX_FLOWS (1,024), the handler returned without consuming the pending connection.

With level-triggered epoll, the listening descriptor remained readable, potentially causing repeated network-worker wakeups without progress.

Root Cause

At maximum TCP flow capacity, the handler returned before accepting the pending connection. The listener therefore remained readable and could repeatedly trigger the worker.

Fix

  • Call accept4() before checking TCP flow capacity.
  • Immediately close the accepted descriptor when capacity is exhausted.
  • Preserve the existing aggregate IPv4/IPv6 flow limit.
  • Avoid creating or tracking additional flows beyond capacity.

Regression Test

Added tests/nstun_tcp_capacity_test.cc, which:

  1. Establishes 1,024 real loopback TCP flows.
  2. Queues one additional connection.
  3. Invokes the actual NSTUN TCP accept handler.
  4. Verifies listener readiness before and after processing.
  5. Confirms the flow limit remains enforced.
  6. Checks for additional file-descriptor leakage.

The test is integrated into the Makefile and supports PASS, FAIL, and SKIP outcomes. Exit code 77 indicates insufficient file-descriptor limits.

Verification

Original implementation:

  • Listener remained readable at maximum flow capacity.
  • Regression test failed as expected.

Patched implementation:

  • Listener readiness cleared after handling the excess connection.
  • Aggregate TCP flow capacity remained enforced.
  • No additional file-descriptor leak was detected.
  • Regression test passed.

Additional local validation:

  • Fresh clean-source build passed.
  • NsJail executable build and --help smoke test passed.
  • Five targeted NSTUN tests passed.
  • git diff --check passed.

Run the focused regression test:

make test-nstun-tcp-capacity

The complete privileged upstream integration suite and real guest-TUN integration tests were not executed.

Security and Availability Impact

Local testing demonstrated temporary excessive network-worker CPU consumption under TCP flow-saturation conditions.

The fix consumes and closes the excess connection, preventing it from continuously leaving the listener readable.

The observed condition recovered automatically in local testing. Remote exploitability, indefinite CPU exhaustion, and production-wide denial of service have not been established.

Files Changed

  • nstun/tcp.cc — Fix TCP accept/capacity-check ordering.
  • tests/nstun_tcp_capacity_test.cc — Add regression coverage.
  • Makefile — Integrate the regression test.

@google-cla

google-cla Bot commented Oct 13, 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