Skip to content

Commit

Permalink
RAND_write_file(): Avoid potential file descriptor leak
Browse files Browse the repository at this point in the history
If fdopen() call fails we need to close the fd. Also
return early as this is most likely some fatal error.

Fixes openssl#25064

Reviewed-by: Tom Cosgrove <tom.cosgrove@arm.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#25081)
  • Loading branch information
coolshrid authored and t8m committed Aug 19, 2024
1 parent 4c37778 commit d604834
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crypto/rand/randfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d604834

Please sign in to comment.