wg: genkey: account for short reads of /dev/urandom

Apparently Haiku has a misbehaving /dev/urandom.

While we're at it, simplify the function signature to completely succeed
or completely fail and make sure the caller checks the result.

Reported-by: Alexander von Gluck IV <kallisti5@unixzen.com>
Nitpicked-by: Aaron Jones <aaronmdjones@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2019-02-28 23:37:24 +01:00
parent 2c6cabd73d
commit 74a6f97b7a
2 changed files with 35 additions and 18 deletions

View File

@ -1751,16 +1751,21 @@ void wg_generate_private_key(wg_key private_key)
void wg_generate_preshared_key(wg_key preshared_key) void wg_generate_preshared_key(wg_key preshared_key)
{ {
ssize_t ret; ssize_t ret;
size_t i;
int fd; int fd;
#if defined(__OpenBSD__) || (defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12) || (defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)))
#if defined(__NR_getrandom) if (!getentropy(preshared_key, sizeof(wg_key)))
ret = syscall(__NR_getrandom, preshared_key, sizeof(wg_key), 0); return;
if (ret == sizeof(wg_key)) #endif
#if defined(__NR_getrandom) && defined(__linux__)
if (syscall(__NR_getrandom, preshared_key, sizeof(wg_key), 0) == sizeof(wg_key))
return; return;
#endif #endif
fd = open("/dev/urandom", O_RDONLY); fd = open("/dev/urandom", O_RDONLY);
assert(fd >= 0); assert(fd >= 0);
ret = read(fd, preshared_key, sizeof(wg_key)); for (i = 0; i < sizeof(wg_key); i += ret) {
close(fd); ret = read(fd, preshared_key + i, sizeof(wg_key) - i);
assert(ret == sizeof(wg_key)); assert(ret > 0);
}
close(fd);
} }

View File

@ -5,6 +5,7 @@
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -27,29 +28,40 @@
#include "encoding.h" #include "encoding.h"
#include "subcommands.h" #include "subcommands.h"
static inline ssize_t get_random_bytes(uint8_t *out, size_t len) static inline bool __attribute__((__warn_unused_result__)) get_random_bytes(uint8_t *out, size_t len)
{ {
ssize_t ret; ssize_t ret = 0;
size_t i;
int fd; int fd;
if (len > 256) {
errno = EOVERFLOW;
return false;
}
#if defined(__OpenBSD__) || (defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12) || (defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25))) #if defined(__OpenBSD__) || (defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12) || (defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)))
ret = getentropy(out, len); if (!getentropy(out, len))
if (!ret) return true;
return len;
#endif #endif
#if defined(__NR_getrandom) && defined(__linux__) #if defined(__NR_getrandom) && defined(__linux__)
ret = syscall(__NR_getrandom, out, len, 0); if (syscall(__NR_getrandom, out, len, 0) == (ssize_t)len)
if (ret >= 0) return true;
return ret;
#endif #endif
fd = open("/dev/urandom", O_RDONLY); fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) if (fd < 0)
return fd; return false;
ret = read(fd, out, len); for (errno = 0, i = 0; i < len; i += ret, ret = 0) {
ret = read(fd, out + i, len - i);
if (ret <= 0) {
ret = errno ? -errno : -EIO;
break;
}
}
close(fd); close(fd);
return ret; errno = -ret;
return i == len;
} }
int genkey_main(int argc, char *argv[]) int genkey_main(int argc, char *argv[])
@ -66,7 +78,7 @@ int genkey_main(int argc, char *argv[])
if (!fstat(STDOUT_FILENO, &stat) && S_ISREG(stat.st_mode) && stat.st_mode & S_IRWXO) if (!fstat(STDOUT_FILENO, &stat) && S_ISREG(stat.st_mode) && stat.st_mode & S_IRWXO)
fputs("Warning: writing to world accessible file.\nConsider setting the umask to 077 and trying again.\n", stderr); fputs("Warning: writing to world accessible file.\nConsider setting the umask to 077 and trying again.\n", stderr);
if (get_random_bytes(key, WG_KEY_LEN) != WG_KEY_LEN) { if (!get_random_bytes(key, WG_KEY_LEN)) {
perror("getrandom"); perror("getrandom");
return 1; return 1;
} }