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

provider: return error if buf too small when get ec pubkey param #20890

Closed

Conversation

liyi77
Copy link
Contributor

@liyi77 liyi77 commented May 5, 2023

FIX: #20889
Should check buffer size first when get ec pubkey param, if buffer size too-small, return -1 as error code.

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

@liyi77 liyi77 force-pushed the ret-error-when-ecpubkey-too-small branch 3 times, most recently from 20a10b0 to 910c9f4 Compare May 5, 2023 17:16
@paulidale paulidale added branch: master Merge to master branch approval: review pending This pull request needs review by a committer triaged: bug The issue/pr is/fixes a bug branch: 3.0 Merge to openssl-3.0 branch branch: 3.1 Merge to openssl-3.1 tests: exempted The PR is exempt from requirements for testing labels May 7, 2023
@slontis slontis added hold: needs tests The PR needs tests to be added to it and removed tests: exempted The PR is exempt from requirements for testing labels May 8, 2023
@slontis
Copy link
Member

slontis commented May 8, 2023

Please add a test for these conditions...

You could add something to an existing test like the following
see evp_extra_test.c

static int test_EC_priv_pub(void)
{
    ....
    ....
    size_t len = 0;
    unsigned char buffer[128];
    ....
    ....
    ....
    *** Add this after the EVP_PKEY_get1_encoded_public_key() calls..
    
    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
                                                     buffer, sizeof(buffer), &len), 1)
        || !TEST_int_eq(len, 65))
        goto err;

    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
                                                     NULL, 0, &len), 1)
        || !TEST_int_eq(len, 65))
        goto err;

    if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
                                                     OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
                                                     buffer, 10, &len), 0)
        || !TEST_int_le(len, 10))
        goto err;

@liyi77 liyi77 force-pushed the ret-error-when-ecpubkey-too-small branch from 910c9f4 to 99e6d7d Compare May 8, 2023 06:07
@liyi77
Copy link
Contributor Author

liyi77 commented May 8, 2023

Thanks for review, patch updated.

@liyi77 liyi77 requested review from slontis and paulidale May 8, 2023 06:28
@liyi77 liyi77 closed this May 8, 2023
@liyi77 liyi77 reopened this May 8, 2023
if (!TEST_int_eq(EVP_PKEY_get_octet_string_param(params_and_pub,
OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
buffer, 10, &len), 0)
|| !TEST_int_eq(len, 65))
Copy link
Member

Choose a reason for hiding this comment

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

This test is not right,,,
If the above test was to magically pass (which it wont), then we would expect the returned len <= 10 if we passed in a max buffer of 10. If it returned 65 it would indicate we just trashed the buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This test is not right,,, If the above test was to magically pass (which it wont), then we would expect the returned len <= 10 if we passed in a max buffer of 10. If it returned 65 it would indicate we just trashed the buffer.

This case means if we input a too-short buffer, this function will not change the value of len (which was set to 65 in the previous testcase).
It's really confusing here, maybe we can set the value of len to 0 before this test.

int EVP_PKEY_get_octet_string_param()
{
    OSSL_PARAM params[2];
    int ret1 = 0, ret2 = 0;
......
   /* return 0 here */
    if ((ret1 = EVP_PKEY_get_params(pkey, params)))
        ret2 = OSSL_PARAM_modified(params);
   /* will not change out_len here */
    if (ret2 && out_len != NULL)
        *out_len = params[0].return_size;
    return ret1 && ret2;
}

Copy link
Member

Choose a reason for hiding this comment

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

I do NOT think we should check the return size is assigned in case of failure. IMO this is implementation-defined behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds reasonable.
There is no reason for the caller to check out_len if the function has returned an error value

@liyi77 liyi77 force-pushed the ret-error-when-ecpubkey-too-small branch from 99e6d7d to a025d50 Compare May 9, 2023 06:33
@liyi77
Copy link
Contributor Author

liyi77 commented May 9, 2023

Thanks for review, patch updated.

providers/implementations/keymgmt/ec_kmgmt.c Outdated Show resolved Hide resolved
@github-actions github-actions bot added the severity: fips change The pull request changes FIPS provider sources label May 9, 2023
FIX: openssl#20889
Should check buffer size when get ec pubkey param,
if buffer is not NULL and size too-small, return 0 as error code.

Signed-off-by: Yi Li <yi1.li@intel.com>
@liyi77 liyi77 force-pushed the ret-error-when-ecpubkey-too-small branch from a025d50 to 01a84ab Compare May 9, 2023 16:21
@t8m t8m removed the hold: needs tests The PR needs tests to be added to it label May 10, 2023
@t8m t8m added the tests: present The PR has suitable tests present label May 10, 2023
@t8m
Copy link
Member

t8m commented May 10, 2023

@paulidale please reconfirm

@paulidale paulidale 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 11, 2023
@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 12, 2023
@openssl-machine
Copy link
Collaborator

This pull request is ready to merge

@t8m
Copy link
Member

t8m commented May 12, 2023

Merged to master, 3.1, and 3.0 branches. Thank you for your contribution.

@t8m t8m closed this May 12, 2023
openssl-machine pushed a commit that referenced this pull request May 12, 2023
Fixes #20889

There was an incorrect value passed to EC_POINT_point2oct() for the
buffer size of the param passed-in.

Added testcases.

Signed-off-by: Yi Li <yi1.li@intel.com>

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #20890)
openssl-machine pushed a commit that referenced this pull request May 12, 2023
Fixes #20889

There was an incorrect value passed to EC_POINT_point2oct() for the
buffer size of the param passed-in.

Added testcases.

Signed-off-by: Yi Li <yi1.li@intel.com>

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #20890)

(cherry picked from commit 9107087)
openssl-machine pushed a commit that referenced this pull request May 12, 2023
Fixes #20889

There was an incorrect value passed to EC_POINT_point2oct() for the
buffer size of the param passed-in.

Added testcases.

Signed-off-by: Yi Li <yi1.li@intel.com>

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #20890)

(cherry picked from commit 9107087)
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 severity: fips change The pull request changes FIPS provider sources tests: present The PR has suitable tests present triaged: bug The issue/pr is/fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should return error if buffer too small when get ec pubkey param
5 participants