Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tAPXG-00FZzO-Uc for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Nov 2024 08:12:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tAPXE-00CQFk-5V for pgsql-hackers@arkaria.postgresql.org; Mon, 11 Nov 2024 08:12:32 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tAPXD-00CQFb-SP for pgsql-hackers@lists.postgresql.org; Mon, 11 Nov 2024 08:12:32 +0000 Received: from fhigh-a2-smtp.messagingengine.com ([103.168.172.153]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tAPXB-001GRH-CZ for pgsql-hackers@postgresql.org; Mon, 11 Nov 2024 08:12:31 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfhigh.phl.internal (Postfix) with ESMTP id 9941311400FB; Mon, 11 Nov 2024 03:12:28 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Mon, 11 Nov 2024 03:12:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=eisentraut.org; h=cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1731312748; x=1731399148; bh=V5zQAtrkpqOVJ9s2EB8DuFnC4YHg7nub0VroaC9O83o=; b= dnnJ7M8/lYWelulgNk6fQ/3I9WjhGtCA6LSjQP7ZRye9CXluVoqqhoh3+Atjq+n0 CraveBEibVEZg02NKqW/5/92ES+2FbYfCUKJP5ZwvbYWiI9Ys556TeqQv4XrrMZX LdoU9lGePKarkK0roBvodufWYVEjdcBuMgDGACEk3AOhbt98TYDJvbdr8Jy8LTCe 1w+4MNCh6vJ2U/TuT36621E5VGVweClGVkzEQBLeMvOXHLisH2bZABhbEVX7nVJy uVOjwbyc3YdsePX55arpTd6xv9XSiqM1Aprh+9qyQoBazNmtsmEbUSiLEVkUMx5U zw1Ui81NdAYeBsqDzF40hQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1731312748; x=1731399148; bh=V 5zQAtrkpqOVJ9s2EB8DuFnC4YHg7nub0VroaC9O83o=; b=OG5G4MhvZcYBENnW0 ZJCn87xE3EVIwpCbaGpt5+BlO18UiclL70k1DKsNXGSnP2JmoCO9OugX+NKSx1DH glizkKzqUSMPQMN403/1oUpVZSTNjlxDnm5xdz7QiVqldDfjNZJumNnxcMSrKTMy klygLRQOs6PHYGsBrhDJoBGr4CnuGBHIjs3JOlsK2UOoHFT7mgcsD4mDuO0CcyUD SlRPe/eYc1VUCI40Yis2o21HyX7ipx8vhFRkGmJy0mKM/in41URBTtbYu+YKzN6u tN5te8BvfspKC1OJelDkqz5USD+DmQHAAoqaloU+8BA7rHlxICVr3Lt2QjIQl1UD gNKoQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddruddugdduudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttddvjeen ucfhrhhomheprfgvthgvrhcugfhishgvnhhtrhgruhhtuceophgvthgvrhesvghishgvnh htrhgruhhtrdhorhhgqeenucggtffrrghtthgvrhhnpedugfejffefleefvdegvdejvdei heevgffhjefgudfhvedvjeevfeehteelgfevfeenucevlhhushhtvghrufhiiigvpedtne curfgrrhgrmhepmhgrihhlfhhrohhmpehpvghtvghrsegvihhsvghnthhrrghuthdrohhr ghdpnhgspghrtghpthhtohepvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepsg hgihhlvghssegtohihohhtvghsohhnghdrtghomhdprhgtphhtthhopehpghhsqhhlqdhh rggtkhgvrhhssehpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: ie0a040ee:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 11 Nov 2024 03:12:27 -0500 (EST) Message-ID: Date: Mon, 11 Nov 2024 09:12:26 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: pgcrypto: better memory management?... To: Bear Giles , PostgreSQL-development References: Content-Language: en-US From: Peter Eisentraut In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 08.11.24 03:15, Bear Giles wrote: > The first is that openssl has supported custom memory management for a > very long time - I remember using it in the late 90s. The function is in > crypto.h > >  typedef void *(*CRYPTO_malloc_fn)(size_t num, const char *file, int line); >  typedef void *(*CRYPTO_realloc_fn)(void *addr, size_t num, const char > *file, >                                     int line); >  typedef void (*CRYPTO_free_fn)(void *addr, const char *file, int line); > int CRYPTO_set_mem_functions(CRYPTO_malloc_fn malloc_fn, >                               CRYPTO_realloc_fn realloc_fn, >                               CRYPTO_free_fn free_fn); > > The main benefit of this function is that it openssl-internal functions > will use the same memory pool as your application. I know the current > extension uses palloc/pfree but I didn't see a call to this function. The problem with this kind of interface is that in PostgreSQL, the idea is behind using palloc() is that you usually don't need to take care to free the memory, because it is automatically freed at some appropriate time (end of row, end of query, end of transaction, etc.). But an external library like openssl doesn't know that. So it could happen that PostgreSQL automatically frees some data structure that openssl thinks should still exist. This kind of problem definitely existed with libxml, which has a very similar interface. So you'd have to do some very careful analysis whether this would be safe for openssl. I suspect it's not worth the bother. > The second item is that openssl also has these wonderful functions: > > int CRYPTO_secure_malloc_init(size_t sz, size_t minsize); > int CRYPTO_secure_malloc_done(void); > void *CRYPTO_secure_malloc(size_t num, const char *file, int line); > void *CRYPTO_secure_zalloc(size_t num, const char *file, int line); > void CRYPTO_secure_free(void *ptr, const char *file, int line); > void CRYPTO_secure_clear_free(void *ptr, size_t num, >                               const char *file, int line); > int CRYPTO_secure_allocated(const void *ptr); > int CRYPTO_secure_malloc_initialized(void); > size_t CRYPTO_secure_actual_size(void *ptr); > size_t CRYPTO_secure_used(void); > > void OPENSSL_cleanse(void *ptr, size_t len); > > These functions address (at least) two potential vulnerabilities. First > it tweaks the memory block so it will never be written to the swap file, > and second it (iirc) also tweaks the memory block to reduce the access > permissions further than usual. PostgreSQL does do this kind of thing already, see explicit_bzero(). Maybe we could do more. This would need a more specific analysis and proposal.