Skip to content

internal/wgfreebsd: add bounds checks and safe integer arithmetic - #162

Open
Huge wants to merge 1 commit into
WireGuard:masterfrom
Huge:fix/freebsd-bounds-and-overflow
Open

Huge wants to merge 1 commit into
WireGuard:masterfrom
Huge:fix/freebsd-bounds-and-overflow

Conversation

@Huge

@Huge Huge commented Oct 5, 2026 •

Copy link
Copy Markdown

Summary

This PR hardens the FreeBSD kernel backend and its internal nv package with defensive boundary checks, type assertion safeguards, and memory leak cleanups:

  1. parseEndpoint: Validate that the input slice ep contains at least sizeof(unix.RawSockaddr) (and RawSockaddrInet4 or RawSockaddrInet6 respectively) before accessing fields, preventing out-of-bounds reads and panics on empty or short slices.
  2. parsePeer: Validate that public-key and preshared-key slices are exactly 32 bytes before casting to wgtypes.Key, preventing runtime conversion panics.
  3. parseAllowedIP: Safe comma-ok type assertions on cidr, ipv4, and ipv6 nvlist values to prevent unexpected panics on malformed kernel responses.
  4. nv.Unmarshal: Return an error on empty input buffers (preventing &d[0] panic), verify that nvlist_unpack succeeded, and ensure C.nvlist_destroy is called to prevent heap memory leaks.
  5. nv.marshal: Guard against integer overflow in C.sizeof_nvlist_ptr * sz during []List allocation, verify that C.malloc succeeded, and clean up previously allocated sub-nvlists and buffers on error.

Testing

  • Ran go test ./... and go vet ./... cleanly across the repository.

Disclaimer: Found the issue by test-driving an LM-powered vulnerability scanner I work on heavily.

- In parseEndpoint, check that the input slice is large enough to contain
  unix.RawSockaddr, unix.RawSockaddrInet4, and unix.RawSockaddrInet6 before
  reading fields, avoiding out-of-bounds reads and panics on empty slices.
- In parsePeer, check public-key and preshared-key slice lengths before
  converting to wgtypes.Key.
- In parseAllowedIP, safely check type assertions to avoid runtime panics
  on unexpected nvlist data.
- In nv.Unmarshal, return an error if input buffer is empty, verify that
  nvlist_unpack succeeded, and destroy the allocated nvlist on return.
- In nv.marshal, check for integer overflow in allocation size, check for
  C.malloc failure, and free allocated nvlist items on sub-marshal errors.

Signed-off-by: Martin Vlach <mr.huge@seznam.cz>
@Huge

Huge commented Oct 27, 2026

Copy link
Copy Markdown
Author

Gentle ping on this one and #163.

About the red build (1.20) check here: it fails before any test runs, while building wireguard-go, because Go 1.20 can't parse that module's go 1.23.1 directive (invalid go version '1.23.1': must match format 1.23). This change doesn't cause it.

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