Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: nasl-cli/openvasd: stack overflow on prefix-statement #1530

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions rust/nasl-syntax/src/grouping_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl<'a> Grouping for Lexer<'a> {
if !end {
Err(unclosed_token!(token))
} else {
self.depth = 0;
match right {
Statement::Assign(category, _, variable, stmt) => Ok(Statement::Assign(
category,
Expand All @@ -53,6 +54,7 @@ impl<'a> Grouping for Lexer<'a> {
while let Some(token) = self.peek() {
if token.category() == &Category::RightCurlyBracket {
self.token();
self.depth = 0;
return Ok((token.clone(), Statement::Block(kw, results, token)));
}
let (end, stmt) = self.statement(0, &|cat| cat == &Category::Semicolon)?;
Expand Down
4 changes: 1 addition & 3 deletions rust/nasl-syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Lexer<'a> {
// is the current depth call within a statement call. The current
// implementation relies that the iterator implementation resets depth to 0
// after a statement, or error, has been returned.
depth: u8,
pub(crate) depth: u8,
}

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -115,7 +115,6 @@ impl<'a> Lexer<'a> {
return Err(unexpected_token!(token));
}
if abort(token.category()) {
self.depth = 0;
return Ok((End::Done(token.clone()), Statement::NoOp(Some(token))));
}
self.prefix_statement(token, abort)
Expand All @@ -124,7 +123,6 @@ impl<'a> Lexer<'a> {
match state {
End::Continue => {}
end => {
self.depth = 0;
return Ok((end, left));
}
}
Expand Down
2 changes: 2 additions & 0 deletions rust/nasl-syntax/src/variable_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ impl<'a> CommaGroup for Lexer<'a> {
End::Continue => {}
};
}

self.depth = 0;
Ok((end, params))
}
}
Expand Down
1 change: 1 addition & 0 deletions rust/nasl-syntax/tests/crash-prefix-recursion.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[3 [-p0[[S[[[[[[[[z4[[a[[[[,[[[[[[[[[[[z4[[a[[[,[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[[[,[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[[[,[[[[[[[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[,[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[Q[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[,[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[s[[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[,[[[[[[[[[[[[[[s[[[[[[[[[[[[[[[[[a[[[[,[[[[[[[[[Q[[[[[[[[[[[[[[[[[[[%[[[[[[[[[[
21 changes: 16 additions & 5 deletions rust/nasl-syntax/tests/missing_input_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@
#[cfg(test)]
mod test {

use nasl_syntax::{logger::NaslLogger, parse};

#[test]
fn validate_recursion_depth_to_prevent_stackoverflow() {
// Reported by Anon, VSS:4.0/AV:L/AC:L/AT:N/PR:N/UI:N/VC:N/VI:L/VA:H/SC:N/SI:L/SA:H
// Reported by @sepehrdaddev, VSS:4.0/AV:L/AC:L/AT:N/PR:N/UI:N/VC:N/VI:L/VA:H/SC:N/SI:L/SA:H
// Crash due to depth limit on recursion.
let code = include_str!("crash-recursion-depth.nasl");
assert_eq!(code.len(), 587);
let result = nasl_syntax::parse(code).collect::<Vec<_>>();
assert_eq!(result.len(), 1);
assert!(result[0].is_err())

assert_eq!(
result.iter().filter_map(|x| x.as_ref().ok()).count(),
0,
"crash-recursion-depth should not have any valid statements."
);

let code = include_str!("crash-prefix-recursion.nasl");
assert_eq!(code.len(), 515);
let result = nasl_syntax::parse(code).collect::<Vec<_>>();
assert_eq!(
result.iter().filter_map(|x| x.as_ref().ok()).count(),
0,
"crash-prefix-recursion should not have any valid statements."
);
}
}