Skip to content

Fix concurrent nc_client_init() and nc_server_init() - #641

Merged
michalvasko merged 2 commits into
develfrom
global-init-race
Sep 11, 2026
Merged

Fix concurrent nc_client_init() and nc_server_init()#641
michalvasko merged 2 commits into
develfrom
global-init-race

Conversation

@Roytak

@Roytak Roytak commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

nc_client_init() and nc_server_init() initialized the same process-global libraries without any lock, so doing both at once from two threads raced inside them.

Both sides now go through nc_global_init()/nc_global_destroy(), which take a lock and track which side initialized the libraries. They are therefore also initialized only once and destroyed only when neither side uses them any longer, so nc_client_destroy() no longer tears down the crypto state of a running server and vice versa.

test_thread_messages also never destroyed either side.

Seen in: https://github.com/CESNET/libnetconf2/actions/runs/34359810512

Comment thread src/session.c Outdated
The client and the server side both initialized the same process-global
libraries (libcurl, the TLS backend, libssh) without any lock, so an
application (or test_thread_messages) that starts a server thread and
initializes the client at the same time raced inside them. With MbedTLS
this corrupts the shared PSA entropy context, which was observed as a
leaked 216 byte hash context, a double free, a segfault or a spurious
psa_crypto_init() failure.

Both sides now init and destroy the libraries through nc_global_init()
and nc_global_destroy() guarded by a lock. The libraries are also
initialized only once and destroyed only once the other side does not
use them any longer, so nc_client_destroy() no longer tears down the
crypto state of a running server and vice versa.
test_thread_messages never destroyed either side, leaking the global
state of the used libraries on every run.
@michalvasko
michalvasko merged commit b76cf6a into devel Sep 11, 2026
11 checks passed
@michalvasko
michalvasko deleted the global-init-race branch September 11, 2026 10:05
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.

2 participants