From 3b5ac658809111f78c5addfd6dc07e6488874050 Mon Sep 17 00:00:00 2001 From: Arpit Jain Date: Tue, 10 Nov 2026 22:41:11 -0700 Subject: [PATCH] cmd/bbolt: reject out of range item ids in page-item The item id is parsed as a uint64 and then narrowed to uint16 at both call sites, so the bounds check in leafPageElement only sees the low 16 bits. An id congruent to a valid index modulo 65536 prints that item and exits 0, and an id congruent to an out of range index is reported back with the truncated number rather than the one given. Keep the id a uint64 until it has been compared against the page count, and narrow it only afterwards. Cherry-picked from etcd-io/bbolt#1315 (f3dbd58). Signed-off-by: Arpit Jain --- cmd/bbolt/main.go | 14 +++++------ cmd/bbolt/main_test.go | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/cmd/bbolt/main.go b/cmd/bbolt/main.go index 12bf3970b..176b0d0ae 100644 --- a/cmd/bbolt/main.go +++ b/cmd/bbolt/main.go @@ -503,13 +503,13 @@ func (cmd *pageItemCommand) Run(args ...string) error { } if !options.valueOnly { - err := cmd.PrintLeafItemKey(cmd.Stdout, buf, uint16(itemID), options.format) + err := cmd.PrintLeafItemKey(cmd.Stdout, buf, itemID, options.format) if err != nil { return err } } if !options.keyOnly { - err := cmd.PrintLeafItemValue(cmd.Stdout, buf, uint16(itemID), options.format) + err := cmd.PrintLeafItemValue(cmd.Stdout, buf, itemID, options.format) if err != nil { return err } @@ -518,15 +518,15 @@ func (cmd *pageItemCommand) Run(args ...string) error { } // leafPageElement retrieves a leaf page element. -func (cmd *pageItemCommand) leafPageElement(pageBytes []byte, index uint16) (*guts_cli.LeafPageElement, error) { +func (cmd *pageItemCommand) leafPageElement(pageBytes []byte, index uint64) (*guts_cli.LeafPageElement, error) { p := (*guts_cli.Page)(unsafe.Pointer(&pageBytes[0])) - if index >= p.Count() { + if index >= uint64(p.Count()) { return nil, fmt.Errorf("leafPageElement: expected item index less than %d, but got %d.", p.Count(), index) } if p.Type() != "leaf" { return nil, fmt.Errorf("leafPageElement: expected page type of 'leaf', but got '%s'", p.Type()) } - return p.LeafPageElement(index), nil + return p.LeafPageElement(uint16(index)), nil } const FORMAT_MODES = "auto|ascii-encoded|hex|bytes|redacted" @@ -573,7 +573,7 @@ func writelnBytes(w io.Writer, b []byte, format string) error { } // PrintLeafItemKey writes the bytes of a leaf element's key. -func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, index uint16, format string) error { +func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, index uint64, format string) error { e, err := cmd.leafPageElement(pageBytes, index) if err != nil { return err @@ -582,7 +582,7 @@ func (cmd *pageItemCommand) PrintLeafItemKey(w io.Writer, pageBytes []byte, inde } // PrintLeafItemKey writes the bytes of a leaf element's value. -func (cmd *pageItemCommand) PrintLeafItemValue(w io.Writer, pageBytes []byte, index uint16, format string) error { +func (cmd *pageItemCommand) PrintLeafItemValue(w io.Writer, pageBytes []byte, index uint64, format string) error { e, err := cmd.leafPageElement(pageBytes, index) if err != nil { return err diff --git a/cmd/bbolt/main_test.go b/cmd/bbolt/main_test.go index 47cdff0a2..6e4eb7316 100644 --- a/cmd/bbolt/main_test.go +++ b/cmd/bbolt/main_test.go @@ -12,6 +12,7 @@ import ( "testing" "go.etcd.io/bbolt/internal/btesting" + "go.etcd.io/bbolt/internal/guts_cli" "github.com/stretchr/testify/require" @@ -286,6 +287,59 @@ func TestPagesCommand_Run(t *testing.T) { require.NoError(t, err) } +func TestPageItemCommand_ItemIDOutOfRange(t *testing.T) { + db := btesting.MustCreateDBWithOption(t, &bolt.Options{PageSize: 4096}) + srcPath := db.Path() + + err := db.Update(func(tx *bolt.Tx) error { + b, bErr := tx.CreateBucketIfNotExists([]byte("data")) + if bErr != nil { + return bErr + } + for i := 0; i < 100; i++ { + if bErr = b.Put([]byte(fmt.Sprintf("key_%d", i)), []byte(fmt.Sprintf("value_%d", i))); bErr != nil { + return bErr + } + } + return nil + }) + require.NoError(t, err) + db.Close() + + defer requireDBNoChange(t, dbData(t, srcPath), srcPath) + + _, hwm, err := guts_cli.ReadPageAndHWMSize(srcPath) + require.NoError(t, err) + leafPageId := 0 + for i := 2; i < int(hwm); i++ { + p, _, err := guts_cli.ReadPage(srcPath, uint64(i)) + require.NoError(t, err) + if p.Type() == "leaf" && p.Count() > 1 { + leafPageId = int(p.Id()) + } + } + require.NotEqual(t, 0, leafPageId) + + testCases := []struct { + name string + itemId string + }{ + // Congruent to the valid index 0 modulo 2^16. + {name: "wraps onto a valid index", itemId: "65536"}, + // Congruent to the out of range index 100 modulo 2^16, so the + // id reported back to the user has to be the one that was typed. + {name: "wraps onto an out of range index", itemId: "65636"}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + m := NewMain() + err := m.Run("page-item", srcPath, fmt.Sprintf("%d", leafPageId), tc.itemId) + require.ErrorContains(t, err, fmt.Sprintf("expected item index less than 100, but got %s", tc.itemId)) + }) + } +} + // Main represents a test wrapper for main.Main that records output. type Main struct { *main.Main