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 Pos35C;
impl Pos35C {
pub fn new() -> Self {
Self
}
fn is_lstat_call(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source).trim();
return func_name == "lstat";
}
}
false
}
fn is_open_without_nofollow(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source).trim();
if func_name == "open" {
if let Some(arguments) = node.child_by_field_name("arguments") {
let args_text = get_node_text(&arguments, source);
return !args_text.contains("O_NOFOLLOW");
}
return true; }
}
}
false
}
fn contains_s_islnk(&self, node: &Node, source: &str) -> bool {
if node.kind() == "call_expression" {
if let Some(function) = node.child_by_field_name("function") {
let func_name = get_node_text(&function, source).trim();
if func_name == "S_ISLNK" {
return true;
}
}
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_s_islnk(&child, source) {
return true;
}
}
}
false
}
fn check_lstat_open_pattern(
&self,
node: &Node,
source: &str,
violations: &mut Vec<RuleViolation>,
) {
if self.is_lstat_call(node, source) {
if let Some(parent) = node.parent() {
if let Some(scope) = self.find_containing_scope(&parent) {
if self.contains_s_islnk(&scope, source) {
if self.contains_open_without_nofollow(&scope, source) {
violations.push(RuleViolation {
rule_id: self.rule_id().to_string(),
severity: self.severity(),
message: "Potential TOCTOU race condition: lstat() check for symbolic link followed by separate open() call. The file could be replaced with a symbolic link between the check and use.".to_string(),
file_path: String::new(),
line: node.start_position().row + 1,
column: node.start_position().column + 1,
suggestion: Some(
"Use open() with O_NOFOLLOW flag (POSIX.1-2008+), or use lstat() + open() + fstat() pattern with attribute comparison (POSIX.1-2001)".to_string()
),
..Default::default()
});
}
}
}
}
}
}
fn find_containing_scope<'a>(&self, node: &Node<'a>) -> Option<Node<'a>> {
let mut current = *node;
while let Some(parent) = current.parent() {
if parent.kind() == "compound_statement" || parent.kind() == "translation_unit" {
return Some(parent);
}
current = parent;
}
None
}
fn contains_open_without_nofollow(&self, node: &Node, source: &str) -> bool {
if self.is_open_without_nofollow(node, source) {
return true;
}
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
if self.contains_open_without_nofollow(&child, source) {
return true;
}
}
}
false
}
}
impl CertRule for Pos35C {
fn rule_id(&self) -> &'static str {
"POS35-C"
}
fn description(&self) -> &'static str {
"Avoid race conditions while checking for the existence of a symbolic link"
}
fn severity(&self) -> Severity {
Severity::High
}
fn category(&self) -> RuleCategory {
RuleCategory::Rule
}
fn cert_id(&self) -> &'static str {
"POS35-C"
}
fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
let mut violations = Vec::new();
self.check_node(node, source, &mut violations);
violations
}
}
impl Pos35C {
fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
self.check_lstat_open_pattern(node, source, violations);
for i in 0..node.child_count() {
if let Some(child) = node.child(i) {
self.check_node(&child, source, violations);
}
}
}
}