From 3fb33cdc4e97b597930a3c49ffa659767f6b1d2b Mon Sep 17 00:00:00 2001 From: Paul Aurich Date: Sat, 7 Sep 2013 14:44:23 -0700 Subject: [PATCH] context: Don't leak EC_KEY in set_curve() SSL_CTX_set_tmp_ecdh() takes a reference to the provided key. ==8323== 1,044 (56 direct, 988 indirect) bytes in 1 blocks are definitely lost in loss record 611 of 631 ==8323== at 0x4C2935B: malloc (vg_replace_malloc.c:270) ==8323== by 0x5E05D9F: CRYPTO_malloc (mem.c:308) ==8323== by 0x5E59859: EC_KEY_new (ec_key.c:75) ==8323== by 0x5E59974: EC_KEY_new_by_curve_name (ec_key.c:96) ==8323== by 0x4E395A7: set_curve (context.c:261) ... --- src/context.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/context.c b/src/context.c index 41bfa15..a3f9cde 100644 --- a/src/context.c +++ b/src/context.c @@ -507,15 +507,22 @@ static int set_curve(lua_State *L) #else static int set_curve(lua_State *L) { + long ret; SSL_CTX *ctx = lsec_checkcontext(L, 1); const char *str = luaL_checkstring(L, 2); EC_KEY *key = find_ec_key(str); + if (!key) { lua_pushboolean(L, 0); lua_pushstring(L, "elliptic curve not supported"); return 2; } - if (!SSL_CTX_set_tmp_ecdh(ctx, key)) { + + ret = SSL_CTX_set_tmp_ecdh(ctx, key); + /* SSL_CTX_set_tmp_ecdh takes its own reference */ + EC_KEY_free(key); + + if (!ret) { lua_pushboolean(L, 0); lua_pushstring(L, "error setting elliptic curve"); return 2;