g10: Fix memory leaks

* g10/card-util.c (change_pin): free answer on errors
  (ask_card_keyattr): free answer on error
* g10/cpr.c (do_get_from_fd): free string
* g10/gpg.c (check_permissions): free dir on weird error
* g10/import.c (append_new_uid): release knode
* g10/keyedit.c (menu_set_keyserver_url): free answer
  (menu_set_keyserver_url): free user
* g10/keygen.c (print_status_key_not_created): move allocation after
  sanity check
  (ask_expire_interval): free answer
  (card_store_key_with_backup): goto leave instaed of return
* g10/keyserver.c (parse_keyserver_uri): goto fail instead of return
* g10/revoke.c (gen_desig_revoke): release kdbhd
  (gen_desig_revoke): free answer
* g10/tofu.c (ask_about_binding): free sqerr and response
* g10/trustdb.c (ask_ownertrust): free pk

--

Signed-off-by: Jakub Jelen <jjelen@redhat.com>

Further changes:

* g10/card-util.c (change_pin): Do not set answer to NULL.
* g10/keyedit.c(menu_set_keyserver_url): Use !func() pattern.

Signed-off-by: Werner Koch <wk@gnupg.org>
GnuPG-bug-id: 5393
This commit is contained in:
Jakub Jelen 2021-05-20 10:13:51 +02:00 committed by Werner Koch
parent 0d2c1e9046
commit 2af7bb2295
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
10 changed files with 50 additions and 12 deletions

View File

@ -140,7 +140,10 @@ change_pin (int unblock_v2, int allow_admin)
answer = cpr_get("cardutil.change_pin.menu",_("Your selection? "));
cpr_kill_prompt();
if (strlen (answer) != 1)
continue;
{
xfree (answer);
continue;
}
if (*answer == '1')
{
@ -185,8 +188,10 @@ change_pin (int unblock_v2, int allow_admin)
}
else if (*answer == 'q' || *answer == 'Q')
{
xfree (answer);
break;
}
xfree (answer);
}
agent_release_card_info (&info);
@ -1450,7 +1455,10 @@ ask_card_keyattr (int keyno, const struct key_attr *current)
algo = *answer? atoi (answer) : 0;
if (!*answer || algo == 1 || algo == 2)
break;
{
xfree (answer);
break;
}
else
tty_printf (_("Invalid selection.\n"));
}

View File

@ -527,7 +527,11 @@ do_get_from_fd ( const char *keyword, int hidden, int getbool )
write_status (STATUS_GOT_IT);
if (getbool) /* Fixme: is this correct??? */
return (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
{
char *rv = (string[0] == 'Y' || string[0] == 'y') ? "" : NULL;
xfree (string);
return rv;
}
return string;
}

View File

@ -1601,6 +1601,7 @@ check_permissions (const char *path, int item)
if (gnupg_stat (dir,&dirbuf) || !S_ISDIR (dirbuf.st_mode))
{
/* Weird error */
xfree(dir);
ret=1;
goto end;
}

View File

@ -4524,7 +4524,10 @@ append_new_uid (unsigned int options,
err = insert_key_origin_uid (n->pkt->pkt.user_id,
curtime, origin, url);
if (err)
return err;
{
release_kbnode (n);
return err;
}
}
if (n_where)

View File

@ -5306,8 +5306,11 @@ menu_set_keyserver_url (ctrl_t ctrl, const char *url, kbnode_t pub_keyblock)
}
}
if (ascii_strcasecmp (answer, "none") == 0)
uri = NULL;
if (!ascii_strcasecmp (answer, "none"))
{
xfree (answer);
uri = NULL;
}
else
{
struct keyserver_spec *keyserver = NULL;
@ -5379,12 +5382,14 @@ menu_set_keyserver_url (ctrl_t ctrl, const char *url, kbnode_t pub_keyblock)
uri
? _("Are you sure you want to replace it? (y/N) ")
: _("Are you sure you want to delete it? (y/N) ")))
xfree (user);
continue;
}
else if (uri == NULL)
{
/* There is no current keyserver URL, so there
is no point in trying to un-set it. */
xfree (user);
continue;
}
@ -5397,6 +5402,7 @@ menu_set_keyserver_url (ctrl_t ctrl, const char *url, kbnode_t pub_keyblock)
log_error ("update_keysig_packet failed: %s\n",
gpg_strerror (rc));
xfree (uri);
xfree (user);
return 0;
}
/* replace the packet */

View File

@ -237,12 +237,13 @@ print_status_key_not_created (const char *handle)
static gpg_error_t
write_uid (kbnode_t root, const char *s)
{
PACKET *pkt = xmalloc_clear (sizeof *pkt);
PACKET *pkt = NULL;
size_t n = strlen (s);
if (n > MAX_UID_PACKET_LENGTH - 10)
return gpg_error (GPG_ERR_INV_USER_ID);
pkt = xmalloc_clear (sizeof *pkt);
pkt->pkttype = PKT_USER_ID;
pkt->pkt.user_id = xmalloc_clear (sizeof *pkt->pkt.user_id + n);
pkt->pkt.user_id->len = n;
@ -2860,7 +2861,10 @@ ask_expire_interval(int object,const char *def_expire)
xfree(prompt);
if(*answer=='\0')
answer=xstrdup(def_expire);
{
xfree (answer);
answer = xstrdup (def_expire);
}
}
cpr_kill_prompt();
trim_spaces(answer);
@ -5238,12 +5242,15 @@ card_store_key_with_backup (ctrl_t ctrl, PKT_public_key *sub_psk,
epoch2isotime (timestamp, (time_t)sk->timestamp);
err = hexkeygrip_from_pk (sk, &hexgrip);
if (err)
return err;
goto leave;
memset(&info, 0, sizeof (info));
rc = agent_scd_getattr ("SERIALNO", &info);
if (rc)
return (gpg_error_t)rc;
{
err = (gpg_error_t)rc;
goto leave;
}
rc = agent_keytocard (hexgrip, 2, 1, info.serialno, timestamp);
xfree (info.serialno);

View File

@ -284,7 +284,7 @@ parse_keyserver_uri (const char *string,int require_scheme)
if(*idx=='\0' || *idx=='[')
{
if(require_scheme)
return NULL;
goto fail;
/* Assume HKP if there is no scheme */
assume_hkp=1;

View File

@ -435,6 +435,7 @@ gen_desig_revoke (ctrl_t ctrl, const char *uname, strlist_t locusr)
iobuf_close(out);
release_revocation_reason_info( reason );
release_armor_context (afx);
keydb_release (kdbhd);
return rc;
}
@ -804,7 +805,10 @@ ask_revocation_reason( int key_rev, int cert_rev, int hint )
trim_spaces( answer );
cpr_kill_prompt();
if( *answer == 'q' || *answer == 'Q')
return NULL; /* cancel */
{
xfree (answer);
return NULL; /* cancel */
}
if( hint && !*answer )
n = hint;
else if(!digitp( answer ) )

View File

@ -1687,6 +1687,8 @@ ask_about_binding (ctrl_t ctrl,
GPGSQL_ARG_END);
if (rc)
{
sqlite3_free (sqerr);
sqerr = NULL;
rc = gpg_error (GPG_ERR_GENERAL);
break;
}
@ -1972,6 +1974,7 @@ ask_about_binding (ctrl_t ctrl,
else if (!response[0])
/* Default to unknown. Don't save it. */
{
xfree (response);
tty_printf (_("Defaulting to unknown.\n"));
*policy = TOFU_POLICY_UNKNOWN;
break;
@ -1983,6 +1986,7 @@ ask_about_binding (ctrl_t ctrl,
if (choice)
{
int c = ((size_t) choice - (size_t) choices) / 2;
xfree (response);
switch (c)
{

View File

@ -1430,6 +1430,7 @@ ask_ownertrust (ctrl_t ctrl, u32 *kid, int minimum)
{
log_error (_("public key %s not found: %s\n"),
keystr(kid), gpg_strerror (rc) );
free_public_key (pk);
return TRUST_UNKNOWN;
}