Skip to content

Make the threading code provider aware#9040

Closed
mattcaswell wants to merge 10 commits intoopenssl:masterfrom
mattcaswell:provider-threads
Closed

Make the threading code provider aware#9040
mattcaswell wants to merge 10 commits intoopenssl:masterfrom
mattcaswell:provider-threads

Conversation

@mattcaswell
Copy link
Member

The RAND code is thread aware and stores thread local variables. In order for that to work when its running inside the FIPS module we need to make some changes to how we clean up threads so that we can delegate some of that cleanup to the providers where appropriate.

We change the thread stop handing into a publish/subscribe model to accomplish this.

(Needed in order to move the RAND code into the FIPS provider - see #9035)

@mattcaswell
Copy link
Member Author

Hmmm. The travis failure is annoying. A classic case of the compiler trying to be too helpful:

crypto/context.c:171:9: error: explicitly assigning value of variable of type 'OPENSSL_CTX *' (aka 'struct openssl_ctx_st *') to itself [-Werror,-Wself-assign]
    ctx = (ctx);
    ~~~ ^  ~~~

That's because of this macro definition in FIPS_MODE:

#ifdef FIPS_MODE
# define openssl_ctx_get_concrete(ctx)  (ctx)
#else
OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
#endif

So when I do the following it gets this error:

    ctx = openssl_ctx_get_concrete(ctx);

A no-op is exactly what I want in FIPS mode. Any bright ideas as to how I can get the compiler to shut up?

@levitte
Copy link
Member

levitte commented May 31, 2019

Hmmm. The travis failure is annoying. A classic case of the compiler trying to be too helpful:

crypto/context.c:171:9: error: explicitly assigning value of variable of type 'OPENSSL_CTX *' (aka 'struct openssl_ctx_st *') to itself [-Werror,-Wself-assign]
    ctx = (ctx);
    ~~~ ^  ~~~

That's because of this macro definition in FIPS_MODE:

#ifdef FIPS_MODE
# define openssl_ctx_get_concrete(ctx)  (ctx)
#else
OPENSSL_CTX *openssl_ctx_get_concrete(OPENSSL_CTX *ctx);
#endif

So when I do the following it gets this error:

    ctx = openssl_ctx_get_concrete(ctx);

A no-op is exactly what I want in FIPS mode. Any bright ideas as to how I can get the compiler to shut up?

The solution is probably to not try and be so smart. Don't create the macro in FIPS mode, and instead, wrap each call:

#ifndef FIPS_MODE
    ctx = openssl_ctx_get_concrete(ctx);
#endif

@mattcaswell
Copy link
Member Author

The solution is probably to not try and be so smart. Don't create the macro in FIPS mode, and instead, wrap each call:

That's what I had originally - but changed it on the suggestion of @slontis.

@slontis
Copy link
Member

slontis commented Jun 1, 2019

The original comment applied to the fact that there are quite a few of these #ifdef FIPS_MODE
snippets which start making the code look quite ugly. If it doesnt work then revert back to the original.
having a new macro name that does ctx = openssl_ctx_get_concrete(ctx); in one case and then nothing in the other case would work, but it probably not be very nice either.

@mattcaswell
Copy link
Member Author

I rebased this to resolve the conflict with master, and implemented @levitte's suggestion for openssl_ctx_get_concrete.

@mattcaswell
Copy link
Member Author

2 fixup commits pushed addressing feedback so far.

@mattcaswell
Copy link
Member Author

Rebased this now that #9039 went in.

Ping?

Copy link
Member

@levitte levitte left a comment

Choose a reason for hiding this comment

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

Doc nit

@mattcaswell
Copy link
Member Author

Fix up commit pushed. Ping @levitte.

@mattcaswell
Copy link
Member Author

Fix up commit pushed addressing the doc feedback comment.

@mattcaswell
Copy link
Member Author

Rebased again due to conflicts with master!

Ping??

Copy link
Member

Choose a reason for hiding this comment

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

Side note: if we expect other providers to do the same thing, then a non-dynamic index isn't the best of ideas... this is something I was thinking of when I originally designed the OPENSSL_CTX API and internals. It's worth giving it another deeper think, methinks...

Copy link
Member Author

Choose a reason for hiding this comment

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

Feel free...

@mattcaswell
Copy link
Member Author

Updated commits pushed addressing feedback.

This adds the ability to take an OPENSSL_CTX parameter and either return it
as is (unchanged), or if it is NULL return a pointer to the default ctx.
In later commits this will allow providers to subscribe to thread stop
events. We will need this in the FIPS module. We also make thread stop
handling OPENSSL_CTX aware (different OPENSSL_CTXs may have different
thread local data that needs cleaning up).
We're going to need some of these functions in the FIPS module, but most
of the rest of the code in init.c is not needed. Therefore we split it out.
This will need to be hooked up in a later commit with an event sent to
the FIPS provider informing it of thread stop events.
The RAND code needs to know about threads stopping in order to cleanup
local thread data. Therefore we add a callback for libcrypto to tell
providers about such events.
This adds the ability to clean up a thread on a per OPENSSL_CTX basis.
This new function works in the same way as OPENSSL_thread_stop() but
for a specified OPENSSL_CTX.
@mattcaswell
Copy link
Member Author

...and immediately I've rebased it again due to conflicts with master....grrrr....

*/
# define OSSL_PARAM_OCTET_PTR 7

/* Typedef for thread stop handling callback */
Copy link
Member

Choose a reason for hiding this comment

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

Considering the rest of this header file, that comment seems a bit... minimal

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixup pushed expanding out that comment.

@mattcaswell
Copy link
Member Author

Pushed. Thanks!!

pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
This adds the ability to take an OPENSSL_CTX parameter and either return it
as is (unchanged), or if it is NULL return a pointer to the default ctx.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
In later commits this will allow providers to subscribe to thread stop
events. We will need this in the FIPS module. We also make thread stop
handling OPENSSL_CTX aware (different OPENSSL_CTXs may have different
thread local data that needs cleaning up).

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
We're going to need some of these functions in the FIPS module, but most
of the rest of the code in init.c is not needed. Therefore we split it out.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
This will need to be hooked up in a later commit with an event sent to
the FIPS provider informing it of thread stop events.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
The RAND code needs to know about threads stopping in order to cleanup
local thread data. Therefore we add a callback for libcrypto to tell
providers about such events.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
This adds the ability to clean up a thread on a per OPENSSL_CTX basis.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
This new function works in the same way as OPENSSL_thread_stop() but
for a specified OPENSSL_CTX.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
pull bot pushed a commit to Mattlk13/openssl-1 that referenced this pull request Jun 17, 2019
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from openssl#9040)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

branch: master Applies to master branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants