Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3114523pxb; Mon, 18 Apr 2022 16:18:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwksaZ+z3tzxox/rG42sW519JKf/KVDDPGYjV4DH3WwCTx2B/CKXLzka3i4H69XHfAx9+PG X-Received: by 2002:a63:135a:0:b0:3aa:2cf6:ffd7 with SMTP id 26-20020a63135a000000b003aa2cf6ffd7mr1366493pgt.351.1650323937098; Mon, 18 Apr 2022 16:18:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650323937; cv=none; d=google.com; s=arc-20160816; b=hXKU4sYpFo2eMMgB13r0Eo3nWT1hV5zP0VgnT0WRaXcZydEtZmwKIAXlKhUVqokx2S qET2050BVHxi6Sd1f9YIOHoFkuufvl2E1/ctLuzuCYPQUYu+Vx64kmv3f+9UE7vzDAWX OMPEmO6nMllZml989KIgT5gM04r8nrzfuOZc4vfFGD0jau7AhKcnZyFikr1YliGEjwrv ROrPAB3qMA7geukCbeRETROCDg7kQPNv/9gEUx3BSIpjYpnGstTdIFXiWp0cwyQY2zDw vamXEE5DtXFuncxlGEXmw6KrwNJxpU0N8KBd6wbfPyc4EI+l6OGq5pMjoVSdZJU7+k5e 5kiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=v0jH66380i5kMqz4FID0Imhjl02it5txeG01IpKgdMU=; b=gi+m5YO8Clg7iySWYWQdrqcnPucijBUmjfLLRtDFFvnm5g3ooQNupsSpoFTw3D5lo4 3wSAQZoGA4lEnI6CvGPvBklKg+3aViTiVqWHwKSZmjl+pN+NV6abxl/fHLv63RDGm7J6 2RWg8Cj6rKl4MKk0F1YoyCQNzB0xOPyV4F1IKXhnQKKYOLZXiIIw96IAFTbHPz/n8chk OhSF4YdgZMBNrZfAKsQHUF4jdhxQ6tPLU+2TDZA2Rz+P7XFHc2KT84kLwsMVyJQqsqwL aryrl5RngTvOcjD1X/hiMlKRrenvkgC9IwSMy5cvkKvQ8yvdPUdOCiifPl8ozi38hYyA QQHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DIQwbI0O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a191-20020a6390c8000000b0039d74ad4055si9663198pge.760.2022.04.18.16.18.40; Mon, 18 Apr 2022 16:18:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DIQwbI0O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245165AbiDROGf (ORCPT + 99 others); Mon, 18 Apr 2022 10:06:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35290 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244537AbiDRNtg (ORCPT ); Mon, 18 Apr 2022 09:49:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB2D9433BB; Mon, 18 Apr 2022 06:01:39 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 0A25660B3C; Mon, 18 Apr 2022 13:01:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1B522C385A1; Mon, 18 Apr 2022 13:01:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286898; bh=dJzCn9cIQspghMBiSeA+o7H9D+g3/toGnQtHulXDBOU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DIQwbI0OvzVdm2jtlSjDI95WQV31Lew6cd88fuXPSoqpTKtZBwghs/ekH21wuyuQA Js+Kc4HAO1U5rkgJVQNQvcDrC+G3zbHCpZwQWlFEngUcIc5zDRBt1UgewAWDqeHsIb 2O1BJb/cSN1sgdYmChO0ZEVdeFw901idJoPI6Wh8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, PaX Team , "Jason A. Donenfeld" , Kees Cook Subject: [PATCH 4.14 280/284] gcc-plugins: latent_entropy: use /dev/urandom Date: Mon, 18 Apr 2022 14:14:21 +0200 Message-Id: <20220418121221.118220161@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason A. Donenfeld commit c40160f2998c897231f8454bf797558d30a20375 upstream. While the latent entropy plugin mostly doesn't derive entropy from get_random_const() for measuring the call graph, when __latent_entropy is applied to a constant, then it's initialized statically to output from get_random_const(). In that case, this data is derived from a 64-bit seed, which means a buffer of 512 bits doesn't really have that amount of compile-time entropy. This patch fixes that shortcoming by just buffering chunks of /dev/urandom output and doling it out as requested. At the same time, it's important that we don't break the use of -frandom-seed, for people who want the runtime benefits of the latent entropy plugin, while still having compile-time determinism. In that case, we detect whether gcc's set_random_seed() has been called by making a call to get_random_seed(noinit=true) in the plugin init function, which is called after set_random_seed() is called but before anything that calls get_random_seed(noinit=false), and seeing if it's zero or not. If it's not zero, we're in deterministic mode, and so we just generate numbers with a basic xorshift prng. Note that we don't detect if -frandom-seed is being used using the documented local_tick variable, because it's assigned via: local_tick = (unsigned) tv.tv_sec * 1000 + tv.tv_usec / 1000; which may well overflow and become -1 on its own, and so isn't reliable: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105171 [kees: The 256 byte rnd_buf size was chosen based on average (250), median (64), and std deviation (575) bytes of used entropy for a defconfig x86_64 build] Fixes: 38addce8b600 ("gcc-plugins: Add latent_entropy plugin") Cc: stable@vger.kernel.org Cc: PaX Team Signed-off-by: Jason A. Donenfeld Signed-off-by: Kees Cook Link: https://lore.kernel.org/r/20220405222815.21155-1-Jason@zx2c4.com Signed-off-by: Greg Kroah-Hartman --- scripts/gcc-plugins/latent_entropy_plugin.c | 44 +++++++++++++++++----------- 1 file changed, 27 insertions(+), 17 deletions(-) --- a/scripts/gcc-plugins/latent_entropy_plugin.c +++ b/scripts/gcc-plugins/latent_entropy_plugin.c @@ -86,25 +86,31 @@ static struct plugin_info latent_entropy .help = "disable\tturn off latent entropy instrumentation\n", }; -static unsigned HOST_WIDE_INT seed; -/* - * get_random_seed() (this is a GCC function) generates the seed. - * This is a simple random generator without any cryptographic security because - * the entropy doesn't come from here. - */ +static unsigned HOST_WIDE_INT deterministic_seed; +static unsigned HOST_WIDE_INT rnd_buf[32]; +static size_t rnd_idx = ARRAY_SIZE(rnd_buf); +static int urandom_fd = -1; + static unsigned HOST_WIDE_INT get_random_const(void) { - unsigned int i; - unsigned HOST_WIDE_INT ret = 0; - - for (i = 0; i < 8 * sizeof(ret); i++) { - ret = (ret << 1) | (seed & 1); - seed >>= 1; - if (ret & 1) - seed ^= 0xD800000000000000ULL; + if (deterministic_seed) { + unsigned HOST_WIDE_INT w = deterministic_seed; + w ^= w << 13; + w ^= w >> 7; + w ^= w << 17; + deterministic_seed = w; + return deterministic_seed; } - return ret; + if (urandom_fd < 0) { + urandom_fd = open("/dev/urandom", O_RDONLY); + gcc_assert(urandom_fd >= 0); + } + if (rnd_idx >= ARRAY_SIZE(rnd_buf)) { + gcc_assert(read(urandom_fd, rnd_buf, sizeof(rnd_buf)) == sizeof(rnd_buf)); + rnd_idx = 0; + } + return rnd_buf[rnd_idx++]; } static tree tree_get_random_const(tree type) @@ -549,8 +555,6 @@ static void latent_entropy_start_unit(vo tree type, id; int quals; - seed = get_random_seed(false); - if (in_lto_p) return; @@ -585,6 +589,12 @@ __visible int plugin_init(struct plugin_ const struct plugin_argument * const argv = plugin_info->argv; int i; + /* + * Call get_random_seed() with noinit=true, so that this returns + * 0 in the case where no seed has been passed via -frandom-seed. + */ + deterministic_seed = get_random_seed(true); + static const struct ggc_root_tab gt_ggc_r_gt_latent_entropy[] = { { .base = &latent_entropy_decl,