Skip to content

Skip current directory and parent directory symbols when reading files in a directory#20312

Closed
olszomal wants to merge 1 commit intoopenssl:masterfrom
olszomal:directory
Closed

Skip current directory and parent directory symbols when reading files in a directory#20312
olszomal wants to merge 1 commit intoopenssl:masterfrom
olszomal:directory

Conversation

@olszomal
Copy link
Copy Markdown
Contributor

@olszomal olszomal commented Feb 16, 2023

SSL_add_dir_cert_subjects_to_stack() function always fails on Windows.
SSL_add_file_cert_subjects_to_stack() function fails on Windows when trying to open a directory as a file :

openssl/ssl/ssl_cert.c

Lines 767 to 768 in 7e55051

if (BIO_read_filename(in, file) <= 0)
goto err;

The following code shows this error. This PoC works in Linux but not Windows:

#include <openssl/x509.h>
#include <openssl/ssl.h>
#include <openssl/safestack.h>

int main(int argc, char **argv) {
    int n, i;
    STACK_OF(X509_NAME) *ca_dn=sk_X509_NAME_new_null();
    (void)argc;
    SSL_add_dir_cert_subjects_to_stack(ca_dn, argv[1]);
    if((n=sk_X509_NAME_num(ca_dn))==0) {
        printf("No trusted certificates found\n");
        sk_X509_NAME_pop_free(ca_dn, X509_NAME_free);
        return 1; /* FAILED */
    }
    for(i=0; i<n; i++) {
        char *ca_name=X509_NAME_oneline(sk_X509_NAME_value(ca_dn, i), NULL, 0);
        printf("CA #%d: %s\n", i, ca_name);
        OPENSSL_free(ca_name);
    }
    sk_X509_NAME_pop_free(ca_dn, X509_NAME_free);
    return 0; /* OK */
}

My improved PR fixes this by skipping current, parent and other subdirectories in SSL_add_dir_cert_subjects_to_stack().

@paulidale
Copy link
Copy Markdown
Contributor

I'm not sure this is the right fix. Opening a directory as a file ought to fail which makes this additional check unnecessary, even if it did open, it shouldn't parse and should still fail.

@mtrojnar
Copy link
Copy Markdown
Contributor

I'm not sure this is the right fix. Opening a directory as a file ought to fail which makes this additional check unnecessary, even if it did open, it shouldn't parse and should still fail.

@paulidale The proposed fix filters out current and parent subdirectories from the generated list of files within the directory provided as an SSL_add_dir_cert_subjects_to_stack() parameter. This is specifically meant to prevent opening those subdirectories as files with SSL_add_file_cert_subjects_to_stack(), so that it doesn't fail.

TL;DR: SSL_add_dir_cert_subjects_to_stack() currently always fails on Windows. AFAIK it never worked on that platform.

@paulidale
Copy link
Copy Markdown
Contributor

Thanks for the explanation.

@levitte
Copy link
Copy Markdown
Member

levitte commented Feb 21, 2023

Hmmm... So it seems that on Linux, this call succeeds:

openssl/ssl/ssl_cert.c

Lines 767 to 768 in 7e55051

if (BIO_read_filename(in, file) <= 0)
goto err;

I assume that on Windows, it fails, thus causing an error indication to be returned...

On platforms where the directory can be opened as a file, it's expected that PEM_read_bio_X598 will return NULL, and thereby exit the function with success, just as it does with empty files:

openssl/ssl/ssl_cert.c

Lines 771 to 772 in 7e55051

if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
break;

The fix here is, unfortunately, incomplete. If the directory with certificates contains a subdirectory, calling SSL_add_dir_cert_subjects_to_stack() will fail as well. My I suggest using stat() instead, to check that the file is a "normal" file? Something like this should suffice (inspired from the inode(7) manual:

           stat(buf, &sb);
           if ((sb.st_mode & S_IFMT) != S_IFREG)
               continue;

Note that this needs to be done after the BIO_snprint() line

@paulidale paulidale added branch: master Applies to master branch approval: review pending This pull request needs review by a committer branch: 3.0 Applies to openssl-3.0 branch branch: 3.1 Applies to openssl-3.1 (EOL) tests: exempted The PR is exempt from requirements for testing labels Feb 22, 2023
@levitte levitte added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels Feb 22, 2023
@openssl-machine
Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but this PR has failing CI tests. Once the tests pass it will get moved to 'approval: ready to merge' automatically, alternatively please review and set the label manually.

@paulidale
Copy link
Copy Markdown
Contributor

Merged, thanks for the fix.

@paulidale paulidale closed this Feb 23, 2023
openssl-machine pushed a commit that referenced this pull request Feb 23, 2023
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #20312)

(cherry picked from commit 1dc35d4)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2023
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #20312)
openssl-machine pushed a commit that referenced this pull request Feb 23, 2023
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from #20312)

(cherry picked from commit 1dc35d4)
paulswartz added a commit to paulswartz/stunnel-docker that referenced this pull request Mar 6, 2023
### Version 5.69, 2023.03.04, urgency: MEDIUM
* New features
   - Improved logging performance with the "output" option.
   - Improved file read performance on the WIN32 platform.
   - DH and kDHEPSK ciphersuites removed from FIPS defaults.
   - Set the LimitNOFILE ulimit in stunnel.service to allow
     for up to 10,000 concurrent clients.
* Bugfixes
   - Fixed the "CApath" option on the WIN32 platform by
     applying openssl/openssl#20312.
   - Fixed stunnel.spec used for building rpm packages.
   - Fixed tests on some OSes and architectures by merging
     Debian 07-tests-errmsg.patch (thx to Peter Pentchev).

Home page: https://www.stunnel.org/
Download: https://www.stunnel.org/downloads.html

SHA-256 hashes:

1ff7d9f30884c75b98c8a0a4e1534fa79adcada2322635e6787337b4e38fdb81 stunnel-5.69.tar.gz
66c4f3bbb94c4a274f2e8e98e3d44e74c0460d6494986f0a94b9b8becdc63cc3 stunnel-5.69-win64-installer.exe
74813a0be13270b5348fc4bc7c16ada668d151773be19f404db1176b7e22aafc stunnel-5.69-android.zip
paulswartz added a commit to paulswartz/stunnel-docker that referenced this pull request Mar 6, 2023
### Version 5.69, 2023.03.04, urgency: MEDIUM
* New features
   - Improved logging performance with the "output" option.
   - Improved file read performance on the WIN32 platform.
   - DH and kDHEPSK ciphersuites removed from FIPS defaults.
   - Set the LimitNOFILE ulimit in stunnel.service to allow
     for up to 10,000 concurrent clients.
* Bugfixes
   - Fixed the "CApath" option on the WIN32 platform by
     applying openssl/openssl#20312.
   - Fixed stunnel.spec used for building rpm packages.
   - Fixed tests on some OSes and architectures by merging
     Debian 07-tests-errmsg.patch (thx to Peter Pentchev).

Home page: https://www.stunnel.org/
Download: https://www.stunnel.org/downloads.html

SHA-256 hashes:

1ff7d9f30884c75b98c8a0a4e1534fa79adcada2322635e6787337b4e38fdb81 stunnel-5.69.tar.gz
66c4f3bbb94c4a274f2e8e98e3d44e74c0460d6494986f0a94b9b8becdc63cc3 stunnel-5.69-win64-installer.exe
74813a0be13270b5348fc4bc7c16ada668d151773be19f404db1176b7e22aafc stunnel-5.69-android.zip
paulswartz added a commit to paulswartz/stunnel-docker that referenced this pull request Mar 6, 2023
* New features
   - Improved logging performance with the "output" option.
   - Improved file read performance on the WIN32 platform.
   - DH and kDHEPSK ciphersuites removed from FIPS defaults.
   - Set the LimitNOFILE ulimit in stunnel.service to allow
     for up to 10,000 concurrent clients.
* Bugfixes
   - Fixed the "CApath" option on the WIN32 platform by
     applying openssl/openssl#20312.
   - Fixed stunnel.spec used for building rpm packages.
   - Fixed tests on some OSes and architectures by merging
     Debian 07-tests-errmsg.patch (thx to Peter Pentchev).

Home page: https://www.stunnel.org/
Download: https://www.stunnel.org/downloads.html

SHA-256 hashes:

1ff7d9f30884c75b98c8a0a4e1534fa79adcada2322635e6787337b4e38fdb81 stunnel-5.69.tar.gz
66c4f3bbb94c4a274f2e8e98e3d44e74c0460d6494986f0a94b9b8becdc63cc3 stunnel-5.69-win64-installer.exe
74813a0be13270b5348fc4bc7c16ada668d151773be19f404db1176b7e22aafc stunnel-5.69-android.zip
paulswartz added a commit to mbta/stunnel-docker that referenced this pull request Mar 6, 2023
* New features
   - Improved logging performance with the "output" option.
   - Improved file read performance on the WIN32 platform.
   - DH and kDHEPSK ciphersuites removed from FIPS defaults.
   - Set the LimitNOFILE ulimit in stunnel.service to allow
     for up to 10,000 concurrent clients.
* Bugfixes
   - Fixed the "CApath" option on the WIN32 platform by
     applying openssl/openssl#20312.
   - Fixed stunnel.spec used for building rpm packages.
   - Fixed tests on some OSes and architectures by merging
     Debian 07-tests-errmsg.patch (thx to Peter Pentchev).

Home page: https://www.stunnel.org/
Download: https://www.stunnel.org/downloads.html

SHA-256 hashes:

1ff7d9f30884c75b98c8a0a4e1534fa79adcada2322635e6787337b4e38fdb81 stunnel-5.69.tar.gz
66c4f3bbb94c4a274f2e8e98e3d44e74c0460d6494986f0a94b9b8becdc63cc3 stunnel-5.69-win64-installer.exe
74813a0be13270b5348fc4bc7c16ada668d151773be19f404db1176b7e22aafc stunnel-5.69-android.zip
kraj pushed a commit to YoeDistro/meta-openembedded that referenced this pull request Mar 10, 2023
License-Update: Copyright year updated to 2023.

Changelog:
==========
* New features
  - Improved logging performance with the "output" option.
  - Improved file read performance on the WIN32 platform.
  - DH and kDHEPSK ciphersuites removed from FIPS defaults.
  - Set the LimitNOFILE ulimit in stunnel.service to allow
    for up to 10,000 concurrent clients.
  - Added the new 'CAengine' service-level option
    to load a trusted CA certificate from an engine.
  - Added requesting client certificates in server
    mode with 'CApath' besides 'CAfile'.
  - Improved file read performance.
  - Improved logging performance.
* Bugfixes
  - Fixed the "CApath" option on the WIN32 platform by
    applying openssl/openssl#20312.
  - Fixed stunnel.spec used for building rpm packages.
  - Fixed tests on some OSes and architectures by merging
    Debian 07-tests-errmsg.patch (thx to Peter Pentchev).
  - Fixed EWOULDBLOCK errors in protocol negotiation.
  - Fixed handling TLS errors in protocol negotiation.
  - Prevented following fatal TLS alerts with TCP resets.
  - Improved OpenSSL initialization on WIN32.
  - Improved testing suite stability.
* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.8.

Signed-off-by: Wang Mingyu <wangmy@fujitsu.com>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
@t8m t8m added the triaged: bug The issue/pr is/fixes a bug label Apr 20, 2023
@t8m
Copy link
Copy Markdown
Member

t8m commented Apr 20, 2023

This caused regression of no-posix-io builds. Fix in #20786

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Jul 10, 2023
Now support OpenSSL 3.0 and stop pkglint's warning.

Version 5.69, 2023.03.04, urgency: MEDIUM

* New features
  - Improved logging performance with the "output" option.
  - Improved file read performance on the WIN32 platform.
  - DH and kDHEPSK ciphersuites removed from FIPS defaults.
  - Set the LimitNOFILE ulimit in stunnel.service to allow
    for up to 10,000 concurrent clients.
* Bugfixes
  - Fixed the "CApath" option on the WIN32 platform by
    applying openssl/openssl#20312.
  - Fixed stunnel.spec used for building rpm packages.
  - Fixed tests on some OSes and architectures by merging
    Debian 07-tests-errmsg.patch (thx to Peter Pentchev).

Version 5.68, 2023.02.07, urgency: HIGH

* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.8.
* New features
  - Added the new 'CAengine' service-level option
    to load a trusted CA certificate from an engine.
  - Added requesting client certificates in server
    mode with 'CApath' besides 'CAfile'.
  - Improved file read performance.
  - Improved logging performance.
* Bugfixes
  - Fixed EWOULDBLOCK errors in protocol negotiation.
  - Fixed handling TLS errors in protocol negotiation.
  - Prevented following fatal TLS alerts with TCP resets.
  - Improved OpenSSL initialization on WIN32.
  - Improved testing suite stability.

Version 5.67, 2022.11.01, urgency: HIGH

* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.7.
* New features
  - Provided a logging callback to custom engines.
* Bugfixes
  - Fixed "make cert" with OpenSSL older than 3.0.
  - Fixed the code and the documentation to use conscious
    language for SNI servers (thx to Clemens Lang).

Version 5.66, 2022.09.11, urgency: MEDIUM

* New features
  - OpenSSL 3.0 FIPS Provider support for Windows.
* Bugfixes
  - Fixed building on machines without pkg-config.
  - Added the missing "environ" declaration for
    BSD-based operating systems.
  - Fixed the passphrase dialog with OpenSSL 3.0.

Version 5.65, 2022.07.17, urgency: HIGH

* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.5.
* Bugfixes
  - Fixed handling globally enabled FIPS.
  - Fixed openssl.cnf processing in WIN32 GUI.
  - Fixed a number of compiler warnings.
  - Fixed tests on older versions of OpenSSL.

Version 5.64, 2022.05.06, urgency: MEDIUM

* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.3.
* New features
  - Updated the pkcs11 engine for Windows.
* Bugfixes
  - Removed the SERVICE_INTERACTIVE_PROCESS flag in
    "stunnel -install".

Version 5.63, 2022.03.15, urgency: HIGH

* Security bugfixes
  - OpenSSL DLLs updated to version 3.0.2.
* New features
  - Updated stunnel.spec to support bash completion.
* Bugfixes
  - Fixed a PRNG initialization crash (thx to Gleydson Soares).

Version 5.62, 2022.01.17, urgency: MEDIUM

* New features
  - Added a bash completion script.
* Bugfixes
  - Fixed a transfer() loop bug.

Version 5.61, 2021.12.22, urgency: LOW

* New features sponsored by the University of Maryland
  - Added new "protocol = capwin" and "protocol = capwinctrl"
    configuration file options.
* New features for the Windows platform
  - Added client mode allowing authenticated users to view
    logs, reconfigure and terminate running stunnel services.
  - Added support for multiple GUI and service instances
    distinguised by the location of stunnel.conf.
  - Improved log window scrolling.
  - Added a new 'Pause auto-scroll' GUI checkbox.
  - Double click on the icon tray replaced with single click.
  - OpenSSL DLLs updated to version 3.0.1.
* Other new features
  - Rewritten the testing framework in python (thx to
    Peter Pentchev for inspiration and initial framework).
  - Added support for missing SSL_set_options() values.
  - Updated stunnel.spec to support RHEL8.
* Bugfixes
  - Fixed OpenSSL 3.0 build.
  - Fixed reloading configuration with
    "systemctl reload stunnel.service".
  - Fixed incorrect messages logged for OpenSSL errors.
  - Fixed printing IPv6 socket option defaults on FreeBSD.
kafei-cy added a commit to kafei-cy/Tongsuo that referenced this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approval: done This pull request has the required number of approvals branch: master Applies to master branch branch: 3.0 Applies to openssl-3.0 branch branch: 3.1 Applies to openssl-3.1 (EOL) tests: exempted The PR is exempt from requirements for testing triaged: bug The issue/pr is/fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants