sing-box icon indicating copy to clipboard operation
sing-box copied to clipboard

[v1.14.0]: panic: runtime error: invalid memory address or nil pointer dereference in protocol/direct.(*Outbound).fetchMyAddresses on FreeBSD

Open arabesc opened this issue 2 months ago • 2 comments

Operating system

Others

System version

FreeBSD v14.4

Installation type

Original sing-box Command Line

Description

sing-box crashes on startup with a SIGSEGV / nil pointer dereference on FreeBSD 14.4 when initializing the direct outbound.

In sing-box v1.14.0, fetchMyAddresses() was introduced into direct.Outbound.Start(). On FreeBSD, the interface monitoring subsystem is unsupported/stubbed, causing h.router.InterfaceMonitor() (or the default interface lookup) to return nil. Attempting to query addresses on a nil reference results in a crash.

Nearly the same configuration works as expected on FreeBSD 14.4 under v1.13.x.

Logs

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x10669f0]

goroutine 1 [running]:
github.com/sagernet/sing-box/protocol/direct.(*Outbound).fetchMyAddresses(0x87405c300)
        github.com/sagernet/sing-box/protocol/direct/outbound.go:102 +0x30
github.com/sagernet/sing-box/protocol/direct.(*Outbound).Start(0x30c4cf0?, 0xa0?)
        github.com/sagernet/sing-box/protocol/direct/outbound.go:95 +0x37
github.com/sagernet/sing-box/adapter.LegacyStart({0x1cd5ea0?, 0x87405c300}, 0x2?)
        github.com/sagernet/sing-box/adapter/lifecycle_legacy.go:5 +0xa9
github.com/sagernet/sing-box/adapter/outbound.(*Manager).Start(0x8741a8000, 0x2)
        github.com/sagernet/sing-box/adapter/outbound/manager.go:86 +0x39d
github.com/sagernet/sing-box/adapter.Start({0x1eafcd0, 0x86ff52370}, {0x1ed3ad8, 0x874239e48}, 0x2, {0x86fded290?, 0xb, 0x0?})
        github.com/sagernet/sing-box/adapter/lifecycle.go:86 +0x1a8
github.com/sagernet/sing-box.(*Box).start(0x8741aa3c0)
        github.com/sagernet/sing-box/box.go:623 +0x37d
github.com/sagernet/sing-box.(*Box).Start(0x8741aa3c0)
        github.com/sagernet/sing-box/box.go:548 +0x36
main.create({{0x874226000, 0x3a17, 0x4000}, 0x0, {0x0, 0x0}, 0x874266210, 0x8741a3b20, 0x0, 0x0, ...})
        github.com/sagernet/sing-box/cmd/sing-box/cmd_run.go:164 +0x4a9
main.run()
        github.com/sagernet/sing-box/cmd/sing-box/cmd_run.go:190 +0x293
main.init.func86(0x8741eae00?, {0x1d6b9ec?, 0x4?, 0x1d6b9f0?})
        github.com/sagernet/sing-box/cmd/sing-box/cmd_run.go:30 +0x17
github.com/spf13/cobra.(*Command).execute(0x3101920, {0x8741804c0, 0x2, 0x2})
        github.com/spf13/[email protected]/command.go:1019 +0xafb
github.com/spf13/cobra.(*Command).ExecuteC(0x30f27a0)
        github.com/spf13/[email protected]/command.go:1148 +0x465
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/[email protected]/command.go:1071
main.main()
        github.com/sagernet/sing-box/cmd/sing-box/main.go:8 +0x1e

arabesc avatar Sep 05 '26 21:09 arabesc

Regarding commit f83b0e4: it now returns an error pointing to route.auto_detect_interface, but according to the documentation, auto_detect_interface isn't supported on FreeBSD.

This means direct outbound still won't be able to initialize on BSD systems.

Could a missing interface monitor be treated as non-fatal on unsupported platforms (e.g. log a warning and skip address fetching) so the service doesn't fail on startup?

arabesc avatar Sep 06 '26 14:09 arabesc

I think there is a small misunderstanding about what f83b0e4 does.

The error:

missing monitor for auto DHCP, set route.auto_detect_interface

is added to:

dns/transport/dhcp/dhcp.go

and only applies when the DHCP DNS transport needs automatic interface discovery.

It is not the behavior added to the direct outbound.

For the crash reported in this issue, the same commit changes:

func (h *Outbound) fetchMyAddresses() {
    myInterfaceNames := h.network.InterfaceMonitor().MyInterfaces()

to:

func (h *Outbound) fetchMyAddresses() {
    interfaceMonitor := h.network.InterfaceMonitor()
    if interfaceMonitor == nil {
        return
    }

    myInterfaceNames := interfaceMonitor.MyInterfaces()

So the direct outbound now does exactly the non-fatal fallback suggested above:

no InterfaceMonitor
    -> skip my-address discovery
    -> continue starting

It does not require route.auto_detect_interface on FreeBSD in order to pass this code path.

The route.auto_detect_interface error belongs to the separate auto-DHCP case.

I also checked current testing; protocol/direct/outbound.go still contains:

interfaceMonitor := h.network.InterfaceMonitor()
if interfaceMonitor == nil {
    return
}

so the original v1.14.0 nil dereference in:

protocol/direct.(*Outbound).fetchMyAddresses

appears fixed.

The relevant fix is:

f83b0e433785938a04ef44869862c59f0b62f284
Fix crash when interface monitor is unavailable

Fixes #4502

There is also an equivalent commit in the current branch history:

288411b0b9044c11a00a8ab478000e3ec1133101
Fix crash when interface monitor is unavailable

The important distinction is therefore:

direct.fetchMyAddresses()
    missing monitor -> non-fatal / skip

auto DHCP without explicit interface
    missing monitor -> configuration error

Those two behaviors are intentional for different reasons.

So unless there is another FreeBSD startup failure after applying f83b0e4, I think the crash described by this issue itself is resolved.

A useful regression test would simply instantiate/start a direct outbound with a NetworkManager whose InterfaceMonitor() returns nil and assert that startup succeeds without populating myAddresses.

HeisLuka avatar Sep 09 '26 21:09 HeisLuka