From 1df11caf71ed8b70b6f19fe3a089193f5b2bd2f4 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 Dec 2016 22:14:50 -0800 Subject: [PATCH] peer: lock fixes. dos fixes. --- lib/net/peer.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/net/peer.js b/lib/net/peer.js index 8ae197bf..864430e5 100644 --- a/lib/net/peer.js +++ b/lib/net/peer.js @@ -995,18 +995,13 @@ Peer.prototype.sendRaw = function sendRaw(cmd, body, checksum) { */ Peer.prototype.error = function error(err) { - var i, args, msg; + var i, msg; if (this.destroyed) return; if (typeof err === 'string') { - args = new Array(arguments.length); - - for (i = 0; i < args.length; i++) - args[i] = arguments[i]; - - msg = util.fmt.apply(util, args); + msg = util.fmt.apply(util, arguments); err = new Error(msg); } @@ -1029,7 +1024,7 @@ Peer.prototype.request = function request(cmd) { var entry; if (self.destroyed) - return reject(new Error('Destroyed')); + return reject(new Error('Request destroyed.')); entry = new RequestEntry(self, cmd, resolve, reject); @@ -1134,10 +1129,8 @@ Peer.prototype.handlePacket = co(function* handlePacket(packet) { case packetTypes.GETUTXOS: case packetTypes.GETBLOCKTXN: unlock = yield this.locker.lock(); - - this.socket.pause(); - try { + this.socket.pause(); return yield this.onPacket(packet); } finally { this.socket.resume(); @@ -1342,6 +1335,15 @@ Peer.prototype.handleFilterClear = co(function* handleFilterClear(packet) { Peer.prototype.handleMerkleBlock = co(function* handleMerkleBlock(packet) { var block = packet.block; + // Potential DoS. + if (!this.options.spv) { + this.logger.warning( + 'Peer sent unsolicited merkleblock (%s).', + this.hostname); + this.increaseBan(100); + return; + } + block.verifyPartial(); this.lastMerkle = block; @@ -2153,6 +2155,13 @@ Peer.prototype.handleSendHeaders = co(function* handleSendHeaders(packet) { */ Peer.prototype.handleBlock = co(function* handleBlock(packet) { + if (this.options.spv) { + this.logger.warning( + 'Peer sent unsolicited block (%s).', + this.hostname); + return; + } + this.fire('block', packet.block); });