]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
RAND_write_file(): Avoid potential file descriptor leak
authorshridhar kalavagunta <coolshrid@hotmail.com>
Sun, 4 Aug 2024 21:04:53 +0000 (16:04 -0500)
committerTomas Mraz <tomas@openssl.org>
Mon, 19 Aug 2024 09:13:23 +0000 (11:13 +0200)
If fdopen() call fails we need to close the fd. Also
return early as this is most likely some fatal error.

Fixes #25064

Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/25081)

(cherry picked from commit d6048344398ec75996fee1f465abb61ab3aa377e)

crypto/rand/randfile.c

index 82f41637387b21ef75fb373a66d4780f21890ce9..d44cf2c2318dd3399d43a2d0c5d16b96eb62fea2 100644 (file)
@@ -208,8 +208,16 @@ int RAND_write_file(const char *file)
          * should be restrictive from the start
          */
         int fd = open(file, O_WRONLY | O_CREAT | O_BINARY, 0600);
-        if (fd != -1)
+
+        if (fd != -1) {
             out = fdopen(fd, "wb");
+            if (out == NULL) {
+                close(fd);
+                ERR_raise_data(ERR_LIB_RAND, RAND_R_CANNOT_OPEN_FILE,
+                               "Filename=%s", file);
+                return -1;
+            }
+        }
     }
 #endif