From 32dbacd4059a52a9982a1ba32bfa82d8e6a358cf Mon Sep 17 00:00:00 2001 From: Jakub Hrozek Date: Mon, 8 Aug 2016 14:07:04 +0200 Subject: [PATCH 1/3] UTIL: Use sss_atomic_read_s in generate_csprng_buffer 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. --- 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 5d1d671096f986d9387e6199112c017e9bf30e1b..7c15bd7cdb336bc38f2055382121d73a3583b1e7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -812,6 +812,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) @@ -833,6 +834,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