Vulnerable Packet Parser
Models a server that receives packets, computes an allocation size, copies data, and frees on error. Three classic vulnerabilities:
- CWE-190 (Integer Overflow) —
packetCount * elementSizeoverflows, producing an undersized allocation. - CWE-787 (Buffer Overflow) —
copyLength > bufferSizecauses an out-of-bounds write. - CWE-416 (Use-After-Free) — the error handler accesses the freed buffer.
Run#
saple analyze examples/vulnerable_server.splOutput#
4 finding(s):
1. [STRUCTURAL] CRITICAL — Pattern 'integerOverflow' structurally matches behavior (constraint satisfiable)
Pattern: integerOverflow
Trace: recvPacket → computeAllocSize → allocateBuffer
2. [STRUCTURAL] CRITICAL — Pattern 'bufferOverflow' structurally matches behavior (constraint satisfiable)
Pattern: bufferOverflow
Trace: allocateBuffer → computeCopyLen → performCopy
3. [STRUCTURAL] HIGH — Pattern 'useAfterFree' structurally matches behavior (constraint satisfiable)
Pattern: useAfterFree
Trace: freeBuffer → errorHandler
4. [PROPERTY] HIGH — Property 'noIntegerOverflow' violated
Property: noIntegerOverflow
Trace: recvPacket → startParsing → computeAllocSizeThe run exits with status 1 because the report holds a property finding.
The integer-overflow pattern matches recvPacket, computeAllocSize, allocateBuffer on the normal path; the buffer-overflow pattern matches the copy; the use-after-free pattern matches freeBuffer followed by errorHandler on the error path.
Complete model#
examples/vulnerable_server.spl, as shipped:
// Vulnerable Network Packet Parser — CWE Vulnerability Detection
//
// Models a simplified network server that parses incoming packets.
// Contains intentional vulnerabilities for SAPLE to detect:
//
// CWE-190: Integer Overflow
// - Packet length field (16-bit) multiplied by element size
// can overflow, causing undersized buffer allocation.
//
// CWE-787: Out-of-Bounds Write (Buffer Overflow)
// - memcpy uses the original (large) length, writing past
// the (small, overflowed) allocated buffer.
//
// CWE-416: Use-After-Free
// - Error path frees the buffer but continues to a handler
// that reads from the freed pointer.
//
// This models the classic pattern:
// uint16_t count = packet.count; // attacker-controlled
// size_t alloc_size = count * sizeof(Item); // integer overflow!
// char *buf = malloc(alloc_size); // small allocation
// memcpy(buf, packet.data, count * sizeof(Item)); // huge copy → overflow
//
// What the analysis should find: the patterns integerOverflow and
// bufferOverflow (CRITICAL) and useAfterFree (HIGH), and a violation of the
// property noIntegerOverflow.
// Lifecycle of the server while it handles one packet.
type ServerState = { IDLE, PARSING, PROCESSING, ERROR, DONE };
// ─── Actions: packet parsing pipeline ───
// Step 1: Receive packet and read the count field (attacker-controlled input)
transition recvPacket(inputCount : Int) {
guard: serverState == IDLE ;
effect: packetCount := inputCount ;
}
// Step 2: Compute allocation size (count * elementSize)
// BUG (CWE-190): If packetCount is large, this overflows
transition computeAllocSize() {
guard: serverState == PARSING ;
effect: allocSize := packetCount * elementSize ;
}
// Step 3: Allocate buffer with the (possibly overflowed) size
transition allocateBuffer() {
guard: allocSize >= 0 ;
effect: bufferSize := allocSize ;
}
// Step 4: Compute copy length from original count (not the overflowed size)
transition computeCopyLen() {
guard: bufferSize >= 0 ;
effect: copyLength := packetCount * elementSize ;
}
// Step 5: Perform the copy
// BUG (CWE-787): copyLength > bufferSize when overflow occurred
transition performCopy() {
guard: copyLength >= 0 ;
effect: bytesWritten := copyLength ;
}
// Step 6: Process the buffer contents
transition processBuffer() {
guard: serverState == PROCESSING && bufferFreed == false ;
effect: serverState := DONE ;
}
// Step 7: Free the buffer
transition freeBuffer() {
guard: bufferSize > 0 ;
effect: bufferFreed := true ;
}
// Step 8: Error handler that touches the buffer
// BUG (CWE-416): Accesses buffer after it was freed
transition errorHandler() {
guard: serverState == ERROR ;
effect: errorBufRead := bufferSize ;
}
// Transition actions (no data effect, just state machine)
transition startParsing() {
guard: serverState == IDLE ;
effect: serverState := PARSING ;
}
// Parsing succeeded: process the packet.
transition startProcessing() {
guard: serverState == PARSING ;
effect: serverState := PROCESSING ;
}
// Parsing failed: take the error path.
transition triggerError() {
guard: serverState == PARSING ;
effect: serverState := ERROR ;
}
// Processing is complete.
transition finishProcessing() {
guard: serverState == PROCESSING ;
effect: serverState := DONE ;
}
// ─── Agent ───
agent PacketParser() {
attributes {
state : ServerState = IDLE ;
}
process {
// Normal path: receive → parse → allocate → copy → process → done
// Error path: receive → parse → error → free → errorHandler (use-after-free!)
Start = recvPacket(inputCount) . startParsing() . Parse ;
Parse = computeAllocSize() . allocateBuffer() .
computeCopyLen() . performCopy() .
startProcessing() . Process
+ triggerError() . ErrorPath ;
Process = processBuffer() . Cleanup ;
Cleanup = freeBuffer() . done ;
ErrorPath = freeBuffer() . errorHandler() . done ;
}
}
// ─── Environment ───
schema ServerEnv {
attributes {
serverState : ServerState = IDLE ;
packetCount : Int = 0 ;
elementSize : Int = 64 ;
allocSize : Int = 0 ;
bufferSize : Int = 0 ;
copyLength : Int = 0 ;
bytesWritten : Int = 0 ;
bufferFreed : Bool = false ;
errorBufRead : Int = 0 ;
maxUint16 : Int = 65535 ;
maxAllocSize : Int = 4194304 ;
}
invariants {
elementSize > 0 ;
}
}
// ─── Vulnerability Patterns ───
// CWE-190: Integer Overflow in allocation size computation
// Signature: compute alloc from attacker input, then allocate with result
pattern integerOverflow {
signature {
recvPacket(inputCount) .
computeAllocSize() .
allocateBuffer() . done
}
constraints {
packetCount * elementSize > maxAllocSize &&
allocSize < packetCount * elementSize
}
severity: CRITICAL ;
}
// CWE-787: Out-of-Bounds Write (buffer overflow via copy)
// Signature: allocate buffer then copy more than allocated
pattern bufferOverflow {
signature {
allocateBuffer() .
computeCopyLen() .
performCopy() . done
}
constraints { copyLength > bufferSize }
severity: CRITICAL ;
}
// CWE-416: Use-After-Free
// Signature: free buffer then access it
pattern useAfterFree {
signature {
freeBuffer() .
errorHandler() . done
}
constraints { bufferFreed == true }
severity: HIGH ;
}
// ─── System ───
system VulnerableServer {
schema: ServerEnv ;
agents {
PacketParser() * 1 ;
}
init {
serverState == IDLE &&
packetCount == 0 &&
bufferFreed == false ;
}
properties {
// SAFETY: Written bytes should never exceed buffer size
noBufferOverflow : bytesWritten <= bufferSize ;
// SAFETY: Buffer should not be accessed after free
noUseAfterFree : bufferFreed == false || errorBufRead == 0 ;
// SAFETY: Allocation size should not overflow
noIntegerOverflow : allocSize >= 0 ;
}
}