Skip to content

Commit 42e1aac

Browse files
committed
Fix DNS parsing CBMC proof
1 parent f8d6f23 commit 42e1aac

2 files changed

Lines changed: 85 additions & 31 deletions

File tree

‎test/cbmc/proofs/DNS/parseDNSAnswer/Makefile.json‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
"ENTRY": "parseDNSAnswer",
33

44
################################################################
5-
# Modeled length of the attacker-controlled DNS payload remaining to be
6-
# parsed. Kept small to bound the name-parse / answer-record loops, but the
7-
# buffer is allocated to EXACTLY this length with nondeterministic contents so
8-
# any over-read is a real defect. 32 bytes is enough to hold a full AAAA
9-
# answer record (name ref 2 + fixed header 10 + IPv6 address 16 = 28).
5+
# Modeled length of the attacker-controlled DNS message. Kept small to bound
6+
# the name-parse / answer-record loops, but the buffer is allocated to EXACTLY
7+
# this length with nondeterministic contents so any over-read is a real defect.
8+
# 32 bytes is enough to hold a full AAAA answer record (name ref 2 + fixed
9+
# header 10 + IPv6 address 16 = 28).
1010
"NETWORK_BUFFER_SIZE": 32,
1111

1212
################################################################
@@ -18,18 +18,47 @@
1818
"ANSWERLOOP_UNWIND": "3",
1919

2020
################################################################
21-
# DNS_SkipNameField.0 : the name-walk while loop (linked in for real from the
22-
# same translation unit). Each iteration consumes >= 2 bytes, so
23-
# (NETWORK_BUFFER_SIZE + 1) / 2 + 1 iterations soundly cover it.
24-
"SKIPLOOP": "DNS_SkipNameField.0",
25-
"SKIPLOOP_UNWIND": "17",
21+
# Size of ParseSet_t::pcName, and hence of every name buffer the answer-name
22+
# validation decodes into or copies around. This is a proof parameter, not a
23+
# property of the code: the name-decode loops are all bounded by the
24+
# destination size, so keeping it small keeps the proof tractable while still
25+
# exercising every bound check against a NETWORK_BUFFER_SIZE-byte packet.
26+
"DNS_CACHE_NAME_LENGTH": 8,
27+
28+
################################################################
29+
# Every remaining loop walks a DNS name into (or out of) one of those
30+
# DNS_CACHE_NAME_LENGTH-byte buffers, and each iteration consumes at least one
31+
# byte of the destination, so DNS_CACHE_NAME_LENGTH + 1 iterations soundly
32+
# cover them all: the unwinding assertion needs one iteration more than the
33+
# loop can actually run.
34+
#
35+
# DNS_ReadNameField.1 : the outer "walk over the string" while loop. Each
36+
# iteration consumes a length byte plus at least one label byte.
37+
# DNS_ReadNameField.0 : the inner "while( uxCount-- != 0U )" label copy, only
38+
# entered after the function has checked uxNameLen + uxCount < uxDestLen,
39+
# so uxCount < DNS_CACHE_NAME_LENGTH.
40+
# strncpy / strlen / strcasecmp : operate on NUL-terminated names held in
41+
# DNS_CACHE_NAME_LENGTH-byte buffers. Both the plain and the fortified
42+
# (__builtin___*_chk) spellings are bounded because which one is emitted is
43+
# platform dependent: the CI Linux toolchain produces strncpy, macOS
44+
# produces __builtin___strncpy_chk.
45+
#
46+
# DNS_SkipNameField is deliberately absent: with the name-validating
47+
# configuration below (ipconfigUSE_LLMNR / ipconfigUSE_MDNS set),
48+
# parseDNSAnswer decodes the answer's owner name with DNS_ReadNameField()
49+
# instead of skipping over it, so that loop is unreachable from this entry
50+
# point.
51+
"NAMELOOP_UNWIND": "9",
2652

2753
################################################################
2854

2955
"CBMCFLAGS":
3056
[
3157
"--unwind 1",
32-
"--unwindset {ANSWERLOOP}:{ANSWERLOOP_UNWIND},{SKIPLOOP}:{SKIPLOOP_UNWIND}"
58+
"--unwindset {ANSWERLOOP}:{ANSWERLOOP_UNWIND}",
59+
"--unwindset DNS_ReadNameField.0:{NAMELOOP_UNWIND},DNS_ReadNameField.1:{NAMELOOP_UNWIND}",
60+
"--unwindset strlen.0:{NAMELOOP_UNWIND},strcasecmp.0:{NAMELOOP_UNWIND}",
61+
"--unwindset strncpy.0:{NAMELOOP_UNWIND},__builtin___strncpy_chk.0:{NAMELOOP_UNWIND}"
3362
],
3463

3564
"OBJS":
@@ -48,7 +77,7 @@
4877
"ipconfigUSE_IPv6=1",
4978
"ipconfigUSE_DNS_CACHE=0",
5079
"ipconfigDNS_USE_CALLBACKS=0",
51-
"ipconfigDNS_CACHE_NAME_LENGTH=16",
80+
"ipconfigDNS_CACHE_NAME_LENGTH={DNS_CACHE_NAME_LENGTH}",
5281
"ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY=4"
5382
]
5483
}

‎test/cbmc/proofs/DNS/parseDNSAnswer/parseDNSAnswer_harness.c‎

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
*
44
* parseDNSAnswer() walks the "answer" records of a DNS response. Every byte
55
* it reads originates from an attacker-controlled UDP payload, so this proof
6-
* models the buffer with fully nondeterministic contents and allocates it to
7-
* EXACTLY the modeled remaining length: any read past pxSet->pucByte +
8-
* pxSet->uxSourceBytesRemaining is therefore a genuine out-of-bounds bug, not
9-
* a proof artifact.
6+
* models the message with fully nondeterministic contents and allocates it to
7+
* EXACTLY the modeled payload length: any read outside
8+
* pxSet->pucUDPPayloadBuffer[ 0 .. pxSet->uxBufferLength ) is therefore a
9+
* genuine out-of-bounds bug, not a proof artifact. pxSet->pucByte points at a
10+
* nondeterministic offset within that message, which is what makes the
11+
* compression-pointer path of the answer-name validation reachable.
12+
*
13+
* The unparsed tail deliberately is NOT modeled as an object of its own: the
14+
* function resolves compression pointers backwards against the start of the
15+
* message, so every byte of the message -- not just the tail -- is legal for it
16+
* to read, exactly as it is for the real caller in DNS_ParseDNSReply().
1017
*/
1118

1219
/* Standard includes. */
@@ -92,27 +99,45 @@ void harness()
9299
struct freertos_addrinfo * pxAddressInfo = NULL;
93100
struct freertos_addrinfo ** ppxAddressInfo;
94101
size_t uxBytesRead;
95-
size_t uxRemaining;
96-
97-
/* Bound the modeled remaining length just enough to keep the name-parse
98-
* and answer-record loops finite (mirrors the small payload bounds used by
99-
* the DNS_ParseDNSReply proof). It is otherwise nondeterministic so the
102+
size_t uxPayloadLength;
103+
size_t uxParsed;
104+
105+
/* The modeled message has to be allocatable as a single CBMC object.
106+
* Checked against the proof parameter rather than assumed of the modeled
107+
* length, so that raising NETWORK_BUFFER_SIZE past the limit fails the
108+
* proof instead of silently shrinking what it covers. */
109+
__CPROVER_assert( NETWORK_BUFFER_SIZE < CBMC_MAX_OBJECT_SIZE,
110+
"NETWORK_BUFFER_SIZE < CBMC_MAX_OBJECT_SIZE" );
111+
112+
/* Bound the modeled payload length just enough to keep the name-parse and
113+
* answer-record loops finite (mirrors the small payload bounds used by the
114+
* DNS_ParseDNSReply proof). It is otherwise nondeterministic so the
100115
* parser's length arithmetic is fully exercised. */
101-
__CPROVER_assume( uxRemaining <= NETWORK_BUFFER_SIZE );
102-
__CPROVER_assume( uxRemaining < CBMC_MAX_OBJECT_SIZE );
116+
__CPROVER_assume( uxPayloadLength <= NETWORK_BUFFER_SIZE );
117+
118+
/* Number of payload bytes the caller has already consumed, i.e. how far
119+
* into the message pucByte points. Nondeterministic so that both the
120+
* "answer name is inline" and "answer name is a compression pointer back
121+
* into the question section" cases are reachable. */
122+
__CPROVER_assume( uxParsed <= uxPayloadLength );
103123

104124
/* Start from a clean ParseSet_t, then set only the fields parseDNSAnswer
105125
* reads. memset mirrors DNS_ParseDNSReply, which zero-inits the struct. */
106126
( void ) memset( &xSet, 0, sizeof( xSet ) );
107127

108-
/* Allocate the packet buffer to EXACTLY the modeled remaining length. */
109-
xSet.pucByte = safeMalloc( uxRemaining );
110-
__CPROVER_assume( xSet.pucByte != NULL );
111-
112-
/* Establish the caller's invariant: pucByte points at the first
113-
* unparsed byte and uxSourceBytesRemaining is the count of bytes that are
114-
* legal to read from there onward. */
115-
xSet.uxSourceBytesRemaining = uxRemaining;
128+
/* Allocate the DNS message to EXACTLY the modeled payload length, so any
129+
* read outside it is a genuine out-of-bounds access. parseDNSAnswer
130+
* resolves compression pointers relative to pucUDPPayloadBuffer, so the
131+
* whole message must be modeled, not just the unparsed tail. */
132+
xSet.pucUDPPayloadBuffer = safeMalloc( uxPayloadLength );
133+
__CPROVER_assume( xSet.pucUDPPayloadBuffer != NULL );
134+
xSet.uxBufferLength = uxPayloadLength;
135+
136+
/* Establish the caller's invariant: pucByte points at the first unparsed
137+
* byte within the message and uxSourceBytesRemaining is the count of bytes
138+
* that are legal to read from there onward. */
139+
xSet.pucByte = &( xSet.pucUDPPayloadBuffer[ uxParsed ] );
140+
xSet.uxSourceBytesRemaining = uxPayloadLength - uxParsed;
116141

117142
/* Number of answer records claimed by the (attacker-controlled) header. */
118143
xSet.usAnswers = nondet_uint16();

0 commit comments

Comments
 (0)