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

ref: use SmallRng #119

Closed
wants to merge 1 commit into from
Closed

ref: use SmallRng #119

wants to merge 1 commit into from

Conversation

joshuarli
Copy link

Alternative to #118.

My understanding is that SmallRng is much smaller and cheaper to initialize than the thread_rng, which is a thread-local ThreadRng (StdRng, with the added overhead of ReseedingRng because it needs to be secure). SmallRng is also faster than StdRng. This is all because SmallRng is not a CSPRNG.

It's worth noting that unreleased rand at this time also shakes rand_chacha from the dependency tree, which is really nice. I believe rand's planning on releasing 0.8, so if this PR looks good, I'd recommend blocking on that. Issues: rust-random/rand#965, rust-random/rand#938.

@Stebalien
Copy link
Owner

Nice!

Comment on lines +16 to +18
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
.sample_iter(Alphanumeric)
.take(rand_len)
.map(char::from)
.collect::<String>()

Copy link
Owner

Choose a reason for hiding this comment

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

That's still going to allocate, unfortunately.

Copy link
Contributor

@taiki-e taiki-e Dec 31, 2020

Choose a reason for hiding this comment

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

I'll look for a way to avoid the allocation.

EDIT: See #119 (comment)

@Stebalien
Copy link
Owner

In addition to the compile issue above, I'm a bit concerned about calling getrandom every time. I assume that'll slow things down significantly.

@Stebalien
Copy link
Owner

One solution would be to put a smallrng in a thread-local variable.

@joshuarli
Copy link
Author

In addition to the compile issue above, I'm a bit concerned about calling getrandom every time. I assume that'll slow things down significantly.

Oh yeah, I don't remember considering performance at the time at all. You can also close this if you'd like.

Comment on lines +14 to +19
let small_rng = SmallRng::from_entropy();
buf.push(small_rng
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
);
Copy link
Contributor

Choose a reason for hiding this comment

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

We can avoid allocation by using char::encode_utf8.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=421dfa2eebe631075a03b06e08c6e7cf

Suggested change
let small_rng = SmallRng::from_entropy();
buf.push(small_rng
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
);
let mut tmp = [0; 1];
let small_rng = SmallRng::from_entropy();
small_rng
.sample_iter(Alphanumeric)
.take(rand_len)
.for_each(|c| buf.push((c as char).encode_utf8(&mut tmp)));

Can we probably resolve performance issues in combination with putting a smallrng in a thread-local variable?

@Stebalien
Copy link
Owner

Merged in #141.

@Stebalien Stebalien closed this Apr 9, 2021
@joshuarli joshuarli deleted the ref/use-small-rng branch April 11, 2021 20:57
@jwgarber
Copy link

jwgarber commented Jun 2, 2021

I have concerns about using a non-CSPRNG to generate temporary file names. For example, the Cert C Coding standard on temporary files warns that

Privileged programs that create temporary files in world-writable directories can be exploited to overwrite protected system files. An attacker who can predict the name of a file created by a privileged program can create a symbolic link (with the same name as the file used by the program) to point to a protected system file. Unless the privileged program is coded securely, the program will follow the symbolic link instead of opening or creating the file that it is supposed to be using. As a result, a protected system file to which the symbolic link points can be overwritten when the program is executed [HP 2003]. Unprivileged programs can be similarly exploited to overwrite protected user files.

This OWASP page also goes into great detail of the potential security implications of using temporary files with predictable names.

The documentation for SmallRng states that it is not a good choice when security against predictions is important (which is the case here), and recommends using StdRng instead, which is cryptographically secure and thus unpredictable. Considering that we do not know all use cases where this crate will be used, I suggest we err on the side of caution and use StdRng or ThreadRng to generate the file names.

@Stebalien
Copy link
Owner

Yeah, fair enough. Using StdRng is probably overkill but the filenames should be unpredictable.

I'm going to try to make using insecure rand opt-in... but I'm having some trouble with the feature specification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants