Skip to content
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

crypto/hd: DerivePrivateKeyForPath can panic with index out of bounds if the path ends with a slash "/", due to improper check of length #8557

Closed
3 of 4 tasks
odeke-em opened this issue Feb 10, 2021 · 0 comments · Fixed by #8607

Comments

@odeke-em
Copy link
Collaborator

Summary of Bug

Found by auditing the code for Stargate and preparing to fuzz it, if any path is provided with a trailing slash for example just by simply passing in "m/16/19/" instead of "m/16/19" or even "/", or "m/16//10"

func TestDerivePrivateKeyForPath(t *testing.T) {
        hd.DerivePrivateKeyForPath([32]byte{}, [32]byte{}, "m/5/")
}

results in this panic

--- FAIL: TestDerivePrivateKeyForPath (0.00s)
panic: runtime error: slice bounds out of range [-1:] [recovered]
	panic: runtime error: slice bounds out of range [-1:]

goroutine 36 [running]:
testing.tRunner.func1.2(0x18b9080, 0xc00024bea8)
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1144 +0x332
testing.tRunner.func1(0xc000103080)
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1147 +0x4b6
panic(0x18b9080, 0xc00024bea8)
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/runtime/panic.go:965 +0x1b9
github.com/cosmos/cosmos-sdk/crypto/hd.DerivePrivateKeyForPath(0x0, 0x0, 0x0, 0x0, 0xed2c7f8abb74f3c9, 0x8f0215d93407b907, 0x7a28ae7c374e8027, 0xb869ce287cabd893, 0x191cada, 0x4, ...)
	/Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/crypto/hd/hdpath.go:192 +0x587
github.com/cosmos/cosmos-sdk/crypto/hd_test.TestDerivePrivateKeyForPath(0xc000103080)
	/Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/crypto/hd/hdpath_test.go:286 +0x4d
testing.tRunner(0xc000103080, 0x1a80808)
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1194 +0xef
created by testing.(*T).Run
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1239 +0x2b3
exit status 2
FAIL	github.com/cosmos/cosmos-sdk/crypto/hd	0.296s

Version

All versions with this code

Cause

This code

for _, part := range parts {
// do we have an apostrophe?
harden := part[len(part)-1:] == "'"

assumes that ALL the parts will have a non-empty string, but that's clearly not true, if a path with a trailing slash were passed it, it'd crash.

Remedy

Before that part[len(part)-1] == "'", we need to check that the segment is non-empty. There is 2 fold remedy:

  • replace "//" with "/"
  • strip trailing and starting slashes

Let's please backport this change to all releases like Stargate et al.

For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
odeke-em added a commit that referenced this issue Feb 17, 2021
…ng slashes

Detected during my audit, right before fuzzing, the code that
checked for presence of hyphens per path segment assumed that
the part would always be non-empty. However, with paths such as:
* m/4/
* /44/
* m/4///

it'd panic with a runtime slice out of bounds.

With this new change, we now:
* firstly strip the right trailing slash
* on finding any empty segments of a path return an error

Fixes #8557
@mergify mergify bot closed this as completed in #8607 Feb 17, 2021
mergify bot pushed a commit that referenced this issue Feb 17, 2021
…ng slashes (#8607)

Detected during my audit, right before fuzzing, the code that
checked for presence of hyphens per path segment assumed that
the part would always be non-empty. However, with paths such as:
* m/4/
* /44/
* m/4///

it'd panic with a runtime slice out of bounds.

With this new change, we now:
* firstly strip the right trailing slash
* on finding any empty segments of a path return an error

Fixes #8557
mergify bot pushed a commit that referenced this issue Feb 17, 2021
…ng slashes (#8607)

Detected during my audit, right before fuzzing, the code that
checked for presence of hyphens per path segment assumed that
the part would always be non-empty. However, with paths such as:
* m/4/
* /44/
* m/4///

it'd panic with a runtime slice out of bounds.

With this new change, we now:
* firstly strip the right trailing slash
* on finding any empty segments of a path return an error

Fixes #8557

(cherry picked from commit f970056)
alessio pushed a commit that referenced this issue Feb 17, 2021
…ng slashes (#8607) (#8608)

Detected during my audit, right before fuzzing, the code that
checked for presence of hyphens per path segment assumed that
the part would always be non-empty. However, with paths such as:
* m/4/
* /44/
* m/4///

it'd panic with a runtime slice out of bounds.

With this new change, we now:
* firstly strip the right trailing slash
* on finding any empty segments of a path return an error

Fixes #8557

(cherry picked from commit f970056)

Co-authored-by: Emmanuel T Odeke <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants