use super::super::{CertRule, RuleViolation};
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use tree_sitter::Node;
pub struct Win04C;
const ENCODE_FUNCS: &[&str] = &["EncodePointer", "EncodeSystemPointer"];
const DECODE_FUNCS: &[&str] = &["DecodePointer", "DecodeSystemPointer"];
impl CertRule for Win04C {
fn rule_id(&self) -> &'static str {
"WIN04-C"
}
fn description(&self) -> &'static str {
"Consider encrypting function pointers"
}
fn severity(&self) -> Severity {
Severity::High
}
fn category(&self) -> RuleCategory {
RuleCategory::Recommendation
}
fn cert_id(&self) -> &'static str {
"WIN04-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Win04C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if node.kind() == "declaration" {
self.check_declaration(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_declaration(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
if !self.is_function_pointer_declaration(node, source) {
return;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "init_declarator" {
if let Some(initializer) = self.get_initializer(&child, source) {
if !self.uses_encode_function(&initializer, source) {
let pos = child.start_position();
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: Severity::High,
message: "Function pointer stored without encryption; consider using EncodePointer()".to_string(),
file_path: String::new(),
line: pos.row + 1,
column: pos.column + 1,
suggestion: Some(
"Use EncodePointer() to encrypt the function pointer before storage".to_string()
),
..Default::default()
});
}
}
}
}
}
}
fn is_function_pointer_declaration(&self, node: &Node, source: &str) -> bool {
let decl_text = get_node_text(node, source);
if decl_text.contains("(*") && decl_text.contains(")(") {
return true;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_function_pointer(&child) {
return true;
}
}
}
false
}
fn contains_function_pointer(&self, node: &Node) -> bool {
let kind = node.kind();
if kind == "function_declarator" {
return true;
}
if kind == "pointer_declarator" {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if child.kind() == "function_declarator" {
return true;
}
if self.contains_function_pointer(&child) {
return true;
}
}
}
}
if kind == "init_declarator" || kind == "parenthesized_declarator" {
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_function_pointer(&child) {
return true;
}
}
}
}
false
}
fn get_initializer<'a>(&self, init_decl: &'a Node<'a>, _source: &str) -> Option<Node<'a>> {
let mut found_eq = false;
for i in 0..init_decl.child_count() {
if let Some(child) = init_decl.child(i) {
if child.kind() == "=" {
found_eq = true;
continue;
}
if found_eq {
return Some(child);
}
}
}
None
}
fn uses_encode_function(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(func) = node.child_by_field_name("function") {
let func_name = get_node_text(&func, source);
if ENCODE_FUNCS.contains(&func_name) || DECODE_FUNCS.contains(&func_name) {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.uses_encode_function(&child, source) {
return true;
}
}
}
false
}
}