From 60596973b503637c742b597aeb862eecae9f9c91 Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 8 Aug 2016 14:07:04 +0200 Subject: [PATCH 109/111] UTIL: Use sss_atomic_read_s in generate_csprng_buffer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There was a bug in generate_csprng_buffer() where if we read the exact amount of bytes from /dev/urandom, we would always return EIO. Instead, let's reuse the existing code from sss_atomic_read_s() which fixes this bug and reduces code duplication. Reviewed-by: Lukáš Slebodník Reviewed-by: Fabiano Fidêncio --- Makefile.am | 2 ++ src/util/crypto/sss_crypto.c | 29 +++++------------------------ 2 files changed, 7 insertions(+), 24 deletions(-) diff --git a/Makefile.am b/Makefile.am index 4d90c7a46e2ee0fe652aa392cf647d056e06c7fc..a32a1e37c85e2370fa006ee73b730145f03c3fc1 100644 --- a/Makefile.am +++ b/Makefile.am @@ -815,6 +815,7 @@ if HAVE_NSS src/util/crypto/nss/nss_nite.c \ src/util/crypto/nss/nss_util.c \ src/util/crypto/sss_crypto.c \ + src/util/atomic_io.c \ $(NULL) SSS_CRYPT_CFLAGS = $(NSS_CFLAGS) SSS_CRYPT_LIBS = $(NSS_LIBS) @@ -836,6 +837,7 @@ else src/util/crypto/libcrypto/crypto_obfuscate.c \ src/util/crypto/libcrypto/crypto_nite.c \ src/util/crypto/sss_crypto.c \ + src/util/atomic_io.c \ $(NULL) SSS_CRYPT_CFLAGS = $(CRYPTO_CFLAGS) SSS_CRYPT_LIBS = $(CRYPTO_LIBS) diff --git a/src/util/crypto/sss_crypto.c b/src/util/crypto/sss_crypto.c index 4c775f3d926ae32f3cb72b1329c0a025a0550ed5..ac90bac07c7006a2950331b86bcc412207a3e401 100644 --- a/src/util/crypto/sss_crypto.c +++ b/src/util/crypto/sss_crypto.c @@ -25,41 +25,22 @@ int generate_csprng_buffer(uint8_t *buf, size_t size) { ssize_t rsize; - ssize_t pos; int ret; int fd; fd = open("/dev/urandom", O_RDONLY); if (fd == -1) return errno; - rsize = 0; - pos = 0; - while (rsize < size) { - rsize = read(fd, buf + pos, size - pos); - switch (rsize) { - case -1: - if (errno == EINTR) continue; - ret = EIO; - goto done; - case 0: - ret = EIO; - goto done; - default: - if (rsize + pos < size - pos) { - pos += rsize; - continue; - } - ret = EIO; - goto done; - } - } - if (rsize != size) { + rsize = sss_atomic_read_s(fd, buf, size); + if (rsize == -1) { + ret = errno; + goto done; + } else if (rsize != size) { ret = EFAULT; goto done; } ret = EOK; - done: close(fd); return ret; -- 2.4.11