Skip to content

Fix caddy l4 proxy nil exception - #149

Open
wodev wants to merge 2 commits into
tailscale:mainfrom
wodev:fix-caddy-l4-proxy-nil-exception
Open

wodev wants to merge 2 commits into
tailscale:mainfrom
wodev:fix-caddy-l4-proxy-nil-exception

Conversation

@wodev

@wodev wodev commented Nov 2, 2026

Copy link
Copy Markdown

Summary

Prevent nil connection addresses from reaching Caddy by caching each accepted tsnet connection’s local and remote addresses at the listener boundary.

This PR adds an address-caching listener to both the TCP and TLS listener paths, together with focused regression tests. Connections whose addresses are already unavailable when accepted are closed and skipped rather than passed to Caddy with invalid metadata.

Problem

Running caddy-l4 https://github.com/mholt/caddy-l4 with a caddy-tailscale listener can cause a sporadic nil-pointer panic that terminates the entire Caddy process. The reported failure occurs in layer4.(*Server).handle, where connection address information is accessed for logging. Updating the Tailscale dependency to v1.104.4 did not resolve the reported crash.

The underlying userspace TCP connection can return nil from RemoteAddr() when its network endpoint is no longer connected. Downstream code that subsequently calls:

conn.RemoteAddr().String()

can therefore panic. Address availability depends on endpoint state, so the failure is timing-sensitive: a connection may initially have valid addresses but lose them before downstream handling or logging completes.

The original proposal added address caching to caddy-l4. During review, a maintainer recommended addressing the behavior outside caddy-l4 rather than adding dependency-specific complexity there. This PR moves the compatibility handling to the point where caddy-tailscale delivers connections to Caddy.

Changes

Cache addresses before handing connections to Caddy

The new addressCachingListener wraps the underlying listener. For each successful Accept(), it reads the connection’s LocalAddr() and RemoteAddr() and returns an addressCachingConn containing those values.

The connection wrapper overrides only the two address methods:

func (conn *addressCachingConn) LocalAddr() net.Addr
func (conn *addressCachingConn) RemoteAddr() net.Addr

Subsequent address lookups return the captured values without consulting the underlying endpoint. The standard net.Conn operations—including reads, writes, deadlines, and close—remain delegated to the embedded connection.

This keeps address metadata available after the underlying endpoint closes, allowing downstream logging and address-based processing to continue safely.

Reject connections whose addresses are already unavailable

Successful acceptance does not guarantee that the peer is still connected when its addresses are inspected. A peer may reset the connection before the listener can capture its metadata.

If either address is nil, the wrapper closes the connection and continues accepting. It deliberately does not substitute an empty or synthetic address, because fabricated metadata could affect logging, matching, or authentication. Errors returned by the underlying listener are propagated unchanged.

Apply the wrapper to both TCP and TLS listeners

For plain TCP, the shared listener wraps the tsnet listener directly.

For TLS, address caching is installed below tls.NewListener:

Caddy
  → *tls.Conn
    → *addressCachingConn
      → underlying tsnet connection

This captures addresses before TLS handling while preserving the outer *tls.Conn exposed to Caddy. Both listener paths receive the same address-preservation behavior.

Regression tests

The added tests cover:

  • Address preservation after closure: the underlying test connection returns nil addresses after closing, while the accepted wrapper retains the original values.
  • Downstream address access: Network() and String() remain usable after the underlying connection closes.
  • Missing addresses at acceptance: connections with a nil local address, remote address, or both are closed and skipped in favor of the next valid connection.
  • Accept error propagation: errors are returned unchanged, including after rejecting a connection with missing addresses.
  • TLS wrapper ordering: accepted connections remain *tls.Conn values and continue exposing cached addresses after their underlying endpoint closes.
  • Listener close delegation: closing the wrapper closes the underlying listener.

These are deterministic unit tests using test doubles; they do not require a running Tailscale node. The TLS test verifies the intended wrapper ordering without performing a handshake.

Scope and limitations

This change is limited to accepted connections on the TCP and TLS listener paths. It does not modify UDP handling, outbound dialing, or caddy-l4.

The wrapper retains the returned net.Addr values; it does not deep-copy arbitrary address implementations. It addresses the reported failure mode in which later address lookups return nil.

The tests validate the wrapper’s behavior, but do not reproduce the complete Caddy/tsnet deployment or invoke the configured listener factories directly. End-to-end testing with repeated connection resets remains useful for confirming the reported production failure is resolved.

Related issue

Fixes mholt/caddy-l4#490.

Co-Authored by GPT-6.1 Sol

wodev added 2 commits November 2, 2026 22:35
Add an address-caching listener that snapshots each accepted
connection's local and remote addresses before handing it to Caddy.
This keeps address information available after the underlying
userspace network endpoint is closed.

If a connection already has a nil local or remote address when
accepted, close it and continue accepting rather than passing an
unusable connection to Caddy or fabricating metadata. This avoids
invalid addresses reaching logging, matching, or authentication.

Apply the wrapper to both shared TCP listeners and TLS listeners so
the behavior is consistent across both listener paths.

Fixes mholt/caddy-l4#490

Signed-off-by: wodev <14372617+wodev@users.noreply.github.com>
Add focused tests for the address-caching listener used by the TCP and
TLS listener paths. Verify that accepted connections retain their
local and remote addresses after the underlying connection closes,
so downstream code can still access valid address metadata.

Cover connections with a missing local address, remote address, or
both, and confirm the listener closes and skips them in favor of the
next valid connection. Also verify that accept errors are propagated
unchanged, including when they occur after a connection with missing
addresses has been rejected.

Exercise the TLS listener ordering used by getTLSListener and confirm
that the accepted TLS connection continues to expose the cached
addresses when its underlying endpoint closes.

Related to mholt/caddy-l4#490

Signed-off-by: wodev <14372617+wodev@users.noreply.github.com>
@wodev
wodev force-pushed the fix-caddy-l4-proxy-nil-exception branch from 45a89ae to 8592ea9 Compare November 2, 2026 22:36
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.

layer4/server.go:230 : panic: runtime error: invalid memory address or nil pointer dereference

1 participant