Skip to content

Commit

Permalink
Fix: issue when multiple forks happen
Browse files Browse the repository at this point in the history
Given the nasl script:

```
set_kb_item(name: "port", value: 1);
set_kb_item(name: "port", value: 2);
set_kb_item(name: "port", value: 3);
set_kb_item(name: "port", value: 4);
set_kb_item(name: "port", value: 5);

set_kb_item(name: "host", value: "a");
set_kb_item(name: "host", value: "b");

p = get_kb_item("port");
h = get_kb_item("host");

display(h, ":", p);
```

it should print every combination of host and port.

This is done by dropping the requirement that only root processes are
allowed to fork.
  • Loading branch information
nichtsfrei committed Nov 18, 2024
1 parent e6abbc2 commit 9c42ee9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
13 changes: 13 additions & 0 deletions rust/examples/double-fork.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set_kb_item(name: "port", value: 1);
set_kb_item(name: "port", value: 2);
set_kb_item(name: "port", value: 3);
set_kb_item(name: "port", value: 4);
set_kb_item(name: "port", value: 5);

set_kb_item(name: "host", value: "a");
set_kb_item(name: "host", value: "b");

p = get_kb_item("port");
h = get_kb_item("host");

display(h, ":", p);
60 changes: 45 additions & 15 deletions rust/src/nasl/interpreter/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,16 @@ impl<'a> Interpreter<'a> {
Ok(if let Some(r) = x.pop() {
// this is a proposal for the case that the caller is immediately executing
// if not the position needs to be reset
if self.index == 0 {
let position = self.position().current_init_statement();
for i in x {
tracing::trace!(return_value=?i, return_position=?self.position(), interpreter_position=?position, "creating interpreter instance" );
self.run_specific.push(RunSpecific {
register: self.register().clone(),
position: position.clone(),
skip_until_return: Some((self.position().clone(), i)),
});
}
} else {
tracing::trace!(
index = self.index,
"we only allow expanding of executions (fork) on root instance"
);
let position = self.position().current_init_statement();
for i in x {
tracing::trace!(return_value=?i, return_position=?self.position(), interpreter_position=?position, "creating interpreter instance" );
self.run_specific.push(RunSpecific {
register: self.register().clone(),
position: position.clone(),
skip_until_return: Some((self.position().clone(), i)),
});
}

tracing::trace!(return_value=?r, "returning interpreter instance" );
r
} else {
Expand Down Expand Up @@ -117,4 +111,40 @@ mod tests {
t.ok("test(a: 1);", 1);
t.ok("test();", 0);
}

#[test]
fn multiple_forks() {
let mut t = TestBuilder::default();
t.run_all(
r#"
set_kb_item(name: "port", value: 1);
set_kb_item(name: "port", value: 2);
set_kb_item(name: "host", value: "a");
set_kb_item(name: "host", value: "b");
get_kb_item("port");
get_kb_item("host");
"#,
);

assert_eq!(t.results().len(), 10);
let results: Vec<_> = t
.results()
.into_iter()
.skip(4)
.filter_map(|x| x.ok())
.collect();

assert_eq!(
results,
vec![
2.into(),
1.into(),
"b".into(),
"b".into(),
"a".into(),
"a".into(),
]
);
}
}

0 comments on commit 9c42ee9

Please sign in to comment.