Linux下熵池大小导致的一些问题
详解熵池
熵池本质上是若干字节。/proc/sys/kernel/random/entropy_avail中存储了熵池现在的大小,/proc/sys/kernel/random/poolsize是熵池的最大容量,单位都是bit。如果entropy_avail的值小于要产生的随机数bit数,那么/dev/random就会堵塞。
那么,为什么熵池不够用呢?
google了一下资料,熵池实际上是从各种noice source中获取数据,noice source可能是键盘事件、鼠标事件、设备时钟中等。linux内核从2.4升级到2.6时,处于安全性的考虑,废弃了一些source。source减少了,熵池补给的速度当然也变慢,进而不够用。
其实,通过消耗熵池,可以构造DOS攻击。原理很简单,熵池空了,依赖随机数的业务(SSL,加密等)就不能正常进行
实际遇到的问题
1.使用gpg生产4096位的密钥时,命令行中出现了如下内容并阻塞:
我们需要生成大量的随机字节。这个时候您可以多做些琐事(像是敲打键盘、移动
鼠标、读写硬盘之类的),这会让随机数字发生器有更好的机会获得足够的熵数。
2.在java中使用SecureRandom类生成随机数时,出现阻塞,如下面的代码:
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class TestSecureRandom {
public static void main(String[] args) throws NoSuchAlgorithmException {
SecureRandom secureRandom = new SecureRandom();//在Linux中正常
Long l = secureRandom.nextLong();
System.out.println(l);
SecureRandom secureRandom1 = SecureRandom.getInstanceStrong();//熵值低的时候,在Linux中阻塞
Long l1 = secureRandom1.nextLong();
System.out.println(l1);
}
}
3.tomcat启动很慢的问题,如下在"Conext:initialization completed"之前会被阻塞很长时间:
2019-10-15 15:15:03 [ INFO][localhost-startStop-1][ContextLoader]-[initWebApplicationContext] Root WebApplication
Context: initialization completed in 3377 msOct 15, 2019 3:15:36 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
补充熵池
我们可以通过安装rng-tools来补充熵池,下面先查看一下熵池大小,命令:
cat /proc/sys/kernel/random/entropy_avail
在centos7中,一般熵池大小在几十至二百之间,还是太小了
安装rng-tools:
yum install -y rng-tools
配置rng参数(我在centos7中测试,发现这一步可以省略):
vim /etc/sysconfig/rngd
添加内容:EXTRAOPTIONS="--rng-device /dev/urandom"
启动rng服务:
systemctl start rngd
systemctl enable rngd
再次查看熵池大小,会发现熵值为3000多了,上面出现的问题随即都得到了解决
tomcat问题的其他解决办法
有两种解决办法:
1)在Tomcat环境中解决
可以通过配置JRE使用非阻塞的Entropy Source。
在catalina.sh中加入这么一行:-Djava.security.egd=file:/dev/./urandom 即可。
加入后再启动Tomcat,整个启动耗时下降到Server startup in 2912 ms。
2)在JVM环境中解决
打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容:
securerandom.source=file:/dev/urandom
替换成
securerandom.source=file:/dev/./urandom
更多推荐
所有评论(0)