Skip to content

Commit

Permalink
Fix: Set the right status when a scan fails because an invalid host l…
Browse files Browse the repository at this point in the history
…ist (#1657)

* Fix: Return with EXIT_FAILURE if the scan fails for any reason. E.g. Invalid target list

* Fix: override scan status if the process exit code is not 0
  • Loading branch information
jjnicola authored Jun 24, 2024
1 parent fe205b2 commit f6d5f2c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
15 changes: 12 additions & 3 deletions rust/openvas/src/openvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl ScanResultFetcher for Scanner {
host_info: Some(hosts_info),
};

let scan_res = ScanResults {
let mut scan_res = ScanResults {
id: scan_id.to_string(),
status: st,
results: all_results
Expand All @@ -369,14 +369,23 @@ impl ScanResultFetcher for Scanner {
.collect(),
};

// If the scan finished, release
// If the scan finished, release. Openvas "finished" status is translated todo
// Succeeded. It is necessary to read the exit code to know if it failed.
if status == Phase::Succeeded {
let mut scan = match self.remove_running(scan_id) {
Some(scan) => scan.0,
None => return Err(OpenvasError::ScanNotFound(scan_id.to_string()).into()),
};

scan.wait().map_err(OpenvasError::CmdError)?;
// Read openvas scanner exit code and if failed, reset the status to Failed.
let exit_status = scan.wait().map_err(OpenvasError::CmdError)?;
if let Some(code) = exit_status.code() {
if code != 0 {
scan_res.status.status = Phase::Failed;
scan_res.status.start_time = scan_res.status.end_time;
scan_res.status.host_info = None;
}
}

redis_help
.release()
Expand Down
25 changes: 19 additions & 6 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,9 @@ scan_stop_cleanup ()

/**
* @brief Attack a whole network.
* return 0 on successes, -1 if there was a critical error.
*/
void
int
attack_network (struct scan_globals *globals)
{
int max_hosts = 0, max_checks;
Expand All @@ -1179,6 +1180,7 @@ attack_network (struct scan_globals *globals)
kb_t arg_host_kb, main_kb;
GSList *unresolved;
char buf[96];
int error = 0;

check_deprecated_prefs ();

Expand All @@ -1191,13 +1193,16 @@ attack_network (struct scan_globals *globals)
gettimeofday (&then, NULL);

if (check_kb_access ())
return;

{
error = -1;
return error;
}
/* Init and check Target List */
hostlist = prefs_get ("TARGET");
if (hostlist == NULL)
{
return;
error = -1;
return error;
}

/* Verify the port range is a valid one */
Expand All @@ -1213,7 +1218,8 @@ attack_network (struct scan_globals *globals)
"Scan terminated.");
set_scan_status ("finished");

return;
error = -1;
return error;
}

/* Initialize the attack. */
Expand All @@ -1224,7 +1230,9 @@ attack_network (struct scan_globals *globals)
if (!sched)
{
g_message ("Couldn't initialize the plugin scheduler");
return;

error = -1;
return error;
}

if (plugins_init_error > 0)
Expand Down Expand Up @@ -1256,6 +1264,8 @@ attack_network (struct scan_globals *globals)
"HOSTS_COUNT");
kb_lnk_reset (main_kb);
g_warning ("Invalid target list. Scan terminated.");

error = -1;
goto stop;
}

Expand Down Expand Up @@ -1300,6 +1310,7 @@ attack_network (struct scan_globals *globals)
host = gvm_hosts_next (hosts);
if (host == NULL)
goto stop;

hosts_init (max_hosts);

g_message ("Vulnerability scan %s started: Target has %d hosts: "
Expand Down Expand Up @@ -1555,4 +1566,6 @@ attack_network (struct scan_globals *globals)
gvm_hosts_free (alive_hosts_list);

set_scan_status ("finished");

return error;
}
2 changes: 1 addition & 1 deletion src/attack.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <gvm/util/kb.h>

void
int
attack_network (struct scan_globals *);

#endif
9 changes: 8 additions & 1 deletion src/openvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ openvas (int argc, char *argv[], char *env[])
/* openvas --scan-start */
if (scan_id)
{
int attack_error = 0;
struct scan_globals *globals;
set_scan_id (g_strdup (scan_id));
globals = g_malloc0 (sizeof (struct scan_globals));
Expand All @@ -635,13 +636,19 @@ openvas (int argc, char *argv[], char *env[])
destroy_scan_globals (globals);
return EXIT_FAILURE;
}
attack_network (globals);
attack_error = attack_network (globals);

gvm_close_sentry ();
destroy_scan_globals (globals);
#ifdef LOG_REFERENCES_AVAILABLE
free_log_reference ();
#endif // LOG_REFERENCES_AVAILABLE

if (attack_error)
{
g_warning ("Scan ending with FAILURE status");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

Expand Down

0 comments on commit f6d5f2c

Please sign in to comment.