HADOOP-11056. OsSecureRandom.setConf() might leak file descriptors. Contributed by Yongjun Zhang.

(cherry picked from commit 8f1a668575)
This commit is contained in:
Colin Patrick Mccabe 2014-09-03 19:35:39 -07:00
parent 23c41897cb
commit 7b287d5ba6
3 changed files with 37 additions and 4 deletions

View File

@ -346,6 +346,9 @@ Release 2.6.0 - UNRELEASED
HADOOP-8815. RandomDatum needs to override hashCode(). HADOOP-8815. RandomDatum needs to override hashCode().
(Brandon Li via suresh) (Brandon Li via suresh)
HADOOP-11056. OsSecureRandom.setConf() might leak file descriptors. (yzhang
via cmccabe)
BREAKDOWN OF HDFS-6134 AND HADOOP-10150 SUBTASKS AND RELATED JIRAS BREAKDOWN OF HDFS-6134 AND HADOOP-10150 SUBTASKS AND RELATED JIRAS
HADOOP-10734. Implement high-performance secure random number sources. HADOOP-10734. Implement high-performance secure random number sources.

View File

@ -23,6 +23,8 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Random; import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configurable; import org.apache.hadoop.conf.Configurable;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
@ -37,6 +39,8 @@ import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_SECURITY
*/ */
@InterfaceAudience.Private @InterfaceAudience.Private
public class OsSecureRandom extends Random implements Closeable, Configurable { public class OsSecureRandom extends Random implements Closeable, Configurable {
public static final Log LOG = LogFactory.getLog(OsSecureRandom.class);
private static final long serialVersionUID = 6391500337172057900L; private static final long serialVersionUID = 6391500337172057900L;
private transient Configuration conf; private transient Configuration conf;
@ -72,12 +76,20 @@ public class OsSecureRandom extends Random implements Closeable, Configurable {
HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_KEY, HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_KEY,
HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_DEFAULT); HADOOP_SECURITY_SECURE_RANDOM_DEVICE_FILE_PATH_DEFAULT);
File randomDevFile = new File(randomDevPath); File randomDevFile = new File(randomDevPath);
try { try {
close();
this.stream = new FileInputStream(randomDevFile); this.stream = new FileInputStream(randomDevFile);
fillReservoir(0);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
try {
fillReservoir(0);
} catch (RuntimeException e) {
close();
throw e;
}
} }
@Override @Override
@ -109,7 +121,10 @@ public class OsSecureRandom extends Random implements Closeable, Configurable {
} }
@Override @Override
synchronized public void close() throws IOException { synchronized public void close() {
stream.close(); if (stream != null) {
IOUtils.cleanup(LOG, stream);
stream = null;
}
} }
} }

View File

@ -22,6 +22,7 @@ import java.util.Arrays;
import org.apache.commons.lang.SystemUtils; import org.apache.commons.lang.SystemUtils;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.util.Shell.ShellCommandExecutor;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
@ -136,4 +137,18 @@ public class TestOsSecureRandom {
} }
random.close(); random.close();
} }
@Test(timeout=120000)
public void testOsSecureRandomSetConf() throws IOException {
Assume.assumeTrue(SystemUtils.IS_OS_LINUX);
OsSecureRandom random = new OsSecureRandom();
for(int n = 0; n < 10; ++n) {
random.setConf(new Configuration());
String[] scmd = new String[] {"/bin/sh", "-c", "lsof | wc -l"};
ShellCommandExecutor sce = new ShellCommandExecutor(scmd);
sce.execute();
System.out.println("==lsof result " + n + ":");
System.out.println(sce.getOutput());
}
}
} }