sqc 0.4.13

Software Code Quality - CERT C compliance checker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! ENV33-C: Do not call system()
//!
//! The C Standard system() function executes a specified command by invoking
//! an implementation-defined command processor, such as a UNIX shell or CMD.EXE
//! in Microsoft Windows. The POSIX popen() and Windows _popen() functions also
//! invoke a command processor but create a pipe between the calling program and
//! the executed command.
//!
//! Use of the system() function can result in exploitable vulnerabilities, in
//! the worst case allowing execution of arbitrary system commands.
//!
//! ## Examples:
//!
//! **Non-compliant:**
//! ```c
//! void process_file(const char *filename) {
//!     char cmd[512];
//!     sprintf(cmd, "process %s", filename);
//!     system(cmd);  // Vulnerable to command injection
//! }
//!
//! FILE *f = popen("ls -la", "r");  // Also vulnerable
//! ```
//!
//! **Compliant:**
//! ```c
//! // Use safer alternatives like exec() family or implement functionality directly
//! void process_file(const char *filename) {
//!     pid_t pid = fork();
//!     if (pid == 0) {
//!         execl("/usr/bin/process", "process", filename, NULL);
//!         _exit(1);
//!     }
//! }
//! ```
//!
//! ## Detection Strategy:
//! - Detect any calls to system(), popen(), or _popen()
//! - Also detects Windows exec/spawn variants (_execl, _spawnl, etc.)
//! - Resolves macro aliases (#define SYSTEM system) via project context
//! - These functions are inherently risky and should be avoided
//! - Suggest safer alternatives like exec() family functions

use super::super::{CertRule, RuleViolation};
use crate::analyze::cfg;
use crate::analyze::const_eval;
use crate::analyze::context::ProjectContext;
use crate::analyze::function_summary::FunctionSummary;
use crate::manifest::{RuleCategory, Severity};
use crate::utility::cert_c::ast_utils::get_node_text;
use std::cell::RefCell;
use std::collections::{HashMap, HashSet};
use tree_sitter::Node;

pub struct Env33C {
    project_aliases: RefCell<HashMap<String, String>>,
    current_aliases: RefCell<HashMap<String, String>>,
    function_summaries: RefCell<HashMap<String, FunctionSummary>>,
    /// Reverse call graph: callee_name → caller names.
    callers: RefCell<HashMap<String, HashSet<String>>>,
}

impl Env33C {
    pub fn new() -> Self {
        Self {
            project_aliases: RefCell::new(HashMap::new()),
            current_aliases: RefCell::new(HashMap::new()),
            function_summaries: RefCell::new(HashMap::new()),
            callers: RefCell::new(HashMap::new()),
        }
    }
}

impl Default for Env33C {
    fn default() -> Self {
        Self::new()
    }
}

impl CertRule for Env33C {
    fn rule_id(&self) -> &'static str {
        "ENV33-C"
    }

    fn description(&self) -> &'static str {
        "Do not call system()"
    }

    fn severity(&self) -> Severity {
        Severity::High
    }

    fn category(&self) -> RuleCategory {
        RuleCategory::Rule
    }

    fn cert_id(&self) -> &'static str {
        "ENV33-C"
    }

    fn set_project_context(&self, context: &ProjectContext) {
        *self.project_aliases.borrow_mut() = context.macro_aliases.clone();
        *self.function_summaries.borrow_mut() = context.function_summaries.clone();

        let mut callers: HashMap<String, HashSet<String>> = HashMap::new();
        for (caller, callees) in &context.call_graph {
            for callee in callees {
                callers
                    .entry(callee.clone())
                    .or_default()
                    .insert(caller.clone());
            }
        }
        *self.callers.borrow_mut() = callers;
    }

    fn check(&self, node: &Node, source: &str) -> Vec<RuleViolation> {
        // Merge project-level aliases with per-file aliases
        let mut aliases = self.project_aliases.borrow().clone();
        aliases.extend(const_eval::collect_macro_aliases(node, source));
        *self.current_aliases.borrow_mut() = aliases;

        let mut violations = Vec::new();
        self.check_node(node, source, &mut violations);
        violations
    }
}

/// Functions that introduce external/untrusted input into a program.
/// If a containing function calls any of these, data passed to system()/popen()
/// may be tainted and the call should remain flagged.
const TAINTED_SOURCE_FUNCTIONS: &[&str] = &[
    // Network I/O
    "recv", "recvfrom", "recvmsg", // Console/file input
    "fgets", "gets", "gets_s", "scanf", "fscanf", "sscanf", "wscanf", "fwscanf", "fread", "fgetc",
    "fgetwc", "getchar", "getwchar", "getc", "getwc", "fgetws", // Environment
    "getenv", "_wgetenv", // POSIX
    "read", "pread",
];

impl Env33C {
    /// Resolve a function name through macro aliases.
    /// If `name` is a macro alias for a dangerous function, returns the target.
    fn resolve_name(&self, name: &str) -> String {
        let aliases = self.current_aliases.borrow();
        if let Some(target) = aliases.get(name) {
            target.clone()
        } else {
            name.to_string()
        }
    }

    /// Recursively check nodes for dangerous function calls
    fn check_node(&self, node: &Node, source: &str, violations: &mut Vec<RuleViolation>) {
        if node.kind() == "call_expression" {
            if let Some(function) = node.child_by_field_name("function") {
                let func_name = get_node_text(&function, source);
                let resolved = self.resolve_name(&func_name);

                if self.is_dangerous_function(&resolved) {
                    // Check if the call can be suppressed:
                    // 1. Direct string literal argument → safe (no injection possible)
                    // 2. Local variable in a function with no tainted input sources → safe
                    // 3. Function parameter → keep flagging (caller may pass tainted data)
                    if self.is_safe_command_call(node, source) {
                        // Skip — command is built from constants with no external input
                    } else {
                        let suggestion = match resolved.as_str() {
                            "system" => "Use the exec() family of functions (execl, execv, etc.) instead, which provide better control and security",
                            "popen" | "_popen" => "Use pipe() and fork() with exec() family functions for better security",
                            _ => "Use safer alternatives like the exec() family of functions"
                        };

                        let display_name = if func_name != resolved {
                            format!("{} (macro for {})", func_name, resolved)
                        } else {
                            func_name.to_string()
                        };

                        violations.push(RuleViolation {
                            rule_id: self.rule_id().to_string(),
                            message: format!(
                                "Call to '{}' is prohibited. This function invokes a command \
                                 processor which can lead to command injection vulnerabilities. \
                                 The function executes arbitrary shell commands and is inherently \
                                 dangerous when user input is involved.",
                                display_name
                            ),
                            severity: self.severity(),
                            line: node.start_position().row + 1,
                            column: node.start_position().column + 1,
                            file_path: String::new(),
                            suggestion: Some(suggestion.to_string()),
                            requires_manual_review: None,
                        });
                    }
                }
            }
        }

        // Recursively check child nodes
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                self.check_node(&child, source, violations);
            }
        }
    }

    /// Check if a system()/popen() call is safe (no tainted input flows to the command).
    ///
    /// Returns true (safe, suppress) when the argument is a local variable in a
    /// function that has no tainted input sources AND no pointer/string parameters
    /// that could carry untrusted data from callers.
    ///
    /// Returns false (keep flagging) when:
    /// - The argument is a function parameter
    /// - The function has pointer/string parameters (data may flow through them)
    /// - The containing function reads external input (recv, fgets, getenv, etc.)
    /// - The argument is a string literal (still a violation: PATH/shell risks)
    fn is_safe_command_call(&self, call_node: &Node, source: &str) -> bool {
        // If the first argument is a direct string literal, always flag —
        // system("cmd") is still an ENV33-C violation (PATH/shell risks)
        if let Some(args) = call_node.child_by_field_name("arguments") {
            for i in 0..args.child_count() {
                if let Some(child) = args.child(i) {
                    let kind = child.kind();
                    if kind != "(" && kind != ")" && kind != "," {
                        // First real argument
                        if kind == "string_literal" {
                            return false; // Keep flagging string literal commands
                        }
                        break;
                    }
                }
            }
        }

        // Find the containing function
        let func_node = match Self::find_containing_function(call_node) {
            Some(f) => f,
            None => return false,
        };

        // Direct taint-source call in this scope (e.g. fgets, recv, getenv)
        // means whatever the command var holds could be externally controlled.
        if self.function_has_tainted_source(&func_node, source) {
            return false;
        }

        // Helper-sink pattern: the function takes a pointer/string param that
        // could carry untrusted data from callers. Defer to callers: suppress
        // only when every caller's prescan summary shows no taint source and
        // no transitive return-tainted path. Unknown callers stay flagging.
        if Self::has_pointer_parameters(&func_node, source) {
            return self.callers_are_all_clean(&func_node, source);
        }

        // No pointer params and no tainted sources → data is locally-constructed
        true
    }

    /// True when we can prove every transitive caller of `func_node`'s
    /// function is free of direct taint sources and transitively-tainted
    /// return values. False when caller info is missing at any level or any
    /// ancestor caller is tainted.
    ///
    /// The reverse call graph is walked BFS-style because Juliet's variants
    /// 52/53/54 route data through multi-level forwarding sinks where the
    /// immediate caller is a clean pass-through but a grand-caller reads
    /// from a taint source (e.g. recv).
    fn callers_are_all_clean(&self, func_node: &Node, source: &str) -> bool {
        let Some(name) = cfg::get_function_name(func_node, source) else {
            return false;
        };
        let callers = self.callers.borrow();
        let Some(root_callers) = callers.get(name) else {
            return false;
        };
        if root_callers.is_empty() {
            return false;
        }

        let summaries = self.function_summaries.borrow();
        let mut visited: HashSet<String> = HashSet::new();
        let mut stack: Vec<String> = root_callers.iter().cloned().collect();

        while let Some(current) = stack.pop() {
            if !visited.insert(current.clone()) {
                continue;
            }
            match summaries.get(&current) {
                Some(s) if !s.has_env03_taint_source && !s.returns_tainted => {}
                _ => return false,
            }
            if let Some(next) = callers.get(&current) {
                for c in next {
                    if !visited.contains(c) {
                        stack.push(c.clone());
                    }
                }
            }
        }
        true
    }

    /// Walk up the AST to find the containing function_definition.
    fn find_containing_function<'a>(node: &Node<'a>) -> Option<Node<'a>> {
        let mut current = node.parent();
        while let Some(n) = current {
            if n.kind() == "function_definition" {
                return Some(n);
            }
            current = n.parent();
        }
        None
    }

    /// Check if a function_definition has any pointer or array parameters.
    /// Functions with pointer params may receive tainted data from callers.
    fn has_pointer_parameters(func_node: &Node, source: &str) -> bool {
        if let Some(declarator) = func_node.child_by_field_name("declarator") {
            Self::find_pointer_params_in_subtree(&declarator, source)
        } else {
            false
        }
    }

    fn find_pointer_params_in_subtree(node: &Node, source: &str) -> bool {
        if node.kind() == "parameter_list" {
            for i in 0..node.child_count() {
                if let Some(param) = node.child(i) {
                    if param.kind() == "parameter_declaration" {
                        let param_text = get_node_text(&param, source);
                        // Skip (void) parameter
                        if param_text.trim() == "void" {
                            continue;
                        }
                        // Check if parameter type contains pointer/array indicators
                        if param_text.contains('*') || param_text.contains('[') {
                            return true;
                        }
                    }
                }
            }
            return false;
        }
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if Self::find_pointer_params_in_subtree(&child, source) {
                    return true;
                }
            }
        }
        false
    }

    /// Check if a function body calls any tainted input source functions.
    fn function_has_tainted_source(&self, func_node: &Node, source: &str) -> bool {
        self.scan_for_tainted_calls(func_node, source)
    }

    fn scan_for_tainted_calls(&self, node: &Node, source: &str) -> bool {
        if node.kind() == "call_expression" {
            if let Some(function) = node.child_by_field_name("function") {
                let name = get_node_text(&function, source);
                // Resolve through macro aliases (e.g., GETENV → getenv)
                let resolved = self.resolve_name(&name);
                if TAINTED_SOURCE_FUNCTIONS.contains(&resolved.as_str()) {
                    return true;
                }
                // Also check the original name (in case alias resolution fails)
                if TAINTED_SOURCE_FUNCTIONS.contains(&name) {
                    return true;
                }
            }
        }
        for i in 0..node.child_count() {
            if let Some(child) = node.child(i) {
                if self.scan_for_tainted_calls(&child, source) {
                    return true;
                }
            }
        }
        false
    }

    /// Check if function is a dangerous command processor invocation
    fn is_dangerous_function(&self, name: &str) -> bool {
        matches!(
            name,
            "system"
                | "popen"
                | "_popen"
                | "_execl"
                | "_execle"
                | "_execlp"
                | "_execv"
                | "_execve"
                | "_execvp"
                | "_spawnl"
                | "_spawnle"
                | "_spawnlp"
                | "_spawnv"
                | "_spawnve"
                | "_spawnvp"
        )
    }
}