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

RandomAlphaString fix for 32-bit #200

Merged
merged 2 commits into from
Sep 5, 2023
Merged

RandomAlphaString fix for 32-bit #200

merged 2 commits into from
Sep 5, 2023

Conversation

abe-winter
Copy link
Member

@abe-winter abe-winter commented Sep 5, 2023

In 32-bit builds, RandomAlphaString can crash with 'index out of range'

Note: golang won't let you compile a simple array[-1] because it knows negative numbers shouldn't be array indices, but this sample will repro if you want to see the failure:

package main
import "math"
import "math/big"
import "crypto/rand"
func main() {
	arr := []int{1,2,3}
	for i := 0; i < 20; i++ {
		val, _ := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
		index := int(val.Int64()) % len(arr)
		println("index", index, "val", arr[index])
	}
}

Then

GOARCH=386 go build main.go && main

(if you're on M1 mac, do GOARCH=arm)

@abe-winter abe-winter added the safe to test Mark as safe to test label Sep 5, 2023
string.go Outdated
@@ -28,16 +28,16 @@ func RandomAlphaString(size int) string {
if err != nil {
panic(err)
}
val := int(valBig.Int64())
val := valBig.Int64()
Copy link
Member

@dgottlieb dgottlieb Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the very local fix. I'm not sure I grok what a 32-bit build has to do with int(bigInt.Int64()) -> a negative number. I'm curious if we know whether we're:

  • Working around a golang bug or
  • We mis-read/neglected reading the golang documentation describing this behavior of big.Int

So as a devil's advocate to the fix here, I feel the complexity of maintaining this is the use of a cryptographically secure number generator. Which goes through this bigint stuff.

I don't think we need cryptographic secure number generation here. There's no (documented) production use of the function in rdk/app/goutils. And if we do need cryptographic generation, we shouldn't expose some string-ifying function for it and let users dictate how long the output should be.

Can we instead just do:

alphas = fmt.Sprintf("%s%s", alphaLowers, strings.ToUpper(alphaLowers))

...
idx := math/rand.Intn(len(alphas))
val := alphas[idx]

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int(random_positive_64_bit_number) on a 32-bit platform can be negative from truncation I think

Copy link
Member

@dgottlieb dgottlieb Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see now. The reproducer is much more clear about what's going on than this code. Thanks for adding that to the PR description!

If my above suggestion is too much code slinging for a "just fix it" issue can we just change:

	maxIntBig       = big.NewInt(math.MaxInt64)

to MaxInt32 instead?

The 64-bit numbers here are just to make the selection "as uniform as possible". 32-bit is perfectly fine to be effectively uniform selection.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ abe-winter
❌ Abe Winter


Abe Winter seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@viambot viambot removed the safe to test Mark as safe to test label Sep 5, 2023
@github-actions
Copy link

github-actions bot commented Sep 5, 2023

For security reasons, this PR must be labeled with safe to test in order for tests to run.

@abe-winter abe-winter added the safe to test Mark as safe to test label Sep 5, 2023
Copy link
Member

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@abe-winter abe-winter changed the title RandomAlphaString fix fot 32-bit RandomAlphaString fix for 32-bit Sep 5, 2023
@abe-winter abe-winter merged commit 300b46e into main Sep 5, 2023
7 checks passed
@abe-winter abe-winter deleted the fix-random-32bit branch September 5, 2023 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
safe to test Mark as safe to test
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants