Two Problems:
1) Because fcntl.h is not included (in unix), O_CREAT is not available,
so the logic to attempt to create with 0600 cannot happen at file
creation time.
2) Because we fopen() with mode "wb", the file gets momentarily
truncated, so when we are running multiple instances of openssl, we have
the potential of spurious, dreaded "unable to write 'random state'"
error message, even we we follow the the instructions in the FAQ and
create $HOME/.rnd.
Reproduce the Problem:
On an AIX box (or whatever platform which lacks a good /dev/urandom), do
the following (in bash):
nconcurrent=40
i=0; while [ $i -lt $nconcurrent ]; do let i=i+1
openssl rand -out rand.$i 10 &
done
Most will succeed, but some will fail with sufficient $nconcurrent as
described above.
Solutions:
1) In the patches below, I #include <fcntl.h>, wrapped by
#ifndef NO_FCNTL_H. I'm mostly unaware of why we wouldn't more freely
include fcntl.h, so maybe e_os.h needs to be tweaked for some platforms.
2) I call open() with O_CREAT|O_RDWR, and then call fdopen() and/or
fopen() with mode "wb+" -- this prevents the truncation which is causing
the problem.
I consider it a much bigger problem that when two instances run at the
same time, one fails unpredictably, than that the the two instances
might be influenced with the same .rnd content.
Thanks,
james jurach
-*- begin patch 0.9.6i -*-
--- randfile.c.orig 2003-03-03 09:24:02.000000000 -0600
+++ randfile.c 2003-03-03 10:41:09.000000000 -0600
@@ -74,6 +74,10 @@
#else
# include <sys/stat.h>
#endif
+#ifndef NO_FCNTL_H
+# include <fcntl.h>
+#endif
+
#include <openssl/crypto.h>
#include <openssl/rand.h>
@@ -137,17 +141,17 @@
FILE *out = NULL;
int n;
-#if defined(O_CREAT) && !defined(WIN32)
+#if defined(O_CREAT) && defined(O_RDWR) && !defined(WIN32)
/* For some reason Win32 can't write to files created this way */
/* chmod(..., 0600) is too late to protect the file,
* permissions should be restrictive from the start */
- int fd = open(file, O_CREAT, 0600);
+ int fd = open(file, O_CREAT|O_RDWR, 0600);
if (fd != -1)
- out = fdopen(fd, "wb");
+ out = fdopen(fd, "wb+");
#endif
if (out == NULL)
- out = fopen(file,"wb");
+ out = fopen(file,"wb+");
if (out == NULL) goto err;
#ifndef NO_CHMOD
-*- end patch 0.9.6i -*-
-*- begin patch 0.9.7a -*-
--- randfile.c.orig 2003-03-03 10:51:19.000000000 -0600
+++ randfile.c 2003-03-03 10:53:44.000000000 -0600
@@ -71,6 +71,9 @@
#ifndef NO_SYS_TYPES_H
# include <sys/types.h>
#endif
+#ifndef NO_FCNTL_H
+# include <fcntl.h>
+#endif
#ifdef MAC_OS_pre_X
# include <stat.h>
#else
@@ -136,17 +139,17 @@
FILE *out = NULL;
int n;
-#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
+#if defined(O_CREAT) && defined(O_RDWR) && !defined(OPENSSL_SYS_WIN32)
/* For some reason Win32 can't write to files created this way */
/* chmod(..., 0600) is too late to protect the file,
* permissions should be restrictive from the start */
- int fd = open(file, O_CREAT, 0600);
+ int fd = open(file, O_CREAT|O_RDWR, 0600);
if (fd != -1)
- out = fdopen(fd, "wb");
+ out = fdopen(fd, "wb+");
#endif
if (out == NULL)
- out = fopen(file,"wb");
+ out = fopen(file,"wb+");
if (out == NULL) goto err;
#ifndef NO_CHMOD
-*- end patch 0.9.7a -*-
______________________________________________________________________
OpenSSL Project http://www.openssl.org
Development Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]