Skip to content

Fix various function pointer UBSAN - #31523

Closed
npajkovsky wants to merge 8 commits into
openssl:masterfrom
npajkovsky:ossl-ubsan
Closed

npajkovsky wants to merge 8 commits into
openssl:masterfrom
npajkovsky:ossl-ubsan

Conversation

@npajkovsky

Copy link
Copy Markdown

This PR contains a bunch of UBSANs in various places. All of them are issues with casting function pointers.

Read it commit by commit; every ubsan is explained, and self-contained.

call OPENSSL_sk_set_thunks for sk_${nametype}_new and
sk_${nametype}_new_reserve, and wrap the long generated stack
constructor and deep-copy macros across multiple lines so the nested
thunk setup is easier to read and maintain.

Signed-off-by: Nikola Pajkovsky <[email protected]>
ensure typed stack duplicates keep their compare, copy, and free thunks so
later operations do not call typed callbacks through erased void * function
pointer signatures.

Signed-off-by: Nikola Pajkovsky <[email protected]>
d2i_OCSP_REQUEST/d2i_OCSP_RESPONSE are declared with type-specific
signatures returning OCSP_REQUEST*/OCSP_RESPONSE*, but the
d2i_OCSP_{REQUEST,RESPONSE}_bio macros routed them through
ASN1_d2i_bio_of -> ASN1_d2i_bio, which calls them via a
d2i_of_void* pointer:

    void *(*)(void **, const unsigned char **, long)

The analogous i2d_OCSP_{REQUEST,RESPONSE}_bio macros did the same
through ASN1_i2d_bio_of -> ASN1_i2d_bio with i2d_of_void*.

Rewrite the macros to dispatch through
ASN1_item_d2i_bio/ASN1_item_i2d_bio with ASN1_ITEM_rptr(OCSP_*)
directly. This eliminates the function-pointer cast entirely; both old
and new paths bottom out in the same ASN1_item_d2i_ex / ASN1_item_i2d
call, so behavior is identical.

Signed-off-by: Nikola Pajkovsky <[email protected]>
PEM_X509_INFO_read_bio_ex stored d2i_X509, d2i_X509_AUX, d2i_X509_CRL
or d2i_AutoPrivateKey -- each with a distinct, type-specific signature
returning X509*, X509_CRL* or EVP_PKEY* -- in a single d2i_of_void*
variable and dispatched through it:

    d2i = (D2I_OF(void))d2i_X509;
    ...
    d2i(pp, &p, len)

Replace the function-pointer dispatch with an enum tag
(pem_info_type) and a switch that calls each typed d2i_* directly
with the correctly type.

Signed-off-by: Nikola Pajkovsky <[email protected]>
decode PEM payloads through the generated typed d2i_* functions in the
PEM read macros instead of routing them through the d2i_of_void
callback path. This avoids UBSan function type mismatch reports while
preserving the public PEM reader APIs.

Signed-off-by: Nikola Pajkovsky <[email protected]>
use IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME) so duplication goes
through ASN1_item_dup() instead of calling ASN1_dup() with cast
i2d/d2i function pointers. This avoids UBSan function type errors
while preserving the public GENERAL_NAME_dup() API.

Signed-off-by: Nikola Pajkovsky <[email protected]>
the d2i_*/i2d_* fp and bio wrappers for RSA_PUBKEY, the DSA and EC key
types, and the EVP_PKEY Private/PUBKEY converters passed their
type-specific functions to ASN1_{d2i,i2d}_{fp,bio}, which invoke them
through the generic d2i_of_void / i2d_of_void / void *(*)(void) pointer
types.

Add file-local thunks with the exact generic signatures that forward to
the concrete functions, and pass those instead of cast pointers. This
removes the mismatched calls while keeping the public API unchanged.

Signed-off-by: Nikola Pajkovsky <[email protected]>
@npajkovsky
npajkovsky requested a review from nhorman June 15, 2026 17:47
@npajkovsky npajkovsky self-assigned this Jun 15, 2026
@github-actions github-actions Bot added the severity: fips change The pull request changes FIPS provider sources label Jun 15, 2026
@bob-beck

Copy link
Copy Markdown
Contributor

So have we decided between this PR and Neil's PR also thunking?

@nhorman ?

@nhorman

nhorman commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

I'm honestly not sure, as I don't know which issues in the thinking epic this pr covers and what (if anything) is unique in mine.

@npajkovsky

Copy link
Copy Markdown
Author

@bob-beck @nhorman this PR has existed before tickets in epic, and since @mbroz pulled @nhorman before the PR was merged, and @nhorman is pretty fast in coding, we've started overlapping. Hopefully, after this PR, there will be no overlap.

@mbroz

mbroz commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

The external test fail looks related to this PR.

@mbroz mbroz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If external test is unrelated, LGTM.

@openssl-machine openssl-machine 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 Jun 24, 2026
@mbroz mbroz added the branch: master Applies to master branch label Jun 24, 2026
@nhorman

nhorman commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

@bob-beck @nhorman this PR has existed before tickets in epic, and since @mbroz pulled @nhorman before the PR was merged, and @nhorman is pretty fast in coding, we've started overlapping. Hopefully, after this PR, there will be no overlap.

@nikolapajkovsky ok, that explains it, thank you for clarifying, though I ran this pr under ubsan, and all the errors fixed in #31641 still seem to be present, so I'm wondering how much overlap there actually is here.
I suppose the best path forward is to complete this PR, merge it, then rebase #31641 to see what conflicts there are and determine the delta to see if there is a net gain.

@nhorman

nhorman commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

The external test fail looks related to this PR.

@mbroz I think that external test error happens because this PR was branched prior to several fixes that went in to address various pkcs11 errors. I imagine a rebase of this PR to the head of master will resolve them.

@npajkovsky

Copy link
Copy Markdown
Author

@bob-beck @nhorman this PR has existed before tickets in epic, and since @mbroz pulled @nhorman before the PR was merged, and @nhorman is pretty fast in coding, we've started overlapping. Hopefully, after this PR, there will be no overlap.

@nikolapajkovsky ok, that explains it, thank you for clarifying, though I ran this pr under ubsan, and all the errors fixed in #31641 still seem to be present, so I'm wondering how much overlap there actually is here. I suppose the best path forward is to complete this PR, merge it, then rebase #31641 to see what conflicts there are and determine the delta to see if there is a net gain.

That would be the preferred way to continue. Thank you.

@npajkovsky

Copy link
Copy Markdown
Author

The external test fail looks related to this PR.

@mbroz I think that external test error happens because this PR was branched prior to several fixes that went in to address various pkcs11 errors. I imagine a rebase of this PR to the head of master will resolve them.

I can do a rebase for sure. However, we already know that pkcs#11 is fixed on master, and the error is not related to this PR.

If you want to have a clean CI before merge, just shout out loud.

@openssl-machine

Copy link
Copy Markdown
Collaborator

24 hours has passed since 'approval: done' was set, but as this PR has been updated in that time the label 'approval: ready to merge' is not being automatically set. Please review the updates and set the label manually.

@nhorman nhorman 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 Jun 25, 2026
@nhorman

nhorman commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

merged to master, thank you

@nhorman nhorman closed this Jun 25, 2026
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
call OPENSSL_sk_set_thunks for sk_${nametype}_new and
sk_${nametype}_new_reserve, and wrap the long generated stack
constructor and deep-copy macros across multiple lines so the nested
thunk setup is easier to read and maintain.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:46 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
ensure typed stack duplicates keep their compare, copy, and free thunks so
later operations do not call typed callbacks through erased void * function
pointer signatures.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:48 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
d2i_OCSP_REQUEST/d2i_OCSP_RESPONSE are declared with type-specific
signatures returning OCSP_REQUEST*/OCSP_RESPONSE*, but the
d2i_OCSP_{REQUEST,RESPONSE}_bio macros routed them through
ASN1_d2i_bio_of -> ASN1_d2i_bio, which calls them via a
d2i_of_void* pointer:

    void *(*)(void **, const unsigned char **, long)

The analogous i2d_OCSP_{REQUEST,RESPONSE}_bio macros did the same
through ASN1_i2d_bio_of -> ASN1_i2d_bio with i2d_of_void*.

Rewrite the macros to dispatch through
ASN1_item_d2i_bio/ASN1_item_i2d_bio with ASN1_ITEM_rptr(OCSP_*)
directly. This eliminates the function-pointer cast entirely; both old
and new paths bottom out in the same ASN1_item_d2i_ex / ASN1_item_i2d
call, so behavior is identical.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:50 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
PEM_X509_INFO_read_bio_ex stored d2i_X509, d2i_X509_AUX, d2i_X509_CRL
or d2i_AutoPrivateKey -- each with a distinct, type-specific signature
returning X509*, X509_CRL* or EVP_PKEY* -- in a single d2i_of_void*
variable and dispatched through it:

    d2i = (D2I_OF(void))d2i_X509;
    ...
    d2i(pp, &p, len)

Replace the function-pointer dispatch with an enum tag
(pem_info_type) and a switch that calls each typed d2i_* directly
with the correctly type.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:53 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:55 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
decode PEM payloads through the generated typed d2i_* functions in the
PEM read macros instead of routing them through the d2i_of_void
callback path. This avoids UBSan function type mismatch reports while
preserving the public PEM reader APIs.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:57 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
use IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME) so duplication goes
through ASN1_item_dup() instead of calling ASN1_dup() with cast
i2d/d2i function pointers. This avoids UBSan function type errors
while preserving the public GENERAL_NAME_dup() API.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:59 2026
(Merged from #31523)
openssl-machine pushed a commit that referenced this pull request Jun 25, 2026
the d2i_*/i2d_* fp and bio wrappers for RSA_PUBKEY, the DSA and EC key
types, and the EVP_PKEY Private/PUBKEY converters passed their
type-specific functions to ASN1_{d2i,i2d}_{fp,bio}, which invoke them
through the generic d2i_of_void / i2d_of_void / void *(*)(void) pointer
types.

Add file-local thunks with the exact generic signatures that forward to
the concrete functions, and pass those instead of cast pointers. This
removes the mismatched calls while keeping the public API unchanged.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:13:01 2026
(Merged from #31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
call OPENSSL_sk_set_thunks for sk_${nametype}_new and
sk_${nametype}_new_reserve, and wrap the long generated stack
constructor and deep-copy macros across multiple lines so the nested
thunk setup is easier to read and maintain.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:46 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
ensure typed stack duplicates keep their compare, copy, and free thunks so
later operations do not call typed callbacks through erased void * function
pointer signatures.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:48 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
d2i_OCSP_REQUEST/d2i_OCSP_RESPONSE are declared with type-specific
signatures returning OCSP_REQUEST*/OCSP_RESPONSE*, but the
d2i_OCSP_{REQUEST,RESPONSE}_bio macros routed them through
ASN1_d2i_bio_of -> ASN1_d2i_bio, which calls them via a
d2i_of_void* pointer:

    void *(*)(void **, const unsigned char **, long)

The analogous i2d_OCSP_{REQUEST,RESPONSE}_bio macros did the same
through ASN1_i2d_bio_of -> ASN1_i2d_bio with i2d_of_void*.

Rewrite the macros to dispatch through
ASN1_item_d2i_bio/ASN1_item_i2d_bio with ASN1_ITEM_rptr(OCSP_*)
directly. This eliminates the function-pointer cast entirely; both old
and new paths bottom out in the same ASN1_item_d2i_ex / ASN1_item_i2d
call, so behavior is identical.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:50 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
PEM_X509_INFO_read_bio_ex stored d2i_X509, d2i_X509_AUX, d2i_X509_CRL
or d2i_AutoPrivateKey -- each with a distinct, type-specific signature
returning X509*, X509_CRL* or EVP_PKEY* -- in a single d2i_of_void*
variable and dispatched through it:

    d2i = (D2I_OF(void))d2i_X509;
    ...
    d2i(pp, &p, len)

Replace the function-pointer dispatch with an enum tag
(pem_info_type) and a switch that calls each typed d2i_* directly
with the correctly type.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:53 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:55 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
decode PEM payloads through the generated typed d2i_* functions in the
PEM read macros instead of routing them through the d2i_of_void
callback path. This avoids UBSan function type mismatch reports while
preserving the public PEM reader APIs.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:57 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
use IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME) so duplication goes
through ASN1_item_dup() instead of calling ASN1_dup() with cast
i2d/d2i function pointers. This avoids UBSan function type errors
while preserving the public GENERAL_NAME_dup() API.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:59 2026
(Merged from openssl#31523)
n13l pushed a commit to n13l/openssl that referenced this pull request Jul 25, 2026
the d2i_*/i2d_* fp and bio wrappers for RSA_PUBKEY, the DSA and EC key
types, and the EVP_PKEY Private/PUBKEY converters passed their
type-specific functions to ASN1_{d2i,i2d}_{fp,bio}, which invoke them
through the generic d2i_of_void / i2d_of_void / void *(*)(void) pointer
types.

Add file-local thunks with the exact generic signatures that forward to
the concrete functions, and pass those instead of cast pointers. This
removes the mismatched calls while keeping the public API unchanged.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:13:01 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
call OPENSSL_sk_set_thunks for sk_${nametype}_new and
sk_${nametype}_new_reserve, and wrap the long generated stack
constructor and deep-copy macros across multiple lines so the nested
thunk setup is easier to read and maintain.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:46 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
ensure typed stack duplicates keep their compare, copy, and free thunks so
later operations do not call typed callbacks through erased void * function
pointer signatures.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:48 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
d2i_OCSP_REQUEST/d2i_OCSP_RESPONSE are declared with type-specific
signatures returning OCSP_REQUEST*/OCSP_RESPONSE*, but the
d2i_OCSP_{REQUEST,RESPONSE}_bio macros routed them through
ASN1_d2i_bio_of -> ASN1_d2i_bio, which calls them via a
d2i_of_void* pointer:

    void *(*)(void **, const unsigned char **, long)

The analogous i2d_OCSP_{REQUEST,RESPONSE}_bio macros did the same
through ASN1_i2d_bio_of -> ASN1_i2d_bio with i2d_of_void*.

Rewrite the macros to dispatch through
ASN1_item_d2i_bio/ASN1_item_i2d_bio with ASN1_ITEM_rptr(OCSP_*)
directly. This eliminates the function-pointer cast entirely; both old
and new paths bottom out in the same ASN1_item_d2i_ex / ASN1_item_i2d
call, so behavior is identical.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:50 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
PEM_X509_INFO_read_bio_ex stored d2i_X509, d2i_X509_AUX, d2i_X509_CRL
or d2i_AutoPrivateKey -- each with a distinct, type-specific signature
returning X509*, X509_CRL* or EVP_PKEY* -- in a single d2i_of_void*
variable and dispatched through it:

    d2i = (D2I_OF(void))d2i_X509;
    ...
    d2i(pp, &p, len)

Replace the function-pointer dispatch with an enum tag
(pem_info_type) and a switch that calls each typed d2i_* directly
with the correctly type.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:53 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:55 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
decode PEM payloads through the generated typed d2i_* functions in the
PEM read macros instead of routing them through the d2i_of_void
callback path. This avoids UBSan function type mismatch reports while
preserving the public PEM reader APIs.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:57 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
use IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME) so duplication goes
through ASN1_item_dup() instead of calling ASN1_dup() with cast
i2d/d2i function pointers. This avoids UBSan function type errors
while preserving the public GENERAL_NAME_dup() API.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:12:59 2026
(Merged from openssl#31523)
rickyringler pushed a commit to rickyringler/openssl that referenced this pull request Aug 21, 2026
the d2i_*/i2d_* fp and bio wrappers for RSA_PUBKEY, the DSA and EC key
types, and the EVP_PKEY Private/PUBKEY converters passed their
type-specific functions to ASN1_{d2i,i2d}_{fp,bio}, which invoke them
through the generic d2i_of_void / i2d_of_void / void *(*)(void) pointer
types.

Add file-local thunks with the exact generic signatures that forward to
the concrete functions, and pass those instead of cast pointers. This
removes the mismatched calls while keeping the public API unchanged.

Signed-off-by: Nikola Pajkovsky <[email protected]>

Reviewed-by: Neil Horman <[email protected]>
Reviewed-by: Milan Broz <[email protected]>
MergeDate: Thu Jun 25 12:13:01 2026
(Merged from openssl#31523)
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 Applies to master branch severity: fips change The pull request changes FIPS provider sources

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix various function pointer UBSAN

6 participants