Received: by 10.192.165.148 with SMTP id m20csp2519498imm; Sun, 22 Apr 2018 08:43:22 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/2fhjFMV7WTi5H6Jq4rtH0MaSfCIzCgPK5p2BcoaM2FlOvX8bgtRYoXZbUbOINWVImvXjL X-Received: by 10.101.97.200 with SMTP id j8mr14569845pgv.443.1524411802193; Sun, 22 Apr 2018 08:43:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524411802; cv=none; d=google.com; s=arc-20160816; b=xOt1s15G4JMmed6RrwQVk2q02ZpkVrRPU6ue2ae00dksRTNZhMmMt5LMfeY28Gf4iC 1oECy84Q936RTlE2nGUZLINUywS9Rxtftx4+2Pto3Yi44oxTG69DGfOwcNrbchTR7KSZ t7IA4dPl9uC//vnt5nVooUQLKSiyaDNwgBIUDuzsKdtXaQfX8eCziukSJb4yO91oaN9n dkZfxdYPlcaNIPDUXgfud528YCF2IKfSlV2Y2e0HR+KXtp0XYeyZIovrLQOIIK+6eesQ pKJ8SjCI8Oz3zltNXuR5/tdplx/cHoYAzMNo22snmVkUztD1LxFltXxVPSgln9Y9SWxo JYiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=9rN2lu/tiiKWYhfLQi4k3qph+GV1gtjVTUrWkwtc8Xg=; b=oWrLwpG7q5uzWaURYAqZsVF8rXJHzQN3Pxvavg8i2JXOrkDjN+EigMwGwrTSRfA+y/ 0fMduIo79xqYI3/NGR6VkRedoY+XodwbTAipbV+mluCtfGwt8tgxQqQRUJxwVAmkyAxq Pu9Djz4gbdILAzyR8GNoFwzZNYeXlzWQvRRE/CZ5NznNsep44J+gpWRYLnLxLDbM7MRI KXyr4XQKWIfHDdqzD11EdxLYXNOfHkud2Sx0g8LhDOxFm4V52TExiLpeU4nc57QG6klS CZCJYwtftKWL5SMTxJ1mRMlhV3e9S3+2g7bA+xCsNfruQmnuDb7lTKDo5k9P+YiaWEX+ eatA== 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 m9si2128086pgn.665.2018.04.22.08.43.08; Sun, 22 Apr 2018 08:43:22 -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; 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 S1755190AbeDVPlk (ORCPT + 99 others); Sun, 22 Apr 2018 11:41:40 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:49174 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755032AbeDVODU (ORCPT ); Sun, 22 Apr 2018 10:03:20 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D3CDF9C; Sun, 22 Apr 2018 14:03:19 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Theodore Tso , stable@kernel.org Subject: [PATCH 4.16 164/196] random: use a different mixing algorithm for add_device_randomness() Date: Sun, 22 Apr 2018 15:53:04 +0200 Message-Id: <20180422135112.744932103@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Theodore Ts'o commit dc12baacb95f205948f64dc936a47d89ee110117 upstream. add_device_randomness() use of crng_fast_load() was highly problematic. Some callers of add_device_randomness() can pass in a large amount of static information. This would immediately promote the crng_init state from 0 to 1, without really doing much to initialize the primary_crng's internal state with something even vaguely unpredictable. Since we don't have the speed constraints of add_interrupt_randomness(), we can do a better job mixing in the what unpredictability a device driver or architecture maintainer might see fit to give us, and do it in a way which does not bump the crng_init_cnt variable. Also, since add_device_randomness() doesn't bump any entropy accounting in crng_init state 0, mix the device randomness into the input_pool entropy pool as well. This is related to CVE-2018-1108. Reported-by: Jann Horn Fixes: ee7998c50c26 ("random: do not ignore early device randomness") Cc: stable@kernel.org # 4.13+ Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -786,6 +786,10 @@ static void crng_initialize(struct crng_ crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1; } +/* + * crng_fast_load() can be called by code in the interrupt service + * path. So we can't afford to dilly-dally. + */ static int crng_fast_load(const char *cp, size_t len) { unsigned long flags; @@ -812,6 +816,51 @@ static int crng_fast_load(const char *cp return 1; } +/* + * crng_slow_load() is called by add_device_randomness, which has two + * attributes. (1) We can't trust the buffer passed to it is + * guaranteed to be unpredictable (so it might not have any entropy at + * all), and (2) it doesn't have the performance constraints of + * crng_fast_load(). + * + * So we do something more comprehensive which is guaranteed to touch + * all of the primary_crng's state, and which uses a LFSR with a + * period of 255 as part of the mixing algorithm. Finally, we do + * *not* advance crng_init_cnt since buffer we may get may be something + * like a fixed DMI table (for example), which might very well be + * unique to the machine, but is otherwise unvarying. + */ +static int crng_slow_load(const char *cp, size_t len) +{ + unsigned long flags; + static unsigned char lfsr = 1; + unsigned char tmp; + unsigned i, max = CHACHA20_KEY_SIZE; + const char * src_buf = cp; + char * dest_buf = (char *) &primary_crng.state[4]; + + if (!spin_trylock_irqsave(&primary_crng.lock, flags)) + return 0; + if (crng_init != 0) { + spin_unlock_irqrestore(&primary_crng.lock, flags); + return 0; + } + if (len > max) + max = len; + + for (i = 0; i < max ; i++) { + tmp = lfsr; + lfsr >>= 1; + if (tmp & 1) + lfsr ^= 0xE1; + tmp = dest_buf[i % CHACHA20_KEY_SIZE]; + dest_buf[i % CHACHA20_KEY_SIZE] ^= src_buf[i % len] ^ lfsr; + lfsr += (tmp << 3) | (tmp >> 5); + } + spin_unlock_irqrestore(&primary_crng.lock, flags); + return 1; +} + static void crng_reseed(struct crng_state *crng, struct entropy_store *r) { unsigned long flags; @@ -981,10 +1030,8 @@ void add_device_randomness(const void *b unsigned long time = random_get_entropy() ^ jiffies; unsigned long flags; - if (!crng_ready()) { - crng_fast_load(buf, size); - return; - } + if (!crng_ready() && size) + crng_slow_load(buf, size); trace_add_device_randomness(size, _RET_IP_); spin_lock_irqsave(&input_pool.lock, flags);