From d5ad99fed6b58c57b34ef33b7c94b9229c479759 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 25 Dec 2014 12:29:21 +0100 Subject: [PATCH] Retry if open(2) is interrupted; set the CLOEXEC flag as well. Also retry if read(2) returns EAGAIN. This shouldn't happen in blocking mode, but it can't hurt either. --- .../randombytes/salsa20/randombytes_salsa20_random.c | 11 +++++++++-- .../randombytes/sysrandom/randombytes_sysrandom.c | 11 +++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c index cb7f9e2f..3d7627d2 100644 --- a/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c +++ b/src/libsodium/randombytes/salsa20/randombytes_salsa20_random.c @@ -94,7 +94,7 @@ safe_read(const int fd, void * const buf_, size_t count) assert(count > (size_t) 0U); do { while ((readnb = read(fd, buf, count)) < (ssize_t) 0 && - errno == EINTR); /* LCOV_EXCL_LINE */ + (errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */ if (readnb < (ssize_t) 0) { return readnb; /* LCOV_EXCL_LINE */ } @@ -125,15 +125,22 @@ randombytes_salsa20_random_random_dev_open(void) int fd; do { - if ((fd = open(*device, O_RDONLY)) != -1) { + fd = open(*device, O_RDONLY); + if (fd != -1) { if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) { +# if defined(F_SETFD) && defined(FD_CLOEXEC) + (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); +# endif return fd; } (void) close(fd); + } else if (errno == EINTR) { + continue; } device++; } while (*device != NULL); + errno = EIO; return -1; /* LCOV_EXCL_STOP */ } diff --git a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c index 79e910df..15b223b4 100644 --- a/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c +++ b/src/libsodium/randombytes/sysrandom/randombytes_sysrandom.c @@ -83,7 +83,7 @@ safe_read(const int fd, void * const buf_, size_t count) assert(count > (size_t) 0U); do { while ((readnb = read(fd, buf, count)) < (ssize_t) 0 && - errno == EINTR); /* LCOV_EXCL_LINE */ + (errno == EINTR || errno == EAGAIN)); /* LCOV_EXCL_LINE */ if (readnb < (ssize_t) 0) { return readnb; /* LCOV_EXCL_LINE */ } @@ -114,15 +114,22 @@ randombytes_sysrandom_random_dev_open(void) int fd; do { - if ((fd = open(*device, O_RDONLY)) != -1) { + fd = open(*device, O_RDONLY); + if (fd != -1) { if (fstat(fd, &st) == 0 && S_ISCHR(st.st_mode)) { +# if defined(F_SETFD) && defined(FD_CLOEXEC) + (void) fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); +# endif return fd; } (void) close(fd); + } else if (errno == EINTR) { + continue; } device++; } while (*device != NULL); + errno = EIO; return -1; /* LCOV_EXCL_STOP */ }