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

Using old pointer after reallocating new buffer #2

Open
wants to merge 1 commit into
base: EC_MOCK_PEER
Choose a base branch
from

Conversation

kshitizvars
Copy link

Below issue is reported while running TLS1.3:-

p11prov_DeriveKey:Host out of memory

Issue occurs because we are trying to use deallocated memory pointer after reallocating new memory due to which key->attrs get corrupted and finally cause issue in importing correct peer key.

Copy link
Owner

@simo5 simo5 left a comment

Choose a reason for hiding this comment

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

Nice catch, but the fix can be simplified and corrected according to my comment.

src/objects.c Outdated
void *ptr = OPENSSL_realloc(key->attrs, sizeof(CK_ATTRIBUTE) * (key->numattrs + add_attrs));
if (!ptr) {
key->attrs = OPENSSL_realloc(key->attrs, sizeof(CK_ATTRIBUTE) * (key->numattrs + add_attrs));
if (!key->attrs) {
Copy link
Owner

Choose a reason for hiding this comment

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

On failure realloc will leave the original pointer untouched, so the correct way is to use ptr, and only if it is not null then key->attrs = ptr;

That was the original intention of this code and the bug is the missing overwriting of key->attrs after the null check.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @simo5

Updated the code, thanks for the pointer.

Below issue is reported while running TLS1.3:-

p11prov_DeriveKey:Host out of memory

Issue occurs because we are trying to use deallocated memory pointer after
reallocating new memory due to which key->attrs get corrupted and finally
cause issue in importing correct peer key.

Signed-off-by: Kshitiz Varshney <[email protected]>
@simo5
Copy link
Owner

simo5 commented Sep 4, 2024

Oh I just realized this PR is on my personal repo, I will integrate the original patch and your fix on top of another PR I am working on in the same area, but it may take me a few days.
I will tag you when I do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants