Java provides several APIs that allow external programs to monitor a running Java program. These APIs also permit the Java program to be monitored remotely by programs on distinct hosts. Such features are convenient for debugging the program or fine-tuning its performance. However, if a Java program is deployed in production with remote monitoring enabled, an attacker can connect to the Java Virtual Machine (JVM) and inspect its behavior and data, including potentially sensitive information. An attacker can also exert control over the program's behavior. Consequently, remote monitoring must be disabled when running a Java program in production.
JVM Tool Interface (JVMTI)
Java 5 introduced the JVM Tool Interface (JVMTI) [Sun 04d], replacing both the JVM Profiler Interface (JVMPI) and the JVM Debug Interface (JVMDI), which are now deprecated.
...
The JVMTI works by using agents that communicate with the running JVM. These agents must be loaded at JVM startup and are usually specified via one of the command-line options -agentlib
or -agentpath
. However, agents can be specified in environment variables, although this feature can be disabled where security is a concern. The JVMTI is always enabled, and JVMTI agents may run under the default security manager without requiring any permissions to be granted.
Java Platform Debugger Architecture (JPDA)
The Java Platform Debugger Architecture (JPDA) builds on the JVMTI and provides high-level facilities for debugging Java systems while they are running [JPDA 2004].
The JPDA facilities are similar to the reflection API, which is described in rule SEC05-J. Do not use reflection to increase accessibility of classes, methods, or fields. In particular, the JPDA provides methods to get and set field and array values. Access control is not enforced, so that even the values of private fields can be set by a remote process via the JPDA.
...
Because JPDA supports remote debugging, a remote host can access the debugger. An attacker can exploit this feature to study sensitive information or modify the behavior of a running Java application unless appropriate protection is enabled. A security manager can ensure that only known, trusted hosts are given permissions to use the debugger interface.
Java SE Monitoring and Management Features
Java contains extensive facilities for monitoring and managing a JVM [JMX 2006]. In particular, the Java Management Extension (JMX) API enables the monitoring and control of class loading, thread state and stack traces, deadlock detection, memory usage, garbage collection, operating system information, and other operations [Sun 04a]. It also has facilities for logging monitoring and management.
The Java SE monitoring and management features fall into four broad categories:
- The JMX technology: This technology serves as the underlying interface for local and remote monitoring and management.
- Instrumentation for the JVM: These facilities enable out-of-the-box monitoring and management of the JVM and are based on the JMX specification.
- Monitoring and management API: These facilities use the
java.lang.management
package to provide the monitoring and management interface. Applications can use this package to monitor themselves or to let JMX technology-compliant technology–compliant tools monitor and manage them. - Monitoring and management tools: Tools such as JConsole implement the JMX interface to provide monitoring and management facilities.
...
The JVM remote monitoring and management facility uses a secure communication channel (Secure Sockets Layer [SSL]) by default. However, if an attacker can start a bogus remote method invocation (RMI) registry server on the monitored machine before the legitimate RMI registry server is started, JMX passwords can be intercepted. Also, SSL can be disabled when using remote monitoring and management, which could, again, compromise security. See The Java SE Monitoring and Management Guide [JMXG 2006] for further details and for mitigation strategies.
There are also provisions to require proper authentication of the remote server. However, users may start a JVM with remote monitoring and management enabled , but with no security; this would leave the JVM open to attack by outsiders. Although accidently accidentally enabling remote monitoring and management is unlikely, users might not realize that starting a JVM so enabled, without any security, could leave their JVM exposed to attack.
If exploited, the monitoring and management facilities can seriously compromise the security of Java applications. For example, an attacker can obtain information about the number of classes loaded and threads running, thread state along with traces of live threads, system properties, VM virtual machine arguments, and memory consumption.
...
Some JVMs allow agents to be started when the JVM is already running. This practice is insecure in a production environment. Refer to the JVMTI documentation [JVMTI 2006] for platform-specific information on enabling/disabling this feature.
...
Agents may run under the default security manager without requiring any permissions to be granted. While Although the JVMTI is useful for debuggers and profilers, such levels of access are inappropriate for deployed production code.
...
This noncompliant code example uses command-line arguments to invoke the JVM so that it can be debugged from a running debugger application by listening for connections using shared memory at transport address mysharedmemory
.:
Code Block | ||
---|---|---|
| ||
${JDK_PATH}/bin/java -agentlib:jdwp=transport=dt_shmem, address=mysharedmemory ApplicationName |
Likewise, the command-line arguments -Xrunjdwp
, (which is equivalent to -agentlib
, ) and -Xdebug
, (which is used by the jdb
tool, ) also enable application debugging.
...
This noncompliant code example invokes the JVM with command-line arguments that permit remote monitoring via port 8000. This invocation may result in a security vulnerability when the password is weak or the SSL protocol is misapplied.
...
Clear the environment variable JAVA_TOOL_OPTIONS
in the manner appropriate for your platform, for example, by setting it to an empty string value. This Doing prevents JVMTI agents from receiving arguments via this mechanism. The command-line argument -Xnoagent
can also be used to disable the debugging features supported by the old Java debugger (oldjdb
).
This compliant solution disables monitoring by remote machines. By default, local monitoring is enabled in Java 6 and later. In earlier versions, the system property com.sun.management.jmxremote
must be set to enable local monitoring. Although the unsupported -XX:+DisableAttachMechanism
command-line option may be used to disable local Java tools from monitoring the JVM, it is always possible to use native debuggers and other tools to perform monitoring. Fortunately, monitoring tools require at least as many privileges as the owner of the JVM process possesses, reducing the threat of a local privilege escalation attack.
Local monitoring uses temporary files and sets the file permissions to those of the owner of the JVM process. Ensure that adequate file protection is in place on the system running the JVM so that the temporary files are accessed appropriately . See rule (see FIO03-J. Remove temporary files before termination for additional information).
The Java SE Monitoring and Management Guide [JMXG 2006] provides further advice:
...
Restrict remote debugging to trusted hosts by modifying the security policy file to grant appropriate permissions only to those trusted hosts. For example, specify the permission java.net.SocketPermission
for only the JPDA host and remove the permission from other hosts.
...
This compliant solution allows the JPDA host to attach to a trusted debugger application.:
Code Block | ||
---|---|---|
| ||
${JDK_PATH}/bin/java -agentlib:jdwp=transport=dt_socket, server=n,address=9000 ApplicationName |
When it is necessary to run a JVM with debugging enabled, avoid granting permissions that are not needed by the application. In particular, avoid granting socket permissions to arbitrary hosts, ; that is, omit the permission java.net.SocketPermission "*", "connect,accept"
.
Exceptions
ENV05-J-EX0: A Java program may be remotely monitored using any of these technologies if it can be guaranteed that no program outside the local trust boundary can access the program. For example, if the program lives on a local network that is both completely trusted and disconnected from any untrusted networks, including the Internet, remote monitoring is permitted.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ENV05-J | highHigh | probableProbable | lowLow | P18 | L1 |
Automated Detection
This rule is not amenable to automated static analysis.
Related Vulnerabilities
CVE-2010-4495 describes a vulnerability in the TIBCO ActiveMatrix product line where in which a flaw in JMX connection processing allowed remote users to execute arbitrary code, cause denial of service, or obtain potentially sensitive information.
...
[JMX 2006] |
|
| |
| |
| |
Section 2.6, "The JVM Tool Interface; " | |
Reflection, Sun Microsystems, Inc. (2006) |
...