Skip to content

Fix for OpenSSL 3.2+. - #11501

Merged
ywkaras merged 1 commit into
apache:masterfrom
ywkaras:openssl3.2_fix
Jul 16, 2024
Merged

ywkaras merged 1 commit into
apache:masterfrom
ywkaras:openssl3.2_fix

Conversation

@ywkaras

@ywkaras ywkaras commented Jul 2, 2024

Copy link
Copy Markdown

It seems BIO user data is no longer initialized to nullptr.

@ywkaras ywkaras self-assigned this Jul 2, 2024
@ywkaras ywkaras added the TLS label Jul 2, 2024
@ywkaras ywkaras added this to the 10.1.0 milestone Jul 2, 2024
@ywkaras ywkaras linked an issue Jul 2, 2024 that may be closed by this pull request
@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 2 times, most recently from 66bdda9 to a64314e Compare July 2, 2024 02:55
Comment thread src/iocore/net/SSLNetVConnection.cc Resolved
@maskit

maskit commented Jul 2, 2024

Copy link
Copy Markdown
Member

It seems BIO user data is no longer initialized to nullptr.

Yeah, and it's initialized to something else. This change probably cause a memory leak and frees something that should not be freed by OpenSSL, if TFO is enabled.
https://github.com/openssl/openssl/blob/77d716d3be95317ade5a7b8dda0b4946522c5a34/crypto/bio/bss_sock.c#L94

@ywkaras
ywkaras marked this pull request as draft July 2, 2024 20:55
@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 2 times, most recently from 1f5b18a to a6a7748 Compare July 4, 2024 03:28
@ywkaras
ywkaras marked this pull request as ready for review July 4, 2024 03:35
@maskit

maskit commented Jul 5, 2024

Copy link
Copy Markdown
Member

How about using BIO_set_ex_data?

@ywkaras

ywkaras commented Jul 8, 2024

Copy link
Copy Markdown
Author

How about using BIO_set_ex_data?

It looks like it's deprecated in OpenSSL 3: https://www.openssl.org/docs/man3.2/man3/BIO_set_ex_data.html

@maskit

maskit commented Jul 8, 2024

Copy link
Copy Markdown
Member

Ones for BIO are not, right?

@ywkaras
ywkaras marked this pull request as draft July 8, 2024 16:14
@ywkaras

ywkaras commented Jul 8, 2024

Copy link
Copy Markdown
Author

Ones for BIO are not, right?

Ah, I see now, it's replaced by CRYPTO_new/set_ex_data in 3.0+. Maybe useable with some ninja #ifdefs.

@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 6 times, most recently from 72a7400 to bdda90d Compare July 10, 2024 15:32
@ywkaras
ywkaras marked this pull request as ready for review July 10, 2024 16:16
Comment thread src/iocore/net/BIO_fastopen.cc Outdated
};

#undef BIO_set_data
#define BIO_set_data(BIO, PTR) ink_assert(BIO_set_ex_data((BIO), ExData::idx(), (PTR)) == 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

BIO_set_data and BIO_set_ex_data has different features. Overwriting BIO_set_data doesn't seem like a right thing to do at all. Could you tell me the motivation?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

These definitions only apply within this file (BIO_fastopen.cc). Boringssl does not provide BIO_set/get_ex_data. It seemed like the best approach to avoid further #ifdefs for OPENSSL_IS_BORINGSSL in this file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's only been supported for about 8 months: google/boringssl@2139aba . This commit does not seem to be in the version of BoringSSL we are using in the Rocky CI job. I guess we could add a CMake check for it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Bummer. I update BoringSSL once in a while so I didn't think it's unavailable.

I'd rather update the BoringSSL requirement if that's the reason. Oldest commit we currently require is https://boringssl.googlesource.com/boringssl/+/a1843d660b47116207877614af53defa767be46a
but I don't think there's a big reason to stick with the commit.
https://lists.apache.org/thread/d6ymfjhpmjogb5stnz92hwhhps909809

What do you think? Would it make the code simple?

Comment thread src/iocore/net/BIO_fastopen.cc Outdated
idx()
{
static int idx_ = []() -> int {
int i = CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, 0, nullptr, _new, _dup, _free);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand the necessity of ExData class in the first place, but even if it's needed, I don't see a reason to use CRYPTO_get_ex_new_index instead of BIO_get_ex_new_index.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

OK I will switch to BIO_get_ex_new_index, apparently it's not deprecated. The ExData class is for scoping and to handle C++ order of initialization fiasco.

@maskit

maskit commented Jul 10, 2024 •

Copy link
Copy Markdown
Member

What would be the issue if we simply use BIO_get_ex_data instead of BIO_get_data regardless of the SSL library and its version.

@ywkaras

ywkaras commented Jul 10, 2024

Copy link
Copy Markdown
Author

What would be the issue if we simply use BIO_get_ex_data instead of BIO_get_data regardless of the SSL library and its version.

Not supported in Boring:

wkaras /opt/boringssl
$ grep 'BIO_.*_ex' $(findsrc)
wkaras /opt/boringssl
$ 

Comment thread src/iocore/net/BIO_fastopen.cc Outdated
BIO_set_data(bio, ptr);
return 0;
case BIO_C_SET_FD:
BIO_set_shutdown(bio, int(larg));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Shutdown??

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I had to add that code to avoid triggering this assert:

ink_assert(BIO_get_shutdown(bio) == BIO_NOCLOSE);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I misunderstood what "shutdown" means here, and now setting it makes sense. But why only on OpenSSL 3+?

The odd thing is that it looks like BIO_meth_get_ctrl returns the ctrl function for Socket BIO, and it sets the shutdown flag. How does that not happen?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I need to retest this. Looking at the openssl code, it shouldn't be necessary.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

My testing was bogus, that change isn't necessary.

Comment thread src/iocore/net/BIO_fastopen.cc Resolved
@ywkaras
ywkaras marked this pull request as draft July 11, 2024 16:44
@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 2 times, most recently from ef80a97 to 5eafc45 Compare July 11, 2024 20:02
@ywkaras
ywkaras marked this pull request as ready for review July 11, 2024 20:49

@maskit maskit left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks good, but this does not build with a BoringSSL which has BIO_get_ex_new_index.

/Users/mkitajo/src/github.com/trafficserver/src/iocore/net/BIO_fastopen.cc:48:15: error: no matching function for call to 'BIO_get_ex_new_index'
      int i = BIO_get_ex_new_index(0, nullptr, _new, _dup, _free);
              ^~~~~~~~~~~~~~~~~~~~
/Users/mkitajo/opt/boringssl/include/openssl/bio.h:819:20: note: candidate function not viable: no known conversion from 'void (void *, void *, CRYPTO_EX_DATA *, int, long, void *)' (aka 'void (void *, void *, crypto_ex_data_st *, int, long, void *)') to 'CRYPTO_EX_unused *' (aka 'int *') for 3rd argument
OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp,
                   ^
/Users/mkitajo/src/github.com/trafficserver/src/iocore/net/BIO_fastopen.cc:59:24: error: use of undeclared identifier 'CRYPTO_set_ex_data'
    ink_release_assert(CRYPTO_set_ex_data(ad, idx_, nullptr) == 1);
                       ^
/Users/mkitajo/src/github.com/trafficserver/src/iocore/net/BIO_fastopen.cc:65:24: error: use of undeclared identifier 'CRYPTO_set_ex_data'
    ink_release_assert(CRYPTO_set_ex_data(ad, idx_, nullptr) == 1);
                       ^
3 errors generated.

@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 4 times, most recently from 1380ddd to 73a5714 Compare July 15, 2024 19:39
Comment thread src/iocore/net/BIO_fastopen.cc Outdated

#if defined(HAVE_BIO_GET_EX_NEW_INDEX) && defined(HAVE_BIO_GET_EX_DATA) && defined(HAVE_BIO_SET_EX_DATA)

// Thwart boringssl's thwarting of ex data for custom BIOs. Boring fails to export these functions.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree that BoringSSL's API is broken, but do we need to use these on code for BoringSSL?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

What alternative do you propose?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove theses exports, and use empty callbacks for BoringSSL.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't quite understand why OpenSSL says this though. BoringSSL doesn't say this.

When setting exdata, the pointer specified with a particular index is saved, and returned on a subsequent "get" call. If the application is going to release the data, it must make sure to set a NULL value at the index, to avoid likely double-free crashes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I don't know why either. In our case, the ex data is not a pointer to an allocated resource, so there is no risk of a double deallocation or a leak. I only clear the ex data pointer to know it was free'd when looking at a coredump.

@ywkaras
ywkaras force-pushed the openssl3.2_fix branch 3 times, most recently from 81663e9 to 4017f39 Compare July 15, 2024 22:40
Comment thread src/iocore/net/BIO_fastopen.cc Outdated
private:
#if BORINGLIKE
static const CRYPTO_EX_unused _new{};
// Hopefully Boring intializes ex data pointers to nullptr by default.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about setting nullptr in fastopen_create by BIO_set_ex_data? I'm not sure if this is really possible. It could be too early to call BIO_set_ex_data.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Another option is setting nullptr after making the BIO on SSLNetVC side. It'd be a silly requirement if this BIO is publicly available for others though.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I made a change in SSLNetVConnection.cc to initialize the ex data to either the destination address or nullptr for all BIOs using the fastopen methods.

Comment thread src/iocore/net/BIO_fastopen.cc Outdated

private:
#if BORINGLIKE
static const CRYPTO_EX_unused _new{};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

[ 80%] Building CXX object src/iocore/net/CMakeFiles/inknet.dir/BIO_fastopen.cc.o
/Users/mkitajo/src/github.com/trafficserver/src/iocore/net/BIO_fastopen.cc:56:15: error: no matching function for call to 'BIO_get_ex_new_index'
      int i = BIO_get_ex_new_index(0, nullptr, _new, _dup, _free);
              ^~~~~~~~~~~~~~~~~~~~
/Users/mkitajo/opt/boringssl/include/openssl/bio.h:819:20: note: candidate function not viable: no known conversion from 'const CRYPTO_EX_unused' (aka 'const int') to 'CRYPTO_EX_unused *' (aka 'int *') for 3rd argument
OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp,
                   ^
1 error generated.
Suggested change
static const CRYPTO_EX_unused _new{};
static constexpr CRYPTO_EX_unused *_new{nullptr};

It seems BIO user data is no longer set to nullptr.
@ywkaras
ywkaras merged commit 6bf342b into apache:master Jul 16, 2024
@cmcfarlen cmcfarlen modified the milestones: 10.1.0, 10.0.0 Jul 21, 2024
@cmcfarlen

Copy link
Copy Markdown
Contributor

Cherry-picked to v10.0.x

cmcfarlen pushed a commit that referenced this pull request Jul 21, 2024
It seems BIO user data is no longer set to nullptr.

(cherry picked from commit 6bf342b)
ezelkow1 pushed a commit to ezelkow1/trafficserver that referenced this pull request Nov 21, 2024
It seems BIO user data is no longer set to nullptr.

(cherry picked from commit 6bf342b)

----- Still needs configure modifications for non-Cmake build
ezelkow1 added a commit that referenced this pull request Nov 21, 2024
* Fix for OpenSSL 3.2+. (#11501)

It seems BIO user data is no longer set to nullptr.

(cherry picked from commit 6bf342b)

----- Still needs configure modifications for non-Cmake build

* Fix socket def

* Chris' make fixes

---------

Co-authored-by: Walt Karas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project
Status: picked-10.0.0

Development

Successfully merging this pull request may close these issues.

Outgoing HTTPS connections fail with OpenSSL 3.2+

4 participants