phase163: harden nvme rdma bridge boundaries

This commit is contained in:
pingqiu committed 2026-07-18 01:14:15 -07:00
1 parent ab0930c399
commit b0bfb5b3f3
10 files changed
+112 -13

No files matched your search

@@ -115,6 +115,7 @@ func Start(cfg Config) (*Device, error) {
doItDone: make(chan error, 1),
serveDone: make(chan error, 1),
}
cleanupFD = false
go func() {
d.serveDone <- protocolServer{backend: cfg.Backend, size: cfg.Size}.serve(ctx, serverFile)
}()
@@ -126,7 +127,6 @@ func Start(cfg Config) (*Device, error) {
return nil, err
}
cleanupFD = false
return d, nil
}
@@ -193,6 +193,9 @@ func openDevice(requested string) (string, int, error) {
}
sort.Slice(paths, func(i, j int) bool { return nbdNumber(paths[i]) < nbdNumber(paths[j]) })
for _, path := range paths {
if nbdNumber(path) == int(^uint(0)>>1) {
continue
}
fd, err := unix.Open(path, unix.O_RDWR|unix.O_EXCL|unix.O_CLOEXEC, 0)
if err == nil {
return path, fd, nil
@@ -0,0 +1,17 @@
//go:build linux
package nbd
import "testing"
func TestNBDNumberAcceptsOnlyWholeDevices(t *testing.T) {
if got := nbdNumber("/dev/nbd12"); got != 12 {
t.Fatalf("nbdNumber(/dev/nbd12)=%d want 12", got)
}
invalid := int(^uint(0) >> 1)
for _, path := range []string{"/dev/nbd0p1", "/dev/nbd-control", "/dev/not-nbd"} {
if got := nbdNumber(path); got != invalid {
t.Fatalf("nbdNumber(%q)=%d want invalid", path, got)
}
}
}
@@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"syscall"
)
@@ -18,6 +19,8 @@ const (
nbdCmdFlush = uint32(3)
nbdCmdMask = uint32(0xffff)
nbdCmdFUA = uint32(1 << 16)
maxNBDRequestSize = uint32(64 << 20)
)
// Backend is the minimum block contract required by the kernel NBD bridge.
@@ -49,6 +52,15 @@ func (s protocolServer) serve(ctx context.Context, rw io.ReadWriter) error {
if command&nbdCmdMask == nbdCmdDisc {
return nil
}
if length > maxNBDRequestSize {
if command&nbdCmdMask == nbdCmdWrite {
return fmt.Errorf("nbd: write request length %d exceeds limit %d", length, maxNBDRequestSize)
}
if err := writeReply(rw, handle, syscall.EINVAL, nil); err != nil {
return err
}
continue
}
if offset > s.size || uint64(length) > s.size-offset {
if command&nbdCmdMask == nbdCmdWrite {
@@ -116,22 +116,60 @@ func TestProtocolServerConsumesRejectedWritePayload(t *testing.T) {
}
}
func TestProtocolServerRejectsOversizedReadWithoutAllocation(t *testing.T) {
backend := &memoryBackend{data: make([]byte, 1024)}
request := new(bytes.Buffer)
handle := [8]byte{6}
writeRequestHeader(t, request, nbdCmdRead, handle, 0, maxNBDRequestSize+1)
writeRequest(t, request, nbdCmdDisc, handle, 0, nil)
reply := new(bytes.Buffer)
if err := (protocolServer{backend: backend, size: uint64(maxNBDRequestSize) + 1}).serve(context.Background(), struct {
io.Reader
io.Writer
}{Reader: request, Writer: reply}); err != nil {
t.Fatalf("serve: %v", err)
}
header := reply.Next(16)
if got := binary.BigEndian.Uint32(header[4:8]); got == 0 {
t.Fatal("oversized read unexpectedly succeeded")
}
}
func TestProtocolServerTerminatesOversizedWriteWithoutReadingPayload(t *testing.T) {
backend := &memoryBackend{data: make([]byte, 1024)}
request := new(bytes.Buffer)
writeRequestHeader(t, request, nbdCmdWrite, [8]byte{5}, 0, maxNBDRequestSize+1)
reply := new(bytes.Buffer)
err := (protocolServer{backend: backend, size: uint64(maxNBDRequestSize) + 1}).serve(context.Background(), struct {
io.Reader
io.Writer
}{Reader: request, Writer: reply})
if err == nil {
t.Fatal("oversized write unexpectedly kept the connection open")
}
}
func writeRequest(t *testing.T, w io.Writer, command uint32, handle [8]byte, offset uint64, payload []byte) {
t.Helper()
writeRequestHeader(t, w, command, handle, offset, uint32(len(payload)))
if command&nbdCmdMask == nbdCmdWrite {
if _, err := w.Write(payload); err != nil {
t.Fatalf("write request payload: %v", err)
}
}
}
func writeRequestHeader(t *testing.T, w io.Writer, command uint32, handle [8]byte, offset uint64, length uint32) {
t.Helper()
header := make([]byte, 28)
binary.BigEndian.PutUint32(header[0:4], nbdRequestMagic)
binary.BigEndian.PutUint32(header[4:8], command)
copy(header[8:16], handle[:])
binary.BigEndian.PutUint64(header[16:24], offset)
binary.BigEndian.PutUint32(header[24:28], uint32(len(payload)))
binary.BigEndian.PutUint32(header[24:28], length)
if _, err := w.Write(header); err != nil {
t.Fatalf("write request header: %v", err)
}
if command&nbdCmdMask == nbdCmdWrite {
if _, err := w.Write(payload); err != nil {
t.Fatalf("write request payload: %v", err)
}
}
}
func readReply(t *testing.T, r io.Reader, handle [8]byte, data []byte) {
@@ -43,8 +43,8 @@ func startPlatformTarget(cfg TargetConfig, backend nbd.Backend) (platformTarget,
if err != nil || port < 1 || port > 65535 {
return nil, "", fmt.Errorf("nvmerdma: invalid service port %q", service)
}
if cfg.SubsysNQN == "" || filepath.Base(cfg.SubsysNQN) != cfg.SubsysNQN {
return nil, "", fmt.Errorf("nvmerdma: invalid subsystem NQN %q", cfg.SubsysNQN)
if err := validateSubsystemNQN(cfg.SubsysNQN); err != nil {
return nil, "", err
}
if cfg.NSID == 0 {
return nil, "", errors.New("nvmerdma: NSID must be non-zero")
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/seaweedfs/seaweed-block/core/frontend"
@@ -37,6 +38,13 @@ func NewTarget(cfg TargetConfig) *Target {
return &Target{cfg: cfg}
}
func validateSubsystemNQN(nqn string) error {
if nqn == "" || nqn == "." || nqn == ".." || strings.ContainsAny(nqn, `/\\`) {
return fmt.Errorf("nvmerdma: invalid subsystem NQN %q", nqn)
}
return nil
}
func (t *Target) Start() (string, error) {
t.mu.Lock()
defer t.mu.Unlock()
@@ -0,0 +1,21 @@
package nvmerdma
import "testing"
func TestValidateSubsystemNQN(t *testing.T) {
for _, nqn := range []string{
"",
".",
"..",
"../escape",
`..\escape`,
"parent/child",
} {
if err := validateSubsystemNQN(nqn); err == nil {
t.Fatalf("validateSubsystemNQN(%q) succeeded", nqn)
}
}
if err := validateSubsystemNQN("nqn.2026-07.io.seaweedfs:volume-1"); err != nil {
t.Fatalf("valid NQN rejected: %v", err)
}
}
@@ -76,7 +76,7 @@ closed rather than create a false supported/listening state.
## Phase 163 Live Evidence
TestOps run `20260718-010533-1ab1` passed 16/16 actions:
TestOps run `20260718-011316-dc5b` passed 16/16 actions:
```text
rdma_implementation_path=kernel_nvmet_rdma_nbd_bridge
@@ -40,7 +40,7 @@ RDMA device, and a non-loopback data-plane address.
## Evidence
Run: `20260718-010533-1ab1`, 16/16 actions PASS.
Run: `20260718-011316-dc5b`, 16/16 actions PASS.
```text
phase163_nvme_rdma_standalone_listener_impl_spike_status=ok
@@ -2,9 +2,9 @@
Status: **PASS** on 2026-07-18.
TestOps run: `20260718-010533-1ab1`.
TestOps run: `20260718-011316-dc5b`.
Local bundle: `results/20260718-010533-1ab1`.
Local bundle: `results/20260718-011316-dc5b`.
## Verdict