·Sun自己也會犯錯: 我非常尊敬Sun的工程師。然而,儘管他們都是很有天賦的人,但還是如你我一樣會犯錯誤。 在2月23號,Sun宣布它發現了JDK中的一處漏洞。下面是他們的聲明: 「在某些Java運行時版本中的漏洞可能會允許惡意Java代碼執行未經授權的命令。然而,惡意命令代碼一定已經取得了代碼執行的授權。」 如果這種代碼在某種環境下被給予執行至少一條可執行命令(如echo),這個漏洞會允許不被信任的Java代碼激活任何可執行命令(如格式化FORMAT)。這樣的錯誤很可能隱藏很久而不被發現。 下面就讓我們來看看具體的代碼:這個漏洞是位於java.lang.Runtime類里的exec()方法中: public Process exec(String [] arstringCommand, String [] arstringEnvironment) throws IOException { // Ensure that the array parameters aren't null, their elements // aren't null, etc. . . . // Do some stuff. . . . // Get the security manager. SecurityManager securitymanager = System.getSecurityManager(); // Check the first element of the command array -- which should // be the name of the executable to invoke. Ensure that it has // executable privilege. if (securitymanager != null) securitymanager.checkExec(arstringCommand[0]); // Now, invoke the executable. return execInternal(arstringCommand, arstringEnvironment); } 你看出問題了嗎? 這個錯誤位於最後三行中(註釋和空格除外)。首先,安全管理器檢查可執行名,看其是否在配置文件中有執行的授權。接下來,代碼執行命令。哎喲!在一個多線程環境中,參數數組內容在這兩步之間就可以改變。由於這兩個輸入參數數組被直接使用,調用者仍然掌握著它們的引用,並且可以修改其內容。 更正:立即複製輸入數組並在拷貝中進行操作。