Received: by 10.192.165.156 with SMTP id m28csp141916imm; Thu, 12 Apr 2018 18:32:42 -0700 (PDT) X-Google-Smtp-Source: AIpwx4889DKRsEIYZ5P/RRgGGW2Ca8dVgG1dRdYJVtYrQGNxqOTVrguozByOxvYZmnU6EmAASoCK X-Received: by 10.99.123.92 with SMTP id k28mr2455290pgn.146.1523583162647; Thu, 12 Apr 2018 18:32:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523583162; cv=none; d=google.com; s=arc-20160816; b=VfOZkThmHboyYaUF6GGsgLjIh2aKZTx8Mc0QBg7oDDhRf/TgKV+bQ7YmAaJbLk7raf 1YkpV3nd+UMtJQOXxY4UBE8LpzoxuJNrGWXAdb9AUGmkL2IIEFyz17J47Omn5JDTEZLy Qt0KfF7Luzf3YxqcoI8uXdYTpupv5i1JQSADqMvHRuAW3/oEeXfAjoLvzIN07EvCIDyf U6l1Pwc7AdQGoBu0qlgSLTnCXnUfSTb+L5q4rSmyZZwQ0Q41ZF9hcDU2HsFQbar+mtaZ upCnk7weJnBxEcG8J7uh7FjJa+bSHVlbW5U5S1/N01bZ+KIceuvZrykCznzlaj2aoxlQ t5AQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=IhWqwlJeN7cwdC89uKmE41ioArclsQiaIQhVahAfj4s=; b=0h5ABvysySeyJCUT94uY2wCW7MlFwJEdn/kwLr25f0fq6gOx9yFrG/fco33AgsvJnK 30wKC7k3tx/ikAT6fvwbyU0YKFB4AhoNIyiBb5oOQS7++cvaawHJnZC4geHNn5WWw23D DkKZHRuIHbCneovc604tQWCx98t/mTFgF8xsRW6vlufwD8ouPpHKjoNNKwaFAMT5jjM0 RaZF4UIXfegN5vqBI73uCJjY+fq8w6sweKP65FzxYlu3LsmgnASEBiZtBKaF+u/RsP68 epslBPLkoSylHYYBSoD8oofBJ+eyhoQnFke1UvVLtVjD4+CKHDFDGUreCp2/nsFnyCNO aYIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@thunk.org header.s=ef5046eb header.b=oN/HsfhD; 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 202si3041091pgg.261.2018.04.12.18.32.29; Thu, 12 Apr 2018 18:32:42 -0700 (PDT) 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; dkim=fail header.i=@thunk.org header.s=ef5046eb header.b=oN/HsfhD; 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 S1753328AbeDMBbA (ORCPT + 99 others); Thu, 12 Apr 2018 21:31:00 -0400 Received: from imap.thunk.org ([74.207.234.97]:37190 "EHLO imap.thunk.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751803AbeDMBa5 (ORCPT ); Thu, 12 Apr 2018 21:30:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=thunk.org; s=ef5046eb; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=IhWqwlJeN7cwdC89uKmE41ioArclsQiaIQhVahAfj4s=; b=oN/HsfhDIZUmOE599qzDyTJ+vB O6c13+5QAizZtIF9YttetqaP0PhEdLJebkDneT5qvyR4lyjINd0Dml+jq4axcDRbgLuFFVAkvHOP3 4hnfZmLzD64fkRYXva9MyqTlwJ77ATQSHyIWXnS8NGsLTqKV6m6jZTxwEdPcWVhqMRmY=; Received: from root (helo=callcc.thunk.org) by imap.thunk.org with local-esmtp (Exim 4.89) (envelope-from ) id 1f6nYG-0002cy-DX; Fri, 13 Apr 2018 01:30:56 +0000 Received: by callcc.thunk.org (Postfix, from userid 15806) id 5F7B27A2E4E; Thu, 12 Apr 2018 21:30:55 -0400 (EDT) From: Theodore Ts'o To: linux-crypto@vger.kernel.org Cc: Linux Kernel Developers List , Theodore Ts'o , stable@kernel.org Subject: [PATCH 3/5] random: set up the NUMA crng instances after the CRNG is fully initialized Date: Thu, 12 Apr 2018 21:30:44 -0400 Message-Id: <20180413013046.404-3-tytso@mit.edu> X-Mailer: git-send-email 2.16.1.72.g5be1f00a9a In-Reply-To: <20180413013046.404-1-tytso@mit.edu> References: <20180413013046.404-1-tytso@mit.edu> X-SA-Exim-Connect-IP: X-SA-Exim-Mail-From: tytso@thunk.org X-SA-Exim-Scanned: No (on imap.thunk.org); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Until the primary_crng is fully initialized, don't initialize the NUMA crng nodes. Otherwise users of /dev/urandom on NUMA systems before the CRNG is fully initialized can get very bad quality randomness. Of course everyone should move to getrandom(2) where this won't be an issue, but there's a lot of legacy code out there. Reported-by: Jann Horn Fixes: 1e7f583af67b ("random: make /dev/urandom scalable for silly...") Cc: stable@kernel.org # 4.8+ Signed-off-by: Theodore Ts'o --- drivers/char/random.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/drivers/char/random.c b/drivers/char/random.c index 2154a5fe4c81..681ee0c0de24 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -787,6 +787,32 @@ static void crng_initialize(struct crng_state *crng) crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; } +#ifdef CONFIG_NUMA +static void numa_crng_init(void) +{ + int i; + struct crng_state *crng; + struct crng_state **pool; + + pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL); + for_each_online_node(i) { + crng = kmalloc_node(sizeof(struct crng_state), + GFP_KERNEL | __GFP_NOFAIL, i); + spin_lock_init(&crng->lock); + crng_initialize(crng); + pool[i] = crng; + } + mb(); + if (cmpxchg(&crng_node_pool, NULL, pool)) { + for_each_node(i) + kfree(pool[i]); + kfree(pool); + } +} +#else +static int numa_crng_init(void) {} +#endif + /* * crng_fast_load() can be called by code in the interrupt service * path. So we can't afford to dilly-dally. @@ -893,6 +919,7 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) spin_unlock_irqrestore(&primary_crng.lock, flags); if (crng == &primary_crng && crng_init < 2) { invalidate_batched_entropy(); + numa_crng_init(); crng_init = 2; process_random_ready_list(); wake_up_interruptible(&crng_init_wait); @@ -1727,28 +1754,9 @@ static void init_std_data(struct entropy_store *r) */ static int rand_initialize(void) { -#ifdef CONFIG_NUMA - int i; - struct crng_state *crng; - struct crng_state **pool; -#endif - init_std_data(&input_pool); init_std_data(&blocking_pool); crng_initialize(&primary_crng); - -#ifdef CONFIG_NUMA - pool = kcalloc(nr_node_ids, sizeof(*pool), GFP_KERNEL|__GFP_NOFAIL); - for_each_online_node(i) { - crng = kmalloc_node(sizeof(struct crng_state), - GFP_KERNEL | __GFP_NOFAIL, i); - spin_lock_init(&crng->lock); - crng_initialize(crng); - pool[i] = crng; - } - mb(); - crng_node_pool = pool; -#endif return 0; } early_initcall(rand_initialize); -- 2.16.1.72.g5be1f00a9a