-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
detect-engine-address.c
5196 lines (4398 loc) · 159 KB
/
detect-engine-address.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2007-2022 Open Information Security Foundation
*
* You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
/**
* \file
*
* \author Victor Julien <victor@inliniac.net>
*
* Address part of the detection engine.
*/
#include "suricata-common.h"
#include "decode.h"
#include "detect.h"
#include "flow-var.h"
#include "util-cidr.h"
#include "util-unittest.h"
#include "util-rule-vars.h"
#include "conf.h"
#include "conf-yaml-loader.h"
#include "detect-engine-siggroup.h"
#include "detect-engine-address.h"
#include "detect-engine-address-ipv4.h"
#include "detect-engine-address-ipv6.h"
#include "detect-engine-port.h"
#include "util-debug.h"
#include "util-byte.h"
#include "util-print.h"
#include "util-var.h"
/* prototypes */
#ifdef DEBUG
static void DetectAddressPrint(DetectAddress *);
#else
#define DetectAddressPrint(...)
#endif
static int DetectAddressCutNot(DetectAddress *, DetectAddress **);
static int DetectAddressCut(DetectEngineCtx *, DetectAddress *, DetectAddress *,
DetectAddress **);
static int DetectAddressParse2(const DetectEngineCtx *de_ctx, DetectAddressHead *gh,
DetectAddressHead *ghn, const char *s, int negate, ResolvedVariablesList *var_list,
int recur);
int DetectAddressMergeNot(DetectAddressHead *gh, DetectAddressHead *ghn);
/**
* \brief Creates and returns a new instance of a DetectAddress.
*
* \retval ag Pointer to the newly created DetectAddress on success;
* NULL on failure.
*/
DetectAddress *DetectAddressInit(void)
{
DetectAddress *ag = SCCalloc(1, sizeof(DetectAddress));
if (unlikely(ag == NULL))
return NULL;
return ag;
}
/**
* \brief Frees a DetectAddress instance.
*
* \param ag Pointer to the DetectAddress instance to be freed.
*/
void DetectAddressFree(DetectAddress *ag)
{
if (ag == NULL)
return;
SCFree(ag);
return;
}
/**
* \internal
* \brief Returns a new instance of DetectAddressHead.
*
* \retval gh Pointer to the new instance of DetectAddressHead.
*/
static DetectAddressHead *DetectAddressHeadInit(void)
{
DetectAddressHead *gh = SCCalloc(1, sizeof(DetectAddressHead));
if (unlikely(gh == NULL))
return NULL;
return gh;
}
/**
* \internal
* \brief Frees a DetectAddressHead instance.
*
* \param gh Pointer to the DetectAddressHead instance to be freed.
*/
static void DetectAddressHeadFree(DetectAddressHead *gh)
{
if (gh != NULL) {
DetectAddressHeadCleanup(gh);
SCFree(gh);
}
}
/**
* \brief copy a DetectAddress
*
* \param orig Pointer to the instance of DetectAddress that contains the
* address data to be copied to the new instance.
*
* \retval ag Pointer to the new instance of DetectAddress that contains the
* copied address.
*/
DetectAddress *DetectAddressCopy(DetectAddress *orig)
{
DetectAddress *ag = DetectAddressInit();
if (ag == NULL)
return NULL;
ag->flags = orig->flags;
COPY_ADDRESS(&orig->ip, &ag->ip);
COPY_ADDRESS(&orig->ip2, &ag->ip2);
return ag;
}
/**
* \internal
* \brief Frees a list of DetectAddress instances.
*
* \param head Pointer to a list of DetectAddress instances to be freed.
*/
static void DetectAddressCleanupList(DetectAddress *head)
{
for (DetectAddress *cur = head; cur != NULL; ) {
DetectAddress *next = cur->next;
cur->next = NULL;
DetectAddressFree(cur);
cur = next;
}
}
/**
* \internal
* \brief Helper function for DetectAddressInsert. Sets one of the
* DetectAddressHead head pointers, to the DetectAddress argument
* based on its address family.
*
* \param gh Pointer to the DetectAddressHead.
* \param newhead Pointer to the DetectAddress.
*
* \retval 0 On success.
* \retval -1 On failure.
*/
static int SetHeadPtr(DetectAddressHead *gh, DetectAddress *newhead)
{
if (newhead->ip.family == AF_INET) {
gh->ipv4_head = newhead;
} else if (newhead->ip.family == AF_INET6) {
gh->ipv6_head = newhead;
} else {
SCLogDebug("newhead->family %u not supported", newhead->ip.family);
return -1;
}
return 0;
}
/**
* \internal
* \brief Returns the DetectAddress head from the DetectAddressHeads,
* based on the address family of the incoming DetectAddress arg.
*
* \param gh Pointer to the DetectAddressHead.
* \param new Pointer to the DetectAddress.
*
* \retval head Pointer to the DetectAddress(the head from
* DetectAddressHead).
*/
static DetectAddress *GetHeadPtr(DetectAddressHead *gh, DetectAddress *new)
{
DetectAddress *head = NULL;
if (new->ip.family == AF_INET)
head = gh->ipv4_head;
else if (new->ip.family == AF_INET6)
head = gh->ipv6_head;
return head;
}
/**
* \internal
* \brief insert DetectAddress into a DetectAddressHead
*
* \param de_ctx Pointer to the detection engine context.
* \param gh Pointer to the DetectAddressHead list to which it has to
* be inserted.
* \param new Pointer to the DetectAddress, that has to be inserted.
*
* \retval 1 On successfully inserting it.
* \retval -1 On error.
* \retval 0 Not inserted, memory of new is freed.
*/
static int DetectAddressInsert(DetectEngineCtx *de_ctx, DetectAddressHead *gh,
DetectAddress *new)
{
DetectAddress *head = NULL;
DetectAddress *cur = NULL;
DetectAddress *c = NULL;
int r = 0;
if (new == NULL)
return 0;
/* get our head ptr based on the address we want to insert */
head = GetHeadPtr(gh, new);
/* see if it already exists or overlaps with existing ag's */
if (head != NULL) {
cur = NULL;
for (cur = head; cur != NULL; cur = cur->next) {
r = DetectAddressCmp(new, cur);
BUG_ON(r == ADDRESS_ER);
/* if so, handle that */
if (r == ADDRESS_EQ) {
/* exact overlap/match */
if (cur != new) {
DetectAddressFree(new);
return 0;
}
return 1;
} else if (r == ADDRESS_GT) {
/* only add it now if we are bigger than the last group.
* Otherwise we'll handle it later. */
if (cur->next == NULL) {
/* put in the list */
new->prev = cur;
cur->next = new;
return 1;
}
} else if (r == ADDRESS_LT) {
/* see if we need to insert the ag anywhere put in the list */
if (cur->prev != NULL)
cur->prev->next = new;
new->prev = cur->prev;
new->next = cur;
cur->prev = new;
/* update head if required */
if (head == cur) {
head = new;
if (SetHeadPtr(gh, head) < 0)
goto error;
}
return 1;
/* alright, those were the simple cases, lets handle the more
* complex ones now */
} else if (r == ADDRESS_ES) {
c = NULL;
r = DetectAddressCut(de_ctx, cur, new, &c);
if (r == -1)
goto error;
DetectAddressInsert(de_ctx, gh, new);
if (c != NULL)
DetectAddressInsert(de_ctx, gh, c);
return 1;
} else if (r == ADDRESS_EB) {
c = NULL;
r = DetectAddressCut(de_ctx, cur, new, &c);
if (r == -1)
goto error;
DetectAddressInsert(de_ctx, gh, new);
if (c != NULL)
DetectAddressInsert(de_ctx, gh, c);
return 1;
} else if (r == ADDRESS_LE) {
c = NULL;
r = DetectAddressCut(de_ctx, cur, new, &c);
if (r == -1)
goto error;
DetectAddressInsert(de_ctx, gh, new);
if (c != NULL)
DetectAddressInsert(de_ctx, gh, c);
return 1;
} else if (r == ADDRESS_GE) {
c = NULL;
r = DetectAddressCut(de_ctx, cur,new,&c);
if (r == -1)
goto error;
DetectAddressInsert(de_ctx, gh, new);
if (c != NULL)
DetectAddressInsert(de_ctx, gh, c);
return 1;
}
}
/* head is NULL, so get a group and set head to it */
} else {
head = new;
if (SetHeadPtr(gh, head) < 0) {
SCLogDebug("SetHeadPtr failed");
goto error;
}
}
return 1;
error:
/* XXX */
return -1;
}
/**
* \brief Checks if two address group lists are equal.
*
* \param list1 Pointer to the first address group list.
* \param list2 Pointer to the second address group list.
*
* \retval true On success.
* \retval false On failure.
*/
bool DetectAddressListsAreEqual(DetectAddress *list1, DetectAddress *list2)
{
DetectAddress *item = list1;
DetectAddress *it = list2;
// First, compare items one by one.
while (item != NULL && it != NULL) {
if (DetectAddressCmp(item, it) != ADDRESS_EQ) {
return false;
}
item = item->next;
it = it->next;
}
// Are the lists of the same size?
if (!(item == NULL && it == NULL)) {
return false;
}
return true;
}
/**
* \internal
* \brief Parses an ipv4/ipv6 address string and updates the result into the
* DetectAddress instance sent as the argument.
*
* \param dd Pointer to the DetectAddress instance which should be updated with
* the address range details from the parsed ip string.
* \param str Pointer to address string that has to be parsed.
*
* \retval 0 On successfully parsing the address string.
* \retval -1 On failure.
*/
static int DetectAddressParseString(DetectAddress *dd, const char *str)
{
char *ip = NULL;
char *ip2 = NULL;
char *mask = NULL;
int r = 0;
char ipstr[256];
/* shouldn't see 'any' here */
BUG_ON(strcasecmp(str, "any") == 0);
strlcpy(ipstr, str, sizeof(ipstr));
SCLogDebug("str %s", str);
/* we work with a copy so that we can put a
* nul-termination in it later */
ip = ipstr;
/* handle the negation case */
if (ip[0] == '!') {
dd->flags |= ADDRESS_FLAG_NOT;
ip++;
}
/* see if the address is an ipv4 or ipv6 address */
if ((strchr(str, ':')) == NULL) {
/* IPv4 Address */
struct in_addr in;
dd->ip.family = AF_INET;
if ((mask = strchr(ip, '/')) != NULL) {
/* 1.2.3.4/xxx format (either dotted or cidr notation */
ip[mask - ip] = '\0';
mask++;
uint32_t ip4addr = 0;
uint32_t netmask = 0;
if ((strchr (mask, '.')) == NULL) {
/* 1.2.3.4/24 format */
for (size_t u = 0; u < strlen(mask); u++) {
if(!isdigit((unsigned char)mask[u]))
goto error;
}
int cidr;
if (StringParseI32RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 32) < 0)
goto error;
netmask = CIDRGet(cidr);
} else {
/* 1.2.3.4/255.255.255.0 format */
r = inet_pton(AF_INET, mask, &in);
if (r <= 0)
goto error;
netmask = in.s_addr;
/* validate netmask */
int cidr = CIDRFromMask(netmask);
if (cidr < 0) {
SCLogError(
"netmask \"%s\" is not usable. Only netmasks that are compatible with "
"CIDR notation are supported. See ticket #5168.",
mask);
goto error;
}
}
r = inet_pton(AF_INET, ip, &in);
if (r <= 0)
goto error;
ip4addr = in.s_addr;
dd->ip.addr_data32[0] = dd->ip2.addr_data32[0] = ip4addr & netmask;
dd->ip2.addr_data32[0] |=~ netmask;
} else if ((ip2 = strchr(ip, '-')) != NULL) {
/* 1.2.3.4-1.2.3.6 range format */
ip[ip2 - ip] = '\0';
ip2++;
r = inet_pton(AF_INET, ip, &in);
if (r <= 0)
goto error;
dd->ip.addr_data32[0] = in.s_addr;
r = inet_pton(AF_INET, ip2, &in);
if (r <= 0)
goto error;
dd->ip2.addr_data32[0] = in.s_addr;
/* a > b is illegal, a = b is ok */
if (SCNtohl(dd->ip.addr_data32[0]) > SCNtohl(dd->ip2.addr_data32[0]))
goto error;
} else {
/* 1.2.3.4 format */
r = inet_pton(AF_INET, ip, &in);
if (r <= 0)
goto error;
/* single host */
dd->ip.addr_data32[0] = in.s_addr;
dd->ip2.addr_data32[0] = in.s_addr;
}
} else {
/* IPv6 Address */
struct in6_addr in6, mask6;
uint32_t ip6addr[4], netmask[4];
dd->ip.family = AF_INET6;
if ((mask = strchr(ip, '/')) != NULL) {
ip[mask - ip] = '\0';
mask++;
int cidr;
if (StringParseI32RangeCheck(&cidr, 10, 0, (const char *)mask, 0, 128) < 0)
goto error;
r = inet_pton(AF_INET6, ip, &in6);
if (r <= 0)
goto error;
memcpy(&ip6addr, &in6.s6_addr, sizeof(ip6addr));
CIDRGetIPv6(cidr, &mask6);
memcpy(&netmask, &mask6.s6_addr, sizeof(netmask));
dd->ip2.addr_data32[0] = dd->ip.addr_data32[0] = ip6addr[0] & netmask[0];
dd->ip2.addr_data32[1] = dd->ip.addr_data32[1] = ip6addr[1] & netmask[1];
dd->ip2.addr_data32[2] = dd->ip.addr_data32[2] = ip6addr[2] & netmask[2];
dd->ip2.addr_data32[3] = dd->ip.addr_data32[3] = ip6addr[3] & netmask[3];
dd->ip2.addr_data32[0] |=~ netmask[0];
dd->ip2.addr_data32[1] |=~ netmask[1];
dd->ip2.addr_data32[2] |=~ netmask[2];
dd->ip2.addr_data32[3] |=~ netmask[3];
} else if ((ip2 = strchr(ip, '-')) != NULL) {
/* 2001::1-2001::4 range format */
ip[ip2 - ip] = '\0';
ip2++;
r = inet_pton(AF_INET6, ip, &in6);
if (r <= 0)
goto error;
memcpy(&dd->ip.address, &in6.s6_addr, sizeof(ip6addr));
r = inet_pton(AF_INET6, ip2, &in6);
if (r <= 0)
goto error;
memcpy(&dd->ip2.address, &in6.s6_addr, sizeof(ip6addr));
/* a > b is illegal, a=b is ok */
if (AddressIPv6Gt(&dd->ip, &dd->ip2))
goto error;
} else {
r = inet_pton(AF_INET6, ip, &in6);
if (r <= 0)
goto error;
memcpy(&dd->ip.address, &in6.s6_addr, sizeof(dd->ip.address));
memcpy(&dd->ip2.address, &in6.s6_addr, sizeof(dd->ip2.address));
}
}
BUG_ON(dd->ip.family == 0);
return 0;
error:
return -1;
}
/**
* \internal
* \brief Simply parse an address and return a DetectAddress instance containing
* the address ranges of the parsed ip addressstring
*
* \param str Pointer to a character string containing the ip address
*
* \retval dd Pointer to the DetectAddress instance containing the address
* range details from the parsed ip string
*/
static DetectAddress *DetectAddressParseSingle(const char *str)
{
SCLogDebug("str %s", str);
DetectAddress *dd = DetectAddressInit();
if (dd == NULL)
return NULL;
if (DetectAddressParseString(dd, str) < 0) {
SCLogDebug("AddressParse failed");
DetectAddressFree(dd);
return NULL;
}
return dd;
}
/**
* \brief Setup a single address string, parse it and add the resulting
* Address-Range(s) to the AddressHead(DetectAddressHead instance).
*
* \param gh Pointer to the Address-Head(DetectAddressHead) to which the
* resulting Address-Range(s) from the parsed ip string has to
* be added.
* \param s Pointer to the ip address string to be parsed.
*
* \retval 0 On success.
* \retval -1 On failure.
*/
static int DetectAddressSetup(DetectAddressHead *gh, const char *s)
{
SCLogDebug("gh %p, s %s", gh, s);
while (*s != '\0' && isspace(*s))
s++;
if (strcasecmp(s, "any") == 0) {
SCLogDebug("adding 0.0.0.0/0 and ::/0 as we\'re handling \'any\'");
DetectAddress *ad = DetectAddressParseSingle("0.0.0.0/0");
if (ad == NULL)
return -1;
BUG_ON(ad->ip.family == 0);
if (DetectAddressInsert(NULL, gh, ad) < 0) {
SCLogDebug("DetectAddressInsert failed");
DetectAddressFree(ad);
return -1;
}
ad = DetectAddressParseSingle("::/0");
if (ad == NULL)
return -1;
BUG_ON(ad->ip.family == 0);
if (DetectAddressInsert(NULL, gh, ad) < 0) {
SCLogDebug("DetectAddressInsert failed");
DetectAddressFree(ad);
return -1;
}
return 0;
}
/* parse the address */
DetectAddress *ad = DetectAddressParseSingle(s);
if (ad == NULL) {
SCLogError("failed to parse address \"%s\"", s);
return -1;
}
/* handle the not case, we apply the negation then insert the part(s) */
if (ad->flags & ADDRESS_FLAG_NOT) {
DetectAddress *ad2 = NULL;
if (DetectAddressCutNot(ad, &ad2) < 0) {
SCLogDebug("DetectAddressCutNot failed");
DetectAddressFree(ad);
return -1;
}
/* normally a 'not' will result in two ad's unless the 'not' is on the start or end
* of the address space (e.g. 0.0.0.0 or 255.255.255.255). */
if (ad2 != NULL) {
if (DetectAddressInsert(NULL, gh, ad2) < 0) {
SCLogDebug("DetectAddressInsert failed");
DetectAddressFree(ad);
DetectAddressFree(ad2);
return -1;
}
}
}
int r = DetectAddressInsert(NULL, gh, ad);
if (r < 0) {
SCLogDebug("DetectAddressInsert failed");
DetectAddressFree(ad);
return -1;
}
SCLogDebug("r %d",r);
return 0;
}
/**
* \brief Parses an address string and updates the 2 address heads with the
* address data.
*
* Note that this function should only be called by the wrapping function
* DetectAddressParse2. The wrapping function provides long address handling
* when the address size exceeds a threshold value.
*
* \todo We don't seem to be handling negated cases, like [addr,![!addr,addr]],
* since we pass around negate without keeping a count of ! with depth.
* Can solve this by keeping a count of the negations with depth, so that
* an even no of negations would count as no negation and an odd no of
* negations would count as a negation.
*
* \param gh Pointer to the address head that should hold address ranges
* that are not negated.
* \param ghn Pointer to the address head that should hold address ranges
* that are negated.
* \param s Pointer to the character string holding the address to be
* parsed.
* \param negate Flag that indicates if the received address string is negated
* or not. 0 if it is not, 1 it it is.
*
* \retval 0 On successfully parsing.
* \retval -1 On failure.
*/
static int DetectAddressParseInternal(const DetectEngineCtx *de_ctx, DetectAddressHead *gh,
DetectAddressHead *ghn, const char *s, int negate, ResolvedVariablesList *var_list,
int recur, char *address, size_t address_length)
{
size_t x = 0;
size_t u = 0;
int o_set = 0, n_set = 0, d_set = 0;
int depth = 0;
const char *rule_var_address = NULL;
char *temp_rule_var_address = NULL;
if (++recur > 64) {
SCLogError("address block recursion "
"limit reached (max 64)");
goto error;
}
SCLogDebug("s %s negate %s", s, negate ? "true" : "false");
size_t size = strlen(s);
for (u = 0, x = 0; u < size && x < address_length; u++) {
if (x == (address_length - 1)) {
SCLogError("Hit the address buffer"
" limit for the supplied address. Invalidating sig. "
"Please file a bug report on this.");
goto error;
}
address[x] = s[u];
x++;
if (!o_set && s[u] == '!') {
n_set = 1;
x--;
} else if (s[u] == '[') {
if (!o_set) {
o_set = 1;
x = 0;
}
depth++;
} else if (s[u] == ']') {
if (depth == 1) {
address[x - 1] = '\0';
x = 0;
SCLogDebug("address %s negate %d, n_set %d", address, negate, n_set);
if (((negate + n_set) % 2) == 0) {
/* normal block */
SCLogDebug("normal block");
if (DetectAddressParse2(de_ctx, gh, ghn, address, (negate + n_set) % 2, var_list, recur) < 0)
goto error;
} else {
/* negated block
*
* Extra steps are necessary. First consider it as a normal
* (non-negated) range. Merge the + and - ranges if
* applicable. Then insert the result into the ghn list. */
SCLogDebug("negated block");
DetectAddressHead tmp_gh = { NULL, NULL };
DetectAddressHead tmp_ghn = { NULL, NULL };
if (DetectAddressParse2(de_ctx, &tmp_gh, &tmp_ghn, address, 0, var_list, recur) < 0) {
DetectAddressHeadCleanup(&tmp_gh);
DetectAddressHeadCleanup(&tmp_ghn);
goto error;
}
DetectAddress *tmp_ad;
DetectAddress *tmp_ad2;
#ifdef DEBUG
SCLogDebug("tmp_gh: IPv4");
for (tmp_ad = tmp_gh.ipv4_head; tmp_ad; tmp_ad = tmp_ad->next) {
DetectAddressPrint(tmp_ad);
}
SCLogDebug("tmp_ghn: IPv4");
for (tmp_ad = tmp_ghn.ipv4_head; tmp_ad; tmp_ad = tmp_ad->next) {
DetectAddressPrint(tmp_ad);
}
SCLogDebug("tmp_gh: IPv6");
for (tmp_ad = tmp_gh.ipv6_head; tmp_ad; tmp_ad = tmp_ad->next) {
DetectAddressPrint(tmp_ad);
}
SCLogDebug("tmp_ghn: IPv6");
for (tmp_ad = tmp_ghn.ipv6_head; tmp_ad; tmp_ad = tmp_ad->next) {
DetectAddressPrint(tmp_ad);
}
#endif
if (DetectAddressMergeNot(&tmp_gh, &tmp_ghn) < 0) {
DetectAddressHeadCleanup(&tmp_ghn);
DetectAddressHeadCleanup(&tmp_gh);
goto error;
}
DetectAddressHeadCleanup(&tmp_ghn);
SCLogDebug("merged successfully");
/* insert the IPv4 addresses into the negated list */
for (tmp_ad = tmp_gh.ipv4_head; tmp_ad; tmp_ad = tmp_ad->next) {
/* work with a copy of the address group */
tmp_ad2 = DetectAddressCopy(tmp_ad);
if (tmp_ad2 == NULL) {
SCLogDebug("DetectAddressCopy failed");
DetectAddressHeadCleanup(&tmp_gh);
goto error;
}
DetectAddressPrint(tmp_ad2);
DetectAddressInsert(NULL, ghn, tmp_ad2);
}
/* insert the IPv6 addresses into the negated list */
for (tmp_ad = tmp_gh.ipv6_head; tmp_ad; tmp_ad = tmp_ad->next) {
/* work with a copy of the address group */
tmp_ad2 = DetectAddressCopy(tmp_ad);
if (tmp_ad2 == NULL) {
SCLogDebug("DetectAddressCopy failed");
DetectAddressHeadCleanup(&tmp_gh);
goto error;
}
DetectAddressPrint(tmp_ad2);
DetectAddressInsert(NULL, ghn, tmp_ad2);
}
DetectAddressHeadCleanup(&tmp_gh);
}
n_set = 0;
}
depth--;
} else if (depth == 0 && s[u] == ',') {
if (o_set == 1) {
o_set = 0;
} else if (d_set == 1) {
address[x - 1] = '\0';
rule_var_address = SCRuleVarsGetConfVar(de_ctx, address,
SC_RULE_VARS_ADDRESS_GROUPS);
if (rule_var_address == NULL)
goto error;
if (strlen(rule_var_address) == 0) {
SCLogError("variable %s resolved "
"to nothing. This is likely a misconfiguration. "
"Note that a negated address needs to be quoted, "
"\"!$HOME_NET\" instead of !$HOME_NET. See issue #295.",
s);
goto error;
}
SCLogDebug("rule_var_address %s", rule_var_address);
if ((negate + n_set) % 2) {
temp_rule_var_address = SCMalloc(strlen(rule_var_address) + 3);
if (unlikely(temp_rule_var_address == NULL))
goto error;
snprintf(temp_rule_var_address, strlen(rule_var_address) + 3,
"[%s]", rule_var_address);
} else {
temp_rule_var_address = SCStrdup(rule_var_address);
if (unlikely(temp_rule_var_address == NULL))
goto error;
}
if (DetectAddressParse2(de_ctx, gh, ghn, temp_rule_var_address,
(negate + n_set) % 2, var_list, recur) < 0) {
if (temp_rule_var_address != rule_var_address)
SCFree(temp_rule_var_address);
goto error;
}
d_set = 0;
n_set = 0;
SCFree(temp_rule_var_address);
} else {
address[x - 1] = '\0';
if (!((negate + n_set) % 2)) {
SCLogDebug("DetectAddressSetup into gh, %s", address);
if (DetectAddressSetup(gh, address) < 0)
goto error;
} else {
SCLogDebug("DetectAddressSetup into ghn, %s", address);
if (DetectAddressSetup(ghn, address) < 0)
goto error;
}
n_set = 0;
}
x = 0;
} else if (depth == 0 && s[u] == '$') {
d_set = 1;
} else if (depth == 0 && u == size - 1) {
if (x == address_length) {
address[x - 1] = '\0';
} else {
address[x] = '\0';
}
x = 0;
if (AddVariableToResolveList(var_list, address) == -1) {
SCLogError("Found a loop in a address "
"groups declaration. This is likely a misconfiguration.");
goto error;
}
if (d_set == 1) {
rule_var_address = SCRuleVarsGetConfVar(de_ctx, address,
SC_RULE_VARS_ADDRESS_GROUPS);
if (rule_var_address == NULL)
goto error;
if (strlen(rule_var_address) == 0) {
SCLogError("variable %s resolved "
"to nothing. This is likely a misconfiguration. "
"Note that a negated address needs to be quoted, "
"\"!$HOME_NET\" instead of !$HOME_NET. See issue #295.",
s);
goto error;
}
SCLogDebug("rule_var_address %s", rule_var_address);
if ((negate + n_set) % 2) {
temp_rule_var_address = SCMalloc(strlen(rule_var_address) + 3);
if (unlikely(temp_rule_var_address == NULL))
goto error;
snprintf(temp_rule_var_address, strlen(rule_var_address) + 3,
"[%s]", rule_var_address);
} else {
temp_rule_var_address = SCStrdup(rule_var_address);
if (unlikely(temp_rule_var_address == NULL))
goto error;
}
if (DetectAddressParse2(de_ctx, gh, ghn, temp_rule_var_address,
(negate + n_set) % 2, var_list, recur) < 0) {
SCLogDebug("DetectAddressParse2 hates us");
if (temp_rule_var_address != rule_var_address)
SCFree(temp_rule_var_address);
goto error;
}
d_set = 0;
SCFree(temp_rule_var_address);
} else {
if (!((negate + n_set) % 2)) {
SCLogDebug("DetectAddressSetup into gh, %s", address);
if (DetectAddressSetup(gh, address) < 0) {
SCLogDebug("DetectAddressSetup gh fail");
goto error;
}
} else {
SCLogDebug("DetectAddressSetup into ghn, %s", address);
if (DetectAddressSetup(ghn, address) < 0) {
SCLogDebug("DetectAddressSetup ghn fail");
goto error;
}
}
}
n_set = 0;
}
}
if (depth > 0) {
SCLogError("not every address block was "
"properly closed in \"%s\", %d missing closing brackets (]). "
"Note: problem might be in a variable.",
s, depth);
goto error;
} else if (depth < 0) {
SCLogError("not every address block was "
"properly opened in \"%s\", %d missing opening brackets ([). "
"Note: problem might be in a variable.",
s, depth * -1);
goto error;
}
return 0;
error:
return -1;
}
/**
* \internal
* \brief Wrapper function for address parsing to minimize heap allocs during address parsing.
*
* \retval Return value from DetectAddressParseInternal
*/
static int DetectAddressParse2(const DetectEngineCtx *de_ctx, DetectAddressHead *gh,
DetectAddressHead *ghn, const char *s, int negate, ResolvedVariablesList *var_list,
int recur)
{
int rc;
#define MAX_ADDRESS_LENGTH 8192
size_t address_length = strlen(s);
if (address_length > (MAX_ADDRESS_LENGTH - 1)) {
char *address = SCCalloc(1, address_length);
if (address == NULL) {
SCLogError("Unable to allocate"
" memory for address parsing.");
return -1;
}
rc = DetectAddressParseInternal(
de_ctx, gh, ghn, s, negate, var_list, recur, address, address_length);
SCFree(address);
} else {
char address[MAX_ADDRESS_LENGTH] = "";
rc = DetectAddressParseInternal(