Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1beclE-0005hg-3P for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Aug 2016 06:43:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1beclD-0003nD-3q for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Aug 2016 06:43:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1becl6-0003iG-DS for pgsql-hackers@postgresql.org; Tue, 30 Aug 2016 06:42:56 +0000 Received: from mail-wm0-x235.google.com ([2a00:1450:400c:c09::235]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1beckx-0003XA-7O for pgsql-hackers@postgresql.org; Tue, 30 Aug 2016 06:42:55 +0000 Received: by mail-wm0-x235.google.com with SMTP id f65so106128043wmi.0 for ; Mon, 29 Aug 2016 23:42:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=fZrfsIzZEj9P/PSyjNiHNOWKpVNcDv3X4UjnyTQCWtk=; b=wJAIwi37oxvztjGDNntlitQcRR4IyHN661L/faiVJo3UcwgnML0e1z0I2Iyd1F0NY1 +vZcJKkEQ0yYwMcZxyKPfF2yYo1qc5VRQMalFFsLi3sVtLchKbhijEwakTVE/ys0NNqA wigjcFam+ce8AYDVYUsbQ+8SHlO4X1RKXdYO5kCX+fowSxz3GeAZ0EfGGF1WuBbGHKVR LC8yvTfWMglZLSTZbdT43tiAkNwNgixf7jFSlm8luqXmQ8KJyNfFdUY9XitIE+xvo+/r ORSdT9CjJyUfcCyNsJQhgO/JZsFUyv+7XWSnqxpi+xUI0kId+ncfl1vuMeEy5BEl0CRL 6UiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=fZrfsIzZEj9P/PSyjNiHNOWKpVNcDv3X4UjnyTQCWtk=; b=XVAGMJCXhfwSXydWA/1gqicVVVT9iyHr4wjzgBagGAqjoylx01pSSvYhrl+6px8PhI 8RGkiymQEGkRQ83uWVeOEqlkUrRQ9inqeABZ8thq950xgBTHsioopkRuQiKum4LROtCd ADtDJhfvmlIGjFT58OfHyuO1Ld6v4cWYbbd3N+MnVK3Dm38CmmXnPaxNgsJ8sYY9bE2A AjjYs3BPINRzgdRZcBFIV1aZ4+lomOTo7O+QxXwGT57XoesbO/CnUn2fEZ6mJy/HtwRW Yxxm7+9xNlfNdDyq1ApxPRQIX5/mExb7MV6V5Qoi9e02AGzL3ogWuNWxTS6O+Mjx0/5V zn2w== X-Gm-Message-State: AE9vXwOGKSO4PtlYl+Mzd1qki5QysVu8WE8hMcS/UFKfxcPMWG5hAmV7Eshi9p/Ut7zYoQ== X-Received: by 10.28.209.134 with SMTP id i128mr13370422wmg.97.1472539364174; Mon, 29 Aug 2016 23:42:44 -0700 (PDT) Received: from [192.168.1.111] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by smtp.gmail.com with ESMTPSA id a194sm16984351wmd.24.2016.08.29.23.42.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 29 Aug 2016 23:42:43 -0700 (PDT) Subject: Re: OpenSSL 1.1 breaks configure and more To: Andreas Karlsson , Victor Wagner , pgsql-hackers@postgresql.org, Christoph Berg , Tom Lane References: <20160627151604.GD1051@msg.df7cb.de> <1805.1467041061@sss.pgh.pa.us> <8a0a5959-0b83-3dc8-d9e7-66ce8c1c5bc7@proxel.se> <688a438c-ccc2-0431-7100-26e418fc3bca@proxel.se> <20160705121347.5c5c702f@fafnir.local.vm> <34821a62-ac89-ffb2-7516-7a8b15c04723@proxel.se> <7ff5558f-cff5-3768-2fb3-6b50b58294e5@iki.fi> <0d419332-ed78-71f8-7dd9-1aefdc6f5dca@proxel.se> From: Heikki Linnakangas Message-ID: Date: Tue, 30 Aug 2016 09:42:39 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.2.0 MIME-Version: 1.0 In-Reply-To: <0d419332-ed78-71f8-7dd9-1aefdc6f5dca@proxel.se> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 08/30/2016 03:26 AM, Andreas Karlsson wrote: > On 08/26/2016 11:31 AM, Heikki Linnakangas wrote: >> On 07/05/2016 04:46 PM, Andreas Karlsson wrote: >>> @@ -280,8 +287,9 @@ px_find_digest(const char *name, PX_MD **res) >>> digest = px_alloc(sizeof(*digest)); >>> digest->algo = md; >>> >>> - EVP_MD_CTX_init(&digest->ctx); >>> - if (EVP_DigestInit_ex(&digest->ctx, digest->algo, NULL) == 0) >>> + digest->ctx = EVP_MD_CTX_create(); >>> + EVP_MD_CTX_init(digest->ctx); >>> + if (EVP_DigestInit_ex(digest->ctx, digest->algo, NULL) == 0) >>> return -1; >>> >>> h = px_alloc(sizeof(*h)); >> >> Now that we're calling EVP_MD_CTX_create((), which allocates memory, are >> we risking memory leaks? It has always been part of the contract that >> you have to call px_md_free(), for any context returned by >> px_find_digest(), but I wonder just how careful we have been about that. >> Before this, you would probably get away with it without leaking, if the >> digest implementation didn't allocate any extra memory or other resources. >> >> At least pg_digest and try_unix_std functions call px_find_digest(), and >> then do more palloc()s which could elog() if you run out of memory, >> leaking th digest struct. Highly unlikely, but I think it would be >> fairly straightforward to reorder those calls to eliminate the risk, so >> we probably should. > > Since px_find_digest() calls palloc() later in the function there is a > slim possibility of memory leaks. Yep. That palloc() would be easy to move to before the EVP_MD_CTX_new() call. And some of the calls to px_find_digest() could likewise be rearranged. But there are some more complicated callers. pgp_encrypt(), for example, builds a pipeline of multiple "mbuf" filters, and one of those filters uses px_find_digest(). > How do we generally handle that things > not allocated with palloc() may leak when something calls elog()? There's the ResourceOwner mechanism, see src/backend/utils/resowner/. That would be the proper way to do this. Call RegisterResourceReleaseCallback() when the context is allocated, and have the callback free it. One pitfall to watch out for is that RegisterResourceReleaseCallback() itself calls palloc(), and can error out, so you have to do things in such an order that you don't leak in that case either. Want to take a stab at that? Another approach is put each allocated context in a list or array in a global variable, and to register a callback to be called at end-of-(sub)transaction, which closes all the contexts. But the resource owner mechanism is probably easier. There's also PG_TRY-CATCH, that you could maybe use in the callers of px_find_digest(), to make sure they call px_free_digest() even on error. But that also seems difficult to use with the pgp_encrypt() pipeline. > I have attached new versions of the patches which are rebased on master, > with slightly improves error handling in px_find_digest(), and handles > the deprecation of ASN1_STRING_data(). Thanks! PS. I just remembered that I've wanted to refactor the pgcrypto calls for symmetric encryption to use the newer EVP API for some time, and even posted a patch for that (https://www.postgresql.org/message-id/561274F1.1030000@iki.fi). I dropped the ball back then, but I think I'll go ahead and do that now, once we get these other OpenSSL changes in. - Heikki -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers