summaryrefslogtreecommitdiffstats
path: root/src/util/crypto
diff options
context:
space:
mode:
authorJakub Hrozek <jhrozek@redhat.com>2016-08-08 14:07:04 +0200
committerJakub Hrozek <jhrozek@redhat.com>2016-08-17 16:55:15 +0200
commitb3a22ee1d91aa4ed1544475be16ec2b7cf886180 (patch)
tree742df0dd5ddc7ec37c69fd1fcc03044bb5a833e1 /src/util/crypto
parent864cdac4c7fbe768d768da5e01b7518eb02836d9 (diff)
downloadsssd-b3a22ee1d91aa4ed1544475be16ec2b7cf886180.tar.gz
sssd-b3a22ee1d91aa4ed1544475be16ec2b7cf886180.tar.xz
sssd-b3a22ee1d91aa4ed1544475be16ec2b7cf886180.zip
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. Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com> Reviewed-by: Fabiano Fidêncio <fabiano@fidencio.org>
Diffstat (limited to 'src/util/crypto')
-rw-r--r--src/util/crypto/sss_crypto.c29
1 files changed, 5 insertions, 24 deletions
diff --git a/src/util/crypto/sss_crypto.c b/src/util/crypto/sss_crypto.c
index 4c775f3d9..ac90bac07 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;