>From 48bfeac3b3c2a8273176ea9b35019b6deed10926 Mon Sep 17 00:00:00 2001
Message-ID: <48bfeac3b3c2a8273176ea9b35019b6deed10926.1793758827.git.gniibe@fsij.org>
From: NIIBE Yutaka <gniibe@fsij.org>
Date: Wed, 4 Nov 2026 11:15:30 +0900
Subject: [PATCH] random: Make sure GCRYCTL_CLOSE_RANDOM_DEVICE finish nonce
 buffer.
To: gcrypt-devel@gnupg.org
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.47.3"

This is a multi-part message in MIME format.
--------------2.47.3
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


* random/random.c (finalize_nonce_buffer): New.
(_gcry_random_close_fds): Call finalize_nonce_buffer.
(nonce_buffer_initialized): Let it out of the create_nonce function.
(finalize_nonce_buffer): New.

--

GnuPG-bug-id: 8533
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
---
 random/random.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)


--------------2.47.3
Content-Type: text/x-patch; name="0001-random-Make-sure-GCRYCTL_CLOSE_RANDOM_DEVICE-finish-.patch"
Content-Transfer-Encoding: 8bit
Content-Disposition: attachment; filename="0001-random-Make-sure-GCRYCTL_CLOSE_RANDOM_DEVICE-finish-.patch"

diff --git a/random/random.c b/random/random.c
index d714c3c2..03914548 100644
--- a/random/random.c
+++ b/random/random.c
@@ -63,7 +63,8 @@ static struct
    generation.  */
 GPGRT_LOCK_DEFINE (nonce_buffer_lock);
 
-
+/* Finish the use of the nonce buffer.  */
+static void finalize_nonce_buffer (void);
 
 /* ---  Functions  --- */
 
@@ -267,6 +268,8 @@ _gcry_random_close_fds (void)
     _gcry_rngsystem_close_fds ();
   else
     _gcry_rngcsprng_close_fds ();
+
+  finalize_nonce_buffer ();
 }
 
 
@@ -479,13 +482,15 @@ _gcry_fast_random_poll (void)
 }
 
 
+/* This variable is to see the nonce buffer is initialized and in use.
+   Protected by NONCE_BUFFER_LOCK.  */
+static int nonce_buffer_initialized = 0;
 
 /* Create an unpredicable nonce of LENGTH bytes in BUFFER. */
 void
 _gcry_create_nonce (void *buffer, size_t length)
 {
   static unsigned char nonce_buffer[20+8];
-  static int nonce_buffer_initialized = 0;
   static volatile pid_t my_pid; /* The volatile is there to make sure the
                                    compiler does not optimize the code away
                                    in case the getpid function is badly
@@ -571,6 +576,30 @@ _gcry_create_nonce (void *buffer, size_t length)
 }
 
 
+/* Finish the use of the nonce buffer.  Clear the variable, so that
+   the nonce buffer will be initialized again when it will be
+   re-opened.  */
+static void
+finalize_nonce_buffer (void)
+{
+  int err;
+
+  /* Acquire the nonce buffer lock. */
+  err = gpgrt_lock_lock (&nonce_buffer_lock);
+  if (err)
+    log_fatal ("failed to acquire the nonce buffer lock: %s\n",
+               gpg_strerror (err));
+
+  nonce_buffer_initialized = 0;
+
+  /* Release the nonce buffer lock. */
+  err = gpgrt_lock_unlock (&nonce_buffer_lock);
+  if (err)
+    log_fatal ("failed to release the nonce buffer lock: %s\n",
+               gpg_strerror (err));
+}
+
+
 /* Run the self-tests for the RNG.  This is currently only implemented
    for the FIPS generator.  */
 gpg_error_t

--------------2.47.3--


