Skip to content

Don't send a data packet before the crypto keys exist - #472

Closed
bill-rich wants to merge 1 commit into
ValveSoftware:masterfrom
bill-rich:fix/snp-send-crypt-guard
Closed

bill-rich wants to merge 1 commit into
ValveSoftware:masterfrom
bill-rich:fix/snp-send-crypt-guard

Conversation

@bill-rich

Copy link
Copy Markdown
Contributor

A remotely initiated P2P connection can have its transport selected while it still owes its ConnectOK and has
not derived keys: the peer's crypt message has set m_eNegotiatedCipher, but BFinishCryptoHandshake has not
run, so m_pCryptContextSend is null. SelectTransport then sends an end-to-end stats packet through
SNP_SendPacket, which only asserts m_bCryptKeysValid (compiled out in release) and dereferences the null
context.

Field crash (release build, Windows 10): access violation at SNP_SendPacket+0x26d, mov ecx,[this+0x16d4]; call [eax+4] with ecx = 0, right after two "Assertion Failed: BCryptKeysValid()" spews from
SetRendezvousCommonFieldsAndSendSignal and the application closing and re-creating that connection. The
minidump stack is the service thread: Think -> P2P ThinkConnection -> ThinkSelectTransport -> SelectTransport ->
SendEndToEndStatsMsg -> SNP_SendPacket. Happy to share the dump.

The fix treats missing keys like a missing transport: assert and drop the packet. DecryptDataChunk already
guards the receive side the same way.

A P2P transport can be selected while the connection still owes its
ConnectOK and has no derived keys, so SNP_SendPacket encrypts with a null
context. Drop the packet instead, as DecryptDataChunk already does on
receive.
zpostfacto pushed a commit that referenced this pull request Nov 24, 2026
A field crash dump shows a remotely initiated P2P connection selecting a
transport and sending end-to-end stats while it still owed its ConnectOK
and had no crypto keys, so SNP_SendPacket encrypted with a null send
context.  Stack: ThinkSelectTransport -> SelectTransport ->
SendEndToEndStatsMsg -> SNP_SendPacket, preceded by BCryptKeysValid()
asserts in SetRendezvousCommonFieldsAndSendSignal, right after the app
closed and re-created the connection.

We don't know yet how the connection gets into that state.  For now,
treat missing keys like a missing transport: assert and drop the packet,
as DecryptDataChunk already does on receive.

From PR #472.
@zpostfacto

Copy link
Copy Markdown
Contributor

Thanks, Bill! Merged as 6dd82fb, with a tweaked commit message.

The guard is good defensive coding and we're happy to have it, but I don't think it's the whole story. m_eNegotiatedCipher is only ever set in FinishCryptoHandshake, in the same place the keys become valid, so the peer's crypt message can't set the cipher early. Your crash doesn't need that anyway: with the cipher still INVALID, SNP_SendPacket takes the encrypt branch and calls through the null context, which matches what you saw. So there's still a state bug somewhere that lets a remotely initiated connection be active, owe its ConnectOK, and have no keys. Your note that it happened right after the app closed and re-created the connection is probably the best lead.

I'd love to take you up on the offer of the dump. If you can, please also send:

  • The library version (GNS release or commit, or Steam client build if you're using the Steamworks version)
  • Any logs from around the time of the crash, especially with P2P rendezvous logging turned up (k_ESteamNetworkingConfig_LogLevel_P2PRendezvous)
  • How the app closes and re-creates the connection: same remote identity? How soon after closing? Is the new one created locally with ConnectP2P, or accepted from the peer?

@zpostfacto zpostfacto closed this Nov 24, 2026
zpostfacto pushed a commit that referenced this pull request Nov 24, 2026
A field crash dump shows a remotely initiated P2P connection selecting a
transport and sending end-to-end stats while it still owed its ConnectOK
and had no crypto keys, so SNP_SendPacket encrypted with a null send
context.  Stack: ThinkSelectTransport -> SelectTransport ->
SendEndToEndStatsMsg -> SNP_SendPacket, preceded by BCryptKeysValid()
asserts in SetRendezvousCommonFieldsAndSendSignal, right after the app
closed and re-created the connection.

We don't know yet how the connection gets into that state.  For now,
treat missing keys like a missing transport: assert and drop the packet,
as DecryptDataChunk already does on receive.

From PR #472.

(cherry picked from commit 6dd82fb)
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