wolfssl icon indicating copy to clipboard operation
wolfssl copied to clipboard

Data Race fix when using Seed callback with multiple threads

Open night1rider opened this issue 1 year ago • 0 comments

Description

While working on an example for threading using wolfssl, I found that when using seedCb a data race occurs.

Since setting the seedCb is just setting a function pointer to a global this means a simple mutex is not enough. For example:

You have Thread A and Thread B. Both Threads have different ways they want to have a seed(for whatever reason) SeedA and SeedB

Right now if Thread A sets seedA callback first and Thread B sets seedB callback after. Thread A has the potential to be using seedB for its RNG if ThreadA has not completed all of its RNG calls before ThreadB sets SeedB. This then leads to a sort of data race where that race is getting to the thread's desired function before it is overwritten.

This PR attempts to alleviate this issue by allowing an override macro WC_MULTI_THREADED_CALLBACKS, currently as of posting this fix is only setup for posix threading.

Sources on functions for Posix Threads https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html https://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_setspecific.html

There is potential to make this more portable but we would need to implement a ThreadId key pair and then all the cleanup that is associated with that sort of mechanism....

night1rider avatar Dec 18 '24 23:12 night1rider