# extended support for (re-)seeding the OpenSSL PRNG from /dev/random # bnc#703221, FATE#312172 Index: b/audit-bsm.c =================================================================== --- a/audit-bsm.c +++ b/audit-bsm.c @@ -509,4 +509,10 @@ audit_generate_ephemeral_server_key(cons { /* not implemented */ } + +void +audit_linux_prng_seed(long bytes, const char *rf) +{ + /* not implemented */ +} #endif /* BSM */ Index: b/audit-linux.c =================================================================== --- a/audit-linux.c +++ b/audit-linux.c @@ -407,4 +407,26 @@ audit_generate_ephemeral_server_key(cons if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) error("cannot write into audit"); } + +void +audit_linux_prng_seed(long bytes, const char *rf) +{ + char buf[AUDIT_LOG_SIZE]; + int audit_fd, audit_ok; + + snprintf(buf, sizeof(buf), "op=prng_seed kind=server bytes=%li source=%s ", bytes, rf); + audit_fd = audit_open(); + if (audit_fd < 0) { + if (errno != EINVAL && errno != EPROTONOSUPPORT && + errno != EAFNOSUPPORT) + error("cannot open audit"); + return; + } + audit_ok = audit_log_user_message(audit_fd, AUDIT_CRYPTO_PARAM_CHANGE_USER, + buf, NULL, 0, NULL, 1); + audit_close(audit_fd); + /* do not abort if the error is EPERM and sshd is run as non root user */ + if ((audit_ok < 0) && ((audit_ok != -1) || (getuid() == 0))) + error("cannot write into audit"); +} #endif /* USE_LINUX_AUDIT */ Index: b/audit.c =================================================================== --- a/audit.c +++ b/audit.c @@ -309,5 +309,11 @@ audit_generate_ephemeral_server_key(cons { debug("audit create ephemeral server key euid %d fingerprint %s", geteuid(), fp); } + +void +audit_linux_prng_seed(long bytes, const char *rf) +{ + debug("audit PRNG seed euid %d bytes %li source %s", geteuid(), bytes, rf); +} # endif /* !defined CUSTOM_SSH_AUDIT_EVENTS */ #endif /* SSH_AUDIT_EVENTS */ Index: b/audit.h =================================================================== --- a/audit.h +++ b/audit.h @@ -68,5 +68,6 @@ void audit_session_key_free(int ctos); void audit_session_key_free_body(int ctos, pid_t, uid_t); void audit_destroy_sensitive_data(const char *, pid_t, uid_t); void audit_generate_ephemeral_server_key(const char *); +void audit_linux_prng_seed(long, const char *); #endif /* _SSH_AUDIT_H */ Index: b/entropy.c =================================================================== --- a/entropy.c +++ b/entropy.c @@ -54,6 +54,7 @@ #include "pathnames.h" #include "log.h" #include "buffer.h" +#include "openbsd-compat/port-linux.h" /* * Portable OpenSSH PRNG seeding: @@ -229,6 +230,9 @@ seed_rng(void) memset(buf, '\0', sizeof(buf)); #endif /* OPENSSL_PRNG_ONLY */ + + linux_seed(); + if (RAND_status() != 1) fatal("PRNG is not seeded"); } Index: b/openbsd-compat/Makefile.in =================================================================== --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in @@ -20,7 +20,7 @@ OPENBSD=base64.o basename.o bcrypt_pbkdf COMPAT=arc4random.o bsd-asprintf.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o getrrsetbyname-ldns.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-poll.o bsd-setres_id.o bsd-snprintf.o bsd-statvfs.o bsd-waitpid.o fake-rfc2553.o openssl-compat.o xmmap.o xcrypt.o kludge-fd_set.o -PORTS=port-aix.o port-irix.o port-linux.o port-solaris.o port-tun.o port-uw.o +PORTS=port-aix.o port-irix.o port-linux.o port-linux-prng.o port-solaris.o port-tun.o port-uw.o .c.o: $(CC) $(CFLAGS) $(CPPFLAGS) -c $< Index: b/openbsd-compat/port-linux-prng.c =================================================================== --- /dev/null +++ b/openbsd-compat/port-linux-prng.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2011 Jan F. Chadima + * (c) 2011 Petr Cerny + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Linux-specific portability code - prng support + */ + +#include "includes.h" +#include "defines.h" + +#include +#include +#include +#include +#include + +#include "log.h" +#include "port-linux.h" +#include "audit.h" + +#define RNG_BYTES_DEFAULT 6L +#define RNG_ENV_VAR "SSH_USE_STRONG_RNG" + +long rand_bytes = 0; +char *rand_file = NULL; + +static void +linux_seed_init(void) +{ + long elen = 0; + char *env = getenv(RNG_ENV_VAR); + + if (env) { + errno = 0; + elen = strtol(env, NULL, 10); + if (errno) { + debug("bogus value in the %s environment variable, using default %li\n", + RNG_ENV_VAR, RNG_BYTES_DEFAULT); + } + } + + if (elen) + rand_file = "/dev/random"; + else + rand_file = "/dev/urandom"; + + rand_bytes = MAX(elen, RNG_BYTES_DEFAULT); +} + +void +linux_seed(void) +{ + long len; + if (!rand_file) + linux_seed_init(); + + errno = 0; + len = RAND_load_file(rand_file, rand_bytes); + if (len != rand_bytes) { + if (errno) + fatal ("cannot read from %s, %s", random, strerror(errno)); + else + fatal ("EOF reading %s", random); + } +} Index: b/openbsd-compat/port-linux.h =================================================================== --- a/openbsd-compat/port-linux.h +++ b/openbsd-compat/port-linux.h @@ -19,6 +19,10 @@ #ifndef _PORT_LINUX_H #define _PORT_LINUX_H +extern long rand_bytes; +extern char *rand_file; +void linux_seed(void); + #ifdef WITH_SELINUX int ssh_selinux_enabled(void); void ssh_selinux_setup_pty(char *, const char *); Index: b/ssh-add.1 =================================================================== --- a/ssh-add.1 +++ b/ssh-add.1 @@ -171,6 +171,20 @@ to make this work.) Identifies the path of a .Ux Ns -domain socket used to communicate with the agent. +.It Ev SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .El .Sh FILES .Bl -tag -width Ds Index: b/ssh-agent.1 =================================================================== --- a/ssh-agent.1 +++ b/ssh-agent.1 @@ -194,6 +194,23 @@ sockets used to contain the connection t These sockets should only be readable by the owner. The sockets should get automatically removed when the agent exits. .El +.Sh ENVIRONMENT +.Bl -tag -width Ds -compact +.Pp +.It Pa SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .Sh SEE ALSO .Xr ssh 1 , .Xr ssh-add 1 , Index: b/ssh-keygen.1 =================================================================== --- a/ssh-keygen.1 +++ b/ssh-keygen.1 @@ -842,6 +842,23 @@ Contains Diffie-Hellman groups used for The file format is described in .Xr moduli 5 . .El +.Sh ENVIRONMENT +.Bl -tag -width Ds -compact +.Pp +.It Pa SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .Sh SEE ALSO .Xr ssh 1 , .Xr ssh-add 1 , Index: b/ssh-keysign.8 =================================================================== --- a/ssh-keysign.8 +++ b/ssh-keysign.8 @@ -80,6 +80,23 @@ must be set-uid root if host-based authe If these files exist they are assumed to contain public certificate information corresponding with the private keys above. .El +.Sh ENVIRONMENT +.Bl -tag -width Ds -compact +.Pp +.It Pa SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .Sh SEE ALSO .Xr ssh 1 , .Xr ssh-keygen 1 , Index: b/ssh.1 =================================================================== --- a/ssh.1 +++ b/ssh.1 @@ -1415,6 +1415,20 @@ For more information, see the .Cm PermitUserEnvironment option in .Xr sshd_config 5 . +.It Ev SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .Sh FILES .Bl -tag -width Ds -compact .It Pa ~/.rhosts Index: b/sshd.8 =================================================================== --- a/sshd.8 +++ b/sshd.8 @@ -944,6 +944,23 @@ concurrently for different ports, this c started last). The content of this file is not sensitive; it can be world-readable. .El +.Sh ENVIRONMENT +.Bl -tag -width Ds -compact +.Pp +.It Pa SSH_USE_STRONG_RNG +The reseeding of the OpenSSL random generator is usually done from +.Cm /dev/urandom . +If the +.Cm SSH_USE_STRONG_RNG +environment variable is set to value other than +.Cm 0 +the OpenSSL random generator is reseeded from +.Cm /dev/random . +The number of bytes read is defined by the SSH_USE_STRONG_RNG value. +Minimum is 6 bytes. +This setting is not recommended on the computers without the hardware +random generator because insufficient entropy causes the connection to +be blocked until enough entropy is available. .Sh SEE ALSO .Xr scp 1 , .Xr sftp 1 , Index: b/sshd.c =================================================================== --- a/sshd.c +++ b/sshd.c @@ -55,6 +55,8 @@ #endif #include "openbsd-compat/sys-tree.h" #include "openbsd-compat/sys-queue.h" +#include "openbsd-compat/port-linux.h" + #include #include @@ -214,6 +216,13 @@ struct { } sensitive_data; /* + * Every RESEED_AFTERth connection triggers call to linux_seed() to re-seed the + * random pool. + */ +#define RESEED_AFTER 100 +static int re_seeding_counter = RESEED_AFTER; + +/* * Flag indicating whether the RSA server key needs to be regenerated. * Is set in the SIGALRM handler and cleared when the key is regenerated. */ @@ -1400,6 +1409,11 @@ server_accept_loop(int *sock_in, int *so startups++; break; } + if(!(--re_seeding_counter)) { + re_seeding_counter = RESEED_AFTER; + linux_seed(); + audit_linux_prng_seed(rand_bytes, rand_file); + } /* * Got connection. Fork a child to handle it, unless