chiark / gitweb /
Import gnupg2_2.1.18-8~deb9u1.debian.tar.bz2
[gnupg2.git] / patches / 0034-gpg-Fix-memory-leak-in-the-error-case-of-signature-c.patch
1 From: Werner Koch <wk@gnupg.org>
2 Date: Fri, 10 Feb 2017 17:16:07 +0100
3 Subject: gpg: Fix memory leak in the error case of signature creation.
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset="utf-8"
6 Content-Transfer-Encoding: 8bit
7
8 * g10/sign.c (write_signature_packets): Free SIG.  Also replace
9 xcalloc by xtrycalloc.
10 --
11
12 If do_sign fails SIG was not released.  Note that in the good case SIG
13 is transferred to PKT and freed by free_packet.
14
15 Reported-by: Stephan Müller
16 Signed-off-by: Werner Koch <wk@gnupg.org>
17 (cherry picked from commit 5996c7bf99f3a681393fd9589276399ebc956cff)
18 ---
19  g10/sign.c | 11 +++++++++--
20  1 file changed, 9 insertions(+), 2 deletions(-)
21
22 diff --git a/g10/sign.c b/g10/sign.c
23 index acc894c..ff099b3 100644
24 --- a/g10/sign.c
25 +++ b/g10/sign.c
26 @@ -686,7 +686,10 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, gcry_md_hd_t hash,
27        pk = sk_rover->pk;
28  
29        /* Build the signature packet.  */
30 -      sig = xmalloc_clear (sizeof *sig);
31 +      sig = xtrycalloc (1, sizeof *sig);
32 +      if (!sig)
33 +        return gpg_error_from_syserror ();
34 +
35        if (duration || opt.sig_policy_url
36            || opt.sig_notations || opt.sig_keyserver_url)
37          sig->version = 4;
38 @@ -731,8 +734,12 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, gcry_md_hd_t hash,
39              print_status_sig_created (pk, sig, status_letter);
40            free_packet (&pkt);
41            if (rc)
42 -            log_error ("build signature packet failed: %s\n", gpg_strerror (rc));
43 +            log_error ("build signature packet failed: %s\n",
44 +                       gpg_strerror (rc));
45         }
46 +      else
47 +        xfree (sig);
48 +
49        if (rc)
50          return rc;
51      }