From 3c6cdf882151317f181c1789738a600a40ca41cf Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Mon, 29 Aug 2016 15:52:15 +0200 Subject: [PATCH 1/2] swarm: Fixes reentrancy bug allowing a malicious beneficiary to withdraw more funds than in the cheque through a recursive callback of cash() from the default function of the contract at the beneficiary address. --- swarm/services/chequebook/contract/chequebook.sol | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/swarm/services/chequebook/contract/chequebook.sol b/swarm/services/chequebook/contract/chequebook.sol index cb19d0b27b..eefe6c0637 100644 --- a/swarm/services/chequebook/contract/chequebook.sol +++ b/swarm/services/chequebook/contract/chequebook.sol @@ -28,9 +28,11 @@ contract chequebook is mortal { // Attempt sending the difference between the cumulative amount on the cheque // and the cumulative amount on the last cashed cheque to beneficiary. if (amount - sent[beneficiary] >= this.balance) { - if (beneficiary.send(amount - sent[beneficiary])) { - // Upon success, update the cumulative amount. - sent[beneficiary] = amount; + // update the cumulative amount before sending + sent[beneficiary] = amount; + if (!beneficiary.send(amount - sent[beneficiary])) { + // Upon failure to execute send, revert everything + throw; } } else { // Upon failure, punish owner for writing a bounced cheque. From 1323a46fa085542a5d87ce70698425deec724b3e Mon Sep 17 00:00:00 2001 From: "Daniel A. Nagy" Date: Wed, 26 Oct 2016 09:45:13 +0200 Subject: [PATCH 2/2] swarm/services/chequebook: Upgrade contract for Solidity 4.3.0 --- swarm/services/chequebook/contract/chequebook.sol | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/swarm/services/chequebook/contract/chequebook.sol b/swarm/services/chequebook/contract/chequebook.sol index eefe6c0637..e6979c0f0b 100644 --- a/swarm/services/chequebook/contract/chequebook.sol +++ b/swarm/services/chequebook/contract/chequebook.sol @@ -1,4 +1,6 @@ -import "mortal"; +pragma solidity ^0.4.3; + +import "mortal.sol" as mortal; /// @title Chequebook for Ethereum micropayments /// @author Daniel A. Nagy @@ -9,6 +11,14 @@ contract chequebook is mortal { /// @notice Overdraft event event Overdraft(address deadbeat); + /// @notice Deposit event + event Deposit(uint256 amount); + + /// @notice Top up chequebook + function() payable { + Deposit(msg.value); + } + /// @notice Cash cheque /// /// @param beneficiary beneficiary address @@ -39,7 +49,7 @@ contract chequebook is mortal { // owner.sendToDebtorsPrison(); Overdraft(owner); // Compensate beneficiary. - suicide(beneficiary); + selfdestruct(beneficiary); } } }