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 1rXmx1-00CfML-5X for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Feb 2024 18:47:16 +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 1rXmx0-00CfCK-AV for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Feb 2024 18:47:14 +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 1rXmwz-00CfCB-UA for pgsql-hackers@lists.postgresql.org; Wed, 07 Feb 2024 18:47:14 +0000 Received: from lahtoruutu.iki.fi ([2a0b:5c81:1c1::37]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rXmww-005aGi-LX for pgsql-hackers@lists.postgresql.org; Wed, 07 Feb 2024 18:47:12 +0000 Received: from [192.168.1.115] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4TVTcM4c8Lz49Psw; Wed, 7 Feb 2024 20:46:59 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1707331626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LwjBf7pXxIV5rzD+PMyNL0KkBF24KyIGJ2esdloY4i4=; b=UawZ1+iM0yvxKiDd6HhWKbDnhtFMcoGWD8stOSDRkPsMgGQ5nI64DNiHm84ZHNpvIilVDA UeVSDMZTrJZxg5tYUkHRPnaY7PXSbn7H4uZVcnItRbqhMVJJBG6ENhiKn43ftykrk+8UlC 317wvadLDm11EcFUqIYF5v55AfpvTxAsEpdAJ3Ks8JZo2a05UvN8NjcgXw9ZrUTkINEBk4 4fWCG0OOAmdX1CjjZzxV7CujT5ar7HlnXt001VexAhLSUd5lek34WzwRA3B0oeaz+e7koQ WkqTcl2DTqJb+TJWrOgSYv+F3E8UBFKvmR5vHhsLv6SbPa9fBmp/DgKOMVGHxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1707331626; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LwjBf7pXxIV5rzD+PMyNL0KkBF24KyIGJ2esdloY4i4=; b=Rmx6GDfZVKsd9IKi0PG+UZHDhc/r1nmOSCnIsiABm62X6KyoVwaL7AmjZpQa92mZfky9zb grBKE4poL/S4/jMXWxsCTIAs99b7s6QRrmVueVco4k6iaxqGEAYes7FKRSnFRj8Q0xAAMZ +R0R1SHSkkr4wd0UiWRtK5nHT5kDc0zxeCPhI8FqGfAIXuzSgE6vK+8WIhnT8Jj9BkEb5o kBKUyERlOB9jsGXMl/+TGFv+7snkYSp2Y+FhUjx+IvLAEWtlEZaMIMSMbgsACeYWyR83tr 59UoarxZDjjEjAZXLF5R4HeV1uKq9B0xS+Xz4ieZIihRjLAfhVgm9ux6V+/LLw== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1707331626; a=rsa-sha256; cv=none; b=Qbdp0mUhubXgenuafe0kBXhV6Outw9liprZQbVj5jj0Skf9lP49Oz6rAG/VQkW9eKyvrlW qzmtULiobV/wXGIFzsy7As7XKuOEd/BENECQHvn8xS35xjrxghzUFZcTvKGUDKUoKeJwgl N2emri6hht6h7srrWndgFBF+Ikm103olrSW4c2LB5vfyo9mYHBcS0LODDxgQdOpqwy8bTf WilvFcAU6wb90OWTGU9duyW0t0nEO8EjUl6LiujH2ujY5vpJkxdwF7f3KNVC5SciyTINnd /srHJatKsugVvEZRg1dR4wdlYLSe4n+IdKEPLTRT457ACys5huw6prmTLzp5uQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: Date: Wed, 7 Feb 2024 20:46:56 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: glibc qsort() vulnerability To: Mats Kindahl , Tom Lane Cc: Nathan Bossart , pgsql-hackers@lists.postgresql.org References: <508821.1707232276@sss.pgh.pa.us> <20240206205305.GB3891538@nathanxps13> <642084.1707252958@sss.pgh.pa.us> Content-Language: en-US From: Heikki Linnakangas 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 07/02/2024 11:09, Mats Kindahl wrote: > On Tue, Feb 6, 2024 at 9:56 PM Tom Lane > wrote: > > Nathan Bossart > writes: > > Even if the glibc issue doesn't apply to Postgres, I'm tempted to > suggest > > that we make it project policy that comparison functions must be > > transitive.  There might be no real issues today, but if we write all > > comparison functions the way Mats is suggesting, it should be > easier to > > reason about overflow risks. > > A comparison routine that is not is probably broken, agreed. > I didn't look through the details of the patch --- I was more > curious whether we had a version of the qsort bug, because > if we do, we should fix that too. > > The patch basically removes the risk of overflow in three routines and > just returns -1, 0, or 1, and adds a comment in one. > > The routines modified do a subtraction of int:s and return that, which > can cause an overflow. This method is used for some int16 as well but > since standard conversions in C will perform the arithmetics in "int" > precision, this cannot overflow, so added a comment there. It might > still be a good idea to follow the same pattern for the int16 routines, > but since there is no bug there, I did not add them to the patch. Doesn't hurt to fix the comparison functions, and +1 on using the same pattern everywhere. However, we use our qsort() with user-defined comparison functions, and we cannot make any guarantees about what they might do. So we must ensure that our qsort() doesn't overflow, no matter what the comparison function does. Looking at our ST_SORT(), it seems safe to me. -- Heikki Linnakangas Neon (https://neon.tech)