Repository navigation
core: Retry privileged binds on EACCES instead of checking port < 1024 - #8253
Open
francislavoie wants to merge 1 commit into
Open
francislavoie wants to merge 1 commit into
francislavoie wants to merge 1 commit into
Conversation
WithBindCapability assumed the kernel's privileged port range ends at 1024, but it is set by net.ipv4.ip_unprivileged_port_start, which can be anywhere from 0 (Docker's default) to 65535. With a value above 1024, a non-root bind to e.g. :1500 failed even though the capability was permitted. Now listen is called normally first, and only if it fails with EACCES on a TCP/UDP port, and CAP_NET_BIND_SERVICE is permitted but not effective, the capability is raised and the bind retried. This also skips raising it when the range starts lower than the port. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Assistance Disclosure
Claude Code (Opus 5.5) wrote the code, tests and this description under my direction; I reviewed the approach and verified the results below.
What
Follow-up to #8102, addressing @Minding000's review comment about the hardcoded 1024 cutoff. Thanks for catching that!
WithBindCapabilityno longer checksport < 1024up front. It callslistennormally, and only if that fails withEACCESon a TCP/UDP port, andCAP_NET_BIND_SERVICEis permitted but not effective, it raises the capability on a locked thread and retries (same mechanism as before).Why
The privileged range is set by
net.ipv4.ip_unprivileged_port_start(0 to 65535, applies to IPv6 too). With a value above 1024, a non-root bind to e.g.:1500failed even though the capability was permitted. With a value below 1024 (Docker's default is 0), #8102 raised the capability needlessly.Retrying on
EACCESwas chosen over reading/proc/sys/net/ipv4/ip_unprivileged_port_startbecause it follows whatever the kernel actually enforces and doesn't depend on/proc/sysbeing visible (e.g. systemdProcSubset=pid). A failedlistenReusableonly touches the listener pool on success, so the retry has no side effects to undo.Testing
Static build with
setcap cap_net_bind_service=+p, in Docker. "ps" is--sysctl net.ipv4.ip_unprivileged_port_start::1500:80:80--cap-drop ALL,:8080--cap-drop ALL, ps=1024,:80:80CapEffis0on every thread afterwards for non-root runs. Unit tests cover whenlistenis retried.🤖 Generated with Claude Code