Skip to content

Commit

Permalink
Change: Update handling of CVEs for the new JSON API.
Browse files Browse the repository at this point in the history
- Handle references explicitly to remove raw_data.
- Add affected software configurations and references to the
response of get_info for CVEs when details are enabled.
  • Loading branch information
a-h-abdelsalam committed Oct 30, 2024
1 parent c90f9d9 commit 00340ea
Show file tree
Hide file tree
Showing 6 changed files with 1,126 additions and 237 deletions.
183 changes: 183 additions & 0 deletions src/gmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
#include "manage_report_configs.h"
#include "manage_report_formats.h"
#include "manage_tls_certificates.h"
#include "sql.h"
#include "utils.h"

#include <arpa/inet.h>
Expand Down Expand Up @@ -128,6 +129,7 @@
#include <gvm/util/fileutils.h>
#include <gvm/util/sshutils.h>
#include <gvm/util/authutils.h>
#include <gvm/util/cpeutils.h>

#undef G_LOG_DOMAIN
/**
Expand Down Expand Up @@ -13252,6 +13254,182 @@ handle_get_groups (gmp_parser_t *gmp_parser, GError **error)
set_client_state (CLIENT_AUTHENTIC);
}

/**
* @brief Print CPE match node with its matched CPEs.
*
* @param[in] node CPE match node to print.
* @param[in] buffer Buffer into which to print match node.
*/
static void
print_cpe_match_nodes_xml(resource_t node, GString *buffer)

Check warning on line 13264 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13264

Added line #L13264 was not covered by tests
{
iterator_t cpe_match_nodes, cpe_match_ranges;
init_iterator (&cpe_match_nodes,

Check warning on line 13267 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13267

Added line #L13267 was not covered by tests
"SELECT operator, negate FROM scap.cpe_match_nodes WHERE id = %llu;",
node);
xml_string_append (buffer, "<operator>%s</operator>", iterator_string (&cpe_match_nodes, 0)?: "");
xml_string_append (buffer, "<negate>%s</negate>", iterator_int (&cpe_match_nodes, 1)? "1" : "0");
cleanup_iterator (&cpe_match_nodes);

Check warning on line 13272 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13270-L13272

Added lines #L13270 - L13272 were not covered by tests

init_cpe_match_range_iterator (&cpe_match_ranges, node);

Check warning on line 13274 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13274

Added line #L13274 was not covered by tests
while (next (&cpe_match_ranges))
{
const gchar *vsi, *vse, *vei, *vee, *match_criteria_id, *range_uri_product;

xml_string_append (buffer, "<match_criteria>");
match_criteria_id = cpe_match_range_iterator_match_criteria_id (&cpe_match_ranges);

Check warning on line 13280 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13279-L13280

Added lines #L13279 - L13280 were not covered by tests
range_uri_product
= fs_cpe_to_uri_cpe (cpe_match_range_iterator_cpe (&cpe_match_ranges));
xml_string_append (buffer, "<match_string>%s</match_string>", range_uri_product?: "");
xml_string_append (buffer, "<vulnerable>%s</vulnerable>",
cpe_match_range_iterator_vulnerable (&cpe_match_ranges) != 0

Check warning on line 13285 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13282-L13285

Added lines #L13282 - L13285 were not covered by tests
? "1"
: "0");
vsi = cpe_match_range_iterator_version_start_incl(&cpe_match_ranges);
vse = cpe_match_range_iterator_version_start_excl(&cpe_match_ranges);
vei = cpe_match_range_iterator_version_end_incl(&cpe_match_ranges);
vee = cpe_match_range_iterator_version_end_excl(&cpe_match_ranges);

Check warning on line 13291 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13288-L13291

Added lines #L13288 - L13291 were not covered by tests

xml_string_append (buffer,

Check warning on line 13293 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13293

Added line #L13293 was not covered by tests
"<version_start_including>%s</version_start_including>",
vsi ?: "");
xml_string_append (buffer,

Check warning on line 13296 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13295-L13296

Added lines #L13295 - L13296 were not covered by tests
"<version_start_excluding>%s</version_start_excluding>",
vse ?: "");
xml_string_append (buffer,

Check warning on line 13299 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13298-L13299

Added lines #L13298 - L13299 were not covered by tests
"<version_end_including>%s</version_end_including>",
vei ?: "");
xml_string_append (buffer,

Check warning on line 13302 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13301-L13302

Added lines #L13301 - L13302 were not covered by tests
"<version_end_excluding>%s</version_end_excluding>",
vee ?: "");

Check warning on line 13304 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13304

Added line #L13304 was not covered by tests

iterator_t cpe_matches;
init_cpe_match_range_matches_iterator (&cpe_matches, match_criteria_id);
xml_string_append (buffer, "<matched_cpes>");

Check warning on line 13308 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13307-L13308

Added lines #L13307 - L13308 were not covered by tests

while (next (&cpe_matches))
{
const gchar *cpe_name_id;
iterator_t cpes;

cpe_name_id = cpe_matches_cpe_name_id(&cpe_matches);

Check warning on line 13315 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13315

Added line #L13315 was not covered by tests

init_iterator (&cpes,

Check warning on line 13317 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13317

Added line #L13317 was not covered by tests
"SELECT name, deprecated FROM scap.cves"
" WHERE cpe_name_id = %s;",
cpe_name_id);

const char* cpe = iterator_string (&cpes, 0);
int deprecated = iterator_int (&cpes, 1);
xml_string_append (buffer, "<cpe>");
xml_string_append (buffer, "<name>%s</name>", cpe?: "");
xml_string_append (buffer,

Check warning on line 13326 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13322-L13326

Added lines #L13322 - L13326 were not covered by tests
"<deprecated>%s</deprecated>",
deprecated ? "1" : "0");
if (deprecated)
{
iterator_t deprecated_by;
init_cpe_deprecated_by_iterator (&deprecated_by, cpe);

Check warning on line 13332 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13332

Added line #L13332 was not covered by tests
while (next (&deprecated_by))
{
xml_string_append (buffer,

Check warning on line 13335 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13335

Added line #L13335 was not covered by tests
"<deprecated_by cpe_id=\"%s\"/>",
cpe_deprecated_by_iterator_deprecated_by
(&deprecated_by));
}
cleanup_iterator (&deprecated_by);

Check warning on line 13340 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13340

Added line #L13340 was not covered by tests
}
xml_string_append (buffer, "</cpe>");
cleanup_iterator (&cpes);

Check warning on line 13343 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13342-L13343

Added lines #L13342 - L13343 were not covered by tests
}
xml_string_append (buffer, "</matched_cpes>");
xml_string_append (buffer, "</match_criteria>");
cleanup_iterator (&cpe_matches);

Check warning on line 13347 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13345-L13347

Added lines #L13345 - L13347 were not covered by tests
}
cleanup_iterator (&cpe_match_ranges);

Check warning on line 13349 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13349

Added line #L13349 was not covered by tests
}
/**
* @brief Print CVE affected software configurations
*
* @param[in] cve_uuid uuid of the CVE.
* @param[out] result Buffer into which to print.
*
*/
static void
print_cve_affected_software_configs_xml (gchar *cve_uuid, GString *result)

Check warning on line 13359 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13359

Added line #L13359 was not covered by tests
{
iterator_t cpe_match_root_nodes;
xml_string_append (result, "<configuration_nodes>");
init_cve_cpe_match_nodes_iterator (&cpe_match_root_nodes, cve_uuid);

Check warning on line 13363 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13362-L13363

Added lines #L13362 - L13363 were not covered by tests
while (next (&cpe_match_root_nodes))
{
result_t root_node;
iterator_t cpe_match_node_childs;
root_node = cpe_match_nodes_iterator_root_id (&cpe_match_root_nodes);
xml_string_append (result, "<node>");
print_cpe_match_nodes_xml(root_node, result);
init_cpe_match_node_childs_iterator (&cpe_match_node_childs, root_node);

Check warning on line 13371 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13368-L13371

Added lines #L13368 - L13371 were not covered by tests
while (next (&cpe_match_node_childs))
{
resource_t child_node;
child_node = cpe_match_node_childs_iterator_id (&cpe_match_node_childs);
xml_string_append (result, "<node>");
print_cpe_match_nodes_xml(child_node, result);
xml_string_append (result, "</node>");

Check warning on line 13378 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13375-L13378

Added lines #L13375 - L13378 were not covered by tests
}
xml_string_append (result, "</node>");
cleanup_iterator (&cpe_match_node_childs);

Check warning on line 13381 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13380-L13381

Added lines #L13380 - L13381 were not covered by tests
}
xml_string_append (result, "</configuration_nodes>");
cleanup_iterator (&cpe_match_root_nodes);

Check warning on line 13384 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13383-L13384

Added lines #L13383 - L13384 were not covered by tests
}

/**
* @brief Print CVE references
*
* @param[in] cve_uuid uuid of the CVE.
* @param[out] result Buffer into which to print.
*
*/
static void
print_cve_references_xml (gchar *cve_uuid, GString *result)

Check warning on line 13395 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13395

Added line #L13395 was not covered by tests
{
iterator_t references;
init_cve_reference_iterator (&references, cve_uuid);
xml_string_append (result, "<references>");

Check warning on line 13399 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13398-L13399

Added lines #L13398 - L13399 were not covered by tests
while (next (&references))
{
xml_string_append (result, "<reference>");
xml_string_append (result, "<url>%s</url>", cve_reference_iterator_url (&references));
xml_string_append (result, "<tags>");
const char * tags_array = cve_reference_iterator_tags (&references);

Check warning on line 13405 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13402-L13405

Added lines #L13402 - L13405 were not covered by tests
if(tags_array && strlen(tags_array) > 2)
{
char *trimmed_array = g_strndup (tags_array + 1, strlen (tags_array) - 2);

Check warning on line 13408 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13408

Added line #L13408 was not covered by tests
gchar **tags, **current_tag;
tags = g_strsplit (trimmed_array, ",", -1);
current_tag = tags;

Check warning on line 13411 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13410-L13411

Added lines #L13410 - L13411 were not covered by tests
while (*current_tag)
{
if (strlen (*current_tag) > 2 && (*current_tag)[0] == '"' && (*current_tag)[strlen (*current_tag) - 1] == '"')
{
char *trimmed_tag = g_strndup (*current_tag + 1, strlen (*current_tag) - 2);
xml_string_append (result, "<tag>%s</tag>", trimmed_tag);
g_free (trimmed_tag);

Check warning on line 13418 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13415-L13418

Added lines #L13415 - L13418 were not covered by tests
}
else
xml_string_append (result, "<tag>%s</tag>", *current_tag);
current_tag++;

Check warning on line 13422 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13421-L13422

Added lines #L13421 - L13422 were not covered by tests
}
g_strfreev (tags);
g_free (trimmed_array);

Check warning on line 13425 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13424-L13425

Added lines #L13424 - L13425 were not covered by tests
}
xml_string_append (result, "</tags>");
xml_string_append (result, "</reference>");

Check warning on line 13428 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13427-L13428

Added lines #L13427 - L13428 were not covered by tests
}
xml_string_append (result, "</references>");
cleanup_iterator (&references);

Check warning on line 13431 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13430-L13431

Added lines #L13430 - L13431 were not covered by tests
}
/**
* @brief Handle end of GET_INFO element.
*
Expand Down Expand Up @@ -13627,6 +13805,11 @@ handle_get_info (gmp_parser_t *gmp_parser, GError **error)
"</warning>");
}
g_string_append (result, "</cert>");

gchar *cve_uuid = g_strdup(get_iterator_uuid (&info));
print_cve_affected_software_configs_xml (cve_uuid, result);
print_cve_references_xml (cve_uuid, result);
g_free(cve_uuid);

Check warning on line 13812 in src/gmp.c

View check run for this annotation

Codecov / codecov/patch

src/gmp.c#L13809-L13812

Added lines #L13809 - L13812 were not covered by tests
}
}
else if (g_strcmp0 ("cert_bund_adv", get_info_data->type) == 0)
Expand Down
27 changes: 27 additions & 0 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,21 @@ app_locations_iterator_location (iterator_t*);
void
init_cpe_match_nodes_iterator (iterator_t*, const char *);

void
init_cve_cpe_match_nodes_iterator (iterator_t*, const char *);

void
init_cve_reference_iterator (iterator_t*, const char *);

const char*
cve_reference_iterator_url (iterator_t*);

const char*
cve_reference_iterator_tags (iterator_t*);

const char*
cve_reference_iterator_tags_count (iterator_t*);

long long int
cpe_match_nodes_iterator_root_id (iterator_t*);

Expand All @@ -1714,6 +1729,12 @@ init_cpe_match_range_iterator (iterator_t*, long long int);
const char*
cpe_match_range_iterator_cpe (iterator_t*);

const char*
cpe_match_range_iterator_match_criteria_id (iterator_t*);

const char*
cpe_match_range_iterator_status (iterator_t*);

const char*
cpe_match_range_iterator_version_start_incl (iterator_t*);

Expand All @@ -1729,6 +1750,12 @@ cpe_match_range_iterator_version_end_excl (iterator_t*);
int
cpe_match_range_iterator_vulnerable (iterator_t*);

void
init_cpe_match_range_matches_iterator (iterator_t*, const char *);

const char*
cpe_matches_cpe_name_id (iterator_t*);

void
init_host_details_cpe_product_iterator (iterator_t*, const char *, report_host_t);

Expand Down
39 changes: 32 additions & 7 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3546,20 +3546,31 @@ manage_db_init (const gchar *name)

sql ("CREATE TABLE scap2.cpe_match_nodes"
" (id SERIAL PRIMARY KEY,"
" parent_id INTEGER DEFAULT 0,"
" root_id INTEGER DEFAULT 0,"
" cve_id INTEGER DEFAULT 0,"
" operator text);");
" root_id integer DEFAULT 0,"
" cve_id integer DEFAULT 0,"
" operator text,"
" negate integer DEFAULT 0);");

sql ("CREATE TABLE scap2.cpe_nodes_match_criteria"

Check warning on line 3554 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3554

Added line #L3554 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" node_id integer DEFAULT 0,"
" vulnerable integer DEFAULT 0,"
" match_criteria_id text);");

sql ("CREATE TABLE scap2.cpe_match_range"
" (id SERIAL PRIMARY KEY,"
" node_id INTEGER DEFAULT 0,"
" vulnerable INTEGER DEFAULT 0,"
" match_criteria_id text,"
" cpe text DEFAULT NULL,"
" version_start_incl text DEFAULT NULL,"
" version_start_excl text DEFAULT NULL,"
" version_end_incl text DEFAULT NULL,"
" version_end_excl text DEFAULT NULL);");
" version_end_excl text DEFAULT NULL,"
" status text);");

sql ("CREATE TABLE scap2.cpe_matches"

Check warning on line 3570 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3570

Added line #L3570 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" match_criteria_id text,"
" cpe_name_id text);");

sql ("CREATE TABLE scap2.cpe_details"
" (id SERIAL PRIMARY KEY,"
Expand All @@ -3575,6 +3586,11 @@ manage_db_init (const gchar *name)
" epss DOUBLE PRECISION,"
" percentile DOUBLE PRECISION);");

sql ("CREATE TABLE scap2.cve_references"

Check warning on line 3589 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3589

Added line #L3589 was not covered by tests
" (id SERIAL PRIMARY KEY,"
" cve_id INTEGER,"
" url text,"
" tags text[]);");

/* Init tables. */

Expand Down Expand Up @@ -3624,6 +3640,15 @@ manage_db_add_constraints (const gchar *name)
sql ("ALTER TABLE scap2.epss_scores"
" ALTER cve SET NOT NULL,"
" ADD UNIQUE (cve);");

sql ("ALTER TABLE scap2.cve_references"

Check warning on line 3644 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3644

Added line #L3644 was not covered by tests
" ALTER cve_id SET NOT NULL,"
" ALTER url SET NOT NULL,"
" ADD UNIQUE (cve_id, url);");

sql ("ALTER TABLE scap2.cpe_match_range"

Check warning on line 3649 in src/manage_pg.c

View check run for this annotation

Codecov / codecov/patch

src/manage_pg.c#L3649

Added line #L3649 was not covered by tests
" ADD UNIQUE (match_criteria_id);");

}
else
{
Expand Down
Loading

0 comments on commit 00340ea

Please sign in to comment.