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

fips: explicitly setup cpuid when initialize #24419

Closed

Conversation

ZenithalHourlyRate
Copy link
Contributor

Fixes: #23979

Previously fips module relies on OPENSSL_cpuid_setup being used as constructor by the linker to correctly setup the capability vector, either via .section .init (for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before the init function for fips module. However, this early constructing behavior has several disadvantages:

  1. Not all platform/toolchain supports such behavior

  2. Initialisation sequence is not well defined, and some function might not be initialized when cpuid_setup is called

  3. Implicit path is hard to maintain and debug

Test

I have tested this branch merged with #24403, and the riscvcap_P inside libcrypto/fips.so are both initialized.

Uncertainties
  1. Now cpuid_setup is put after function_id init, and before context/thread/self-test. Putting it before thread init comes from the below snippet, but I do not know whether to put it before context creation or not.

openssl/crypto/init.c

Lines 68 to 74 in a6afe2b

OPENSSL_cpuid_setup();
if (!ossl_init_thread())
goto err;
if (!CRYPTO_THREAD_init_local(&in_init_config_local, NULL))
goto err;

  1. This behavior should be documented somewhere, where should I put them.

  2. Test and coverage for this also needs massive effort. The coverage is also a problem when we gradually remove the constructor behavior of all cpuid_setup (Set OPENSSL_ppccap_P global variable in fips provider context #24399 (comment))

Checklist
  • documentation is added or updated
  • tests are added or updated

Fixes: openssl#23979

Previously fips module relies on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug
@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label May 16, 2024
@t8m t8m added triaged: refactor The issue/pr requests/implements refactoring triaged: bug The issue/pr is/fixes a bug branch: master Merge to master branch branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 branch: 3.3 Merge to openssl-3.3 tests: deferred Tests will be added in a subsequent PR (label should be removed when the PR with tests is merged) approval: review pending This pull request needs review by a committer approval: otc review pending This pull request needs review by an OTC member and removed triaged: refactor The issue/pr requests/implements refactoring labels May 16, 2024
@paulidale paulidale removed the approval: otc review pending This pull request needs review by an OTC member label May 16, 2024
@t8m
Copy link
Member

t8m commented May 17, 2024

This means OPENSSL_cpuid_setup() will be called twice on platforms that have it as a constructor/DEP init. Should we drop the attribute from it and call it from another call marked as constructor, that won't be compiled into FIPS_MODULE?

@t8m
Copy link
Member

t8m commented May 17, 2024

This means OPENSSL_cpuid_setup() will be called twice on platforms that have it as a constructor/DEP init. Should we drop the attribute from it and call it from another call marked as constructor, that won't be compiled into FIPS_MODULE?

IMO that should be done at least for the master branch. For stable branches we should IMO skip this call on platforms which mark it as constructor to minimize the changes.

@ZenithalHourlyRate
Copy link
Contributor Author

Should we drop the attribute from it and call it from another call marked as constructor, that won't be compiled into FIPS_MODULE?

I think libcrypto also does not need such another constructor as crypto/init.c also explicitly initialized cpuid

Also, calling cpuid_setup twice should not be harmful.

@t8m
Copy link
Member

t8m commented May 17, 2024

I think libcrypto also does not need such another constructor as crypto/init.c also explicitly initialized cpuid

The problem is that this init function might not be called in some circumstances such as when only the deprecated low-level functions are being called by an application.

Also, calling cpuid_setup twice should not be harmful.

Yeah, that's right. And given it is already called twice for libcrypto, let's ignore this issue for now.

@t8m t8m 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 17, 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 18, 2024
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@t8m
Copy link
Member

t8m commented May 20, 2024

Merged to all the active branches. Thank you for your contribution.

@t8m t8m closed this May 20, 2024
@sanumesh
Copy link
Contributor

Is this fix already pushed ? When will it be available in branch ?

openssl-machine pushed a commit that referenced this pull request May 22, 2024
Fixes: #23979

Previously fips module relied on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24419)

(cherry picked from commit a192b24)
openssl-machine pushed a commit that referenced this pull request May 22, 2024
Fixes: #23979

Previously fips module relied on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24419)

(cherry picked from commit a192b24)
openssl-machine pushed a commit that referenced this pull request May 22, 2024
Fixes: #23979

Previously fips module relied on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24419)

(cherry picked from commit a192b24)
openssl-machine pushed a commit that referenced this pull request May 22, 2024
Fixes: #23979

Previously fips module relied on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24419)
openssl-machine pushed a commit that referenced this pull request May 22, 2024
Fixes: #23979

Previously fips module relied on OPENSSL_cpuid_setup
being used as constructor by the linker to correctly
setup the capability vector, either via .section .init
(for x86_64) or via __attribute__((constructor)).

This would make ld.so call OPENSSL_cpuid_setup before
the init function for fips module. However, this early
constructing behavior has several disadvantages:

1. Not all platform/toolchain supports such behavior

2. Initialisation sequence is not well defined, and
some function might not be initialized when cpuid_setup
is called

3. Implicit path is hard to maintain and debug

Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #24419)

(cherry picked from commit a192b24)
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 branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 branch: 3.2 Merge to openssl-3.2 branch: 3.3 Merge to openssl-3.3 severity: fips change The pull request changes FIPS provider sources tests: deferred Tests will be added in a subsequent PR (label should be removed when the PR with tests is merged) triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

optimization capabilities missing in openssl fips provider
6 participants