Patterns
A pattern is a triple: a behavior signature, a constraint and a severity. The analyzer looks for the signature in the model's behavior and then asks the SMT solver whether the constraint can hold there. Patterns detect vulnerabilities (CWE classes) but can describe any behavior of interest.
pattern name {
signature {
action1(binder1, binder2) .
action2(binder3) .
done
}
constraints { <expression over the binders> }
severity: LEVEL ;
}All three parts are required, in this order. The severity is one of CRITICAL, HIGH, MEDIUM, LOW.
Signatures and binders#
A signature is a prefix chain of actions ending in done. Each argument position of an action is one of:
- An identifier — a binder. It binds the value of that argument at the matched step and can be used in the constraint.
_is an ordinary identifier: it binds like any other name. There is no wildcard syntax; an unused binder simply is not mentioned in the constraint. - A literal — a literal pattern. The step matches only when the argument equals the literal:
imul(src_reg, multiplier, imm, 32, 2)only matches the 32-bit two-operand form.
Keep binder names distinct within a signature and state equalities between operands in the constraint (c == a).
f(0x40:BV<64>)) never matches today. Bind the argument and constrain it instead: f(k) with k == 0x40:BV<64>.
Constraints#
The constraint is a Boolean expression over the binders, the model's attributes and enumeration members. On bit-vector models it may use the full bit-vector vocabulary (extract, zero_extend, *u, <u, …). A register binder typed with an enumeration can be compared to a member: reg == RSP. A constraint of true makes the pattern purely structural.
How matching works#
Level 1 (structural). The actions of the signature are matched, in order, against the actions along each behavior path of the model, as a subsequence: other actions may occur between two matched ones. Actions match by transition name; literal arguments must agree. Each signature action binds to the first matching action after the previous one, and later occurrences are not tried: a switchTo(a) . switchTo(b) signature sees only the first two switches on a path, so with the constraint a == RED && b == GREEN the path RED . YELLOW . GREEN gives no structural finding. (On a hand-written model, the concrete walk of the model may still report another combination as a [CONCRETE] finding.)
Level 2 (semantic). For each structural match the binders are tied to the values at the matched steps, and the solver checks the constraint. Unsatisfiable: no finding. Satisfiable: a finding. Undecidable within the budget, or depending on values the analysis does not know: an undecided finding (verdict UNKNOWN), never silently dropped. What the front ends add on top — data flow along the path, guards, the trace verifier's verdicts — is described on the front-end pages.
Examples#
// CWE-190: a 32-bit multiply that wraps, and the wrapped value is the malloc size
// (from patterns/x86-cwe-v0.6.spl)
pattern cwe_190_imul32_wraparound_before_malloc_bv {
signature {
imul32(dst_reg, src_reg, multiplier) .
call_malloc(size_arg) .
done
}
constraints {
extract<63, 32>(zero_extend(extract<31, 0>(src_reg), 32) *u
zero_extend(extract<31, 0>(multiplier), 32)) != 0
&&
size_arg == zero_extend(extract<31, 0>(src_reg *u multiplier), 32)
}
severity: CRITICAL ;
}
// A flag observation on an AArch64 trace (from patterns/aarch64-trace-v0.1.spl)
pattern cwe_190_adds_signed_overflow_w {
signature {
adds_w(dst, a, b) .
done
}
constraints {
V == 1
}
severity: LOW ;
}
// A hand-written model: two writes to shared data from different cores
pattern dataRace {
signature {
cpuWriteShared(core1, val1) .
cpuWriteShared(core2, val2) .
done
}
constraints { core1 != core2 }
severity: CRITICAL ;
}Using patterns#
- Put
patterndeclarations in the model file itself. - Load a pattern file for one run:
saple analyze model.spl --pattern-file my-patterns.spl(repeatable). - Use a shipped library: see Pattern Libraries.
- Build chain patterns from existing ones with
saple compose: see CLI Tools.