2022-04-22 22:13:13

by Jann Horn

[permalink] [raw]
Subject: Re: [PATCH] random: add fork_event sysctl for polling VM forks

On Tue, Apr 19, 2022 at 6:04 PM Jason A. Donenfeld <[email protected]> wrote:
> In order to inform userspace of virtual machine forks, this commit adds
> a "fork_event" 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.
>
> For example, the following snippet can be used to print a message every
> time a VM forks, after the RNG has been reseeded:
>
> struct pollfd fd = { .fd = open("/proc/sys/kernel/random/fork_event", O_RDONLY) };
> assert(fd.fd >= 0);
> for (;;) {
> assert(poll(&fd, 1, -1) > 0);
> puts("vm fork detected");
> }

This is a bit of a weird API, because normally .poll is supposed to be
level-triggered rather than edge-triggered... and AFAIK things like
epoll also kinda assume that ->poll() doesn't modify state (but that
only _really_ matters in weird cases). But at the same time, it looks
like the existing proc_sys_poll() already goes against that? So I
don't know what the right thing to do there is...