Skip to content

Commit

Permalink
Fix: use main_kb to verify scan_id for inconsistencies
Browse files Browse the repository at this point in the history
This commit fixes the issue that the inconsistencies were not
necessarily checked against the shared database between ospd and
openvas.

Since ospd sets the scanner id into redis before calling openvas it is
necessary to use the main_kb to verify the locally set scanid with the
one in the database.

This is done by globally storing the main_kb via plugutils `set_main_kb`
and getting it via `get_main_kb`.
  • Loading branch information
nichtsfrei committed Nov 9, 2022
1 parent 5c165b3 commit 11e06e4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 23 deletions.
61 changes: 48 additions & 13 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ plug_current_vhost (void)
return current_vhost->value;
}

static int plug_fork_child (kb_t);
static int plug_fork_child (kb_t, kb_t);

void
plug_set_dep (struct script_infos *args, const char *depname)
Expand Down Expand Up @@ -265,7 +265,7 @@ plug_get_host_fqdn (struct script_infos *args)
return g_strdup (current_vhost->value);
while (vhosts)
{
int ret = plug_fork_child (args->key);
int ret = plug_fork_child (args->results, args->key);

if (ret == 0)
{
Expand Down Expand Up @@ -419,6 +419,39 @@ check_kb_inconsistency (kb_t main_kb)
return -3;
}

// shared database between openvas and ospd.
kb_t main_kb = NULL;

/**
* @brief sets the shared database between ospd and openvas as a main_kb for
* further usage.
* @description this sets the given kb as a main_kb global variable. It is NOT
* threadsafe and must be called after each reconnect or fork.
*
* @param main_kb Current main kb.
*
*/
void
set_main_kb (kb_t kb)
{
main_kb = kb;
}

/**
* @brief gets the main_kb.
* @description returns the previously set main_kb; when asserts are enabled it
* will abort when main_kb is not set. However each usage must check if the
* return is NULL or not.
*
* @return the set main_kb
*/
kb_t
get_main_kb (void)
{
assert (main_kb);
return main_kb;
}

/**
* @brief calls check_kb_inconsistency and logs as debug when local scan_id is
missing.
Expand All @@ -435,9 +468,10 @@ check_kb_inconsistency (kb_t main_kb)
* @return 0 on success, -1 on inconsistency.
*/
static int
check_kb_inconsistency_log (kb_t kb, const char *name)
check_kb_inconsistency_log (const char *name)
{
char *current_scan_id;
kb_t kb = get_main_kb ();
int result = check_kb_inconsistency (kb);
switch (result)
{
Expand All @@ -454,8 +488,8 @@ check_kb_inconsistency_log (kb_t kb, const char *name)
// openvas-nasl calls
break;
case -2:
g_debug ("%s: No internal/scanid; this indicates wrongful usage",
__func__);
g_warning ("%s: No internal/scanid found.", __func__);
return -1;
break;
default:
{
Expand Down Expand Up @@ -483,7 +517,7 @@ check_kb_inconsistency_log (kb_t kb, const char *name)
int
kb_check_push_str (kb_t kb, const char *name, const char *value)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_push_str (kb, name, value) : -1;
}

Expand All @@ -505,7 +539,7 @@ kb_check_push_str (kb_t kb, const char *name, const char *value)
int
kb_check_set_str (kb_t kb, const char *name, const char *value, size_t len)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_set_str (kb, name, value, len) : -1;
}

Expand All @@ -528,7 +562,7 @@ int
kb_check_add_str_unique (kb_t kb, const char *name, const char *value,
size_t len, int pos)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_add_str_unique (kb, name, value, len, pos) : -1;
}

Expand All @@ -550,7 +584,7 @@ kb_check_add_str_unique (kb_t kb, const char *name, const char *value,
int
kb_check_set_int (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_set_int (kb, name, value) : -1;
}

Expand All @@ -572,7 +606,7 @@ kb_check_set_int (kb_t kb, const char *name, int value)
int
kb_check_add_int (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_add_int (kb, name, value) : -1;
}

Expand All @@ -594,7 +628,7 @@ kb_check_add_int (kb_t kb, const char *name, int value)
int
kb_check_add_int_unique (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log (kb, name);
int result = check_kb_inconsistency_log (name);
return result == 0 ? kb_item_add_int_unique (kb, name, value) : -1;
}

Expand Down Expand Up @@ -1085,7 +1119,7 @@ sig_n (int signo, void (*fnc) (int))
* failure
*/
static int
plug_fork_child (kb_t kb)
plug_fork_child (kb_t main, kb_t kb)
{
pid_t pid;

Expand All @@ -1095,6 +1129,7 @@ plug_fork_child (kb_t kb)
sig_n (SIGTERM, _exit);
mqtt_reset ();
kb_lnk_reset (kb);
kb_lnk_reset (main);
nvticache_reset ();
srand48 (getpid () + getppid () + time (NULL));
return 0;
Expand Down Expand Up @@ -1174,7 +1209,7 @@ plug_get_key (struct script_infos *args, char *name, int *type, size_t *len,
res_list = res;
while (res)
{
int pret = plug_fork_child (kb);
int pret = plug_fork_child (args->results, kb);

if (pret == 0)
{
Expand Down
5 changes: 4 additions & 1 deletion misc/plugutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,12 @@ host_get_port_state_udp (struct script_infos *, int);
/*
* Inter Plugins Communication functions
*/

int check_kb_inconsistency (kb_t);

void set_main_kb (kb_t);
kb_t
get_main_kb (void);

int
kb_check_push_str (kb_t, const char *, const char *);

Expand Down
4 changes: 2 additions & 2 deletions misc/scanneraux.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ struct script_infos
{
struct scan_globals *globals;
struct ipc_context *ipc_context;
kb_t key;
kb_t results;
kb_t key; // nvt_kb
kb_t results; // main_kb
nvti_t *nvti;
char *oid;
char *name;
Expand Down
4 changes: 3 additions & 1 deletion src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ connect_main_kb (kb_t *main_kb)

*main_kb = kb_direct_conn (prefs_get ("db_address"), i);
if (main_kb)
return 0;
{
return 0;
}

g_warning ("Not possible to get the main kb connection.");
return -1;
Expand Down
4 changes: 4 additions & 0 deletions src/openvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,13 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals)
return -1;

snprintf (key, sizeof (key), "internal/%s/scanprefs", globals->scan_id);

kb = kb_find (prefs_get ("db_address"), key);
if (!kb)
return -1;
// 2022-10-19: currently internal/%s/scanprefs are set by ospd which is the
// main_kb in our context
set_main_kb (kb);

res = kb_item_get_all (kb, key);
if (!res)
Expand Down
14 changes: 8 additions & 6 deletions src/processes.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "processes.h"

#include "../misc/plugutils.h"
#include "debug_utils.h" /* for init_sentry() */
#include "sighand.h"

Expand Down Expand Up @@ -88,7 +89,7 @@ procs_cleanup_children (void)
*
*/
static void
clean_procs ()
clean_procs (void)
{
ipc_destroy_contexts (ipcc);
ipcc = NULL;
Expand Down Expand Up @@ -138,7 +139,7 @@ terminate_process (pid_t pid)
*
*/
void
procs_terminate_childs ()
procs_terminate_childs (void)
{
if (ipcc == NULL)
return;
Expand Down Expand Up @@ -174,7 +175,7 @@ init_child_signal_handlers (void)
}

static void
pre_fork_fun_call (struct ipc_context *ctx, void *args)
pre_fn_call (struct ipc_context *ctx, void *args)
{
(void) ctx;
(void) args;
Expand All @@ -188,11 +189,12 @@ pre_fork_fun_call (struct ipc_context *ctx, void *args)
mqtt_reset ();
init_sentry ();
srand48 (getpid () + getppid () + (long) time (NULL));

g_debug ("%s: exit", __func__);
}

static void
post_fork_fun_call (struct ipc_context *ctx, void *args)
post_fn_call (struct ipc_context *ctx, void *args)
{
(void) ctx;
(void) args;
Expand Down Expand Up @@ -240,8 +242,8 @@ create_ipc_process (ipc_process_func func, void *args)
if (ipcc == NULL)
ipcc = ipc_contexts_init (10);

ec.pre_func = (ipc_process_func) &pre_fork_fun_call;
ec.post_func = (ipc_process_func) &post_fork_fun_call;
ec.pre_func = (ipc_process_func) &pre_fn_call;
ec.post_func = (ipc_process_func) &post_fn_call;
ec.func = (ipc_process_func) func;
ec.func_arg = args;
// check for exited processes and clean file descriptor
Expand Down

0 comments on commit 11e06e4

Please sign in to comment.