Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gcEBI-0004Kt-82 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 18:45:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gcEBG-0002aE-Vw for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Dec 2018 18:45:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gcEBG-0002a7-LW for pgsql-hackers@lists.postgresql.org; Wed, 26 Dec 2018 18:45:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gcEB8-0003KT-SJ for pgsql-hackers@postgresql.org; Wed, 26 Dec 2018 18:45:21 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id wBQIj0Zx003860; Wed, 26 Dec 2018 13:45:00 -0500 From: Tom Lane To: Alvaro Herrera cc: Adrien Nayrat , Thomas Munro , Dmitry Dolgov <9erthalion6@gmail.com>, Tomas Vondra , vik.fearing@2ndquadrant.com, Robert Haas , Michael Paquier , David Rowley , Pg Hackers Subject: random() (was Re: New GUC to sample log queries) In-reply-to: <20181129214850.kiq6mvofsgplqjdr@alvherre.pgsql> References: <20181129214850.kiq6mvofsgplqjdr@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Thu, 29 Nov 2018 18:48:50 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3858.1545849900.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 26 Dec 2018 13:45:00 -0500 Message-ID: <3859.1545849900@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera writes: > Thanks! I pushed this with two changes -- one was to reword the docs a > bit more, and the other was to compute in_sample only if it's going to > be used (when exceeded is true). I hope this won't upset any compilers = ... > I wonder if there's any sensible way to verify the behavior in an > automated fashion. Who know what marvels we'd discover about the > reliability/uniformity of random() across platforms. So Coverity doesn't like this patch: 2250 in_sample =3D exceeded && 2251 log_statement_sample_rate !=3D 0 && 2252 (log_statement_sample_rate =3D=3D 1 || >>> CID 1441909: Security best practices violations (DC.WEAK_CRYPTO) >>> "random" should not be used for security related applications, as = linear congruential algorithms are too easy to break. 2253 random() <=3D log_statement_sample_rate * MAX_RANDOM_VALUE); 2254 = 2255 if ((exceeded && in_sample) || log_duration) I am not sure I buy the argument that this is a security hazard, but there are other reasons to question the use of random() here, some of which you stated yourself above. Another one is that using random() for internal purposes interferes with applications' possible use of drandom() and setseed(), ie an application depending on getting a particular random series would see different behavior depending on whether this GUC is active or not. I wonder whether we should establish a project policy to avoid use of random() for internal purposes, ie try to get to a point where drandom() is the only caller in the backend. A quick grep says that there's a dozen or so callers, so this patch certainly isn't the only offender ... but should we make an effort to convert them all to use, say, pg_erand48()? I think all the existing callers could happily share a process-wide random state, so we could make a wrapper that's no harder to use than random(). Another idea, which would be a lot less prone to breakage by add-on code, is to change drandom() and setseed() to themselves use pg_erand48() with a private seed. Then we could positively promise that their behavior isn't affected by anything else (and it'd become platform-independent, too, which it probably isn't today). There would be a one-time compatibility breakage for anyone who's depending on the exact current behavior, but that might be OK considering how weak our guarantees are for it right now. regards, tom lane