okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

SecurityException: Failed to read sufficient bytes from /dev/urandom

Open tepamid opened this issue 9 years ago • 1 comments

In our app we are using OkHttp WebSockets from ReactNative. And sometimes okhttp.ws. WebSocketCall.create method crash with "SecurityException: Failed to read sufficient bytes from /dev/urandom. Expected: 1024, actual: 0"

The crash happens only on Android 5.x.x. After some source codes research I found out that in Android 5.x.x other Security Provider - OpenSSL than is used in Android 4.x and Android 6+

OpenSSL uses RAND_load_file function (https://github.com/openssl/openssl/blob/6974fca49d9d0b110c02c83a7bbe01907472ac5e/crypto/rand/randfile.c) to read entropy stored in file /dev/urandom in order to initialize pseudo-random number generator with a truly random seed. Apparently in some cases either file can't be opened for read or only 0 bytes are read.

Unfortunately I can't understand the root cause why only 0 bytes are read or file fails to be opened.

However, I think that in WebSocketCall you should use not a default SecureRandom constructor, but rather instantiate SecureRandom with a more reliable Security Provider like those used in Android 4 or Android 6 (need to derive from SecureRandom and override protected constructor).

The problem reproduces on a range of Android 5.x.x devices. Here's top: 8% Redmi Note 3 8% SM-N9005 6% Lenovo A6020a40 5% LG-H955 5% m2 69% 40 other machines

Here's the complete exception stacktrace: com.android.org.conscrypt.OpenSSLRandom.seedOpenSSLPRNGFromLinuxRNG in OpenSSLRandom.java::75 com.android.org.conscrypt.OpenSSLRandom.selfSeedIfNotSeeded in OpenSSLRandom.java::61 com.android.org.conscrypt.OpenSSLRandom.engineNextBytes in OpenSSLRandom.java::37 java.security.SecureRandom.nextBytes in SecureRandom.java::271 okhttp3.ws.WebSocketCall. in WebSocketCall.java::64 okhttp3.ws.WebSocketCall. in WebSocketCall.java::54 okhttp3.ws.WebSocketCall.create in WebSocketCall.java::46 com.facebook.react.modules.websocket.WebSocketModule.connect in WebSocketModule.java::115 java.lang.reflect.Method.invoke in Method.java::-2 java.lang.reflect.Method.invoke in Method.java::372 com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke in BaseJavaModule.java::319 com.facebook.react.cxxbridge.JavaModuleWrapper.invoke in JavaModuleWrapper.java::139 com.facebook.react.bridge.queue.NativeRunnable.run in NativeRunnable.java::-2 android.os.Handler.handleCallback in Handler.java::815 android.os.Handler.dispatchMessage in Handler.java::104 com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage in MessageQueueThreadHandler.java::31 android.os.Looper.loop in Looper.java::194 com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run in MessageQueueThreadImpl.java::196 java.lang.Thread.run in Thread.java::831

tepamid avatar Jan 24 '17 15:01 tepamid

That’s odd. As specified /dev/urandom is always supposed to provide entropy.

swankjesse avatar Jan 24 '17 15:01 swankjesse