Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6136189iog; Thu, 23 Jun 2022 12:06:14 -0700 (PDT) X-Google-Smtp-Source: AGRyM1vCwKDJaJWgSESM+c5st0f4gKQZ2HMdFFGJgl/5qAsEpqPw+4JNs7PlxihKLFFdDqZHjq7x X-Received: by 2002:a17:907:eab:b0:70f:599c:c730 with SMTP id ho43-20020a1709070eab00b0070f599cc730mr9954732ejc.362.1656011174404; Thu, 23 Jun 2022 12:06:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011174; cv=none; d=google.com; s=arc-20160816; b=pxWgq3ebpL+60Z2GhIs+/PkBDkU5EMk6Fh8sOyEVYktl8TqLoB5aRMF57UjEUoUNXM aOxQrwZrHjUdxkNJDMJ1KIQ0QRf7Ux1IclbUX6QvqrEOnRkesQk4Kb9n9HOp0GifYrHN KEZvCyLpkNnlly/1yTaT7Ub+PrFX4mj9xZ/oXAAa3ychTK3RLXdN16z84VIc4yBCWgJf GCgbydibIZ8OaeOFAebo5LgSfDXReu/g6R1AyceI4X4CvFLq8WNWCZpCn+CmmazCERNO /3qx9zqBlegqBpvM0ov0BDkGi1dQO9Y4YUdyeTi/b8V35KbbcxsWzIl3R1oLU71hVnTS LHcw== 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=7PdcGeaDubWlT5TZ1qZMhrpgxMZjui/XZA9OyE+pS0s=; b=z5LRzzDe/rTah7LP/vuRGBeCQxe3qIXMO3xd1VSkayzLpGpFmW0NsnlaAsAbeol6Su 4zWBoJmNIRZXvMR2ooRjZdpjq7Sk6C6HOwJg7vj0J/ht2NAUfFaXWW7YN2utJN+gOiUc 7GLtzekaC6eh2Wx40HXIYDkiec5C6rYmSsNvVcR0Lo1nYBsI9ho/I8dkRvgGXj+pfroX gy6TO3+bvJqYeHwwSk+92EY38on1NAizLoZl2yhjLlfLFHgC3JU5n1uPswrKVNnauPJM 0rEKjoNh6oCkZx9936MPA8o/EZaTcRwfO9z2ZF5OgT8Y0QiisyuusDgsCdEApQ41nbTy ixGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iY7e3K0B; 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 z10-20020a17090674ca00b0070b70788697si6863025ejl.160.2022.06.23.12.05.47; Thu, 23 Jun 2022 12:06:14 -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=iY7e3K0B; 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 S230373AbiFWRNk (ORCPT + 99 others); Thu, 23 Jun 2022 13:13:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231767AbiFWRLl (ORCPT ); Thu, 23 Jun 2022 13:11:41 -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 401F1FE9; Thu, 23 Jun 2022 09:50:56 -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 CC58B61FC2; Thu, 23 Jun 2022 16:50:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B5916C3411B; Thu, 23 Jun 2022 16:50:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656003055; bh=26K/+lq/Nl/U9R1SiSZW77RUyAOQ9pPzlVvMh+JeS9s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iY7e3K0BE4oQamEoomyq5MDrjt4MSVNIAiYAq0xSK6BZbnBFG+r1j4nKwAE5p9BGP N32u2sTusXmk6Wo/9A9PnTHN/SrWz4ckhl4LBg8gIXbuG04tpKb/2hKsnob7ihTKjY OGmYbJgGoktWlj74KU3RV9gzO8MZKmhc0604rJPw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sultan Alsawaf , Eric Biggers , Dominik Brodowski , "Jason A. Donenfeld" Subject: [PATCH 4.9 114/264] random: make credit_entropy_bits() always safe Date: Thu, 23 Jun 2022 18:41:47 +0200 Message-Id: <20220623164347.294617279@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164344.053938039@linuxfoundation.org> References: <20220623164344.053938039@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 a49c010e61e1938be851f5e49ac219d49b704103 upstream. This is called from various hwgenerator drivers, so rather than having one "safe" version for userspace and one "unsafe" version for the kernel, just make everything safe; the checks are cheap and sensible to have anyway. Reported-by: Sultan Alsawaf Reviewed-by: Eric Biggers Reviewed-by: Dominik Brodowski Signed-off-by: Jason A. Donenfeld Signed-off-by: Greg Kroah-Hartman --- drivers/char/random.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -448,18 +448,15 @@ static void process_random_ready_list(vo spin_unlock_irqrestore(&random_ready_list_lock, flags); } -/* - * Credit (or debit) the entropy store with n bits of entropy. - * Use credit_entropy_bits_safe() if the value comes from userspace - * or otherwise should be checked for extreme values. - */ static void credit_entropy_bits(int nbits) { int entropy_count, orig; - if (!nbits) + if (nbits <= 0) return; + nbits = min(nbits, POOL_BITS); + do { orig = READ_ONCE(input_pool.entropy_count); entropy_count = min(POOL_BITS, orig + nbits); @@ -471,18 +468,6 @@ static void credit_entropy_bits(int nbit crng_reseed(&primary_crng, true); } -static int credit_entropy_bits_safe(int nbits) -{ - if (nbits < 0) - return -EINVAL; - - /* Cap the value to avoid overflows */ - nbits = min(nbits, POOL_BITS); - - credit_entropy_bits(nbits); - return 0; -} - /********************************************************************* * * CRNG using CHACHA20 @@ -1577,7 +1562,10 @@ static long random_ioctl(struct file *f, return -EPERM; if (get_user(ent_count, p)) return -EFAULT; - return credit_entropy_bits_safe(ent_count); + if (ent_count < 0) + return -EINVAL; + credit_entropy_bits(ent_count); + return 0; case RNDADDENTROPY: if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -1590,7 +1578,8 @@ static long random_ioctl(struct file *f, retval = write_pool((const char __user *)p, size); if (retval < 0) return retval; - return credit_entropy_bits_safe(ent_count); + credit_entropy_bits(ent_count); + return 0; case RNDZAPENTCNT: case RNDCLEARPOOL: /*