linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: linux-kernel@vger.kernel.org, linux-crypto@vger.kernel.org,
	netdev@vger.kernel.org, Alexander Graf <graf@amazon.com>,
	Jann Horn <jannh@google.com>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Theodore Ts'o <tytso@mit.edu>
Subject: Re: [PATCH 2/3] random: provide notifier for VM fork
Date: Wed, 2 Mar 2022 09:53:34 +0100	[thread overview]
Message-ID: <Yh8wjrf7HVf56Anw@kroah.com> (raw)
In-Reply-To: <20220301231038.530897-3-Jason@zx2c4.com>

On Wed, Mar 02, 2022 at 12:10:37AM +0100, Jason A. Donenfeld wrote:
> Drivers such as WireGuard need to learn when VMs fork in order to clear
> sessions. This commit provides a simple notifier_block for that, with a
> register and unregister function. When no VM fork detection is compiled
> in, this turns into a no-op, similar to how the power notifier works.
> 
> Cc: Dominik Brodowski <linux@dominikbrodowski.net>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Cc: Theodore Ts'o <tytso@mit.edu>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
>  drivers/char/random.c  | 15 +++++++++++++++
>  include/linux/random.h |  5 +++++
>  2 files changed, 20 insertions(+)
> 
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 6bd1bbab7392..483fd2dc2057 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -1141,6 +1141,8 @@ void add_bootloader_randomness(const void *buf, size_t size)
>  EXPORT_SYMBOL_GPL(add_bootloader_randomness);
>  
>  #if IS_ENABLED(CONFIG_VMGENID)
> +static BLOCKING_NOTIFIER_HEAD(vmfork_notifier);
> +
>  /*
>   * Handle a new unique VM ID, which is unique, not secret, so we
>   * don't credit it, but we do immediately force a reseed after so
> @@ -1152,11 +1154,24 @@ void add_vmfork_randomness(const void *unique_vm_id, size_t size)
>  	if (crng_ready()) {
>  		crng_reseed(true);
>  		pr_notice("crng reseeded due to virtual machine fork\n");
> +		blocking_notifier_call_chain(&vmfork_notifier, 0, NULL);
>  	}
>  }
>  #if IS_MODULE(CONFIG_VMGENID)
>  EXPORT_SYMBOL_GPL(add_vmfork_randomness);
>  #endif
> +
> +int register_random_vmfork_notifier(struct notifier_block *nb)
> +{
> +	return blocking_notifier_chain_register(&vmfork_notifier, nb);
> +}
> +EXPORT_SYMBOL_GPL(register_random_vmfork_notifier);
> +
> +int unregister_random_vmfork_notifier(struct notifier_block *nb)
> +{
> +	return blocking_notifier_chain_unregister(&vmfork_notifier, nb);
> +}
> +EXPORT_SYMBOL_GPL(unregister_random_vmfork_notifier);
>  #endif
>  
>  struct fast_pool {
> diff --git a/include/linux/random.h b/include/linux/random.h
> index e84b6fa27435..7fccbc7e5a75 100644
> --- a/include/linux/random.h
> +++ b/include/linux/random.h
> @@ -31,6 +31,11 @@ extern void add_hwgenerator_randomness(const void *buffer, size_t count,
>  				       size_t entropy);
>  #if IS_ENABLED(CONFIG_VMGENID)
>  extern void add_vmfork_randomness(const void *unique_vm_id, size_t size);
> +extern int register_random_vmfork_notifier(struct notifier_block *nb);
> +extern int unregister_random_vmfork_notifier(struct notifier_block *nb);
> +#else
> +static inline int register_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
> +static inline int unregister_random_vmfork_notifier(struct notifier_block *nb) { return 0; }
>  #endif
>  
>  extern void get_random_bytes(void *buf, size_t nbytes);
> -- 
> 2.35.1
> 

It seems crazy that the "we just were spawned as a new vm" notifier is
based in the random driver, but sure, put it here for now!  :)

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

  reply	other threads:[~2022-03-02  8:53 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-01 23:10 [PATCH 0/3] random: wire up in-kernel virtual machine fork notifications Jason A. Donenfeld
2022-03-01 23:10 ` [PATCH 1/3] random: replace custom notifier chain with standard one Jason A. Donenfeld
2022-03-02  5:33   ` Dominik Brodowski
2022-03-02 11:42     ` Jason A. Donenfeld
2022-03-02 14:53       ` Dominik Brodowski
2022-03-01 23:10 ` [PATCH 2/3] random: provide notifier for VM fork Jason A. Donenfeld
2022-03-02  8:53   ` Greg KH [this message]
2022-03-02 11:41     ` Jason A. Donenfeld
2022-03-01 23:10 ` [PATCH 3/3] wireguard: device: clear keys on " Jason A. Donenfeld
2022-03-01 23:25   ` Jakub Kicinski
2022-03-02  8:36   ` Michael S. Tsirkin
2022-03-02 11:44     ` Jason A. Donenfeld
2022-03-02 13:06       ` Michael S. Tsirkin
2022-03-13  1:07   ` [PATCH v2] " Jason A. Donenfeld

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Yh8wjrf7HVf56Anw@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=Jason@zx2c4.com \
    --cc=graf@amazon.com \
    --cc=jannh@google.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=netdev@vger.kernel.org \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).