Skip to content

Fix accepted socket ownership and GnuTLS teardown - #298

Merged
rgerhards merged 9 commits into
rsyslog:masterfrom
rgerhards:codex/accept-double-close
Aug 11, 2026
Merged

Fix accepted socket ownership and GnuTLS teardown#298
rgerhards merged 9 commits into
rsyslog:masterfrom
rgerhards:codex/accept-double-close

Conversation

@rgerhards

@rgerhards rgerhards commented Aug 10, 2026

Copy link
Copy Markdown
Member

Commit 1: Prevent descriptor reuse close after failed accept

The accepted socket now transfers explicitly to relpTcp_t: the local
descriptor is cleared immediately after assignment. If TLS initialization then
fails, relpTcpDestruct() is the sole owner responsible for closing the
socket, while outer accept cleanup only closes descriptors whose ownership was
never transferred.

The accompanying deterministic regression test forces a post-transfer TLS
error, reuses the accepted descriptor after the first close, and verifies that
cleanup does not close the replacement. It reports two closes on the old path
and passed 100 consecutive runs with the fix.

Commit 2: Fix GnuTLS shutdown ordering and resource cleanup

GnuTLS session shutdown and deinitialization now happen before the transport
socket is closed, allowing gnutls_bye() to send close_notify.
GNUTLS_E_INTERRUPTED is retried, while GNUTLS_E_AGAIN is not, preventing
teardown from stalling on a nonblocking socket. OpenSSL retains its existing
post-socket-close cleanup ordering.

GnuTLS sessions, client and server credentials, and DH parameters are now
released even when initialization or the handshake ends before bTLSActive
is set. The lifecycle regression test verifies that the transport remains open
during gnutls_bye() and that partially initialized resources are released.

Validation

  • old accept path reproduced two closes of the same numeric descriptor
  • fixed descriptor-reuse regression passed 100 consecutive runs
  • combined GnuTLS/OpenSSL build: 31 passed, 1 skipped, 0 failed
  • OpenSSL-only build: 29 passed, 1 skipped, 0 failed
  • GnuTLS-only build: 28 passed, 1 skipped, 0 failed
  • warning-as-error builds passed
  • make dist passed

This PR is separate from and does not modify OpenSSL BIO ownership PR #297.

@rgerhards
rgerhards force-pushed the codex/accept-double-close branch from 2e33a0b to 82cbf67 Compare August 10, 2026 14:05
Transfer ownership of an accepted socket explicitly to relpTcp_t by clearing
the local descriptor immediately after assignment. If TLS initialization then
fails, relpTcpDestruct() is the sole owner responsible for closing the socket;
the outer accept cleanup only closes descriptors whose ownership was never
transferred.

Add a deterministic regression test that forces a post-transfer TLS error,
reuses the accepted descriptor after the first close, and verifies that error
cleanup does not close the replacement. The test reports two closes on the old
path and passed 100 consecutive runs with the fix.
Run GnuTLS session shutdown and deinitialization before closing the transport
socket so gnutls_bye() can send close_notify. Retry GNUTLS_E_INTERRUPTED, but
do not retry GNUTLS_E_AGAIN, preventing teardown from stalling on a
nonblocking socket. OpenSSL retains its existing post-socket-close cleanup
ordering.

Release GnuTLS sessions, client and server credentials, and DH parameters even
when initialization or the handshake ended before bTLSActive was set. Add a
lifecycle regression test that verifies the transport remains open during
gnutls_bye() and that partially initialized resources are all released.
@rgerhards
rgerhards force-pushed the codex/accept-double-close branch from 82cbf67 to 396f43f Compare August 10, 2026 16:30
@rgerhards

Copy link
Copy Markdown
Member Author

@codex review

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 10, 2026

Copy link
Copy Markdown

@codex review

@cubic-dev-ai review

@rgerhards I have started the AI code review. It will take a few minutes to complete.

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 396f43f4b1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tcp.c Outdated
Comment thread tests/Makefile.am Outdated
Comment thread tests/Makefile.am Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread tests/Makefile.am Outdated
Comment thread tests/Makefile.am Outdated
Comment thread src/tcp.c Outdated
Comment thread tests/tls-gtls-lifecycle.c
@rgerhards

Copy link
Copy Markdown
Member Author

@codex review

@cubic-dev-ai review

@cubic-dev-ai

cubic-dev-ai Bot commented Aug 10, 2026

Copy link
Copy Markdown

@codex review

@cubic-dev-ai review

@rgerhards I have started the AI code review. It will take a few minutes to complete.

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create a Codex account and connect to github.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: bee4dc7e3f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/tcp.c Outdated
Comment thread tests/tls-gtls-lifecycle.c
@rgerhards

Copy link
Copy Markdown
Member Author

Addressed the new review findings in 828d767: partial OpenSSL setup now frees its SSL object without attempting shutdown, the GnuTLS EAGAIN fixture fails promptly if retry behavior regresses, and the GNU ld --wrap fixtures are excluded on Solaris Studio where their runtime interposition is incompatible.\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 828d767d89

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/Makefile.am Outdated
if BUILD_STATIC
# These white-box fixtures use GNU ld --wrap interposition. Solaris Studio
# accepts the option but does not provide compatible runtime interposition.
if !OS_SOLARIS

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Gate wrapper tests on linker capability

On supported non-Solaris hosts using a non-GNU-compatible linker—most notably macOS and AIX, which are both handled in configure.ac—this guard still enables the fixtures, but their -Wl,--wrap=... flags rely on the GNU-style symbol wrapping documented by GNU ld. Native Apple and AIX linkers reject these options, causing make check to fail while linking the test programs. Detect --wrap support or restrict these fixtures to compatible linkers rather than excluding only Solaris.

Useful? React with 👍 / 👎.

@rgerhards

Copy link
Copy Markdown
Member Author

Follow-up fix in a5c63b9: the OpenSSL client error path was freeing a BIO after SSL had taken ownership, leaving SSL with a dangling BIO. It now frees through SSL_free() exactly once; partial SSL teardown occurs before socket close, and the full local suite passes (32 pass, 1 expected Valgrind skip).\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a5c63b9b77

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tcp.c
Comment on lines +817 to +818
if(pThis->pEngine->tls_lib == 0 && pThis->bTLSActive) {
relpTcpDestructTLS_gtls(pThis);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Free GnuTLS sessions before they become active

When a GnuTLS accept fails fatally during the initial handshake, gnutls_init() has already populated pThis->session, but bTLSActive is still false because it is set only after the handshake succeeds or becomes retryable. The accept error path then calls relpTcpDestruct(), and this guard skips relpTcpDestructTLS_gtls(), permanently leaking the session for every malformed or failed inbound TLS connection; repeated unauthenticated connections can therefore exhaust server memory. The lifecycle test masks this case by explicitly setting bTLSActive = 1 on its supposedly partial object.

Useful? React with 👍 / 👎.

@rgerhards

Copy link
Copy Markdown
Member Author

Solaris correction in 7a4b5b4: Automake negated-condition syntax did not suppress the GNU ld --wrap fixtures on Solaris Studio. The explicit OS_SOLARIS/else branch does; Linux local suite remains green.\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 7a4b5b4f64

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@rgerhards

Copy link
Copy Markdown
Member Author

Final Solaris conditional fix in 5303c8a: configure now runs AC_CANONICAL_HOST before evaluating platform conditionals, so OS_SOLARIS is correctly set on the SPARC builder. Full local suite: 32 pass, 1 expected Valgrind skip.\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 5303c8a9eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@rgerhards

Copy link
Copy Markdown
Member Author

Solaris configure follow-up in a67e71d: AC_CANONICAL_HOST is incompatible with this legacy Solaris dependency-tracking setup. Platform selection now falls back to uname when host is unset, avoiding the bootstrap failure while setting OS_SOLARIS on SunOS. Full local suite still passes.\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a67e71d653

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread configure.ac Outdated
case "${host}" in
*-*-linux*)
case "${host}:$(uname -s)" in
*-*-linux*:*|*:Linux)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Only fall back to uname when host is empty

When configuring on Linux with an explicit non-Linux target such as --host=sparc-sun-solaris2.11, the combined value still matches this first *:Linux alternative before reaching the Solaris arm. The configuration consequently defines OS_LINUX while omitting OS_SOLARIS, the Solaris feature flags, and SOL_LIBS; other cross-targets are similarly classified as the build OS. Test host first and consult uname only when it is unset.

Useful? React with 👍 / 👎.

@rgerhards

Copy link
Copy Markdown
Member Author

Solaris gate correction in b8a4bfb: platform logic is restored unchanged; a new OS_SOLARIS_WRAP conditional uses uname solely to omit the GNU ld --wrap fixtures on SunOS. This avoids the Solaris configure regression. Full local suite passes.\n\n@codex review\n\n@cubic-dev-ai review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b8a4bfb513

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@rgerhards
rgerhards marked this pull request as ready for review August 11, 2026 09:23
@rgerhards
rgerhards merged commit 413306f into rsyslog:master Aug 11, 2026
10 of 13 checks passed
@rgerhards
rgerhards deleted the codex/accept-double-close branch August 11, 2026 09:25
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.

1 participant