Skip to content

crypto/types: CompactBitArray doesn't defensively check that .SetIndex has a positive index before indexing #9164

Closed
@odeke-em

Description

Summary of Bug

If we audit this code

func (bA *CompactBitArray) SetIndex(i int, v bool) bool {
if bA == nil {
return false
}
if i >= bA.Count() {
return false
}
if v {
bA.Elems[i>>3] |= (uint8(1) << uint8(7-(i%8)))
} else {
bA.Elems[i>>3] &= ^(uint8(1) << uint8(7-(i%8)))
}
return true
}

we'll notice
if i >= bA.Count() {
return false
}
if v {
bA.Elems[i>>3] |= (uint8(1) << uint8(7-(i%8)))
} else {
bA.Elems[i>>3] &= ^(uint8(1) << uint8(7-(i%8)))
}

the code in (*CompactBitArray).SetIndex assumes that it'll always take in a positive index. However, the stakes have changed, the cosmos-sdk will be used in every way and our mandate is ensuring reliability and security. We have an explicit check in there that the index won't cause out of bounds, we should simply add one too to ensure that we don't accept negative values.

Version

Latest with bffcae5


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned

/cc @cuonglm

Activity

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions