<div dir="ltr">While looking at NativePRNG, I filed<div><br></div><div><a href="https://bugs.openjdk.java.net/browse/JDK-8047769">https://bugs.openjdk.java.net/browse/JDK-8047769</a><br><br>SecureRandom should be more frugal with file descriptors<br>
<br>If I run this java program on Linux <br><br>public class SecureRandoms { </div><div><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"> public static void main(String[] args) throws Throwable { </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"> new java.security.SecureRandom(); </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"> } </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">} </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">it creates 6 file descriptors for /dev/random and /dev/urandom, as shown by: </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">strace -q -ff -e open java SecureRandoms |& grep /dev/ </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 5 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 6 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 7 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/random", O_RDONLY) = 8 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 9 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">[pid 20769] open("/dev/urandom", O_RDONLY) = 10 </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">Looking at jdk/src/solaris/classes/sun/security/provider/NativePRNG.java </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">it looks like 2 file descriptors are created for every variant of NativePRNG, whether or not they are ever used. Which is wasteful. In fact, you only ever need at most two file descriptors, one for /dev/random and one for /dev/urandom. </span><br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">
<br style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px"><span style="color:rgb(0,0,0);font-family:sans-serif;font-size:13px;line-height:17.000099182128906px">Further, it would be nice if the file descriptors were closed when idle and lazily re-created. Especially /dev/random should typically be used at startup and never thereafter.</span><br>
</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jun 20, 2014 at 7:59 AM, Alan Bateman <span dir="ltr"><<a href="mailto:Alan.Bateman@oracle.com" target="_blank">Alan.Bateman@oracle.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">On 20/06/2014 15:02, Peter Levart wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
And, as Martin pointed out, it seems to be used for tests that exercise particular responses from NameService API to test the behaviour of JDK classes. It would be a shame for those tests to go away.<br>
</blockquote></div>
We've been talking about removing it for many years because it has been so troublesome. If we really need to having something for testing then I don't think it needs to be general purpose, we can get right of the lookup at least.<span class="HOEnZb"><font color="#888888"><br>
<br>
-Alan.<br>
</font></span></blockquote></div><br></div>