Skip to content

Conversation

@0pcom
Copy link
Collaborator

@0pcom 0pcom commented Dec 29, 2025

Fixes #221

Summary of Changes

This panic:

[2025-12-27T14:32:44.078054838-06:00] INFO [dmsg_srv]: Serving stream. dst_addr=02b307aee5c8ce1666c63891f8af25ad2f0a47a243914c963942b3ba35b9d095ae:80 session=02ea53289b0bd89d18884f7bc202d720243aca3f9fe6e96b1f5fa22daacff7ea40 src_addr=02ea53289b0bd89d18884f7bc202d720243aca3f9fe6e96b1f5fa22daacff7ea40:49392 yamux_id=479
2025/12/27 14:32:44 Invalid public key
panic: Invalid public key

goroutine 7022848 [running]:
log.Panic({0xc00161cd00?, 0x0?, 0x0?})
	/usr/lib/go/src/log/log.go:451 +0x6e
github.com/skycoin/skycoin/src/cipher.MustNewPubKey({0xc006dfea80?, 0x4d9bff5ab27c9c2a?, 0xa7307f0449f740a3?})
	/home/user/go/pkg/mod/github.com/skycoin/skycoin@v0.28.1-0.20251215160458-f84f07154eae/src/cipher/crypto.go:114 +0xb9
github.com/skycoin/dmsg/pkg/noise.Secp256k1.DH({}, {0xc00925d0c1, 0x20, 0x20}, {0xc006dfea80?, 0x20?, 0x20?})
	/home/user/go/pkg/mod/github.com/skycoin/dmsg@v1.3.29-0.20251215165124-9654b31f23f2/pkg/noise/dh.go:26 +0x48
github.com/skycoin/noise.(*HandshakeState).ReadMessage(0xc00127e000, {0x0, 0x0, 0x0}, {0xc007562002, 0x23, 0x1ffe})
	/home/user/go/pkg/mod/github.com/skycoin/noise@v0.0.0-20180327030543-2492fe189ae6/state.go:450 +0x46f
github.com/skycoin/dmsg/pkg/noise.(*Noise).ProcessHandshakeMessage(0xc002c023c0?, {0xc007562002?, 0xc00161cea8?, 0xc00564ce70?})
	/home/user/go/pkg/mod/github.com/skycoin/dmsg@v1.3.29-0.20251215165124-9654b31f23f2/pkg/noise/noise.go:116 +0xb7
github.com/skycoin/dmsg/pkg/noise.ResponderHandshake(0xc003ab9450, 0xc002c023c0, {0x7f02d0060db8, 0xc00011c400})
	/home/user/go/pkg/mod/github.com/skycoin/dmsg@v1.3.29-0.20251215165124-9654b31f23f2/pkg/noise/read_writer.go:243 +0x51
github.com/skycoin/dmsg/pkg/noise.(*ReadWriter).Handshake.func1()
	/home/user/go/pkg/mod/github.com/skycoin/dmsg@v1.3.29-0.20251215165124-9654b31f23f2/pkg/noise/read_writer.go:182 +0x89
created by github.com/skycoin/dmsg/pkg/noise.(*ReadWriter).Handshake in goroutine 7022911
	/home/user/go/pkg/mod/github.com/skycoin/dmsg@v1.3.29-0.20251215165124-9654b31f23f2/pkg/noise/read_writer.go:178 +0x8d
exit status 2

was caused by cipher.MustNewPubKey() in the noise handshake DH function (pkg/noise/dh.go:26), which would
crash the entire server when a client sent an invalid public key.

Fixed Files (branch fix/invalid-pubkey-panic )

  1. pkg/noise/dh.go - Replaced panic-inducing Must* functions with error-returning versions
  2. pkg/noise/read_writer.go - Added panic recovery in handshake goroutine
  3. pkg/dmsg/server.go - Added panic recovery in handleSession
  4. pkg/dmsg/server_session.go - Added panic recovery in stream goroutines

Defense in Depth Approach

The fix uses multiple layers:

• Root cause: DH function now handles invalid keys gracefully, returning empty bytes that will cause handshake to fail
• Handshake layer: Panic recovery catches any remaining panics and converts to errors
• Session layer: Panic recovery prevents session crashes from affecting the server
• Stream layer: Individual stream panics won't kill the session

Moses Narrow added 16 commits August 23, 2025 18:05
Prevents dmsg server crashes when handling clients with invalid public keys.

Changes:
- Modified noise DH function to use non-panic cipher methods (NewPubKey, NewSecKey, ECDH)
  instead of MustNewPubKey, MustNewSecKey, MustECDH
- Added panic recovery in noise handshake to catch any remaining panics
- Added panic recovery in server handleSession to prevent server crashes
- Added panic recovery in server stream goroutines for additional safety
- Added error logging for failed session creation

Invalid public keys will now cause the handshake to fail gracefully instead
of crashing the entire dmsg server process. The server will log the error
and continue serving other clients.

Fixes a 2+ year old issue where 'Invalid public key' panic would crash
production dmsg servers.
Adds TestInvalidPublicKeyNoPanic to verify the server doesn't crash when
receiving connections with invalid public keys during noise handshake.

The test:
1. Sends malformed handshake data with invalid public key to server
2. Verifies server logs error and rejects connection gracefully
3. Confirms server continues to accept valid client connections

This is a regression test for the 2+ year old panic bug that has been
fixed in the previous commits.
- Use proper error handling for conn.Close()
- Add nolint directives for intentionally ignored errors
- Fix ineffectual assignment in test code
@0pcom 0pcom merged commit 7965982 into skycoin:develop Dec 29, 2025
3 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.

Unexpected panic on wrong public key

1 participant