From 019df8e4c5a5cf16fb77038d0049ad8b0a4999de Mon Sep 17 00:00:00 2001 From: TLINDEN Date: Thu, 7 Aug 2014 21:33:52 +0200 Subject: [PATCH] fixed memory leaks --- libpcp/mem.c | 2 +- libpcp/mgmt.c | 16 +++++++++------- libpcp/vault.c | 9 +++++++-- src/encryption.c | 23 +++++++++++------------ src/keymgmt.c | 2 -- src/pcp.c | 3 ++- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/libpcp/mem.c b/libpcp/mem.c index 5734954..b0810f0 100644 --- a/libpcp/mem.c +++ b/libpcp/mem.c @@ -38,7 +38,7 @@ void *ucmalloc(size_t s) { memset (value, 0, size); - /* printf("allocated %d bytes at %p\n", (int)size, value); */ + /* printf("allocated %ld bytes at %p\n", size, value); */ return value; } diff --git a/libpcp/mgmt.c b/libpcp/mgmt.c index c440a71..810e69f 100644 --- a/libpcp/mgmt.c +++ b/libpcp/mgmt.c @@ -71,8 +71,6 @@ int _check_keysig_h(PCPCTX *ptx, Buffer *blob, rfc_pub_sig_h *h) { } int _check_sigsubs(PCPCTX *ptx, Buffer *blob, pcp_pubkey_t *p, rfc_pub_sig_s *subheader) { - byte *ignore = ucmalloc(32); - if(subheader->size > buffer_left(blob)) { fatal(ptx, "Invalid header size %ld specified in source\n", subheader->size); return 1; @@ -126,7 +124,7 @@ int _check_sigsubs(PCPCTX *ptx, Buffer *blob, pcp_pubkey_t *p, rfc_pub_sig_s *su is already known from the key ctime. This may change in the future though. */ - if(buffer_get_chunk(blob, ignore, subheader->size) == 0) { + if(buffer_fwd_offset(blob, subheader->size) == 0) { fatal(ptx, "Invalid 'unsupported' notation, expected %ld bytes, but got 0\n", subheader->size); return 1; } @@ -267,10 +265,11 @@ pcp_ks_bundle_t *pcp_import_pub(PCPCTX *ptx, byte *raw, size_t rawsize) { } pcp_ks_bundle_t *pcp_import_pub_rfc(PCPCTX *ptx, Buffer *blob) { - pcp_pubkey_t *p = ucmalloc(sizeof(pcp_pubkey_t)); - pcp_keysig_t *sk = ucmalloc(sizeof(pcp_keysig_t)); + pcp_keysig_t *sk = NULL; + pcp_ks_bundle_t *b = NULL; rfc_pub_sig_h *sigheader = ucmalloc(sizeof(rfc_pub_sig_h)); rfc_pub_sig_s *subheader = ucmalloc(sizeof(rfc_pub_sig_s)); + pcp_pubkey_t *p = ucmalloc(sizeof(pcp_pubkey_t)); if(buffer_done(blob)) goto be; p->ctime = buffer_get64na(blob); @@ -299,6 +298,8 @@ pcp_ks_bundle_t *pcp_import_pub_rfc(PCPCTX *ptx, Buffer *blob) { if(_check_sigsubs(ptx, blob, p, subheader) != 0) goto bes; } + ucfree(sigheader, sizeof(rfc_pub_sig_h)); + ucfree(subheader, sizeof(rfc_pub_sig_s)); /* calc id */ char *id = pcp_getpubkeyid(p); @@ -309,9 +310,10 @@ pcp_ks_bundle_t *pcp_import_pub_rfc(PCPCTX *ptx, Buffer *blob) { p->type = PCP_KEY_TYPE_PUBLIC; p->version = PCP_KEY_VERSION; - pcp_ks_bundle_t *b = ucmalloc(sizeof(pcp_ks_bundle_t)); - /* retrieve signature, store and verify it */ + b = ucmalloc(sizeof(pcp_ks_bundle_t)); + sk = ucmalloc(sizeof(pcp_keysig_t)); + if(_check_hash_keysig(ptx, blob, p, sk) != 0) { b->p = p; b->s = NULL; diff --git a/libpcp/vault.c b/libpcp/vault.c index b076080..7abb63f 100644 --- a/libpcp/vault.c +++ b/libpcp/vault.c @@ -168,9 +168,14 @@ int pcpvault_addkey(PCPCTX *ptx, vault_t *vault, void *item, uint8_t type) { blob = buffer_new(PCP_RAW_KEYSIZE, "bs"); pcp_pubkeyblob(blob, (pcp_pubkey_t *)item); } - else if(type == PCP_KEYSIG_NATIVE || type == PCP_KEYSIG_NATIVE) { + else if(type == PCP_KEYSIG_NATIVE || type == PCP_KEYSIG_PBP) { saveitem = ucmalloc(sizeof(pcp_keysig_t)); - memcpy(saveitem, item, sizeof(pcp_keysig_t)); + pcp_keysig_t *ksin = (pcp_keysig_t *)item; + pcp_keysig_t *ksout = (pcp_keysig_t *)saveitem; + + memcpy(ksout, ksin, sizeof(pcp_keysig_t)); + ksout->blob = ucmalloc(ksin->size); + memcpy(ksout->blob, ksin->blob, ksin->size); blob = pcp_keysig2blob(item); itemsize = buffer_size(blob); } diff --git a/src/encryption.c b/src/encryption.c index c6272c1..e5df02b 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -196,7 +196,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec /* found one by id, copy into local hash */ pub = ucmalloc(sizeof(pcp_pubkey_t)); memcpy(pub, tmp, sizeof(pcp_pubkey_t)); - HASH_ADD_STR( pubhash, id, tmp); + HASH_ADD_STR( pubhash, id, pub); } } else if(recipient != NULL) { @@ -211,7 +211,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec if(strnstr(tmp->mail, rec->value, 255) != NULL || strnstr(tmp->owner, rec->value, 255) != NULL) { pub = ucmalloc(sizeof(pcp_pubkey_t)); memcpy(pub, tmp, sizeof(pcp_pubkey_t)); - HASH_ADD_STR( pubhash, id, tmp); + HASH_ADD_STR( pubhash, id, pub); /* fprintf(stderr, " => found a matching key %s\n", tmp->id); */ } rec = rec->next; @@ -247,6 +247,7 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec strncpy(passphrase, passwd, strlen(passwd)+1); } secret = pcpkey_decrypt(ptx, secret, passphrase); + ucfree(passphrase, strlen(passwd)+1); if(secret == NULL) goto erren2; } @@ -297,27 +298,25 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec ps_close(pin); if(clen > 0) { - if(id == NULL && recipient == NULL) + if(id == NULL && recipient == NULL) { fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes symetrically\n", (SIZE_T_CAST)clen); - else if(id != NULL) - fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for 0x%s successfully\n", (SIZE_T_CAST)clen, id); + } else { fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for:\n", (SIZE_T_CAST)clen); - pcp_pubkey_t *cur; - pcphash_iteratepub(ptx, cur) { - fprintf(stderr, "%s <%s>\n", cur->owner, cur->mail); + pcp_pubkey_t *cur, *t; + HASH_ITER(hh, pubhash, cur, t) { + fprintf(stderr, " 0x%s - %s <%s>\n", cur->id, cur->owner, cur->mail); } - free(cur); } if(signcrypt) fprintf(stderr, "Signed encrypted file successfully\n"); + + pcphash_cleanpub(pubhash); return 0; } erren2: - free(pubhash); /* FIXME: it's a uthash, dont use free() but func instead */ - free(tmp); - free(pub); + pcphash_cleanpub(pubhash); erren3: diff --git a/src/keymgmt.c b/src/keymgmt.c index 35f359c..925816d 100644 --- a/src/keymgmt.c +++ b/src/keymgmt.c @@ -589,7 +589,6 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) { if(pcpvault_addkey(ptx, vault, (void *)pub, PCP_KEY_TYPE_PUBLIC) == 0) { fprintf(stderr, "key 0x%s added to %s.\n", pub->id, vault->filename); /* avoid double free */ - pub = NULL; success = 0; } else @@ -600,7 +599,6 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) { /* FIXME: remove pubkey if storing the keysig failed */ goto errimp2; } - keysig = NULL; } } else diff --git a/src/pcp.c b/src/pcp.c index d04430f..6f05205 100644 --- a/src/pcp.c +++ b/src/pcp.c @@ -594,6 +594,7 @@ int main (int argc, char **argv) { p_clean(recipient); if(id != NULL) free(id); - + if(keyid != NULL) + free(keyid); return e; }