CWE-696: Incorrect Behavior Order
Description
The product performs multiple related behaviors, but the behaviors are performed in the wrong order in ways which may produce resultant weaknesses.
Submission Date :
Sept. 9, 2008, midnight
Modification Date :
2023-10-26 00:00:00+00:00
Organization :
MITRE
Example - 1
The following code attempts to validate a given input path by checking it against an allowlist and then return the canonical path. In this specific case, the path is considered valid if it starts with the string "/safe_dir/". The problem with the above code is that the validation step occurs before canonicalization occurs. An attacker could provide an input path of "/safe_dir/../" that would pass the validation step. However, the canonicalization process sees the double dot as a traversal to the parent directory and hence when canonicized the path would become just "/". To avoid this problem, validation should occur after canonicalization takes place. In this case canonicalization occurs during the initialization of the File object. The code below fixes the issue.
File f = new File(path);return f.getCanonicalPath();String path = getInputPath();if (path.startsWith("/safe_dir/")){}
return f.getCanonicalPath();
String path = getInputPath();File f = new File(path);if (f.getCanonicalPath().startsWith("/safe_dir/")){}
Example - 2
This data prints the contents of a specified file requested by a user. This code first reads a specified file into memory, then prints the file if the user is authorized to see its contents. The read of the file into memory may be resource intensive and is unnecessary if the user is not allowed to see the file anyway.
// //read file into string//
echo $file;return true;
echo 'You are not authorized to view this file';
$file = file_get_contents($filename);if ($file && isOwnerOf($username,$filename)){}else{}return false;function printFile($username,$filename){}
Example - 3
Assume that the module foo_bar implements a protected register. The register content is the asset. Only transactions made by user id (indicated by signal usr_id) 0x4 are allowed to modify the register contents. The signal grant_access is used to provide access. This code uses Verilog blocking assignments for data_out and grant_access. Therefore, these assignments happen sequentially (i.e., data_out is updated to new value first, and grant_access is updated the next cycle) and not in parallel. Therefore, the asset data_out is allowed to be modified even before the access control check is complete and grant_access signal is set. Since grant_access does not have a reset value, it will be meta-stable and will randomly go to either 0 or 1. Flipping the order of the assignment of data_out and grant_access should solve the problem. The correct snippet of code is shown below.
data_out = 0;
data_out = (grant_access) ? data_in : data_out;assign grant_access = (usr_id == 3'h4) ? 1'b1 : 1'b0;if (!rst_n)else
module foo_bar(data_out, usr_id, data_in, clk, rst_n);output reg [7:0] data_out;input wire [2:0] usr_id;input wire [7:0] data_in; input wire clk, rst_n;wire grant_access;always @ (posedge clk or negedge rst_n)beginendendmodule
data_out = 0;
assign grant_access = (usr_id == 3'h4) ? 1'b1 : 1'b0;data_out = (grant_access) ? data_in : data_out;if (!rst_n)else
always @ (posedge clk or negedge rst_n)beginendendmodule
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-179: Incorrect Behavior Order: Early Validation
CWE-408: Incorrect Behavior Order: Early Amplification
CWE-551: Incorrect Behavior Order: Authorization Before Parsing and Canonicalization
CWE-691: Insufficient Control Flow Management
CWE-1190: DMA Device Enabled Too Early in Boot Phase
CWE-1193: Power-On of Untrusted Execution Core Before Enabling Fabric Access Control
CWE-1280: Access Control Check Implemented After Asset is Accessed
Visit http://cwe.mitre.org/ for more details.