Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nil dereference during handshake packet decoding #1379

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions p2p/discover/v5wire/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,9 @@ func (c *Codec) decodeWhoareyou(head *Header, headerData []byte) (Packet, error)
func (c *Codec) decodeHandshakeMessage(fromAddr string, head *Header, headerData, msgData []byte) (n *enode.Node, p Packet, err error) {
node, auth, session, err := c.decodeHandshake(fromAddr, head)
if err != nil {
c.sc.deleteHandshake(auth.h.SrcID, fromAddr)
if auth != nil {
c.sc.deleteHandshake(auth.h.SrcID, fromAddr)
}
return nil, nil, err
}

Expand All @@ -556,39 +558,40 @@ func (c *Codec) decodeHandshakeMessage(fromAddr string, head *Header, headerData
return node, msg, nil
}

func (c *Codec) decodeHandshake(fromAddr string, head *Header) (n *enode.Node, auth handshakeAuthData, s *session, err error) {
if auth, err = c.decodeHandshakeAuthData(head); err != nil {
return nil, auth, nil, err
func (c *Codec) decodeHandshake(fromAddr string, head *Header) (n *enode.Node, auth *handshakeAuthData, s *session, err error) {
tempAuth := &handshakeAuthData{}
if *tempAuth, err = c.decodeHandshakeAuthData(head); err != nil {
return nil, nil, nil, err
}

// Verify against our last WHOAREYOU.
challenge := c.sc.getHandshake(auth.h.SrcID, fromAddr)
challenge := c.sc.getHandshake(tempAuth.h.SrcID, fromAddr)
if challenge == nil {
return nil, auth, nil, errUnexpectedHandshake
return nil, nil, nil, errUnexpectedHandshake
}
// Get node record.
n, err = c.decodeHandshakeRecord(challenge.Node, auth.h.SrcID, auth.record)
n, err = c.decodeHandshakeRecord(challenge.Node, tempAuth.h.SrcID, tempAuth.record)
if err != nil {
return nil, auth, nil, err
return nil, nil, nil, err
}
// Verify ID nonce signature.
sig := auth.signature
sig := tempAuth.signature
cdata := challenge.ChallengeData

err = verifyIDSignature(c.sha256, sig, n, cdata, auth.pubkey, c.localnode.ID())
err = verifyIDSignature(c.sha256, sig, n, cdata, tempAuth.pubkey, c.localnode.ID())
if err != nil {
return nil, auth, nil, err
return nil, nil, nil, err
}
// Verify ephemeral key is on curve.
ephkey, err := DecodePubkey(c.privkey.Curve, auth.pubkey)
// Verify ephemeral key is on curve
ephkey, err := DecodePubkey(c.privkey.Curve, tempAuth.pubkey)
if err != nil {
return nil, auth, nil, errInvalidAuthKey
return nil, nil, nil, errInvalidAuthKey
}
// Derive session keys.
session := deriveKeys(sha256.New, c.privkey, ephkey, auth.h.SrcID, c.localnode.ID(), cdata)
session := deriveKeys(sha256.New, c.privkey, ephkey, tempAuth.h.SrcID, c.localnode.ID(), cdata)
session = session.keysFlipped()

return n, auth, session, nil
return n, tempAuth, session, nil
}

// decodeHandshakeAuthData reads the authdata section of a handshake packet.
Expand Down
Loading