Reentrancy Guard Patterns
Reentrancy guard patterns are coding structures designed to prevent an attacker from repeatedly calling a function before the previous execution has finished. This vulnerability occurs when a contract makes an external call to an untrusted contract before updating its own state.
The attacker can use the external call to re-enter the function and manipulate balances or collateral before the initial transaction is complete. A reentrancy guard uses a boolean flag or a modifier to lock the function, ensuring that no further calls can be made until the first one returns.
This is a mandatory security pattern for any contract that manages ether or tokens. In the world of derivatives, where funds are frequently moved between protocols, reentrancy guards are the primary defense against drain attacks.
They enforce a sequential execution flow that protects the protocol's internal balance sheets.