diff --git a/seaweed-block/core/frontend/nvme/h2c_bounds_test.go b/seaweed-block/core/frontend/nvme/h2c_bounds_test.go new file mode 100644 index 0000000000..136723b53b --- /dev/null +++ b/seaweed-block/core/frontend/nvme/h2c_bounds_test.go @@ -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) + } + }) + } +} diff --git a/seaweed-block/core/frontend/nvme/session.go b/seaweed-block/core/frontend/nvme/session.go index 5eb97d4aca..6578770090 100644 --- a/seaweed-block/core/frontend/nvme/session.go +++ b/seaweed-block/core/frontend/nvme/session.go @@ -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) diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index ec73b8f3da..d99cd88210 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -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: