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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions scripts/cocci/null-after-free.cocci
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Read a list of 'free' type function names from stdin, if stdin is a pipe.
@initialize:python@
@@
import sys
functions = set(['free'])
if not sys.stdin.isatty():
for line in sys.stdin:
f = line.rstrip('\n')
functions.add(f)

// Match all function names.
@a@
identifier F;
@@
F(...)

// Match all function names in the set.
@script:python b depends on a@
F << a.F;
f;
@@
if F in functions:
coccinelle.f = F
cocci.include_match(True)

// Remove casts.
@@
identifier b.f;
type t;
expression x;
@@
- f((t *)(x))
+ f(x)

// Add NULL assignment after free, if absent.
@@
identifier b.f;
expression x;
@@
(
f(x);
x = NULL;
|
f(x);
+ x = NULL;
)

// Remove NULL assignments to local variables.
@@
local idexpression x;
@@
- x = NULL;
... when != x

// Remove NULL assignments followed later by another assignment.
@@
idexpression x;
identifier y;
@@
(
- x = NULL;
... when != x
x = ...;
|
- x->y = NULL;
... when != x->y
x->y = ...;
)

// Remove NULL assignments followed immediately by another assignment.
@@
expression z;
@@
- z = NULL;
z = ...;

// Remove NULL assignments to structure members before the structure is freed.
vixentael marked this conversation as resolved.
Show resolved Hide resolved
@@
identifier b.f;
identifier x, y;
@@
- x->y = NULL;
... when != x
f(x);
12 changes: 12 additions & 0 deletions scripts/cocci/themis-free-functions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
soter_hash_destroy
soter_rsa_key_pair_gen_destroy
soter_sym_ctx_destroy
soter_asym_cipher_destroy
soter_hmac_destroy
soter_sign_destroy
soter_verify_destroy
themis_message_destroy
themis_secure_message_ec_encrypter_destroy
themis_secure_message_rsa_encrypter_destroy
themis_secure_message_signer_destroy
themis_secure_message_verifier_destroy
4 changes: 4 additions & 0 deletions src/soter/soter_hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ soter_status_t soter_hmac_init(soter_hmac_ctx_t* hmac_ctx,
res = soter_hash_update(hmac_ctx->hash_ctx, key, key_length);
if (SOTER_SUCCESS != res) {
soter_hash_destroy(hmac_ctx->hash_ctx);
hmac_ctx->hash_ctx = NULL;
return res;
}

res = soter_hash_final(hmac_ctx->hash_ctx, hmac_ctx->o_key_pad, &o_key_pad_length);
if (SOTER_SUCCESS != res) {
soter_hash_destroy(hmac_ctx->hash_ctx);
hmac_ctx->hash_ctx = NULL;
return res;
}
} else {
Expand All @@ -88,12 +90,14 @@ soter_status_t soter_hmac_init(soter_hmac_ctx_t* hmac_ctx,
res = soter_hash_init(hmac_ctx->hash_ctx, algo);
if (res != SOTER_SUCCESS) {
soter_hash_destroy(hmac_ctx->hash_ctx);
hmac_ctx->hash_ctx = NULL;
return SOTER_FAIL;
}

res = soter_hash_update(hmac_ctx->hash_ctx, i_key_pad, block_size);
if (SOTER_SUCCESS != res) {
soter_hash_destroy(hmac_ctx->hash_ctx);
hmac_ctx->hash_ctx = NULL;
soter_hmac_cleanup(hmac_ctx);
return res;
}
Expand Down
1 change: 0 additions & 1 deletion src/themis/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,5 @@ themis_status_t themis_message_destroy(themis_message_t* ctx)
free(ctx->data);
}
free(ctx);
ctx = NULL;
return THEMIS_SUCCESS;
}
2 changes: 0 additions & 2 deletions src/themis/secure_message_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ themis_secure_message_signer_t* themis_secure_message_signer_init(const uint8_t*
if (!ctx) {
return NULL;
}
ctx->sign_ctx = NULL;
ctx->sign_ctx = soter_sign_create(get_alg_id(key, key_length), NULL, 0, key, key_length);
if (!(ctx->sign_ctx)) {
free(ctx);
Expand Down Expand Up @@ -117,7 +116,6 @@ themis_status_t themis_secure_message_signer_proceed(themis_secure_message_signe
themis_status_t themis_secure_message_signer_destroy(themis_secure_message_signer_t* ctx)
{
soter_sign_destroy(ctx->sign_ctx);
ctx->sign_ctx = NULL;
free(ctx);
return THEMIS_SUCCESS;
}
Expand Down