Skip to content

Commit

Permalink
Change: use globally stored main_kb instead of passing it around
Browse files Browse the repository at this point in the history
Refactors that instead of passing around pointer it uses a globally
stored one to be easier to read.
  • Loading branch information
nichtsfrei authored and jjnicola committed Nov 29, 2022
1 parent 9c7faec commit faf8fe5
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 79 deletions.
3 changes: 1 addition & 2 deletions misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ include_directories (${GLIB_INCLUDE_DIRS} ${GLIB_JSON_INCLUDE_DIRS}

set (FILES bpf_share.c ftp_funcs.c vendorversion.c network.c plugutils.c pcap.c
scan_id.c strutils.c table_driven_lsc.c ipc.c ipc_openvas.c ipc_pipe.c
user_agent.c scanneraux.c)

user_agent.c scanneraux.c kb_cache.c)

# On windows we are always PIC and stack-protector is replaces by DEP
# Also stack protection needs a shared library to work
Expand Down
59 changes: 59 additions & 0 deletions misc/kb_cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Portions Copyright (C) 2009-2022 Greenbone Networks GmbH
* Based on work Copyright (C) 1998 - 2003 Renaud Deraison
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* @file kb_cache.c
* @brief kb_cache.h implementation.
*/

#include "kb_cache.h"

// 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;
}
34 changes: 34 additions & 0 deletions misc/kb_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Portions Copyright (C) 2009-2022 Greenbone Networks GmbH
* Based on work Copyright (C) 1998 - 2007 Tenable Network Security, Inc.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* @file kb_cache.h
* @brief Header file to cache main_kb.
*/

#ifndef MISC_KB_CACHE_H
#define MISC_KB_CACHE_H
#include <gvm/util/kb.h>

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

#endif
4 changes: 3 additions & 1 deletion misc/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "../nasl/nasl_debug.h" /* for nasl_*_filename */
#include "kb_cache.h"

#include <arpa/inet.h> /* for inet_pton */
#include <errno.h>
Expand Down Expand Up @@ -1994,7 +1995,8 @@ open_sock_tcp (struct script_infos *args, unsigned int port, int timeout)
" was set to closed.",
host_port_ip_str,
plug_current_vhost () ? plug_current_vhost () : " ", port);
kb_item_push_str_with_main_kb_check (args->results,

kb_item_push_str_with_main_kb_check (get_main_kb (),
"internal/results", buffer);
}
}
Expand Down
49 changes: 5 additions & 44 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "plugutils.h"

#include "kb_cache.h"
#include "network.h" // for OPENVAS_ENCAPS_IP
#include "scan_id.h"
#include "support.h" // for g_memdup2 workaround
Expand Down Expand Up @@ -265,7 +266,7 @@ plug_get_host_fqdn (struct script_infos *args)
return g_strdup (current_vhost->value);
while (vhosts)
{
int ret = plug_fork_child (args->results, args->key);
int ret = plug_fork_child (get_main_kb (), args->key);

if (ret == 0)
{
Expand Down Expand Up @@ -421,39 +422,6 @@ 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 Down Expand Up @@ -673,7 +641,6 @@ proto_post_wrapped (const char *oid, struct script_infos *desc, int port,
GError *err = NULL;
GString *action_str;
gsize length;
kb_t kb;

/* Should not happen, just to avoid trouble stop here if no NVTI found */
if (!oid)
Expand Down Expand Up @@ -709,8 +676,8 @@ proto_post_wrapped (const char *oid, struct script_infos *desc, int port,
return;
}

kb = plug_get_results_kb (desc);
kb_item_push_str_with_main_kb_check (kb, "internal/results", data);

kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results", data);
g_free (data);
g_free (buffer);
g_string_free (action_str, TRUE);
Expand Down Expand Up @@ -1104,12 +1071,6 @@ plug_get_kb (struct script_infos *args)
return args->key;
}

kb_t
plug_get_results_kb (struct script_infos *args)
{
return args->results;
}

static void
plug_get_key_sigchld ()
{
Expand Down Expand Up @@ -1228,7 +1189,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 (args->results, kb);
int pret = plug_fork_child (get_main_kb (), kb);

if (pret == 0)
{
Expand Down
7 changes: 0 additions & 7 deletions misc/plugutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ host_get_port_state_udp (struct script_infos *, int);
*/
int check_kb_inconsistency (kb_t);

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

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

Expand Down Expand Up @@ -193,9 +189,6 @@ plug_replace_key_len (struct script_infos *, char *, int, void *, size_t);
kb_t
plug_get_kb (struct script_infos *);

kb_t
plug_get_results_kb (struct script_infos *);

void *
plug_get_key (struct script_infos *, char *, int *, size_t *, int);

Expand Down
3 changes: 1 addition & 2 deletions misc/scanneraux.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ struct script_infos
{
struct scan_globals *globals;
struct ipc_context *ipc_context;
kb_t key; // nvt_kb
kb_t results; // main_kb
kb_t key; // nvt_kb
nvti_t *nvti;
char *oid;
char *name;
Expand Down
1 change: 0 additions & 1 deletion nasl/nasl.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ init (struct in6_addr *ip, GSList *vhosts, kb_t kb)

infos->standalone = 1;
infos->key = kb;
infos->results = kb;
infos->ip = ip;
infos->vhosts = vhosts;
if (prefs_get_bool ("test_empty_vhost"))
Expand Down
31 changes: 15 additions & 16 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "attack.h"

#include "../misc/ipc_openvas.h"
#include "../misc/kb_cache.h"
#include "../misc/network.h" /* for auth_printf */
#include "../misc/nvt_categories.h" /* for ACT_INIT */
#include "../misc/pcap_openvas.h" /* for v6_is_local_ip */
Expand Down Expand Up @@ -85,7 +86,6 @@
struct attack_start_args
{
struct scan_globals *globals;
kb_t main_kb;
kb_t host_kb;
struct ipc_context *ipc_context; // use dto communicate with parent
plugins_scheduler_t sched;
Expand Down Expand Up @@ -525,7 +525,7 @@ launch_plugin (struct scan_globals *globals, struct scheduler_plugin *plugin,
char *oid, *name, *error = NULL, ip_str[INET6_ADDRSTRLEN];
nvti_t *nvti;

kb_lnk_reset (args->main_kb);
kb_lnk_reset (get_main_kb ());
addr6_to_str (ip, ip_str);
oid = plugin->oid;
nvti = nvticache_get_nvt (oid);
Expand Down Expand Up @@ -597,7 +597,7 @@ launch_plugin (struct scan_globals *globals, struct scheduler_plugin *plugin,
}
launch_error = 0;
pid = plugin_launch (globals, plugin, ip, vhosts, args->host_kb,
args->main_kb, nvti, &launch_error);
get_main_kb (), nvti, &launch_error);
if (launch_error == ERR_NO_FREE_SLOT || launch_error == ERR_CANT_FORK)
{
plugin->running_state = PLUGIN_STATUS_UNRUN;
Expand Down Expand Up @@ -634,8 +634,8 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
host_kb = args->host_kb;
host_vhosts = args->host->vhosts;
globals->host_pid = getpid ();
host_set_time (args->main_kb, ip_str, "HOST_START");
kb_lnk_reset (args->main_kb);
host_set_time (get_main_kb (), ip_str, "HOST_START");
kb_lnk_reset (get_main_kb ());
setproctitle ("openvas: testing %s", ip_str);
kb_lnk_reset (args->host_kb);

Expand All @@ -652,7 +652,7 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
return;
}

if (check_kb_inconsistency (args->main_kb) != 0)
if (check_kb_inconsistency (get_main_kb ()) != 0)
{
// As long as we don't have a proper communication channel
// to our ancestors we just kill our parent and ourselves
Expand Down Expand Up @@ -688,9 +688,9 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
"<description/><type/><name/></source></detail></host>",
ip_str);
kb_item_push_str_with_main_kb_check (
args->main_kb, "internal/results", buffer);
get_main_kb (), "internal/results", buffer);

comm_send_status_host_dead (args->main_kb, ip_str);
comm_send_status_host_dead (get_main_kb (), ip_str);
goto host_died;
}
else if (e == ERR_NO_FREE_SLOT)
Expand Down Expand Up @@ -729,7 +729,7 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
&& !scan_is_stopped ())
{
last_status = (cur_plug * 100) / num_plugs + 2;
if (comm_send_status (args->main_kb, ip_str, cur_plug, num_plugs)
if (comm_send_status (get_main_kb (), ip_str, cur_plug, num_plugs)
< 0)
goto host_died;
}
Expand All @@ -740,7 +740,7 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
else if (plugin != NULL && plugin == PLUG_RUNNING)
/* 50 milliseconds. */
usleep (50000);
pluginlaunch_wait_for_free_process (args->main_kb, args->host_kb);
pluginlaunch_wait_for_free_process (get_main_kb (), args->host_kb);
}

if (!scan_is_stopped () && prefs_get_bool ("table_driven_lsc")
Expand All @@ -754,17 +754,17 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
buffer, sizeof (buffer),
"ERRMSG|||%s||| ||| ||| ||| Unable to launch table driven lsc",
ip_str);
kb_item_push_str_with_main_kb_check (args->main_kb,
kb_item_push_str_with_main_kb_check (get_main_kb (),
"internal/results", buffer);
g_warning ("%s: Unable to launch table driven LSC", __func__);
}
}

pluginlaunch_wait (args->main_kb, args->host_kb);
pluginlaunch_wait (get_main_kb (), args->host_kb);
if (!scan_is_stopped ())
{
int ret;
ret = comm_send_status (args->main_kb, ip_str, num_plugs, num_plugs);
ret = comm_send_status (get_main_kb (), ip_str, num_plugs, num_plugs);
if (ret == 0)
all_plugs_launched = 1;
}
Expand All @@ -776,7 +776,7 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
globals->scan_id, ip_str);
pluginlaunch_stop ();
plugins_scheduler_free (args->sched);
host_set_time (args->main_kb, ip_str, "HOST_END");
host_set_time (get_main_kb (), ip_str, "HOST_END");
}

/*
Expand Down Expand Up @@ -913,7 +913,7 @@ attack_start (struct ipc_context *ipcc, struct attack_start_args *args)
struct in6_addr hostip;
struct timeval then;
kb_t kb = args->host_kb;
kb_t main_kb = args->main_kb;
kb_t main_kb = get_main_kb ();
int ret, ret_host_auth;
args->ipc_context = ipcc;

Expand Down Expand Up @@ -1369,7 +1369,6 @@ attack_network (struct scan_globals *globals)
args.globals = globals;
args.sched = sched;
args.host_kb = arg_host_kb;
args.main_kb = main_kb;

forkagain:
pid = create_ipc_process ((ipc_process_func) attack_start, &args);
Expand Down
Loading

0 comments on commit faf8fe5

Please sign in to comment.