use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use crate::utility::cert_c::declarator_utils::{is_array_declarator, is_pointer_declarator};
use tree_sitter::Node;
pub struct Api01C;
impl CertRule for Api01C {
fn rule_id(&self) -> &'static str {
"API01-C"
}
fn description(&self) -> &'static str {
"Avoid laying out strings in memory directly before sensitive data"
}
fn severity(&self) -> Severity {
Severity::High
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"API01-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Api01C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "struct_specifier" {
self.check_struct_layout(node, source, violations);
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
fn check_struct_layout(
&self,
struct_node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let field_list = match struct_node.child_by_field_name("body") {
Some(body) => body,
None => return, };
let mut fields = Vec::new();
for i in 0..field_list.child_count() {
if let Some(child) = field_list.child(i) {
if child.kind() == "field_declaration" {
fields.push(child);
}
}
}
for i in 0..fields.len() {
if self.is_char_array_field(&fields[i], source) {
for j in (i + 1)..fields.len() {
if self.is_pointer_field(&fields[j], source) {
self.report_violation(
&fields[i],
&fields[j],
struct_node,
source,
violations,
);
break; }
}
}
}
}
fn is_char_array_field(&self, field_node: &Node, source: &str) -> bool {
let type_node = match field_node.child_by_field_name("type") {
Some(t) => t,
None => return false,
};
let type_text = get_node_text(&type_node, source);
if !type_text.contains("char") {
return false;
}
if let Some(declarator) = field_node.child_by_field_name("declarator") {
return is_array_declarator(&declarator);
}
false
}
fn is_pointer_field(&self, field_node: &Node, source: &str) -> bool {
let type_node = match field_node.child_by_field_name("type") {
Some(t) => t,
None => return false,
};
let type_text = get_node_text(&type_node, source);
if type_text.contains('*') {
return true;
}
if let Some(declarator) = field_node.child_by_field_name("declarator") {
return is_pointer_declarator(&declarator);
}
false
}
fn report_violation(
&self,
char_array_field: &Node,
pointer_field: &Node,
struct_node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
let char_array_text = get_node_text(char_array_field, source);
let pointer_text = get_node_text(pointer_field, source);
let struct_name = if let Some(name_node) = struct_node.child_by_field_name("name") {
get_node_text(&name_node, source)
} else {
"anonymous"
};
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::High,
message: format!(
"String buffer placed before pointer in struct '{}': '{}' before '{}' - Buffer overflow could corrupt pointer",
struct_name,
char_array_text.lines().next().unwrap_or(char_array_text).trim(),
pointer_text.lines().next().unwrap_or(pointer_text).trim()
),
file_path: String::new(),
line: char_array_field.start_position().row + 1,
column: char_array_field.start_position().column + 1,
suggestion: Some(format!(
"Move the string buffer '{}' after the pointer field '{}', or use a char* pointer instead of a char array",
char_array_text.split('[').next().unwrap_or("").trim(),
pointer_text.split(':').next().unwrap_or(pointer_text).trim()
)),
..Default::default()
});
}
}