From 286f5ebeb8f4df7465fde5eeca356c4391ddd893 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 10:46:10 -0800 Subject: [PATCH 1/4] durable: red test, a created lock directory's parent must be 0755 V1-SPEC r2.45 (1): MkdirAll(locks, 0700) also makes a missing parent /run/sw-block 0700; created by root, it keeps the unit's user out even after the locks directory is chowned. The tmpfiles.d layout is parent 0755, locks 0700. Co-Authored-By: Claude Opus 5.5 --- .../durable/volume_lock_parent_mode_test.go | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go diff --git a/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go new file mode 100644 index 0000000000..3890219402 --- /dev/null +++ b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go @@ -0,0 +1,40 @@ +//go:build unix + +package durable_test + +import ( + "os" + "path/filepath" + "syscall" + "testing" + + "github.com/seaweedfs/seaweed-block/core/frontend/durable" +) + +// V1-SPEC r2.45 (1), as the tmpfiles.d entry lays it out: a lock directory +// the provider creates gets a traversable parent (0755) and is itself 0700, +// whatever the umask. A 0700 parent created by root would keep the unit's +// user out even after the locks directory is chowned to it. +func TestHostVolumeLockCreatesParent0755AndLocks0700(t *testing.T) { + old := syscall.Umask(0o077) // a strict umask must not narrow the parent + defer syscall.Umask(old) + parent := filepath.Join(t.TempDir(), "sw-block") + locks := filepath.Join(parent, "locks") + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err != nil { + t.Fatal(err) + } + _ = p.Close() + for path, want := range map[string]os.FileMode{parent: 0o755, locks: 0o700} { + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if got := fi.Mode().Perm(); got != want { + t.Fatalf("%s created %v, want %v", path, got, want) + } + } +} From 4c36f1f8614b80bcea97e47569ae38d370b11c9b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 10:46:38 -0800 Subject: [PATCH 2/4] durable: lay out a created lock directory as tmpfiles.d does A missing /run/sw-block is created 0755 and the locks directory 0700, with the modes set explicitly (the umask cannot narrow them); existing directories are left alone. The cure line also names the parent. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider.go | 27 +++++++++++++++++-- .../docs/releases/one-process-per-volume.md | 14 +++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index eb391db3d8..00ac1bb722 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -405,7 +405,7 @@ func NewDurableProvider(cfg ProviderConfig, view frontend.ProjectionView) (*Dura if cfg.VolumeLockDirTestOnly == "" { cfg.VolumeLockDirTestOnly = HostVolumeLockDir } - if err := os.MkdirAll(cfg.VolumeLockDirTestOnly, 0o700); err != nil { + if err := makeVolumeLockDir(cfg.VolumeLockDirTestOnly); err != nil { return nil, fmt.Errorf("%w %s: %w; %s", ErrVolumeLockDirUnavailable, cfg.VolumeLockDirTestOnly, err, volumeLockCure) } } @@ -560,7 +560,30 @@ var ErrVolumeLockDirUnavailable = errors.New("durable: host volume lock director // volumeLockCure names the operator's cure. A directory or lock file a root // process created is root-owned: an unprivileged unit then cannot use it. -const volumeLockCure = "install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), or chown the directory and its lock files to this process's user" +const volumeLockCure = "install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), or make its parent directory traversable (0755) and chown the directory and its lock files to this process's user" + +// makeVolumeLockDir lays out a missing lock directory as the tmpfiles.d +// entry does: its parent 0755 (traversable by the unit's user even when root +// created it) and the directory itself 0700. Existing directories are left +// as they are. Modes are set explicitly, so the umask cannot narrow them. +func makeVolumeLockDir(dir string) error { + parent := filepath.Dir(dir) + if _, err := os.Stat(parent); errors.Is(err, os.ErrNotExist) { + if err := os.MkdirAll(parent, 0o755); err != nil { + return err + } + if err := os.Chmod(parent, 0o755); err != nil { + return err + } + } + if err := os.Mkdir(dir, 0o700); err != nil { + if errors.Is(err, os.ErrExist) { + return nil + } + return err + } + return os.Chmod(dir, 0o700) +} // lockVolumeOnHost takes the volume's host lock (nil without // HostVolumeLock). A second live process for the volume on this host gets diff --git a/seaweed-block/docs/releases/one-process-per-volume.md b/seaweed-block/docs/releases/one-process-per-volume.md index e2604748c5..93ed89b56b 100644 --- a/seaweed-block/docs/releases/one-process-per-volume.md +++ b/seaweed-block/docs/releases/one-process-per-volume.md @@ -7,7 +7,8 @@ the same host is refused before it opens anything, whatever its `--durable-root`. The lock is released when the process exits, including on a crash or kill. -blockvolume creates the directory (`0700`) when it is missing and it may, and +blockvolume creates the directory when it is missing and it may, laid out as +the tmpfiles.d entry does (`/run/sw-block` `0755`, `/run/sw-block/locks` `0700`), and refuses to start when it cannot create or open the directory or a volume's lock file. The shipped units cannot create it (`ProtectSystem=strict`), so install the tmpfiles.d entry shipped in [`deploy/systemd/sw-block.tmpfiles.conf`](../../deploy/systemd/sw-block.tmpfiles.conf): @@ -22,11 +23,12 @@ boot. Both shipped units allow writes to it (`ReadWritePaths=-/run/sw-block/locks`). Without it, or when it is not usable, blockvolume exits at start with `host volume lock directory unavailable ... install the tmpfiles.d entry (deploy/systemd/sw-block.tmpfiles.conf), -or chown the directory and its lock files to this process's user`. A -directory created by a root process (the root ublk unit, or blockvolume run -by hand as root) is root-owned, and an unprivileged unit on that host then -cannot use it: install the tmpfiles.d entry, or chown the directory to the -unit's user. Lock files are opened read-only, so a lock file another user +or make its parent directory traversable (0755) and chown the directory and +its lock files to this process's user`. A directory created by a root +process (the root ublk unit, or blockvolume run by hand as root) is +root-owned, and an unprivileged unit on that host then cannot use it: +install the tmpfiles.d entry, or chown the locks directory to the unit's +user and make sure `/run/sw-block` is `0755`. Lock files are opened read-only, so a lock file another user created stays lockable. ## A rolled-back copy of a root is refused From 5c84520d48f1fedc3fac3d8e3c79b5d45f74e7d6 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 12:22:36 -0800 Subject: [PATCH 3/4] durable: red test, a regular file as the lock directory is accepted The new helper treats any existing lock path as the directory; the old constructor refused a non-directory. Controls: an existing parent and an existing lock directory keep their modes. Co-Authored-By: Claude Opus 5.5 --- .../durable/one_process_per_volume_test.go | 19 ++++++++ .../durable/volume_lock_parent_mode_test.go | 48 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/seaweed-block/core/frontend/durable/one_process_per_volume_test.go b/seaweed-block/core/frontend/durable/one_process_per_volume_test.go index 55af4a8d40..98b3329fe1 100644 --- a/seaweed-block/core/frontend/durable/one_process_per_volume_test.go +++ b/seaweed-block/core/frontend/durable/one_process_per_volume_test.go @@ -98,6 +98,25 @@ func TestHostVolumeLockDirectoryCreatedOrRefused(t *testing.T) { } } +// A lock path that exists but is not a directory refuses, as before the +// lock directory was laid out by the provider. +func TestHostVolumeLockRefusesNonDirectoryLockPath(t *testing.T) { + locks := filepath.Join(t.TempDir(), "locks") + if err := os.WriteFile(locks, nil, 0o600); err != nil { + t.Fatal(err) + } + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err == nil { + _ = p.Close() + } + if !errors.Is(err, durable.ErrVolumeLockDirUnavailable) { + t.Fatalf("a regular file as the lock directory was accepted: %v", err) + } +} + // A lock file this process cannot write (one another user created, as the // root ublk unit does) is still taken: the lock opens it read-only. func TestHostVolumeLockTakesReadOnlyLockFile(t *testing.T) { diff --git a/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go index 3890219402..ba85f454d9 100644 --- a/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go +++ b/seaweed-block/core/frontend/durable/volume_lock_parent_mode_test.go @@ -38,3 +38,51 @@ func TestHostVolumeLockCreatesParent0755AndLocks0700(t *testing.T) { } } } + +// newLockProvider opens a provider whose host lock directory is locks. +func newLockProvider(t *testing.T, locks string) { + t.Helper() + p, err := durable.NewDurableProvider(durable.ProviderConfig{ + Impl: durable.ImplWALStore, StorageRoot: t.TempDir(), BlockSize: 4096, NumBlocks: pcBlocks, + HostVolumeLock: true, VolumeLockDirTestOnly: locks, + }, v1View()) + if err != nil { + t.Fatal(err) + } + _ = p.Close() +} + +func wantPerm(t *testing.T, path string, want os.FileMode) { + t.Helper() + fi, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if got := fi.Mode().Perm(); got != want { + t.Fatalf("%s is %v, want %v", path, got, want) + } +} + +// Directories the provider did not create keep their modes: an existing +// parent with a missing lock directory, and both existing. +func TestHostVolumeLockKeepsExistingDirectoryModes(t *testing.T) { + parent := filepath.Join(t.TempDir(), "sw-block") + if err := os.Mkdir(parent, 0o711); err != nil { + t.Fatal(err) + } + if err := os.Chmod(parent, 0o711); err != nil { + t.Fatal(err) + } + locks := filepath.Join(parent, "locks") + newLockProvider(t, locks) + wantPerm(t, parent, 0o711) + wantPerm(t, locks, 0o700) + + if err := os.Chmod(locks, 0o750); err != nil { + t.Fatal(err) + } + newLockProvider(t, locks) + wantPerm(t, parent, 0o711) + wantPerm(t, locks, 0o750) +} + From dc393ba380532f21052a865df2f76087629518dc Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 22 Nov 2026 12:22:51 -0800 Subject: [PATCH 4/4] durable: refuse a non-directory lock path; chmod only what this call created The lock directory and its parent are chmod'ed only when this call's own Mkdir created them, so an existing or concurrently created directory keeps its mode; an existing lock path that is not a directory refuses (Lstat), as the constructor did before. The invariant row links the mode tests. Co-Authored-By: Claude Opus 5.5 --- .../core/frontend/durable/provider.go | 34 ++++++++++++------- seaweed-block/docs/design/invariants.md | 2 +- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/seaweed-block/core/frontend/durable/provider.go b/seaweed-block/core/frontend/durable/provider.go index 00ac1bb722..e99ada32b2 100644 --- a/seaweed-block/core/frontend/durable/provider.go +++ b/seaweed-block/core/frontend/durable/provider.go @@ -564,25 +564,35 @@ const volumeLockCure = "install the tmpfiles.d entry (deploy/systemd/sw-block.tm // makeVolumeLockDir lays out a missing lock directory as the tmpfiles.d // entry does: its parent 0755 (traversable by the unit's user even when root -// created it) and the directory itself 0700. Existing directories are left -// as they are. Modes are set explicitly, so the umask cannot narrow them. +// created it) and the directory itself 0700. Only a directory this call +// creates is chmod'ed (the umask cannot narrow it); one that exists, or that +// another process creates meanwhile, keeps its mode. An existing lock path +// that is not a directory refuses. func makeVolumeLockDir(dir string) error { parent := filepath.Dir(dir) - if _, err := os.Stat(parent); errors.Is(err, os.ErrNotExist) { - if err := os.MkdirAll(parent, 0o755); err != nil { - return err - } + if err := os.MkdirAll(filepath.Dir(parent), 0o755); err != nil { + return err + } + if err := os.Mkdir(parent, 0o755); err == nil { if err := os.Chmod(parent, 0o755); err != nil { return err } - } - if err := os.Mkdir(dir, 0o700); err != nil { - if errors.Is(err, os.ErrExist) { - return nil - } + } else if !errors.Is(err, os.ErrExist) { return err } - return os.Chmod(dir, 0o700) + if err := os.Mkdir(dir, 0o700); err == nil { + return os.Chmod(dir, 0o700) + } else if !errors.Is(err, os.ErrExist) { + return err + } + fi, err := os.Lstat(dir) + if err != nil { + return err + } + if !fi.IsDir() { + return fmt.Errorf("%s exists and is not a directory", dir) + } + return nil } // lockVolumeOnHost takes the volume's host lock (nil without diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 9a07e8cd70..8d2d96542f 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -715,7 +715,7 @@ Notes: | `INV-WS-CAS-UNKNOWN-001` | An unknown HEAD CAS outcome is settled by re-reading HEAD, never by a blind retry; until it is settled the proposal stays pending and its intents stay. | M5 CAS → Pending | `core/storage/chunk_layer.go` `captureLocked`, `resolvePending`; `core/storage/chunk_restore.go` `ClaimChunkHead` | `TestChunkRemote_UncertainCASIsNotPrecondition` (`core/frontend/chunkstore/chunk_remote_test.go`); `TestChunkLayer_C6_UnknownNotAppliedNeverClaimsSuccess` (`core/storage/chunk_layer_contract_test.go`); `TestClaimChunkHeadSettlesUnknownWithoutReplay` (`core/storage/chunk_restore_test.go`); `TestChunkIntent_UncertainCASKeepsIntentUntilSettled` (`core/storage/chunk_layer_intent_test.go`) | — | ACTIVE | | `INV-WS-CLAIM-BEFORE-SERVE-001` | A new holder claims HEAD at its master-issued epoch before I/O opens; a HEAD with a higher epoch, or another holder at the same epoch, fences the claim. | M4 Claiming → Holder \| Fenced | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestClaimChunkHeadRefusesStaleAuthorityAndAdmissionDrift` (`core/storage/chunk_restore_test.go`); `TestRestoreSeedPrecedesTakeoverCAS` (`core/frontend/durable/provider_seed_order_test.go`); `TestWorkspaceMoveRestoresLazilyAndFencesOldHost` (`core/host/master/workspace_restore_test.go`) | `WorkspaceTakeover` `SeedMatchesTakeover` | ACTIVE | | `INV-WS-STALE-LOCAL-ROOT-001` | An existing local root whose HEAD another holder has published, or whose local durable LSN is below HEAD's `remote_lsn` (a rolled-back copy), is not served or cut; the attach fails closed with `ErrChunkStaleLocalRoot`. | M4 Claiming → Refused | `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestWALStoreChunk_MoveBackOntoAnOldRootIsRefused`, `TestWALStoreChunk_MoveBackRefusedOnDeferredAttach` (`core/frontend/durable/provider_chunk_moveback_test.go`); `TestLocalReopenRejectsForeignClaimImage` (`core/frontend/durable/provider_reopen_belt_test.go`); `TestRolledBackCopyBelowHeadLSNIsRefused` (`core/frontend/durable/rolled_back_copy_test.go`); `TestCutPublishesOnlyLocallyDurableLSN` (`core/frontend/durable/cut_durable_before_cas_test.go`) | — | ACTIVE | -| `INV-WS-ONE-PROCESS-PER-HOST-001` | One host runs at most one live blockvolume per volume: each takes the volume's exclusive host lock before its store opens and holds it until the store closes; a second is refused before it opens anything, whatever its `--durable-root`. | M4 (start) → Attaching, Refused | `core/frontend/durable/provider.go` `DurableProvider.lockVolumeOnHost` | `TestOneLiveProcessPerVolumePerHost`, `TestHostVolumeLockDirectoryCreatedOrRefused`, `TestHostVolumeLockTakesReadOnlyLockFile` (`core/frontend/durable/one_process_per_volume_test.go`); `TestVolumeLockDirTestOnlyRefusedUnderSystemd` (`cmd/blockvolume/volume_lock_flag_test.go`) | — | ACTIVE | +| `INV-WS-ONE-PROCESS-PER-HOST-001` | One host runs at most one live blockvolume per volume: each takes the volume's exclusive host lock before its store opens and holds it until the store closes; a second is refused before it opens anything, whatever its `--durable-root`. | M4 (start) → Attaching, Refused | `core/frontend/durable/provider.go` `DurableProvider.lockVolumeOnHost` | `TestOneLiveProcessPerVolumePerHost`, `TestHostVolumeLockDirectoryCreatedOrRefused`, `TestHostVolumeLockRefusesNonDirectoryLockPath`, `TestHostVolumeLockTakesReadOnlyLockFile` (`core/frontend/durable/one_process_per_volume_test.go`); `TestHostVolumeLockCreatesParent0755AndLocks0700`, `TestHostVolumeLockKeepsExistingDirectoryModes` (`core/frontend/durable/volume_lock_parent_mode_test.go`); `TestVolumeLockDirTestOnlyRefusedUnderSystemd` (`cmd/blockvolume/volume_lock_flag_test.go`) | — | ACTIVE | | `INV-WS-SINGLE-FILER-001` | A chunk volume starts only when `--chunk-filer` is the only filer of its group: a master listing another filer or several refuses at once, whichever master answers first; a filer no master lists within 60 s is refused. A startup census, not a fence: a filer added to the group later is not detected. | M4 (startup) → Refused | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring`; `cmd/sw-block/chunk.go` `refuseUnlessSingleFiler` | `TestCheckSingleFilerRefusesAnotherOrSeveralFilers`, `TestCheckSingleFilerFailsClosed`, `TestCheckSingleFilerFavourableThenStalledRefuses`, `TestIsTargetComparesScopedAddresses`, `TestCheckSingleFilerPasses` (`core/frontend/chunkstore/single_filer_test.go`), `TestChunkWiringRefusesAMultiFilerGroup` (`cmd/blockvolume/single_filer_test.go`), `TestOpsChunk_WritingCommandsRunTheSingleFilerCheck` (`cmd/sw-block/chunk_start_check_test.go`), `TestOpsChunk_VerifyRunsTheSingleFilerCheck` (`cmd/sw-block/chunk_verify_test.go`) | — | ACTIVE | | `INV-WS-LEASE-REGRANT-001` | Re-grant after expiry: same holder and epoch only, no move pending or issued, not revoking, store certain, one durable write, and only once max(horizon, deadline) + skew bound has passed; the new process claims HEAD with a new generation before any I/O, so no older incarnation writes or CASes after. | M2 lease: Expired → Granted | `core/authority/workspace_lease_view.go` `regrantWorkspaceLease`; `core/authority/workspace.go` `forcedMoveBarrierLocked`; `core/storage/chunk_restore.go` `ClaimChunkHead` | `TestWorkspaceLeaseExpiredSameHolderRegrantIsDurable`, `TestWorkspaceLeaseRegrantWaitsForFullSkew`, `TestWorkspaceLeaseRegrantDefeatsAlreadyPassedForcedPreflight`, `TestWorkspaceLeaseForcedIssueDefeatsOldHolderRegrant`, `TestWorkspaceLeaseRegrantWaitsForDurabilityAndLatchesFailure` (`core/authority/workspace_regrant_test.go`); `TestWorkspaceLeaseRegrantPreservesLocalLogThroughFirstCut`, `TestWorkspaceLeaseRegrantCannotUnsealSameEpoch`, `TestWorkspaceLeaseRegrantEmptyRootClaimsOnlyHEAD` (`core/authority/workspace_regrant_storage_test.go`) | `WorkspaceRegrant` `NoDualServingInc`, `RegrantNoLoss`, `DiskCoversMemory` | ACTIVE | | `INV-WS-LEASE-001` | A host admits frontend I/O and cuts only under an unexpired lease for its exact `{volume, replica, epoch}`; a fenced guard never reopens, even on a late renewal reply. | M4 lease guard: Valid → Fenced | `core/host/volume/workspace_lease.go` `permits`, `Refresh` | `TestWorkspaceLeaseExpiryFencesWithoutTimerAndCannotReopen`, `TestWorkspaceDelayedRenewalCannotUndoExpiry` (`core/host/volume/workspace_lease_test.go`); `TestWorkspaceHostRequiresLeaseBeforeFrontendAndCut` (`core/host/volume/workspace_runtime_test.go`); `TestWorkspacePausedGranteeRefusedAtAttach` (`core/host/master/workspace_paused_grantee_test.go`) | `WorkspaceTakeover` `NoDualServing` | ACTIVE |