mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 02:27:26 +01:00
fix: validate H2C data ranges without integer overflow
This commit is contained in:
1 parent
d4acf0a24f
commit
cd03b059c8
3 files changed
+56
-2
No files matched your search
@@ -0,0 +1,46 @@
|
||||
package nvme
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Exercise the real TCP framing and receive path, without a socket or backend.
|
||||
func TestRecvH2CDataRangeBounds(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
offset uint32
|
||||
payload []byte
|
||||
reject bool
|
||||
}{
|
||||
{"uint32-wrap", math.MaxUint32, []byte{1, 2}, true},
|
||||
{"exact-bound", 0, []byte{1, 2, 3, 4}, false},
|
||||
{"bound-plus-one", 1, []byte{1, 2, 3, 4}, true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var stream bytes.Buffer
|
||||
w := NewWriter(&stream)
|
||||
h := H2CDataHeader{DATAO: tc.offset, DATAL: uint32(len(tc.payload))}
|
||||
if err := w.SendWithData(pduH2CData, 0, &h, h2cDataHdrSize, tc.payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := &Session{wire: newTCPPDUTransport(&stream)}
|
||||
// A panic is a test failure, never an acceptable malformed-input outcome.
|
||||
defer func() {
|
||||
if p := recover(); p != nil {
|
||||
t.Fatalf("invalid range panicked instead of refusing: %v", p)
|
||||
}
|
||||
}()
|
||||
data, err := s.recvH2CData(4)
|
||||
if tc.reject {
|
||||
if err == nil || !strings.Contains(err.Error(), "data exceeds expected size") || data != nil {
|
||||
t.Fatalf("range not refused: data=%v err=%v", data, err)
|
||||
}
|
||||
} else if err != nil || !bytes.Equal(data, tc.payload) {
|
||||
t.Fatalf("exact-bound payload changed: %v %v", data, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -402,11 +402,12 @@ func (s *Session) recvH2CData(totalBytes uint32) ([]byte, error) {
|
||||
if dataLen == 0 {
|
||||
return nil, fmt.Errorf("recvH2CData: H2CData PDU has no payload")
|
||||
}
|
||||
if h2c.DATAO+dataLen > totalBytes {
|
||||
end := uint64(h2c.DATAO) + uint64(dataLen)
|
||||
if end > uint64(totalBytes) {
|
||||
return nil, fmt.Errorf("recvH2CData: data exceeds expected size (%d+%d > %d)",
|
||||
h2c.DATAO, dataLen, totalBytes)
|
||||
}
|
||||
if err := s.wire.ReceiveData(buf[h2c.DATAO : h2c.DATAO+dataLen]); err != nil {
|
||||
if err := s.wire.ReceiveData(buf[uint64(h2c.DATAO):end]); err != nil {
|
||||
return nil, fmt.Errorf("recvH2CData: receive data: %w", err)
|
||||
}
|
||||
s.recordH2CData(dataLen)
|
||||
|
||||
@@ -411,6 +411,13 @@ Notes:
|
||||
|
||||
---
|
||||
|
||||
|
||||
### B.8 NVMe/TCP receive bounds
|
||||
|
||||
| ID | Statement | Machine / transition | Enforced in | Test | TLA+ property | Status |
|
||||
|---|---|---|---|---|---|---|
|
||||
| `INV-NVME-H2C-RANGE-001` | An H2CData range exceeding the expected transfer size is refused before slicing, including a uint32 offset-plus-length overflow; an exact-bound payload is preserved. | Frontend H2C receive validation; no storage state transition | `core/frontend/nvme/session.go` `recvH2CData` | `TestRecvH2CDataRangeBounds` (`core/frontend/nvme/h2c_bounds_test.go`) | — | ACTIVE |
|
||||
|
||||
## Part C. Change rules
|
||||
|
||||
How a fix or a feature is calibrated against this model:
|
||||
|
||||
Reference in new issue
Block a user