Bug Summary

File:lwan-mod-rewrite.c
Warning:line 569, column 5
Attempt to free released memory

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name lwan-mod-rewrite.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -fno-plt -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib/clang/12.0.1 -include /home/buildbot/lwan-worker/clang-analyze/build/lwan-build-config.h -D _FILE_OFFSET_BITS=64 -D _TIME_BITS=64 -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib/missing -I /usr/include/luajit-2.0 -I /usr/include/valgrind -I /home/buildbot/lwan-worker/clang-analyze/build/src/lib -I /home/buildbot/lwan-worker/clang-analyze/build -internal-isystem /usr/local/include -internal-isystem /usr/lib/clang/12.0.1/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wno-unused-parameter -Wno-free-nonheap-object -std=gnu99 -fdebug-compilation-dir /home/buildbot/lwan-worker/clang-analyze/build/src/lib -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -analyzer-output=html -faddrsig -o /home/buildbot/lwan-worker/clang-analyze/CLANG/2021-09-18-054345-183695-1 -x c /home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c
1/*
2 * lwan - simple web server
3 * Copyright (c) 2015 Leandro A. F. Pereira <leandro@hardinfo.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
18 * USA.
19 */
20
21#define _GNU_SOURCE
22#include <ctype.h>
23#include <limits.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27
28#include "lwan-private.h"
29
30#include "patterns.h"
31#include "lwan-array.h"
32#include "lwan-mod-rewrite.h"
33#include "lwan-strbuf.h"
34
35#ifdef HAVE_LUA
36#include <lauxlib.h>
37#include <lua.h>
38#include <lualib.h>
39
40#include "lwan-lua.h"
41#endif
42
43enum pattern_flag {
44 PATTERN_HANDLE_REWRITE = 1 << 0,
45 PATTERN_HANDLE_REDIRECT = 1 << 1,
46 PATTERN_HANDLE_MASK = PATTERN_HANDLE_REWRITE | PATTERN_HANDLE_REDIRECT,
47
48 PATTERN_EXPAND_LWAN = 1 << 2,
49 PATTERN_EXPAND_LUA = 1 << 3,
50 PATTERN_EXPAND_MASK = PATTERN_EXPAND_LWAN | PATTERN_EXPAND_LUA,
51
52 PATTERN_COND_COOKIE = 1 << 4,
53 PATTERN_COND_ENV_VAR = 1 << 5,
54 PATTERN_COND_STAT = 1 << 6,
55 PATTERN_COND_QUERY_VAR = 1 << 7,
56 PATTERN_COND_POST_VAR = 1 << 8,
57 PATTERN_COND_HEADER = 1 << 9,
58 PATTERN_COND_LUA = 1 << 10,
59 PATTERN_COND_METHOD = 1 << 11,
60 PATTERN_COND_MASK = PATTERN_COND_COOKIE | PATTERN_COND_ENV_VAR |
61 PATTERN_COND_STAT | PATTERN_COND_QUERY_VAR |
62 PATTERN_COND_POST_VAR | PATTERN_COND_HEADER |
63 PATTERN_COND_LUA | PATTERN_COND_METHOD,
64
65 PATTERN_COND_STAT__HAS_IS_FILE = 1 << 12,
66 PATTERN_COND_STAT__HAS_IS_DIR = 1 << 13,
67 PATTERN_COND_STAT__IS_FILE = 1 << 14,
68 PATTERN_COND_STAT__IS_DIR = 1 << 15,
69
70 PATTERN_COND_STAT__FILE_CHECK =
71 PATTERN_COND_STAT__HAS_IS_FILE | PATTERN_COND_STAT__IS_FILE,
72 PATTERN_COND_STAT__DIR_CHECK =
73 PATTERN_COND_STAT__HAS_IS_DIR | PATTERN_COND_STAT__IS_DIR,
74};
75
76struct pattern {
77 char *pattern;
78 char *expand_pattern;
79 struct {
80 struct lwan_key_value cookie;
81 struct lwan_key_value env_var;
82 struct lwan_key_value query_var;
83 struct lwan_key_value post_var;
84 struct lwan_key_value header;
85 struct {
86 char *path;
87 } stat;
88 struct {
89 char *script;
90 } lua;
91 enum lwan_request_flags method;
92 /* FIXME: Use pahole to find alignment holes? */
93 } condition;
94 enum pattern_flag flags;
95};
96
97DEFINE_ARRAY_TYPE(pattern_array, struct pattern)struct pattern_array { struct lwan_array base; }; __attribute__
((unused)) static inline struct pattern *pattern_array_append
( struct pattern_array *array) { return (struct pattern *)lwan_array_append_heap
(&array->base, sizeof(struct pattern)); } __attribute__
((unused)) static inline struct pattern_array *coro_pattern_array_new
(struct coro *coro) { return (struct pattern_array *)coro_lwan_array_new
(coro, 0); } __attribute__((unused)) static inline struct pattern
*pattern_array_get_array(struct pattern_array *array) { return
(struct pattern *)array->base.base; } __attribute__((unused
)) __attribute__((nonnull(1))) static inline void pattern_array_init
( struct pattern_array *array) { array->base = (struct lwan_array
){.base = ((void*)0), .elements = 0}; } __attribute__((unused
)) static inline int pattern_array_reset( struct pattern_array
*array) { return lwan_array_reset(&array->base, ((void
*)0)); } __attribute__((unused)) static inline struct pattern
*pattern_array_append0(struct pattern_array *array) { struct
pattern *element = pattern_array_append(array); if (element)
memset(element, 0, sizeof(*element)); return element; } __attribute__
((unused)) static inline void pattern_array_sort( struct pattern_array
*array, int (*cmp)(const void *a, const void *b)) { lwan_array_sort
(&array->base, sizeof(struct pattern), cmp); } __attribute__
((unused)) static inline size_t pattern_array_get_elem_index(
const struct pattern_array *array, struct pattern *elem) { (
(void) sizeof ((elem >= (struct pattern *)array->base.base
) ? 1 : 0), __extension__ ({ if (elem >= (struct pattern *
)array->base.base) ; else __assert_fail ("elem >= (struct pattern *)array->base.base"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 97, __extension__ __PRETTY_FUNCTION__); })); ((void) sizeof
((elem < (struct pattern *)array->base.base + array->
base.elements) ? 1 : 0), __extension__ ({ if (elem < (struct
pattern *)array->base.base + array->base.elements) ; else
__assert_fail ("elem < (struct pattern *)array->base.base + array->base.elements"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 97, __extension__ __PRETTY_FUNCTION__); })); return (size_t
)(elem - (struct pattern *)array->base.base); } __attribute__
((unused)) static inline struct pattern *pattern_array_get_elem
(const struct pattern_array *array, size_t index) { ((void) sizeof
((index <= array->base.elements) ? 1 : 0), __extension__
({ if (index <= array->base.elements) ; else __assert_fail
("index <= array->base.elements", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 97, __extension__ __PRETTY_FUNCTION__); })); return &((
struct pattern *)array->base.base)[index]; } __attribute__
((unused)) static inline size_t pattern_array_len( const struct
pattern_array *array) { return array->base.elements; }
98
99struct private_data {
100 struct pattern_array patterns;
101};
102
103static enum lwan_http_status module_redirect_to(struct lwan_request *request,
104 const char *url)
105{
106 const struct lwan_key_value headers[] = {
107 {"Location", coro_strdup(request->conn->coro, url)},
108 {},
109 };
110
111 request->response.headers =
112 coro_memdup(request->conn->coro, headers, sizeof(headers));
113
114 if (LIKELY(headers[0].value && request->response.headers)__builtin_expect((!!(headers[0].value && request->
response.headers)), (1))
)
115 return HTTP_MOVED_PERMANENTLY;
116
117 return HTTP_INTERNAL_ERROR;
118}
119
120static enum lwan_http_status module_rewrite_as(struct lwan_request *request,
121 const char *url)
122{
123 request->url.value = coro_strdup(request->conn->coro, url);
124
125 if (UNLIKELY(!request->url.value)__builtin_expect(((!request->url.value)), (0)))
126 return HTTP_INTERNAL_ERROR;
127
128 request->url.len = strlen(request->url.value);
129 request->original_url = request->url;
130 request->flags |= RESPONSE_URL_REWRITTEN;
131
132 return HTTP_OK;
133}
134
135#define MAX_INT_DIGITS(3 * sizeof(int)) (3 * sizeof(int))
136
137static __attribute__((noinline)) int parse_int_len(const char *s, size_t len,
138 int default_value)
139{
140 if (UNLIKELY(len > MAX_INT_DIGITS)__builtin_expect(((len > (3 * sizeof(int)))), (0)))
141 return default_value;
142
143 return parse_int(strndupa(s, len)(__extension__ ({ const char *__old = (s); size_t __len = strnlen
(__old, (len)); char *__new = (char *) __builtin_alloca (__len
+ 1); __new[__len] = '\0'; (char *) memcpy (__new, __old, __len
); }))
, default_value);
144}
145
146static const char *expand_string(const char *expand_pattern,
147 const char *orig,
148 char buffer[static PATH_MAX4096],
149 const struct str_find *sf,
150 int captures)
151{
152 struct lwan_strbuf strbuf;
153 const char *ptr;
154
155 ptr = strchr(expand_pattern, '%');
156 if (!ptr)
157 return expand_pattern;
158
159 if (!lwan_strbuf_init_with_fixed_buffer(&strbuf, buffer, PATH_MAX4096))
160 return NULL((void*)0);
161
162 do {
163 size_t index_len = strspn(ptr + 1, "0123456789");
164
165 if (ptr > expand_pattern) {
166 const size_t len = (size_t)(ptr - expand_pattern);
167
168 if (UNLIKELY(!lwan_strbuf_append_str(&strbuf, expand_pattern, len))__builtin_expect(((!lwan_strbuf_append_str(&strbuf, expand_pattern
, len))), (0))
)
169 return NULL((void*)0);
170
171 expand_pattern += len;
172 }
173
174 if (LIKELY(index_len > 0)__builtin_expect((!!(index_len > 0)), (1))) {
175 const int index = parse_int_len(ptr + 1, index_len, -1);
176
177 if (UNLIKELY(index < 0 || index > captures)__builtin_expect(((index < 0 || index > captures)), (0)
)
)
178 return NULL((void*)0);
179
180 if (UNLIKELY(!lwan_strbuf_append_str(__builtin_expect(((!lwan_strbuf_append_str( &strbuf, orig
+ sf[index].sm_so, (size_t)(sf[index].sm_eo - sf[index].sm_so
)))), (0))
181 &strbuf, orig + sf[index].sm_so,__builtin_expect(((!lwan_strbuf_append_str( &strbuf, orig
+ sf[index].sm_so, (size_t)(sf[index].sm_eo - sf[index].sm_so
)))), (0))
182 (size_t)(sf[index].sm_eo - sf[index].sm_so)))__builtin_expect(((!lwan_strbuf_append_str( &strbuf, orig
+ sf[index].sm_so, (size_t)(sf[index].sm_eo - sf[index].sm_so
)))), (0))
)
183 return NULL((void*)0);
184
185 expand_pattern += index_len;
186 } else if (UNLIKELY(!lwan_strbuf_append_char(&strbuf, '%'))__builtin_expect(((!lwan_strbuf_append_char(&strbuf, '%')
)), (0))
) {
187 return NULL((void*)0);
188 }
189
190 expand_pattern++;
191 } while ((ptr = strchr(expand_pattern, '%')));
192
193 const size_t remaining_len = strlen(expand_pattern);
194 if (remaining_len &&
195 !lwan_strbuf_append_str(&strbuf, expand_pattern, remaining_len))
196 return NULL((void*)0);
197
198 if (UNLIKELY(!lwan_strbuf_get_length(&strbuf))__builtin_expect(((!lwan_strbuf_get_length(&strbuf))), (0
))
)
199 return NULL((void*)0);
200
201 return lwan_strbuf_get_buffer(&strbuf);
202}
203
204static ALWAYS_INLINEinline __attribute__((always_inline)) const char *expand(const struct pattern *pattern,
205 const char *orig,
206 char buffer[static PATH_MAX4096],
207 const struct str_find *sf,
208 int captures)
209{
210 return expand_string(pattern->expand_pattern, orig, buffer, sf, captures);
211}
212
213#ifdef HAVE_LUA
214static void
215lua_close_defer(void *data)
216{
217 lua_close((lua_State *)data);
218}
219
220static const char *expand_lua(struct lwan_request *request,
221 struct pattern *pattern, const char *orig,
222 char buffer[static PATH_MAX4096],
223 const struct str_find *sf, int captures)
224{
225 const char *output;
226 size_t output_len;
227 int i;
228 lua_State *L;
229
230 L = lwan_lua_create_state(NULL((void*)0), pattern->expand_pattern);
231 if (UNLIKELY(!L)__builtin_expect(((!L)), (0)))
232 return NULL((void*)0);
233 coro_defer(request->conn->coro, lua_close_defer, L);
234
235 lua_getglobal(L, "handle_rewrite")lua_getfield(L, (-10002), ("handle_rewrite"));
236 if (!lua_isfunction(L, -1)(lua_type(L, (-1)) == 6)) {
237 lwan_status_error(lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 239, __FUNCTION__, "Could not obtain reference to `handle_rewrite()` function: %s"
, lwan_lua_state_last_error(L))
238 "Could not obtain reference to `handle_rewrite()` function: %s",lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 239, __FUNCTION__, "Could not obtain reference to `handle_rewrite()` function: %s"
, lwan_lua_state_last_error(L))
239 lwan_lua_state_last_error(L))lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 239, __FUNCTION__, "Could not obtain reference to `handle_rewrite()` function: %s"
, lwan_lua_state_last_error(L))
;
240 return NULL((void*)0);
241 }
242
243 lwan_lua_state_push_request(L, request);
244
245 lua_createtable(L, captures, 0);
246 for (i = 0; i < captures; i++) {
247 lua_pushinteger(L, i);
248 lua_pushlstring(L, orig + sf[i].sm_so,
249 (size_t)(sf[i].sm_eo - sf[i].sm_so));
250 lua_settable(L, -3);
251 }
252
253 if (lua_pcall(L, 2, 1, 0) != 0) {
254 lwan_status_error("Could not execute `handle_rewrite()` function: %s",lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 255, __FUNCTION__, "Could not execute `handle_rewrite()` function: %s"
, lwan_lua_state_last_error(L))
255 lwan_lua_state_last_error(L))lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 255, __FUNCTION__, "Could not execute `handle_rewrite()` function: %s"
, lwan_lua_state_last_error(L))
;
256 return NULL((void*)0);
257 }
258
259 output = lua_tolstring(L, -1, &output_len);
260 if (output_len >= PATH_MAX4096) {
261 lwan_status_error("Rewritten URL exceeds %d bytes (got %zu bytes)",lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 262, __FUNCTION__, "Rewritten URL exceeds %d bytes (got %zu bytes)"
, 4096, output_len)
262 PATH_MAX, output_len)lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 262, __FUNCTION__, "Rewritten URL exceeds %d bytes (got %zu bytes)"
, 4096, output_len)
;
263 return NULL((void*)0);
264 }
265
266 return memcpy(buffer, output, output_len + 1);
267}
268#endif
269
270static bool_Bool condition_matches(struct lwan_request *request,
271 const struct pattern *p,
272 const struct str_find *sf,
273 int captures)
274{
275 if (LIKELY(!(p->flags & PATTERN_COND_MASK))__builtin_expect((!!(!(p->flags & PATTERN_COND_MASK)))
, (1))
)
276 return true1;
277
278 const char *url = request->url.value;
279 char expanded_buf[PATH_MAX4096];
280
281 if (p->flags & PATTERN_COND_METHOD) {
282 if (lwan_request_get_method(request) != p->condition.method)
283 return false0;
284 }
285
286 if (p->flags & PATTERN_COND_COOKIE) {
287 assert(p->condition.cookie.key)((void) sizeof ((p->condition.cookie.key) ? 1 : 0), __extension__
({ if (p->condition.cookie.key) ; else __assert_fail ("p->condition.cookie.key"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 287, __extension__ __PRETTY_FUNCTION__); }))
;
288 assert(p->condition.cookie.value)((void) sizeof ((p->condition.cookie.value) ? 1 : 0), __extension__
({ if (p->condition.cookie.value) ; else __assert_fail ("p->condition.cookie.value"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 288, __extension__ __PRETTY_FUNCTION__); }))
;
289
290 const char *cookie =
291 lwan_request_get_cookie(request, p->condition.cookie.key);
292 if (!cookie)
293 return false0;
294
295 const char *val = expand_string(p->condition.cookie.value, url,
296 expanded_buf, sf, captures);
297 if (!val || !streq(val, cookie))
298 return false0;
299 }
300
301 if (p->flags & PATTERN_COND_ENV_VAR) {
302 assert(p->condition.env_var.key)((void) sizeof ((p->condition.env_var.key) ? 1 : 0), __extension__
({ if (p->condition.env_var.key) ; else __assert_fail ("p->condition.env_var.key"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 302, __extension__ __PRETTY_FUNCTION__); }))
;
303 assert(p->condition.env_var.value)((void) sizeof ((p->condition.env_var.value) ? 1 : 0), __extension__
({ if (p->condition.env_var.value) ; else __assert_fail (
"p->condition.env_var.value", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 303, __extension__ __PRETTY_FUNCTION__); }))
;
304
305 const char *env_var = secure_getenv(p->condition.env_var.key);
306 if (!env_var)
307 return false0;
308
309 const char *val = expand_string(p->condition.env_var.value, url,
310 expanded_buf, sf, captures);
311 if (!val || !streq(val, env_var))
312 return false0;
313 }
314
315 if (p->flags & PATTERN_COND_QUERY_VAR) {
316 assert(p->condition.query_var.key)((void) sizeof ((p->condition.query_var.key) ? 1 : 0), __extension__
({ if (p->condition.query_var.key) ; else __assert_fail (
"p->condition.query_var.key", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 316, __extension__ __PRETTY_FUNCTION__); }))
;
317 assert(p->condition.query_var.value)((void) sizeof ((p->condition.query_var.value) ? 1 : 0), __extension__
({ if (p->condition.query_var.value) ; else __assert_fail
("p->condition.query_var.value", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 317, __extension__ __PRETTY_FUNCTION__); }))
;
318
319 const char *query =
320 lwan_request_get_query_param(request, p->condition.query_var.key);
321
322 if (!query)
323 return false0;
324
325 const char *val = expand_string(p->condition.query_var.value, url,
326 expanded_buf, sf, captures);
327 if (!val || !streq(val, query))
328 return false0;
329 }
330
331 if (p->flags & PATTERN_COND_POST_VAR) {
332 assert(p->condition.post_var.key)((void) sizeof ((p->condition.post_var.key) ? 1 : 0), __extension__
({ if (p->condition.post_var.key) ; else __assert_fail ("p->condition.post_var.key"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 332, __extension__ __PRETTY_FUNCTION__); }))
;
333 assert(p->condition.post_var.value)((void) sizeof ((p->condition.post_var.value) ? 1 : 0), __extension__
({ if (p->condition.post_var.value) ; else __assert_fail (
"p->condition.post_var.value", "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 333, __extension__ __PRETTY_FUNCTION__); }))
;
334
335 const char *post =
336 lwan_request_get_post_param(request, p->condition.post_var.key);
337 if (!post)
338 return false0;
339
340 const char *val = expand_string(p->condition.post_var.value, url,
341 expanded_buf, sf, captures);
342 if (!val || !streq(val, post))
343 return false0;
344 }
345
346 if (p->flags & PATTERN_COND_STAT) {
347 assert(p->condition.stat.path)((void) sizeof ((p->condition.stat.path) ? 1 : 0), __extension__
({ if (p->condition.stat.path) ; else __assert_fail ("p->condition.stat.path"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 347, __extension__ __PRETTY_FUNCTION__); }))
;
348
349 struct stat st;
350
351 /* FIXME: Expanding path from a user-controlled URL and use the
352 * resulting path to call stat(2) on could lead to some information
353 * disclosure vulnerability. Would require the server to be configured
354 * in a certain way, though.
355 */
356 const char *path = expand_string(p->condition.stat.path, url,
357 expanded_buf, sf, captures);
358 if (!path || stat(path, &st) < 0)
359 return false0;
360
361 if ((p->flags & PATTERN_COND_STAT__FILE_CHECK) ==
362 PATTERN_COND_STAT__FILE_CHECK &&
363 !S_ISREG(st.st_mode)((((st.st_mode)) & 0170000) == (0100000)))
364 return false0;
365 if ((p->flags & PATTERN_COND_STAT__DIR_CHECK) ==
366 PATTERN_COND_STAT__DIR_CHECK &&
367 !S_ISDIR(st.st_mode)((((st.st_mode)) & 0170000) == (0040000)))
368 return false0;
369 }
370
371#ifdef HAVE_LUA
372 if (p->flags & PATTERN_COND_LUA) {
373 assert(p->condition.lua.script)((void) sizeof ((p->condition.lua.script) ? 1 : 0), __extension__
({ if (p->condition.lua.script) ; else __assert_fail ("p->condition.lua.script"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 373, __extension__ __PRETTY_FUNCTION__); }))
;
374
375 lua_State *L = lwan_lua_create_state(NULL((void*)0), p->condition.lua.script);
376 if (!L)
377 return false0;
378 coro_defer(request->conn->coro, lua_close_defer, L);
379
380 lua_getglobal(L, "matches")lua_getfield(L, (-10002), ("matches"));
381 if (!lua_isfunction(L, -1)(lua_type(L, (-1)) == 6)) {
382 lwan_status_error(lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 384, __FUNCTION__, "Could not obtain reference to `matches()` function: %s"
, lwan_lua_state_last_error(L))
383 "Could not obtain reference to `matches()` function: %s",lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 384, __FUNCTION__, "Could not obtain reference to `matches()` function: %s"
, lwan_lua_state_last_error(L))
384 lwan_lua_state_last_error(L))lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 384, __FUNCTION__, "Could not obtain reference to `matches()` function: %s"
, lwan_lua_state_last_error(L))
;
385 return false0;
386 }
387
388 lwan_lua_state_push_request(L, request);
389
390 if (lua_pcall(L, 1, 1, 0) != 0) {
391 lwan_status_error("Could not execute `matches()` function: %s",lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 392, __FUNCTION__, "Could not execute `matches()` function: %s"
, lwan_lua_state_last_error(L))
392 lwan_lua_state_last_error(L))lwan_status_error_debug("/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 392, __FUNCTION__, "Could not execute `matches()` function: %s"
, lwan_lua_state_last_error(L))
;
393 return false0;
394 }
395
396 if (!lua_toboolean(L, -1))
397 return false0;
398 }
399#else
400 assert(!(p->flags & PATTERN_COND_LUA))((void) sizeof ((!(p->flags & PATTERN_COND_LUA)) ? 1 :
0), __extension__ ({ if (!(p->flags & PATTERN_COND_LUA
)) ; else __assert_fail ("!(p->flags & PATTERN_COND_LUA)"
, "/home/buildbot/lwan-worker/clang-analyze/build/src/lib/lwan-mod-rewrite.c"
, 400, __extension__ __PRETTY_FUNCTION__); }))
;
401#endif
402
403 return true1;
404}
405
406static enum lwan_http_status
407rewrite_handle_request(struct lwan_request *request,
408 struct lwan_response *response __attribute__((unused)),
409 void *instance)
410{
411 struct private_data *pd = instance;
412 const char *url = request->url.value;
413 char final_url[PATH_MAX4096];
414 struct pattern *p;
415
416 LWAN_ARRAY_FOREACH(&pd->patterns, p)for (p = (&pd->patterns)->base.base; p < ((typeof
(p))(&pd->patterns)->base.base + (&pd->patterns
)->base.elements); p++)
{
417 struct str_find sf[MAXCAPTURES32];
418 const char *expanded = NULL((void*)0);
419 const char *errmsg;
420 int captures;
421
422 captures = str_find(url, p->pattern, sf, MAXCAPTURES32, &errmsg);
423 if (captures <= 0)
424 continue;
425
426 if (!condition_matches(request, p, sf, captures))
427 continue;
428
429 switch (p->flags & PATTERN_EXPAND_MASK) {
430#ifdef HAVE_LUA
431 case PATTERN_EXPAND_LUA:
432 expanded = expand_lua(request, p, url, final_url, sf, captures);
433 break;
434#endif
435 case PATTERN_EXPAND_LWAN:
436 expanded = expand(p, url, final_url, sf, captures);
437 break;
438 }
439
440 if (LIKELY(expanded)__builtin_expect((!!(expanded)), (1))) {
441 switch (p->flags & PATTERN_HANDLE_MASK) {
442 case PATTERN_HANDLE_REDIRECT:
443 return module_redirect_to(request, expanded);
444 case PATTERN_HANDLE_REWRITE:
445 return module_rewrite_as(request, expanded);
446 }
447 }
448
449 return HTTP_INTERNAL_ERROR;
450 }
451
452 return HTTP_NOT_FOUND;
453}
454
455static void *rewrite_create(const char *prefix __attribute__((unused)),
456 void *instance __attribute__((unused)))
457{
458 struct private_data *pd = malloc(sizeof(*pd));
459
460 if (!pd)
461 return NULL((void*)0);
462
463 pattern_array_init(&pd->patterns);
464
465 return pd;
466}
467
468static void rewrite_destroy(void *instance)
469{
470 struct private_data *pd = instance;
471 struct pattern *iter;
472
473 LWAN_ARRAY_FOREACH(&pd->patterns, iter)for (iter = (&pd->patterns)->base.base; iter < (
(typeof(iter))(&pd->patterns)->base.base + (&pd
->patterns)->base.elements); iter++)
{
474 free(iter->pattern);
475 free(iter->expand_pattern);
476 if (iter->flags & PATTERN_COND_COOKIE) {
477 free(iter->condition.cookie.key);
478 free(iter->condition.cookie.value);
479 }
480 if (iter->flags & PATTERN_COND_ENV_VAR) {
481 free(iter->condition.env_var.key);
482 free(iter->condition.env_var.value);
483 }
484 if (iter->flags & PATTERN_COND_QUERY_VAR) {
485 free(iter->condition.query_var.key);
486 free(iter->condition.query_var.value);
487 }
488 if (iter->flags & PATTERN_COND_POST_VAR) {
489 free(iter->condition.post_var.key);
490 free(iter->condition.post_var.value);
491 }
492 if (iter->flags & PATTERN_COND_HEADER) {
493 free(iter->condition.header.key);
494 free(iter->condition.header.value);
495 }
496 if (iter->flags & PATTERN_COND_STAT) {
497 free(iter->condition.stat.path);
498 }
499#ifdef HAVE_LUA
500 if (iter->flags & PATTERN_COND_LUA) {
501 free(iter->condition.lua.script);
502 }
503#endif
504 }
505
506 pattern_array_reset(&pd->patterns);
507 free(pd);
508}
509
510static void *rewrite_create_from_hash(const char *prefix,
511 const struct hash *hash
512 __attribute__((unused)))
513{
514 return rewrite_create(prefix, NULL((void*)0));
515}
516
517static void parse_condition_key_value(struct pattern *pattern,
518 struct lwan_key_value *key_value,
519 enum pattern_flag condition_type,
520 struct config *config,
521 const struct config_line *line)
522{
523 char *key = NULL((void*)0), *value = NULL((void*)0);
524
525 while ((line = config_read_line(config))) {
14
Loop condition is true. Entering loop body
526 switch (line->type) {
15
Control jumps to 'case CONFIG_LINE_TYPE_LINE:' at line 541
527 case CONFIG_LINE_TYPE_SECTION:
528 config_error(config, "Unexpected section: %s", line->key);
529 goto out;
530
531 case CONFIG_LINE_TYPE_SECTION_END:
532 if (!key || !value) {
533 config_error(config, "Key/value has not been specified");
534 goto out;
535 }
536
537 *key_value = (struct lwan_key_value){key, value};
538 pattern->flags |= condition_type & PATTERN_COND_MASK;
539 return;
540
541 case CONFIG_LINE_TYPE_LINE:
542 if (key
15.1
'key' is null
|| value
15.2
'value' is null
) {
16
Taking false branch
543 config_error(config,
544 "Can only condition on a single key/value pair. "
545 "Currently has: %s=%s",
546 key, value);
547 goto out;
548 }
549
550 key = strdup(line->key);
17
Memory is allocated
551 if (!key) {
18
Assuming 'key' is non-null
19
Taking false branch
552 config_error(config,
553 "Could not copy key while parsing condition");
554 goto out;
555 }
556
557 value = strdup(line->value);
558 if (!value) {
20
Assuming 'value' is null
21
Taking true branch
559 free(key);
22
Memory is released
560 config_error(config,
561 "Could not copy value while parsing condition");
562 goto out;
23
Control jumps to line 569
563 }
564 break;
565 }
566 }
567
568out:
569 free(key);
24
Attempt to free released memory
570 free(value);
571}
572
573static void parse_condition_stat(struct pattern *pattern,
574 struct config *config,
575 const struct config_line *line)
576{
577 char *path = NULL((void*)0), *is_dir = NULL((void*)0), *is_file = NULL((void*)0);
578
579 while ((line = config_read_line(config))) {
580 switch (line->type) {
581 case CONFIG_LINE_TYPE_SECTION:
582 config_error(config, "Unexpected section: %s", line->key);
583 goto out;
584
585 case CONFIG_LINE_TYPE_SECTION_END:
586 if (!path) {
587 config_error(config, "Path not specified");
588 goto out;
589 }
590
591 pattern->condition.stat.path = path;
592 if (is_dir) {
593 pattern->flags |= PATTERN_COND_STAT__HAS_IS_DIR;
594 if (parse_bool(is_dir, false0))
595 pattern->flags |= PATTERN_COND_STAT__IS_DIR;
596 }
597 if (is_file) {
598 pattern->flags |= PATTERN_COND_STAT__HAS_IS_FILE;
599 if (parse_bool(is_file, false0))
600 pattern->flags |= PATTERN_COND_STAT__IS_FILE;
601 }
602 pattern->flags |= PATTERN_COND_STAT;
603 return;
604
605 case CONFIG_LINE_TYPE_LINE:
606 if (streq(line->key, "path")) {
607 if (path) {
608 config_error(config, "Path `%s` already specified", path);
609 goto out;
610 }
611 path = strdup(line->value);
612 if (!path) {
613 config_error(config, "Could not copy path");
614 goto out;
615 }
616 } else if (streq(line->key, "is_dir")) {
617 is_dir = line->value;
618 } else if (streq(line->key, "is_file")) {
619 is_file = line->value;
620 } else {
621 config_error(config, "Unexpected key: %s", line->key);
622 goto out;
623 }
624
625 break;
626 }
627 }
628
629out:
630 free(path);
631}
632
633#ifdef HAVE_LUA
634static void parse_condition_lua(struct pattern *pattern,
635 struct config *config,
636 const struct config_line *line)
637{
638 char *script = NULL((void*)0);
639
640 while ((line = config_read_line(config))) {
641 switch (line->type) {
642 case CONFIG_LINE_TYPE_SECTION:
643 config_error(config, "Unexpected section: %s", line->key);
644 goto out;
645
646 case CONFIG_LINE_TYPE_SECTION_END:
647 if (!script) {
648 config_error(config, "Script not specified");
649 goto out;
650 }
651
652 pattern->condition.lua.script = script;
653 pattern->flags |= PATTERN_COND_LUA;
654 return;
655
656 case CONFIG_LINE_TYPE_LINE:
657 if (streq(line->key, "script")) {
658 if (script) {
659 config_error(config, "Script already specified");
660 goto out;
661 }
662 script = strdup(line->value);
663 if (!script) {
664 config_error(config, "Could not copy script");
665 goto out;
666 }
667 } else {
668 config_error(config, "Unexpected key: %s", line->key);
669 goto out;
670 }
671
672 break;
673 }
674 }
675
676out:
677 free(script);
678}
679#endif
680
681static bool_Bool get_method_from_string(struct pattern *pattern, const char *string)
682{
683#define GENERATE_CMP(upper, lower, mask, constant) \
684 if (!strcasecmp(string, #upper)) { \
685 pattern->condition.method = (mask); \
686 return true1; \
687 }
688
689 FOR_EACH_REQUEST_METHOD(GENERATE_CMP)GENERATE_CMP(GET, get, (1 << 0), ((uint32_t)(('G') | ('E'
) << 8 | ('T') << 16 | (' ') << 24))) GENERATE_CMP
(POST, post, (1 << 3 | 1 << 1 | 1 << 0), ((
uint32_t)(('P') | ('O') << 8 | ('S') << 16 | ('T'
) << 24))) GENERATE_CMP(HEAD, head, (1 << 1), ((uint32_t
)(('H') | ('E') << 8 | ('A') << 16 | ('D') <<
24))) GENERATE_CMP(OPTIONS, options, (1 << 2), ((uint32_t
)(('O') | ('P') << 8 | ('T') << 16 | ('I') <<
24))) GENERATE_CMP(DELETE, delete, (1 << 1 | 1 <<
2), ((uint32_t)(('D') | ('E') << 8 | ('L') << 16
| ('E') << 24))) GENERATE_CMP(PUT, put, (1 << 3 |
1 << 2 | 1 << 0), ((uint32_t)(('P') | ('U') <<
8 | ('T') << 16 | (' ') << 24)))
690
691#undef GENERATE_CMP
692
693 return false0;
694}
695
696static void parse_condition(struct pattern *pattern,
697 struct config *config,
698 const struct config_line *line)
699{
700 if (streq(line->value, "cookie")) {
12
Taking true branch
701 return parse_condition_key_value(pattern, &pattern->condition.cookie,
13
Calling 'parse_condition_key_value'
702 PATTERN_COND_COOKIE, config, line);
703 }
704 if (streq(line->value, "query")) {
705 return parse_condition_key_value(pattern, &pattern->condition.query_var,
706 PATTERN_COND_QUERY_VAR, config, line);
707 }
708 if (streq(line->value, "post")) {
709 return parse_condition_key_value(pattern, &pattern->condition.post_var,
710 PATTERN_COND_POST_VAR, config, line);
711 }
712 if (streq(line->value, "environment")) {
713 return parse_condition_key_value(pattern, &pattern->condition.env_var,
714 PATTERN_COND_ENV_VAR, config, line);
715 }
716 if (streq(line->value, "header")) {
717 return parse_condition_key_value(pattern, &pattern->condition.header,
718 PATTERN_COND_HEADER, config, line);
719 }
720 if (streq(line->value, "method")) {
721 struct lwan_key_value method = {};
722
723 parse_condition_key_value(pattern, &method,
724 PATTERN_COND_METHOD, config, line);
725 if (config_last_error(config))
726 return;
727
728 if (!streq(method.key, "name")) {
729 config_error(config, "Method condition requires `name`");
730 } else if (!get_method_from_string(pattern, method.value)) {
731 config_error(config, "Unknown HTTP method: %s", method.value);
732 }
733
734 free(method.key);
735 free(method.value);
736
737 return;
738 }
739 if (streq(line->value, "stat")) {
740 return parse_condition_stat(pattern, config, line);
741 }
742#ifdef HAVE_LUA
743 if (streq(line->value, "lua")) {
744 return parse_condition_lua(pattern, config, line);
745 }
746#endif
747
748 config_error(config, "Condition `%s' not supported", line->value);
749}
750
751static bool_Bool rewrite_parse_conf_pattern(struct private_data *pd,
752 struct config *config,
753 const struct config_line *line)
754{
755 struct pattern *pattern;
756 char *redirect_to = NULL((void*)0), *rewrite_as = NULL((void*)0);
757 bool_Bool expand_with_lua = false0;
758
759 pattern = pattern_array_append0(&pd->patterns);
760 if (!pattern
4.1
'pattern' is non-null
)
5
Taking false branch
761 goto out_no_free;
762
763 pattern->pattern = strdup(line->value);
764 if (!pattern->pattern)
6
Assuming field 'pattern' is non-null
7
Taking false branch
765 goto out;
766
767 while ((line = config_read_line(config))) {
8
Loop condition is true. Entering loop body
768 switch (line->type) {
9
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 789
769 case CONFIG_LINE_TYPE_LINE:
770 if (streq(line->key, "redirect_to")) {
771 free(redirect_to);
772
773 redirect_to = strdup(line->value);
774 if (!redirect_to)
775 goto out;
776 } else if (streq(line->key, "rewrite_as")) {
777 free(rewrite_as);
778
779 rewrite_as = strdup(line->value);
780 if (!rewrite_as)
781 goto out;
782 } else if (streq(line->key, "expand_with_lua")) {
783 expand_with_lua = parse_bool(line->value, false0);
784 } else {
785 config_error(config, "Unexpected key: %s", line->key);
786 goto out;
787 }
788 break;
789 case CONFIG_LINE_TYPE_SECTION:
790 if (streq(line->key, "condition")) {
10
Taking true branch
791 parse_condition(pattern, config, line);
11
Calling 'parse_condition'
792 } else {
793 config_error(config, "Unexpected section: %s", line->key);
794 }
795 break;
796 case CONFIG_LINE_TYPE_SECTION_END:
797 if (redirect_to && rewrite_as) {
798 config_error(
799 config,
800 "`redirect to` and `rewrite as` are mutually exclusive");
801 goto out;
802 }
803 if (redirect_to) {
804 pattern->expand_pattern = redirect_to;
805 pattern->flags |= PATTERN_HANDLE_REDIRECT;
806 } else if (rewrite_as) {
807 pattern->expand_pattern = rewrite_as;
808 pattern->flags |= PATTERN_HANDLE_REWRITE;
809 } else {
810 config_error(
811 config,
812 "either `redirect to` or `rewrite as` are required");
813 goto out;
814 }
815 if (expand_with_lua) {
816#ifdef HAVE_LUA
817 pattern->flags |= PATTERN_EXPAND_LUA;
818#else
819 config_error(config, "Lwan has been built without Lua. "
820 "`expand_with_lua` is not available");
821 goto out;
822#endif
823 } else {
824 pattern->flags |= PATTERN_EXPAND_LWAN;
825 }
826
827 return true1;
828 }
829 }
830
831out:
832 free(pattern->pattern);
833 free(redirect_to);
834 free(rewrite_as);
835out_no_free:
836 config_error(config, "Could not copy pattern");
837 return false0;
838}
839
840static bool_Bool rewrite_parse_conf(void *instance, struct config *config)
841{
842 struct private_data *pd = instance;
843 const struct config_line *line;
844
845 while ((line = config_read_line(config))) {
1
Loop condition is true. Entering loop body
846 switch (line->type) {
2
Control jumps to 'case CONFIG_LINE_TYPE_SECTION:' at line 850
847 case CONFIG_LINE_TYPE_LINE:
848 config_error(config, "Unknown option: %s", line->key);
849 break;
850 case CONFIG_LINE_TYPE_SECTION:
851 if (streq(line->key, "pattern")) {
3
Taking true branch
852 rewrite_parse_conf_pattern(pd, config, line);
4
Calling 'rewrite_parse_conf_pattern'
853 } else {
854 config_error(config, "Unknown section: %s", line->key);
855 }
856 break;
857 case CONFIG_LINE_TYPE_SECTION_END:
858 break;
859 }
860 }
861
862 return !config_last_error(config);
863}
864
865static const struct lwan_module module = {
866 .create = rewrite_create,
867 .create_from_hash = rewrite_create_from_hash,
868 .parse_conf = rewrite_parse_conf,
869 .destroy = rewrite_destroy,
870 .handle_request = rewrite_handle_request,
871 .flags = HANDLER_CAN_REWRITE_URL
872};
873
874LWAN_REGISTER_MODULE(rewrite, &module)const struct lwan_module_info __attribute__((used, section("lwan_module"
))) lwan_module_info_rewrite = {.name = "rewrite", .module = &
module}
;