Intel x86-64 Model

The x86-64 front end, --arch x86-64, reads a static Intel-syntax assembly listing of one function and turns it into a BV<64> SPL model with bit-vector instruction semantics. saple analyze then matches the x86 pattern library against every path of the function and asks Z3 whether each pattern's constraint can hold on that path.

Always pass --arch x86-64
Only the .asm extension selects the x86-64 front end on its own; a listing saved as .txt or .s does not. Give --arch x86-64 to both saple translate and saple analyze. When the front end cannot run inside saple analyze (a .txt listing without --arch, an unknown --arch, a listing the front end rejects), analyze prints the error on standard error but still reports No vulnerabilities detected. ([] in JSON) and exits 0.

Input: a listing#

Instructions get synthetic addresses 0x1000, 0x1010, … (one per instruction), which appear in the comments of the model.

What the model contains#

Example: CWE-190 before malloc#

The listing demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt multiplies a 32-bit argument by 64 and passes the product to malloc. A large argument wraps the 32-bit product, so the allocation is too small.

x86-64 listing
push rbp
mov rbp, rsp
sub rsp, 32
mov dword [rbp-4], edi
mov eax, dword [rbp-4]
imul eax, eax, 0x40
mov edi, eax
call malloc
mov qword [rbp-16], rax
mov rax, qword [rbp-16]
mov rsp, rbp
pop rbp
ret

1. Translate#

Shell
saple translate demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt \
  --arch x86-64 -o imul.spl
Output
warning: static listing: the initial registers are symbolic (no initial values were observed); the analysis assumes a SysV entry stack pointer
Wrote SPL to imul.spl
Translated: 1 agents, 1 behaviors, 1 warnings

The process of the generated model, one action per instruction (the transitions and the schema are left out here):

SPL
agent program() {
  attributes { }
  process {
    B0 =
      // step 0 @ 0x1000: push rbp
      push(RBP) .
      // step 1 @ 0x1010: mov rbp, rsp
      mov(RBP, RSP) .
      // step 2 @ 0x1020: sub rsp, 32
      sub_op(RSP, 32) .
      // step 3 @ 0x1030: mov dword [rbp-4], edi
      mov_st((RBP + 0xfffffffffffffffc:BV<64>), RDI, 32) .
      // step 4 @ 0x1040: mov eax, dword [rbp-4]
      mov_mem(RAX, (RBP + 0xfffffffffffffffc:BV<64>), 32) .
      // step 5 @ 0x1050: imul eax, eax, 0x40
      imul32(RAX, RAX, 64) .
      // step 6 @ 0x1060: mov edi, eax
      mov32(RDI, RAX) .
      // step 7 @ 0x1070: call malloc
      call_malloc(RDI) .
      // step 8 @ 0x1080: mov qword [rbp-16], rax
      mov_st((RBP + 0xfffffffffffffff0:BV<64>), RAX, 64) .
      // step 9 @ 0x1090: mov rax, qword [rbp-16]
      mov_mem(RAX, (RBP + 0xfffffffffffffff0:BV<64>), 64) .
      // step 10 @ 0x10a0: mov rsp, rbp
      mov(RSP, RBP) .
      // step 11 @ 0x10b0: pop rbp
      pop(RBP) .
      // step 12 @ 0x10c0: ret
      done ;
  }
}

and the semantics of the multiply, as emitted:

SPL
transition imul32(dst_reg, src_reg, multiplier) {
    guard: true ;
    effect: let full = (sign_extend(extract<31,0>(src_reg), 32) * sign_extend(extract<31,0>(multiplier), 32)) ;
        let result = extract<31,0>(full) ;
        let ovf = ~(sign_extend(result, 32) == full) ;
        dst_reg := zero_extend(result, 32) ;
        RFLAGS.CF := ovf ;
        RFLAGS.OF := ovf ;
}

2. Analyze#

Run from the repository root, so that --pattern-set v0.6 finds patterns/x86-cwe-v0.6.spl:

Shell
saple analyze imul.spl --pattern-set=v0.6

Output (the banner goes to standard error):

Output
  Assumption: x86-64 SysV entry: RSP@0 is a user-space stack pointer, 0x10000 <= RSP@0 < 0x800000000000 and RSP@0 % 16 == 8

  2 finding(s):

  1. [STRUCTURAL] CRITICAL — Pattern 'cwe_190_imul32_wraparound_before_malloc_v6' structurally matches behavior (constraint undecided: width mismatch: BV<64> against BV<96>)
     Pattern: cwe_190_imul32_wraparound_before_malloc_v6
     Trace: imul32 → call_malloc

  2. [STRUCTURAL] CRITICAL — Pattern 'cwe_190_imul32_wraparound_before_malloc_bv' structurally matches behavior (constraint satisfiable)
     Pattern: cwe_190_imul32_wraparound_before_malloc_bv
     Trace: imul32 → call_malloc

The run exits with status 1. Finding 1 comes from the library's older _v6 pattern, written for an integer schema: its constraint does not type on a bit-vector model, so it is reported undecided with a width mismatch. Such a finding says nothing about the program and does not affect the exit status. The _bv pattern is the one that applies.

Analyzing the listing directly#

saple analyze also accepts the listing itself and translates it in memory:

Shell
saple analyze demo/corpus/cwe-190/vulnerable/imul32_overflow_before_malloc.txt \
  --arch x86-64 --pattern-set=v0.6

The findings are the same, plus the front end's warning about the symbolic initial registers, reported as a MEDIUM finding without a pattern.

A guarded variant#

demo/corpus/cwe-190/safe/checked_before_calloc.txt compares the argument with 0x100000 and jumps past the multiply (ja overflow_handler) when it is larger. On the only path that reaches calloc the guard bounds the argument, the product cannot wrap, and the _bv pattern's constraint is unsatisfiable: the report holds no _bv finding and the run exits with status 0.

Limits#

↑ ↓ to moveEnter to openEsc to close