Skip to content

web: fix nil pointer dereference when systemd socket activation is disabled - #435

Open
mrueg wants to merge 1 commit into
prometheus:masterfrom
mrueg:fix/checkflags-nil-listen-addresses
Open

web: fix nil pointer dereference when systemd socket activation is disabled#435
mrueg wants to merge 1 commit into
prometheus:masterfrom
mrueg:fix/checkflags-nil-listen-addresses

Conversation

@mrueg

@mrueg mrueg commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

checkFlags only required listen addresses to be set when WebSystemdSocket was nil:

if c.WebSystemdSocket == nil && (c.WebListenAddresses == nil || len(*c.WebListenAddresses) == 0) {
	return ErrNoListeners
}

A non-nil pointer to false satisfies that condition, so a FlagConfig with systemd socket activation explicitly disabled and no listen addresses passes validation. ListenAndServe then falls through to the port-listener path and panics dereferencing the nil pointer:

listeners := make([]net.Listener, 0, len(*flags.WebListenAddresses))

kingpinflag.AddFlags always hands out a non-nil WebSystemdSocket pointer (it points at false on non-Linux), so this is reachable from any caller that builds a FlagConfig directly instead of going through the kingpin flags — FlagConfig is exported with documented fields, so that is a supported use.

Fix: require listen addresses whenever systemd socket activation is not actually enabled.

checkFlags, ErrNoListeners and ErrMissingFlag had no test coverage at all, so this adds a table test for checkFlags plus an end-to-end check that ListenAndServe returns ErrNoListeners rather than panicking. Reverting the one-line fix makes the new tests fail with the nil dereference panic.

…sabled

checkFlags only required listen addresses to be set when WebSystemdSocket
was nil. A non-nil pointer to false satisfied the check, so a FlagConfig
with systemd socket activation explicitly disabled and no listen addresses
passed validation, and ListenAndServe then panicked dereferencing the nil
WebListenAddresses:

    listeners := make([]net.Listener, 0, len(*flags.WebListenAddresses))

kingpinflag.AddFlags always returns a non-nil WebSystemdSocket pointer, so
this is reachable from any caller that builds a FlagConfig directly rather
than through the kingpin flags.

Require listen addresses whenever systemd socket activation is not actually
enabled, and add tests for checkFlags, which was previously uncovered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Manuel Rüger <manuel@rueg.eu>
@mrueg
mrueg force-pushed the fix/checkflags-nil-listen-addresses branch from 7f52774 to 0ac3bd0 Compare September 2, 2026 11:31
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