From f10bf64b186e154bb2a196204900b199898c8e25 Mon Sep 17 00:00:00 2001 From: Juha Niskanen Date: Tue, 8 Dec 2020 18:20:53 +0200 Subject: [PATCH] crypto/random_pool.c: fix getrandom() when thread calling it gets canceled Change-Id: I8f008beea52cd6dfa494af424b3c4ceb474e45a6 --- crypto/random_pool.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/crypto/random_pool.c b/crypto/random_pool.c index 93843a9eec7..0794105c152 100644 --- a/crypto/random_pool.c +++ b/crypto/random_pool.c @@ -55,11 +55,11 @@ #include /**************************************************************************** - * Definitions + * Pre-processor Definitions ****************************************************************************/ #ifndef MIN -#define MIN(a,b) ((a) < (b) ? (a) : (b)) +# define MIN(a,b) ((a) < (b) ? (a) : (b)) #endif #define ROTL_32(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) ) @@ -565,10 +565,19 @@ void getrandom(FAR void *bytes, size_t nbytes) { int ret; - ret = nxsem_wait_uninterruptible(&g_rng.rd_sem); - if (ret >= 0) + do { - rng_buf_internal(bytes, nbytes); - nxsem_post(&g_rng.rd_sem); + ret = nxsem_wait_uninterruptible(&g_rng.rd_sem); + + /* The only possible error should be if we were awakened by + * thread cancellation. At this point, we must continue to acquire + * the semaphore anyway. + */ + + DEBUGASSERT(ret == OK || ret == -ECANCELED); } + while (ret < 0); + + rng_buf_internal(bytes, nbytes); + nxsem_post(&g_rng.rd_sem); }