344
Use a language that does not allow this weakness to occur or provides constructs that make this
weakness easier to avoid. For example, Ada allows the programmer to constrain the values of a
variable and languages such as Java and Ruby will allow the programmer to handle exceptions
when an out-of-bounds index is accessed.
Phase: Operation
Phase: Build and Compilation
Strategy = Environment Hardening
Run or compile the software using features or extensions that randomly arrange the positions
of a program's executable and libraries in memory. Because this makes the addresses
unpredictable, it can prevent an attacker from reliably jumping to exploitable code. Examples
include Address Space Layout Randomization (ASLR) [REF-58] [REF-60] and Position-
Independent Executables (PIE) [REF-64]. Imported modules may be similarly realigned if their
default memory addresses conflict with other modules, in a process known as "rebasing" (for
Windows) and "prelinking" (for Linux) [REF-1332] using randomly generated addresses. ASLR
for libraries cannot be used in conjunction with prelink since it would require relocating the
libraries at run-time, defeating the whole purpose of prelinking. For more information on these
techniques see D3-SAOR (Segment Address Offset Randomization) from D3FEND [REF-1335].
Effectiveness = Defense in Depth
These techniques do not provide a complete solution. For instance, exploits frequently use a bug
that discloses memory addresses in order to maximize reliability of code execution [REF-1337]. It
has also been shown that a side-channel attack can bypass ASLR [REF-1333]
Phase: Operation
Strategy = Environment Hardening
Use a CPU and operating system that offers Data Execution Protection (using hardware NX
or XD bits) or the equivalent techniques that simulate this feature in software, such as PaX
[REF-60] [REF-61]. These techniques ensure that any instruction executed is exclusively at a
memory address that is part of the code segment. For more information on these techniques see
D3-PSEP (Process Segment Execution Prevention) from D3FEND [REF-1336].
Effectiveness = Defense in Depth
This is not a complete solution, since buffer overflows could be used to overwrite nearby
variables to modify the software's state in dangerous ways. In addition, it cannot be used in
cases in which self-modifying code is required. Finally, an attack could still cause a denial of
service, since the typical response is to exit the application.
Phase: Implementation
Strategy = Input Validation
Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use
a list of acceptable inputs that strictly conform to specifications. Reject any input that does not
strictly conform to specifications, or transform it into something that does. When performing
input validation, consider all potentially relevant properties, including length, type of input, the
full range of acceptable values, missing or extra inputs, syntax, consistency across related
fields, and conformance to business rules. As an example of business rule logic, "boat" may be
syntactically valid because it only contains alphanumeric characters, but it is not valid if the input
is only expected to contain colors such as "red" or "blue." Do not rely exclusively on looking for
malicious or malformed inputs. This is likely to miss at least one undesirable input, especially if
the code's environment changes. This can give attackers enough room to bypass the intended
validation. However, denylists can be useful for detecting potential attacks or determining which
inputs are so malformed that they should be rejected outright. When accessing a user-controlled
array index, use a stringent range of values that are within the target array. Make sure that you
CWE Version 4.14
CWE-129: Improper Validation of Array Index
CWE-129: Improper Validation of Array Index