summaryrefslogtreecommitdiffstats
path: root/crypto/rand
diff options
context:
space:
mode:
authorAndy Polyakov <appro@openssl.org>2018-03-23 15:12:20 +0100
committerAndy Polyakov <appro@openssl.org>2018-03-27 19:55:54 +0200
commit242fcd695db6225ef98c5ad084e6f15ec5953158 (patch)
treedba35b81ce365e8309c66786efe13c9a54880daf /crypto/rand
parentf770d75b1cac264d6280ec7326277daff6965cbb (diff)
rand/randfile.c: permit non-regular files in RAND_load_file.
Apparently applications rely on RAND_load_file's ability to work with non-regular files, customarily with /dev/urandom, so that the ban was not exactly appropriate. Reviewed-by: Rich Salz <rsalz@openssl.org> Reviewed-by: Bernd Edlinger <bernd.edlinger@hotmail.de> Reviewed-by: Paul Dale <paul.dale@oracle.com> Reviewed-by: Tim Hudson <tjh@openssl.org> (Merged from https://github.com/openssl/openssl/pull/5737)
Diffstat (limited to 'crypto/rand')
-rw-r--r--crypto/rand/randfile.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/crypto/rand/randfile.c b/crypto/rand/randfile.c
index a979eb99a2..fa6f49e1b5 100644
--- a/crypto/rand/randfile.c
+++ b/crypto/rand/randfile.c
@@ -32,6 +32,8 @@
# define chmod _chmod
# define open _open
# define fdopen _fdopen
+# define fstat _fstat
+# define fileno _fileno
# endif
#endif
@@ -82,27 +84,45 @@ int RAND_load_file(const char *file, long bytes)
if (bytes == 0)
return 0;
-#ifndef OPENSSL_NO_POSIX_IO
- if (stat(file, &sb) < 0 || !S_ISREG(sb.st_mode)) {
- RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_NOT_A_REGULAR_FILE);
+ if ((in = openssl_fopen(file, "rb")) == NULL) {
+ RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_CANNOT_OPEN_FILE);
ERR_add_error_data(2, "Filename=", file);
return -1;
}
-#endif
- if ((in = openssl_fopen(file, "rb")) == NULL) {
- RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_CANNOT_OPEN_FILE);
+
+#ifndef OPENSSL_NO_POSIX_IO
+ if (fstat(fileno(in), &sb) < 0) {
+ RANDerr(RAND_F_RAND_LOAD_FILE, RAND_R_INTERNAL_ERROR);
ERR_add_error_data(2, "Filename=", file);
return -1;
}
+ if (!S_ISREG(sb.st_mode) && bytes < 0)
+ bytes = 256;
+#endif
+ /*
+ * Don't buffer, because even if |file| is regular file, we have
+ * no control over the buffer, so why would we want a copy of its
+ * contents lying around?
+ */
+ setbuf(in, NULL);
+
for ( ; ; ) {
if (bytes > 0)
n = (bytes < RAND_FILE_SIZE) ? (int)bytes : RAND_FILE_SIZE;
else
n = RAND_FILE_SIZE;
i = fread(buf, 1, n, in);
- if (i <= 0)
+#ifdef EINTR
+ if (ferror(in) && errno == EINTR){
+ clearerr(in);
+ if (i == 0)
+ continue;
+ }
+#endif
+ if (i == 0)
break;
+
RAND_add(buf, i, (double)i);
ret += i;