Skip to content

db: reject a corrupt freelist at open instead of panicking mid-commit - #1348

Open
tony1233200 wants to merge 1 commit into
etcd-io:mainfrom
tony1233200:fix/1319-validate-freelist-at-open
Open

tony1233200 wants to merge 1 commit into
etcd-io:mainfrom
tony1233200:fix/1319-validate-freelist-at-open

Conversation

@tony1233200

Copy link
Copy Markdown

A corrupt freelist was accepted at open and only died later, in the middle of a commit, with a panic: invalid page allocation: 0, page N already freed, or freelist pgid (N) above high water mark. Open now validates the cheap structural invariants the writer always maintains — every entry is a real page id (never a meta page), lies below the high-water mark, and appears at most once — and fails with a clear error instead.

Changes

  • db.go: loadFreelist now returns an error (stored so repeat calls see the same result); validateFreelist checks the loaded ids via Copyall (free + pending, both backends share it).
  • db.go (Open): a failed freelist load closes the DB and returns the error, like the other open failures.
  • tx_check.go (Tx.check): a failed freelist load is reported on the check channel instead of proceeding with a corrupt freelist.
  • freelist_validate_test.go + testdata/issue1319/: regression tests using the reproducer databases attached to Reader accepts pages violating writer invariants, freelist and page checks miss them #1319.

Repro (before the fix, on main a1121b9)

All three reproducer databases opened fine and panicked at Commit:

  • bbolt_freelist.go_117.db → panic: invalid page allocation: 0
  • bbolt_freelist.go_175.db → panic: page 2 already freed
  • bbolt_node.go_375.db → panic: freelist pgid (8960) above high water mark (4)

After the fix, Open with PreLoadFreelist: true returns e.g. freelist contains invalid page id 0 / freelist page id 8960 is at or above the high-water mark 4. (The new fixture test fails on unpatched main — Open returns a nil error — and passes with the fix.)

Tests

  • go test -run 'TestLoadFreelistRejectsInvalidEntries|TestValidateFreelistRejectsDuplicateEntries' -count=1 . — pass
  • go test -p 1 -count=1 -skip 'TestConcurrent|TestSimulate|TestQuick' . — pass (array freelist)
  • TEST_FREELIST_TYPE=hashmap go test -p 1 -count=1 -skip 'TestConcurrent|TestSimulate|TestQuick' . — pass
  • go test -p 1 -count=1 ./internal/... — pass
  • go vet ./... — clean except the pre-existing reflect.ValueOf copylocks note in db_test.go:1507, which the file itself marks //nolint:govet
  • gofmt clean; gofumpt clean on the new test file (the db.go/tx_check.go gofumpt diffs are pre-existing lines elsewhere in those files, verified against unpatched main, and left untouched)

Note: the full non--short suite was not run to completion on this small VM — TestBucket_Delete_FreelistOverflow (a bulk-write test doing ~8k fdatasync commits, on a code path this change does not touch) exceeds a 300s timeout here; the repo's own default test timeout is 30m. The -short suites above, which cover the load/check paths this change touches, pass on both freelist backends.

Notes / scope

A freelist containing an invalid page id, an entry at or above the
high-water mark, or a duplicate was accepted at open and only died
later, mid-commit, with a panic ('invalid page allocation: 0',
'page N already freed', 'freelist pgid (N) above high water mark').
Validate the loaded freelist in loadFreelist and report the problem
as an error from Open (and on the Tx.Check channel) instead.

Fixes etcd-io#1319 (freelist rows).
@kubernetes-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tony1233200
Once this PR has been reviewed and has the lgtm label, please assign serathius for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

1 participant