|
3 | 3 | * |
4 | 4 | * parseDNSAnswer() walks the "answer" records of a DNS response. Every byte |
5 | 5 | * 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(). |
10 | 17 | */ |
11 | 18 |
|
12 | 19 | /* Standard includes. */ |
@@ -92,27 +99,45 @@ void harness() |
92 | 99 | struct freertos_addrinfo * pxAddressInfo = NULL; |
93 | 100 | struct freertos_addrinfo ** ppxAddressInfo; |
94 | 101 | 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 |
100 | 115 | * 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 ); |
103 | 123 |
|
104 | 124 | /* Start from a clean ParseSet_t, then set only the fields parseDNSAnswer |
105 | 125 | * reads. memset mirrors DNS_ParseDNSReply, which zero-inits the struct. */ |
106 | 126 | ( void ) memset( &xSet, 0, sizeof( xSet ) ); |
107 | 127 |
|
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; |
116 | 141 |
|
117 | 142 | /* Number of answer records claimed by the (attacker-controlled) header. */ |
118 | 143 | xSet.usAnswers = nondet_uint16(); |
|
0 commit comments