|
| 1 | +open HolKernel boolLib liteLib simpLib Parse bossLib; |
| 2 | +open policy_arith_to_varTheory; |
| 3 | +open bdd_utilsLib; |
| 4 | + |
| 5 | +val _ = new_theory "policy_min_example"; |
| 6 | + |
| 7 | + |
| 8 | +(* --------------------------------------------------------------------------- |
| 9 | + Policy Minimization Example |
| 10 | + |
| 11 | + This script demonstrates PolyGram's policy minimization on a |
| 12 | + policy defined over three predicates: |
| 13 | + |
| 14 | + y1: tcp.dstport <= 1023 (standard service ports) |
| 15 | + y2: tcp.dstport >= 49152 (dynamic/ephemeral ports) |
| 16 | + z: ip.ttl >= 2 (packet has enough hops left) |
| 17 | + |
| 18 | + |
| 19 | + The input policy has 9 rules with several issues: |
| 20 | + - Rules that are unreachable due to match-first semantics |
| 21 | + - Rules with unsatisfiable conditions (e.g. y1 AND y2, which is empty) |
| 22 | + - Rules that are subsumed by earlier rules |
| 23 | + - Redundant conditions within a single rule |
| 24 | + |
| 25 | + PolyGram generates a minimized policy and produces a certified proof |
| 26 | + that the minimized policy is semantically equivalent to the original. |
| 27 | + --------------------------------------------------------------------------- *) |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Type single_rule = “:((string# num list) action_expr) arith_rule”; |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +(* Packet type descriptor: encodes the bit-vector width of each header field |
| 36 | + Used by trans-back to compute interval complements (Section IV-D) *) |
| 37 | +val test_pd_type = “["h", type_record [("ip", type_record [ ("ttl", type_length 8);]); |
| 38 | + ("tcp", type_record [("dstPort", type_length 16)])]]”; |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +(* tcp.dstport <= 1023 (standard-service traffic) *) |
| 43 | +val y1 = “(arithm_le (lv_acc (lv_acc (lv_x "h") "tcp") "dstPort") ^(bdd_utilsLib.make_bv 1023 16))”; |
| 44 | + |
| 45 | +(* tcp.dstport >= 49152 (dynamic-port traffic) *) |
| 46 | +val y2 = “(arithm_ge (lv_acc (lv_acc (lv_x "h") "tcp") "dstPort") ^(bdd_utilsLib.make_bv 49152 16))”; |
| 47 | + |
| 48 | +(* ip.ttl >= 2 *) |
| 49 | +val z = “(arithm_ge (lv_acc (lv_acc (lv_x "h") "ip") "ttl") ^(bdd_utilsLib.make_bv 2 8))”; |
| 50 | + |
| 51 | + |
| 52 | +(* Lifted predicates *) |
| 53 | + |
| 54 | +val a_y1 = “arith_a ^y1”; |
| 55 | +val a_y2 = “arith_a ^y2”; |
| 56 | +val a_z = “arith_a ^z”; |
| 57 | + |
| 58 | + |
| 59 | +(* ----------------------------------------------------------------------- |
| 60 | + Bloated input policy (9 rules) |
| 61 | + |
| 62 | + Rule 1: y1 AND z : fwd(1) standard ports, good TTL |
| 63 | + Rule 2: y1 AND z AND y2 ; fwd(1) UNSATISFIABLE: y1 AND y2 is empty |
| 64 | + (disjoint port ranges) |
| 65 | + Rule 3: y2 AND z : fwd(1) dynamic ports, good TTL |
| 66 | + Rule 4: y1 AND NOT y1: fwd(2) UNSATISFIABLE: always false |
| 67 | + Rule 5: y1 : fwd(1) subsumes Rule 1 (no z needed) |
| 68 | + but comes AFTER Rule 1 in match-first! |
| 69 | + Rule 6: NOT y1 AND NOT y2 AND z ; fwd(1) middle ports, good TTL |
| 70 | + Rule 7: y2 AND z AND NOT z ; fwd(1) UNSATISFIABLE: z AND NOT z is empty |
| 71 | + Rule 8: NOT z : fwd(2) bad TTL -> fwd(2) |
| 72 | + Rule 9: y1 AND NOT z : fwd(1) standard ports, bad TTL |
| 73 | + UNREACHABLE: Rule 8 fires first |
| 74 | + Default: drop |
| 75 | + ----------------------------------------------------------------------- *) |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +(* Rule 1: y1 AND z -> fwd(1) *) |
| 81 | +val rule1 = “(arith_and ^a_y1 ^a_z, |
| 82 | + action ("fwd",[1])):single_rule”; |
| 83 | + |
| 84 | +(* Rule 2: y1 AND z AND y2 -> fwd(1) [UNSATISFIABLE: y1 AND y2 = empty] *) |
| 85 | +val rule2 = “(arith_and ^a_y1 (arith_and ^a_z ^a_y2), |
| 86 | + action ("fwd",[1])):single_rule”; |
| 87 | + |
| 88 | +(* Rule 3: y2 AND z -> fwd(1) *) |
| 89 | +val rule3 = “(arith_and ^a_y2 ^a_z, |
| 90 | + action ("fwd",[1])):single_rule”; |
| 91 | + |
| 92 | +(* Rule 4: y1 AND NOT y1 -> fwd(2) [UNSATISFIABLE: always false] *) |
| 93 | +val rule4 = “(arith_and ^a_y1 (arith_not ^a_y1), |
| 94 | + action ("fwd",[2])):single_rule”; |
| 95 | + |
| 96 | +(* Rule 5: y1 -> fwd(1) [comes after Rule 1, partially subsumed] *) |
| 97 | +val rule5 = “(^a_y1, |
| 98 | + action ("fwd",[1])):single_rule”; |
| 99 | + |
| 100 | +(* Rule 6: NOT y1 AND NOT y2 AND z -> fwd(1) middle ports, good TTL *) |
| 101 | +val rule6 = “(arith_and (arith_not ^a_y1) (arith_and (arith_not ^a_y2) ^a_z), |
| 102 | + action ("fwd",[1])):single_rule”; |
| 103 | + |
| 104 | +(* Rule 7: y2 AND z AND NOT z -> fwd(1) [UNSATISFIABLE: z AND NOT z = empty] *) |
| 105 | +val rule7 = “(arith_and ^a_y2 (arith_and ^a_z (arith_not ^a_z)), |
| 106 | + action ("fwd",[1])):single_rule”; |
| 107 | + |
| 108 | +(* Rule 8: NOT z -> fwd(2) bad TTL *) |
| 109 | +val rule8 = “(arith_not ^a_z, |
| 110 | + action ("fwd",[2])):single_rule”; |
| 111 | + |
| 112 | +(* Rule 9: y1 AND NOT z -> fwd(1) [UNREACHABLE: Rule 8 fires first] *) |
| 113 | +val rule9 = “(arith_and ^a_y1 (arith_not ^a_z), |
| 114 | + action ("fwd",[1])):single_rule”; |
| 115 | + |
| 116 | +val arith_policy_default = “(arith_a a_True, action ("drop",[])):single_rule”; |
| 117 | + |
| 118 | +val arith_policy = “[ |
| 119 | + ^rule1; |
| 120 | + ^rule2; |
| 121 | + ^rule3; |
| 122 | + ^rule4; |
| 123 | + ^rule5; |
| 124 | + ^rule6; |
| 125 | + ^rule7; |
| 126 | + ^rule8; |
| 127 | + ^rule9; |
| 128 | + ^arith_policy_default |
| 129 | +]:single_rule list”; |
| 130 | + |
| 131 | +(* ----------------------------------------------------------------------- |
| 132 | + Mapping m |
| 133 | + ----------------------------------------------------------------------- *) |
| 134 | + |
| 135 | +val policy_me = “[ |
| 136 | + ("y1", ^y1); |
| 137 | + ("y2", ^y2); |
| 138 | + ("z", ^z) |
| 139 | +]”; |
| 140 | + |
| 141 | +(* Variable order *) |
| 142 | +val policy_order = “["y1"; "y2"; "z"]”; |
| 143 | + |
| 144 | +(* ----------------------------------------------------------------------- |
| 145 | + Policy minimization |
| 146 | + PolyGram generates a minimized policy and proves it equivalent to the |
| 147 | + bloated input policy above. |
| 148 | + ----------------------------------------------------------------------- *) |
| 149 | + |
| 150 | +val final_thm_res_eq_cake = fwd_proof_gen_eq_cake.gen_eq_policy_and_prove |
| 151 | + (arith_policy, policy_me, test_pd_type, policy_order, |
| 152 | + "policy_min_example"); |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +val _ = export_theory (); |
0 commit comments