界: Security Features
軟體安全性並非安全性軟體。我們關注驗證、Access Control、保密性、加密以及權限管理之類的主題。
Authorization Bypass: tx.origin
Abstract
函數將全域變數
tx.origin
用於授權目的。Explanation
tx.origin
全域變數會保存發起交易的帳戶位址。如果智慧合約 S1 從帳戶 A1 接收交易,然後 S1 呼叫另一個智慧合約 S2,則在 S2 內部,
tx.origin
會包含帳戶 A1 的位址,以用於呼叫 S1。如果 tx.origin
的意圖是驗證 A1 的授權,則此授權會被略過。現在,如果攻擊者可以誘騙使用者將交易傳送到惡意合約中,以供其叫用易受攻擊的合約,其中使用者透過
tx.origin
獲得授權,則 tx.origin
將保存發起交易的使用者帳戶的位址,而授權將被略過。範例 1:以下程式碼要求合約擁有者 (先前設定於建構函數中) 須與
tx.origin
相同,然後才能將資金轉移到提供的位址。如果攻擊者能誘騙合約擁有者傳送交易到惡意合約,該惡意合約會立即呼叫易受攻擊的合約中的
sendTo
函數,此時 require
陳述式中的條件將為 true,並且資金會被轉移到攻擊者合約在呼叫 sendTo
時指定的任何位址。
function sendTo(address receiver, uint amount) public {
require(tx.origin == owner);
receiver.transfer(amount);
}
References
[1] Enterprise Ethereum Alliance No tx.origin
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 4
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 477
[7] Standards Mapping - Smart Contract Weakness Classification SWC-115
desc.structural.solidity.swc115