Invocation of System.exit()
terminates the Java Virtual Machine (JVM), consequently terminating all running programs and threads running thereon. This can result in denial-of-service (DoS) attacks. For example, a call to System.exit()
that is embedded in Java Server Pages (JSP) code can cause a web server to terminate, preventing further service for users. Programs must prevent both inadvertent and malicious calls to System.exit()
. Additionally, programs should perform necessary clean-up cleanup actions when forcibly terminated (for example, by using the Windows Task Manager, POSIX kill
command, or other mechanismmechanisms).
Noncompliant Code Example
This noncompliant code example uses System.exit()
to forcefully shutdown shut down the JVM and terminate the running process. The program lacks a security manager; consequently, it lacks the capability to check whether the caller is permitted to invoke System.exit()
.
Code Block | ||
---|---|---|
| ||
public class InterceptExit {
public static void main(String[] args) {
// ...
System.exit(1); // Abrupt exit
System.out.println("This never executes");
}
}
|
...
This compliant solution installs a custom security manager PasswordSecurityManager
that overrides the checkExit()
method defined in the SecurityManager
class. This override is required to enable invocation of cleanup code before allowing the exit. The default checkExit()
method in the SecurityManager
class lacks this facility.
Code Block | ||
---|---|---|
| ||
class PasswordSecurityManager extends SecurityManager { private boolean isExitAllowedFlag; public PasswordSecurityManager(){ super(); isExitAllowedFlag = false; } public boolean isExitAllowed(){ return isExitAllowedFlag; } @Override public void checkExit(int status) { if (!isExitAllowed()) { throw new SecurityException(); } super.checkExit(status); } public void setExitAllowed(boolean f) { isExitAllowedFlag = f; } } public class InterceptExit { public static void main(String[] args) { PasswordSecurityManager secManager = new PasswordSecurityManager(); System.setSecurityManager(secManager); try { // ... System.exit(1); // Abrupt exit call } catch (Throwable x) { if (x instanceof SecurityException) { System.out.println("Intercepted System.exit()"); // Log exception } else { // Forward to exception handler } } // ... secManager.setExitAllowed(true); // Permit exit // System.exit() will work subsequently // ... } } |
This implementation uses an internal flag to track whether the exit is permitted. The method setExitAllowed()
sets this flag. The checkExit()
method throws a SecurityException
when the flag is unset (e.g.that is, false). ConsequentlyBecause this flag is not initially set, normal exception processing bypasses the initial call to System.exit()
. The program catches the SecurityException
and performs mandatory clean-up cleanup operations, including logging the exception. The setExitAllowedSystem.exit()
method is invoked enabled only after clean-up cleanup is complete. Consequently, the program exits gracefully.
Exceptions
*EXC09ERR09-J-EX0:* It is permissible for a command -line utility to call {{ Wiki Markup System.exit()
}} or terminate prematurely; for , for example, when the required number of arguments are not input \ [[Bloch 2008|AA. Bibliography#Bloch 08]\] and \[], [ESA 2005|AA. Bibliography#ESA 05]\].
Risk Assessment
Allowing inadvertent unauthorized calls to System.exit()
may lead to denial of service.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR09-J |
Low |
Unlikely |
Medium | P2 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| JAVA.DEBUG.CALL | Debug Call (Java) | ||||||
Coverity | 7.5 | DC.CODING_STYLE | Implemented | ||||||
Parasoft Jtest |
| CERT.ERR09.JVM CERT.ERR09.EXIT | Do not stop the JVM in a web component Do not call methods which terminates Java Virtual Machine | ||||||
SonarQube |
| S1147 | Exit methods should not be called |
Related Guidelines
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="025e98bb-1d7b-474e-ab7d-966e632ca001"><ac:plain-text-body><![CDATA[
[[MITRE 2009
AA. Bibliography#MITRE 09]]
[CWE ID 382
J2EE Bad Practices: Use of |
]]></ac:plain-text-body></ac:structured-macro>
Bibliography
Android Implementation Details
On Android, System.exit()
should not be used because it will terminate the virtual machine abruptly, ignoring the activity life cycle, which may prevent proper garbage collection.
Bibliography
[API 2014] | Method |
Section 9.5, "The Finalize Method" | |
[ESA 2005] | Rule 78, |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b41e894a-c596-4b3a-8c1e-6915859b7db8"><ac:plain-text-body><![CDATA[
[[API 2006
AA. Bibliography#API 06]]
[method checkExit()
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/SecurityManager.html#checkExit(int)], Class Runtime, method addShutdownHook
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1e62b6da-435a-4905-b21d-80f3ea5edbc3"><ac:plain-text-body><![CDATA[
[[Austin 2000
AA. Bibliography#Austin 00]]
[Writing a Security Manager
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/signed2.html]
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7cce4f78-63c0-4805-81a8-4d97dc9abc96"><ac:plain-text-body><![CDATA[
[[Darwin 2004
AA. Bibliography#Darwin 04]]
9.5 The Finalize Method
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1af5101b-df19-4766-9d07-989e5ab7d3b0"><ac:plain-text-body><![CDATA[
[[ESA 2005
AA. Bibliography#ESA 05]]
Restrict the use of the |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c50f8f49-5e1f-4499-808d-9307b45605d1"><ac:plain-text-body><![CDATA[
[ |
] | Section 7.4 |
]]></ac:plain-text-body></ac:structured-macro>
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="996dcfd2-a271-43ec-9db7-a0cdfd2039b3"><ac:plain-text-body><![CDATA[
[[Kalinovsky 2004
, "JVM Shutdown" |
] | Chapter 16, "Intercepting a Call to |
]]></ac:plain-text-body></ac:structured-macro>
" |
...
06. Exceptional Behavior (ERR) ERR06-J. Do not let code throw undeclared checked exceptions