Greenbone Vulnerability Management Libraries 22.8.0
cvss.c File Reference

CVSS utility functions. More...

#include "cvss.h"
#include <glib.h>
#include <math.h>
#include <strings.h>
Include dependency graph for cvss.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  impact_item
 Describe a CVSS impact element. More...
 
struct  cvss
 Describe a CVSS metrics. More...
 

Macros

#define G_LOG_DOMAIN   "libgvm base"
 GLib log domain.
 
#define AV_NETWORK   1.0
 AccessVector (AV) Constants.
 
#define AV_ADJACENT_NETWORK   0.646
 
#define AV_LOCAL   0.395
 
#define AC_LOW   0.71
 AccessComplexity (AC) Constants.
 
#define AC_MEDIUM   0.61
 
#define AC_HIGH   0.35
 
#define Au_MULTIPLE_INSTANCES   0.45
 Authentication (Au) Constants.
 
#define Au_SINGLE_INSTANCE   0.56
 
#define Au_NONE   0.704
 
#define C_NONE   0.0
 ConfidentialityImpact (C) Constants.
 
#define C_PARTIAL   0.275
 
#define C_COMPLETE   0.660
 
#define I_NONE   0.0
 IntegrityImpact (I) Constants.
 
#define I_PARTIAL   0.275
 
#define I_COMPLETE   0.660
 
#define A_NONE   0.0
 AvailabilityImpact (A) Constants.
 
#define A_PARTIAL   0.275
 
#define A_COMPLETE   0.660
 

Enumerations

enum  base_metrics {
  A , I , C , Au ,
  AC , AV
}
 Base metrics. More...
 

Functions

static double get_cvss_score_from_base_metrics_v3 (const char *cvss_str)
 Calculate CVSS Score.
 
static int toenum (const char *str, enum base_metrics *res)
 Determine base metric enumeration from a string.
 
static double get_impact_subscore (const struct cvss *cvss)
 Calculate Impact Sub Score.
 
static double get_exploitability_subscore (const struct cvss *cvss)
 Calculate Exploitability Sub Score.
 
static int set_impact_from_str (const char *value, enum base_metrics metric, struct cvss *cvss)
 Set impact score from string representation.
 
static double __get_cvss_score (struct cvss *cvss)
 Final CVSS score computation helper.
 
double get_cvss_score_from_base_metrics (const char *cvss_str)
 Calculate CVSS Score.
 
static double roundup (double cvss)
 Round final score as in spec.
 
static double v3_impact (const char *value)
 Get impact.
 

Variables

static const struct impact_item impact_map [][3]
 

Detailed Description

CVSS utility functions.

This file contains utility functions for handling CVSS v2 and v3. get_cvss_score_from_base_metrics calculates the CVSS base score from a CVSS base vector.

CVSS v3.1:

See equations at https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator and constants at https://www.first.org/cvss/v3.1/specification-document (section 7.4. Metric Values).

CVSS v3.0:

See equations at https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator and constants at https://www.first.org/cvss/v3.0/specification-document (section 8.4. Metric Levels).

CVSS v2:

The base equation is the foundation of CVSS scoring. The base equation is: BaseScore6 = round_to_1_decimal(((0.6*Impact)+(0.4*Exploitability)–1.5)*f(Impact))

Impact = 10.41*(1-(1-ConfImpact)*(1-IntegImpact)*(1-AvailImpact))

Exploitability = 20* AccessVector*AccessComplexity*Authentication

f(impact)= 0 if Impact=0, 1.176 otherwise AccessVector = case AccessVector of requires local access: 0.395 adjacent network accessible: 0.646 network accessible: 1.0 AccessComplexity = case AccessComplexity of high: 0.35 medium: 0.61 low: 0.71 Authentication = case Authentication of requires multiple instances of authentication: 0.45 requires single instance of authentication: 0.56 requires no authentication: 0.704 ConfImpact = case ConfidentialityImpact of none: 0.0 partial: 0.275 complete: 0.660 IntegImpact = case IntegrityImpact of none: 0.0 partial: 0.275 complete: 0.660 AvailImpact = case AvailabilityImpact of none: 0.0 partial: 0.275 complete: 0.660

Definition in file cvss.c.

Macro Definition Documentation

◆ A_COMPLETE

#define A_COMPLETE   0.660

Complete Availability Impact.

Definition at line 125 of file cvss.c.

◆ A_NONE

#define A_NONE   0.0

AvailabilityImpact (A) Constants.

No Availability Impact.

Definition at line 123 of file cvss.c.

◆ A_PARTIAL

#define A_PARTIAL   0.275

Partial Availability Impact.

Definition at line 124 of file cvss.c.

◆ AC_HIGH

#define AC_HIGH   0.35

Access Complexity High.

Definition at line 97 of file cvss.c.

◆ AC_LOW

#define AC_LOW   0.71

AccessComplexity (AC) Constants.

Access Complexity Low.

Definition at line 95 of file cvss.c.

◆ AC_MEDIUM

#define AC_MEDIUM   0.61

Access Complexity Medium.

Definition at line 96 of file cvss.c.

◆ Au_MULTIPLE_INSTANCES

#define Au_MULTIPLE_INSTANCES   0.45

Authentication (Au) Constants.

Authentication multiple instances.

Definition at line 102 of file cvss.c.

◆ Au_NONE

#define Au_NONE   0.704

No Authentication.

Definition at line 104 of file cvss.c.

◆ Au_SINGLE_INSTANCE

#define Au_SINGLE_INSTANCE   0.56

Authentication single instances.

Definition at line 103 of file cvss.c.

◆ AV_ADJACENT_NETWORK

#define AV_ADJACENT_NETWORK   0.646

Access Vector Adjacent Network.

Definition at line 89 of file cvss.c.

◆ AV_LOCAL

#define AV_LOCAL   0.395

Access Vector Local.

Definition at line 90 of file cvss.c.

◆ AV_NETWORK

#define AV_NETWORK   1.0

AccessVector (AV) Constants.

Access Vector Network.

Definition at line 88 of file cvss.c.

◆ C_COMPLETE

#define C_COMPLETE   0.660

Complete Confidentiality Impact.

Definition at line 111 of file cvss.c.

◆ C_NONE

#define C_NONE   0.0

ConfidentialityImpact (C) Constants.

No Confidentiality Impact.

Definition at line 109 of file cvss.c.

◆ C_PARTIAL

#define C_PARTIAL   0.275

Partial Confidentiality Impact.

Definition at line 110 of file cvss.c.

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "libgvm base"

GLib log domain.

Definition at line 75 of file cvss.c.

◆ I_COMPLETE

#define I_COMPLETE   0.660

Complete Integrity Impact.

Definition at line 118 of file cvss.c.

◆ I_NONE

#define I_NONE   0.0

IntegrityImpact (I) Constants.

No Integrity Impact.

Definition at line 116 of file cvss.c.

◆ I_PARTIAL

#define I_PARTIAL   0.275

Partial Integrity Impact.

Definition at line 117 of file cvss.c.

Enumeration Type Documentation

◆ base_metrics

Base metrics.

Enumerator

Availability Impact.

Integrity Impact.

Confidentiality Impact.

Au 

Authentication.

AC 

Access Complexity.

AV 

Access Vector.

Definition at line 131 of file cvss.c.

132{
133 A,
134 I,
135 C,
136 Au,
137 AC,
138 AV
139};
@ AC
Definition: cvss.c:137
@ A
Definition: cvss.c:133
@ C
Definition: cvss.c:135
@ Au
Definition: cvss.c:136
@ I
Definition: cvss.c:134
@ AV
Definition: cvss.c:138

Function Documentation

◆ __get_cvss_score()

static double __get_cvss_score ( struct cvss cvss)
static

Final CVSS score computation helper.

Parameters
[in]cvssThe CVSS structure that contains the different metrics and associated scores.
Returns
the CVSS score, as a double.

Definition at line 334 of file cvss.c.

335{
336 double impact = 1.176;
337 double impact_sub;
338 double exploitability_sub;
339
340 impact_sub = get_impact_subscore (cvss);
341 exploitability_sub = get_exploitability_subscore (cvss);
342
343 if (impact_sub < 0.1)
344 impact = 0.0;
345
346 return (((0.6 * impact_sub) + (0.4 * exploitability_sub) - 1.5) * impact)
347 + 0.0;
348}
static double get_exploitability_subscore(const struct cvss *cvss)
Calculate Exploitability Sub Score.
Definition: cvss.c:261
static double get_impact_subscore(const struct cvss *cvss)
Calculate Impact Sub Score.
Definition: cvss.c:244
Describe a CVSS metrics.
Definition: cvss.c:154

References get_exploitability_subscore(), and get_impact_subscore().

Referenced by get_cvss_score_from_base_metrics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cvss_score_from_base_metrics()

double get_cvss_score_from_base_metrics ( const char *  cvss_str)

Calculate CVSS Score.

Parameters
cvss_strBase vector string from which to compute score.
Returns
The resulting score. -1 upon error during parsing.

Definition at line 358 of file cvss.c.

359{
360 struct cvss cvss;
361 char *token, *base_str, *base_metrics;
362
363 if (cvss_str == NULL)
364 return -1.0;
365
366 if (g_str_has_prefix (cvss_str, "CVSS:3.1/")
367 || g_str_has_prefix (cvss_str, "CVSS:3.0/"))
369 + strlen ("CVSS:3.X/"));
370
371 memset (&cvss, 0x00, sizeof (struct cvss));
372
373 base_str = base_metrics = g_strdup_printf ("%s/", cvss_str);
374
375 while ((token = strchr (base_metrics, '/')) != NULL)
376 {
377 char *token2 = strtok (base_metrics, ":");
378 char *metric_name = token2;
379 char *metric_value;
380 enum base_metrics mval;
381 int rc;
382
383 *token++ = '\0';
384
385 if (metric_name == NULL)
386 goto ret_err;
387
388 metric_value = strtok (NULL, ":");
389
390 if (metric_value == NULL)
391 goto ret_err;
392
393 rc = toenum (metric_name, &mval);
394 if (rc)
395 goto ret_err;
396
397 if (set_impact_from_str (metric_value, mval, &cvss))
398 goto ret_err;
399
400 base_metrics = token;
401 }
402
403 g_free (base_str);
404 return __get_cvss_score (&cvss);
405
406ret_err:
407 g_free (base_str);
408 return (double) -1;
409}
static int set_impact_from_str(const char *value, enum base_metrics metric, struct cvss *cvss)
Set impact score from string representation.
Definition: cvss.c:277
static int toenum(const char *str, enum base_metrics *res)
Determine base metric enumeration from a string.
Definition: cvss.c:211
base_metrics
Base metrics.
Definition: cvss.c:132
static double get_cvss_score_from_base_metrics_v3(const char *)
Calculate CVSS Score.
Definition: cvss.c:467
static double __get_cvss_score(struct cvss *cvss)
Final CVSS score computation helper.
Definition: cvss.c:334

References __get_cvss_score(), get_cvss_score_from_base_metrics_v3(), set_impact_from_str(), and toenum().

Referenced by Ensure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_cvss_score_from_base_metrics_v3()

static double get_cvss_score_from_base_metrics_v3 ( const char *  cvss_str)
static

Calculate CVSS Score.

Parameters
cvss_strVector from which to compute score, without prefix.
Returns
CVSS score, or -1 on error.

Definition at line 467 of file cvss.c.

468{
469 gchar **split, **point;
470 int scope_changed;
471 double impact_conf, impact_integ, impact_avail;
472 double vector, complexity, privilege, user;
473 double isc_base, impact, exploitability, base;
474
475 /* https://nvd.nist.gov/vuln-metrics/cvss/v3-calculator
476 * https://www.first.org/cvss/v3.1/specification-document
477 * https://www.first.org/cvss/v3.0/specification-document */
478
479 scope_changed = -1;
480 impact_conf = -1.0;
481 impact_integ = -1.0;
482 impact_avail = -1.0;
483 vector = -1.0;
484 complexity = -1.0;
485 privilege = -1.0;
486 user = -1.0;
487
488 /* AV:N/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:N */
489
490 split = g_strsplit (cvss_str, "/", 0);
491 point = split;
492 while (*point)
493 {
494 /* Scope. */
495 if (strncasecmp ("S:", *point, 2) == 0)
496 {
497 if (strcasecmp (*point + 2, "U") == 0)
498 scope_changed = 0;
499 else if (strcasecmp (*point + 2, "C") == 0)
500 scope_changed = 1;
501 }
502
503 /* Confidentiality. */
504 if (strncasecmp ("C:", *point, 2) == 0)
505 impact_conf = v3_impact (*point + 2);
506
507 /* Integrity. */
508 if (strncasecmp ("I:", *point, 2) == 0)
509 impact_integ = v3_impact (*point + 2);
510
511 /* Availability. */
512 if (strncasecmp ("A:", *point, 2) == 0)
513 impact_avail = v3_impact (*point + 2);
514
515 /* Attack Vector. */
516 if (strncasecmp ("AV:", *point, 3) == 0)
517 {
518 if (strcasecmp (*point + 3, "N") == 0)
519 vector = 0.85;
520 else if (strcasecmp (*point + 3, "A") == 0)
521 vector = 0.62;
522 else if (strcasecmp (*point + 3, "L") == 0)
523 vector = 0.55;
524 else if (strcasecmp (*point + 3, "P") == 0)
525 vector = 0.2;
526 }
527
528 /* Attack Complexity. */
529 if (strncasecmp ("AC:", *point, 3) == 0)
530 {
531 if (strcasecmp (*point + 3, "L") == 0)
532 complexity = 0.77;
533 else if (strcasecmp (*point + 3, "H") == 0)
534 complexity = 0.44;
535 }
536
537 /* Privileges Required. */
538 if (strncasecmp ("PR:", *point, 3) == 0)
539 {
540 if (strcasecmp (*point + 3, "N") == 0)
541 privilege = 0.85;
542 else if (strcasecmp (*point + 3, "L") == 0)
543 privilege = 0.62;
544 else if (strcasecmp (*point + 3, "H") == 0)
545 privilege = 0.27;
546 else
547 privilege = -1.0;
548 }
549
550 /* User Interaction. */
551 if (strncasecmp ("UI:", *point, 3) == 0)
552 {
553 if (strcasecmp (*point + 3, "N") == 0)
554 user = 0.85;
555 else if (strcasecmp (*point + 3, "R") == 0)
556 user = 0.62;
557 }
558
559 point++;
560 }
561
562 g_strfreev (split);
563
564 /* All of the base metrics are required. */
565
566 if (scope_changed == -1 || impact_conf == -1.0 || impact_integ == -1.0
567 || impact_avail == -1.0 || vector == -1.0 || complexity == -1.0
568 || privilege == -1.0 || user == -1.0)
569 return -1.0;
570
571 /* Privileges Required has a special case for S:C. */
572
573 if (scope_changed && privilege == 0.62)
574 privilege = 0.68;
575 else if (scope_changed && privilege == 0.27)
576 privilege = 0.5;
577
578 /* Impact. */
579
580 isc_base = 1 - ((1 - impact_conf) * (1 - impact_integ) * (1 - impact_avail));
581
582 if (scope_changed)
583 impact = 7.52 * (isc_base - 0.029) - 3.25 * pow ((isc_base - 0.02), 15);
584 else
585 impact = 6.42 * isc_base;
586
587 if (impact <= 0)
588 return 0.0;
589
590 /* Exploitability. */
591
592 exploitability = 8.22 * vector * complexity * privilege * user;
593
594 /* Final. */
595
596 if (scope_changed)
597 base = 1.08 * (impact + exploitability);
598 else
599 base = impact + exploitability;
600
601 if (base > 10.0)
602 return 10.0;
603
604 return roundup (base);
605}
static double v3_impact(const char *value)
Get impact.
Definition: cvss.c:448
static double roundup(double cvss)
Round final score as in spec.
Definition: cvss.c:421

References roundup(), and v3_impact().

Referenced by get_cvss_score_from_base_metrics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_exploitability_subscore()

static double get_exploitability_subscore ( const struct cvss cvss)
static

Calculate Exploitability Sub Score.

Parameters
[in]cvssContains the subscores associated to the metrics.
Returns
The resulting subscore.

Definition at line 261 of file cvss.c.

262{
265}
double access_vector
Definition: cvss.c:158
double authentication
Definition: cvss.c:160
double access_complexity
Definition: cvss.c:159

References cvss::access_complexity, cvss::access_vector, and cvss::authentication.

Referenced by __get_cvss_score().

Here is the caller graph for this function:

◆ get_impact_subscore()

static double get_impact_subscore ( const struct cvss cvss)
static

Calculate Impact Sub Score.

Parameters
[in]cvssContains the subscores associated to the metrics.
Returns
The resulting subscore.

Definition at line 244 of file cvss.c.

245{
246 return 10.41
247 * (1
248 - (1 - cvss->conf_impact) * (1 - cvss->integ_impact)
249 * (1 - cvss->avail_impact));
250}
double integ_impact
Definition: cvss.c:156
double avail_impact
Definition: cvss.c:157
double conf_impact
Definition: cvss.c:155

References cvss::avail_impact, cvss::conf_impact, and cvss::integ_impact.

Referenced by __get_cvss_score().

Here is the caller graph for this function:

◆ roundup()

static double roundup ( double  cvss)
static

Round final score as in spec.

Parameters
cvssCVSS score.
Returns
Rounded score.

Definition at line 421 of file cvss.c.

422{
423 int trim;
424
425 /* "Roundup returns the smallest number, specified to 1 decimal place,
426 * that is equal to or higher than its input. For example, Roundup (4.02)
427 * returns 4.1; and Roundup (4.00) returns 4.0." */
428
429 /* 3.020000000 => 3.1 */
430 /* 3.000000001 => 3.0 */
431 /* 5.299996 => 5.3 */
432 /* 5.500320 => 5.6 */
433
434 trim = round (cvss * 100000);
435 if ((trim % 10000) == 0)
436 return ((double) trim) / 100000;
437 return (floor (trim / 10000) + 1) / 10.0;
438}

Referenced by Ensure(), and get_cvss_score_from_base_metrics_v3().

Here is the caller graph for this function:

◆ set_impact_from_str()

static int set_impact_from_str ( const char *  value,
enum base_metrics  metric,
struct cvss cvss 
)
inlinestatic

Set impact score from string representation.

Parameters
[in]valueThe literal value associated to the metric.
[in]metricThe enumeration constant identifying the metric.
[out]cvssThe structure to update with the score.
Returns
0 on success, -1 on error.

Definition at line 277 of file cvss.c.

279{
280 int i;
281
282 for (i = 0; i < 3; i++)
283 {
284 const struct impact_item *impact;
285
286 impact = &impact_map[metric][i];
287
288 if (g_strcmp0 (impact->name, value) == 0)
289 {
290 switch (metric)
291 {
292 case A:
293 cvss->avail_impact = impact->nvalue;
294 break;
295
296 case I:
297 cvss->integ_impact = impact->nvalue;
298 break;
299
300 case C:
301 cvss->conf_impact = impact->nvalue;
302 break;
303
304 case Au:
305 cvss->authentication = impact->nvalue;
306 break;
307
308 case AV:
309 cvss->access_vector = impact->nvalue;
310 break;
311
312 case AC:
313 cvss->access_complexity = impact->nvalue;
314 break;
315
316 default:
317 return -1;
318 }
319 return 0;
320 }
321 }
322 return -1;
323}
static const struct impact_item impact_map[][3]
Definition: cvss.c:163
Describe a CVSS impact element.
Definition: cvss.c:145
const char * name
Definition: cvss.c:146
double nvalue
Definition: cvss.c:147

References A, AC, cvss::access_complexity, cvss::access_vector, Au, cvss::authentication, AV, cvss::avail_impact, C, cvss::conf_impact, I, impact_map, cvss::integ_impact, impact_item::name, and impact_item::nvalue.

Referenced by get_cvss_score_from_base_metrics().

Here is the caller graph for this function:

◆ toenum()

static int toenum ( const char *  str,
enum base_metrics res 
)
static

Determine base metric enumeration from a string.

Parameters
[in]strBase metric in string form, for example "A".
[out]resWhere to write the desired value.
Returns
0 on success, -1 on error.

Definition at line 211 of file cvss.c.

212{
213 int rc = 0; /* let's be optimistic */
214
215 if (g_strcmp0 (str, "A") == 0)
216 *res = A;
217 else if (g_strcmp0 (str, "I") == 0)
218 *res = I;
219 else if (g_strcmp0 (str, "C") == 0)
220 *res = C;
221 else if (g_strcmp0 (str, "Au") == 0)
222 *res = Au;
223 else if (g_strcmp0 (str, "AU") == 0)
224 *res = Au;
225 else if (g_strcmp0 (str, "AV") == 0)
226 *res = AV;
227 else if (g_strcmp0 (str, "AC") == 0)
228 *res = AC;
229 else
230 rc = -1;
231
232 return rc;
233}

References A, AC, Au, AV, C, and I.

Referenced by get_cvss_score_from_base_metrics().

Here is the caller graph for this function:

◆ v3_impact()

static double v3_impact ( const char *  value)
static

Get impact.

Parameters
valueMetric value.
Returns
Impact.

Definition at line 448 of file cvss.c.

449{
450 if (strcasecmp (value, "N") == 0)
451 return 0.0;
452 if (strcasecmp (value, "L") == 0)
453 return 0.22;
454 if (strcasecmp (value, "H") == 0)
455 return 0.56;
456 return -1.0;
457}

Referenced by get_cvss_score_from_base_metrics_v3().

Here is the caller graph for this function:

Variable Documentation

◆ impact_map

const struct impact_item impact_map[][3]
static

Definition at line 163 of file cvss.c.

Referenced by set_impact_from_str().