...
Java provides two options for program termination: Runtime.exit()
(this is equivalent to System.exit()
) and Runtime.halt()
.
Runtime.exit()
Runtime.exit()
is the typical way of exiting a program:
...
The Runtime.addShutdownHook()
method can be used to customize Runtime.exit()
to perform additional actions at program termination.
This method takes a single Thread
, which must be initalized but unstarted. Then, when the JVM begins to shut down, the thread will be run. Since the JVM usually has a fixed time to shut down, these threads should not be long-running and should not attempt user interaction.
Runtime.halt()
Runtime.halt()
works similarly but does NOT run shutdown hooks or finalizers:
Forcibly terminates the currently running Java virtual machine. This method never returns normally.
This method should be used with extreme caution. Unlike the exit method, this method does not cause shutdown hooks to be started and does not run uninvoked finalizers if finalization-on-exit has been enabled. If the shutdown sequence has already been initiated then this method does not wait for any running shutdown hooks or finalizers to finish their work.
Differences from C/C++
In contrast with C and C++, Java does not flush unwritten buffered data or close open files when it exits, so programs must do this manually. Programs must also do any other cleanup that involves external resources, such as releasing shared locks.
Noncompliant Code Example
This example creates a new file, outputs some text to it, and abruptly exits using Runtime.exit()
. Consequently, the file is closed without the text actually being written to it.
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("foo.txt"))); out.println("hello"); Runtime.getRuntime().exit(1); } } |
Compliant Solution (
...
close()
)
This solution explicitly closes the file before exiting
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("foo.txt"))); out.println("hello"); out.close(); Runtime.getRuntime().exit(1); } } |
Compliant Solution (Shutdown Hook)
This compliant solution adds a shutdown hook to close the file. This hook is invoked by Runtime.exit()
is called before the JVM is halted.
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("foo.txt"))); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { out.close(); } })); out.println("hello"); Runtime.getRuntime().exit(1); } } |
Noncompliant Code Example (Runtime.halt()
)
This noncompliant code example calls Runtime.halt()
instead of Runtime.exit()
. Runtime.halt()
stops the JVM without invoking any shutdown hooks; consequently the file is not properly written to or closed.
Code Block | ||
---|---|---|
| ||
public class CreateFile { public static void main(String[] args) throws FileNotFoundException { final PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("foo.txt"))); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { out.close(); } })); out.println("hello"); Runtime.getRuntime().halt(1); } } |
Noncompliant Code Example (
...
signal)
When a user forcefully exits a program by pressing the ctrl + c
key or by using the kill
command, the JVM terminates abruptly. Although this event cannot be captured, the program should nevertheless perform any mandatory clean-up operations before exiting. This noncompliant code example fails to do so.
Code Block | ||
---|---|---|
| ||
public class InterceptExit { public static void main(String[] args) { System.out.println("Regular code block"); // Abrupt exit such as ctrl + c key pressed System.out.println("This never executes"); } } |
Compliant Solution (addShutdownHook()
)
Use the addShutdownHook()
method of java.lang.Runtime
to assist with performing clean-up operations in the event of abrupt termination. The JVM starts the shutdown hook thread when abrupt termination is initiated; the shutdown hook runs concurrently with other JVM threads.
...
The JVM can abort for external reasons, such as an external SIGKILL
signal (UNIX) or the TerminateProcess
call (Microsoft Windows), or memory corruption caused by native methods. Shutdown hooks may fail to execute as expected in such cases, because the JVM cannot guarantee that they will be executed as intended.
Risk Assessment
Using Runtime.halt()
in place of Runtime.exit()
may not perform necessary cleanup, potentially leaving sensitive data exposed or leaving data in an inconsistent state.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
ERR04 FIO16-CPP J | medium | low | medium | P4 | L3 |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
Other Languages
This rule appears in the C Secure Coding Standard as ERR04-C. Choose an appropriate termination strategy.
This rule appears in the C++ Secure Coding Standard as ERR04-CCPP. Choose an appropriate termination strategy.
Bibliography
Wiki Markup |
---|
\[[ISO/IEC PDTR 24772|AA. Bibliography#ISO/IEC PDTR 24772]\] "REU Termination strategy" \[[MITRE 07|AA. Bibliography#MITRE 07]\] [CWE ID 705|http://cwe.mitre.org/data/definitions/705.html], "Incorrect Control Flow Scoping" |
...