From 189ba9830ad512800a8314313325e6f5b0b1e2b8 Mon Sep 17 00:00:00 2001 From: tony1233200 <127579663+tony1233200@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:04:09 +0000 Subject: [PATCH] db: reject a corrupt freelist at open instead of panicking mid-commit 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 #1319 (freelist rows). --- db.go | 47 +++++++++++++++-- freelist_validate_test.go | 54 ++++++++++++++++++++ testdata/issue1319/bbolt_freelist.go_117.db | Bin 0 -> 16387 bytes testdata/issue1319/bbolt_freelist.go_175.db | Bin 0 -> 16387 bytes testdata/issue1319/bbolt_node.go_375.db | Bin 0 -> 16394 bytes tx_check.go | 5 +- 6 files changed, 100 insertions(+), 6 deletions(-) create mode 100644 freelist_validate_test.go create mode 100644 testdata/issue1319/bbolt_freelist.go_117.db create mode 100644 testdata/issue1319/bbolt_freelist.go_175.db create mode 100644 testdata/issue1319/bbolt_node.go_375.db diff --git a/db.go b/db.go index 770561c28..2047b4adc 100644 --- a/db.go +++ b/db.go @@ -134,8 +134,9 @@ type DB struct { rwtx *Tx stats *Stats - freelist fl.Interface - freelistLoad sync.Once + freelist fl.Interface + freelistLoad sync.Once + freelistLoadErr error pagePool sync.Pool @@ -301,7 +302,11 @@ func Open(path string, mode os.FileMode, options *Options) (db *DB, err error) { } if db.PreLoadFreelist { - db.loadFreelist() + if err := db.loadFreelist(); err != nil { + _ = db.close() + lg.Errorf("failed to load freelist (%s): %v", path, err) + return nil, err + } } if db.readOnly { @@ -419,7 +424,11 @@ func (db *DB) getPageSizeFromSecondMeta() (int, bool, error) { // loadFreelist reads the freelist if it is synced, or reconstructs it // by scanning the DB if it is not synced. It assumes there are no // concurrent accesses being made to the freelist. -func (db *DB) loadFreelist() { +// +// A freelist that violates the invariants the writer maintains (see +// validateFreelist) is reported as an error instead of being accepted +// and panicking in the middle of a later commit. +func (db *DB) loadFreelist() error { db.freelistLoad.Do(func() { db.freelist = newFreelist(db.FreelistType) if !db.hasSyncedFreelist() { @@ -429,10 +438,38 @@ func (db *DB) loadFreelist() { // Read free list from freelist page. db.freelist.Read(db.page(db.meta().Freelist())) } - if db.stats != nil { + db.freelistLoadErr = db.validateFreelist() + if db.freelistLoadErr == nil && db.stats != nil { db.stats.FreePageN = db.freelist.FreeCount() } }) + return db.freelistLoadErr +} + +// validateFreelist checks the cheap structural invariants of the loaded +// freelist that the writer always maintains: every entry must be a real +// page id (never a meta page), must lie below the high-water mark, and +// must appear at most once. Without this, a corrupt freelist is accepted +// at open and only dies later, mid-commit, with a panic such as +// "invalid page allocation: 0" or "page N already freed" (see #1319). +func (db *DB) validateFreelist() error { + highWaterMark := db.meta().Pgid() + ids := make([]common.Pgid, db.freelist.Count()) + db.freelist.Copyall(ids) + seen := make(map[common.Pgid]struct{}, len(ids)) + for _, id := range ids { + if id <= 1 { + return fmt.Errorf("freelist contains invalid page id %d", id) + } + if id >= highWaterMark { + return fmt.Errorf("freelist page id %d is at or above the high-water mark %d", id, highWaterMark) + } + if _, ok := seen[id]; ok { + return fmt.Errorf("freelist contains duplicate page id %d", id) + } + seen[id] = struct{}{} + } + return nil } func (db *DB) hasSyncedFreelist() bool { diff --git a/freelist_validate_test.go b/freelist_validate_test.go new file mode 100644 index 000000000..44fdb91ba --- /dev/null +++ b/freelist_validate_test.go @@ -0,0 +1,54 @@ +package bbolt + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.etcd.io/bbolt/internal/common" +) + +// TestLoadFreelistRejectsInvalidEntries is a regression test for +// https://github.com/etcd-io/bbolt/issues/1319: a corrupt freelist was +// accepted at open and only died later, mid-commit, with a panic +// ("invalid page allocation: 0", "page N already freed", or +// "freelist pgid (N) above high water mark"). Open now fails with a +// clear error instead. Fixtures are the reproducer databases attached +// to the issue. +func TestLoadFreelistRejectsInvalidEntries(t *testing.T) { + for _, tc := range []struct { + fixture string + want string + }{ + {"bbolt_freelist.go_117.db", "freelist contains invalid page id 0"}, + {"bbolt_freelist.go_175.db", "freelist contains invalid page id 0"}, + {"bbolt_node.go_375.db", "freelist page id 8960 is at or above the high-water mark 4"}, + } { + t.Run(tc.fixture, func(t *testing.T) { + data, err := os.ReadFile(filepath.Join("testdata", "issue1319", tc.fixture)) + require.NoError(t, err) + path := filepath.Join(t.TempDir(), "db") + require.NoError(t, os.WriteFile(path, data, 0o600)) + _, err = Open(path, 0o600, &Options{PreLoadFreelist: true}) + require.Error(t, err) + require.Contains(t, err.Error(), tc.want) + }) + } +} + +// TestValidateFreelistRejectsDuplicateEntries covers the duplicate-id +// branch of validateFreelist, which the issue fixtures do not reach: +// their sorted ids trip the invalid-id check first. +func TestValidateFreelistRejectsDuplicateEntries(t *testing.T) { + path := filepath.Join(t.TempDir(), "db") + db, err := Open(path, 0o600, nil) + require.NoError(t, err) + defer db.Close() + + // A fresh database has meta pages 0/1, freelist page 2 and root + // page 3, so 2 is a plausible free page id below the high-water + // mark; listing it twice is the corruption under test. + db.freelist.Init(common.Pgids{2, 2}) + require.EqualError(t, db.validateFreelist(), "freelist contains duplicate page id 2") +} diff --git a/testdata/issue1319/bbolt_freelist.go_117.db b/testdata/issue1319/bbolt_freelist.go_117.db new file mode 100644 index 0000000000000000000000000000000000000000..d37e34f0299b4a6e794787dc1510fc62aa6933e5 GIT binary patch literal 16387 zcmeI3OK;Oa5P;+KK^szsqR8tpo=mRJqAkwVF6p>{L_; zDOYZs_z_&V!3F*Z2mV31aRZf^c(yeTc3L%Uope^(j%Vk!``MD$b{dPhZItBx_vC&Y zk5lfz2#rh8v4JA}<@nD(?;fSC)hkkG$pn}H69^~pHQdZrCcp%k028<%0>2Lq4lbxP zmt_J>AW8&o2TaEQC>Y?z!%-xl(4^!^Jn8eljGunIw|Y~18gz|Bap^oKCcp%kK%WV~ zmRatZ)jp@@beE1m=F+X07t92h023H^0`gPoz%PUsFuVlhMVgBJa2GiNLT+H?!+qHD zE47yGh-R2efNd^d{G`LXZlkctbs3)+TijOFQc9UoGRpi6xpfJ0Py8FFsmF}F>WkAb zZoaI)YMGX^<1~ZnC4F((W7eBGjFamOll@~HR%O6!nE{(GPnQWW0VZ%-0xPFY5b^kS z#OK%V>B-+_#$%J6)VN)H4}A=J2|DWEpyRil2c-W3iQdbk-vKADUIrb1#3}thfR1%O zlD`fcM$CVY^eX7+Unc!C=veO)bfac8>ma!?0VXgU1XhM)8G);42U>>HFwpmqc;^=9 zo*4Xp1$=kmZAQ!4^6K7We{2$r7>_XzEn}l5suuYxIJVZP3EP{%d+bk!cffJz%|^pX zjN)HR@!S^8t`uJ=Pwx~x-n94=T%$Qq*bAs_HY|N1zo6;cVvovpiFoIB-`PBj1A$^MUXCSXbPmm5mIea~lh%XxSPD9aDOoJ`%wQop!)-}dP4z12ncug3#AcrpD zUp1aWq)#n9kxsp=i~7cY$V(r83cQkTL0=Ljq2??3tX9YsO1bKrS+hF3tf|>zp^z^Y zb9zb7mWuhDUVK{@jv>?er38xO2-JI_E`g2!C7ma5e1r5#!?I0ywPrc%rd1VlvDaY0 zHnM42roAPq_Z#&cqvEWIUDx1&Z&$$Hf@`?T=ja4b(3y+&nOEpt;BIf1a1T6d+?W6p eI9CF>bFIe3qE0}5u8R6Rd45cQ2{3^{An+HuFuk1s literal 0 HcmV?d00001 diff --git a/testdata/issue1319/bbolt_freelist.go_175.db b/testdata/issue1319/bbolt_freelist.go_175.db new file mode 100644 index 0000000000000000000000000000000000000000..f1f91e840c753907da3482f7de3773a90ba3d5ca GIT binary patch literal 16387 zcmeHNy>HV%6t|NCArzXxf?%nt3Mz);#QA6{hDsoW1VR)Egy>M*Af$}2 zF)=d&3ruXxtc-|(|DbFL34y|U_FZCKlcs6DoaCK!zWd$1_wK!)9sBuWMRCg%Z+E^= z?*yc@cRVBRtz*Ibmit?_t#-Y|zqcrn~ZTG<4FFmj^JQGI6-t5`x;`^Y0 za@4Vj`+={Z;`Ne&iuf~B++V!H@h@mmdYR(|5c1+R!1y7Nkbeyr*Llz7b+~Dv{YxC@ z0HgdG$DaV>dLN(~d81r_wrL5}w?EzQo#cDpUQEfl)35^Sqt0#i#I(#(kuRz`W2HQ^ zntI`oB<{nmBk28MsO3`04o~3`P290qxs?e;_Gh<=Hg{^-zzOaHSGx;qLrA(3h4J$z z;eZ8bcQABAu-`8FIHgJWo`kCsekd)zhDZF6HlVmhtJ_P5!=*%R=uFcG?Ae;(npxz5c6?vFj!A6?S^`u85ZLLyBaz9#>^OBeRzq6 zqLKRrR@ms^%?Ymt%HIWp-M7{5O$^;aU-EZt=wXoq#DGs2NczMNa>LIIG`30o>?Qd| P3=jjvfXl!ipA+#PDGu24 literal 0 HcmV?d00001 diff --git a/testdata/issue1319/bbolt_node.go_375.db b/testdata/issue1319/bbolt_node.go_375.db new file mode 100644 index 0000000000000000000000000000000000000000..9b574a5eebf8bb94ffa55de22ce4687811a74234 GIT binary patch literal 16394 zcmeI3L2J}N6vvZn)w)!>s~6FWR1xt|cQ>1)TRaGg2o|9gJy@Zn$&`j}64_*nMf?Eb zMK68`FJ8o(U&PN~y$N1K-`mXCu(q(lAiM5=;O*qiy!Z0*%WQTtlW1C2W@Nqpd2Zj} z?ON7Qk?pD)^pT?MRr}W;FYnZ&)e}lb4FaPRm>t~|mLLEEAOHg6Pe3346pMY0reBD?&pi%|11CbyL+qWt(7a&$WYTH1vNP`oe*%;AOHfT5b)w?>p|+KqLgR| z0s0+O^|I8jl2Kk?C!JpeQ1)+0=l#5s_9jg_aQqum_ep2_b*Xi#O=Dh zkm^H=p(ZV;m)}BVtc zo#9Uo@h}m+zX;)V`$;N#qvmj0<{;f3(Z1=B%tdRlY3_)s{U5isa1aWrU&j_;U-5egbtrZ z9s*2XmilbH_Ba&bW&!d%k}i-eiH>Mkp4DpFZM)OF1V8`;K%h7Q`eTCv0w4eaATSvOe#$Z#+5W>U Fe*pz0dvyQ+ literal 0 HcmV?d00001 diff --git a/tx_check.go b/tx_check.go index 952da01e3..ba063645b 100644 --- a/tx_check.go +++ b/tx_check.go @@ -42,7 +42,10 @@ func (tx *Tx) check(cfg checkConfig, ch chan error) { } }() // Force loading free list if opened in ReadOnly mode. - tx.db.loadFreelist() + if err := tx.db.loadFreelist(); err != nil { + ch <- err + return + } // Check if any pages are double freed. freed := make(map[common.Pgid]bool)