Debian Codegraph @

test-data/pkg/writer/rawmmdb.go

raw
1 package writer
2
3 // Raw MMDB binary encoding helpers.
4 //
5 // This is a Go port of libmaxminddb's t/mmdb_test_writer.h, extended with
6 // large-size (case-31) encoding and complete database builders for crafting
7 // intentionally malformed MMDB files that cannot be created through mmdbwriter.
8
9 import "encoding/binary"
10
11 const (
12 metadataMarker = "\xab\xcd\xefMaxMind.com"
13 dataSeparatorSize = 16
14 )
15
16 var (
17 metadataKeysStandard = []string{
18 "binary_format_major_version",
19 "binary_format_minor_version",
20 "build_epoch",
21 "database_type",
22 "description",
23 "ip_version",
24 "languages",
25 "node_count",
26 "record_size",
27 }
28 metadataKeysEmptyMapLast = []string{
29 "binary_format_major_version",
30 "binary_format_minor_version",
31 "build_epoch",
32 "database_type",
33 "ip_version",
34 "languages",
35 "node_count",
36 "record_size",
37 "description",
38 }
39 metadataKeysEmptyArrayLast = []string{
40 "binary_format_major_version",
41 "binary_format_minor_version",
42 "build_epoch",
43 "database_type",
44 "description",
45 "ip_version",
46 "node_count",
47 "record_size",
48 "languages",
49 }
50 )
51
52 // writeMap writes a map control byte (type 7) for sizes <= 28.
53 func writeMap(buf []byte, size int) int {
54 buf[0] = (7 << 5) | byte(size&0x1f)
55 return 1
56 }
57
58 // writeString writes a string value (type 2).
59 func writeString(buf []byte, s string) int {
60 buf[0] = (2 << 5) | byte(len(s)&0x1f)
61 copy(buf[1:], s)
62 return 1 + len(s)
63 }
64
65 // writeUint16 writes a uint16 value (type 5, 2 bytes).
66 func writeUint16(buf []byte, v uint16) int {
67 buf[0] = (5 << 5) | 2
68 binary.BigEndian.PutUint16(buf[1:], v)
69 return 3
70 }
71
72 // writeUint32 writes a uint32 value (type 6, 4 bytes).
73 func writeUint32(buf []byte, v uint32) int {
74 buf[0] = (6 << 5) | 4
75 binary.BigEndian.PutUint32(buf[1:], v)
76 return 5
77 }
78
79 // writeUint64 writes a uint64 value (extended type 9, 8 bytes).
80 func writeUint64(buf []byte, v uint64) int {
81 buf[0] = (0 << 5) | 8
82 buf[1] = 2 // extended type: 7 + 2 = 9 (uint64)
83 binary.BigEndian.PutUint64(buf[2:], v)
84 return 10
85 }
86
87 // writeMetaKey writes a metadata key as a string.
88 func writeMetaKey(buf []byte, key string) int {
89 return writeString(buf, key)
90 }
91
92 // writeLargeArray writes an array control byte (extended type 11) with
93 // case-31 size encoding for sizes > 65820.
94 func writeLargeArray(buf []byte, size uint32) int {
95 adjusted := size - 65821
96 buf[0] = (0 << 5) | 31 // extended type, size = case 31
97 buf[1] = 4 // extended type: 7 + 4 = 11 (array)
98 buf[2] = byte((adjusted >> 16) & 0xFF)
99 buf[3] = byte((adjusted >> 8) & 0xFF)
100 buf[4] = byte(adjusted & 0xFF)
101 return 5
102 }
103
104 // writeLargeMap writes a map control byte (type 7) with case-31 size
105 // encoding for sizes > 65820.
106 func writeLargeMap(buf []byte, size uint32) int {
107 adjusted := size - 65821
108 buf[0] = (7 << 5) | 31 // type 7 (map), size = case 31
109 buf[1] = byte((adjusted >> 16) & 0xFF)
110 buf[2] = byte((adjusted >> 8) & 0xFF)
111 buf[3] = byte(adjusted & 0xFF)
112 return 4
113 }
114
115 // writeEmptyArray writes a zero-length array (extended type 11).
116 func writeEmptyArray(buf []byte) int {
117 buf[0] = 0 // extended type, size 0
118 buf[1] = 4 // 7 + 4 = 11 (array)
119 return 2
120 }
121
122 // writeSearchTree writes a 1-node search tree with 24-bit records,
123 // both pointing to the data section.
124 func writeSearchTree(buf []byte, recordValue uint32) int {
125 buf[0] = byte((recordValue >> 16) & 0xFF)
126 buf[1] = byte((recordValue >> 8) & 0xFF)
127 buf[2] = byte(recordValue & 0xFF)
128 buf[3] = byte((recordValue >> 16) & 0xFF)
129 buf[4] = byte((recordValue >> 8) & 0xFF)
130 buf[5] = byte(recordValue & 0xFF)
131 return 6
132 }
133
134 func writeMetadataBlockWithKeyOrder(
135 buf []byte,
136 nodeCount uint32,
137 buildEpoch uint64,
138 keys []string,
139 ) int {
140 pos := 0
141
142 copy(buf[pos:], metadataMarker)
143 pos += len(metadataMarker)
144
145 pos += writeMap(buf[pos:], len(keys))
146
147 valueWriters := map[string]func([]byte) int{
148 "binary_format_major_version": func(b []byte) int { return writeUint16(b, 2) },
149 "binary_format_minor_version": func(b []byte) int { return writeUint16(b, 0) },
150 "build_epoch": func(b []byte) int { return writeUint64(b, buildEpoch) },
151 "database_type": func(b []byte) int { return writeString(b, "Test") },
152 "description": func(b []byte) int { return writeMap(b, 0) },
153 "ip_version": func(b []byte) int { return writeUint16(b, 4) },
154 "languages": writeEmptyArray,
155 "node_count": func(b []byte) int { return writeUint32(b, nodeCount) },
156 "record_size": func(b []byte) int { return writeUint16(b, 24) },
157 }
158
159 for _, key := range keys {
160 valueWriter, ok := valueWriters[key]
161 if !ok {
162 panic("unknown metadata key: " + key)
163 }
164 pos += writeMetaKey(buf[pos:], key)
165 pos += valueWriter(buf[pos:])
166 }
167
168 return pos
169 }
170
171 // writeMetadataBlock writes the metadata marker followed by a standard
172 // metadata map with the given parameters.
173 func writeMetadataBlock(buf []byte, nodeCount uint32, buildEpoch uint64) int {
174 return writeMetadataBlockWithKeyOrder(buf, nodeCount, buildEpoch, metadataKeysStandard)
175 }
176
177 func buildSimpleDB(metadataWriter func([]byte, uint32, uint64) int) []byte {
178 const nodeCount = 1
179 const recordValue = nodeCount + 16
180 const buildEpoch = 1_000_000_000
181
182 buf := make([]byte, 1024)
183 pos := 0
184
185 pos += writeSearchTree(buf[pos:], recordValue)
186
187 // 16-byte null separator
188 pos += dataSeparatorSize
189
190 // Data: a simple map with one string entry
191 pos += writeMap(buf[pos:], 1)
192 pos += writeString(buf[pos:], "ip")
193 pos += writeString(buf[pos:], "test")
194
195 pos += metadataWriter(buf[pos:], nodeCount, buildEpoch)
196
197 return buf[:pos]
198 }
199
200 // buildOversizedArrayDB creates a complete MMDB with an array claiming
201 // 1,000,000 elements but containing only 2 actual entries.
202 func buildOversizedArrayDB() []byte {
203 const nodeCount = 1
204 const recordValue = nodeCount + 16
205
206 buf := make([]byte, 1024)
207 pos := 0
208
209 pos += writeSearchTree(buf[pos:], recordValue)
210
211 // 16-byte null separator
212 pos += dataSeparatorSize
213
214 // Data: array claiming 1M elements, only 2 strings present
215 pos += writeLargeArray(buf[pos:], 1_000_000)
216 pos += writeString(buf[pos:], "x")
217 pos += writeString(buf[pos:], "y")
218
219 pos += writeMetadataBlock(buf[pos:], nodeCount, 1_000_000_000)
220
221 return buf[:pos]
222 }
223
224 // buildOversizedMapDB creates a complete MMDB with a map claiming
225 // 1,000,000 entries but containing only 1 key-value pair.
226 func buildOversizedMapDB() []byte {
227 const nodeCount = 1
228 const recordValue = nodeCount + 16
229
230 buf := make([]byte, 1024)
231 pos := 0
232
233 pos += writeSearchTree(buf[pos:], recordValue)
234
235 // 16-byte null separator
236 pos += dataSeparatorSize
237
238 // Data: map claiming 1M entries, only 1 k/v pair present
239 pos += writeLargeMap(buf[pos:], 1_000_000)
240 pos += writeString(buf[pos:], "k")
241 pos += writeString(buf[pos:], "v")
242
243 pos += writeMetadataBlock(buf[pos:], nodeCount, 1_000_000_000)
244
245 return buf[:pos]
246 }
247
248 // buildUint64MaxEpochDB creates a complete MMDB with build_epoch set to
249 // UINT64_MAX (18446744073709551615). The database is structurally valid
250 // but the extreme epoch value can cause overflow in time conversions.
251 func buildUint64MaxEpochDB() []byte {
252 return buildSimpleDB(func(buf []byte, nodeCount uint32, _ uint64) int {
253 return writeMetadataBlock(buf, nodeCount, ^uint64(0))
254 })
255 }
256
257 // writeMetadataBlockEmptyMapLast writes a metadata block where the last field
258 // is "description" (an empty map). This triggers the off-by-one bug where
259 // offset_to_next == data_section_size for a 0-length container.
260 func writeMetadataBlockEmptyMapLast(buf []byte, nodeCount uint32, buildEpoch uint64) int {
261 return writeMetadataBlockWithKeyOrder(
262 buf,
263 nodeCount,
264 buildEpoch,
265 metadataKeysEmptyMapLast,
266 )
267 }
268
269 // writeMetadataBlockEmptyArrayLast writes a metadata block where the last
270 // field is "languages" (an empty array).
271 func writeMetadataBlockEmptyArrayLast(buf []byte, nodeCount uint32, buildEpoch uint64) int {
272 return writeMetadataBlockWithKeyOrder(
273 buf,
274 nodeCount,
275 buildEpoch,
276 metadataKeysEmptyArrayLast,
277 )
278 }
279
280 // buildEmptyMapLastInMetadataDB creates a valid MMDB where the metadata
281 // map's last field is "description" (an empty map {}). This reproduces the
282 // off-by-one bug in get_entry_data_list() where offset == data_section_size
283 // is incorrectly rejected for 0-length containers.
284 func buildEmptyMapLastInMetadataDB() []byte {
285 return buildSimpleDB(writeMetadataBlockEmptyMapLast)
286 }
287
288 // buildEmptyArrayLastInMetadataDB creates a valid MMDB where the metadata
289 // map's last field is "languages" (an empty array []). Tests the array
290 // validation path of the same off-by-one bug.
291 func buildEmptyArrayLastInMetadataDB() []byte {
292 return buildSimpleDB(writeMetadataBlockEmptyArrayLast)
293 }
294
295 // buildCorruptSearchTreeDB creates a complete MMDB where the metadata claims
296 // node_count = 100 but the actual search tree has only 1 node worth of real
297 // data (6 bytes for 24-bit records). The file is padded so MMDB_open
298 // succeeds (it validates file_size >= search_tree_size + separator), but
299 // MMDB_read_node with a node_number like 50 reads zeroed memory and should
300 // return MMDB_CORRUPT_SEARCH_TREE_ERROR.
301 func buildCorruptSearchTreeDB() []byte {
302 const fakeNodeCount = 100
303 const recordSize = 24
304 // fakeNodeCount * (recordSize/4) = bytes in the fake search tree
305 const fakeSearchTreeSize = fakeNodeCount * (recordSize / 4)
306 const recordValue = fakeNodeCount + 16
307
308 // Allocate enough for the fake tree + separator + data + metadata
309 buf := make([]byte, fakeSearchTreeSize+dataSeparatorSize+1024)
310 pos := 0
311
312 // Write 1 real node at position 0; rest stays zeroed
313 writeSearchTree(buf[pos:], recordValue)
314 pos = fakeSearchTreeSize // skip to end of the fake tree
315
316 // 16-byte null separator
317 pos += dataSeparatorSize
318
319 // Data: a simple map
320 pos += writeMap(buf[pos:], 1)
321 pos += writeString(buf[pos:], "ip")
322 pos += writeString(buf[pos:], "test")
323
324 // Metadata claims 100 nodes
325 pos += writeMetadataBlock(buf[pos:], fakeNodeCount, 1_000_000_000)
326
327 return buf[:pos]
328 }