fixed memory leaks

This commit is contained in:
TLINDEN
2014-08-07 21:33:52 +02:00
parent 230a9a2341
commit 019df8e4c5
6 changed files with 30 additions and 25 deletions

View File

@@ -38,7 +38,7 @@ void *ucmalloc(size_t s) {
memset (value, 0, size); 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; return value;
} }

View File

@@ -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) { 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)) { if(subheader->size > buffer_left(blob)) {
fatal(ptx, "Invalid header size %ld specified in source\n", subheader->size); fatal(ptx, "Invalid header size %ld specified in source\n", subheader->size);
return 1; 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 is already known from the key ctime. This may change in
the future though. 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); fatal(ptx, "Invalid 'unsupported' notation, expected %ld bytes, but got 0\n", subheader->size);
return 1; 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_ks_bundle_t *pcp_import_pub_rfc(PCPCTX *ptx, Buffer *blob) {
pcp_pubkey_t *p = ucmalloc(sizeof(pcp_pubkey_t)); pcp_keysig_t *sk = NULL;
pcp_keysig_t *sk = ucmalloc(sizeof(pcp_keysig_t)); pcp_ks_bundle_t *b = NULL;
rfc_pub_sig_h *sigheader = ucmalloc(sizeof(rfc_pub_sig_h)); rfc_pub_sig_h *sigheader = ucmalloc(sizeof(rfc_pub_sig_h));
rfc_pub_sig_s *subheader = ucmalloc(sizeof(rfc_pub_sig_s)); 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; if(buffer_done(blob)) goto be;
p->ctime = buffer_get64na(blob); 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) if(_check_sigsubs(ptx, blob, p, subheader) != 0)
goto bes; goto bes;
} }
ucfree(sigheader, sizeof(rfc_pub_sig_h));
ucfree(subheader, sizeof(rfc_pub_sig_s));
/* calc id */ /* calc id */
char *id = pcp_getpubkeyid(p); 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->type = PCP_KEY_TYPE_PUBLIC;
p->version = PCP_KEY_VERSION; p->version = PCP_KEY_VERSION;
pcp_ks_bundle_t *b = ucmalloc(sizeof(pcp_ks_bundle_t));
/* retrieve signature, store and verify it */ /* 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) { if(_check_hash_keysig(ptx, blob, p, sk) != 0) {
b->p = p; b->p = p;
b->s = NULL; b->s = NULL;

View File

@@ -168,9 +168,14 @@ int pcpvault_addkey(PCPCTX *ptx, vault_t *vault, void *item, uint8_t type) {
blob = buffer_new(PCP_RAW_KEYSIZE, "bs"); blob = buffer_new(PCP_RAW_KEYSIZE, "bs");
pcp_pubkeyblob(blob, (pcp_pubkey_t *)item); 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)); 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); blob = pcp_keysig2blob(item);
itemsize = buffer_size(blob); itemsize = buffer_size(blob);
} }

View File

@@ -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 */ /* found one by id, copy into local hash */
pub = ucmalloc(sizeof(pcp_pubkey_t)); pub = ucmalloc(sizeof(pcp_pubkey_t));
memcpy(pub, tmp, 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) { 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) { if(strnstr(tmp->mail, rec->value, 255) != NULL || strnstr(tmp->owner, rec->value, 255) != NULL) {
pub = ucmalloc(sizeof(pcp_pubkey_t)); pub = ucmalloc(sizeof(pcp_pubkey_t));
memcpy(pub, tmp, 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); */ /* fprintf(stderr, " => found a matching key %s\n", tmp->id); */
} }
rec = rec->next; 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); strncpy(passphrase, passwd, strlen(passwd)+1);
} }
secret = pcpkey_decrypt(ptx, secret, passphrase); secret = pcpkey_decrypt(ptx, secret, passphrase);
ucfree(passphrase, strlen(passwd)+1);
if(secret == NULL) if(secret == NULL)
goto erren2; goto erren2;
} }
@@ -297,27 +298,25 @@ int pcpencrypt(char *id, char *infile, char *outfile, char *passwd, plist_t *rec
ps_close(pin); ps_close(pin);
if(clen > 0) { 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); 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 { else {
fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for:\n", (SIZE_T_CAST)clen); fprintf(stderr, "Encrypted %"FMT_SIZE_T" bytes for:\n", (SIZE_T_CAST)clen);
pcp_pubkey_t *cur; pcp_pubkey_t *cur, *t;
pcphash_iteratepub(ptx, cur) { HASH_ITER(hh, pubhash, cur, t) {
fprintf(stderr, "%s <%s>\n", cur->owner, cur->mail); fprintf(stderr, " 0x%s - %s <%s>\n", cur->id, cur->owner, cur->mail);
} }
free(cur);
} }
if(signcrypt) if(signcrypt)
fprintf(stderr, "Signed encrypted file successfully\n"); fprintf(stderr, "Signed encrypted file successfully\n");
pcphash_cleanpub(pubhash);
return 0; return 0;
} }
erren2: erren2:
free(pubhash); /* FIXME: it's a uthash, dont use free() but func instead */ pcphash_cleanpub(pubhash);
free(tmp);
free(pub);
erren3: erren3:

View File

@@ -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) { 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); fprintf(stderr, "key 0x%s added to %s.\n", pub->id, vault->filename);
/* avoid double free */ /* avoid double free */
pub = NULL;
success = 0; success = 0;
} }
else else
@@ -600,7 +599,6 @@ int pcp_import (vault_t *vault, FILE *in, char *passwd) {
/* FIXME: remove pubkey if storing the keysig failed */ /* FIXME: remove pubkey if storing the keysig failed */
goto errimp2; goto errimp2;
} }
keysig = NULL;
} }
} }
else else

View File

@@ -594,6 +594,7 @@ int main (int argc, char **argv) {
p_clean(recipient); p_clean(recipient);
if(id != NULL) if(id != NULL)
free(id); free(id);
if(keyid != NULL)
free(keyid);
return e; return e;
} }