Q&A-24Walk me through how an authorization check that uses tx.origin instead of msg.sender can be exploited, and show how the check should be written instead.
Answer
Start with the call-chain distinction. tx.origin is the EOA that signed and sent the transaction; it stays constant for the whole transaction. msg.sender is the immediate caller of the current call frame. In A -> B -> C, C sees tx.origin == A and msg.sender == B. Authorization must be based on msg.sender because that is the identity you can verify at the point of the check; tx.origin is just the transaction's originator and can be an EOA that was tricked into starting the chain.
The exploit: a wallet contract has require(tx.origin == owner). An attacker deploys a phishing contract with a function that calls the wallet's withdraw to the attacker. The attacker gets the owner to call the phishing contract (fake airdrop claim, malicious dapp, a signed message that triggers a call). Inside that call, the phishing contract calls the wallet. At the wallet, tx.origin is still the owner's EOA, so the check passes, but msg.sender is the phishing contract. Funds leave.
Trace:
owner EOA -> Phishing.claim() -> Wallet.withdraw(attacker)
At Wallet: tx.origin = owner, msg.sender = Phishing. tx.origin == owner is true. msg.sender == owner would be false.
The fix is require(msg.sender == owner). Better, use a role-based modifier: OpenZeppelin AccessControl's onlyRole(WITHDRAWER_ROLE) or Ownable's onlyOwner, both of which check msg.sender. If you need multiple roles, grant them explicitly and check the role in the modifier. Never use tx.origin for authorization.
Why msg.sender is correct: it is the address that directly invoked the function. The EVM sets it to the caller and an intermediate contract cannot spoof it. tx.origin can be an EOA that is not the intended authorizer, and it breaks under account abstraction (ERC-4337) where tx.origin may be a bundler, and under meta-transactions where a relayer submits. It also breaks multisig: if a 3-of-5 multisig calls a contract with a tx.origin check, any single signer who initiates the transaction passes the check, bypassing the threshold.
Failure modes to name: phishing, malicious intermediate contract, compromised front-end, multisig threshold bypass, account abstraction incompatibility, meta-transaction relayers. Detection: Slither's tx-origin detector, manual review of every require on tx.origin, and a test that routes a privileged call through an attacker contract and asserts it reverts.
solidity // vulnerable contract Wallet { address public owner; function withdraw(address to, uint256 amount) external { require(tx.origin == owner, "not owner"); payable(to).transfer(amount); } } // fixed import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; contract Wallet is Ownable { function withdraw(address to, uint256 amount) external onlyOwner { payable(to).transfer(amount); } }
Checked against Solidity 0.8.24 and OpenZeppelin Contracts 5.x.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.