-
Notifications
You must be signed in to change notification settings - Fork 167
/
gen_bpf.c
2362 lines (2137 loc) · 62.3 KB
/
gen_bpf.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
/**
* Seccomp BPF Translator
*
* Copyright (c) 2012 Red Hat <pmoore@redhat.com>
* Author: Paul Moore <paul@paul-moore.com>
*/
/*
* This library is free software; you can redistribute it and/or modify it
* under the terms of version 2.1 of the GNU Lesser General Public License as
* published by the Free Software Foundation.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses>.
*/
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#include <endian.h>
#include <seccomp.h>
#include "arch.h"
#include "arch-x32.h"
#include "gen_bpf.h"
#include "db.h"
#include "hash.h"
#include "system.h"
#include "helper.h"
/* allocation increments */
#define AINC_BLK 2
#define AINC_PROG 64
/* binary tree definitions */
#define SYSCALLS_PER_NODE (4)
#define BTREE_HSH_INVALID (UINT64_MAX)
#define BTREE_SYSCALL_INVALID (UINT_MAX)
struct acc_state {
int32_t offset;
uint32_t mask;
};
#define _ACC_STATE(x,y) \
(struct acc_state){ .offset = (x), .mask = (y) }
#define _ACC_STATE_OFFSET(x) \
_ACC_STATE(x,ARG_MASK_MAX)
#define _ACC_STATE_UNDEF \
_ACC_STATE_OFFSET(-1)
#define _ACC_CMP_EQ(x,y) \
((x).offset == (y).offset && (x).mask == (y).mask)
enum bpf_jump_type {
TGT_NONE = 0,
TGT_K, /* immediate "k" value */
TGT_NXT, /* fall through to the next block */
TGT_IMM, /* resolved immediate value */
TGT_PTR_DB, /* pointer to part of the filter db */
TGT_PTR_BLK, /* pointer to an instruction block */
TGT_PTR_HSH, /* pointer to a block hash table */
};
struct bpf_jump {
union {
uint8_t imm_j;
uint32_t imm_k;
uint64_t hash;
struct db_arg_chain_tree *db;
struct bpf_blk *blk;
unsigned int nxt;
} tgt;
enum bpf_jump_type type;
};
#define _BPF_OP(a,x) \
(_htot16(a,x))
#define _BPF_JMP_NO \
((struct bpf_jump) { .type = TGT_NONE })
#define _BPF_JMP_NXT(x) \
((struct bpf_jump) { .type = TGT_NXT, .tgt = { .nxt = (x) } })
#define _BPF_JMP_IMM(x) \
((struct bpf_jump) { .type = TGT_IMM, .tgt = { .imm_j = (x) } })
#define _BPF_JMP_DB(x) \
((struct bpf_jump) { .type = TGT_PTR_DB, .tgt = { .db = (x) } })
#define _BPF_JMP_BLK(x) \
((struct bpf_jump) { .type = TGT_PTR_BLK, .tgt = { .blk = (x) } })
#define _BPF_JMP_HSH(x) \
((struct bpf_jump) { .type = TGT_PTR_HSH, .tgt = { .hash = (x) } })
#define _BPF_K(a,x) \
((struct bpf_jump) { .type = TGT_K, .tgt = { .imm_k = _htot32(a,x) } })
#define _BPF_JMP_MAX 255
#define _BPF_JMP_MAX_RET 255
struct bpf_instr {
uint16_t op;
struct bpf_jump jt;
struct bpf_jump jf;
struct bpf_jump k;
};
#define _BPF_OFFSET_SYSCALL (offsetof(struct seccomp_data, nr))
#define _BPF_SYSCALL(a) _BPF_K(a,_BPF_OFFSET_SYSCALL)
#define _BPF_OFFSET_ARCH (offsetof(struct seccomp_data, arch))
#define _BPF_ARCH(a) _BPF_K(a,_BPF_OFFSET_ARCH)
struct bpf_blk {
/* bpf instructions */
struct bpf_instr *blks;
unsigned int blk_cnt;
unsigned int blk_alloc;
/* accumulator state */
struct acc_state acc_start;
struct acc_state acc_end;
/* priority - higher is better */
unsigned int priority;
/* status flags */
bool flag_hash; /* added to the hash table */
bool flag_dup; /* duplicate block and in use */
bool flag_unique; /* ->blks is unique to this block */
/* original db_arg_chain_tree node */
const struct db_arg_chain_tree *node;
/* used during block assembly */
uint64_t hash;
struct bpf_blk *hash_nxt;
struct bpf_blk *prev, *next;
struct bpf_blk *lvl_prv, *lvl_nxt;
};
#define _BLK_MSZE(x) \
((x)->blk_cnt * sizeof(*((x)->blks)))
struct bpf_hash_bkt {
struct bpf_blk *blk;
struct bpf_hash_bkt *next;
unsigned int found;
};
#define _BPF_HASH_BITS 8
#define _BPF_HASH_SIZE (1 << _BPF_HASH_BITS)
#define _BPF_HASH_MASK (_BPF_HASH_BITS - 1)
struct bpf_state {
/* block hash table */
struct bpf_hash_bkt *htbl[_BPF_HASH_SIZE];
/* filter attributes */
const struct db_filter_attr *attr;
/* bad arch action */
uint64_t bad_arch_hsh;
/* default action */
uint64_t def_hsh;
/* bpf program */
struct bpf_program *bpf;
/* WARNING - the following variables are temporary use only */
const struct arch_def *arch;
struct bpf_blk *b_head;
struct bpf_blk *b_tail;
struct bpf_blk *b_new;
};
/**
* Populate a BPF instruction
* @param _ins the BPF instruction
* @param _op the BPF operand
* @param _jt the BPF jt value
* @param _jf the BPF jf value
* @param _k the BPF k value
*
* Set the given values on the provided bpf_instr struct.
*
*/
#define _BPF_INSTR(_ins,_op,_jt,_jf,_k) \
do { \
memset(&(_ins), 0, sizeof(_ins)); \
(_ins).op = (_op); \
(_ins).jt = _jt; \
(_ins).jf = _jf; \
(_ins).k = _k; \
} while (0)
static struct bpf_blk *_gen_bpf_chain(struct bpf_state *state,
const struct db_sys_list *sys,
const struct db_arg_chain_tree *chain,
const struct bpf_jump *nxt_jump,
struct acc_state *a_state);
static struct bpf_blk *_hsh_remove(struct bpf_state *state, uint64_t h_val);
static struct bpf_blk *_hsh_find(const struct bpf_state *state, uint64_t h_val);
/**
* Convert a 16-bit host integer into the target's endianness
* @param arch the architecture definition
* @param val the 16-bit integer
*
* Convert the endianness of the supplied value and return it to the caller.
*
*/
static uint16_t _htot16(const struct arch_def *arch, uint16_t val)
{
if (arch->endian == ARCH_ENDIAN_LITTLE)
return htole16(val);
else
return htobe16(val);
}
/**
* Convert a 32-bit host integer into the target's endianness
* @param arch the architecture definition
* @param val the 32-bit integer
*
* Convert the endianness of the supplied value and return it to the caller.
*
*/
static uint32_t _htot32(const struct arch_def *arch, uint32_t val)
{
if (arch->endian == ARCH_ENDIAN_LITTLE)
return htole32(val);
else
return htobe32(val);
}
/**
* Free the BPF instruction block
* @param state the BPF state
* @param blk the BPF instruction block
*
* Free the BPF instruction block, any linked blocks are preserved and the hash
* table is not modified. In general, you probably want to use _blk_free()
* instead.
*
*/
static void __blk_free(struct bpf_state *state, struct bpf_blk *blk)
{
struct bpf_blk *b_tmp;
while (blk->hash_nxt != NULL) {
b_tmp = blk->hash_nxt;
blk->hash_nxt = b_tmp->hash_nxt;
if (!b_tmp->flag_dup)
free(b_tmp);
}
if (blk->blks != NULL && blk->flag_unique)
free(blk->blks);
free(blk);
}
/**
* Free the BPF instruction block
* @param state the BPF state
* @param blk the BPF instruction block
*
* Free the BPF instruction block including any linked blocks. The hash table
* is updated to reflect the newly removed block(s).
*
*/
static void _blk_free(struct bpf_state *state, struct bpf_blk *blk)
{
int iter;
struct bpf_blk *b_iter;
struct bpf_instr *i_iter;
if (blk == NULL)
return;
/* remove this block from the hash table */
_hsh_remove(state, blk->hash);
/* run through the block freeing TGT_PTR_{BLK,HSH} jump targets */
for (iter = 0; iter < blk->blk_cnt; iter++) {
i_iter = &blk->blks[iter];
switch (i_iter->jt.type) {
case TGT_PTR_BLK:
_blk_free(state, i_iter->jt.tgt.blk);
break;
case TGT_PTR_HSH:
b_iter = _hsh_find(state, i_iter->jt.tgt.hash);
_blk_free(state, b_iter);
break;
default:
/* do nothing */
break;
}
switch (i_iter->jf.type) {
case TGT_PTR_BLK:
_blk_free(state, i_iter->jf.tgt.blk);
break;
case TGT_PTR_HSH:
b_iter = _hsh_find(state, i_iter->jf.tgt.hash);
_blk_free(state, b_iter);
break;
default:
/* do nothing */
break;
}
}
__blk_free(state, blk);
}
/**
* Allocate and initialize a new instruction block
*
* Allocate a new BPF instruction block and perform some very basic
* initialization. Returns a pointer to the block on success, NULL on failure.
*
*/
static struct bpf_blk *_blk_alloc(void)
{
struct bpf_blk *blk;
blk = zmalloc(sizeof(*blk));
if (blk == NULL)
return NULL;
blk->flag_unique = true;
blk->acc_start = _ACC_STATE_UNDEF;
blk->acc_end = _ACC_STATE_UNDEF;
return blk;
}
/**
* Resize an instruction block
* @param state the BPF state
* @param blk the existing instruction block, or NULL
* @param size_add the minimum amount of instructions to add
*
* Resize the given instruction block such that it is at least as large as the
* current size plus @size_add. Returns a pointer to the block on success,
* NULL on failure.
*
*/
static struct bpf_blk *_blk_resize(struct bpf_state *state,
struct bpf_blk *blk,
unsigned int size_add)
{
unsigned int size_adj = (AINC_BLK > size_add ? AINC_BLK : size_add);
struct bpf_instr *new;
size_t old_size, new_size;
if (blk == NULL)
return NULL;
if ((blk->blk_cnt + size_adj) <= blk->blk_alloc)
return blk;
old_size = blk->blk_alloc * sizeof(*new);
blk->blk_alloc += size_adj;
new_size = blk->blk_alloc * sizeof(*new);
new = zrealloc(blk->blks, old_size, new_size);
if (new == NULL) {
_blk_free(state, blk);
return NULL;
}
blk->blks = new;
return blk;
}
/**
* Append a new BPF instruction to an instruction block
* @param state the BPF state
* @param blk the existing instruction block, or NULL
* @param instr the new instruction
*
* Add the new BPF instruction to the end of the given instruction block. If
* the given instruction block is NULL, a new block will be allocated. Returns
* a pointer to the block on success, NULL on failure, and in the case of
* failure the instruction block is free'd.
*
*/
static struct bpf_blk *_blk_append(struct bpf_state *state,
struct bpf_blk *blk,
const struct bpf_instr *instr)
{
if (blk == NULL) {
blk = _blk_alloc();
if (blk == NULL)
return NULL;
}
if (_blk_resize(state, blk, 1) == NULL)
return NULL;
memcpy(&blk->blks[blk->blk_cnt++], instr, sizeof(*instr));
return blk;
}
/**
* Prepend a new BPF instruction to an instruction block
* @param state the BPF state
* @param blk the existing instruction block, or NULL
* @param instr the new instruction
*
* Add the new BPF instruction to the start of the given instruction block.
* If the given instruction block is NULL, a new block will be allocated.
* Returns a pointer to the block on success, NULL on failure, and in the case
* of failure the instruction block is free'd.
*
*/
static struct bpf_blk *_blk_prepend(struct bpf_state *state,
struct bpf_blk *blk,
const struct bpf_instr *instr)
{
/* empty - we can treat this like a normal append operation */
if (blk == NULL || blk->blk_cnt == 0)
return _blk_append(state, blk, instr);
if (_blk_resize(state, blk, 1) == NULL)
return NULL;
memmove(&blk->blks[1], &blk->blks[0], blk->blk_cnt++ * sizeof(*instr));
memcpy(&blk->blks[0], instr, sizeof(*instr));
return blk;
}
/**
* Append a block of BPF instructions to the final BPF program
* @param prg the BPF program
* @param blk the BPF instruction block
*
* Add the BPF instruction block to the end of the BPF program and perform the
* necessary translation. Returns zero on success, negative values on failure
* and in the case of failure the BPF program is free'd.
*
*/
static int _bpf_append_blk(struct bpf_program *prg, const struct bpf_blk *blk)
{
int rc;
bpf_instr_raw *i_new;
bpf_instr_raw *i_iter;
unsigned int old_cnt = prg->blk_cnt;
unsigned int iter;
size_t old_size, new_size;
/* (re)allocate the program memory */
old_size = BPF_PGM_SIZE(prg);
prg->blk_cnt += blk->blk_cnt;
new_size = BPF_PGM_SIZE(prg);
i_new = zrealloc(prg->blks, old_size, new_size);
if (i_new == NULL) {
rc = -ENOMEM;
goto bpf_append_blk_failure;
}
prg->blks = i_new;
/* transfer and translate the blocks to raw instructions */
for (iter = 0; iter < blk->blk_cnt; iter++) {
i_iter = &(prg->blks[old_cnt + iter]);
i_iter->code = blk->blks[iter].op;
switch (blk->blks[iter].jt.type) {
case TGT_NONE:
i_iter->jt = 0;
break;
case TGT_IMM:
/* jump to the value specified */
i_iter->jt = blk->blks[iter].jt.tgt.imm_j;
break;
default:
/* fatal error - we should never get here */
rc = -EFAULT;
goto bpf_append_blk_failure;
}
switch (blk->blks[iter].jf.type) {
case TGT_NONE:
i_iter->jf = 0;
break;
case TGT_IMM:
/* jump to the value specified */
i_iter->jf = blk->blks[iter].jf.tgt.imm_j;
break;
default:
/* fatal error - we should never get here */
rc = -EFAULT;
goto bpf_append_blk_failure;
}
switch (blk->blks[iter].k.type) {
case TGT_K:
i_iter->k = blk->blks[iter].k.tgt.imm_k;
break;
default:
/* fatal error - we should never get here */
rc = -EFAULT;
goto bpf_append_blk_failure;
}
}
return prg->blk_cnt;
bpf_append_blk_failure:
prg->blk_cnt = 0;
free(prg->blks);
return rc;
}
/**
* Free the BPF program
* @param prg the BPF program
*
* Free the BPF program. None of the associated BPF state used to generate the
* BPF program is released in this function.
*
*/
static void _program_free(struct bpf_program *prg)
{
if (prg == NULL)
return;
if (prg->blks != NULL)
free(prg->blks);
free(prg);
}
/**
* Free the BPF state
* @param state the BPF state
*
* Free all of the BPF state, including the BPF program if present.
*
*/
static void _state_release(struct bpf_state *state)
{
unsigned int bkt;
struct bpf_hash_bkt *iter;
if (state == NULL)
return;
/* release all of the hash table entries */
for (bkt = 0; bkt < _BPF_HASH_SIZE; bkt++) {
while (state->htbl[bkt]) {
iter = state->htbl[bkt];
state->htbl[bkt] = iter->next;
__blk_free(state, iter->blk);
free(iter);
}
}
_program_free(state->bpf);
memset(state, 0, sizeof(*state));
}
/**
* Add an instruction block to the BPF state hash table
* @param state the BPF state
* @param blk_p pointer to the BPF instruction block
* @param found initial found value (see _hsh_find_once() for description)
*
* This function adds an instruction block to the hash table, and frees the
* block if an identical instruction block already exists, returning a pointer
* to the original block in place of the given block. Returns zero on success
* and negative values on failure.
*
*/
static int _hsh_add(struct bpf_state *state, struct bpf_blk **blk_p,
unsigned int found)
{
uint64_t h_val, h_val_tmp[3];
struct bpf_hash_bkt *h_new, *h_iter, *h_prev = NULL;
struct bpf_blk *blk = *blk_p;
struct bpf_blk *b_iter;
if (blk->flag_hash)
return 0;
h_new = zmalloc(sizeof(*h_new));
if (h_new == NULL)
return -ENOMEM;
/* generate the hash */
h_val_tmp[0] = hash(blk->blks, _BLK_MSZE(blk));
h_val_tmp[1] = hash(&blk->acc_start, sizeof(blk->acc_start));
h_val_tmp[2] = hash(&blk->acc_end, sizeof(blk->acc_end));
h_val = hash(h_val_tmp, sizeof(h_val_tmp));
blk->hash = h_val;
blk->flag_hash = true;
blk->node = NULL;
h_new->blk = blk;
h_new->found = (found ? 1 : 0);
/* insert the block into the hash table */
hsh_add_restart:
h_iter = state->htbl[h_val & _BPF_HASH_MASK];
if (h_iter != NULL) {
do {
if ((h_iter->blk->hash == h_val) &&
(_BLK_MSZE(h_iter->blk) == _BLK_MSZE(blk)) &&
(memcmp(h_iter->blk->blks, blk->blks,
_BLK_MSZE(blk)) == 0) &&
_ACC_CMP_EQ(h_iter->blk->acc_start,
blk->acc_start) &&
_ACC_CMP_EQ(h_iter->blk->acc_end,
blk->acc_end)) {
/* duplicate block */
free(h_new);
/* store the duplicate block */
b_iter = h_iter->blk;
while (b_iter->hash_nxt != NULL)
b_iter = b_iter->hash_nxt;
b_iter->hash_nxt = blk;
/* in some cases we want to return the
* duplicate block */
if (found) {
blk->flag_dup = true;
return 0;
}
/* update the priority if needed */
if (h_iter->blk->priority < blk->priority)
h_iter->blk->priority = blk->priority;
/* try to save some memory */
free(blk->blks);
blk->blks = h_iter->blk->blks;
blk->flag_unique = false;
*blk_p = h_iter->blk;
return 0;
} else if (h_iter->blk->hash == h_val) {
/* hash collision */
if ((h_val >> 32) == 0xffffffff) {
/* overflow */
blk->flag_hash = false;
blk->hash = 0;
free(h_new);
return -EFAULT;
}
h_val += ((uint64_t)1 << 32);
h_new->blk->hash = h_val;
/* restart at the beginning of the bucket */
goto hsh_add_restart;
} else {
/* no match, move along */
h_prev = h_iter;
h_iter = h_iter->next;
}
} while (h_iter != NULL);
h_prev->next = h_new;
} else
state->htbl[h_val & _BPF_HASH_MASK] = h_new;
return 0;
}
/**
* Remove an entry from the hash table
* @param state the BPF state
* @param h_val the hash value
*
* Remove an entry from the hash table and return it to the caller, NULL is
* returned if the entry can not be found.
*
*/
static struct bpf_blk *_hsh_remove(struct bpf_state *state, uint64_t h_val)
{
unsigned int bkt = h_val & _BPF_HASH_MASK;
struct bpf_blk *blk;
struct bpf_hash_bkt *h_iter, *h_prev = NULL;
h_iter = state->htbl[bkt];
while (h_iter != NULL) {
if (h_iter->blk->hash == h_val) {
if (h_prev != NULL)
h_prev->next = h_iter->next;
else
state->htbl[bkt] = h_iter->next;
blk = h_iter->blk;
free(h_iter);
return blk;
}
h_prev = h_iter;
h_iter = h_iter->next;
}
return NULL;
}
/**
* Find and return a hash bucket
* @param state the BPF state
* @param h_val the hash value
*
* Find the entry associated with the given hash value and return it to the
* caller, NULL is returned if the entry can not be found. This function
* should not be called directly; use _hsh_find() and _hsh_find_once() instead.
*
*/
static struct bpf_hash_bkt *_hsh_find_bkt(const struct bpf_state *state,
uint64_t h_val)
{
struct bpf_hash_bkt *h_iter;
h_iter = state->htbl[h_val & _BPF_HASH_MASK];
while (h_iter != NULL) {
if (h_iter->blk->hash == h_val)
return h_iter;
h_iter = h_iter->next;
}
return NULL;
}
/**
* Find and only return an entry in the hash table once
* @param state the BPF state
* @param h_val the hash value
*
* Find the entry associated with the given hash value and return it to the
* caller if it has not be returned previously by this function; returns NULL
* if the entry can not be found or has already been returned in a previous
* call.
*
*/
static struct bpf_blk *_hsh_find_once(const struct bpf_state *state,
uint64_t h_val)
{
struct bpf_hash_bkt *h_iter;
h_iter = _hsh_find_bkt(state, h_val);
if (h_iter == NULL || h_iter->found != 0)
return NULL;
h_iter->found = 1;
return h_iter->blk;
}
/**
* Finds an entry in the hash table
* @param state the BPF state
* @param h_val the hash value
*
* Find the entry associated with the given hash value and return it to the
* caller, NULL is returned if the entry can not be found.
*
*/
static struct bpf_blk *_hsh_find(const struct bpf_state *state, uint64_t h_val)
{
struct bpf_hash_bkt *h_iter;
h_iter = _hsh_find_bkt(state, h_val);
if (h_iter == NULL)
return NULL;
return h_iter->blk;
}
/**
* Generate a BPF action instruction
* @param state the BPF state
* @param blk the BPF instruction block, or NULL
* @param action the desired action
*
* Generate a BPF action instruction and append it to the given instruction
* block. Returns a pointer to the instruction block on success, NULL on
* failure.
*
*/
static struct bpf_blk *_gen_bpf_action(struct bpf_state *state,
struct bpf_blk *blk, uint32_t action)
{
struct bpf_instr instr;
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_RET),
_BPF_JMP_NO, _BPF_JMP_NO, _BPF_K(state->arch, action));
return _blk_append(state, blk, &instr);
}
/**
* Generate a BPF action instruction and insert it into the hash table
* @param state the BPF state
* @param action the desired action
*
* Generate a BPF action instruction and insert it into the hash table.
* Returns a pointer to the instruction block on success, NULL on failure.
*
*/
static struct bpf_blk *_gen_bpf_action_hsh(struct bpf_state *state,
uint32_t action)
{
struct bpf_blk *blk;
blk = _gen_bpf_action(state, NULL, action);
if (blk == NULL)
return NULL;
if (_hsh_add(state, &blk, 0) < 0) {
_blk_free(state, blk);
return NULL;
}
return blk;
}
/**
* Generate a BPF instruction block for a given chain node
* @param state the BPF state
* @param node the filter chain node
* @param a_state the accumulator state
*
* Generate BPF instructions to execute the filter for the given chain node.
* Returns a pointer to the instruction block on success, NULL on failure.
*
*/
static struct bpf_blk *_gen_bpf_node(struct bpf_state *state,
const struct db_arg_chain_tree *node,
struct acc_state *a_state)
{
int32_t acc_offset;
uint32_t acc_mask;
uint64_t act_t_hash = 0, act_f_hash = 0;
struct bpf_blk *blk, *b_act;
struct bpf_instr instr;
blk = _blk_alloc();
if (blk == NULL)
return NULL;
blk->acc_start = *a_state;
/* generate the action blocks */
if (node->act_t_flg) {
b_act = _gen_bpf_action_hsh(state, node->act_t);
if (b_act == NULL)
goto node_failure;
act_t_hash = b_act->hash;
}
if (node->act_f_flg) {
b_act = _gen_bpf_action_hsh(state, node->act_f);
if (b_act == NULL)
goto node_failure;
act_f_hash = b_act->hash;
}
/* check the accumulator state */
acc_offset = node->arg_offset;
acc_mask = node->mask;
if (acc_offset < 0)
goto node_failure;
if ((acc_offset != a_state->offset) ||
((acc_mask & a_state->mask) != acc_mask)) {
/* reload the accumulator */
a_state->offset = acc_offset;
a_state->mask = ARG_MASK_MAX;
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_LD + BPF_ABS),
_BPF_JMP_NO, _BPF_JMP_NO,
_BPF_K(state->arch, acc_offset));
blk = _blk_append(state, blk, &instr);
if (blk == NULL)
goto node_failure;
/* we're not dependent on the accumulator anymore */
blk->acc_start = _ACC_STATE_UNDEF;
}
if (acc_mask != a_state->mask) {
/* apply the bitmask */
a_state->mask = acc_mask;
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_ALU + BPF_AND),
_BPF_JMP_NO, _BPF_JMP_NO,
_BPF_K(state->arch, acc_mask));
blk = _blk_append(state, blk, &instr);
if (blk == NULL)
goto node_failure;
}
/* set the accumulator state at the end of the block */
/* NOTE: the accumulator end state is very critical when we are
* assembling the final state; we assume that however we leave
* this instruction block the accumulator state is represented
* by blk->acc_end, it must be kept correct */
blk->acc_end = *a_state;
/* check the accumulator against the datum */
switch (node->op) {
case SCMP_CMP_MASKED_EQ:
case SCMP_CMP_EQ:
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_JMP + BPF_JEQ),
_BPF_JMP_NO, _BPF_JMP_NO,
_BPF_K(state->arch, node->datum));
break;
case SCMP_CMP_GT:
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_JMP + BPF_JGT),
_BPF_JMP_NO, _BPF_JMP_NO,
_BPF_K(state->arch, node->datum));
break;
case SCMP_CMP_GE:
_BPF_INSTR(instr, _BPF_OP(state->arch, BPF_JMP + BPF_JGE),
_BPF_JMP_NO, _BPF_JMP_NO,
_BPF_K(state->arch, node->datum));
break;
case SCMP_CMP_NE:
case SCMP_CMP_LT:
case SCMP_CMP_LE:
default:
/* fatal error, we should never get here */
goto node_failure;
}
/* fixup the jump targets */
if (node->nxt_t != NULL)
instr.jt = _BPF_JMP_DB(node->nxt_t);
else if (node->act_t_flg)
instr.jt = _BPF_JMP_HSH(act_t_hash);
else
instr.jt = _BPF_JMP_NXT(0);
if (node->nxt_f != NULL)
instr.jf = _BPF_JMP_DB(node->nxt_f);
else if (node->act_f_flg)
instr.jf = _BPF_JMP_HSH(act_f_hash);
else
instr.jf = _BPF_JMP_NXT(0);
blk = _blk_append(state, blk, &instr);
if (blk == NULL)
goto node_failure;
blk->node = node;
return blk;
node_failure:
_blk_free(state, blk);
return NULL;
}
/**
* Resolve the jump targets in a BPF instruction block
* @param state the BPF state
* @param sys the syscall filter
* @param blk the BPF instruction block
* @param nxt_jump the jump to fallthrough to at the end of the level
*
* Resolve the jump targets in a BPF instruction block generated by the
* _gen_bpf_chain_lvl() function and adds the resulting block to the hash
* table. Returns a pointer to the new instruction block on success, NULL on
* failure.
*
*/
static struct bpf_blk *_gen_bpf_chain_lvl_res(struct bpf_state *state,
const struct db_sys_list *sys,
struct bpf_blk *blk,
const struct bpf_jump *nxt_jump)
{
int rc;
unsigned int iter;
struct bpf_blk *b_new;
struct bpf_instr *i_iter;
struct db_arg_chain_tree *node;
if (blk->flag_hash)
return blk;
/* convert TGT_PTR_DB to TGT_PTR_HSH references */
for (iter = 0; iter < blk->blk_cnt; iter++) {
i_iter = &blk->blks[iter];
switch (i_iter->jt.type) {
case TGT_NONE:
case TGT_IMM:
case TGT_PTR_HSH:
/* ignore these jump types */
break;
case TGT_PTR_BLK:
b_new = _gen_bpf_chain_lvl_res(state, sys,
i_iter->jt.tgt.blk,
nxt_jump);
if (b_new == NULL)
return NULL;
i_iter->jt = _BPF_JMP_HSH(b_new->hash);
break;
case TGT_PTR_DB:
node = (struct db_arg_chain_tree *)i_iter->jt.tgt.db;
b_new = _gen_bpf_chain(state, sys, node,
nxt_jump, &blk->acc_end);
if (b_new == NULL)
return NULL;
i_iter->jt = _BPF_JMP_HSH(b_new->hash);
break;
default:
/* we should not be here */
return NULL;
}
switch (i_iter->jf.type) {
case TGT_NONE:
case TGT_IMM:
case TGT_PTR_HSH:
/* ignore these jump types */
break;
case TGT_PTR_BLK:
b_new = _gen_bpf_chain_lvl_res(state, sys,