Fix possible AssertionError in ByteRange due to invalid access

This commit is contained in:
kdeme 2019-10-10 14:40:40 +02:00
parent 903b72a5c8
commit 9ee208876d
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
2 changed files with 22 additions and 9 deletions

View File

@ -460,10 +460,14 @@ proc waitSingleMsg(peer: Peer, MsgType: type): Future[MsgType] {.async.} =
"Invalid RLPx message body")
elif nextMsgId == 1: # p2p.disconnect
if nextMsgData.isList():
let reason = DisconnectionReason nextMsgData.listElem(0).toInt(uint32)
await peer.disconnect(reason)
trace "disconnect message received in waitSingleMsg", reason, peer
raisePeerDisconnected("Unexpected disconnect", reason)
else:
raise newException(RlpTypeMismatch,
"List expected, but the source RLP is not a list.")
else:
warn "Dropped RLPX message",
msg = peer.dispatcher.messages[nextMsgId].name
@ -515,11 +519,17 @@ proc dispatchMessages*(peer: Peer) {.async.} =
except PeerDisconnected:
return
# TODO: is there a reason why we don't just allow `sendDisconnectMsg`
# to do its work here, with the usual thunk checks included?
if msgId == 1: # p2p.disconnect
if msgData.isList():
let reason = msgData.listElem(0).toInt(uint32).DisconnectionReason
trace "disconnect message received in dispatchMessages", reason, peer
await peer.disconnect(reason, false)
break
else:
debug "disconnect message with invalid rlp list", peer
await peer.disconnect(BreachOfProtocol, true)
try:
await peer.invokeThunk(msgId, msgData)
@ -784,7 +794,9 @@ p2pProtocol devp2p(version = 0, rlpxName = "p2p"):
listenPort: uint,
nodeId: array[RawPublicKeySize, byte])
proc sendDisconnectMsg(peer: Peer, reason: DisconnectionReason)
proc sendDisconnectMsg(peer: Peer, reason: DisconnectionReason) =
trace "disconnect message received", reason, peer
await peer.disconnect(reason, false)
# Adding an empty RLP list as the spec defines.
# The parity client specifically checks if there is rlp data.

View File

@ -272,6 +272,7 @@ iterator items*(self: var Rlp): var Rlp =
position = elemEnd
proc listElem*(self: Rlp, i: int): Rlp =
doAssert isList()
let
payloadOffset = payloadOffset()