安全管理員操作指南
目錄
背景
Java SecurityManager 允許網路瀏覽器在自己的沙箱中執行小程式,以防止不受信任的程式碼存取本機檔案系統上的檔案、連線到小程式載入來源以外的主機,等等。SecurityManager 可以保護您免於瀏覽器中執行不受信任的小程式,同樣地,在執行 Tomcat 時使用 SecurityManager 可以保護您的伺服器免於木馬程式、JSP、JSP bean 和標籤程式庫的攻擊,甚至可以避免無意的錯誤。
想像一下,如果一位被授權在您的網站上發布 JSP 的人無意間在他們的 JSP 中包含以下內容
<% System.exit(1); %>
每次 Tomcat 執行這個 JSP,Tomcat 都會結束。使用 Java SecurityManager 只是系統管理員用來維持伺服器安全和可靠的另一道防線。
警告 - 已使用 Tomcat 程式碼庫進行安全性稽核。大部分關鍵套件都已受到保護,並且已實作新的安全性套件保護機制。儘管如此,在允許不受信任的使用者發布網路應用程式、JSP、servlet、bean 或標籤程式庫之前,請務必確認您滿意 SecurityManager 組態。不過,執行 SecurityManager 絕對比不執行來得好。
已知問題
從 Java 17 開始,SecurityManager 已被標示為不建議使用,預計將在未來的 Java 版本中移除。目前使用 SecurityManager 的使用者建議開始規劃移除事宜。
權限
權限類別用於定義 Tomcat 載入的類別將擁有的權限。有許多權限類別是 JDK 的標準部分,您也可以建立自己的權限類別供您自己的網路應用程式使用。Tomcat 中使用了這兩種技術。
標準權限
這只是適用於 Tomcat 的標準系統 SecurityManager 權限類別的簡短摘要。請參閱 http://docs.oracle.com/javase/7/docs/technotes/guides/security/ 以取得更多資訊。
- java.util.PropertyPermission - 控制對 JVM 屬性的讀取/寫入存取,例如
java.home
。 - java.lang.RuntimePermission - 控制使用某些系統/執行時期函數,例如
exit()
和exec()
。也控制套件存取/定義。 - java.io.FilePermission - 控制對檔案和目錄的讀取/寫入/執行存取。
- java.net.SocketPermission - 控制使用網路 socket。
- java.net.NetPermission - 控制使用多播網路連線。
- java.lang.reflect.ReflectPermission - 控制使用反射進行類別內省。
- java.security.SecurityPermission - 控制對安全性方法的存取。
- java.security.AllPermission - 允許存取所有權限,就像您在沒有 SecurityManager 的情況下執行 Tomcat 一樣。
使用 SecurityManager 設定 Tomcat
政策檔案格式
Java SecurityManager 實作的安全性政策會在 $CATALINA_BASE/conf/catalina.policy
檔案中設定。此檔案會完全取代 JDK 系統目錄中現有的 java.policy
檔案。
catalina.policy
檔案中的項目使用標準的 java.policy
檔案格式,如下所示
// Example policy file entry
grant [signedBy <signer>,] [codeBase <code source>] {
permission <class> [<name> [, <action list>]];
};
授予權限時,signedBy 和 codeBase 項目是選用的。註解行以 "//" 開頭,並在當前行的結尾結束。codeBase
的格式為 URL,對於檔案 URL,可以使用 ${java.home}
和 ${catalina.home}
屬性(它們會擴充為由 JAVA_HOME
、CATALINA_HOME
和 CATALINA_BASE
環境變數為它們定義的目錄路徑)。
預設政策檔案
預設的 $CATALINA_BASE/conf/catalina.policy
檔案如下所示
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ============================================================================
// catalina.policy - Security Policy Permissions for Tomcat
//
// This file contains a default set of security policies to be enforced (by the
// JVM) when Catalina is executed with the "-security" option. In addition
// to the permissions granted here, the following additional permissions are
// granted to each web application:
//
// * Read access to the web application's document root directory
// * Read, write and delete access to the web application's working directory
// ============================================================================
// ========== SYSTEM CODE PERMISSIONS =========================================
// These permissions apply to javac
grant codeBase "file:${java.home}/lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions
grant codeBase "file:${java.home}/jre/lib/ext/-" {
permission java.security.AllPermission;
};
// These permissions apply to javac when ${java.home} points at $JAVA_HOME/jre
grant codeBase "file:${java.home}/../lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions when
// ${java.home} points at $JAVA_HOME/jre
grant codeBase "file:${java.home}/lib/ext/-" {
permission java.security.AllPermission;
};
// This permission is required when using javac to compile JSPs
grant codeBase "jrt:/jdk.compiler" {
permission java.security.AllPermission;
};
// ========== CATALINA CODE PERMISSIONS =======================================
// These permissions apply to the daemon code
grant codeBase "file:${catalina.home}/bin/commons-daemon.jar" {
permission java.security.AllPermission;
};
// These permissions apply to the logging API
// Note: If tomcat-juli.jar is in ${catalina.base} and not in ${catalina.home},
// update this section accordingly.
// grant codeBase "file:${catalina.base}/bin/tomcat-juli.jar" {..}
grant codeBase "file:${catalina.home}/bin/tomcat-juli.jar" {
permission java.io.FilePermission
"${java.home}${file.separator}lib${file.separator}logging.properties", "read";
permission java.io.FilePermission
"${catalina.base}${file.separator}conf${file.separator}logging.properties", "read";
permission java.io.FilePermission
"${catalina.base}${file.separator}logs", "read, write";
permission java.io.FilePermission
"${catalina.base}${file.separator}logs${file.separator}*", "read, write, delete";
permission java.lang.RuntimePermission "shutdownHooks";
permission java.lang.RuntimePermission "getClassLoader";
permission java.lang.RuntimePermission "setContextClassLoader";
permission java.lang.management.ManagementPermission "monitor";
permission java.util.logging.LoggingPermission "control";
permission java.util.PropertyPermission "java.util.logging.config.class", "read";
permission java.util.PropertyPermission "java.util.logging.config.file", "read";
permission java.util.PropertyPermission "org.apache.juli.AsyncMaxRecordCount", "read";
permission java.util.PropertyPermission "org.apache.juli.AsyncOverflowDropType", "read";
permission java.util.PropertyPermission "org.apache.juli.ClassLoaderLogManager.debug", "read";
permission java.util.PropertyPermission "catalina.base", "read";
// Note: To enable per context logging configuration, permit read access to
// the appropriate file. Be sure that the logging configuration is
// secure before enabling such access.
// E.g. for the examples web application (uncomment and unwrap
// the following to be on a single line):
// permission java.io.FilePermission "${catalina.base}${file.separator}
// webapps${file.separator}examples${file.separator}WEB-INF
// ${file.separator}classes${file.separator}logging.properties", "read";
};
// These permissions apply to the server startup code
grant codeBase "file:${catalina.home}/bin/bootstrap.jar" {
permission java.security.AllPermission;
};
// These permissions apply to the servlet API classes
// and those that are shared across all class loaders
// located in the "lib" directory
grant codeBase "file:${catalina.home}/lib/-" {
permission java.security.AllPermission;
};
// If using a per instance lib directory, i.e. ${catalina.base}/lib,
// then the following permission will need to be uncommented
// grant codeBase "file:${catalina.base}/lib/-" {
// permission java.security.AllPermission;
// };
// ========== WEB APPLICATION PERMISSIONS =====================================
// These permissions are granted by default to all web applications
// In addition, a web application will be given a read FilePermission
// for all files and directories in its document root.
grant {
// Required for JNDI lookup of named JDBC DataSource's and
// javamail named MimePart DataSource used to send mail
permission java.util.PropertyPermission "java.home", "read";
permission java.util.PropertyPermission "java.naming.*", "read";
permission java.util.PropertyPermission "javax.sql.*", "read";
// OS Specific properties to allow read access
permission java.util.PropertyPermission "os.name", "read";
permission java.util.PropertyPermission "os.version", "read";
permission java.util.PropertyPermission "os.arch", "read";
permission java.util.PropertyPermission "file.separator", "read";
permission java.util.PropertyPermission "path.separator", "read";
permission java.util.PropertyPermission "line.separator", "read";
// JVM properties to allow read access
permission java.util.PropertyPermission "java.version", "read";
permission java.util.PropertyPermission "java.vendor", "read";
permission java.util.PropertyPermission "java.vendor.url", "read";
permission java.util.PropertyPermission "java.class.version", "read";
permission java.util.PropertyPermission "java.specification.version", "read";
permission java.util.PropertyPermission "java.specification.vendor", "read";
permission java.util.PropertyPermission "java.specification.name", "read";
permission java.util.PropertyPermission "java.vm.specification.version", "read";
permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
permission java.util.PropertyPermission "java.vm.specification.name", "read";
permission java.util.PropertyPermission "java.vm.version", "read";
permission java.util.PropertyPermission "java.vm.vendor", "read";
permission java.util.PropertyPermission "java.vm.name", "read";
// Required for OpenJMX
permission java.lang.RuntimePermission "getAttribute";
// Allow read of JAXP compliant XML parser debug
permission java.util.PropertyPermission "jaxp.debug", "read";
// All JSPs need to be able to read this package
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.tomcat";
// Precompiled JSPs need access to these packages.
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.jasper.el";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.jasper.runtime";
permission java.lang.RuntimePermission
"accessClassInPackage.org.apache.jasper.runtime.*";
// Applications using WebSocket need to be able to access these packages
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.tomcat.websocket";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.tomcat.websocket.server";
};
// The Manager application needs access to the following packages to support the
// session display functionality. It also requires the custom Tomcat
// DeployXmlPermission to enable the use of META-INF/context.xml
// These settings support the following configurations:
// - default CATALINA_HOME == CATALINA_BASE
// - CATALINA_HOME != CATALINA_BASE, per instance Manager in CATALINA_BASE
// - CATALINA_HOME != CATALINA_BASE, shared Manager in CATALINA_HOME
grant codeBase "file:${catalina.base}/webapps/manager/-" {
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.ha.session";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.manager";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.manager.util";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.util";
permission org.apache.catalina.security.DeployXmlPermission "manager";
};
grant codeBase "file:${catalina.home}/webapps/manager/-" {
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.ha.session";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.manager";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.manager.util";
permission java.lang.RuntimePermission "accessClassInPackage.org.apache.catalina.util";
permission org.apache.catalina.security.DeployXmlPermission "manager";
};
// The Host Manager application needs the custom Tomcat DeployXmlPermission to
// enable the use of META-INF/context.xml
// These settings support the following configurations:
// - default CATALINA_HOME == CATALINA_BASE
// - CATALINA_HOME != CATALINA_BASE, per instance Host Manager in CATALINA_BASE
// - CATALINA_HOME != CATALINA_BASE, shared Host Manager in CATALINA_HOME
grant codeBase "file:${catalina.base}/webapps/host-manager/-" {
permission org.apache.catalina.security.DeployXmlPermission "host-manager";
};
grant codeBase "file:${catalina.home}/webapps/host-manager/-" {
permission org.apache.catalina.security.DeployXmlPermission "host-manager";
};
// You can assign additional permissions to particular web applications by
// adding additional "grant" entries here, based on the code base for that
// application, /WEB-INF/classes/, or /WEB-INF/lib/ jar files.
//
// Different permissions can be granted to JSP pages, classes loaded from
// the /WEB-INF/classes/ directory, all jar files in the /WEB-INF/lib/
// directory, or even to individual jar files in the /WEB-INF/lib/ directory.
//
// For instance, assume that the standard "examples" application
// included a JDBC driver that needed to establish a network connection to the
// corresponding database and used the scrape taglib to get the weather from
// the NOAA web server. You might create a "grant" entries like this:
//
// The permissions granted to the context root directory apply to JSP pages.
// grant codeBase "file:${catalina.base}/webapps/examples/-" {
// permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
// permission java.net.SocketPermission "*.noaa.gov:80", "connect";
// };
//
// The permissions granted to the context WEB-INF/classes directory
// grant codeBase "file:${catalina.base}/webapps/examples/WEB-INF/classes/-" {
// };
//
// The permission granted to your JDBC driver
// grant codeBase "jar:file:${catalina.base}/webapps/examples/WEB-INF/lib/driver.jar!/-" {
// permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
// };
// The permission granted to the scrape taglib
// grant codeBase "jar:file:${catalina.base}/webapps/examples/WEB-INF/lib/scrape.jar!/-" {
// permission java.net.SocketPermission "*.noaa.gov:80", "connect";
// };
// To grant permissions for web applications using packed WAR files, use the
// Tomcat specific WAR url scheme.
//
// The permissions granted to the entire web application
// grant codeBase "war:file:${catalina.base}/webapps/examples.war*/-" {
// };
//
// The permissions granted to a specific JAR
// grant codeBase "war:file:${catalina.base}/webapps/examples.war*/WEB-INF/lib/foo.jar" {
// };
使用 SecurityManager 啟動 Tomcat
一旦您設定了 catalina.policy
檔案以與 SecurityManager 搭配使用,就可以使用 "-security" 選項,在 SecurityManager 就緒的情況下啟動 Tomcat
$CATALINA_HOME/bin/catalina.sh start -security (Unix)
%CATALINA_HOME%\bin\catalina start -security (Windows)
封裝 WAR 檔案的權限
使用封裝的 WAR 檔案時,有必要使用 Tomcat 的自訂 war URL 協定,以將權限指定給 Web 應用程式程式碼。
若要將權限指定給整個 Web 應用程式,政策檔案中的項目會如下所示
// Example policy file entry
grant codeBase "war:file:${catalina.base}/webapps/examples.war*/-" {
...
};
若要將權限指定給 Web 應用程式中的單一 JAR,政策檔案中的項目會如下所示
// Example policy file entry
grant codeBase "war:file:${catalina.base}/webapps/examples.war*/WEB-INF/lib/foo.jar" {
...
};
在 Tomcat 中設定套件保護
從 Tomcat 5 開始,現在可以設定哪些 Tomcat 內部套件受到套件定義和存取的保護。請參閱 http://www.oracle.com/technetwork/java/seccodeguide-139067.html 以取得更多資訊。
警告:請注意,移除預設套件保護可能會開啟一個安全漏洞
預設屬性檔案
預設的 $CATALINA_BASE/conf/catalina.properties
檔案如下所示
#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted.
package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,
org.apache.jasper.
#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageDefinition unless the
# corresponding RuntimePermission ("defineClassInPackage."+package) has
# been granted.
#
# by default, no packages are restricted for definition, and none of
# the class loaders supplied with the JDK call checkPackageDefinition.
#
package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,
org.apache.tomcat.,org.apache.jasper.
一旦您設定了 catalina.properties
檔案以與 SecurityManager 搭配使用,請記得重新啟動 Tomcat。
疑難排解
如果您的 Web 應用程式嘗試執行受限於缺乏必要權限的作業,它會在 SecurityManager 偵測到違規時擲出 AccessControLException
或 SecurityException
。除錯遺失的權限可能具有挑戰性,其中一個選項是在執行期間開啟所有安全決策的除錯輸出。這是在啟動 Tomcat 之前設定系統屬性來完成的。執行此命令的最簡單方法是透過 CATALINA_OPTS
環境變數。在啟動 Tomcat 之前執行此命令。
export CATALINA_OPTS=-Djava.security.debug=all (Unix)
set CATALINA_OPTS=-Djava.security.debug=all (Windows)
在啟動 Tomcat 之前。
警告 - 這將產生數百萬位元組的輸出!然而,它可以協助您追蹤問題,方法是搜尋「FAILED」一詞並判斷檢查了哪些權限。請參閱 Java 安全文件,以瞭解您還可以在此處指定的更多選項。