Skip to content

fix(plugin): prevent panic when ListenHosts is empty#7565

Merged
yongtang merged 1 commit into
coredns:masterfrom
thevilledev:fix/listenhost-panic
Sep 19, 2025
Merged

fix(plugin): prevent panic when ListenHosts is empty#7565
yongtang merged 1 commit into
coredns:masterfrom
thevilledev:fix/listenhost-panic

Conversation

@thevilledev

@thevilledev thevilledev commented Sep 19, 2025

Copy link
Copy Markdown
Collaborator

1. Why is this pull request needed and what does it do?

Fix a runtime panic caused by direct indexing into an empty ListenHosts slice. The trace plugin accessed cfg.ListenHosts[0] to build a service endpoint. The loop plugin used conf.ListenHosts[0] to construct a self-query address. When bind resolves no addresses or excludes all configured addresses, ListenHosts can be empty. Both plugins now check length before indexing.

2. Which issues (if any) are related?

OSS-Fuzz finding #42515496

You can run TestBind_FilterAll against master branch and the test will panic on:

panic: runtime error: index out of range [0] with length 0 [recovered, repanicked]

Details --- FAIL: TestBind_FilterAll (0.00s) panic: runtime error: index out of range [0] with length 0 [recovered, repanicked]

goroutine 99 [running]:
testing.tRunner.func1.2({0x10586dc60, 0x1400061a0a8})
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1872 +0x190
testing.tRunner.func1()
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1875 +0x31c
panic({0x10586dc60?, 0x1400061a0a8?})
/opt/homebrew/Cellar/go/1.25.1/libexec/src/runtime/panic.go:783 +0x120
github.com/coredns/coredns/plugin/trace.traceParse(0x1400023d440)
/git/coredns/plugin/trace/setup.go:40 +0x8f0
github.com/coredns/coredns/plugin/trace.setup(0x1400023d440)
/git/coredns/plugin/trace/setup.go:17 +0x24
github.com/coredns/caddy.executeDirectives(0x140001e4600, {0x104e2a6e8, 0x8}, {0x107425f80, 0x36, 0x1?}, {0x14000606440, 0x1, 0x10744c840?}, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:663 +0x480
github.com/coredns/caddy.ValidateAndExecuteDirectives({0x105a9a3e0, 0x14000612210}, 0x1400009fd28?, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:614 +0x2cc
github.com/coredns/caddy.startWithListenerFds({0x105a9a3e0, 0x14000612210}, 0x140001e4600, 0x0)
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:517 +0x224
github.com/coredns/caddy.Start({0x105a9a3e0, 0x14000612210})
/go/pkg/mod/github.com/coredns/caddy@v1.1.2-0.20241029205200-8de985351a98/caddy.go:474 +0xbc
github.com/coredns/coredns/test.CoreDNSServer({0x104efe2e5?, 0x10307ad20?})
/git/coredns/test/server.go:21 +0x144
github.com/coredns/coredns/test.TestBind_FilterAll(0x140002ee8c0)
/git/coredns/test/corefile_test.go:36 +0x30
testing.tRunner(0x140002ee8c0, 0x105a6ac38)
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1934 +0xc8
created by testing.(*T).Run in goroutine 1
/opt/homebrew/Cellar/go/1.25.1/libexec/src/testing/testing.go:1997 +0x364
exit status 2
FAIL github.com/coredns/coredns/test 0.664s

3. Which documentation changes (if any) need to be made?

None.

4. Does this introduce a backward incompatible change or deprecation?

No, code hardening.

Fix a runtime panic caused by direct indexing into an empty
ListenHosts slice. The trace plugin accessed cfg.ListenHosts[0]
to build a service endpoint. The loop plugin used conf.ListenHosts[0]
to construct a self-query address. When bind resolves no addresses or
excludes all configured addresses, ListenHosts can be empty. Both
plugins now check length before indexing.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
@codecov

codecov Bot commented Sep 19, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 62.21%. Comparing base (93c57b6) to head (cd70434).
⚠️ Report is 1654 commits behind head on master.

Files with missing lines Patch % Lines
plugin/loop/setup.go 0.00% 4 Missing ⚠️
plugin/trace/setup.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7565      +/-   ##
==========================================
+ Coverage   55.70%   62.21%   +6.51%     
==========================================
  Files         224      274      +50     
  Lines       10016    18313    +8297     
==========================================
+ Hits         5579    11393    +5814     
- Misses       3978     6252    +2274     
- Partials      459      668     +209     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yongtang yongtang merged commit 5532ba8 into coredns:master Sep 19, 2025
13 checks passed
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.

2 participants