fixed resource leaks

This commit is contained in:
Frank Morgner 2015-02-05 01:36:40 +01:00
parent 11881a61b8
commit 3a92bf7af5
14 changed files with 45 additions and 20 deletions

View File

@ -690,16 +690,14 @@ cwa_provider_t *dnie_get_cwa_provider(sc_card_t * card)
static int dnie_transmit_apdu_internal(sc_card_t * card, sc_apdu_t * apdu)
{
u8 *buf = NULL; /* use for store partial le responses */
u8 buf[2048]; /* use for store partial le responses */
int res = SC_SUCCESS;
cwa_provider_t *provider = NULL;
if ((card == NULL) || (card->ctx == NULL) || (apdu == NULL))
return SC_ERROR_INVALID_ARGUMENTS;
LOG_FUNC_CALLED(card->ctx);
provider = GET_DNIE_PRIV_DATA(card)->cwa_provider;
buf = calloc(2048, sizeof(u8));
if (!buf)
LOG_FUNC_RETURN(card->ctx, SC_ERROR_OUT_OF_MEMORY);
memset(buf, 0, sizeof(buf));
/* check if envelope is needed */
if (apdu->lc <= card->max_send_size) {

View File

@ -324,6 +324,7 @@ static int cwa_parse_tlv(sc_card_t * card,
/* set index to next Tag to jump to */
next = tlv->buflen;
}
free(buffer);
LOG_FUNC_RETURN(ctx, SC_SUCCESS); /* mark no error */
}
@ -1418,9 +1419,9 @@ int cwa_create_secure_channel(sc_card_t * card,
int cwa_encode_apdu(sc_card_t * card,
cwa_provider_t * provider, sc_apdu_t * from, sc_apdu_t * to)
{
u8 *apdubuf; /* to store resulting apdu */
u8 *apdubuf = NULL; /* to store resulting apdu */
size_t apdulen;
u8 *ccbuf; /* where to store data to eval cryptographic checksum CC */
u8 *ccbuf = NULL; /* where to store data to eval cryptographic checksum CC */
size_t cclen = 0;
u8 macbuf[8]; /* to store and compute CC */
DES_key_schedule k1;
@ -1594,9 +1595,13 @@ int cwa_encode_apdu(sc_card_t * card,
/* that's all folks */
res = SC_SUCCESS;
encode_end:
encode_end:
if (msg)
sc_log(ctx, msg);
free(msgbuf);
free(cryptbuf);
free(ccbuf);
free(apdubuf);
LOG_FUNC_RETURN(ctx, res);
}

View File

@ -102,8 +102,9 @@ int parse_odf(const u8 * buf, size_t buflen, struct sc_pkcs15_card *p15card)
};
struct sc_asn1_entry asn1_odf[10];
sc_path_t *path_prefix = calloc(1, sizeof(sc_path_t));
sc_format_path("3F005015", path_prefix);
sc_path_t path_prefix;
sc_format_path("3F005015", &path_prefix);
sc_copy_asn1_entry(c_asn1_odf, asn1_odf);
for (i = 0; asn1_odf[i].name != NULL; i++)
@ -116,7 +117,7 @@ int parse_odf(const u8 * buf, size_t buflen, struct sc_pkcs15_card *p15card)
if (r < 0)
return r;
type = r;
r = sc_pkcs15_make_absolute_path(path_prefix, &path);
r = sc_pkcs15_make_absolute_path(&path_prefix, &path);
if (r < 0)
return r;
r = sc_pkcs15_add_df(p15card, odf_indexes[type], &path);

View File

@ -1227,7 +1227,7 @@ sc_pkcs15_read_der_file(sc_context_t *ctx, char * filename,
rbuf = NULL;
r = rbuflen;
out:
if (f > 0)
if (f >= 0)
close(f);
LOG_FUNC_RETURN(ctx, r);

View File

@ -957,6 +957,7 @@ sc_pkcs15_get_application_by_type(struct sc_card * card, char *app_type)
char *type = (char *)scconf_get_str(blocks[0], "type", app_type);
if (!strcmp(type, app_type)) {
out = app_info;
free(blocks);
break;
}
}

View File

@ -47,7 +47,7 @@ const char *sc_get_version(void)
int sc_hex_to_bin(const char *in, u8 *out, size_t *outlen)
{
int err = 0;
int err = SC_SUCCESS;
size_t left, count = 0;
assert(in != NULL && out != NULL && outlen != NULL);

View File

@ -183,7 +183,7 @@ CK_RV card_removed(sc_reader_t * reader)
CK_RV card_detect(sc_reader_t *reader)
{
struct sc_pkcs11_card *p11card = NULL;
int rc;
int rc, free_p11card = 0;
CK_RV rv;
unsigned int i;
int j;
@ -228,6 +228,7 @@ again:
/* Detect the card if it's not known already */
if (p11card == NULL) {
sc_log(context, "%s: First seen the card ", reader->name);
free_p11card = 1;
p11card = (struct sc_pkcs11_card *)calloc(1, sizeof(struct sc_pkcs11_card));
if (!p11card)
return CKR_HOST_MEMORY;
@ -316,6 +317,9 @@ again:
}
}
if (free_p11card)
free(p11card);
sc_log(context, "%s: Detection ended", reader->name);
return CKR_OK;
}

View File

@ -258,6 +258,8 @@ authentic_pkcs15_new_file(struct sc_profile *profile, struct sc_card *card,
file->path.type, sc_print_path(&file->path));
if (out)
*out = file;
else
sc_file_free(file);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}

View File

@ -246,6 +246,8 @@ iasecc_pkcs15_new_file(struct sc_profile *profile, struct sc_card *card,
if (out)
*out = file;
else
sc_file_free(file);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
@ -503,6 +505,8 @@ iasecc_sdo_allocate_prvkey(struct sc_profile *profile, struct sc_card *card,
if (out)
*out = sdo;
else
free(sdo);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
@ -565,6 +569,8 @@ iasecc_sdo_allocate_pubkey(struct sc_profile *profile, struct sc_card *card, str
if (out)
*out = sdo;
else
free(sdo);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}
@ -609,6 +615,9 @@ iasecc_sdo_convert_to_file(struct sc_card *card, struct iasecc_sdo *sdo, struct
if (out)
*out = file;
else
sc_file_free(file);
LOG_FUNC_RETURN(ctx, SC_SUCCESS);
}

View File

@ -352,6 +352,3 @@ struct sc_pkcs15init_operations* sc_pkcs15init_get_westcos_ops(void)
{
return &sc_pkcs15init_westcos_operations;
}

View File

@ -381,6 +381,7 @@ static int do_dump_do(sc_card_t *card, unsigned int tag)
if (sizeof(buffer) != r) {
return EXIT_FAILURE;
}
close(tmp);
} else {
util_hex_dump_asc(stdout, buffer, sizeof(buffer), -1);
}

View File

@ -495,6 +495,7 @@ int main(int argc, char * const argv[])
break;
case 'Z':
compress_cert = 1;
break;
case 'C':
do_load_cert = 1;
cert_id = optarg;

View File

@ -1327,12 +1327,17 @@ static int unlock_pin(CK_SLOT_ID slot, CK_SESSION_HANDLE sess, int login_type)
if (r < 0)
return 1;
if (!new_pin || !*new_pin || strcmp(new_buf, new_pin) != 0) {
if (new_pin != opt_new_pin)
free(new_pin);
printf(" different new PINs, exiting\n");
return -1;
}
if (!new_pin || !*new_pin || strlen(new_pin) > 20)
if (!new_pin || !*new_pin || strlen(new_pin) > 20) {
if (new_pin != opt_new_pin)
free(new_pin);
return 1;
}
}
rv = p11->C_SetPIN(sess,

View File

@ -684,10 +684,10 @@ do_erase(sc_card_t *in_card, struct sc_profile *profile)
struct sc_aid aid;
aid.len = sizeof(aid.value);
if (sc_hex_to_bin(opt_bind_to_aid, aid.value, &aid.len)) {
r = sc_hex_to_bin(opt_bind_to_aid, aid.value, &aid.len);
if (r < 0) {
fprintf(stderr, "Invalid AID value: '%s'\n", opt_bind_to_aid);
return 1;
goto err;
}
r = sc_pkcs15init_erase_card(p15card, profile, &aid);
@ -697,6 +697,7 @@ do_erase(sc_card_t *in_card, struct sc_profile *profile)
}
ignore_cmdline_pins--;
err:
sc_pkcs15_card_free(p15card);
return r;
}