tailscale/tailscale
Linux updateMagicsockPort adds firewall rule for old port instead of new
Summary
Context: The
updateMagicsockPortfunction inwgengine/router/osrouter/router_linux.gomanages firewall rules when the magicsock UDP port changes.Bug: When updating the magicsock port, the function adds a firewall rule for the old port value instead of the new port value.
Actual vs. expected: The firewall rule is created for the old port while the router’s internal state is updated to the new port, creating a state inconsistency.
Impact: Traffic on the new port will be blocked by the firewall, causing connection failures when Tailscale changes UDP ports during NAT traversal or network changes.
Code with bug
In wgengine/router/osrouter/router_linux.go, function updateMagicsockPort:
The variable *magicsockPort contains the old port value when passed to AddMagicsockPortRule, and is only updated to the new value afterward.
Test demonstration
A test script demonstrates the bug by changing the port from 12345 to 54321:
Buggy implementation output:
Fixed implementation output:
The test clearly shows the mismatch: the buggy implementation deletes the rule for port 12345 and then re-adds a rule for 12345, while the internal state is updated to 54321. This leaves no firewall rule for the actual port in use.
Recommended fix
Change the AddMagicsockPortRule call to use the port parameter instead of *magicsockPort:
This ensures the firewall rule is created for the new port value before updating the internal state.