/src/mod_auth_openidc/test/fuzz/fuzz_authz.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 "Require claim" authorization (src/handle/authz.c): |
12 | | * oidc_authz_24_checker_claim, the authz provider Apache calls for every |
13 | | * request to a location that has one. It matches the claims the user was |
14 | | * authenticated with -- the userinfo claims, the id_token claims and the scope |
15 | | * returned by the token endpoint, which the provider issues and, for many |
16 | | * claims, the user picks -- against the Require arguments, with a hand-rolled |
17 | | * walk: "." recursing into nested objects, ":" comparing against every JSON |
18 | | * type and into arrays, "~" compiling a PCRE per request and running it over |
19 | | * strings and string arrays, and the shortest-claim-name rule that keeps a |
20 | | * provider-chosen claim name such as "iss:https" from satisfying |
21 | | * "Require claim iss:https://op.example.com". A denied request then takes the |
22 | | * OIDCUnAutzAction path: the error page, the 302 meta refresh, the bearer |
23 | | * scope error for AuthType oauth20, or the step-up authentication request. |
24 | | * |
25 | | * Input layout: the first byte selects the variation |
26 | | * bits 0-1 OIDCUnAutzAction: unset (403), 401, 302 <url>, auth |
27 | | * bit 2 AuthType oauth20 |
28 | | * bit 3 an anonymous user (r->user ""), which the pass/OPTIONS/content |
29 | | * handler short-cuts apply to |
30 | | * bit 4 OIDCUnAuthAction pass |
31 | | * bit 5 an OPTIONS request |
32 | | * bit 6 the Require arguments given as an expression (the test stubs |
33 | | * evaluate an expression to its own text) |
34 | | * bit 7 a browser navigation (Accept: text/html), so OIDCUnAutzAction |
35 | | * auth issues the authentication request instead of a 401 |
36 | | * and the rest holds up to four newline-separated sections: |
37 | | * the Require claim arguments, tokenized like the configuration line (an |
38 | | * input with more than FUZZ_AUTHZ_REQUIRE_MAX bytes of them is skipped) |
39 | | * the scope from the token endpoint (empty: none) |
40 | | * the id_token claims, a JSON object on one line (empty or not an object: none) |
41 | | * the userinfo claims, a JSON object: the rest of the input, newlines and all |
42 | | * |
43 | | * The step-up authentication request runs against the fixture's static |
44 | | * provider, offline, as in fuzz_discovery_response. |
45 | | */ |
46 | | |
47 | | #include "fuzz.h" |
48 | | /* util.h first: see the include-order note in fuzz_metadata.c */ |
49 | | /* clang-format off */ |
50 | | #include "util.h" /* test fixture */ |
51 | | #include "cfg/dir.h" /* oidc_cfg_dir_config_create, oidc_cmd_dir_unautz_action_set, ... */ |
52 | | #include "cfg/directives.h" /* OIDCUnAutzAction, OIDCUnAuthAction */ |
53 | | #include "handle/handle.h" /* oidc_authz_24_checker_claim */ |
54 | | #include "mod_auth_openidc.h" /* auth_openidc_module */ |
55 | | #include "util/request_state.h" /* oidc_request_state_set, oidc_request_state_json_set_new */ |
56 | | #include "util/util.h" /* oidc_json_decode_object */ |
57 | | /* clang-format on */ |
58 | | |
59 | | #include <apr_pools.h> |
60 | | #include <apr_strings.h> |
61 | | #include <apr_tables.h> |
62 | | #include <string.h> |
63 | | |
64 | | /* the OIDCUnAutzAction 302 target */ |
65 | 210 | #define FUZZ_UNAUTZ_URL "https://www.example.com/denied" |
66 | | |
67 | | /* |
68 | | * The largest input whose Require arguments may contain a "~" regex. The |
69 | | * module runs a Require regex with PCRE2's default match limit (10 million), |
70 | | * once per string claim, so a catastrophically backtracking pattern such as |
71 | | * "(a+)+$" costs some 0.2s per claim value: a 4KB input with a 120-element |
72 | | * array took over 20s, which ClusterFuzz would file as a timeout although the |
73 | | * pattern is the administrator's choice. At 512 bytes the worst case stays at |
74 | | * a few seconds, while the compile/exec/free handling of the regex path is |
75 | | * still fuzzed. |
76 | | */ |
77 | 1.35k | #define FUZZ_AUTHZ_REGEX_MAX_INPUT 512 |
78 | | |
79 | | /* |
80 | | * The longest Require arguments (the first section) that are run. The module |
81 | | * tries every whitespace-separated word of them against every claim, and at |
82 | | * the debug log level of the fixture it formats a line into the request pool |
83 | | * for each claim that it looks at, so the cost is words x claims: 12,000 words |
84 | | * against 12,000 claims, a 218KB input, took 19s and 3.4GB, which ClusterFuzz |
85 | | * files as an out-of-memory or a timeout although the words are the |
86 | | * administrator's configuration line, a handful in any real one. Only the |
87 | | * claims are the provider's, and in those alone the cost is linear. 256 bytes |
88 | | * hold a long Require line and at most 128 words: against the 116,000 claims |
89 | | * that fit in a 1MB input those take 2s and 400MB. |
90 | | */ |
91 | 1.36k | #define FUZZ_AUTHZ_REQUIRE_MAX 256 |
92 | | |
93 | | static int g_ready = 0; |
94 | | |
95 | | /* a cmd_parms on the given pool, the way the fixture's oidc_test_cmd_get builds one */ |
96 | 1.55k | static cmd_parms *fuzz_cmd(apr_pool_t *pool, request_rec *r, const char *directive) { |
97 | 1.55k | cmd_parms *cmd = apr_pcalloc(pool, sizeof(cmd_parms)); |
98 | 1.55k | cmd->server = r->server; |
99 | 1.55k | cmd->pool = pool; |
100 | 1.55k | cmd->temp_pool = pool; |
101 | 1.55k | cmd->directive = apr_pcalloc(pool, sizeof(ap_directive_t)); |
102 | 1.55k | cmd->directive->directive = directive; |
103 | 1.55k | return cmd; |
104 | 1.55k | } |
105 | | |
106 | | /* engine-called one-time init, pre-forkserver on AFL++: see fuzz.h */ |
107 | 34 | int LLVMFuzzerInitialize(int *argc, char ***argv) { |
108 | 34 | (void)argc; |
109 | 34 | (void)argv; |
110 | 34 | if (!g_ready) { |
111 | 34 | oidc_test_setup(); |
112 | 34 | g_ready = 1; |
113 | 34 | } |
114 | 34 | return 0; |
115 | 34 | } |
116 | | |
117 | | /* a JSON object section into the request state, where the authentication phase leaves the claims */ |
118 | 2.74k | static void fuzz_authz_claims_set(request_rec *r, const char *key, const char *s_json) { |
119 | 2.74k | oidc_json_t *json = NULL; |
120 | 2.74k | if ((s_json == NULL) || (s_json[0] == '\0')) |
121 | 1.85k | return; |
122 | 894 | if ((oidc_json_decode_object(r, s_json, &json) == TRUE) && (json != NULL)) |
123 | 759 | oidc_request_state_json_set_new(r, key, json); |
124 | 894 | } |
125 | | |
126 | 1.39k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
127 | 1.39k | if (!g_ready) |
128 | 0 | LLVMFuzzerInitialize(NULL, NULL); |
129 | | |
130 | | /* the Require arguments (the first section, after the selector byte): bound their length, and |
131 | | * the input when there is a regex in them, see above */ |
132 | 1.39k | if (size > 1) { |
133 | 1.36k | const uint8_t *args = data + 1; |
134 | 1.36k | const uint8_t *nl = memchr(args, '\n', size - 1); |
135 | 1.36k | const size_t args_len = nl ? (size_t)(nl - args) : size - 1; |
136 | 1.36k | if (args_len > FUZZ_AUTHZ_REQUIRE_MAX) |
137 | 15 | return 0; |
138 | 1.35k | if ((size > FUZZ_AUTHZ_REGEX_MAX_INPUT) && (memchr(args, '~', args_len) != NULL)) |
139 | 3 | return 0; |
140 | 1.35k | } |
141 | | |
142 | 1.37k | apr_pool_t *pool = NULL; |
143 | 1.37k | apr_pool_create(&pool, oidc_test_pool_get()); |
144 | | |
145 | | /* shallow copy of the fixture request with a per-input pool and its own header and environment |
146 | | * tables: a denied request sets response headers, the authz error variable and cookies */ |
147 | 1.37k | request_rec r = *oidc_test_request_get(); |
148 | 1.37k | r.pool = pool; |
149 | 1.37k | r.user = "alice"; |
150 | 1.37k | r.method = "GET"; |
151 | 1.37k | r.method_number = M_GET; |
152 | 1.37k | r.headers_in = apr_table_copy(pool, r.headers_in); |
153 | 1.37k | r.headers_out = apr_table_make(pool, 8); |
154 | 1.37k | r.err_headers_out = apr_table_make(pool, 8); |
155 | 1.37k | r.subprocess_env = apr_table_make(pool, 8); |
156 | | /* the ap_pass_brigade stub captures the response body into the request state of the |
157 | | * filter's request: point it at this copy so that lands in the per-input pool too */ |
158 | 1.37k | ap_filter_t filter; |
159 | 1.37k | memset(&filter, 0, sizeof(filter)); |
160 | 1.37k | filter.r = &r; |
161 | 1.37k | r.output_filters = &filter; |
162 | | |
163 | 1.37k | const unsigned int sel = (size > 0) ? data[0] : 0; |
164 | | |
165 | | /* a per-input location config, installed in place of the fixture's for this input */ |
166 | 1.37k | oidc_dir_cfg_t *saved = ap_get_module_config(r.per_dir_config, &auth_openidc_module); |
167 | 1.37k | oidc_dir_cfg_t *dir_cfg = oidc_cfg_dir_config_create(pool, NULL); |
168 | 1.37k | cmd_parms *cmd = fuzz_cmd(pool, &r, OIDCUnAutzAction); |
169 | 1.37k | switch (sel & 3) { |
170 | 164 | case 1: |
171 | 164 | oidc_cmd_dir_unautz_action_set(cmd, dir_cfg, "401", NULL); |
172 | 164 | break; |
173 | 210 | case 2: |
174 | 210 | oidc_cmd_dir_unautz_action_set(cmd, dir_cfg, "302", FUZZ_UNAUTZ_URL); |
175 | 210 | break; |
176 | 275 | case 3: |
177 | 275 | oidc_cmd_dir_unautz_action_set(cmd, dir_cfg, "auth", NULL); |
178 | 275 | break; |
179 | 723 | default: |
180 | 723 | break; |
181 | 1.37k | } |
182 | 1.37k | if (sel & 16) |
183 | 182 | oidc_cmd_dir_unauth_action_set(fuzz_cmd(pool, &r, OIDCUnAuthAction), dir_cfg, "pass", NULL); |
184 | 1.37k | ap_set_module_config(r.per_dir_config, &auth_openidc_module, dir_cfg); |
185 | | |
186 | 1.37k | oidc_test_set_auth_type((sel & 4) ? "oauth20" : NULL); |
187 | 1.37k | if (sel & 8) |
188 | 161 | r.user = ""; |
189 | 1.37k | if (sel & 32) { |
190 | 996 | r.method = "OPTIONS"; |
191 | 996 | r.method_number = M_OPTIONS; |
192 | 996 | } |
193 | 1.37k | if (sel & 128) |
194 | 170 | apr_table_set(r.headers_in, "Accept", "text/html"); |
195 | | |
196 | | /* the four sections */ |
197 | 1.37k | char *sections[4] = {NULL, NULL, NULL, NULL}; |
198 | 1.37k | sections[0] = (size > 0) ? fuzz_strndup(pool, data + 1, size - 1) : fuzz_strndup(pool, data, 0); |
199 | 3.39k | for (int i = 1; i < 4; i++) { |
200 | 3.36k | char *nl = strchr(sections[i - 1], '\n'); |
201 | 3.36k | if (nl == NULL) |
202 | 1.34k | break; |
203 | 2.02k | *nl = '\0'; |
204 | 2.02k | sections[i] = nl + 1; |
205 | 2.02k | } |
206 | 1.37k | const char *require_args = sections[0]; |
207 | 1.37k | if ((sections[1] != NULL) && (sections[1][0] != '\0')) |
208 | 277 | oidc_request_state_set(&r, OIDC_REQUEST_STATE_KEY_SCOPE, sections[1]); |
209 | 1.37k | fuzz_authz_claims_set(&r, OIDC_REQUEST_STATE_KEY_IDTOKEN, sections[2]); |
210 | 1.37k | fuzz_authz_claims_set(&r, OIDC_REQUEST_STATE_KEY_CLAIMS, sections[3]); |
211 | | |
212 | 1.37k | ap_expr_info_t *expr = NULL; |
213 | 1.37k | if (sel & 64) { |
214 | 344 | expr = apr_pcalloc(pool, sizeof(ap_expr_info_t)); |
215 | 344 | expr->filename = require_args; |
216 | 344 | } |
217 | | |
218 | 1.37k | oidc_authz_24_checker_claim(&r, require_args, expr); |
219 | | |
220 | 1.37k | oidc_test_set_auth_type(NULL); |
221 | 1.37k | ap_set_module_config(r.per_dir_config, &auth_openidc_module, saved); |
222 | | |
223 | 1.37k | apr_pool_destroy(pool); |
224 | 1.37k | return 0; |
225 | 1.37k | } |