-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: deobfuscate dragonberry #383
Changes from 3 commits
feb2ac3
f94ee2a
e97c91d
f329a25
b3d6417
a14131c
f4e01fc
d8f9ef6
b097d9a
dbda17f
a885b72
20e9f42
0a229f4
cf1e6ad
ef294c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,38 +20,56 @@ import ( | |
_ "golang.org/x/crypto/ripemd160" //nolint:staticcheck | ||
) | ||
|
||
// validate the IAVL Ops | ||
func validateIavlOps(op opType, b int) error { | ||
// validateIavlOps validates the prefix to ensure it begins with | ||
// the height, size, and version of the IAVL tree. Each varint must be a bounded value. | ||
// In addition, the remaining bytes are validated to ensure they correspond to the correct | ||
// length. | ||
func validateIavlOps(op opType, layerNum int) error { | ||
r := bytes.NewReader(op.GetPrefix()) | ||
|
||
values := []int64{} | ||
for i := 0; i < 3; i++ { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided to remove the for loop for readability, so each height, size, and version could have a var name rather than an index |
||
varInt, err := binary.ReadVarint(r) | ||
if err != nil { | ||
return err | ||
} | ||
values = append(values, varInt) | ||
height, err := binary.ReadVarint(r) | ||
if err != nil { | ||
return err | ||
} | ||
if int(height) < 0 || int(height) < layerNum { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previous check: |
||
return fmt.Errorf("IAVL height (%d) must be non-negative and less than the layer number (%d)", height, layerNum) | ||
} | ||
|
||
// values must be bounded | ||
if int(varInt) < 0 { | ||
return fmt.Errorf("wrong value in IAVL leaf op") | ||
} | ||
size, err := binary.ReadVarint(r) | ||
if err != nil { | ||
return err | ||
} | ||
if int(values[0]) < b { | ||
return fmt.Errorf("wrong value in IAVL leaf op") | ||
|
||
if int(size) < 0 { | ||
return fmt.Errorf("IAVL size must be non-negative") | ||
} | ||
|
||
r2 := r.Len() | ||
if b == 0 { | ||
if r2 != 0 { | ||
return fmt.Errorf("invalid op") | ||
version, err := binary.ReadVarint(r) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if int(version) < 0 { | ||
return fmt.Errorf("IAVL version must be non-negative") | ||
} | ||
|
||
// grab the length of the remainder of the prefix | ||
remLen := r.Len() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if layerNum == 0 { | ||
if remLen != 0 { | ||
return fmt.Errorf("expected remaining prefix length to be 0, got: %d", remLen) | ||
} | ||
} else { | ||
if !(r2^(0xff&0x01) == 0 || r2 == (0xde+int('v'))/10) { | ||
// when the child comes from the left, the suffix if filled in | ||
// prefix: height | size | version | length byte (1 remainder) | ||
// | ||
// when the child comes from the right, the suffix is empty | ||
// prefix: height | size | version | length byte | 32 byte hash | next length byte (34 remainder) | ||
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks @damiannolan for this! |
||
if remLen != 1 && remLen != 34 { | ||
return fmt.Errorf("invalid op") | ||
} | ||
if op.GetHash()^1 != 0 { | ||
return fmt.Errorf("invalid op") | ||
if op.GetHash() != HashOp_SHA256 { | ||
return fmt.Errorf("IAVL hash op must be %v", HashOp_SHA256) | ||
} | ||
} | ||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
b
->layerNum
open to other naming suggestionsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
layerNum
is good to me, maybe we can indicate in godoc that it corresponds to tree depth