linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
To: linux-kernel@vger.kernel.org
Cc: "Jason A. Donenfeld" <Jason@zx2c4.com>,
	Dominik Brodowski <linux@dominikbrodowski.net>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Theodore Ts'o <tytso@mit.edu>, Alexander Graf <graf@amazon.com>,
	Adrian Catangiu <adrian@parity.io>, Jann Horn <jannh@google.com>
Subject: [PATCH RFC v1] random: add vm_id sysctl with polling
Date: Wed,  2 Mar 2022 15:33:31 +0100	[thread overview]
Message-ID: <20220302143331.654426-1-Jason@zx2c4.com> (raw)

This is an RFC, because I'm in no rush to add new userspace APIs without
being quite sure that this is the way forward.

In order to inform userspace of virtual machine forks, this commit adds
a "vm_id" sysctl, with the same semantics as the existing "boot_id" one,
except it changes after a VM fork and allows userspace processes to
poll() on it for changes.

It avoids exposing the actual vmgenid from the hypervisor to userspace,
in case there is any randomness value in keeping it secret.

The main purpose of posting this patch is just to have something
concrete to demonstrate the idea, as well as to show how simple this is
to implement. The discussion of races and such is for the thread in the
link below.

Link: https://lore.kernel.org/lkml/Yh4+9+UpanJWAIyZ@zx2c4.com/
Cc: Dominik Brodowski <linux@dominikbrodowski.net>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Theodore Ts'o <tytso@mit.edu>
Cc: Alexander Graf <graf@amazon.com>
Cc: Adrian Catangiu <adrian@parity.io>
Cc: Jann Horn <jannh@google.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 drivers/char/random.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/char/random.c b/drivers/char/random.c
index 07ca3164522c..942a6dc4f00b 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -1142,6 +1142,8 @@ EXPORT_SYMBOL_GPL(add_bootloader_randomness);
 
 #if IS_ENABLED(CONFIG_VMGENID)
 static BLOCKING_NOTIFIER_HEAD(vmfork_chain);
+static DEFINE_CTL_TABLE_POLL(sysctl_vmid_poll);
+static u8 sysctl_vmid[UUID_SIZE];
 
 /*
  * Handle a new unique VM ID, which is unique, not secret, so we
@@ -1156,6 +1158,10 @@ void add_vmfork_randomness(const void *unique_vm_id, size_t size)
 		pr_notice("crng reseeded due to virtual machine fork\n");
 	}
 	blocking_notifier_call_chain(&vmfork_chain, 0, NULL);
+	if (IS_ENABLED(CONFIG_SYSCTL)) {
+		WRITE_ONCE(sysctl_vmid[8], 0);
+		proc_sys_poll_notify(&sysctl_vmid_poll);
+	}
 }
 #if IS_MODULE(CONFIG_VMGENID)
 EXPORT_SYMBOL_GPL(add_vmfork_randomness);
@@ -1684,6 +1690,15 @@ static struct ctl_table random_table[] = {
 		.mode		= 0444,
 		.proc_handler	= proc_do_uuid,
 	},
+#if IS_ENABLED(CONFIG_VMGENID)
+	{
+		.procname	= "vm_id",
+		.data		= &sysctl_vmid,
+		.mode		= 0444,
+		.proc_handler	= proc_do_uuid,
+		.poll		= &sysctl_vmid_poll,
+	},
+#endif
 	{
 		.procname	= "uuid",
 		.mode		= 0444,
-- 
2.35.1


             reply	other threads:[~2022-03-02 14:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-02 14:33 Jason A. Donenfeld [this message]
2022-03-09 21:59 ` [PATCH RFC v2] random: add vm_id sysctl for polling 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=20220302143331.654426-1-Jason@zx2c4.com \
    --to=jason@zx2c4.com \
    --cc=adrian@parity.io \
    --cc=graf@amazon.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --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).