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

Add a semantic patch to prevent use after free. #546

Merged
merged 1 commit into from
Oct 1, 2019

Conversation

mansourmoufid
Copy link
Contributor

Hello, thank you for this excellent project.

In the file soter_hmac.c there is a double-free.

soter_hash_destroy(hmac_ctx->hash_ctx);
soter_hmac_cleanup(hmac_ctx);

The function soter_hmac_cleanup immediately follows soter_hash_destroy.

The first function frees hmac_ctx->hash_ctx:

soter_status_t soter_hash_destroy(soter_hash_ctx_t* hash_ctx)
{
if (!hash_ctx) {
return SOTER_INVALID_PARAMETER;
}
soter_hash_cleanup(hash_ctx);
free(hash_ctx);
return SOTER_SUCCESS;
}

The second function calls the first function again, with the same argument:

soter_status_t soter_hmac_cleanup(soter_hmac_ctx_t* hmac_ctx)
{
if (NULL == hmac_ctx) {
return SOTER_INVALID_PARAMETER;
}
memset(hmac_ctx->o_key_pad, 0, sizeof(hmac_ctx->o_key_pad));
soter_hash_destroy(hmac_ctx->hash_ctx);
hmac_ctx->hash_ctx = NULL;
return SOTER_SUCCESS;
}

This can be prevented by inserting a NULL assignment between the two function calls.

Here is a semantic patch (see Coccinelle) that does this for all such functions (not only in this specific instance, although it turns out only this one instance was a bug).

The file themis-free-functions.txt contains a list of all functions that free one of their arguments. The file null-after-free.cocci is a semantic patch that inserts a NULL assignment after every call to each of the functions in the previous file, and then attempts to remove unnecessary NULL assignments.

Apply this semantic patch with the command:

spatch --sp-file scripts/cocci/null-after-free.cocci --dir src --in-place < scripts/cocci/themis-free-functions.txt 

@vixentael vixentael added bug core Themis Core written in C, its packages labels Sep 30, 2019
@vixentael
Copy link
Contributor

Thank you, @eliteraspberries! This looks fantastic (especially semantic patch) 👍

Double free fixes are very timely: we did some of them during last month (#525, #535, #524). Great job!

@ilammy
Copy link
Collaborator

ilammy commented Oct 1, 2019

Wow! Coccinelle... That’s a tool I’ve seen used mostly in Linux kernel development. Maybe because it’s one of the few "big" C projects that warrant its use. Or because people somehow rely more on their IDEs for refactoring.

Copy link
Collaborator

@ilammy ilammy left a comment

Choose a reason for hiding this comment

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

The changes look sound. Sign me up for merging this.

@eliteraspberries, thank you for your contribution! ❤️

Double kudos for showing an example of Coccinelle. I hope we’d make a good use of this amazing tool for future refactoring.

scripts/cocci/null-after-free.cocci Show resolved Hide resolved
@ilammy
Copy link
Collaborator

ilammy commented Oct 1, 2019

P.S. Some notes if anyone wants to apply the semantic patch from source. On my Linux box Coccinelle from repos works without an issue. On macOS Coccinelle from Homebrew does not like Homebrew’s Python 2 (python@2), removing it and leaving only system Python and Python 3 from Homebrew is enough to make it work.

@ilammy ilammy merged commit 34f361b into cossacklabs:master Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug core Themis Core written in C, its packages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants