From 4231968cd25b676c7f8680b247ebc0c30b636037 Mon Sep 17 00:00:00 2001 From: Xuhui zhang Date: Fri, 27 Nov 2026 11:38:31 +0800 Subject: [PATCH] meta: reject out-of-range attribute fields in changelog replay AttrFields parsed every field as int64 and narrowed it without checks, so negative or overflowing uid/gid/mode/flags/nsec/acl/tier values were silently truncated during replay. Parse unsigned fields with the bounded Uint32/Uint16/Uint8 helpers and keep the signed second fields as Int64. --- pkg/meta/changelog.go | 29 +++++++++++++++------------ pkg/meta/changelog_test.go | 40 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/pkg/meta/changelog.go b/pkg/meta/changelog.go index a5f55809bdf0..ac65027b517b 100644 --- a/pkg/meta/changelog.go +++ b/pkg/meta/changelog.go @@ -357,21 +357,26 @@ func (e *ChangeEntry) AttrFields(i int) (*Attr, error) { if n := len(e.Args) - i; n != attrLogFields { return nil, fmt.Errorf("%s: attribute tail at %d has %d of %d fields", e.Op, i, n, attrLogFields) } - var v [attrLogFields]int64 - for j := range v { - var err error - if v[j], err = e.Int64(i + j); err != nil { + var a Attr + var errs [attrLogFields]error + a.Uid, errs[0] = e.Uint32(i) + a.Gid, errs[1] = e.Uint32(i + 1) + a.Mode, errs[2] = e.Uint16(i + 2) + a.Flags, errs[3] = e.Uint8(i + 3) + a.Atime, errs[4] = e.Int64(i + 4) + a.Mtime, errs[5] = e.Int64(i + 5) + a.Atimensec, errs[6] = e.Uint32(i + 6) + a.Mtimensec, errs[7] = e.Uint32(i + 7) + a.Ctime, errs[8] = e.Int64(i + 8) + a.Ctimensec, errs[9] = e.Uint32(i + 9) + a.AccessACL, errs[10] = e.Uint32(i + 10) + a.Tier, errs[11] = e.Uint8(i + 11) + for _, err := range errs { + if err != nil { return nil, err } } - return &Attr{ - Uid: uint32(v[0]), Gid: uint32(v[1]), - Mode: uint16(v[2]), Flags: uint8(v[3]), - Atime: v[4], Mtime: v[5], - Atimensec: uint32(v[6]), Mtimensec: uint32(v[7]), - Ctime: v[8], Ctimensec: uint32(v[9]), - AccessACL: uint32(v[10]), Tier: uint8(v[11]), - }, nil + return &a, nil } func (e *ChangeEntry) Bool(i int) (bool, error) { diff --git a/pkg/meta/changelog_test.go b/pkg/meta/changelog_test.go index df9c6639b263..307c1ac8799e 100644 --- a/pkg/meta/changelog_test.go +++ b/pkg/meta/changelog_test.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "math" "math/rand" "os" "path/filepath" @@ -341,6 +342,45 @@ func TestChangeEntryAttrFields(t *testing.T) { if _, err := e.AttrFields(4); err == nil { t.Fatal("a truncated attribute tail should fail") } + + maxAttr := &Attr{ + Uid: math.MaxUint32, Gid: math.MaxUint32, Mode: math.MaxUint16, Flags: math.MaxUint8, + Atime: -1, Mtime: math.MinInt64, Atimensec: math.MaxUint32, Mtimensec: math.MaxUint32, + Ctime: math.MaxInt64, Ctimensec: math.MaxUint32, AccessACL: math.MaxUint32, Tier: math.MaxUint8, + } + if e, err = ParseChangeEntry(1, fmt.Sprintf("1.0|REPAIRDIR(5,%s)|(3,88)", maxAttr.logFields())); err != nil { + t.Fatal(err) + } + if got, err = e.AttrFields(1); err != nil { + t.Fatal(err) + } + if *got != *maxAttr { + t.Fatalf("AttrFields(1) = %+v, want %+v", got, maxAttr) + } + fields := strings.Split(attr.logFields(), ",") + for j, bad := range map[int][]string{ + 0: {"-1", "4294967296"}, + 1: {"-1", "4294967296"}, + 2: {"-1", "65536"}, + 3: {"-1", "256"}, + 6: {"-1", "4294967296"}, + 7: {"-1", "4294967296"}, + 9: {"-1", "4294967296"}, + 10: {"-1", "4294967296"}, + 11: {"-1", "256"}, + } { + for _, v := range bad { + f := append([]string(nil), fields...) + f[j] = v + e, err := ParseChangeEntry(1, fmt.Sprintf("1.0|REPAIRDIR(5,%s)|(3,88)", strings.Join(f, ","))) + if err != nil { + t.Fatal(err) + } + if got, err := e.AttrFields(1); err == nil { + t.Fatalf("AttrFields with field %d = %s should fail, got %+v", j, v, got) + } + } + } } func TestLoadProgress(t *testing.T) {