Skip to content

Fix double fclose in wolfSSL_BIO_read_filename() - #11923

Open
julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:fix-bio-read-filename-double-close
Open

julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:fix-bio-read-filename-double-close

Conversation

@julek-wolfssl

Copy link
Copy Markdown
Member

wolfSSL_BIO_read_filename() closed the BIO's existing file even when the BIO did not own it. It also kept the closed FILE* in the BIO when the new file failed to open, so wolfSSL_BIO_free() closed it a second time.

  • Close the old file only when the BIO has BIO_CLOSE set, and always drop it from the BIO.
  • Remove the duplicate wolfSSL_BIO_read_filename() call in wolfSSL_X509_load_crl_file(), a leftover from a merge. It opened the file twice and hit the double fclose when the second open failed.

Fixes #11903

wolfSSL_BIO_read_filename() closed the BIO's file even when the BIO did
not own it, and kept the closed FILE* when the new file failed to open.
wolfSSL_BIO_free() then closed it a second time.

Close the old file only with BIO_CLOSE and always drop it from the BIO.

wolfSSL_X509_load_crl_file() called wolfSSL_BIO_read_filename() twice
on the same file, a leftover from a merge. Remove the second call. It
opened the file twice and hit the double fclose when the second open
failed.

Fixes wolfSSL#11903

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the file-ownership and cleanup fix.

No open issues 🎉

What changed in this PR

Fixes #11903 by correcting file ownership and cleanup in wolfSSL_BIO_read_filename(), preventing a double close when opening a replacement file fails.

Changes:

  • Close the previous file only when the BIO owns it, and clear the stored file handle.
  • Remove the duplicate file-open call during CRL loading.
  • Add regression tests for failed opens, recovery, and BIO_NOCLOSE behavior.
File Description
tests/​api/​test_ossl_bio.h Declares and registers the new regression test.
tests/​api/​test_ossl_bio.c Tests replacement-file failures and file ownership handling.
src/​x509.c Removes the redundant file-open call during CRL loading.
src/​bio.c Fixes file ownership checks, clears stale handles, and corrects the debug label.

Review effort: Balanced

@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

@Marti2203

Copy link
Copy Markdown

Thanks Juliusz! I tested f2c1b8f with the reproducer from #11903 (--enable-opensslextra, ASan and release builds): the failed BIO_read_filename() followed by BIO_free() no longer reports a double free, the control (successful open) is unchanged, and in the release build an unrelated stream opened after the failed call stays open (fgetc() returns data instead of EOF). The new test_wolfSSL_BIO_read_filename and the ossl_bio group pass (19 passed, 5 skipped). The BIO_set_fp(..., BIO_CLOSE) after a successful open also covers the BIO_NOCLOSE case, and removing the duplicate call in wolfSSL_X509_load_crl_file() looks right to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wolfSSL_BIO_read_filename() leaves a closed FILE* in the BIO when the new file cannot be opened

3 participants