From patchwork Fri Aug 3 13:19:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janne Blomqvist X-Patchwork-Id: 953231 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-483103-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="A2/DolI+"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="i5CnpjGN"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41hnhC2kSrz9s3Z for ; Fri, 3 Aug 2018 23:19:37 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; q=dns; s=default; b=PiHwAxx2Gufd NJWkNEbyrVe0saYABKDQo2k/KUyXXWf0xFRjMJ+9xohm36Phtf+BzRFDS+F6ggdd AYpG2pFdUmMISJw/RndnllLPpPrkNWXuOeYUw1RQ1MopwiYsVn7OaEDYrC8hD6jC Mj0ybHTXkpnYcwGBjeVQCnVaGZnECmg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id; s=default; bh=fhVpMjilJiQJt3YmJL YfwXm3KPo=; b=A2/DolI+8p+UTWM5IimrFvA236IvVtstK+IaxfgEvlobsGaXPd k5iSMukRZ63AeECu6jqD6XHDjDeWSRh3IN2x6Y3tqXJ5SOY/DOzCC+wFbNdLLUeN hsB3pLV3daKzycI9xv9u8ZdMWYieg0y36dqxlu2G9MO0JEZCx3AmQw4LY= Received: (qmail 46774 invoked by alias); 3 Aug 2018 13:19:29 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 43624 invoked by uid 89); 3 Aug 2018 13:19:24 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS, TIME_LIMIT_EXCEEDED autolearn=unavailable version=3.3.2 spammy=alarm, H*m:gmail, H*r:sk:w16-v6s, Janne X-HELO: mail-lj1-f181.google.com Received: from mail-lj1-f181.google.com (HELO mail-lj1-f181.google.com) (209.85.208.181) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 03 Aug 2018 13:19:14 +0000 Received: by mail-lj1-f181.google.com with SMTP id w16-v6so4900446ljh.12; Fri, 03 Aug 2018 06:19:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=uhPNZ4keFG0smXzee5tjjEgeqixycWiAVsfIBJyS27k=; b=i5CnpjGNuGRCxreA9fVsLnhBrDOdv6saCDEuQlGOfpIwMl/Qob779lymObiik7ic7G C2n4DBQAE2Xf3nU++wlsgp9WWRzv/0cUKGb+/8FpLq3vEeaBQrNfQNhJkLwJA5YTwm8P zrxHSAYVUQo5Co7T74Wd8W/3NHSLiANNfGL//439EJPVaTw4+gZriG2b9b5u/2nJBRhO DFlJQJ5xtMubT4vmCCLI/MR/1eglF2ncFtE1rx2u2PwYG4i4NRP5HV05KzArTpUSmDjT DHMSwT00jgLAPQSCxa4QfCmtsFJ+DCTvEkE4aDb49t0tGqALbpraSjJ4Fa+H+Z+GUkbC zuUg== Received: from dt.lan (88-114-247-254.elisa-laajakaista.fi. [88.114.247.254]) by smtp.gmail.com with ESMTPSA id b22-v6sm898643ljj.93.2018.08.03.06.19.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Aug 2018 06:19:10 -0700 (PDT) From: Janne Blomqvist To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Janne Blomqvist Subject: [PATCH] Use getentropy() for seeding PRNG Date: Fri, 3 Aug 2018 16:19:03 +0300 Message-Id: <20180803131903.24303-1-blomqvist.janne@gmail.com> The getentropy function, found on Linux, OpenBSD, and recently also FreeBSD, can be used to get random bytes to initialize the PRNG. It is similar to the traditional way of reading from /dev/urandom, but being a system call rather than a special file, it doesn't suffer from problems like running out of file descriptors, or failure when running in a container where /dev/urandom is not available. Regtested on x86_64-pc-linux-gnu, Ok for trunk? 2018-08-03 Janne Blomqvist * configure.ac: Check for getentropy. * intrinsics/random.c (getosrandom): Use getentropy if available. * config.h.in: Regenerated. * configure: Regenerated. --- libgfortran/configure.ac | 3 ++- libgfortran/intrinsics/random.c | 7 ++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/libgfortran/configure.ac b/libgfortran/configure.ac index bf6d3634dda..900c7466dec 100644 --- a/libgfortran/configure.ac +++ b/libgfortran/configure.ac @@ -312,7 +312,8 @@ if test "${hardwire_newlib:-0}" -eq 1; then fi else AC_CHECK_FUNCS_ONCE(getrusage times mkstemp strtof strtold snprintf \ - ftruncate chsize chdir getlogin gethostname kill link symlink sleep ttyname \ + ftruncate chsize chdir getentropy getlogin gethostname kill link symlink \ + sleep ttyname \ alarm access fork setmode fcntl \ gettimeofday stat fstat lstat getpwuid vsnprintf dup \ getcwd localtime_r gmtime_r getpwuid_r ttyname_r clock_gettime \ diff --git a/libgfortran/intrinsics/random.c b/libgfortran/intrinsics/random.c index 234c5ff95fd..1f47f32188b 100644 --- a/libgfortran/intrinsics/random.c +++ b/libgfortran/intrinsics/random.c @@ -309,12 +309,9 @@ getosrandom (void *buf, size_t buflen) for (size_t i = 0; i < buflen / sizeof (unsigned int); i++) rand_s (&b[i]); return buflen; +#elif defined(HAVE_GETENTROPY) + return getentropy (buf, buflen); #else - /* - TODO: When glibc adds a wrapper for the getrandom() system call - on Linux, one could use that. - - TODO: One could use getentropy() on OpenBSD. */ int flags = O_RDONLY; #ifdef O_CLOEXEC flags |= O_CLOEXEC;