-
Notifications
You must be signed in to change notification settings - Fork 3
/
lsmsb.aw
2488 lines (1974 loc) · 69.2 KB
/
lsmsb.aw
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
<!DOCTYPE html>
<html>
<head>
<title>LSMSB</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" charset="utf-8">
</head>
<body>
<h1>LSMSB: A Linux Sandboxing Scheme</h1>
<p>Adam Langley (<tt>[email protected]</tt>)<br>
Version <tt>20090606</tt></p>
@@TOC
<p>This is LSMSB, a sandboxing scheme for Linux based on the ideas of the <a
href="http://www.318.com/techjournal/security/a-brief-introduction-to-mac-os-x-sandbox-technology/">
OS X sandbox</a> (which, in turn, was inspired by TrustedBSD and FreeBSD).</p>
<p>Imagine that you're working on a university computer and you get a binary
which promises to do some fiendishly complex calculation, reading from a file
<tt>./input</tt> and writing to a file <tt>./output</tt>. It also talks to a
specific server to access a pre-computed lookup table. You want to run it, but
you don't want to have to trust that it won't do anything malicious (save
giving the wrong answer).</p>
<p>You current options are very limited. Without root access you cannot setup a
chroot jail, as troublesome as that is. If the system has SELinux or AppArmor
installed, the tools are there but those are MAC systems and only root can
define their policies.</p>
<p>Your best bet, currently, is either to use <tt>ptrace</tt> or to run a whole
virtual machine. The former is slow and difficult to get right in the face of
threads and the latter is a sledgehammer when we just want to crack a nut.</p>
<p>To address these concerns we need a sandboxing system which is:</p>
<ol>
<li>Available: The sandbox must be available to normal users. MAC systems
which are only configured by root are not the correct solution for this.</li>
<li>Flexible: <tt>seccomp</tt> is (currently) too rigid to really be useful. A
sandbox must be able to express the correct level of authority.</li>
<li>Reliable: The sandbox should not be open to races etc.</li>
<li>Deployable: One should be able to use a sandbox via a couple of call in
<tt>main</tt>(). If you have to implement an IPC system and pass file descriptors around
in order to achieve reliability then that's a significant demerit.</li>
<li>Composable: If I choose to impose a sandbox on a process before <tt>exec</tt>()
then that process should still be able to impose another sandbox on itself. The
resulting authority should be the intersection of the two sandboxes.</li>
<li>Affordable: It should be reasonable to sandbox many processes. If the
sandbox has a high performance impact, then that's a problem. If the sandbox
requires a tracing process for every sandboxed process, then that's a
problem.</li>
</ol>
<p>We present a sandboxing scheme using the LSM hooks in the Linux kernel. At
the moment this scheme is a prototype only and this code is based off of
2.6.30-rc<i>x</i>.</p>
@/* Kernel code
<div style="float:left; padding-right: 1em;"><object width="212" height="200" data="dia1.svg" type="image/svg+xml" class="img"></object></div>
<p>LSMSB uses the Linux Security Modules hooks to intercept security decisions in
the kernel. The policies are implemented by tables of rules which are uploaded
from user-space. Each process has a stack of zero or more sandboxes. Each
sandbox may define a rule table for a given operation. An action must be
accepted by all the sandboxes in the stack for it to be permitted. Child
processes inherit the sandbox stack of their parents and are free to push extra
sandboxes onto their stack. By construction, this can only reduce their
authority.</p>
<div style="clear: left;"></div>
@<Sandbox structure@>=
struct lsmsb_sandbox {
atomic_t refcount;
struct lsmsb_sandbox *parent;
struct lsmsb_filter *dentry_open;
// TODO: add support for more actions
};
@/ Rule tables
<p>An LSMSB filter evaluates a table of rules to decide if a given action is
permitted or not. The table of rules is specific to a given filter as the
filter defines the context in which the table is evaluated. The context
determines which arguments are provided to the table, their order, syntax and
semantics. It also defines the meaning of the return code although, by
convention, zero always means reject and non-zero means accept.</p>
<p>The rule tables are very limited. Conditionals do exist, but a rule can only
jump forward, thus there are no loops. This also means that the rule tables are
not Turing complete and are guaranteed to terminate.</p>
<h5>The filter structure</h5>
<p>Each rule in the table is a single 32-bit unsigned integer. Because constant
values often don't fit in 32-bits, we need another way to deal with them. Thus
constants are kept in a side array, linked with each filter. The rules in the
table can reference them by their index.</p>
<p>For working storage, the rules in the table have an array of 16 registers which
can either store a 32-bit unsigned integer or a byte-string (which is a normal
string, but may contain NUL characters). If a table is exceedingly complex, it
may need more than 16-registers of storage to hold temporary values. For these
situations, we also allow the table to specify the number of spill slots
that it needs. Spill slots act just like registers except that they have to be
read and written explicitly.</p>
<p>From this, the structure for a filter is obvious:</p>
@<Filter structure@>=
struct lsmsb_filter {
unsigned num_operations;
unsigned num_spill_slots;
unsigned num_constants;
uint32_t *operations;
struct lsmsb_value constants[0];
};
#define LSMSB_NUM_REGISTERS 16
// This is the maximum number of operations in a filter. Note that the code
// assumes that this value fits in a uint16_t with a couple of values to spare.
#define LSMSB_FILTER_OPS_MAX 32768
#define LSMSB_SPILL_SLOTS_MAX 32
#define LSMSB_CONSTANTS_MAX 256
#define LSMSB_CONSTANT_LENGTH_MAX 512
@/ Filter values
<p>
The contents of registers, spill slots and constants are all ‘values’. These
values are either a 32-bit unsigned int or a bytestring and represented by the
following structure.
</p>
<p>
For integer values, <tt>value</tt> is the integer and <tt>data</tt> is set to <tt>NULL</tt>. For
bytestrings, <tt>data</tt> is a pointer to the bytes (and thus not <tt>NULL</tt>) and <tt>value</tt>
is the number of bytes.
</p>
@<Value structure@>=
struct lsmsb_value {
uint8_t *data;
uint32_t value;
};
@/ Operations
<p>
Each rule in the table consists of at least an operation, which is encoded in
the top 8-bits. Given the operation, there are often other arguments (register
numbers etc) encoded in the remaining 24-bits, the format of which is specific
to each operation.
</p>
<p>
The operations are numbered from zero by the following enum:
</p>
<table>
<tr><th>Name</th><th>Explanation</th><th>Effect</th></tr>
<tr><td><tt>MOV</tt></td> <td>Move</td> <td>reg<sub>1</sub> = reg<sub>2</sub></td></tr>
<tr><td><tt>LDI</tt></td> <td>Load immediate</td> <td>reg<sub>1</sub> = immediate value</td></tr>
<tr><td><tt>LDC</tt></td> <td>Load constant</td> <td>reg<sub>1</sub> = constant value</td></tr>
<tr><td><tt>RET</tt></td> <td>Return value in register</td> <td><i>terminates the filter</i></td></tr>
<tr><td><tt>JMP</tt></td> <td>Jump</td> <td>Skips the next <i>n</i> rules</td></tr>
<tr><td><tt>SPILL</tt></td> <td>Spill register</td> <td>spill<sub>1</sub> = reg<sub>1</sub></td></tr>
<tr><td><tt>UNSPILL</tt></td> <td>Unspill register</td> <td>reg<sub>1</sub> = spill<sub>1</sub></td></tr>
<tr><td><tt>JC</tt></td> <td>Jump conditionally</td> <td>If reg<sub>1</sub> > 0 then skip the next <i>n</i> rules</td></tr>
<tr><td><tt>EQ</tt></td> <td>Equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> == reg<sub>3</sub></td></tr>
<tr><td><tt>GT</tt></td> <td>Greater than?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> > reg<sub>3</sub></td></tr>
<tr><td><tt>LT</tt></td> <td>Less than?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> < reg<sub>3</sub></td></tr>
<tr><td><tt>GTE</tt></td> <td>Greater than or equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> >= reg<sub>3</sub></td></tr>
<tr><td><tt>LTE</tt></td> <td>Less than or equal?</td> <td>reg<sub>1</sub> = reg<sub>2</sub> <= reg<sub>3</sub></td></tr>
<tr><td><tt>AND</tt></td> <td>Bitwise conjunction</td> <td>reg<sub>1</sub> = reg<sub>2</sub> & reg<sub>3</sub></td></tr>
<tr><td><tt>OR</tt></td> <td>Bitwise disjunction</td> <td>reg<sub>1</sub> = reg<sub>2</sub> | reg<sub>3</sub></td></tr>
<tr><td><tt>XOR</tt></td> <td>Bitwise exclusive-or</td> <td>reg<sub>1</sub> = reg<sub>2</sub> ^ reg<sub>3</sub></td></tr>
<tr><td><tt>ISPREFIXOF</tt></td> <td>Bytestring prefix</td> <td>reg<sub>1</sub> = reg<sub>2</sub> ⊆ reg<sub>3</sub></td></tr>
</table>
@<List of operations@>=
enum lsmsb_opcode {
LSMSB_OPCODE_MOV = 0,
LSMSB_OPCODE_LDI,
LSMSB_OPCODE_LDC,
LSMSB_OPCODE_RET,
LSMSB_OPCODE_JMP,
LSMSB_OPCODE_SPILL,
LSMSB_OPCODE_UNSPILL,
LSMSB_OPCODE_JC,
LSMSB_OPCODE_EQ,
LSMSB_OPCODE_GT,
LSMSB_OPCODE_LT,
LSMSB_OPCODE_GTE,
LSMSB_OPCODE_LTE,
LSMSB_OPCODE_AND,
LSMSB_OPCODE_OR,
LSMSB_OPCODE_XOR,
LSMSB_OPCODE_ISPREFIXOF,
};
@/ Available filter types
<p>The type of a filter signifies the operation which it intends to filter, as
well as the context that it runs in. (See <a href="@@cite:Filter structure@@">above</a>). Here are the currently defined filters.</p>
<table>
<tr><th>Name</th> <th>Explanation</th> <th>Arguments</th></tr>
<tr><td><tt>DENTRY_OPEN</tt></td> <td>File open</td> <td>Filename(<i>bs</i>) and mode(<i>i</i>)</td></tr>
</table>
@<Filter codes@>=
enum lsmsb_filter_code {
LSMSB_FILTER_CODE_DENTRY_OPEN = 0,
LSMSB_FILTER_CODE_MAX, // not a real filter code
};
@/** Typechecking.
<p>
The rule tables are simple enough that they can be validated such that we can
know they they can never cause a run-time error. This is helpful as it means
that we can pay the verification cost once (when loading the table) and omit
run-time checks when evaluating.
</p>
@<Typechecking@>=
@<Predecessor table utility functions@>
@<Predecessor tables@>
@<Type unification@>
@<Typecheck function@>
@/ Predecessor tables
<p>
For each rule in the table there is a set of rules which can be the immediate
predecessor of that rule. In the simple case, the rule preceding will fall
though and be the single predecessor. In more complex cases, a rule might be
the target of several jumps and a fall though.
</p>
<p>
The predecessor table conceptually has <i>n</i> rows and <i>x</i> columns, where <i>n</i> is
the number of rules in the rule table and <i>x</i> is a constant value. If we didn't
have jumps each rule could only have a single predecessor, <i>x</i> could be one,
and we would have a simple array. However, in the presence of jumps <i>x</i> may
need to be greater than one.
</p>
<p>
In the pessimal case, every rule in the table except the last is a jump to the
very last one. Then <i>x</i> would need to be <i>n - 1</i>, even though most of
the slots in the table would be unused. Therefore we take a hybrid approach. We
define <i>x</i> to be a small number and, if a row overflows, we mark it with a
magic value. Most of the time this will work fine but, in exceptional cases,
we have to deal with the overflows.
</p>
<div style="text-align: center;"><object width="420" height="175" data="dia2.svg" type="image/svg+xml" class="img"></object></div>
@<Predecessor tables@>=
/* This is the magic unset value in the predecessor table. This value must be
* all ones because we clear the table with a memset. */
#define LSMSB_PREDECESSOR_TABLE_INVAL 0xffff
/* This is a magic value in the predecessor table which marks an overflow. */
#define LSMSB_PREDECESSOR_TABLE_OVERFLOW (LSMSB_FILTER_OPS_MAX + 1)
/* This is the number of predecessors that we record, per row, before
* overflowing */
#define LSMSB_PREDECESSOR_TABLE_WIDTH 2
@<Appending to a predecessor table row@>
@<Building the predecessor table@>
@/ Appending to a predecessor table row
<p>
Appending to a row simply involves scanning the entries in the row until a
free slot is found. If we don't find a free slot we mark the first entry in the
row with the magic overflow tag.
</p>
@<Appending to a predecessor table row@>=
static void lsmsb_predecessor_table_append(uint16_t *ptable, unsigned target,
unsigned source)
{
uint16_t *row = &ptable[target * LSMSB_PREDECESSOR_TABLE_WIDTH];
unsigned i;
for (i = 0; i < LSMSB_PREDECESSOR_TABLE_WIDTH; ++i) {
if (row[i] == LSMSB_PREDECESSOR_TABLE_OVERFLOW)
return; // this is already an overflow row
if (row[i] == LSMSB_PREDECESSOR_TABLE_INVAL) {
row[i] = source;
return;
}
}
/* we reached the end of the table without a spare slot. We have to mark
the row as overflowed. */
row[0] = LSMSB_PREDECESSOR_TABLE_OVERFLOW;
}
@/ Building the predecessor table
<p>
We build the predecessor table by first clearing it then, for each rule, we
find its one or two successor instructions and mark it as a predecessor for
those instructions.
</p>
@<Building the predecessor table@>=
static char lsmsb_predecessor_table_fill(uint16_t *ptable, const uint32_t *ops, unsigned num_ops)
{
unsigned i;
if (num_ops == 0)
return 1;
@<Clear the predecessor table@>
/* First we deal with all the elements except the last. */
for (i = 0; i < num_ops - 1; ++i) {
const uint32_t op = ops[i];
const enum lsmsb_opcode opcode = lsmsb_op_opcode_get(op);
if (lsmsb_opcode_falls_through(opcode))
lsmsb_predecessor_table_append(ptable, i + 1, i);
if (lsmsb_opcode_is_jump(opcode)) {
/* 0 <= i, jumplength <= 0xffff */
const unsigned target = i + lsmsb_op_jump_length(op);
if (target == i)
return 0; /* zero length jump */
if (target >= num_ops)
return 0; /* tried to jump off the end */
lsmsb_predecessor_table_append(ptable, target, i);
}
}
/* Now deal with the last operation. */
if (lsmsb_op_opcode_get(ops[num_ops - 1]) != LSMSB_OPCODE_RET)
return 0;
return 1;
}
@/ Clearing the predecessor table
<p>
Since we chose our magic unused value to be <tt>0xffff</tt>, we can clear the table
using <tt>memset</tt>.
</p>
@<Clear the predecessor table@>=
memset(ptable, 0xff, sizeof(uint16_t) * num_ops * LSMSB_PREDECESSOR_TABLE_WIDTH);
@/ Type unification
<p>
Type unification is the process of finding, for each rule, the set of possible
types for each register and spill slot. Since we only have two types (integers
and bytestrings), we can encode this using only a couple of bits.
</p>
<p>
If we then find a rule which is going to operate on a register which could
possibly hold the wrong type, we reject the rule table.
</p>
@<Type unification@>=
@<lsmsb_type@>
@<Type vector utility functions@>
@<is_predecessor_of@>
@<Type vector unification@>
@<Type vector updating@>
@<Type vector arrays@>
@<Filter contexts@>
@<type_vector_for_filter@>
@/ Type vectors
<p>
A <i>type vector</i> is an array of 2-bit values, one for each register and
spill slot. Each 2-bit value is either:
</p>
<table>
<tr><td>Undefined:</td> <td>at the start of evaluation, every spill slot and register
which isn't defined by the context has an undefined type.</td></tr>
<tr><td>Integer:</td> <td>an integer type.</td></tr>
<tr><td>Bytestring:</td> <td>a byte-string type.</td></tr>
<tr><td>Conflicting:</td> <td>the type differs depending on the control flow.</td></tr>
</table>
@<lsmsb_type|<tt>lsmsb_type</tt>@>=
enum lsmsb_type {
LSMSB_TYPE_UNDEF = 0,
LSMSB_TYPE_U32,
LSMSB_TYPE_BYTESTRING,
LSMSB_TYPE_CONFLICTING,
};
static inline char lsmsb_type_is_value(enum lsmsb_type type)
{
return type == LSMSB_TYPE_U32 || type == LSMSB_TYPE_BYTESTRING;
}
static inline enum lsmsb_type lsmsb_constant_type_get(const struct lsmsb_value *v)
{
return v->data ? LSMSB_TYPE_BYTESTRING : LSMSB_TYPE_U32;
}
@/ Unifying type vectors
<p>
Given two type vectors (say, the type vectors from the two predecessors of a
rule), we unify them by considering each element of each vector, pairwise.
Then, for each pair, if they match then we output that value, otherwise, we
record the value as conflicting.
</p>
<p>
To implement this quickly, we try to operate on many elements concurrently.
Most of the time a rule table will not use spill slots so the type vector will
be 2 × 16 = 32 bits long and we can do them all in one go.
</p>
<p>
To understand the code below, consider a single pair of 2-bit inputs. If we
exclusive-or them, we'll end up with a true bit in the result iff the inputs
differed. If we could map 01, 10, and 11 to 11 and then OR with the original
input, that would map equal inputs to themselves and differing inputs to
<tt>LSMSB_TYPE_CONFLICTING</tt>. (Remember that
<tt>LSMSB_TYPE_CONFLICTING</tt> <a href="@@cite:lsmsb_type@@">has a value of
11</a> in binary).
</p>
<p>
In order to transform the exclusive-or output we define <tt>left</tt> to be the left
bit and <tt>right</tt> to be the right bit. Then we OR together <tt>left</tt>,
<tt>right</tt>, <tt>left >> 1</tt> and <tt>right << 1</tt>. This
results in the desired mapping.
</p>
<p>
We work with 32-bit quantities where possible and fall back to 8-bit values
otherwise. Although the type vector might not be a multiple of 4 values long,
we still operate on the undefined bits at the end because it's harmless and
faster.
</p>
@<Type vector unification@>=
static void lsmsb_type_vector_unify(uint8_t *a, const uint8_t *b, unsigned bytelen)
{
unsigned offset = 0;
while (bytelen >= 4) {
uint32_t u = *((uint32_t *) (a + offset));
uint32_t v = *((uint32_t *) (b + offset));
uint32_t x = u ^ v;
uint32_t left = x & 0xaaaaaaaau, right = x & 0x55555555;
uint32_t result = (left | (left >> 1)) | (right | (right << 1));
*((uint32_t *) (a + offset)) |= result;
offset += 4;
bytelen -= 4;
}
while (bytelen) {
uint8_t u = a[offset];
uint8_t v = b[offset];
uint8_t x = u ^ v;
uint32_t left = x & 0xaa, right = x & 0x55;
uint32_t result = (left | (left >> 1)) | (right | (right << 1));
a[offset] |= result;
offset++;
bytelen--;
}
}
@/ Updating the type vectors for an operation
<p>
Each operation may require certain types as inputs and may mutate the type of
outputs. We model this in a function which operates on type vectors.
</p>
<p>
At any point, if an operation would encounter a type error, we return 0.
</p>
@<Type vector updating@>=
static char lsmsb_op_type_vector_update(uint8_t *tv, uint32_t op,
const struct lsmsb_value *constants,
unsigned num_constants,
unsigned num_spills) {
const enum lsmsb_opcode opcode = lsmsb_op_opcode_get(op);
unsigned reg1, reg2, reg3;
enum lsmsb_type type1, type2, type3;
unsigned c1, s1;
switch (opcode) {
case LSMSB_OPCODE_MOV:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
type2 = lsmsb_type_vector_reg_get(tv, reg2);
if (!lsmsb_type_is_value(type2))
return 0;
lsmsb_type_vector_reg_set(tv, reg1, type2);
return 1;
case LSMSB_OPCODE_LDI:
reg1 = lsmsb_op_reg1_get(op);
lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);
return 1;
case LSMSB_OPCODE_LDC:
reg1 = lsmsb_op_reg1_get(op);
c1 = lsmsb_op_constant1_get(op);
if (c1 >= num_constants)
return 0;
type1 = lsmsb_constant_type_get(&constants[c1]);
lsmsb_type_vector_reg_set(tv, reg1, type1);
return 1;
case LSMSB_OPCODE_RET:
reg1 = lsmsb_op_reg1_get(op);
if (lsmsb_type_vector_reg_get(tv, reg1) != LSMSB_TYPE_U32)
return 0;
return 1;
case LSMSB_OPCODE_JMP:
return 1;
case LSMSB_OPCODE_SPILL:
s1 = lsmsb_op_spill1_get(op);
if (s1 >= num_spills)
return 0;
reg1 = lsmsb_op_reg3_get(op);
type1 = lsmsb_type_vector_reg_get(tv, reg1);
if (!lsmsb_type_is_value(type1))
return 0;
lsmsb_type_vector_spill_set(tv, s1, type1);
return 1;
case LSMSB_OPCODE_UNSPILL:
reg1 = lsmsb_op_reg1_get(op);
s1 = lsmsb_op_spill2_get(op);
if (s1 >= num_spills)
return 0;
type1 = lsmsb_type_vector_spill_get(tv, s1);
if (!lsmsb_type_is_value(type1))
return 0;
lsmsb_type_vector_reg_set(tv, reg1, type1);
return 1;
case LSMSB_OPCODE_JC:
reg1 = lsmsb_op_reg1_get(op);
if (lsmsb_type_vector_reg_get(tv, reg1) != LSMSB_TYPE_U32)
return 0;
return 1;
case LSMSB_OPCODE_EQ:
case LSMSB_OPCODE_GT:
case LSMSB_OPCODE_LT:
case LSMSB_OPCODE_GTE:
case LSMSB_OPCODE_LTE:
case LSMSB_OPCODE_AND:
case LSMSB_OPCODE_OR:
case LSMSB_OPCODE_XOR:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
type2 = lsmsb_type_vector_reg_get(tv, reg2);
type3 = lsmsb_type_vector_reg_get(tv, reg3);
if (type2 != LSMSB_TYPE_U32 || type3 != LSMSB_TYPE_U32)
return 0;
lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);
return 1;
case LSMSB_OPCODE_ISPREFIXOF:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
type2 = lsmsb_type_vector_reg_get(tv, reg2);
type3 = lsmsb_type_vector_reg_get(tv, reg3);
if (type2 != LSMSB_TYPE_BYTESTRING ||
type3 != LSMSB_TYPE_BYTESTRING)
return 0;
lsmsb_type_vector_reg_set(tv, reg1, LSMSB_TYPE_U32);
return 1;
default:
return 0;
}
}
@/ Type vector arrays
<p>
A <i>type vector array</i> is a simply an array of type vectors, one for each
operation in the rule table. We can build a type vector array by iterating over
the operations, unifying the type vectors for all predecessors and then
updating the type vector for that operation.
</p>
<p>
By the end we will have either found a type error or proved that evaluating the
rule table will not encounter any run-time errors.
</p>
@<Type vector arrays@>=
static char lsmsb_type_vector_array_fill(uint8_t *tva,
const uint16_t *ptable,
const uint8_t *context_tv,
const struct lsmsb_filter *filter) {
const unsigned tva_width = lsmsb_filter_tva_width(filter);
const uint32_t *ops = filter->operations;
unsigned i, j;
if (filter->num_operations == 0)
return 1;
memset(tva, 0, tva_width); /* set the first row to LSMSB_TYPE_UNDEF */
memcpy(tva, context_tv, LSMSB_NUM_REGISTERS / 4);
if (!lsmsb_op_type_vector_update(tva, ops[0], filter->constants,
filter->num_constants,
filter->num_spill_slots)) {
return 0;
}
for (i = 1; i < filter->num_operations; ++i) {
const uint16_t *ptable_row =
&ptable[i * LSMSB_PREDECESSOR_TABLE_WIDTH];
uint8_t *tva_row = tva + i * tva_width;
char found_predecessor = 0;
if (ptable_row[0] == LSMSB_PREDECESSOR_TABLE_OVERFLOW) {
@<handle overflowed row@>
} else {
@<handle normal row@>
}
if (!lsmsb_op_type_vector_update(tva_row, ops[i],
filter->constants,
filter->num_constants,
filter->num_spill_slots)) {
return 0;
}
}
return 1;
}
@/ Unifying normal rows
<p>
With a row where the predecessor table didn't overflow, we can easily find the
predecessor operations. For the first one, we need to copy its type vector. For
all subsequent predecessors, we unify the current type vector with their
type vector.
</p>
<p>
Here we also deal with the case of an operation with no predecessor. This can
only occur if no control flow path leads to it. In this case, we reject the
rule table. We assume that the tools used to generate the rule tables are
sufficiently smart to remove dead code and we don't want to waste kernel memory
keeping it around.
</p>
@<handle normal row@>=
for (j = 0; j < LSMSB_PREDECESSOR_TABLE_WIDTH; ++j) {
const unsigned p = ptable_row[j];
const uint8_t *tva_row_p = tva + p * tva_width;
if (p == LSMSB_PREDECESSOR_TABLE_INVAL)
break;
if (!found_predecessor) {
memcpy(tva_row, tva_row_p, tva_width);
found_predecessor = 1;
continue;
}
lsmsb_type_vector_unify(tva_row, tva_row_p,
tva_width);
}
if (!found_predecessor)
return 0; // Dead code.
@/ Unifying overflow rows
<p>
When the predecessor table is marked as overflowed, we have to find all the
predecessors ourselves. We can do this by checking all previous operations for
jumps to the current operation and checking the previous instruction for fall
though. (Recall that we only have forward jumps.)
</p>
@<handle overflowed row@>=
for (j = 0; j < i; ++j) {
if (lsmsb_op_is_predecessor_of(ops, j, i)) {
if (!found_predecessor) {
memcpy(tva_row,
tva + j * tva_width,
tva_width);
found_predecessor = 1;
continue;
}
lsmsb_type_vector_unify(
tva_row,
tva + j * tva_width,
tva_width);
}
}
if (!found_predecessor)
return 0; // shouldn't ever happen
@/ Testing for predecessor rules
@<is_predecessor_of|Predecessor testing function@>=
static char lsmsb_op_is_predecessor_of(const uint32_t *ops, unsigned i,
unsigned target) {
const uint32_t op = ops[i];
const enum lsmsb_opcode opcode = lsmsb_op_opcode_get(op);
if (i == target - 1 &&
lsmsb_opcode_falls_through(opcode)) {
return 1;
}
if (lsmsb_opcode_is_jump(opcode) &&
lsmsb_op_jump_length(op) + i == target) {
return 1;
}
return 0;
}
@/ Type vector utility functions
@<Type vector utility functions@>=
static inline unsigned lsmsb_filter_tva_width(const struct lsmsb_filter *filter)
{
return (LSMSB_NUM_REGISTERS + filter->num_spill_slots + 3) / 4;
}
static inline enum lsmsb_type lsmsb_type_vector_reg_get(uint8_t *vector, unsigned reg)
{
const unsigned byte = reg >> 2;
const unsigned index = reg & 3;
return (enum lsmsb_type) ((vector[byte] >> (6 - (index * 2))) & 3);
}
static inline enum lsmsb_type lsmsb_type_vector_spill_get(uint8_t *vector, unsigned slot)
{
return lsmsb_type_vector_reg_get(vector, slot + LSMSB_NUM_REGISTERS);
}
static inline void lsmsb_type_vector_reg_set(uint8_t *vector, unsigned reg,
enum lsmsb_type newtype)
{
const unsigned byte = reg >> 2;
const unsigned index = reg & 3;
static const uint8_t masks[4] = { 0x3f, 0xcf, 0xf3, 0xfc };
const uint8_t new_value = (vector[byte] & masks[index]) |
newtype << (6 - (index * 2));
vector[byte] = new_value;
}
static inline void lsmsb_type_vector_spill_set(uint8_t *vector, unsigned slot,
enum lsmsb_type newtype)
{
lsmsb_type_vector_reg_set(vector, slot + LSMSB_NUM_REGISTERS, newtype);
}
@/ Bringing typechecking together
<p>
Pulling together the above code is straight-forward. We allocate memory for the
predecessor table and type vector array and generate them both. If we manage to
generate them both and the rule table checks out, then we're done.
</p>
@<Typecheck function@>=
int lsmsb_filter_typecheck(const struct lsmsb_filter *filter,
const uint8_t *context_type_vector)
{
const unsigned tva_width = lsmsb_filter_tva_width(filter);
uint16_t *predecessor_table;
uint8_t *type_vector_array;
int return_code = -EINVAL;
predecessor_table = (uint16_t*) kmalloc(
filter->num_operations *
LSMSB_PREDECESSOR_TABLE_WIDTH *
sizeof(uint16_t), GFP_KERNEL);
if (!predecessor_table)
return -ENOMEM;
type_vector_array = kmalloc(filter->num_operations *
tva_width, GFP_KERNEL);
if (!type_vector_array) {
kfree(predecessor_table);
return -ENOMEM;
}
if (!lsmsb_predecessor_table_fill(predecessor_table, filter->operations,
filter->num_operations)) {
goto exit;
}
if (!lsmsb_type_vector_array_fill(type_vector_array, predecessor_table,
context_type_vector, filter)) {
goto exit;
}
return_code = 0;
exit:
kfree(type_vector_array);
kfree(predecessor_table);
return return_code;
}
@/** Evaluating filters
<p>Since typechecking has eliminated most run-time errors from the filter, the
evaluation of filters can happen without many of those checks.</p>
<p>The evaluation is straight-forward: a register machine is simulated with
with the register values in an array on the stack. Each instruction is
dispatched using a switch. There are several pieces of low-hanging fruit that
could make this code faster but, for now, we choose the simplest code that
works.</p>
@<Evaluating filters@>=
char lsmsb_filter_run(const struct lsmsb_filter *filter,
const struct lsmsb_value *init_values,
unsigned num_init_values)
{
unsigned ip = 0;
struct lsmsb_value regs[LSMSB_NUM_REGISTERS];
struct lsmsb_value *spills = NULL;
uint32_t op;
enum lsmsb_opcode opcode;
unsigned reg1, reg2, reg3, c1, s1;
unsigned imm;
char return_value, returned = 0;
memcpy(regs, init_values, num_init_values * sizeof(struct lsmsb_value));
if (filter->num_spill_slots) {
spills = kmalloc(sizeof(struct lsmsb_value) *
filter->num_spill_slots, GFP_ATOMIC);
if (!spills) {
printk("lsmsb: failed to allocate %u spill slots\n",
filter->num_spill_slots);
return 0;
}
}
for (ip = 0; !returned; ++ip) {
op = filter->operations[ip];
opcode = lsmsb_op_opcode_get(op);
switch (opcode) {
case LSMSB_OPCODE_MOV:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
memcpy(®s[reg1], ®s[reg2],
sizeof(struct lsmsb_value));
break;
case LSMSB_OPCODE_LDI:
reg1 = lsmsb_op_reg1_get(op);
imm = lsmsb_op_imm_get(op);
lsmsb_value_u32_set(®s[reg1], imm);
break;
case LSMSB_OPCODE_LDC:
reg1 = lsmsb_op_reg1_get(op);
c1 = lsmsb_op_constant1_get(op);
memcpy(®s[reg1], &filter->constants[c1],
sizeof(struct lsmsb_value));
break;
case LSMSB_OPCODE_RET:
reg1 = lsmsb_op_reg1_get(op);
return_value = regs[reg1].value > 0;
returned = 1;
break;
case LSMSB_OPCODE_JMP:
ip--;
ip += lsmsb_op_jump_length(op);
break;
case LSMSB_OPCODE_SPILL:
s1 = lsmsb_op_spill1_get(op);
reg1 = lsmsb_op_reg3_get(op);
memcpy(&spills[s1], ®s[reg1],
sizeof(struct lsmsb_value));
break;
case LSMSB_OPCODE_UNSPILL:
reg1 = lsmsb_op_reg1_get(op);
s1 = lsmsb_op_spill2_get(op);
memcpy(®s[reg1], &spills[s1],
sizeof(struct lsmsb_value));
break;
case LSMSB_OPCODE_JC:
reg1 = lsmsb_op_reg1_get(op);
if (regs[reg1].value) {
ip--;
ip += lsmsb_op_jump_length(op);
}
break;
case LSMSB_OPCODE_EQ:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value == regs[reg3].value;
break;
case LSMSB_OPCODE_GT:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value > regs[reg3].value;
break;
case LSMSB_OPCODE_LT:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value < regs[reg3].value;
break;
case LSMSB_OPCODE_GTE:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value >= regs[reg3].value;
break;
case LSMSB_OPCODE_LTE:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value <= regs[reg3].value;
break;
case LSMSB_OPCODE_AND:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value & regs[reg3].value;
break;
case LSMSB_OPCODE_OR:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value | regs[reg3].value;
break;
case LSMSB_OPCODE_XOR:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].value = regs[reg2].value ^ regs[reg3].value;
break;
case LSMSB_OPCODE_ISPREFIXOF:
reg1 = lsmsb_op_reg1_get(op);
reg2 = lsmsb_op_reg2_get(op);
reg3 = lsmsb_op_reg3_get(op);
regs[reg1].data = NULL;
if (regs[reg2].value > regs[reg3].value) {
regs[reg1].value = 0;
} else {
regs[reg1].value =
memcmp(regs[reg2].data, regs[reg3].data,
regs[reg2].value) == 0;
}
break;
default:
// should never hit this
returned = 1;
return_value = 0;
break;
}
}