blob: 1daaddbd54cb732a80206a01fcf1ddedcf1461ee [file] [log] [blame]
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +02001/*
2 * Modsecurity wrapper for haproxy
3 *
4 * This file contains the wrapper which sends data in ModSecurity
5 * and returns the verdict.
6 *
7 * Copyright 2016 OZON, Thierry Fournier <thierry.fournier@ozon.io>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15#include <limits.h>
16#include <stdio.h>
17#include <stdarg.h>
18
19#include <common/time.h>
20
21#include <types/global.h>
22#include <types/stream.h>
23
24#include <proto/arg.h>
25#include <proto/hdr_idx.h>
26#include <proto/hlua.h>
27#include <proto/log.h>
28#include <proto/proto_http.h>
29#include <proto/spoe.h>
30
31#include <api.h>
32
33#include "modsec_wrapper.h"
34#include "spoa.h"
35
36static char host_name[60];
37
38/* Note: The document and the code of "apr_table_make" considers
39 * that this function doesn't fails. The Apache APR code says
40 * other thing. If the system doesn't have any more memory, a
41 * a segfault occurs :(. Be carrefull with this module.
42 */
43
44struct directory_config *modsec_config = NULL;
45static server_rec *modsec_server = NULL;
46
47struct apr_bucket_haproxy {
48 apr_bucket_refcount refcount;
49 char *buffer;
50 size_t length;
51};
52
53static void haproxy_bucket_destroy(void *data)
54{
55 struct apr_bucket_haproxy *bucket = data;
56
57 if (apr_bucket_shared_destroy(bucket))
58 apr_bucket_free(bucket);
59}
60
61static apr_status_t haproxy_bucket_read(apr_bucket *bucket, const char **str,
62 apr_size_t *len, apr_read_type_e block)
63{
64 struct apr_bucket_haproxy *data = bucket->data;
65
66 if (bucket->start) {
67 *str = NULL;
68 *len = 0;
69 return APR_SUCCESS;
70 }
71
72 *str = data->buffer;
73 *len = data->length;
74 bucket->start = 1; /* Just a flag to say that the read is started */
75
76 return APR_SUCCESS;
77}
78
79static const apr_bucket_type_t apr_bucket_type_haproxy = {
80 "HAProxy", 7, APR_BUCKET_DATA,
81 haproxy_bucket_destroy,
82 haproxy_bucket_read,
83 apr_bucket_setaside_noop,
84 apr_bucket_shared_split,
85 apr_bucket_shared_copy
86};
87
88static char *chunk_strdup(struct request_rec *req, const char *str, size_t len)
89{
90 char *out;
91
92 out = apr_pcalloc(req->pool, len + 1);
93 if (!out)
94 return NULL;
95 memcpy(out, str, len);
96 out[len] = '\0';
97 return out;
98}
99
100static char *printf_dup(struct request_rec *req, char *fmt, ...)
101{
102 char *out;
103 va_list ap;
104 int len;
105
106 va_start(ap, fmt);
107 len = vsnprintf(NULL, 0, fmt, ap);
Dragan Dosen2f1cacb2017-09-18 09:20:43 +0200108 va_end(ap);
109
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200110 if (len == -1)
111 return NULL;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200112
113 out = apr_pcalloc(req->pool, len + 1);
114 if (!out)
115 return NULL;
116
117 va_start(ap, fmt);
118 len = vsnprintf(out, len + 1, fmt, ap);
Dragan Dosen2f1cacb2017-09-18 09:20:43 +0200119 va_end(ap);
120
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200121 if (len == -1)
122 return NULL;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200123
124 return out;
125}
126
127/* This function send logs. For now, it do nothing. */
128static void modsec_log(void *obj, int level, char *str)
129{
130 LOG(&null_worker, "%s", str);
131}
132
Joseph Herlant9fe83fa2018-11-09 18:25:59 -0800133/* This function load the ModSecurity file. It returns -1 if the
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200134 * initialisation fails.
135 */
136int modsecurity_load(const char *file)
137{
138 const char *msg;
139 char cwd[128];
140
141 /* Initialises modsecurity. */
142
143 modsec_server = modsecInit();
144 if (modsec_server == NULL) {
145 LOG(&null_worker, "ModSecurity initilisation failed.\n");
146 return -1;
147 }
148
149 modsecSetLogHook(NULL, modsec_log);
150
151 gethostname(host_name, 60);
152 modsec_server->server_hostname = host_name;
153
154 modsecStartConfig();
155
156 modsec_config = modsecGetDefaultConfig();
157 if (modsec_config == NULL) {
158 LOG(&null_worker, "ModSecurity default configuration initilisation failed.\n");
159 return -1;
160 }
161
162 msg = modsecProcessConfig(modsec_config, file, getcwd(cwd, 128));
163 if (msg != NULL) {
164 LOG(&null_worker, "ModSecurity load configuration failed.\n");
165 return -1;
166 }
167
168 modsecFinalizeConfig();
169
170 modsecInitProcess();
171
172 return 1;
173}
174
175struct modsec_hdr {
176 const char *name;
177 uint64_t name_len;
178 const char *value;
179 uint64_t value_len;
180};
181
182int modsecurity_process(struct worker *worker, struct modsecurity_parameters *params)
183{
184 struct conn_rec *cr;
185 struct request_rec *req;
186 struct apr_bucket_brigade *brigade;
187 struct apr_bucket *link_bucket;
188 struct apr_bucket_haproxy *data_bucket;
189 struct apr_bucket *last_bucket;
190 int i;
191 long clength;
192 char *err;
193 int fail;
194 const char *lang;
195 char *name, *value;
196 // int body_partial;
197 struct timeval now;
198 int ret;
199 char *buf;
200 char *end;
201 const char *uniqueid;
202 uint64_t uniqueid_len;
203 const char *meth;
204 uint64_t meth_len;
205 const char *path;
206 uint64_t path_len;
207 const char *qs;
208 uint64_t qs_len;
209 const char *vers;
210 uint64_t vers_len;
211 const char *body;
212 uint64_t body_len;
213 uint64_t body_exposed_len;
214 uint64_t hdr_nb;
215 struct modsec_hdr hdrs[255];
216 struct modsec_hdr hdr;
217 int status;
218 int return_code = -1;
219
220 /* Decode uniqueid. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200221 uniqueid = params->uniqueid.data.u.str.area;
222 uniqueid_len = params->uniqueid.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200223
224 /* Decode method. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200225 meth = params->method.data.u.str.area;
226 meth_len = params->method.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200227
228 /* Decode path. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200229 path = params->path.data.u.str.area;
230 path_len = params->path.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200231
232 /* Decode query string. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200233 qs = params->query.data.u.str.area;
234 qs_len = params->query.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200235
236 /* Decode version. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200237 vers = params->vers.data.u.str.area;
238 vers_len = params->vers.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200239
240 /* Decode header binary block. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200241 buf = params->hdrs_bin.data.u.str.area;
242 end = buf + params->hdrs_bin.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200243
244 /* Decode each header. */
245 hdr_nb = 0;
246 while (1) {
247
248 /* Initialise the storage struct. It is useless
249 * because the process fail if the struct is not
250 * fully filled. This init is just does in order
251 * to prevent bug after some improvements.
252 */
253 memset(&hdr, 0, sizeof(hdr));
254
255 /* Decode header name. */
256 ret = decode_varint(&buf, end, &hdr.name_len);
257 if (ret == -1)
258 return -1;
259 hdr.name = buf;
260 buf += hdr.name_len;
261 if (buf > end)
262 return -1;
263
264 /* Decode header value. */
265 ret = decode_varint(&buf, end, &hdr.value_len);
266 if (ret == -1)
267 return -1;
268 hdr.value = buf;
269 buf += hdr.value_len;
270 if (buf > end)
271 return -1;
272
273 /* Detect the end of the headers. */
274 if (hdr.name_len == 0 && hdr.value_len == 0)
275 break;
276
277 /* Store the header. */
278 if (hdr_nb < 255) {
279 memcpy(&hdrs[hdr_nb], &hdr, sizeof(hdr));
280 hdr_nb++;
281 }
282 }
283
284 /* Decode body length. Note that the following control
285 * is just set for avoifing a gcc warning.
286 */
287 body_exposed_len = (uint64_t)params->body_length.data.u.sint;
288 if (body_exposed_len < 0)
289 return -1;
290
291 /* Decode body. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200292 body = params->body.data.u.str.area;
293 body_len = params->body.data.u.str.data;
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200294
295 fail = 1;
296
297 /* Init processing */
298
299 cr = modsecNewConnection();
300 req = modsecNewRequest(cr, modsec_config);
301
302 /* Load request. */
303
304 req->proxyreq = PROXYREQ_NONE;
305 req->header_only = 0; /* May modified later */
306
307 /* Copy header list. */
308
309 for (i = 0; i < hdr_nb; i++) {
310 name = chunk_strdup(req, hdrs[i].name, hdrs[i].name_len);
311 if (!name) {
312 errno = ENOMEM;
313 goto fail;
314 }
315 value = chunk_strdup(req, hdrs[i].value, hdrs[i].value_len);
316 if (!value) {
317 errno = ENOMEM;
318 goto fail;
319 }
320 apr_table_setn(req->headers_in, name, value);
321 }
322
323 /* Process special headers. */
324 req->range = apr_table_get(req->headers_in, "Range");
325 req->content_type = apr_table_get(req->headers_in, "Content-Type");
326 req->content_encoding = apr_table_get(req->headers_in, "Content-Encoding");
327 req->hostname = apr_table_get(req->headers_in, "Host");
328 req->parsed_uri.hostname = chunk_strdup(req, req->hostname, strlen(req->hostname));
329
330 lang = apr_table_get(req->headers_in, "Content-Languages");
331 if (lang != NULL) {
332 req->content_languages = apr_array_make(req->pool, 1, sizeof(const char *));
333 *(const char **)apr_array_push(req->content_languages) = lang;
334 }
335
336 lang = apr_table_get(req->headers_in, "Content-Length");
337 if (lang) {
338 errno = 0;
339 clength = strtol(lang, &err, 10);
340 if (*err != '\0' || errno != 0 || clength < 0 || clength > INT_MAX) {
341 errno = ERANGE;
342 goto fail;
343 }
344 req->clength = clength;
345 }
346
347 /* Copy the first line of the request. */
348 req->the_request = printf_dup(req, "%.*s %.*s%s%.*s %.*s",
349 meth_len, meth,
350 path_len, path,
351 qs_len > 0 ? "?" : "",
352 qs_len, qs,
353 vers_len, vers);
354 if (!req->the_request) {
355 errno = ENOMEM;
356 goto fail;
357 }
358
359 /* Copy the method. */
360 req->method = chunk_strdup(req, meth, meth_len);
361 if (!req->method) {
362 errno = ENOMEM;
363 goto fail;
364 }
365
366 /* Set the method number. */
367 if (meth_len < 3) {
368 errno = EINVAL;
369 goto fail;
370 }
371
372 /* Detect the method */
373 switch (meth_len) {
374 case 3:
375 if (strncmp(req->method, "GET", 3) == 0)
376 req->method_number = M_GET;
377 else if (strncmp(req->method, "PUT", 3) == 0)
378 req->method_number = M_PUT;
379 else {
380 errno = EINVAL;
381 goto fail;
382 }
383 break;
384 case 4:
385 if (strncmp(req->method, "POST", 4) == 0)
386 req->method_number = M_POST;
387 else if (strncmp(req->method, "HEAD", 4) == 0) {
388 req->method_number = M_GET;
389 req->header_only = 1;
390 }
391 else if (strncmp(req->method, "COPY", 4) == 0)
392 req->method_number = M_COPY;
393 else if (strncmp(req->method, "MOVE", 4) == 0)
394 req->method_number = M_MOVE;
395 else if (strncmp(req->method, "LOCK", 4) == 0)
396 req->method_number = M_LOCK;
397 else {
398 errno = EINVAL;
399 goto fail;
400 }
401 break;
402 case 5:
403 if (strncmp(req->method, "TRACE", 5) == 0)
404 req->method_number = M_TRACE;
405 else if (strncmp(req->method, "PATCH", 5) == 0)
406 req->method_number = M_PATCH;
407 else if (strncmp(req->method, "MKCOL", 5) == 0)
408 req->method_number = M_MKCOL;
409 else if (strncmp(req->method, "MERGE", 5) == 0)
410 req->method_number = M_MERGE;
411 else if (strncmp(req->method, "LABEL", 5) == 0)
412 req->method_number = M_LABEL;
413 else {
414 errno = EINVAL;
415 goto fail;
416 }
417 break;
418 case 6:
419 if (strncmp(req->method, "DELETE", 6) == 0)
420 req->method_number = M_DELETE;
421 else if (strncmp(req->method, "REPORT", 6) == 0)
422 req->method_number = M_REPORT;
423 else if (strncmp(req->method, "UPDATE", 6) == 0)
424 req->method_number = M_UPDATE;
425 else if (strncmp(req->method, "UNLOCK", 6) == 0)
426 req->method_number = M_UNLOCK;
427 else {
428 errno = EINVAL;
429 goto fail;
430 }
431 break;
432 case 7:
433 if (strncmp(req->method, "CHECKIN", 7) == 0)
434 req->method_number = M_CHECKIN;
435 else if (strncmp(req->method, "INVALID", 7) == 0)
436 req->method_number = M_INVALID;
437 else if (strncmp(req->method, "CONNECT", 7) == 0)
438 req->method_number = M_CONNECT;
439 else if (strncmp(req->method, "OPTIONS", 7) == 0)
440 req->method_number = M_OPTIONS;
441 else {
442 errno = EINVAL;
443 goto fail;
444 }
445 break;
446 case 8:
447 if (strncmp(req->method, "PROPFIND", 8) == 0)
448 req->method_number = M_PROPFIND;
449 else if (strncmp(req->method, "CHECKOUT", 8) == 0)
450 req->method_number = M_CHECKOUT;
451 else {
452 errno = EINVAL;
453 goto fail;
454 }
455 break;
456 case 9:
457 if (strncmp(req->method, "PROPPATCH", 9) == 0)
458 req->method_number = M_PROPPATCH;
459 else {
460 errno = EINVAL;
461 goto fail;
462 }
463 break;
464 case 10:
465 if (strncmp(req->method, "MKACTIVITY", 10) == 0)
466 req->method_number = M_MKACTIVITY;
467 else if (strncmp(req->method, "UNCHECKOUT", 10) == 0)
468 req->method_number = M_UNCHECKOUT;
469 else {
470 errno = EINVAL;
471 goto fail;
472 }
473 break;
474 case 11:
475 if (strncmp(req->method, "MKWORKSPACE", 11) == 0)
476 req->method_number = M_MKWORKSPACE;
477 else {
478 errno = EINVAL;
479 goto fail;
480 }
481 break;
482 case 15:
483 if (strncmp(req->method, "VERSION_CONTROL", 15) == 0)
484 req->method_number = M_VERSION_CONTROL;
485 else {
486 errno = EINVAL;
487 goto fail;
488 }
489 break;
490 case 16:
491 if (strncmp(req->method, "BASELINE_CONTROL", 16) == 0)
492 req->method_number = M_BASELINE_CONTROL;
493 else {
494 errno = EINVAL;
495 goto fail;
496 }
497 break;
498 default:
499 errno = EINVAL;
500 goto fail;
501 }
502
503 /* Copy the protocol. */
504 req->protocol = chunk_strdup(req, vers, vers_len);
505 if (!req->protocol) {
506 errno = ENOMEM;
507 goto fail;
508 }
509
510 /* Compute the protocol number. */
511 if (vers_len >= 8)
512 req->proto_num = 1000 + !!(vers[7] == '1');
513
514 /* The request time. */
515 gettimeofday(&now, NULL);
516 req->request_time = apr_time_make(now.tv_sec, now.tv_usec / 1000);
517
518 /* No status line. */
519 req->status_line = NULL;
520 req->status = 0;
521
522 /* Copy path. */
523 req->parsed_uri.path = chunk_strdup(req, path, path_len);
524 if (!req->parsed_uri.path) {
525 errno = ENOMEM;
526 goto fail;
527 }
528
529 /* Copy args (query string). */
530 req->args = chunk_strdup(req, qs, qs_len);
531 if (!req->args) {
532 errno = ENOMEM;
533 goto fail;
534 }
535
536 /* Set parsed_uri */
537
538 req->parsed_uri.scheme = "http";
539
540 if (req->hostname && req->parsed_uri.scheme && req->parsed_uri.path) {
541 i = snprintf(NULL, 0, "%s://%s%s",
542 req->parsed_uri.scheme, req->hostname, req->parsed_uri.path);
543 req->uri = apr_pcalloc(req->pool, i + 1);
544 if (!req->uri) {
545 errno = ENOMEM;
546 goto fail;
547 }
548 i = snprintf(req->uri, i + 1, "%s://%s%s",
549 req->parsed_uri.scheme, req->hostname, req->parsed_uri.path);
550 }
551
552 req->filename = req->parsed_uri.path;
553
554 /* Set unique id */
555
556 apr_table_setn(req->subprocess_env, "UNIQUE_ID", chunk_strdup(req, uniqueid, uniqueid_len));
557
558 /*
559 *
560 * Load body.
561 *
562 */
563
564 /* Create an empty bucket brigade */
565 brigade = apr_brigade_create(req->pool, req->connection->bucket_alloc);
566 if (!brigade) {
567 errno = ENOMEM;
568 goto fail;
569 }
570
Joseph Herlant9fe83fa2018-11-09 18:25:59 -0800571 /* Stores HTTP body available data in a bucket */
Thierry FOURNIERa5ec06d2017-04-10 23:47:23 +0200572 data_bucket = apr_bucket_alloc(sizeof(*data_bucket), req->connection->bucket_alloc);
573 if (!data_bucket) {
574 errno = ENOMEM;
575 goto fail;
576 }
577 data_bucket->buffer = (char *)body;
578 data_bucket->length = body_len;
579
580 /* Create linked bucket */
581 link_bucket = apr_bucket_alloc(sizeof(*link_bucket), req->connection->bucket_alloc);
582 if (!link_bucket) {
583 errno = ENOMEM;
584 goto fail;
585 }
586 APR_BUCKET_INIT(link_bucket); /* link */
587 link_bucket->free = apr_bucket_free;
588 link_bucket->list = req->connection->bucket_alloc;
589 link_bucket = apr_bucket_shared_make(link_bucket, data_bucket, 0, body_len);
590 link_bucket->type = &apr_bucket_type_haproxy;
591
592 /* Insert the bucket at the end of the brigade. */
593 APR_BRIGADE_INSERT_TAIL(brigade, link_bucket);
594
595 /* Insert the last bucket. */
596 last_bucket = apr_bucket_eos_create(req->connection->bucket_alloc);
597 APR_BRIGADE_INSERT_TAIL(brigade, last_bucket);
598
599 /* Declares the bucket brigade in modsecurity */
600 modsecSetBodyBrigade(req, brigade);
601
602 /*
603 *
604 * Process analysis.
605 *
606 */
607
608 /* Process request headers analysis. */
609 status = modsecProcessRequestHeaders(req);
610 if (status != DECLINED && status != DONE)
611 return_code = status;
612
613 /* Process request body analysis. */
614 status = modsecProcessRequestBody(req);
615 if (status != DECLINED && status != DONE)
616 return_code = status;
617
618 /* End processing. */
619
620 fail = 0;
621 if (return_code == -1)
622 return_code = 0;
623
624fail:
625
626 modsecFinishRequest(req);
627 modsecFinishConnection(cr);
628
629 if (fail) {
630
631 /* errno == ERANGE / ENOMEM / EINVAL */
632 switch (errno) {
633 case ERANGE: LOG(worker, "Invalid range");
634 case ENOMEM: LOG(worker, "Out of memory error");
635 case EINVAL: LOG(worker, "Invalid value");
636 default: LOG(worker, "Unknown error");
637 }
638 }
639
640 return return_code;
641}