Coverage Report

Created: 2026-12-01 06:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mod_auth_openidc/test/fuzz/fuzz_metrics.c
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  Licensed under the Apache License,
4
 * Version 2.0 (the "License"); you may not use this file except in
5
 * compliance with the License.  You may obtain a copy of the License at
6
 *
7
 *   http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Copyright (C) 2017-2026 ZmartZone Holding BV - hans.zandbelt@openidc.com
10
 *
11
 * Fuzz target for the metrics subsystem (src/metrics.c). What the module
12
 * records about a request is partly chosen by others: the claim.id_token and
13
 * claim.userinfo counters (OIDCMetricsData claim.id_token.<name>) are keyed by
14
 * claim name and labelled with the claim value, i.e. data the provider issues
15
 * and, for a display name or an e-mail address, data the user picks. Each input
16
 * records counter and timing samples, flushes them into the shared-memory JSON
17
 * document the way the per-child flush thread does -- merging into whatever an
18
 * earlier flush, possibly another process's, left there -- and then renders
19
 * that document through every OIDCMetricsPublish format: prometheus (label
20
 * escaping, name normalization, both histogram families), json, internal and
21
 * status (the server_name/counter/name/value selectors and the readiness
22
 * probe), plus the fuzzer's own publish request with its reset parameter.
23
 *
24
 * Input layout: the first line is the publish request's query string (format,
25
 * reset, server_name, counter, name, value); every following line is one
26
 * operation, selected by its first character:
27
 *   c<type>\t<name>\t<value>  oidc_metrics_counter_inc(type, name, value);
28
 *                             "c<type>\t<value>" records without a name, and
29
 *                             a bare "c<type>" without a value either
30
 *   t<type>\t<elapsed>        oidc_metrics_timing_add(type, elapsed), in
31
 *                             microseconds; negative and huge values included
32
 *   f                         flush the samples recorded so far
33
 *   j<json>                   replace the shared-memory document with <json>
34
 *                             as-is: what other processes' flushes left behind,
35
 *                             or a corrupted segment, which the key lookups
36
 *                             promise not to index out of bounds on
37
 * <type> is taken modulo the number of counter or timing types, since the
38
 * module only ever passes an enum value. Other lines are ignored. Whatever is
39
 * left unflushed at the end is flushed before the rendering.
40
 *
41
 * Why metrics.c is #included rather than linked: the flush
42
 * (oidc_metrics_store) and the shared-memory accessors are static, and in the
43
 * module only the background thread that oidc_metrics_child_init starts calls
44
 * them -- asynchronously, on a timer, which a fuzz target cannot use. Compiling
45
 * the file into this translation unit gives the target synchronous access to
46
 * them without exporting test hooks from the module. Every external symbol of
47
 * metrics.o is then already defined here, so the linker never pulls metrics.o
48
 * out of libauth_openidc.a: there is one copy of the code and of its globals.
49
 * Only oidc_metrics_post_config runs, never child_init, so there is no thread.
50
 */
51
52
#include "fuzz.h"
53
/* util.h first: see the include-order note in fuzz_metadata.c */
54
/* clang-format off */
55
#include "util.h"              /* test fixture */
56
#include "../../src/metrics.c" /* the code under test, statics included: see above */
57
/* clang-format on */
58
59
#include <apr_pools.h>
60
#include <apr_strings.h>
61
#include <apr_tables.h>
62
#include <stdio.h>
63
#include <stdlib.h>
64
#include <string.h>
65
66
/*
67
 * The shared-memory segment size (OIDCMetricsCacheJsonMax). Small, so that a
68
 * few KB of input can outgrow it and reach the "json value too large" flush
69
 * error; with the 1 MB default that path would be out of the fuzzer's reach.
70
 */
71
2
#define FUZZ_METRICS_JSON_MAX "4096"
72
73
static int g_ready = 0;
74
75
/* engine-called one-time init, pre-forkserver on AFL++: see fuzz.h */
76
2
int LLVMFuzzerInitialize(int *argc, char ***argv) {
77
2
  (void)argc;
78
2
  (void)argv;
79
2
  if (!g_ready) {
80
2
    oidc_test_setup();
81
2
    request_rec *r = oidc_test_request_get();
82
    /* read once, by the first _oidc_metrics_shm_size() call in post_config */
83
2
    oidc_test_setenv(oidc_test_pool_get(), OIDC_METRICS_CACHE_JSON_MAX_ENV_VAR, FUZZ_METRICS_JSON_MAX);
84
    /* the segment, the local tables and both mutexes; no flush thread */
85
2
    if (oidc_metrics_post_config(r->server->process->pconf, r->server) != TRUE) {
86
0
      fprintf(stderr, "fuzz_metrics: oidc_metrics_post_config failed\n");
87
0
      abort();
88
0
    }
89
2
    g_ready = 1;
90
2
  }
91
2
  return 0;
92
2
}
93
94
/* a decimal type number at *p, reduced to one of n enum values; *p is advanced past it */
95
2.02M
static unsigned int fuzz_metrics_type(char **p, unsigned int n) {
96
2.02M
  char *end = NULL;
97
2.02M
  unsigned long v = strtoul(*p, &end, 10);
98
2.02M
  *p = end;
99
2.02M
  return (unsigned int)(v % n);
100
2.02M
}
101
102
/* what the flush thread does on each tick (oidc_metrics_thread_run), minus the timer */
103
12.4k
static void fuzz_metrics_flush(server_rec *s) {
104
12.4k
  if (oidc_metrics_store(s) == TRUE)
105
12.4k
    oidc_metrics_local_reset(s);
106
12.4k
}
107
108
2.12M
static void fuzz_metrics_op(request_rec *r, char *line) {
109
2.12M
  char *p = line + 1;
110
2.12M
  switch (line[0]) {
111
1.21M
  case 'c': {
112
1.21M
    unsigned int type = fuzz_metrics_type(&p, OM_NUMBER_OF_COUNTERS);
113
1.21M
    char *name = NULL;
114
1.21M
    char *value = NULL;
115
1.21M
    char *tab = strchr(p, '\t');
116
1.21M
    if (tab != NULL) {
117
15.2k
      value = tab + 1;
118
15.2k
      tab = strchr(value, '\t');
119
15.2k
      if (tab != NULL) {
120
1.92k
        *tab = '\0';
121
1.92k
        name = value;
122
1.92k
        value = tab + 1;
123
1.92k
      }
124
15.2k
    }
125
1.21M
    oidc_metrics_counter_inc(r, (oidc_metrics_counter_type_t)type, name, value);
126
1.21M
    break;
127
0
  }
128
809k
  case 't': {
129
809k
    unsigned int type = fuzz_metrics_type(&p, OM_NUMBER_OF_TIMINGS);
130
809k
    apr_time_t elapsed = (*p == '\t') ? (apr_time_t)strtoll(p + 1, NULL, 10) : 0;
131
809k
    oidc_metrics_timing_add(r, (oidc_metrics_timing_type_t)type, elapsed);
132
809k
    break;
133
0
  }
134
9.54k
  case 'f':
135
9.54k
    fuzz_metrics_flush(r->server);
136
9.54k
    break;
137
5.07k
  case 'j':
138
5.07k
    _oidc_metrics_storage_set(r->server, p);
139
5.07k
    break;
140
83.7k
  default:
141
83.7k
    break;
142
2.12M
  }
143
2.12M
}
144
145
/* the fixed publish requests every input is rendered through, before the fuzzer's own */
146
static const char *const fuzz_metrics_formats[] = {"format=prometheus", "format=json", "format=internal",
147
               "format=status"};
148
149
2.89k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
150
2.89k
  if (!g_ready)
151
0
    LLVMFuzzerInitialize(NULL, NULL);
152
153
2.89k
  apr_pool_t *pool = NULL;
154
2.89k
  apr_pool_create(&pool, oidc_test_pool_get());
155
156
  /* shallow copy of the fixture request with a per-input pool, and of its server and process
157
   * records: the subsystem allocates its flush scratch pool and the numbers in its overflow
158
   * warning from the process pool, and oidc_slog formats every server-level log line into
159
   * pconf; neither is cleared between requests in the module, so here both are the per-input
160
   * pool too, or each overflowing input would grow the fixture's pools for good */
161
2.89k
  request_rec r = *oidc_test_request_get();
162
2.89k
  process_rec process = *r.server->process;
163
2.89k
  process.pool = pool;
164
2.89k
  process.pconf = pool;
165
2.89k
  server_rec server = *r.server;
166
2.89k
  server.process = &process;
167
2.89k
  r.server = &server;
168
2.89k
  r.pool = pool;
169
2.89k
  r.headers_in = apr_table_copy(pool, r.headers_in);
170
2.89k
  r.headers_out = apr_table_make(pool, 8);
171
2.89k
  r.err_headers_out = apr_table_make(pool, 8);
172
  /* the ap_pass_brigade stub captures the response body into the request state of the
173
   * filter's request: point it at this copy so that lands in the per-input pool too */
174
2.89k
  ap_filter_t filter;
175
2.89k
  memset(&filter, 0, sizeof(filter));
176
2.89k
  filter.r = &r;
177
2.89k
  r.output_filters = &filter;
178
179
  /* nothing carries over between inputs: an empty segment, a zeroed self-health header and
180
   * empty local tables */
181
2.89k
  memset(_oidc_metrics_shm_hdr(), 0, sizeof(oidc_metrics_shm_hdr_t));
182
2.89k
  _oidc_metrics_storage_set(&server, NULL);
183
2.89k
  oidc_metrics_local_reset(&server);
184
185
2.89k
  char *query = fuzz_strndup(pool, data, size);
186
2.89k
  char *rest = strchr(query, '\n');
187
2.89k
  if (rest != NULL)
188
2.47k
    *rest++ = '\0';
189
2.12M
  while (rest != NULL) {
190
2.12M
    char *line = rest;
191
2.12M
    rest = strchr(line, '\n');
192
2.12M
    if (rest != NULL)
193
2.12M
      *rest++ = '\0';
194
2.12M
    fuzz_metrics_op(&r, line);
195
2.12M
  }
196
2.89k
  fuzz_metrics_flush(&server);
197
198
14.4k
  for (size_t i = 0; i < sizeof(fuzz_metrics_formats) / sizeof(fuzz_metrics_formats[0]); i++) {
199
11.5k
    r.args = (char *)fuzz_metrics_formats[i];
200
11.5k
    oidc_metrics_handle_request(&r);
201
11.5k
  }
202
2.89k
  r.args = query;
203
2.89k
  oidc_metrics_handle_request(&r);
204
205
2.89k
  apr_pool_destroy(pool);
206
2.89k
  return 0;
207
2.89k
}