random: add vm_id sysctl with polling
Subsystems (2)
Jason A. Donenfeld
maintainerRFC PATCH 1/1 random: add vm_id sysctl with polling
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.
--- drivers/char/random.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/char/random.c +15 -0
@@ -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,
Signature · 1 line
2.35.1
Jason A. Donenfeld
maintainerRFC PATCH v2 1/1 random: add vm_id sysctl for polling
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, which does not return any data, but allows userspace processes to poll() on it for notification of VM forks.
It avoids exposing the actual vmgenid from the hypervisor to userspace, in case there is any randomness value in keeping it secret. Rather, userspace is expected to simply use getrandom() if it wants a fresh value.
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.
---
Changes v1->v2:
- No need to return any actual data in the file.
Documentation/admin-guide/sysctl/kernel.rst | 3 ++- drivers/char/random.c | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-)
Documentation/admin-guide/sysctl/kernel.rst +2 -1
@@ -1039,8 +1039,9 @@ This is a directory, with the following entries: * ``write_wakeup_threshold``: when the entropy count drops below this (as a number of bits), processes waiting to write to ``/dev/random`` are woken up. This file is writable for compatibility purposes, but - writing to it has no effect on any RNG behavior. + writing to it has no effect on any RNG behavior; +* ``vm_id``: unreadable, but can be poll()'d on for VM forks. randomize_va_space ==================
drivers/char/random.c +21 -0
@@ -1163,6 +1163,7 @@ 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);/* * Handle a new unique VM ID, which is unique, not secret, so we
@@ -1177,6 +1178,8 @@ 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 )) +proc_sys_poll_notify (&sysctl_vmid_poll); }#if IS_MODULE (CONFIG_VMGENID )EXPORT_SYMBOL_GPL (add_vmfork_randomness);
@@ -1625,6 +1628,8 @@ const struct file_operations random_fops = {* It is writable to avoid breaking old userspaces, but writing * to it does not change any behavior of the RNG. * +* - vm_id - an unreadable file that can be poll()'d on for VM forks. +* ********************************************************************/ #ifdef CONFIG_SYSCTL
@@ -1678,6 +1683,14 @@ static int proc_do_rointvec(struct ctl_table *table, int write, void *buffer,return write ?0 :proc_dointvec (table,0 , buffer, lenp, ppos); } +#if IS_ENABLED (CONFIG_VMGENID ) +static int proc_do_nodata (struct ctl_table *table,int write,void *buffer, +size_t *lenp,loff_t *ppos) +{ +return -ENODATA ; +} +#endif +static struct ctl_table random_table[] = { { .procname ="poolsize" ,
@@ -1718,6 +1731,14 @@ static struct ctl_table random_table[] = { .mode = 0444, .proc_handler = proc_do_uuid, }, +#if IS_ENABLED (CONFIG_VMGENID ) + { + .procname = "vm_id", + .mode = 0444, + .poll = &sysctl_vmid_poll, + .proc_handler = proc_do_nodata, + }, +#endif { } };
Signature · 1 line
2.35.1