finalize() Method Declared Public

Incomplete Variant
Structure: Simple
Description

This vulnerability occurs when a Java class declares its finalize() method as public, violating secure coding practices for mobile code.

Extended Description

In Java, the finalize() method is a special method called by the garbage collector before an object is destroyed. Declaring it as public breaks the intended contract, as this method should only ever be invoked automatically by the JVM's garbage collection process, not directly by application code. Explicit calls to finalize() are strongly discouraged and are a common source of errors and unpredictable behavior. For mobile code, such as applets or frameworks where code is downloaded and executed remotely, this flaw becomes a direct security threat. An attacker could exploit the publicly accessible finalize() method to trigger malicious logic, interfere with object cleanup, or cause a denial of service by manually invoking garbage collection routines. The secure practice is to always declare finalize() as protected, and within it, only call super.finalize().

Common Consequences 1
Scope: ConfidentialityIntegrityAvailability

Impact: Alter Execution LogicExecute Unauthorized Code or CommandsModify Application Data

Detection Methods 1
Automated Static AnalysisHigh
Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then searching for potentially-vulnerable patterns that connect "sources" (origins of input) with "sinks" (destinations where the data interacts with external components, a lower layer such as the OS, etc.)
Potential Mitigations 1
Phase: Implementation
If you are using finalize() as it was designed, there is no reason to declare finalize() with anything other than protected access.
Demonstrative Examples 1
The following Java Applet code mistakenly declares a public finalize() method.

Code Example:

Bad
Java
java
Mobile code, in this case a Java Applet, is code that is transmitted across a network and executed on a remote machine. Because mobile code developers have little if any control of the environment in which their code will execute, special security concerns become relevant. One of the biggest environmental threats results from the risk that the mobile code will run side-by-side with other, potentially malicious, mobile code. Because all of the popular web browsers execute code from multiple sources together in the same JVM, many of the security guidelines for mobile code are focused on preventing manipulation of your objects' state and behavior by adversaries who have access to the same virtual machine where your product is running.
Applicable Platforms
Languages:
Java : Undetermined
Modes of Introduction
Implementation
Taxonomy Mapping
  • The CERT Oracle Secure Coding Standard for Java (2011)
  • Software Fault Patterns