Skip to content

Commit d1332ad

Browse files
committed
fix 32-bit overflow: validate valueLen as uint32 before narrowing to int
1 parent 7691628 commit d1332ad

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

libcoraza/coraza.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,11 @@ func coraza_add_request_headers(t C.coraza_transaction_t, packed *C.char, packed
312312
if off+4 > len(buf) {
313313
return C.CORAZA_ERROR
314314
}
315-
valueLen := int(uint32(buf[off])<<24 | uint32(buf[off+1])<<16 | uint32(buf[off+2])<<8 | uint32(buf[off+3]))
315+
vl := uint32(buf[off])<<24 | uint32(buf[off+1])<<16 | uint32(buf[off+2])<<8 | uint32(buf[off+3])
316+
if vl > uint32(len(buf)) {
317+
return C.CORAZA_ERROR
318+
}
319+
valueLen := int(vl)
316320
off += 4
317321
if off+valueLen > len(buf) {
318322
return C.CORAZA_ERROR
@@ -388,7 +392,11 @@ func coraza_add_response_headers(t C.coraza_transaction_t, packed *C.char, packe
388392
if off+4 > len(buf) {
389393
return C.CORAZA_ERROR
390394
}
391-
valueLen := int(uint32(buf[off])<<24 | uint32(buf[off+1])<<16 | uint32(buf[off+2])<<8 | uint32(buf[off+3]))
395+
vl := uint32(buf[off])<<24 | uint32(buf[off+1])<<16 | uint32(buf[off+2])<<8 | uint32(buf[off+3])
396+
if vl > uint32(len(buf)) {
397+
return C.CORAZA_ERROR
398+
}
399+
valueLen := int(vl)
392400
off += 4
393401
if off+valueLen > len(buf) {
394402
return C.CORAZA_ERROR

0 commit comments

Comments
 (0)