]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
lib: avoid file descriptor leak when application forks
authorDaiki Ueno <ueno@gnu.org>
Mon, 25 May 2020 08:09:28 +0000 (10:09 +0200)
committerDaiki Ueno <ueno@gnu.org>
Sat, 30 May 2020 09:10:11 +0000 (11:10 +0200)
This makes use of the "e" flag of fopen, provided by the Gnulib's
fopen-gnu module.

Reported by Remi Denis-Courmont in:
https://gitlab.com/gnutls/gnutls/-/issues/985
and fix suggested by Tim Rühsen.

Signed-off-by: Daiki Ueno <ueno@gnu.org>
bootstrap.conf
lib/auth/psk_passwd.c
lib/auth/srp_passwd.c
lib/file.c
lib/fips.c
lib/kx.c
lib/pkcs11.c
lib/priority.c
lib/verify-tofu.c

index dcf346d6c63c48d1454ed1b8e3ebd57ea560c07b..3abfe10464e70c546ce630cf540189b08d077b7d 100644 (file)
@@ -25,10 +25,10 @@ checkout_only_file=
 local_gl_dir=gl/override/
 required_submodules="tests/suite/tls-fuzzer/python-ecdsa tests/suite/tls-fuzzer/tlsfuzzer tests/suite/tls-fuzzer/tlslite-ng devel/nettle devel/libtasn1"
 
-# Reproduce by: gnulib-tool --import --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca attribute byteswap c-ctype extensions func gendocs getline gettext-h gettimeofday hash-pjw-bare havelib intprops ldd lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r unistd vasprintf verify vsnprintf warnings
+# Reproduce by: gnulib-tool --import --local-dir=gl/override --lib=libgnu --source-base=gl --m4-base=gl/m4 --doc-base=doc --tests-base=gl/tests --aux-dir=build-aux --with-tests --avoid=alignof-tests --avoid=lock-tests --avoid=lseek-tests --lgpl=2 --no-conditional-dependencies --libtool --macro-prefix=gl --no-vc-files alloca attribute byteswap c-ctype extensions fopen-gnu func gendocs getline gettext-h gettimeofday hash-pjw-bare havelib intprops ldd lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv snprintf stdint strcase strndup strtok_r strverscmp sys_socket sys_stat time_r unistd vasprintf verify vsnprintf warnings
 
 gnulib_modules="
-alloca attribute byteswap c-ctype c-strcase extensions func gendocs getline gettext-h gettimeofday hash hash-pjw-bare havelib arpa_inet inet_ntop inet_pton intprops ldd lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv setsockopt snprintf stdint strcase strdup-posix strndup strtok_r strverscmp sys_socket sys_stat sys_types time_r unistd valgrind-tests vasprintf verify vsnprintf warnings
+alloca attribute byteswap c-ctype c-strcase extensions fopen-gnu func gendocs getline gettext-h gettimeofday hash hash-pjw-bare havelib arpa_inet inet_ntop inet_pton intprops ldd lib-msvc-compat lib-symbol-versions maintainer-makefile manywarnings memmem-simple minmax netdb netinet_in pmccabe2html read-file secure_getenv setsockopt snprintf stdint strcase strdup-posix strndup strtok_r strverscmp sys_socket sys_stat sys_types time_r unistd valgrind-tests vasprintf verify vsnprintf warnings
 "
 
 unistring_modules="
index a0427914f928982f3e734bccbc3843c196adeb74..7384703bc7450889f607fa617e01c97798405758 100644 (file)
@@ -203,7 +203,7 @@ _gnutls_psk_pwd_find_entry(gnutls_session_t session,
 
        /* Open the selected password file.
         */
-       fd = fopen(cred->password_file, "r");
+       fd = fopen(cred->password_file, "re");
        if (fd == NULL) {
                gnutls_assert();
                return GNUTLS_E_SRP_PWD_ERROR;
index baa4086e77ae855eeb85956d39ede94b6be261e5..78848cdf10c7c08faf5e0eea21475ca7e23484e6 100644 (file)
@@ -202,7 +202,7 @@ pwd_read_conf(const char *pconf_file, SRP_PWD_ENTRY * entry, int idx)
 
        snprintf(indexstr, sizeof(indexstr), "%u", (unsigned int) idx);
 
-       fd = fopen(pconf_file, "r");
+       fd = fopen(pconf_file, "re");
        if (fd == NULL) {
                gnutls_assert();
                return GNUTLS_E_FILE_ERROR;
@@ -308,7 +308,7 @@ _gnutls_srp_pwd_read_entry(gnutls_session_t state, char *username,
 
        /* Open the selected password file.
         */
-       fd = fopen(cred->password_file, "r");
+       fd = fopen(cred->password_file, "re");
        if (fd == NULL) {
                gnutls_assert();
                ret = GNUTLS_E_SRP_PWD_ERROR;
index 2bcdee8b7e90de4cd1c7faf0458b6dee757de9c4..03aa265c38300addbc170f1a2873aa65234a800f 100644 (file)
@@ -29,7 +29,7 @@ int _gnutls_file_exists(const char *file)
 {
        FILE *fd;
 
-       fd = fopen(file, "r");
+       fd = fopen(file, "re");
        if (fd == NULL)
                return -1;
 
index 3c43250aaf8fb40ecd33dc2ee16a594ac478c359..0f0c34c1df72a350a3856ab7fd46a0b9ad1b8d63 100644 (file)
@@ -93,7 +93,7 @@ unsigned _gnutls_fips_mode_enabled(void)
                goto exit;
        }
 
-       fd = fopen(FIPS_KERNEL_FILE, "r");
+       fd = fopen(FIPS_KERNEL_FILE, "re");
        if (fd != NULL) {
                f1p = fgetc(fd);
                fclose(fd);
index a874f15114f06cf803a15587b247568adf97a283..1eda14d3d6aec3133ebf30d2b15ea8a184e837a3 100644 (file)
--- a/lib/kx.c
+++ b/lib/kx.c
@@ -143,7 +143,7 @@ void _gnutls_nss_keylog_write(gnutls_session_t session,
                checked_env = 1;
                keylogfile = secure_getenv("SSLKEYLOGFILE");
                if (keylogfile != NULL)
-                       keylog = fopen(keylogfile, "a");
+                       keylog = fopen(keylogfile, "ae");
        }
 
        if (keylog) {
index d03bf6e44406a4094a9737535de5bf757d827dc7..fad16aaf4f60919103f1a5d28fb750d1d2cd18ac 100644 (file)
@@ -889,7 +889,7 @@ static void compat_load(const char *configfile)
        if (configfile == NULL)
                configfile = "/etc/gnutls/pkcs11.conf";
 
-       fp = fopen(configfile, "r");
+       fp = fopen(configfile, "re");
        if (fp == NULL) {
                gnutls_assert();
                return;
index ad99459adba722fa5e41f770f0d8824d5e9da812..0a284ae1f11eb2a43574d32904579b412d6b88fb 100644 (file)
@@ -1304,6 +1304,7 @@ static void _gnutls_update_system_priorities(void)
 {
        int ret;
        struct stat sb;
+       FILE *fp;
 
        if (stat(system_priority_file, &sb) < 0) {
                _gnutls_debug_log("cfg: unable to access: %s: %d\n",
@@ -1321,7 +1322,14 @@ static void _gnutls_update_system_priorities(void)
        if (system_wide_priority_strings_init != 0)
                _name_val_array_clear(&system_wide_priority_strings);
 
-       ret = ini_parse(system_priority_file, cfg_ini_handler, NULL);
+       fp = fopen(system_priority_file, "re");
+       if (fp == NULL) {
+               _gnutls_debug_log("cfg: unable to open: %s: %d\n",
+                                 system_priority_file, errno);
+               return;
+       }
+       ret = ini_parse_file(fp, cfg_ini_handler, NULL);
+       fclose(fp);
        if (ret != 0) {
                _gnutls_debug_log("cfg: unable to parse: %s: %d\n",
                                  system_priority_file, ret);
index 36328e04aff264b870bbd8626190b4e540e65f14..8baebaa01186bda761c27ec88e9f0ec94c4d84ab 100644 (file)
@@ -343,7 +343,7 @@ static int verify_pubkey(const char *file,
        if (service != NULL)
                service_len = strlen(service);
 
-       fd = fopen(file, "rb");
+       fd = fopen(file, "rbe");
        if (fd == NULL) {
                ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
                goto cleanup;
@@ -414,7 +414,7 @@ int store_pubkey(const char *db_name, const char *host,
                goto cleanup;
        }
 
-       fd = fopen(db_name, "ab+");
+       fd = fopen(db_name, "abe+");
        if (fd == NULL) {
                ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
                goto cleanup;
@@ -449,7 +449,7 @@ int store_commitment(const char *db_name, const char *host,
        FILE *fd;
        char buffer[MAX_HASH_SIZE * 2 + 1];
 
-       fd = fopen(db_name, "ab+");
+       fd = fopen(db_name, "abe+");
        if (fd == NULL)
                return gnutls_assert_val(GNUTLS_E_FILE_ERROR);