Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp6143531iog; Thu, 23 Jun 2022 12:14:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tvBaikFkVRvNloAKQJUTf2DPE80L7GNQ/kmdQtCSDbEvrp3Uby6ikib5/1HRplN9UHZ01t X-Received: by 2002:a17:902:ab8a:b0:16a:4521:a7b7 with SMTP id f10-20020a170902ab8a00b0016a4521a7b7mr10597032plr.57.1656011697751; Thu, 23 Jun 2022 12:14:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656011697; cv=none; d=google.com; s=arc-20160816; b=jUAQQDdSTzevSmwklhZzLxA5u3FOk51pidIOsnNvvm1QenD/IKRzzgfZlkYNtZTNqm fQrqBl7PB2TVr4J8HN0nrmMFIqkz2MLnIbOOwag5Evi6UKVD11Hn8X3sYPb3d4v1T/mI puwp5Qe4TscmTbckyQPbuGjmCob07YNc6qru0ecRHTElhaSRoMkwxGUEvb0sGP80XECc TK2O9uzr/bBYrqiLGcm2h0wTWG9bnyM5LLnsmTb91qQdZOdhnasjhpS0jMUSPlT91Olv ABaZfyGVckxfiyE0ihtnl7j+ea+XZn8B+yhcUlDVU0roJJZfWuyUdwmY3TrKjXktFXgC SGGw== 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=0CqpeDmTSykRXC40Cnv/28DFDKXHdcsq+7ZRPReaKrQ=; b=XDKZCCnGHwiThDYx0MKuGDj9q7SNNWRti1p9dUePycopZzvZLSLMJ25tWeljqQRlja MF4GkEy/saj4I3VX2sNMHxuhBDFZ4b6PRg5t0algByMsW5h5JtxKGquISjIfstmezJp4 jWAQeaWrNjuCFnT3XkjkDrDLlSU6gfjlqpLwYijRdhmu9flogppmmsi5ZDtAbw6eTSnn s10wxWSqVQKmcRUev6FnjSMjG63O8/cIJKUERw57rCoJnBgF7akVeLz9FsxEJ3BztsEF CKWS0buZixsNcfBELSB9vNcGPiAdI1Y1Q/4VwYiOIeW3C9fnVp9vEArivBO/AQp6bJIm Y2yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=eA94Iyy3; 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 g205-20020a6252d6000000b005252711f6b6si14023124pfb.336.2022.06.23.12.14.46; Thu, 23 Jun 2022 12:14: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=eA94Iyy3; 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 S235833AbiFWSBv (ORCPT + 99 others); Thu, 23 Jun 2022 14:01:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235567AbiFWSAd (ORCPT ); Thu, 23 Jun 2022 14:00:33 -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 CC0DCB2CF3; Thu, 23 Jun 2022 10:16:31 -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 C008461DED; Thu, 23 Jun 2022 17:16:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9596DC3411B; Thu, 23 Jun 2022 17:16:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1656004590; bh=vTKQqfjX4F70+FXW6akEB42FGx0gbfSWX/LAI81xnYI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eA94Iyy324iLq3q7pu8oeCEhOTS7MhQc0V79TXLCqn0gOJxbHKZ4Hvx6OAykPff1G Jw/Qv67H5MPhfbe+Fvy6V3zAfi4ltM0IXsEcVQLO9lGRYr+13nif5lCmkAu/9GZyEl wPIjWaEkk3JPOcbdN1dvd/QHRi1DoV0WuBfWEhCw= 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.19 079/234] random: make credit_entropy_bits() always safe Date: Thu, 23 Jun 2022 18:42:26 +0200 Message-Id: <20220623164345.293829763@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220623164343.042598055@linuxfoundation.org> References: <20220623164343.042598055@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 @@ -447,18 +447,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); @@ -470,18 +467,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 @@ -1526,7 +1511,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; @@ -1539,7 +1527,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: /*