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 1rYTgB-00GC5m-DJ for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Feb 2024 16:24:44 +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 1rYTg9-009rgt-6M for pgsql-hackers@arkaria.postgresql.org; Fri, 09 Feb 2024 16:24:41 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rYTg8-009rgi-PU for pgsql-hackers@lists.postgresql.org; Fri, 09 Feb 2024 16:24:40 +0000 Received: from mail-io1-xd34.google.com ([2607:f8b0:4864:20::d34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rYTg5-006KUe-Se for pgsql-hackers@lists.postgresql.org; Fri, 09 Feb 2024 16:24:40 +0000 Received: by mail-io1-xd34.google.com with SMTP id ca18e2360f4ac-7bee8858a8aso44515639f.0 for ; Fri, 09 Feb 2024 08:24:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707495876; x=1708100676; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=qh/gdlrTo2wsl1mICRei5IaxDGCFP7ljFNYuH1HIeBc=; b=hUbv2cUG393ZK9k+gEje3z/OV8Qvq5csMS/YE6ezTdz5ylGj6l1nQ6Bjxg2VKsLXMb CRnu1eORSpm/A4f0ULfozztpguOtO54cgpdIKNk202D9xRSErpChDaEHk+cXq9K3Dyvc efEYI1pkEq8xEyT9yoOB51BtCoU90EZ02UzHZvvHOq+aFpt5DujnA8uyD5/BSATwx9f7 yvZCRWMP9ylaSr6Hea2+Re/7lx2RgYec0vrGs7d70vCYmXqgRC0LJHaQ9HNiyYhPt6My ep28XZYwYNA16nmt+J7CcGbupA5KnIL4NekEtrxajIjPuSn3PNZSCnWPh/jBRbFkfTsq WPcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707495876; x=1708100676; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qh/gdlrTo2wsl1mICRei5IaxDGCFP7ljFNYuH1HIeBc=; b=iyfG8HPgDzeC5yh8jTdlhFKmiTZ2dyGWNjhEnatSgtacqeKw4YUfGPs4CsMMtil1lc 0K7e/0wU9gZkp2tEzWUQofgzwDJtkO4Wo5bVF2Ee7tB/hl29UR5WyTWDozWACZtja66W +aM2DGE17/LzGUKU8fd9D15yaCtcTsDVm1hO84GkfqvhUJKasvfJs8NWhiZu6g2iiTA/ 1H5NSsRUY1OvMoCmBrU6l8VukP3MM+KSgveLupAJ3LrlZRPSf5djzA0ynuCiM0jDXe46 Br1LgQ5PNxyDKuYBALJY4+v9fp9pSIA27Rke0aR2ClAXPrqGmh9rfDfKPito4WmB0mJP oq1w== X-Gm-Message-State: AOJu0YzJHVZSVA73KMZpMiK2k8bzO344BlfdaLuxUJo0kLfpspFumnRO 6fq2AWjUpHyc8GCEeKpfsX7jSqqw50sgBnMvHP4flRBOab3JDqdG X-Google-Smtp-Source: AGHT+IGWcfbNkjQymxyrzyjQkn10wlstHCMmuQufQ1YP2Be1+vwyM5itfSbWF7ZGEXpHbF23CORbqw== X-Received: by 2002:a6b:e307:0:b0:7c4:48e5:cbaf with SMTP id u7-20020a6be307000000b007c448e5cbafmr944506ioc.10.1707495875739; Fri, 09 Feb 2024 08:24:35 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCUNvBh/iKFyJqcXU3MQevhsDk3Mbt81WtUnA9x5LExwPV3qSCaT/Soz2z9PFs6CXSddu9F0hkCDYI2M50fyoHC5VOMrQ/t0o6RQpY62DqtM/XTKVrZY3u41yBiYNTjinaTC6FuWNiMtSGgLmvE+f2zkiZljvwARuYBxpnkiCXZqOO4g1zzKPaobtURIedtEGQ== Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id ay14-20020a056638410e00b00471239ce9c4sm470046jab.168.2024.02.09.08.24.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 08:24:35 -0800 (PST) Date: Fri, 9 Feb 2024 10:24:33 -0600 From: Nathan Bossart To: Mats Kindahl Cc: Tom Lane , Andres Freund , Thomas Munro , Heikki Linnakangas , pgsql-hackers@lists.postgresql.org Subject: Re: glibc qsort() vulnerability Message-ID: <20240209162433.GA663211@nathanxps13> References: <20240208025620.GC445153@nathanxps13> <20240208183835.GA503311@nathanxps13> <1074897.1707417842@sss.pgh.pa.us> <20240208195954.vlpoii4ftoow2of4@awork3.anarazel.de> <20240208200737.GA504276@nathanxps13> <1242426.1707424769@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Feb 09, 2024 at 08:52:26AM +0100, Mats Kindahl wrote: > Here is a new version introducing pg_cmp_s32 and friends and use them > instead of the INT_CMP macro introduced before. It also moves the > definitions to common/int.h and adds that as an include to all locations > using these functions. Thanks for the new version of the patch. > Note that for integers with sizes less than sizeof(int), C standard > conversions will convert the values to "int" before doing the arithmetic, > so no casting is *necessary*. I did not force the 16-bit functions to > return -1 or 1 and have updated the comment accordingly. It might not be necessary, but this is one of those places where I would add casting anyway to make it abundantly clear what we are expecting to happen and why it is safe. > The types "int" and "size_t" are treated as s32 and u32 respectively since > that seems to be the case for most of the code, even if strictly not > correct (size_t can be an unsigned long int for some architecture). Why is it safe to do this? > - return ((const SPLITCOST *) a)->cost - ((const SPLITCOST *) b)->cost; > + return INT_CMP(((const SPLITCOST *) a)->cost, ((const SPLITCOST *) b)->cost); The patch still contains several calls to INT_CMP. > +/*------------------------------------------------------------------------ > + * Comparison routines for integers > + *------------------------------------------------------------------------ > + */ I'd suggest separating this part out to a 0001 patch to make it easier to review. The 0002 patch could take care of converting the existing qsort comparators. > +static inline int > +pg_cmp_s16(int16 a, int16 b) > +{ > + return a - b; > +} > + > +static inline int > +pg_cmp_u16(uint16 a, uint16 b) > +{ > + return a - b; > +} > + > +static inline int > +pg_cmp_s32(int32 a, int32 b) > +{ > + return (a > b) - (a < b); > +} > + > +static inline int > +pg_cmp_u32(uint32 a, uint32 b) > +{ > + return (a > b) - (a < b); > +} > + > +static inline int > +pg_cmp_s64(int64 a, int64 b) > +{ > + return (a > b) - (a < b); > +} > + > +static inline int > +pg_cmp_u64(uint64 a, uint64 b) > +{ > + return (a > b) - (a < b); > +} As suggested above, IMHO we should be rather liberal with the casting to ensure it is abundantly clear what is happening here. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com