Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this change moves stack of compression methods, now global variable #24414

Closed
wants to merge 1 commit into from

Conversation

Sashan
Copy link
Contributor

@Sashan Sashan commented May 16, 2024

in libssl into crypto context. This change is rquired by atexit PR.

@Sashan Sashan requested a review from mattcaswell May 16, 2024 04:39
@Sashan Sashan force-pushed the compression branch 2 times, most recently from 6e47851 to d4a5fb3 Compare May 16, 2024 08:02
Copy link
Member

@mattcaswell mattcaswell left a comment

Choose a reason for hiding this comment

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

When I build this I get linker errors:

/usr/bin/ld: ./libssl.so: undefined reference to `ossl_lib_ctx_get_data'
/usr/bin/ld: ./libssl.so: undefined reference to `ossl_find_compression'
/usr/bin/ld: ./libssl.so: undefined reference to `ossl_lib_ctx_set0_compression_methods'

This is because these symbols are not exported from libcrypto. It seems to me the only symbol that you actually need to export from libcrypto is `ossl_lib_ctx_get_data'. The other two could probably just be in libssl.

We have as yet not had the concept of internal symbols that are exported from libcrypto and are privately used by libssl. All symbols exported from libcrypto are public and form part of the API. With that in mind I would actually create a new public libcrypto function OSSL_LIB_CTX_get_data as a simple wrapper around ossl_lib_ctx_get_data. The public documentation would say that, although this is a public function, all the indexes that you can pass are reserved for internal use and so applications should not call this function. We might at some point in the future extend it with the idea to support application supplied indexes - but we don't have to worry about that now.

crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/context.c Outdated Show resolved Hide resolved
crypto/context.c Outdated Show resolved Hide resolved
crypto/context.c Outdated Show resolved Hide resolved
include/internal/stack_of_comp_methods.h.in Outdated Show resolved Hide resolved
ssl/ssl_init.c Show resolved Hide resolved
@Sashan
Copy link
Contributor Author

Sashan commented May 16, 2024

When I build this I get linker errors:

/usr/bin/ld: ./libssl.so: undefined reference to `ossl_lib_ctx_get_data'
/usr/bin/ld: ./libssl.so: undefined reference to `ossl_find_compression'
/usr/bin/ld: ./libssl.so: undefined reference to `ossl_lib_ctx_set0_compression_methods'

I've not noticed those errors on OpenBSD/clang tool chain. I can see those errors on Debian-12/gcc tool chain. I agree we can remove ossl_find_compression() which implementation can be kept in libssl. I will also need public version of ossl_lib_ctx_set0_compression_methods() because it is a workhorse for SSL_COMP_set0_compression_methods() function. Unless we want to remove SSL_COMP_set0_compression_methods() from libssl API.

@mattcaswell
Copy link
Member

I will also need public version of ossl_lib_ctx_set0_compression_methods() because it is a workhorse for SSL_COMP_set0_compression_methods() function. Unless we want to remove SSL_COMP_set0_compression_methods() from libssl API.

I'm not sure we need to move this to libcrypto.

What if the libssl data we stored in the libctx was actually a STACK_OF(OSSL_COMP) **? Then SSL_COMP_set0_compression_methods() could become:

STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
                                                      *meths)
{
    STACK_OF(OSSL_COMP) **data = OSSL_LIB_CTX_get_data(NULL, OSSL_LIB_CTX_COMP_METHODS);
    STACK_OF(SSL_COMP) *old_meths = (STACK_OF(SSL_COMP) *)*data;

    *data = (STACK_OF(OSSL_COMP) *)meths;
    return old_meths;
}

@Sashan
Copy link
Contributor Author

Sashan commented May 17, 2024

The updated PR addresses all suggestions from @mattcaswell

I failed to figure out how to hook newly added file crypto/comp_methods.c with FIPS module build. Therefore the PR currently disables compression methods for FIPS. With out this workaround in place the fips module fails to load due to unresolved symbols (ossl_load_builtin_compressions()). Perhaps we should dissolve crypto/comp_methods.c into existing source code files in libcrypto.

@github-actions github-actions bot added severity: fips change The pull request changes FIPS provider sources severity: ABI change This pull request contains ABI changes labels May 17, 2024
crypto/context.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/context.c Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
@Sashan
Copy link
Contributor Author

Sashan commented May 17, 2024

I will also need public version of ossl_lib_ctx_set0_compression_methods() because it is a workhorse for SSL_COMP_set0_compression_methods() function. Unless we want to remove SSL_COMP_set0_compression_methods() from libssl API.

I'm not sure we need to move this to libcrypto.

What if the libssl data we stored in the libctx was actually a STACK_OF(OSSL_COMP) **? Then SSL_COMP_set0_compression_methods() could become:

STACK_OF(SSL_COMP) *SSL_COMP_set0_compression_methods(STACK_OF(SSL_COMP)
                                                      *meths)
{
    STACK_OF(OSSL_COMP) **data = OSSL_LIB_CTX_get_data(NULL, OSSL_LIB_CTX_COMP_METHODS);
    STACK_OF(SSL_COMP) *old_meths = (STACK_OF(SSL_COMP) *)*data;

    *data = (STACK_OF(OSSL_COMP) *)meths;
    return old_meths;
}

I'll update PR shortly to see how it will look. On the other hand I'm not sure if those changes are heading in desired direction. I feel it goes against the concept 'all data are private' to OSSL_LIB_CTX. Changing OSSL_LIB_CTX_get_data() so it returns address of OSSL_LIB_CTX member makes auditing/reasoning about the code harder. Explicit set function is better in my opinion, because it makes reasoning/auditing the code bit easier.

@mattcaswell
Copy link
Member

I feel it goes against the concept 'all data are private' to OSSL_LIB_CTX.

That isn't actually the current design of OSSL_LIB_CTX. Currently the context.c code mostly has a bunch of internal members which are "black boxes" as far as context.c is concerned. The internals of the black box are private to the module that uses it. E.g. consider ctx->drbg. The context.c code knows nothing about what that data member is - rather this is managed by the crypto/rand/rand_lib.c code where we discover that ctx->drbg is actually a RAND_GLOBAL structure.

@Sashan
Copy link
Contributor Author

Sashan commented May 17, 2024

New version makes OSSL_LIB_CTX_get_data() to return pointer-to-pointer for OSSL_LIB_CTX_COMP_METHODS index.

crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
crypto/comp_methods.c Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
include/internal/stack_of_comp_methods.h.in Outdated Show resolved Hide resolved
@Sashan Sashan marked this pull request as ready for review May 21, 2024 15:23
@Sashan Sashan requested a review from mattcaswell May 22, 2024 07:55
Copy link
Member

@mattcaswell mattcaswell left a comment

Choose a reason for hiding this comment

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

Looks good. Just a few minor nits remain.

doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
doc/man3/OSSL_LIB_CTX.pod Outdated Show resolved Hide resolved
include/internal/cryptlib.h Outdated Show resolved Hide resolved
@mattcaswell
Copy link
Member

CI failures seem to be due to #24463

@mattcaswell mattcaswell added branch: master Merge to master branch approval: review pending This pull request needs review by a committer labels May 22, 2024
@mattcaswell mattcaswell added the tests: exempted The PR is exempt from requirements for testing label May 22, 2024
@t8m t8m closed this May 22, 2024
@t8m t8m reopened this May 22, 2024
@t8m t8m added the triaged: refactor The issue/pr requests/implements refactoring label May 22, 2024
typedef struct ssl_comp_st SSL_COMP;
typedef struct ossl_comp_st SSL_COMP;
Copy link
Member

Choose a reason for hiding this comment

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

I would not change the typedef from ssl_comp_st to ossl_comp_st. Just move it to openssl/comp.h and keep using SSL_COMP everywhere instead of OSSL_COMP.

Copy link
Member

Choose a reason for hiding this comment

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

Those casts from OSSL_COMP to SSL_COMP are awkward and unnecessary if we just do not introduce the new type.

Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

Just a few final tweaks needed

Comment on lines 59 to 63
typedef struct ssl_comp_st {
int id;
const char *name;
COMP_METHOD *method;
} SSL_COMP;
Copy link
Member

Choose a reason for hiding this comment

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

The structure content should be kept internal -> into internal/comp.h


STACK_OF(SSL_COMP);

DEFINE_STACK_OF(SSL_COMP)
Copy link
Member

Choose a reason for hiding this comment

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

This must be generated through the generate_stack_macros("SSL_COMP") perl generator by moving that from ssl.h.in -> i.e., it requires generating comp.h from comp.h.in

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering why we are bothering to move it? Why not just keep it in ssl.h.in?

Copy link
Member

Choose a reason for hiding this comment

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

You need to be able to release the stack from libcrypto, so that means the definition needs to be in a header that code linked into libcrypto can include. IMO ssl.h should not be included.

Copy link
Member

Choose a reason for hiding this comment

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

IMO ssl.h should not be included.

Why not if we're only using it for some typedefs etc? Moving things to comp.h is problematic because it is excluded in the case of a no-comp build - whereas SSL_COMP should not be.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively you just replicate the DEFINE_STACK_OF(SSL_COMP) line in context.c and not include ssl.h at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the updated PR includes all suggestions from t8m. the code in comp.h.in got reshuffled so only relevant portion in that file is protected by OPENSSL_NO_COMP guard. I did test and build it:
- perl ./Configure --strict-warnings enable-zlib enable-comp
- perl ./Configure --strict-warnings no-zlib no-brotli no-zstd no-comp
both versions did pass build and make test

ssl/ssl_ciph.c Outdated Show resolved Hide resolved
ssl/ssl_ciph.c Outdated Show resolved Hide resolved
@@ -976,7 +974,6 @@ extern "C" {
*/
{-
generate_const_stack_macros("SSL_CIPHER")
Copy link
Member

Choose a reason for hiding this comment

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

isn't this missing ; ?

*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/

{-
use OpenSSL::stackhash qw(generate_stack_macros generate_const_stack_macros);
Copy link
Member

Choose a reason for hiding this comment

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

generate_const_stack_macros in't needed

Comment on lines 3 to 6
*
* Copyright 4 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
* Copyright 2005 Nokia. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't add this. None of the things moved were IMO added by these copyright holders.

@@ -1,12 +1,20 @@
/*
* Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
Copy link
Member

Choose a reason for hiding this comment

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

The original copyright should be kept 2015-2024

ssl/ssl_ciph.c Outdated
return ssl_comp_methods;
STACK_OF(SSL_COMP) **rv;

rv = (STACK_OF(SSL_COMP) **) OSSL_LIB_CTX_get_data(NULL,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: no space after **)

in libssl into crypto context. This change is rquired by atexit PR.
Copy link
Member

@t8m t8m left a comment

Choose a reason for hiding this comment

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

@mattcaswell IMO this is OK. Maybe the SSL_COMP stack declarations could have been handled differently but I see this as cleaner way.

typedef struct ssl_comp_st SSL_COMP;

{-
generate_stack_macros("SSL_COMP");
Copy link
Member

Choose a reason for hiding this comment

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

I would have preferred to keep this in ssl.h.in. But ok.

@mattcaswell mattcaswell added approval: done This pull request has the required number of approvals and removed approval: review pending This pull request needs review by a committer labels May 24, 2024
@openssl-machine openssl-machine added approval: ready to merge The 24 hour grace period has passed, ready to merge and removed approval: done This pull request has the required number of approvals labels May 25, 2024
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@t8m
Copy link
Member

t8m commented May 28, 2024

Merged to the master branch with reworded commit message. Thank you.

@t8m t8m closed this May 28, 2024
openssl-machine pushed a commit that referenced this pull request May 28, 2024
The compression methods are now a global variable in libssl.
This change moves it into OSSL library context.

It is necessary to eliminate atexit call from libssl.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24414)
jvdsn pushed a commit to jvdsn/openssl that referenced this pull request Jun 3, 2024
The compression methods are now a global variable in libssl.
This change moves it into OSSL library context.

It is necessary to eliminate atexit call from libssl.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from openssl#24414)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approval: ready to merge The 24 hour grace period has passed, ready to merge branch: master Merge to master branch severity: ABI change This pull request contains ABI changes severity: fips change The pull request changes FIPS provider sources tests: exempted The PR is exempt from requirements for testing triaged: refactor The issue/pr requests/implements refactoring
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants