Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp881083imj; Fri, 15 Feb 2019 08:18:45 -0800 (PST) X-Google-Smtp-Source: AHgI3IbjrzUJFXUGjlkS2PnMaShXd6kWisuIwjYyyWX0wsK4rzNZzbDCICw3EGVby25lYjeiAMIK X-Received: by 2002:a17:902:a7:: with SMTP id a36mr10968034pla.295.1550247524970; Fri, 15 Feb 2019 08:18:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550247524; cv=none; d=google.com; s=arc-20160816; b=xxNMewH+35pAMGIClKRk5tbXoHltb0F/iKR8y7Y0//xM6GCTH+aNEquX2e+3yRQRRU QFPu+qCrQAasOjPY1/UHDEb8IOIU30J8/437DJz5dlf4stL6tib+GR8EDuhOMLH5umzV 64/7S3GZB2KPWw4N0aZL3n4S79XGGQdF+wqcOfqtyLGsQB3+aYeXv8QGZ43KeRkZsKGz wic+RnMoUYSPssZcK2Qa8JjrGgg2qLso6aqXzf48Bkv4txWhCHT8Sagp5P6C/FXpqByX lEJ5OgdePGUaAYCHCJbQoh0/rDNB//GzRiEOlMzA1WwG1HgDrAzUd9vpc1+rn2MvnLXz MiPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :content-id:content-language:accept-language:in-reply-to:references :message-id:date:thread-index:thread-topic:subject:to:from; bh=7gHRJ5s6pOF7FaCwDKELq7j5Bor7aM7haaCdviRiBHI=; b=nr7hxK+8qxYGFLryRPIrmOOgVucVC6tiFXNpZT/k/N/cxSk+C6nj0TvHulyRNOPV7n rz/ruaGZ0lPGIXSoNAMb/PI++uiK7JWVqnxRKh2XzeL/Y79EgumC7ueslUEzXz31Y3f8 c0Ku5/NWmG45+dczLwX4ag+js9CptGTZPzziK1qFSGiTTieC3ULEJB0nwuzmb6CqjR0U e3eAa5eZnvoJaDg5pKJ0xqLsqtbuuzQ/rz52lMrFPciGBaX9JH3HKA/R1wf+p4Tl2S4G eBdpfguvE41oht2YGh1Iep/MP5Awl39y8UMKCx4LBBg04fSxbb8XDzQCKt50k+IvizAd VQPg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r17si5267268pgv.329.2019.02.15.08.18.28; Fri, 15 Feb 2019 08:18:44 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389458AbfBON6Y convert rfc822-to-8bit (ORCPT + 99 others); Fri, 15 Feb 2019 08:58:24 -0500 Received: from mail-oln040092069078.outbound.protection.outlook.com ([40.92.69.78]:63568 "EHLO EUR02-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2389179AbfBON6Y (ORCPT ); Fri, 15 Feb 2019 08:58:24 -0500 Received: from VE1EUR02FT048.eop-EUR02.prod.protection.outlook.com (10.152.12.57) by VE1EUR02HT004.eop-EUR02.prod.protection.outlook.com (10.152.12.154) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1580.10; Fri, 15 Feb 2019 13:58:21 +0000 Received: from VI1PR0702MB3840.eurprd07.prod.outlook.com (10.152.12.51) by VE1EUR02FT048.mail.protection.outlook.com (10.152.13.177) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1580.10 via Frontend Transport; Fri, 15 Feb 2019 13:58:21 +0000 Received: from VI1PR0702MB3840.eurprd07.prod.outlook.com ([fe80::6139:4cf3:fb81:b105]) by VI1PR0702MB3840.eurprd07.prod.outlook.com ([fe80::6139:4cf3:fb81:b105%4]) with mapi id 15.20.1622.016; Fri, 15 Feb 2019 13:58:21 +0000 From: Bernd Edlinger To: Theodore Ts'o , Arnd Bergmann , "Greg Kroah-Hartman" , "linux-kernel@vger.kernel.org" Subject: [PATCHv2] random: Make /dev/random wait for crng_ready Thread-Topic: [PATCHv2] random: Make /dev/random wait for crng_ready Thread-Index: AQHUxTaCraRKyzzTqEeNDzUOb6MJSA== Date: Fri, 15 Feb 2019 13:58:20 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US, en-GB, de-DE Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: AM6PR07CA0028.eurprd07.prod.outlook.com (2603:10a6:209:2a::41) To VI1PR0702MB3840.eurprd07.prod.outlook.com (2603:10a6:803:f::33) x-incomingtopheadermarker: OriginalChecksum:518EC2E4920B5CE4E54595EE7F9D75BB0C5CB5B5CC1B25C77F3A8B5C16E8CD37;UpperCasedChecksum:92D2516CABA04D84F283EBD246AADF0C5AE2723ECAA6F1BE5CE9940748AFF645;SizeAsReceived:8568;Count:62 x-ms-exchange-messagesentrepresentingtype: 1 x-tmn: [CUwzH2k5aV1a+EuzE4UNK9l9YFq4Tu5c] x-microsoft-original-message-id: x-ms-publictraffictype: Email x-incomingheadercount: 62 x-eopattributedmessage: 0 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(201702061078)(5061506573)(5061507331)(1603103135)(2017031320274)(2017031322404)(2017031323274)(2017031324274)(1601125500)(1603101475)(1701031045);SRVR:VE1EUR02HT004; x-ms-traffictypediagnostic: VE1EUR02HT004: x-exchange-antispam-report-cfa-test: BCL:0;PCL:0;RULEID:(4566010)(82015058);SRVR:VE1EUR02HT004;BCL:0;PCL:0;RULEID:;SRVR:VE1EUR02HT004; x-microsoft-antispam-message-info: 1VZM3GpJrOQPS4i+UR3ZeIh4mD1sPa8DAACnLahM1Ip24LFQD73NvBv/8kU1piri Content-Type: text/plain; charset="Windows-1252" Content-ID: <6BD5007BEADE9D469EFDCBABE8987754@eurprd07.prod.outlook.com> Content-Transfer-Encoding: 8BIT MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: d4d70346-2c10-4f39-8c00-e767963926d9 X-MS-Exchange-CrossTenant-Network-Message-Id: 9eef8b67-a072-4a0f-a06c-08d6934da4b8 X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: d4d70346-2c10-4f39-8c00-e767963926d9 X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Feb 2019 13:58:20.1499 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Internet X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-Transport-CrossTenantHeadersStamped: VE1EUR02HT004 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Reading from /dev/random may return data while the getrandom syscall is still blocking. Those bytes are not yet cryptographically secure. The first byte from /dev/random can have as little as 8 bits entropy estimation. Once a read blocks, it will block until /proc/sys/kernel/random/read_wakeup_threshold bits are available, which is usually 64 bits, but can be configured as low as 8 bits. A select will wake up when at least read_wakeup_threshold bits are available. Also when constantly reading bytes out of /dev/random it will prevent the crng init done event forever. Fixed by making read and select on /dev/random wait until the crng is fully initialized. Signed-off-by: Bernd Edlinger --- v2: Fixed one white space in the code. Also added some more details about the problem to the commit message. --- drivers/char/random.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index bd449ad..c8f16f0 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -721,7 +721,7 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits) } /* should we wake readers? */ - if (entropy_bits >= random_read_wakeup_bits && + if (crng_ready() && entropy_bits >= random_read_wakeup_bits && wq_has_sleeper(&random_read_wait)) { wake_up_interruptible(&random_read_wait); kill_fasync(&fasync, SIGIO, POLL_IN); @@ -1826,7 +1826,9 @@ _random_read(int nonblock, char __user *buf, size_t nbytes) nbytes = min_t(size_t, nbytes, SEC_XFER_SIZE); while (1) { - n = extract_entropy_user(&blocking_pool, buf, nbytes); + n = crng_ready() + ? extract_entropy_user(&blocking_pool, buf, nbytes) + : 0; if (n < 0) return n; trace_random_read(n*8, (nbytes-n)*8, @@ -1840,6 +1842,7 @@ _random_read(int nonblock, char __user *buf, size_t nbytes) return -EAGAIN; wait_event_interruptible(random_read_wait, + crng_ready() && ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits); if (signal_pending(current)) @@ -1884,7 +1887,8 @@ random_poll(struct file *file, poll_table * wait) poll_wait(file, &random_read_wait, wait); poll_wait(file, &random_write_wait, wait); mask = 0; - if (ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits) + if (crng_ready() && + ENTROPY_BITS(&input_pool) >= random_read_wakeup_bits) mask |= EPOLLIN | EPOLLRDNORM; if (ENTROPY_BITS(&input_pool) < random_write_wakeup_bits) mask |= EPOLLOUT | EPOLLWRNORM; -- 2.7.4