278
Detection Methods
Automated Static Analysis
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.)
Effectiveness = High
Potential Mitigations
Phase: Architecture and Design
Strategy = Libraries or Frameworks
Libraries that are loaded should be well understood and come from a trusted source. The
application can execute code contained in the native libraries, which often contain calls that are
susceptible to other security problems, such as buffer overflows or command injection. All native
libraries should be validated to determine if the application requires the use of the library. It is
very difficult to determine what these native libraries actually do, and the potential for malicious
code is high. In addition, the potential for an inadvertent mistake in these native libraries is also
high, as many are written in C or C++ and may be susceptible to buffer overflow or race condition
problems. To help prevent buffer overflow attacks, validate all input to native calls for content and
length. If the native library does not come from a trusted source, review the source code of the
library. The library should be built from the reviewed source before using it.
Demonstrative Examples
Example 1:
The following code uses System.loadLibrary() to load code from a native library named library.dll,
which is normally found in a standard system directory.
Example Language: Java (Bad)
...
System.loadLibrary("library.dll");
...
The problem here is that System.loadLibrary() accepts a library name, not a path, for the library
to be loaded. From the Java 1.4.2 API documentation this function behaves as follows [1]: A file
containing native code is loaded from the local file system from a place where library files are
conventionally obtained. The details of this process are implementation-dependent. The mapping
from a library name to a specific filename is done in a system-specific manner. If an attacker is able
to place a malicious copy of library.dll higher in the search order than file the application intends to
load, then the application will load the malicious copy instead of the intended file. Because of the
nature of the application, it runs with elevated privileges, which means the contents of the attacker's
library.dll will now be run with elevated privileges, possibly giving them complete control of the
system.
Example 2:
The following code from a privileged application uses a registry entry to determine the directory in
which it is installed and loads a library file based on a relative path from the specified directory.
Example Language: C (Bad)
...
RegQueryValueEx(hkey, "APPHOME",
0, 0, (BYTE*)home, &size);
CWE Version 4.14
CWE-114: Process Control
CWE-114: Process Control