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
//! SEC018: Race Condition in File Operations (TOCTOU)
//!
//! **Rule**: Detect Time-of-Check, Time-of-Use (TOCTOU) race conditions
//!
//! **Why this matters**:
//! TOCTOU vulnerabilities occur when file state is checked, then used later:
//! - Between check and use, attacker can modify the file
//! - Symlink attacks can redirect to sensitive files
//! - Concurrent modifications can cause security issues
//! - Critical for privilege escalation prevention
//!
//! **Examples**:
//!
//! ❌ **DANGEROUS** (TOCTOU race condition):
//! ```bash
//! # Race: file could change between check and use
//! if [ -f "$CONFIG" ]; then
//! source "$CONFIG" # Attacker could replace with malicious file
//! fi
//!
//! # Race: symlink attack possible
//! [ -w "$LOGFILE" ] && echo "data" > "$LOGFILE"
//! ```
//!
//! ✅ **SAFE** (avoid TOCTOU):
//! ```bash
//! # Use file descriptor (atomic)
//! exec 3< "$CONFIG" && source /dev/fd/3
//!
//! # Use command's built-in checks
//! source "$CONFIG" 2>/dev/null || echo "Config not found"
//!
//! # For deletion, rm -f doesn't need check
//! rm -f "$FILE" # Safe: doesn't fail if file doesn't exist
//! ```
//!
//! ## Detection Patterns
//!
//! This rule detects:
//! - `[ -f "$FILE" ] && cat/source/read "$FILE"` - Check then read
//! - `[ -e "$FILE" ] && rm "$FILE"` - Check then delete (use `rm -f`)
//! - `[ -w "$FILE" ] && echo/write to "$FILE"` - Check then write
//! - `test -f "$FILE" && operation` - Alternative test syntax
//!
//! ## Auto-fix
//!
//! Suggests:
//! - Use file descriptors for atomic operations
//! - Use command's built-in error handling (e.g., `rm -f`)
//! - Use proper file locking mechanisms
use crate::linter::LintResult;
use crate::linter::{Diagnostic, Severity, Span};
/// Check for TOCTOU race conditions in file operations
pub fn check(source: &str) -> LintResult {
let mut result = LintResult::new();
for (line_num, line) in source.lines().enumerate() {
let trimmed = line.trim();
// Strip comments
let code_only = if let Some(pos) = trimmed.find('#') {
&trimmed[..pos]
} else {
trimmed
};
let code_only = code_only.trim();
// Pattern 1: [ -f "$FILE" ] && cat/source "$FILE"
// Pattern 2: [ -e "$FILE" ] && operation "$FILE"
// Pattern 3: [ -w "$FILE" ] && write to "$FILE"
if (code_only.contains("[ -f")
|| code_only.contains("[ -e")
|| code_only.contains("[ -w")
|| code_only.contains("test -f")
|| code_only.contains("test -e")
|| code_only.contains("test -w"))
&& code_only.contains("&&")
{
// Check if same variable appears in test and operation
if let Some(var_name) = extract_test_variable(code_only) {
if code_only[code_only.find("&&").unwrap() + 2..].contains(&var_name) {
// Detect specific dangerous operations
let after_test = &code_only[code_only.find("&&").unwrap() + 2..];
if after_test.contains("cat")
|| after_test.contains("source")
|| after_test.contains("rm")
|| after_test.contains('>')
|| after_test.contains("cp")
|| after_test.contains("mv")
{
let span = Span::new(line_num + 1, 1, line_num + 1, line.len());
let diag = Diagnostic::new(
"SEC018",
Severity::Warning,
format!(
"TOCTOU race condition: file '{}' is checked then used - file could change between check and use, use file descriptors or atomic operations",
var_name
),
span,
);
result.add(diag);
}
}
}
}
// Pattern 4: if [ -f "$FILE" ]; then ... operation on $FILE
// This is a multi-line pattern, more complex to detect accurately
// For now, we'll focus on single-line patterns which are more common
}
result
}
/// Extract variable name from test expression
/// Example: `[ -f "$CONFIG" ]` → Some("CONFIG")
/// Example: `test -w "$LOGFILE"` → Some("LOGFILE")
fn extract_test_variable(line: &str) -> Option<String> {
// Find the test part (before &&)
let test_part = if let Some(pos) = line.find("&&") {
&line[..pos]
} else {
line
};
// Extract variable from "$VAR" pattern
if let Some(start) = test_part.find("\"$") {
if let Some(end) = test_part[start + 2..].find('"') {
let var_name = &test_part[start + 2..start + 2 + end];
return Some(var_name.to_string());
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
// RED Phase: Write failing tests first (EXTREME TDD)
/// RED TEST 1: Detect [ -f ] && cat (check then read)
#[test]
fn test_SEC018_detects_check_then_cat() {
let script = r#"#!/bin/bash
[ -f "$CONFIG" ] && cat "$CONFIG"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("TOCTOU"));
assert!(diag.message.contains("CONFIG"));
}
/// RED TEST 2: Detect [ -e ] && rm (check then delete)
#[test]
fn test_SEC018_detects_check_then_rm() {
let script = r#"#!/bin/bash
[ -e "$TMPFILE" ] && rm "$TMPFILE"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("race condition"));
}
/// RED TEST 3: Detect [ -w ] && write (check then write)
#[test]
fn test_SEC018_detects_check_then_write() {
let script = r#"#!/bin/bash
[ -w "$LOGFILE" ] && echo "data" > "$LOGFILE"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("LOGFILE"));
}
/// RED TEST 4: Detect test -f syntax
#[test]
fn test_SEC018_detects_test_syntax() {
let script = r#"#!/bin/bash
test -f "$FILE" && source "$FILE"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
assert!(diag.message.contains("FILE"));
}
/// RED TEST 5: Pass safe rm -f usage (no check needed)
#[test]
fn test_SEC018_passes_safe_rm_f() {
let script = r#"#!/bin/bash
# Safe: rm -f doesn't need existence check
rm -f "$TMPFILE"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 0, "Safe rm -f should pass");
}
/// RED TEST 6: Pass when different variables used
#[test]
fn test_SEC018_passes_different_variables() {
let script = r#"#!/bin/bash
# Different variables, not TOCTOU
[ -f "$CONFIG1" ] && cat "$CONFIG2"
"#;
let result = check(script);
assert_eq!(
result.diagnostics.len(),
0,
"Different variables should pass"
);
}
/// RED TEST 7: Detect [ -f ] && cp (check then copy)
#[test]
fn test_SEC018_detects_check_then_cp() {
let script = r#"#!/bin/bash
[ -f "$SOURCE" ] && cp "$SOURCE" /dest/
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
}
/// RED TEST 8: Detect [ -f ] && mv (check then move)
#[test]
fn test_SEC018_detects_check_then_mv() {
let script = r#"#!/bin/bash
[ -e "$OLD" ] && mv "$OLD" "$NEW"
"#;
let result = check(script);
assert_eq!(result.diagnostics.len(), 1);
let diag = &result.diagnostics[0];
assert_eq!(diag.code, "SEC018");
assert_eq!(diag.severity, Severity::Warning);
}
}
#[cfg(test)]
mod property_tests {
use super::*;
use proptest::prelude::*;
proptest! {
#![proptest_config(proptest::test_runner::Config::with_cases(10))]
/// PROPERTY TEST 1: Never panics on any input
#[test]
fn prop_sec018_never_panics(s in ".*") {
let _ = check(&s);
}
/// PROPERTY TEST 2: Always detects [ -f ] && cat pattern
#[test]
fn prop_sec018_detects_check_then_cat(
var_name in "[A-Z_]{1,20}",
) {
let script = format!("[ -f \"${}\" ] && cat \"${}\"", var_name, var_name);
let result = check(&script);
prop_assert_eq!(result.diagnostics.len(), 1);
prop_assert_eq!(result.diagnostics[0].code.as_str(), "SEC018");
}
/// PROPERTY TEST 3: Passes when no && operator
#[test]
fn prop_sec018_passes_without_and(
var_name in "[A-Z_]{1,20}",
) {
let script = format!("cat \"${}\"", var_name);
let result = check(&script);
prop_assert_eq!(result.diagnostics.len(), 0);
}
}
}