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

Limit OsRng to a single file handle when reading from a file #239

Merged
merged 2 commits into from
Jan 30, 2018

Conversation

dhardy
Copy link
Member

@dhardy dhardy commented Jan 16, 2018

Fix #234.

I'm not entirely happy with the code; it uses unwrap a lot and requires use of unsafe to access the mutex, and it also requires Rust 1.22; anything older gets:

error[E0493]: destructors in statics are an unstable feature

lazy_static gets around this by using a Box cast to a pointer which never gets freed.

The reasons I didn't use lazy_static are:

  • it would be difficult to encapsulate the logic in ReadRng since lazy_static doesn't allow a constructor or argument to be passed
  • it would still require the mutex to safeguard access from other threads

@burdges
Copy link
Contributor

burdges commented Jan 16, 2018

I'd think the Once and outer Option can be eliminated once const fn advances enough to cover Mutex::new, but that'll be quite a while.

@dhardy
Copy link
Member Author

dhardy commented Jan 16, 2018

As it is this requires using a very recent compiler compared to the 1.15 we support now, so I'm hesitant adding it for that reason. Rand v0.5 will have quite a few changes anyway though so may be a good time to update the compiler.

Thanks for taking a look.

@dhardy
Copy link
Member Author

dhardy commented Jan 23, 2018

Rebased. I plan to merge this with the bump to the minimum Rust version, and probably with all the unwrap usage (I don't see a better alternative). Any comments?

@pitdicker
Copy link
Contributor

When you made this PR I was curious how often reading from /dev/urandom is really needed anymore, see rust-lang/rfcs#2152 (comment).

How many distributions support old(er) pre-3.17 Linux kernel versions?

  • Debian Jessie uses 3.16 and receives security support until may 2018.
  • Ubuntu 14.04 (Trusty Tar) uses 3.13, and is supported until April 2019.
  • RHEL 6. uses kernel 2.6.32, supported until November 2020.
  • RHEL 7.4 kernel 3.10, but is said to support getrandom (https://access.redhat.com/blogs/766093/posts/3050871).

So still necessary on some old but still supported Linux distro's.

MacOS should switch to the same method as iOS, and not read from /dev/urandom.

Then there are Dragonfly BSD and NetBSD that don't have a better interface, and Redox. Not sure how Redox handles file descriptors, and maybe they are willing to provide another interface?

If I forget about the BSD's for a moment, I have a bit mixed feelings about requiring a newer compiler version and extra complexity to support an edge case on older platforms. I am not saying I disagree, just pointing out when this code is going to get used.

@pitdicker
Copy link
Contributor

Is it possible to return an new error instead of unwrapping? For the rest I don't feel confident to review, do you want to cc someone?

@dhardy
Copy link
Member Author

dhardy commented Jan 23, 2018

@burdges are you happy enough reviewing this code? (I guess you already did; it hasn't changed.)

@pitdicker yes, but all 5 unwraps would be code logic errors if they failed, and consequently OsRng would be permanently unavailable. I don't feel like panic from unwrap is such a bad option if somehow the logic breaks; it at least forces visibility of the issue.

Note: the Servo devs had a real problem here frequently running out of file handles when running tests.

This is required by the new code (but see lazy_static code
for a workaround supporting older Rustc).
@dhardy
Copy link
Member Author

dhardy commented Jan 29, 2018

@asajeffrey @jdm review request

@dhardy dhardy mentioned this pull request Jan 30, 2018
@dhardy dhardy merged commit 927ab7b into rust-random:master Jan 30, 2018
@cuviper
Copy link
Contributor

cuviper commented Feb 6, 2018

The rustc bump becomes doubly painful when you have rand-0.3.22 now depending on rand-0.4.

@dhardy
Copy link
Member Author

dhardy commented Feb 6, 2018

This was merged into master, which will be released as 0.5 eventually, not 0.4. There are several breaking changes between 0.4 and 0.5 so I don't anticipate the same kind of compatibility release (possibly there will be partial compatibility by opting into a feature).

@cuviper
Copy link
Contributor

cuviper commented Feb 6, 2018

@dhardy Ah, sorry! I misunderstood the errors we were getting from rayon using rand-0.3, which now uses rand-0.4. I actually was previously aware that rand-0.4 required rust 1.15, but I guess I forgot this and then jumped to the wrong conclusion when searching the reason for this new break.

@abreis abreis mentioned this pull request Mar 22, 2018
pitdicker pushed a commit that referenced this pull request Apr 4, 2018
Limit OsRng to a single file handle when reading from a file
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.

5 participants