Commit f348ec9
committed
fix bNetStr2Bstr: replace strlen with strnlen, cap digit-loop iterations
strlen(buff) scanned until a null terminator regardless of buffer
content, creating an OOB read risk on buffers that are not properly
null-terminated (e.g. data read directly from a network socket).
Two hardening changes, without breaking the API:
1. Cap the digit-parsing loop at 11 iterations (INT_MAX is 10 decimal
digits). Add a post-loop check that ':' was actually found, so a
buffer with no ':' in the first 11 bytes returns NULL immediately
rather than scanning indefinitely.
2. Replace strlen with strnlen(buff, i + 2 + x), limiting the scan to
exactly the number of bytes a valid netstring of the claimed length
requires. An unterminated or truncated buffer is still caught by the
existing bounds check; the scan is now bounded rather than open-ended.
Residual limitation: without a length parameter the caller cannot
provide an absolute bound, so strnlen may still scan up to x bytes
into the data region for large x. A future bNetStr2BstrN(buff, len)
variant would fully eliminate this.1 parent e7d99e5 commit f348ec9
1 file changed
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
310 | 310 | | |
311 | 311 | | |
312 | 312 | | |
313 | | - | |
314 | 313 | | |
315 | | - | |
| 314 | + | |
316 | 315 | | |
317 | 316 | | |
318 | 317 | | |
319 | 318 | | |
320 | 319 | | |
321 | 320 | | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
322 | 326 | | |
323 | 327 | | |
324 | 328 | | |
| |||
0 commit comments