Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Babis Chalios

On 2/11/23 12:51, Michael S. Tsirkin wrote:


On Thu, Nov 02, 2023 at 12:38:28PM +0100, Babis Chalios wrote:

On 2/11/23 12:20, Michael S. Tsirkin wrote:

On Thu, Sep 28, 2023 at 08:16:11PM +0200, Babis Chalios wrote:

On 27/9/23 23:47, Michael S. Tsirkin wrote:

On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:

On 22/9/23 18:01, Michael S. Tsirkin wrote:

On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:

On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
 <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis

Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.

That sounds like a nice mechanism. However in our case the page
holding the counter that gets increased by the hardware is a kernel
page.

The reason for that is that things other than us (virtio-rng) might
want to notify for leak events. For example, I think that Jason
intended to use this mechanism to periodically notify user-space
PRNGs that they need to reseed.

Cheers,
Babis

Now I'm lost.
when you write, e.g.:
4. vcpu 0: get getrandom() entropy and cache epoch value
how does vcpu access the epoch?

The kernel provides a user space API to map a pointer to the epoch
value. User space then caches its value and checks it every time it
needs to make sure that no entropy leak has happened before using
cached kernel entropy.

virtio-rng driver adds a copy on leak command to the queue for
increasing this value (that's what we are 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Michael S. Tsirkin
On Thu, Nov 02, 2023 at 12:38:28PM +0100, Babis Chalios wrote:
> 
> On 2/11/23 12:20, Michael S. Tsirkin wrote:
> > 
> > On Thu, Sep 28, 2023 at 08:16:11PM +0200, Babis Chalios wrote:
> > > 
> > > On 27/9/23 23:47, Michael S. Tsirkin wrote:
> > > > On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:
> > > > > On 22/9/23 18:01, Michael S. Tsirkin wrote:
> > > > > > On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> > > > > > > On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > > > > > > > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > > > > > > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > > > > > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios 
> > > > > > > > > > wrote:
> > > > > > > > > > > Resending to fix e-mail formatting issues (sorry for the 
> > > > > > > > > > > spam)
> > > > > > > > > > > 
> > > > > > > > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > > > > > > > Yes, that's what the driver does now in the RFC 
> > > > > > > > > > > > > > > > patch.
> > > > > > > > > > > > > > > > However, this just
> > > > > > > > > > > > > > > > decreases
> > > > > > > > > > > > > > > > the race window, it doesn't eliminate it. If a 
> > > > > > > > > > > > > > > > third
> > > > > > > > > > > > > > > > leak event happens it
> > > > > > > > > > > > > > > > might not
> > > > > > > > > > > > > > > > find any buffers to use:
> > > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > > 3. poll queue X
> > > > > > > > > > > > > > > > 4. used buffers in queue X   <- leak event 
> > > > > > > > > > > > > > > > 1 will
> > > > > > > > > > > > > > > > use buffers in X
> > > > > > > > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > > > > > > > 6. poll queue 1-X<- leak event 
> > > > > > > > > > > > > > > > 2 will
> > > > > > > > > > > > > > > > use buffers in 1-X
> > > > > > > > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > > > > > > > > <- leak 
> > > > > > > > > > > > > > > > event 3 (it
> > > > > > > > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > > > > > > > 9. goto 3
> > > > > > > > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > > > > > > > What if the leak event 3 arrives before step 5 had 
> > > > > > > > > > > > > > time to
> > > > > > > > > > > > > > actually add the
> > > > > > > > > > > > > > buffers in X and make
> > > > > > > > > > > > > > them visible to the device?
> > > > > > > > > > > > > Then it will see a single event in 1-X instead of two 
> > > > > > > > > > > > > events.  A leak is
> > > > > > > > > > > > > a leak though, I don't see does it matter how many 
> > > > > > > > > > > > > triggered.
> > > > > > > > > > > > > 
> > > > > > > > > > > So the scenario I have in mind is the following:
> > > > > > > > > > > 
> > > > > > > > > > > (Epoch here is terminology that I used in the Linux RFC. 
> > > > > > > > > > > It is a value
> > > > > > > > > > > maintained by random.c
> > > > > > > > > > > that changes every time a leak event happens).
> > > > > > > > > > > 
> > > > > > > > > > > 1. add buffers to 1-X
> > > > > > > > > > > 2. add buffers to X
> > > > > > > > > > > 3. poll queue X
> > > > > > > > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > > > > > > > 5. Device: First snapshot, uses buffers in X
> > > > > > > > > > > 6. vcpu 1: sees used buffers
> > > > > > > > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > > > > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches 
> > > > > > > > > > > it
> > > > > > > > > > > 9. Device: Third snapshot, no buffers in either queue, 
> > > > > > > > > > > (vcpu 1 from step 6
> > > > > > > > > > > has not yet finished adding new buffers).
> > > > > > > > > > > 10. vcpu 1 adds new buffer in X
> > > > > > > > > > > 11. vcpu 0: getrandom() will not see new epoch and gets 
> > > > > > > > > > > stale entropy.
> > > > > > > > > > > 
> > > > > > > > > > > 
> > > > > > > > > > > In this succession of events, when the third snapshot 
> > > > > > > > > > > will happen, the
> > > > > > > > > > > device won't find
> > > > > > > > > > > any buffers in either queue, so it won't increase the RNG 
> > > > > > > > > > > epoch value. So,
> > > > > > > > > > > any entropy
> > > > > > > > > > > gathered after step 8 will be the same across all 
> > > > > > > > > > > snapshots. Am I missing
> > > > > > > > > > > something?
> > > > > > > > > > > 
> > > > > > > > > > > Cheers,
> > > > > > > > > > > Babis
> > > > > > > > > > > 
> > > > > > > > > > Yes but notice how this is followed by:
> > > > > > > > > > 
> > > > > > > > > > 12. vcpu 1: sees used 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Babis Chalios




On 2/11/23 12:25, Michael S. Tsirkin wrote:



On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:


On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
  <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.

OK. Basically I think if there's a way for device to detect that
guest is stuck and not refilling the queue in a timely
manner, then we are ok - host will make its own decisions
on whether to snapshot or not.

However, I feel in that case we need a way to create a big
backlog of buffers for guest to fill such that this
ring empty condition is very unlikely.
One or even 2 queues does not seem enough then.

For example, I can see a "stop" command that will
tell device: "stop consuming buffers" and device
will stop consuming buffers until the next leak event.


Yup, that seems reasonable to me. In that case, we could
have a single queue, where the driver will fill up with multiple
batches of commands where the last one is "stop" command, and
then back-fill as needed.

That should make it very unlikely for a well-behaving guest to
run out of entropy leak commands in the queue. However, even
with that design, I think there is value in putting something in the
spec about the fact that the host might want to consider if it should,
or not, take a snapshot when it finds the leak queue empty.

Cheers,
Babis





I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis



-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Babis Chalios



On 2/11/23 12:20, Michael S. Tsirkin wrote:


On Thu, Sep 28, 2023 at 08:16:11PM +0200, Babis Chalios wrote:


On 27/9/23 23:47, Michael S. Tsirkin wrote:

On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:

On 22/9/23 18:01, Michael S. Tsirkin wrote:

On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:

On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
<- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis

Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.

That sounds like a nice mechanism. However in our case the page
holding the counter that gets increased by the hardware is a kernel
page.

The reason for that is that things other than us (virtio-rng) might
want to notify for leak events. For example, I think that Jason
intended to use this mechanism to periodically notify user-space
PRNGs that they need to reseed.

Cheers,
Babis

Now I'm lost.
when you write, e.g.:
4. vcpu 0: get getrandom() entropy and cache epoch value
how does vcpu access the epoch?

The kernel provides a user space API to map a pointer to the epoch
value. User space then caches its value and checks it every time it
needs to make sure that no entropy leak has happened before using
cached kernel entropy.

virtio-rng driver adds a copy on leak command to the queue for
increasing this value (that's what we are speaking about in this thread).
But other systems might want to report "leaks", such as random.c
itself.


Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Michael S. Tsirkin
On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> 
> 
> On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > > > Resending to fix e-mail formatting issues (sorry for the spam)
> > > > > 
> > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > Yes, that's what the driver does now in the RFC patch.
> > > > > > > > > > However, this just
> > > > > > > > > > decreases
> > > > > > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > > > > > leak event happens it
> > > > > > > > > > might not
> > > > > > > > > > find any buffers to use:
> > > > > > > > > > 
> > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 3. poll queue X
> > > > > > > > > > 4. used buffers in queue X   <- leak event 1 will
> > > > > > > > > > use buffers in X
> > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > 6. poll queue 1-X<- leak event 2 will
> > > > > > > > > > use buffers in 1-X
> > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > >  <- leak event 3 (it
> > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > 9. goto 3
> > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > What if the leak event 3 arrives before step 5 had time to
> > > > > > > > actually add the
> > > > > > > > buffers in X and make
> > > > > > > > them visible to the device?
> > > > > > > Then it will see a single event in 1-X instead of two events.  A 
> > > > > > > leak is
> > > > > > > a leak though, I don't see does it matter how many triggered.
> > > > > > > 
> > > > > So the scenario I have in mind is the following:
> > > > > 
> > > > > (Epoch here is terminology that I used in the Linux RFC. It is a value
> > > > > maintained by random.c
> > > > > that changes every time a leak event happens).
> > > > > 
> > > > > 1. add buffers to 1-X
> > > > > 2. add buffers to X
> > > > > 3. poll queue X
> > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > 5. Device: First snapshot, uses buffers in X
> > > > > 6. vcpu 1: sees used buffers
> > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > > > 9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from 
> > > > > step 6
> > > > > has not yet finished adding new buffers).
> > > > > 10. vcpu 1 adds new buffer in X
> > > > > 11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.
> > > > > 
> > > > > 
> > > > > In this succession of events, when the third snapshot will happen, the
> > > > > device won't find
> > > > > any buffers in either queue, so it won't increase the RNG epoch 
> > > > > value. So,
> > > > > any entropy
> > > > > gathered after step 8 will be the same across all snapshots. Am I 
> > > > > missing
> > > > > something?
> > > > > 
> > > > > Cheers,
> > > > > Babis
> > > > > 
> > > > Yes but notice how this is followed by:
> > > > 
> > > > 12. vcpu 1: sees used buffers in 1-X
> > > > 
> > > > Driver can notify getrandom I guess?
> > > It could, but then we have the exact race condition that VMGENID had,
> > > userspace has already consumed stale entropy and there's nothing we
> > > can do about that.
> > > 
> > > Although this is indeed a corner case, it feels like it beats the purpose
> > > of having the hardware update directly userspace (via copy on leak).
> > > 
> > > How do you feel about the proposal a couple of emails back? It looks to
> > > me that it avoids completely the race condition.
> > > 
> > > Cheers,
> > > Babis
> > It does. The problem of course is that this means that e.g.
> > taking a snapshot of a guest that is stuck won't work well.
> 
> That is true, but does it matter? The intention of the proposal
> is that if it is not safe to take snapshots (i.e. no buffers in the
> queue) don't take snapshots.

OK. Basically I think if there's a way for device to detect that
guest is stuck and not refilling the queue in a timely
manner, then we are ok - host will make its own decisions
on whether to snapshot or not.

However, I feel in that case we need a way to create a big
backlog of buffers for guest to fill such that this
ring empty condition is very unlikely.
One or even 2 queues does not seem enough then.

For example, I can see a "stop" command that will
tell device: "stop consuming buffers" and device
will stop consuming buffers until the next leak event.




> > I have been thinking of adding MAP/UNMAP descriptors for
> > a while now. Thus it will be possible to modify
> > userspace memory without consuming buffers.
> > Would something like 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-11-02 Thread Michael S. Tsirkin
On Thu, Sep 28, 2023 at 08:16:11PM +0200, Babis Chalios wrote:
> 
> 
> On 27/9/23 23:47, Michael S. Tsirkin wrote:
> > On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:
> > > On 22/9/23 18:01, Michael S. Tsirkin wrote:
> > > > On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> > > > > On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > > > > > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > > > > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > > > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > > > > > > > Resending to fix e-mail formatting issues (sorry for the spam)
> > > > > > > > > 
> > > > > > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > > > > > Yes, that's what the driver does now in the RFC 
> > > > > > > > > > > > > > patch.
> > > > > > > > > > > > > > However, this just
> > > > > > > > > > > > > > decreases
> > > > > > > > > > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > > > > > > > > > leak event happens it
> > > > > > > > > > > > > > might not
> > > > > > > > > > > > > > find any buffers to use:
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > 
> > > > > > > > > > > > > > 3. poll queue X
> > > > > > > > > > > > > > 4. used buffers in queue X   <- leak event 1 
> > > > > > > > > > > > > > will
> > > > > > > > > > > > > > use buffers in X
> > > > > > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > > > > > 6. poll queue 1-X<- leak event 2 
> > > > > > > > > > > > > > will
> > > > > > > > > > > > > > use buffers in 1-X
> > > > > > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > > > > > ><- leak 
> > > > > > > > > > > > > > event 3 (it
> > > > > > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > > > > > 9. goto 3
> > > > > > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > > > > > What if the leak event 3 arrives before step 5 had time 
> > > > > > > > > > > > to
> > > > > > > > > > > > actually add the
> > > > > > > > > > > > buffers in X and make
> > > > > > > > > > > > them visible to the device?
> > > > > > > > > > > Then it will see a single event in 1-X instead of two 
> > > > > > > > > > > events.  A leak is
> > > > > > > > > > > a leak though, I don't see does it matter how many 
> > > > > > > > > > > triggered.
> > > > > > > > > > > 
> > > > > > > > > So the scenario I have in mind is the following:
> > > > > > > > > 
> > > > > > > > > (Epoch here is terminology that I used in the Linux RFC. It 
> > > > > > > > > is a value
> > > > > > > > > maintained by random.c
> > > > > > > > > that changes every time a leak event happens).
> > > > > > > > > 
> > > > > > > > > 1. add buffers to 1-X
> > > > > > > > > 2. add buffers to X
> > > > > > > > > 3. poll queue X
> > > > > > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > > > > > 5. Device: First snapshot, uses buffers in X
> > > > > > > > > 6. vcpu 1: sees used buffers
> > > > > > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > > > > > > > 9. Device: Third snapshot, no buffers in either queue, (vcpu 
> > > > > > > > > 1 from step 6
> > > > > > > > > has not yet finished adding new buffers).
> > > > > > > > > 10. vcpu 1 adds new buffer in X
> > > > > > > > > 11. vcpu 0: getrandom() will not see new epoch and gets stale 
> > > > > > > > > entropy.
> > > > > > > > > 
> > > > > > > > > 
> > > > > > > > > In this succession of events, when the third snapshot will 
> > > > > > > > > happen, the
> > > > > > > > > device won't find
> > > > > > > > > any buffers in either queue, so it won't increase the RNG 
> > > > > > > > > epoch value. So,
> > > > > > > > > any entropy
> > > > > > > > > gathered after step 8 will be the same across all snapshots. 
> > > > > > > > > Am I missing
> > > > > > > > > something?
> > > > > > > > > 
> > > > > > > > > Cheers,
> > > > > > > > > Babis
> > > > > > > > > 
> > > > > > > > Yes but notice how this is followed by:
> > > > > > > > 
> > > > > > > > 12. vcpu 1: sees used buffers in 1-X
> > > > > > > > 
> > > > > > > > Driver can notify getrandom I guess?
> > > > > > > It could, but then we have the exact race condition that VMGENID 
> > > > > > > had,
> > > > > > > userspace has already consumed stale entropy and there's nothing 
> > > > > > > we
> > > > > > > can do about that.
> > > > > > > 
> > > > > > > Although this is indeed a corner case, it feels like it beats the 
> > > > > > > purpose
> > > > > > > of having the hardware update directly userspace (via copy on 
> > > > > > > leak).
> > > > > > > 
> > > > > > > How do you feel 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-10-13 Thread Michael S. Tsirkin
On Fri, Oct 13, 2023 at 09:49:40AM +0200, Babis Chalios wrote:
> 
> 
> On 28/9/23 20:16, Babis Chalios wrote:
> > 
> > 
> > On 27/9/23 23:47, Michael S. Tsirkin wrote:
> > > On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:
> > > > On 22/9/23 18:01, Michael S. Tsirkin wrote:
> > > > > On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> > > > > > On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > > > > > > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > > > > > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > > > > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > > > > > > > > Resending to fix e-mail formatting issues (sorry for the 
> > > > > > > > > > spam)
> > > > > > > > > > 
> > > > > > > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > > > > > > Yes, that's what the driver does now in the RFC 
> > > > > > > > > > > > > > > patch.
> > > > > > > > > > > > > > > However, this just
> > > > > > > > > > > > > > > decreases
> > > > > > > > > > > > > > > the race window, it doesn't eliminate it. If a 
> > > > > > > > > > > > > > > third
> > > > > > > > > > > > > > > leak event happens it
> > > > > > > > > > > > > > > might not
> > > > > > > > > > > > > > > find any buffers to use:
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > 
> > > > > > > > > > > > > > > 3. poll queue X
> > > > > > > > > > > > > > > 4. used buffers in queue X   <- leak event 1 
> > > > > > > > > > > > > > > will
> > > > > > > > > > > > > > > use buffers in X
> > > > > > > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > > > > > > 6. poll queue 1-X    <- leak event 2 
> > > > > > > > > > > > > > > will
> > > > > > > > > > > > > > > use buffers in 1-X
> > > > > > > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > > > > > > > <- leak event 3 (it
> > > > > > > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > > > > > > 9. goto 3
> > > > > > > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > > > > > > What if the leak event 3 arrives before step 5 had 
> > > > > > > > > > > > > time to
> > > > > > > > > > > > > actually add the
> > > > > > > > > > > > > buffers in X and make
> > > > > > > > > > > > > them visible to the device?
> > > > > > > > > > > > Then it will see a single event
> > > > > > > > > > > > in 1-X instead of two events.  A
> > > > > > > > > > > > leak is
> > > > > > > > > > > > a leak though, I don't see does it matter how many 
> > > > > > > > > > > > triggered.
> > > > > > > > > > > > 
> > > > > > > > > > So the scenario I have in mind is the following:
> > > > > > > > > > 
> > > > > > > > > > (Epoch here is terminology that I used
> > > > > > > > > > in the Linux RFC. It is a value
> > > > > > > > > > maintained by random.c
> > > > > > > > > > that changes every time a leak event happens).
> > > > > > > > > > 
> > > > > > > > > > 1. add buffers to 1-X
> > > > > > > > > > 2. add buffers to X
> > > > > > > > > > 3. poll queue X
> > > > > > > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > > > > > > 5. Device: First snapshot, uses buffers in X
> > > > > > > > > > 6. vcpu 1: sees used buffers
> > > > > > > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > > > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > > > > > > > > 9. Device: Third snapshot, no buffers in
> > > > > > > > > > either queue, (vcpu 1 from step 6
> > > > > > > > > > has not yet finished adding new buffers).
> > > > > > > > > > 10. vcpu 1 adds new buffer in X
> > > > > > > > > > 11. vcpu 0: getrandom() will not see new
> > > > > > > > > > epoch and gets stale entropy.
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > In this succession of events, when the
> > > > > > > > > > third snapshot will happen, the
> > > > > > > > > > device won't find
> > > > > > > > > > any buffers in either queue, so it won't
> > > > > > > > > > increase the RNG epoch value. So,
> > > > > > > > > > any entropy
> > > > > > > > > > gathered after step 8 will be the same
> > > > > > > > > > across all snapshots. Am I missing
> > > > > > > > > > something?
> > > > > > > > > > 
> > > > > > > > > > Cheers,
> > > > > > > > > > Babis
> > > > > > > > > > 
> > > > > > > > > Yes but notice how this is followed by:
> > > > > > > > > 
> > > > > > > > > 12. vcpu 1: sees used buffers in 1-X
> > > > > > > > > 
> > > > > > > > > Driver can notify getrandom I guess?
> > > > > > > > It could, but then we have the exact race
> > > > > > > > condition that VMGENID had,
> > > > > > > > userspace has already consumed stale entropy and there's 
> > > > > > > > nothing we
> > > > > > > > can do about that.
> > > > > > > > 
> > > 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-10-13 Thread Babis Chalios




On 28/9/23 20:16, Babis Chalios wrote:



On 27/9/23 23:47, Michael S. Tsirkin wrote:

On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:

On 22/9/23 18:01, Michael S. Tsirkin wrote:

On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:

On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X    <- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
<- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?
Then it will see a single event in 1-X instead of two 
events.  A leak is

a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is 
a value

maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 
from step 6

has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale 
entropy.



In this succession of events, when the third snapshot will 
happen, the

device won't find
any buffers in either queue, so it won't increase the RNG 
epoch value. So,

any entropy
gathered after step 8 will be the same across all snapshots. 
Am I missing

something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?
It could, but then we have the exact race condition that VMGENID 
had,

userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats 
the purpose
of having the hardware update directly userspace (via copy on 
leak).


How do you feel about the proposal a couple of emails back? It 
looks to

me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis

Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.

That sounds like a nice mechanism. However in our case the page
holding the counter that gets increased by the hardware is a kernel
page.

The reason for that is that things other than us (virtio-rng) might
want to notify for leak events. For example, I think that Jason
intended to use this mechanism to periodically notify user-space
PRNGs that they need to reseed.

Cheers,
Babis


Now I'm lost.
when you write, e.g.:
4. vcpu 0: get getrandom() entropy and cache epoch value
how does vcpu access the epoch?


The kernel provides a user space API to map a pointer to the epoch
value. User space then caches its value and checks it every time it
needs to make sure that no entropy leak has happened before using
cached kernel entropy.

virtio-rng driver adds a copy on leak command to the queue for
increasing this value (that's what we are speaking about in this thread).
But other systems might want to report "leaks", such as random.c
itself.

Cheers,
Babis


Hey Michael, does this explain the flow of things?

To summarize, 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-28 Thread Babis Chalios




On 27/9/23 23:47, Michael S. Tsirkin wrote:

On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:

On 22/9/23 18:01, Michael S. Tsirkin wrote:

On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:

On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
   <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis

Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.

That sounds like a nice mechanism. However in our case the page
holding the counter that gets increased by the hardware is a kernel
page.

The reason for that is that things other than us (virtio-rng) might
want to notify for leak events. For example, I think that Jason
intended to use this mechanism to periodically notify user-space
PRNGs that they need to reseed.

Cheers,
Babis


Now I'm lost.
when you write, e.g.:
4. vcpu 0: get getrandom() entropy and cache epoch value
how does vcpu access the epoch?


The kernel provides a user space API to map a pointer to the epoch
value. User space then caches its value and checks it every time it
needs to make sure that no entropy leak has happened before using
cached kernel entropy.

virtio-rng driver adds a copy on leak command to the queue for
increasing this value (that's what we are speaking about in this thread).
But other systems might want to report "leaks", such as random.c
itself.

Cheers,
Babis

-
To unsubscribe, e-mail: 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-27 Thread Michael S. Tsirkin
On Wed, Sep 27, 2023 at 12:43:20PM +0200, Babis Chalios wrote:
> On 22/9/23 18:01, Michael S. Tsirkin wrote:
> > On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> > > 
> > > On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > > > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > > > > > Resending to fix e-mail formatting issues (sorry for the spam)
> > > > > > > 
> > > > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > > > Yes, that's what the driver does now in the RFC patch.
> > > > > > > > > > > > However, this just
> > > > > > > > > > > > decreases
> > > > > > > > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > > > > > > > leak event happens it
> > > > > > > > > > > > might not
> > > > > > > > > > > > find any buffers to use:
> > > > > > > > > > > > 
> > > > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > > > 
> > > > > > > > > > > > 
> > > > > > > > > > > > 3. poll queue X
> > > > > > > > > > > > 4. used buffers in queue X   <- leak event 1 will
> > > > > > > > > > > > use buffers in X
> > > > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > > > 6. poll queue 1-X<- leak event 2 will
> > > > > > > > > > > > use buffers in 1-X
> > > > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > > > >   <- leak event 3 
> > > > > > > > > > > > (it
> > > > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > > > 9. goto 3
> > > > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > > > What if the leak event 3 arrives before step 5 had time to
> > > > > > > > > > actually add the
> > > > > > > > > > buffers in X and make
> > > > > > > > > > them visible to the device?
> > > > > > > > > Then it will see a single event in 1-X instead of two events. 
> > > > > > > > >  A leak is
> > > > > > > > > a leak though, I don't see does it matter how many triggered.
> > > > > > > > > 
> > > > > > > So the scenario I have in mind is the following:
> > > > > > > 
> > > > > > > (Epoch here is terminology that I used in the Linux RFC. It is a 
> > > > > > > value
> > > > > > > maintained by random.c
> > > > > > > that changes every time a leak event happens).
> > > > > > > 
> > > > > > > 1. add buffers to 1-X
> > > > > > > 2. add buffers to X
> > > > > > > 3. poll queue X
> > > > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > > > 5. Device: First snapshot, uses buffers in X
> > > > > > > 6. vcpu 1: sees used buffers
> > > > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > > > > > 9. Device: Third snapshot, no buffers in either queue, (vcpu 1 
> > > > > > > from step 6
> > > > > > > has not yet finished adding new buffers).
> > > > > > > 10. vcpu 1 adds new buffer in X
> > > > > > > 11. vcpu 0: getrandom() will not see new epoch and gets stale 
> > > > > > > entropy.
> > > > > > > 
> > > > > > > 
> > > > > > > In this succession of events, when the third snapshot will 
> > > > > > > happen, the
> > > > > > > device won't find
> > > > > > > any buffers in either queue, so it won't increase the RNG epoch 
> > > > > > > value. So,
> > > > > > > any entropy
> > > > > > > gathered after step 8 will be the same across all snapshots. Am I 
> > > > > > > missing
> > > > > > > something?
> > > > > > > 
> > > > > > > Cheers,
> > > > > > > Babis
> > > > > > > 
> > > > > > Yes but notice how this is followed by:
> > > > > > 
> > > > > > 12. vcpu 1: sees used buffers in 1-X
> > > > > > 
> > > > > > Driver can notify getrandom I guess?
> > > > > It could, but then we have the exact race condition that VMGENID had,
> > > > > userspace has already consumed stale entropy and there's nothing we
> > > > > can do about that.
> > > > > 
> > > > > Although this is indeed a corner case, it feels like it beats the 
> > > > > purpose
> > > > > of having the hardware update directly userspace (via copy on leak).
> > > > > 
> > > > > How do you feel about the proposal a couple of emails back? It looks 
> > > > > to
> > > > > me that it avoids completely the race condition.
> > > > > 
> > > > > Cheers,
> > > > > Babis
> > > > It does. The problem of course is that this means that e.g.
> > > > taking a snapshot of a guest that is stuck won't work well.
> > > That is true, but does it matter? The intention of the proposal
> > > is that if it is not safe to take snapshots (i.e. no buffers in the
> > > queue) don't take snapshots.
> > > 
> > > > I have been thinking of adding MAP/UNMAP descriptors for
> > > > a while now. Thus it will be possible to modify
> > > > userspace memory without consuming 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-27 Thread Babis Chalios

On 22/9/23 18:01, Michael S. Tsirkin wrote:

On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:


On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
  <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.

That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?

I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis


Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.


That sounds like a nice mechanism. However in our case the page
holding the counter that gets increased by the hardware is a kernel
page.

The reason for that is that things other than us (virtio-rng) might
want to notify for leak events. For example, I think that Jason
intended to use this mechanism to periodically notify user-space
PRNGs that they need to reseed.

Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-22 Thread Michael S. Tsirkin
On Fri, Sep 22, 2023 at 05:40:50PM +0200, Babis Chalios wrote:
> 
> 
> On 22/9/23 17:06, Michael S. Tsirkin wrote:
> > On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> > > On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > > > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > > > Resending to fix e-mail formatting issues (sorry for the spam)
> > > > > 
> > > > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > > > Yes, that's what the driver does now in the RFC patch.
> > > > > > > > > > However, this just
> > > > > > > > > > decreases
> > > > > > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > > > > > leak event happens it
> > > > > > > > > > might not
> > > > > > > > > > find any buffers to use:
> > > > > > > > > > 
> > > > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > > > 2. available buffers to queue X
> > > > > > > > > > 
> > > > > > > > > > 
> > > > > > > > > > 3. poll queue X
> > > > > > > > > > 4. used buffers in queue X   <- leak event 1 will
> > > > > > > > > > use buffers in X
> > > > > > > > > > 5. avail buffers in queue X
> > > > > > > > > > 6. poll queue 1-X<- leak event 2 will
> > > > > > > > > > use buffers in 1-X
> > > > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > > >  <- leak event 3 (it
> > > > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > > > 9. goto 3
> > > > > > > > > I don't get it. we added buffers in step 5.
> > > > > > > > What if the leak event 3 arrives before step 5 had time to
> > > > > > > > actually add the
> > > > > > > > buffers in X and make
> > > > > > > > them visible to the device?
> > > > > > > Then it will see a single event in 1-X instead of two events.  A 
> > > > > > > leak is
> > > > > > > a leak though, I don't see does it matter how many triggered.
> > > > > > > 
> > > > > So the scenario I have in mind is the following:
> > > > > 
> > > > > (Epoch here is terminology that I used in the Linux RFC. It is a value
> > > > > maintained by random.c
> > > > > that changes every time a leak event happens).
> > > > > 
> > > > > 1. add buffers to 1-X
> > > > > 2. add buffers to X
> > > > > 3. poll queue X
> > > > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > > > 5. Device: First snapshot, uses buffers in X
> > > > > 6. vcpu 1: sees used buffers
> > > > > 7. Device: Second snapshot, uses buffers in 1-X
> > > > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > > > 9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from 
> > > > > step 6
> > > > > has not yet finished adding new buffers).
> > > > > 10. vcpu 1 adds new buffer in X
> > > > > 11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.
> > > > > 
> > > > > 
> > > > > In this succession of events, when the third snapshot will happen, the
> > > > > device won't find
> > > > > any buffers in either queue, so it won't increase the RNG epoch 
> > > > > value. So,
> > > > > any entropy
> > > > > gathered after step 8 will be the same across all snapshots. Am I 
> > > > > missing
> > > > > something?
> > > > > 
> > > > > Cheers,
> > > > > Babis
> > > > > 
> > > > Yes but notice how this is followed by:
> > > > 
> > > > 12. vcpu 1: sees used buffers in 1-X
> > > > 
> > > > Driver can notify getrandom I guess?
> > > It could, but then we have the exact race condition that VMGENID had,
> > > userspace has already consumed stale entropy and there's nothing we
> > > can do about that.
> > > 
> > > Although this is indeed a corner case, it feels like it beats the purpose
> > > of having the hardware update directly userspace (via copy on leak).
> > > 
> > > How do you feel about the proposal a couple of emails back? It looks to
> > > me that it avoids completely the race condition.
> > > 
> > > Cheers,
> > > Babis
> > It does. The problem of course is that this means that e.g.
> > taking a snapshot of a guest that is stuck won't work well.
> 
> That is true, but does it matter? The intention of the proposal
> is that if it is not safe to take snapshots (i.e. no buffers in the
> queue) don't take snapshots.
> 
> > I have been thinking of adding MAP/UNMAP descriptors for
> > a while now. Thus it will be possible to modify
> > userspace memory without consuming buffers.
> > Would something like this solve the problem?
> 
> I am not familiar with MAP/UNMAP descriptors. Is there
> a link where I can read about them?
> 
> Cheers,
> Babis


Heh no I just came up with the name. Will write up in a couple
of days, but the idea is that driver does get_user_pages,
adds buffer to queue, and device will remember the address
and change that memory on a snapshot. If there are buffers
in the queue it will also use these to tell driver,
but if there are no buffers then it won't.

-- 
MST


-
To 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-22 Thread Babis Chalios




On 22/9/23 17:06, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
 <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.


That is true, but does it matter? The intention of the proposal
is that if it is not safe to take snapshots (i.e. no buffers in the
queue) don't take snapshots.


I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?


I am not familiar with MAP/UNMAP descriptors. Is there
a link where I can read about them?

Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-22 Thread Michael S. Tsirkin
On Tue, Sep 19, 2023 at 12:11:37PM +0200, Babis Chalios wrote:
> On 19/9/23 12:01, Michael S. Tsirkin wrote:
> > On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> > > Resending to fix e-mail formatting issues (sorry for the spam)
> > > 
> > > On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > > > Yes, that's what the driver does now in the RFC patch.
> > > > > > > > However, this just
> > > > > > > > decreases
> > > > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > > > leak event happens it
> > > > > > > > might not
> > > > > > > > find any buffers to use:
> > > > > > > > 
> > > > > > > > 1. available buffers to queue 1-X
> > > > > > > > 2. available buffers to queue X
> > > > > > > > 
> > > > > > > > 
> > > > > > > > 3. poll queue X
> > > > > > > > 4. used buffers in queue X   <- leak event 1 will
> > > > > > > > use buffers in X
> > > > > > > > 5. avail buffers in queue X
> > > > > > > > 6. poll queue 1-X<- leak event 2 will
> > > > > > > > use buffers in 1-X
> > > > > > > > 7. used buffers in queue 1-X
> > > > > > > > 8. avail buffers in queue 1-X
> > > > > > > > <- leak event 3 (it
> > > > > > > > needs buffers in X, race with step 5)
> > > > > > > > 9. goto 3
> > > > > > > I don't get it. we added buffers in step 5.
> > > > > > What if the leak event 3 arrives before step 5 had time to
> > > > > > actually add the
> > > > > > buffers in X and make
> > > > > > them visible to the device?
> > > > > Then it will see a single event in 1-X instead of two events.  A leak 
> > > > > is
> > > > > a leak though, I don't see does it matter how many triggered.
> > > > > 
> > > 
> > > So the scenario I have in mind is the following:
> > > 
> > > (Epoch here is terminology that I used in the Linux RFC. It is a value
> > > maintained by random.c
> > > that changes every time a leak event happens).
> > > 
> > > 1. add buffers to 1-X
> > > 2. add buffers to X
> > > 3. poll queue X
> > > 4. vcpu 0: get getrandom() entropy and cache epoch value
> > > 5. Device: First snapshot, uses buffers in X
> > > 6. vcpu 1: sees used buffers
> > > 7. Device: Second snapshot, uses buffers in 1-X
> > > 8. vcpu 0: getrandom() observes new  epoch value & caches it
> > > 9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
> > > has not yet finished adding new buffers).
> > > 10. vcpu 1 adds new buffer in X
> > > 11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.
> > > 
> > > 
> > > In this succession of events, when the third snapshot will happen, the
> > > device won't find
> > > any buffers in either queue, so it won't increase the RNG epoch value. So,
> > > any entropy
> > > gathered after step 8 will be the same across all snapshots. Am I missing
> > > something?
> > > 
> > > Cheers,
> > > Babis
> > > 
> > Yes but notice how this is followed by:
> > 
> > 12. vcpu 1: sees used buffers in 1-X
> > 
> > Driver can notify getrandom I guess?
> 
> It could, but then we have the exact race condition that VMGENID had,
> userspace has already consumed stale entropy and there's nothing we
> can do about that.
> 
> Although this is indeed a corner case, it feels like it beats the purpose
> of having the hardware update directly userspace (via copy on leak).
> 
> How do you feel about the proposal a couple of emails back? It looks to
> me that it avoids completely the race condition.
> 
> Cheers,
> Babis

It does. The problem of course is that this means that e.g.
taking a snapshot of a guest that is stuck won't work well.
I have been thinking of adding MAP/UNMAP descriptors for
a while now. Thus it will be possible to modify
userspace memory without consuming buffers.
Would something like this solve the problem?



> -
> To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
> For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-22 Thread Babis Chalios

Hi Michael,

On 19/9/23 12:11, Babis Chalios wrote:

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X    <- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
    <- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?
Then it will see a single event in 1-X instead of two events.  A 
leak is

a leak though, I don't see does it matter how many triggered.



So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from 
step 6

has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch 
value. So,

any entropy
gathered after step 8 will be the same across all snapshots. Am I 
missing

something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?


It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.


Any thoughts on this? Sorry for pushing. I want to finalize the details 
on this,

so I can close open fronts on the LKML patch.

Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-19 Thread Babis Chalios

On 19/9/23 12:01, Michael S. Tsirkin wrote:

On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:

Yes, that's what the driver does now in the RFC patch.
However, this just
decreases
the race window, it doesn't eliminate it. If a third
leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will
use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will
use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
<- leak event 3 (it
needs buffers in X, race with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to
actually add the
buffers in X and make
them visible to the device?

Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.



So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value
maintained by random.c
that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
has not yet finished adding new buffers).
10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. So,
any entropy
gathered after step 8 will be the same across all snapshots. Am I missing
something?

Cheers,
Babis


Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?


It could, but then we have the exact race condition that VMGENID had,
userspace has already consumed stale entropy and there's nothing we
can do about that.

Although this is indeed a corner case, it feels like it beats the purpose
of having the hardware update directly userspace (via copy on leak).

How do you feel about the proposal a couple of emails back? It looks to
me that it avoids completely the race condition.

Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-19 Thread Michael S. Tsirkin
On Tue, Sep 19, 2023 at 09:32:08AM +0200, Babis Chalios wrote:
> Resending to fix e-mail formatting issues (sorry for the spam)
> 
> On 18/9/23 18:30, Babis Chalios wrote:
> > > > > > Yes, that's what the driver does now in the RFC patch.
> > > > > > However, this just
> > > > > > decreases
> > > > > > the race window, it doesn't eliminate it. If a third
> > > > > > leak event happens it
> > > > > > might not
> > > > > > find any buffers to use:
> > > > > > 
> > > > > > 1. available buffers to queue 1-X
> > > > > > 2. available buffers to queue X
> > > > > > 
> > > > > > 
> > > > > > 3. poll queue X
> > > > > > 4. used buffers in queue X   <- leak event 1 will
> > > > > > use buffers in X
> > > > > > 5. avail buffers in queue X
> > > > > > 6. poll queue 1-X    <- leak event 2 will
> > > > > > use buffers in 1-X
> > > > > > 7. used buffers in queue 1-X
> > > > > > 8. avail buffers in queue 1-X
> > > > > >    <- leak event 3 (it
> > > > > > needs buffers in X, race with step 5)
> > > > > > 9. goto 3
> > > > > I don't get it. we added buffers in step 5.
> > > > What if the leak event 3 arrives before step 5 had time to
> > > > actually add the
> > > > buffers in X and make
> > > > them visible to the device?
> > > 
> > > Then it will see a single event in 1-X instead of two events.  A leak is
> > > a leak though, I don't see does it matter how many triggered.
> > > 
> 
> 
> So the scenario I have in mind is the following:
> 
> (Epoch here is terminology that I used in the Linux RFC. It is a value
> maintained by random.c
> that changes every time a leak event happens).
> 
> 1. add buffers to 1-X
> 2. add buffers to X
> 3. poll queue X
> 4. vcpu 0: get getrandom() entropy and cache epoch value
> 5. Device: First snapshot, uses buffers in X
> 6. vcpu 1: sees used buffers
> 7. Device: Second snapshot, uses buffers in 1-X
> 8. vcpu 0: getrandom() observes new  epoch value & caches it
> 9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 6
> has not yet finished adding new buffers).
> 10. vcpu 1 adds new buffer in X
> 11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.
> 
> 
> In this succession of events, when the third snapshot will happen, the
> device won't find
> any buffers in either queue, so it won't increase the RNG epoch value. So,
> any entropy
> gathered after step 8 will be the same across all snapshots. Am I missing
> something?
> 
> Cheers,
> Babis
> 

Yes but notice how this is followed by:

12. vcpu 1: sees used buffers in 1-X

Driver can notify getrandom I guess?

-- 
MST


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-19 Thread Babis Chalios

Resending to fix e-mail formatting issues (sorry for the spam)

On 18/9/23 18:30, Babis Chalios wrote:
Yes, that's what the driver does now in the RFC patch. However, 
this just

decreases
the race window, it doesn't eliminate it. If a third leak event 
happens it

might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will use buffers 
in X

5. avail buffers in queue X
6. poll queue 1-X    <- leak event 2 will use buffers 
in 1-X

7. used buffers in queue 1-X
8. avail buffers in queue 1-X
   <- leak event 3 (it needs 
buffers in X, race with step 5)

9. goto 3

I don't get it. we added buffers in step 5.
What if the leak event 3 arrives before step 5 had time to actually 
add the

buffers in X and make
them visible to the device?


Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.




So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC. It is a value 
maintained by random.c

that changes every time a leak event happens).

1. add buffers to 1-X
2. add buffers to X
3. poll queue X
4. vcpu 0: get getrandom() entropy and cache epoch value
5. Device: First snapshot, uses buffers in X
6. vcpu 1: sees used buffers
7. Device: Second snapshot, uses buffers in 1-X
8. vcpu 0: getrandom() observes new  epoch value & caches it
9. Device: Third snapshot, no buffers in either queue, (vcpu 1 from step 
6 has not yet finished adding new buffers).

10. vcpu 1 adds new buffer in X
11. vcpu 0: getrandom() will not see new epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, the 
device won't find
any buffers in either queue, so it won't increase the RNG epoch value. 
So, any entropy
gathered after step 8 will be the same across all snapshots. Am I 
missing something?


Cheers,
Babis




-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Babis Chalios

Yes, that's what the driver does now in the RFC patch. However, this just
decreases
the race window, it doesn't eliminate it. If a third leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
   <- leak event 3 (it needs buffers in X, race 
with step 5)
9. goto 3

I don't get it. we added buffers in step 5.

What if the leak event 3 arrives before step 5 had time to actually add the
buffers in X and make
them visible to the device?


Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.



So the scenario I have in mind is the following:

(Epoch here is terminology that I used in the Linux RFC).

    Driver 
 Device


1.     add buffers to 1-X
2.     add buffers to X
3.     poll queue X
4.     vcpu 0: cache getrandom() entropy
        and cache epoch value
5.           First snapshot: use buffers in X
6.     vcpu 1: sees used buffers
7.  Second snapshot: use buffers in 1-X
8.     vcpu 0: getrandom() observes new
    epoch value & caches it
9.    Third snapshot: no buffers in either queue vcpu 1 (from 
step 6 has not yet finished adding new buffers).

10.   vcpu 1 adds new buffer in X
11.   vcpu 0: getrandom() will not see new
        epoch and gets stale entropy.


In this succession of events, when the third snapshot will happen, it 
won't find any buffers in either queue,
so it won't increase the RNG epoch value. So, any entropy gathered after 
step 8 will be the same across all

snapshots. Am I missing something?

Cheers,
Babis



-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Michael S. Tsirkin
On Mon, Sep 18, 2023 at 04:02:54PM +0200, Babis Chalios wrote:
> On 18/9/23 15:58, Michael S. Tsirkin wrote:
> > On Mon, Sep 18, 2023 at 03:00:43PM +0200, Babis Chalios wrote:
> > > > > Right, so I think that there is a race condition between the time the 
> > > > > driver
> > > > > sees the used buffers of the first
> > > > > batch and until it adds the second batch on the next leak queue.
> > > > > 
> > > > > 1. driver adds batch 1
> > > > > 2. leak event
> > > > > 3. device uses batch 1
> > > > > 4. driver sees the used buffers and
> > > > >   a. switches leak queues
> > > > >   b. adds batch 2.
> > > > > 5. devices finds initial leak queue empty and sees buffers in second 
> > > > > leak
> > > > > queue.
> > > > > 
> > > > > If a second leak event happens after step 3 above and before all of 
> > > > > steps 4
> > > > > complete then batch 2 will not
> > > > > be processed as part of the second leak event.
> > > > driver can just pre-add buffers in the second queue.
> > > > 
> > > > 1. available buffers to queue 1-X
> > > > 2. available buffers to queue X
> > > > 
> > > > 
> > > > 3. poll queue X
> > > > 4. used buffers in queue X
> > > > 5. avail buffers in queue X
> > > > 6. poll queue 1-X
> > > > 7. used buffers in queue X
> > > > 8. avail buffers in queue X
> > > > 9. goto 3
> > > > 
> > > Yes, that's what the driver does now in the RFC patch. However, this just
> > > decreases
> > > the race window, it doesn't eliminate it. If a third leak event happens it
> > > might not
> > > find any buffers to use:
> > > 
> > > 1. available buffers to queue 1-X
> > > 2. available buffers to queue X
> > > 
> > > 
> > > 3. poll queue X
> > > 4. used buffers in queue X   <- leak event 1 will use buffers in X
> > > 5. avail buffers in queue X
> > > 6. poll queue 1-X<- leak event 2 will use buffers in 1-X
> > > 7. used buffers in queue 1-X
> > > 8. avail buffers in queue 1-X
> > >   <- leak event 3 (it needs buffers in X, 
> > > race with step 5)
> > > 9. goto 3
> > 
> > I don't get it. we added buffers in step 5.
> 
> What if the leak event 3 arrives before step 5 had time to actually add the
> buffers in X and make
> them visible to the device?


Then it will see a single event in 1-X instead of two events.  A leak is
a leak though, I don't see does it matter how many triggered.


> 
> > 
> > > 
> > > If, instead, we define a single leak queue and require that VMM should 
> > > refuse to take a snapshot
> > > if that queue is empty, we avoid the race condition in all cases and IMHO 
> > > the protocol becomes
> > > much simpler.
> > > 
> > > 
> > > Cheers,
> > > Babis
> > > 
> > 
> > -
> > To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
> > For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org
> > 


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Babis Chalios

On 18/9/23 15:58, Michael S. Tsirkin wrote:

On Mon, Sep 18, 2023 at 03:00:43PM +0200, Babis Chalios wrote:

Right, so I think that there is a race condition between the time the driver
sees the used buffers of the first
batch and until it adds the second batch on the next leak queue.

1. driver adds batch 1
2. leak event
3. device uses batch 1
4. driver sees the used buffers and
  a. switches leak queues
  b. adds batch 2.
5. devices finds initial leak queue empty and sees buffers in second leak
queue.

If a second leak event happens after step 3 above and before all of steps 4
complete then batch 2 will not
be processed as part of the second leak event.

driver can just pre-add buffers in the second queue.

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X
5. avail buffers in queue X
6. poll queue 1-X
7. used buffers in queue X
8. avail buffers in queue X
9. goto 3


Yes, that's what the driver does now in the RFC patch. However, this just
decreases
the race window, it doesn't eliminate it. If a third leak event happens it
might not
find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
  <- leak event 3 (it needs buffers in X, race 
with step 5)
9. goto 3


I don't get it. we added buffers in step 5.


What if the leak event 3 arrives before step 5 had time to actually add 
the buffers in X and make

them visible to the device?






If, instead, we define a single leak queue and require that VMM should refuse 
to take a snapshot
if that queue is empty, we avoid the race condition in all cases and IMHO the 
protocol becomes
much simpler.


Cheers,
Babis



-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org




-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Michael S. Tsirkin
On Mon, Sep 18, 2023 at 03:00:43PM +0200, Babis Chalios wrote:
> 
> > > Right, so I think that there is a race condition between the time the 
> > > driver
> > > sees the used buffers of the first
> > > batch and until it adds the second batch on the next leak queue.
> > > 
> > > 1. driver adds batch 1
> > > 2. leak event
> > > 3. device uses batch 1
> > > 4. driver sees the used buffers and
> > >  a. switches leak queues
> > >  b. adds batch 2.
> > > 5. devices finds initial leak queue empty and sees buffers in second leak
> > > queue.
> > > 
> > > If a second leak event happens after step 3 above and before all of steps 
> > > 4
> > > complete then batch 2 will not
> > > be processed as part of the second leak event.
> > driver can just pre-add buffers in the second queue.
> > 
> > 1. available buffers to queue 1-X
> > 2. available buffers to queue X
> > 
> > 
> > 3. poll queue X
> > 4. used buffers in queue X
> > 5. avail buffers in queue X
> > 6. poll queue 1-X
> > 7. used buffers in queue X
> > 8. avail buffers in queue X
> > 9. goto 3
> > 
> 
> Yes, that's what the driver does now in the RFC patch. However, this just
> decreases
> the race window, it doesn't eliminate it. If a third leak event happens it
> might not
> find any buffers to use:
> 
> 1. available buffers to queue 1-X
> 2. available buffers to queue X
> 
> 
> 3. poll queue X
> 4. used buffers in queue X   <- leak event 1 will use buffers in X
> 5. avail buffers in queue X
> 6. poll queue 1-X<- leak event 2 will use buffers in 1-X
> 7. used buffers in queue 1-X
> 8. avail buffers in queue 1-X
>  <- leak event 3 (it needs buffers in X, race 
> with step 5)
> 9. goto 3


I don't get it. we added buffers in step 5.

> 
> 
> If, instead, we define a single leak queue and require that VMM should refuse 
> to take a snapshot
> if that queue is empty, we avoid the race condition in all cases and IMHO the 
> protocol becomes
> much simpler.
> 
> 
> Cheers,
> Babis
> 


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Babis Chalios




Right, so I think that there is a race condition between the time the driver
sees the used buffers of the first
batch and until it adds the second batch on the next leak queue.

1. driver adds batch 1
2. leak event
3. device uses batch 1
4. driver sees the used buffers and
 a. switches leak queues
 b. adds batch 2.
5. devices finds initial leak queue empty and sees buffers in second leak
queue.

If a second leak event happens after step 3 above and before all of steps 4
complete then batch 2 will not
be processed as part of the second leak event.

driver can just pre-add buffers in the second queue.

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X
5. avail buffers in queue X
6. poll queue 1-X
7. used buffers in queue X
8. avail buffers in queue X
9. goto 3



Yes, that's what the driver does now in the RFC patch. However, this 
just decreases
the race window, it doesn't eliminate it. If a third leak event happens 
it might not

find any buffers to use:

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X   <- leak event 1 will use buffers in X
5. avail buffers in queue X
6. poll queue 1-X<- leak event 2 will use buffers in 1-X
7. used buffers in queue 1-X
8. avail buffers in queue 1-X
 <- leak event 3 (it needs buffers in X, race 
with step 5)
9. goto 3


If, instead, we define a single leak queue and require that VMM should refuse 
to take a snapshot
if that queue is empty, we avoid the race condition in all cases and IMHO the 
protocol becomes
much simpler.


Cheers,
Babis



-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Michael S. Tsirkin
On Wed, Sep 13, 2023 at 01:19:49PM +0200, Babis Chalios wrote:
> 
> 
> On 13/9/23 11:37, Michael S. Tsirkin wrote:
> > CAUTION: This email originated from outside of the organization. Do not 
> > click links or open attachments unless you can confirm the sender and know 
> > the content is safe.
> > 
> > 
> > 
> > On Wed, Sep 13, 2023 at 11:32:57AM +0200, Babis Chalios wrote:
> > > > I do not understand why this matters though. we know there was a leak,
> > > > why does it matter whether there was one or two leaks?
> > > > 
> > > > > In the last RFC implementing this in Linux we sent to LKML [1] we 
> > > > > avoid
> > > the
> > > > > issue by pre-populating both
> > > > > queues, but that does not solve the problem if a third entropy leak
> > > event
> > > > > arrives. The probability of this
> > > > > happening is indeed small, but we thought of a potential solution to
> > > this.
> > > > > What if we modify the spec here to instruct the VMM to deny taking a
> > > > > snapshot if there are not any buffers
> > > > > in the active leak queue? If we did this, we could even simplify the
> > > spec to
> > > > > just introduce a single entropy
> > > > > leak queue, so we could avoid the complexity of switching between 
> > > > > active
> > > > > leak queues in the driver and
> > > > > the device. WDYT?
> > > > here's the problem:
> > > > 
> > > > - driver adds batch 1 of buffers
> > > > - leak
> > > > - device starts using buffers from batch 1
> > > > - driver sees some buffers and starts adding batch 2
> > > If understand this clause:
> > > 
> > > > > +\item Upon detecting that buffers have been used, driver
> > > > > +  switches to another leak queue making it active
> > > > > +  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
> > > > > +  It then starts adding buffers to the new leak queue.
> > > correctly:
> > > 
> > > At this point, the driver will first switch active leak queue and
> > > then add batch 2 to the new leak queue.
> > > 
> > > and due to this:
> > > 
> > > > > +\item Device will keep using buffers in the active leak queue
> > > > > +  until it detects that both the current leak queue is empty and
> > > another
> > > > > +  leak queue has buffers. At that point device switches to
> > > > > +  another leak queue, making it active.
> > > > > +\item After the switch, buffers from the new leak queue are not
> > > > > +  used until an information leak is detected.
> > > > > +\end{enumerate}
> > > the following won't happen:
> > > 
> > > > - device sees batch 2 and thinks this is part of batch 1
> > > >      consumes them all
> > > Does it make sense?
> > > 
> > > Cheers,
> > > Babis
> > yes, the queue switch is used as a barrier to detect a new leak event.
> 
> Right, so I think that there is a race condition between the time the driver
> sees the used buffers of the first
> batch and until it adds the second batch on the next leak queue.
> 
> 1. driver adds batch 1
> 2. leak event
> 3. device uses batch 1
> 4. driver sees the used buffers and
>     a. switches leak queues
>     b. adds batch 2.
> 5. devices finds initial leak queue empty and sees buffers in second leak
> queue.
> 
> If a second leak event happens after step 3 above and before all of steps 4
> complete then batch 2 will not
> be processed as part of the second leak event.

driver can just pre-add buffers in the second queue.

1. available buffers to queue 1-X
2. available buffers to queue X


3. poll queue X
4. used buffers in queue X
5. avail buffers in queue X
6. poll queue 1-X
7. used buffers in queue X
8. avail buffers in queue X
9. goto 3




> > --
> > MST
> > 
> > 
> > -
> > To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
> > For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org
> > 
> 
> 
> -
> To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
> For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-18 Thread Babis Chalios




yes, the queue switch is used as a barrier to detect a new leak event.


Right, so I think that there is a race condition between the time the 
driver sees the used buffers of the first

batch and until it adds the second batch on the next leak queue.

1. driver adds batch 1
2. leak event
3. device uses batch 1
4. driver sees the used buffers and
    a. switches leak queues
    b. adds batch 2.
5. devices finds initial leak queue empty and sees buffers in second 
leak queue.


If a second leak event happens after step 3 above and before all of 
steps 4 complete then batch 2 will not

be processed as part of the second leak event.



Hey Michael, any thoughts on this? I think the my Linux RFC patch[1] 
showcases the problem in the
`entropy_leak_detected` function which handles the used buffers. If on 
the VMM we receive a new leak
event before `entropy_leak_detected` runs to completion (and adds a new 
batch of buffers) the leak

event will not have any buffers to handle.

[1] 
https://lore.kernel.org/lkml/20230823090107.65749-3-bchal...@amazon.es/T/#m085769c7b9c08f4acac626e7b4ecde11af13a5be


Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-13 Thread Babis Chalios




On 13/9/23 11:37, Michael S. Tsirkin wrote:

CAUTION: This email originated from outside of the organization. Do not click 
links or open attachments unless you can confirm the sender and know the 
content is safe.



On Wed, Sep 13, 2023 at 11:32:57AM +0200, Babis Chalios wrote:

I do not understand why this matters though. we know there was a leak,
why does it matter whether there was one or two leaks?


In the last RFC implementing this in Linux we sent to LKML [1] we avoid

the

issue by pre-populating both
queues, but that does not solve the problem if a third entropy leak

event

arrives. The probability of this
happening is indeed small, but we thought of a potential solution to

this.

What if we modify the spec here to instruct the VMM to deny taking a
snapshot if there are not any buffers
in the active leak queue? If we did this, we could even simplify the

spec to

just introduce a single entropy
leak queue, so we could avoid the complexity of switching between active
leak queues in the driver and
the device. WDYT?

here's the problem:

- driver adds batch 1 of buffers
- leak
- device starts using buffers from batch 1
- driver sees some buffers and starts adding batch 2

If understand this clause:


+\item Upon detecting that buffers have been used, driver
+  switches to another leak queue making it active
+  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
+  It then starts adding buffers to the new leak queue.

correctly:

At this point, the driver will first switch active leak queue and
then add batch 2 to the new leak queue.

and due to this:


+\item Device will keep using buffers in the active leak queue
+  until it detects that both the current leak queue is empty and

another

+  leak queue has buffers. At that point device switches to
+  another leak queue, making it active.
+\item After the switch, buffers from the new leak queue are not
+  used until an information leak is detected.
+\end{enumerate}

the following won't happen:


- device sees batch 2 and thinks this is part of batch 1
     consumes them all

Does it make sense?

Cheers,
Babis

yes, the queue switch is used as a barrier to detect a new leak event.


Right, so I think that there is a race condition between the time the 
driver sees the used buffers of the first

batch and until it adds the second batch on the next leak queue.

1. driver adds batch 1
2. leak event
3. device uses batch 1
4. driver sees the used buffers and
    a. switches leak queues
    b. adds batch 2.
5. devices finds initial leak queue empty and sees buffers in second 
leak queue.


If a second leak event happens after step 3 above and before all of 
steps 4 complete then batch 2 will not

be processed as part of the second leak event.


--
MST


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org




-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-13 Thread Michael S. Tsirkin
On Wed, Sep 13, 2023 at 11:32:57AM +0200, Babis Chalios wrote:
> > I do not understand why this matters though. we know there was a leak,
> > why does it matter whether there was one or two leaks?
> >
> > > In the last RFC implementing this in Linux we sent to LKML [1] we avoid
> the
> > > issue by pre-populating both
> > > queues, but that does not solve the problem if a third entropy leak
> event
> > > arrives. The probability of this
> > > happening is indeed small, but we thought of a potential solution to
> this.
> > >
> > > What if we modify the spec here to instruct the VMM to deny taking a
> > > snapshot if there are not any buffers
> > > in the active leak queue? If we did this, we could even simplify the
> spec to
> > > just introduce a single entropy
> > > leak queue, so we could avoid the complexity of switching between active
> > > leak queues in the driver and
> > > the device. WDYT?
> >
> > here's the problem:
> >
> > - driver adds batch 1 of buffers
> > - leak
> > - device starts using buffers from batch 1
> > - driver sees some buffers and starts adding batch 2
> 
> If understand this clause:
> 
> > > +\item Upon detecting that buffers have been used, driver
> > > +  switches to another leak queue making it active
> > > +  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
> > > +  It then starts adding buffers to the new leak queue.
> 
> correctly:
> 
> At this point, the driver will first switch active leak queue and
> then add batch 2 to the new leak queue.
> 
> and due to this:
> 
> > > +\item Device will keep using buffers in the active leak queue
> > > +  until it detects that both the current leak queue is empty and
> another
> > > +  leak queue has buffers. At that point device switches to
> > > +  another leak queue, making it active.
> > > +\item After the switch, buffers from the new leak queue are not
> > > +  used until an information leak is detected.
> > > +\end{enumerate}
> 
> the following won't happen:
> 
> > - device sees batch 2 and thinks this is part of batch 1
> >    consumes them all
> 
> Does it make sense?
> 
> Cheers,
> Babis

yes, the queue switch is used as a barrier to detect a new leak event.

-- 
MST


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-13 Thread Babis Chalios

> I do not understand why this matters though. we know there was a leak,
> why does it matter whether there was one or two leaks?
>
> > In the last RFC implementing this in Linux we sent to LKML [1] we 
avoid the

> > issue by pre-populating both
> > queues, but that does not solve the problem if a third entropy leak 
event

> > arrives. The probability of this
> > happening is indeed small, but we thought of a potential solution 
to this.

> >
> > What if we modify the spec here to instruct the VMM to deny taking a
> > snapshot if there are not any buffers
> > in the active leak queue? If we did this, we could even simplify 
the spec to

> > just introduce a single entropy
> > leak queue, so we could avoid the complexity of switching between 
active

> > leak queues in the driver and
> > the device. WDYT?
>
> here's the problem:
>
> - driver adds batch 1 of buffers
> - leak
> - device starts using buffers from batch 1
> - driver sees some buffers and starts adding batch 2

If understand this clause:

> > +\item Upon detecting that buffers have been used, driver
> > +  switches to another leak queue making it active
> > +  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
> > +  It then starts adding buffers to the new leak queue.

correctly:

At this point, the driver will first switch active leak queue and
then add batch 2 to the new leak queue.

and due to this:

> > +\item Device will keep using buffers in the active leak queue
> > +  until it detects that both the current leak queue is empty 
and another

> > +  leak queue has buffers. At that point device switches to
> > +  another leak queue, making it active.
> > +\item After the switch, buffers from the new leak queue are not
> > +  used until an information leak is detected.
> > +\end{enumerate}

the following won't happen:

> - device sees batch 2 and thinks this is part of batch 1
>    consumes them all

Does it make sense?

Cheers,
Babis

-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org



Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-09-12 Thread Michael S. Tsirkin
On Thu, Aug 31, 2023 at 12:16:22PM +0200, Babis Chalios wrote:
> Hi Michael,
> 
> On 21/11/22 17:30, Michael S. Tsirkin wrote:
> > Add virtqueues to support reporting entropy leaks (similar to virtio based 
> > vmgenid).
> > 
> > Signed-off-by: Michael S. Tsirkin 
> > ---
> >   virtio-rng.tex | 61 +-
> >   1 file changed, 60 insertions(+), 1 deletion(-)
> > 
> > diff --git a/virtio-rng.tex b/virtio-rng.tex
> > index 1ec7164..4760dfa 100644
> > --- a/virtio-rng.tex
> > +++ b/virtio-rng.tex
> > @@ -9,10 +9,14 @@ \subsection{Device ID}\label{sec:Device Types / Entropy 
> > Device / Device ID}
> >   \subsection{Virtqueues}\label{sec:Device Types / Entropy Device / 
> > Virtqueues}
> >   \begin{description}
> >   \item[0] requestq
> > +\item[1] leakq1 (only if VIRTIO_RNG_F_LEAK is offered)
> > +\item[2] leakq2 (only if VIRTIO_RNG_F_LEAK is offered)
> >   \end{description}
> >   \subsection{Feature bits}\label{sec:Device Types / Entropy Device / 
> > Feature bits}
> > -  None currently defined
> > +\begin{description}
> > +\item[VIRTIO_RNG_F_LEAK (0)] Device can report and handle information 
> > leaks.
> > +\end{description}
> >   \subsection{Device configuration layout}\label{sec:Device Types / Entropy 
> > Device / Device configuration layout}
> > None currently defined.
> > @@ -21,6 +25,7 @@ \subsection{Device Initialization}\label{sec:Device Types 
> > / Entropy Device / Dev
> >   \begin{enumerate}
> >   \item The \field{requestq} virtqueue is initialized
> > +\item If VIRTIO_RNG_F_LEAK has been negotiated, \field{leakq1} and 
> > \field{leakq2} are initialized
> >   \end{enumerate}
> >   \subsection{Device Operation}\label{sec:Device Types / Entropy Device / 
> > Device Operation}
> > @@ -41,3 +46,57 @@ \subsection{Device Operation}\label{sec:Device Types / 
> > Entropy Device / Device O
> >   The device MUST place one or more random bytes into the buffer
> >   made available to it through \field{requestq}, but it
> >   MAY use less than the entire buffer length.
> > +
> > +\subsubsection{Reporting Information Leaks}{Device Types / Entropy Device 
> > / Device Operation / Reporting Information Leaks}
> > +
> > +The device might, after the fact, detect that some of the entropy
> > +it supplied to the driver has after the fact degraded in quality
> > +or leaked to the outside world.  One example is when the device
> > +is part of the virtual machine undergoing a restore from snapshot
> > +operation. Another example is when the information leaks from the
> > +host system through a side-channel.
> > +
> > +The driver would typically react by causing regeneration of any
> > +information that might have leaked and that has to be secret or
> > +unique.  It is understood that when an information leak has been
> > +detected it is likely not limited to the entropy received through
> > +the specific device. In particular, this is the case for
> > +snapshoting It is thus suggested that the system fully
> > +regenerate any unique/secret information in this scenario.
> > +
> > +If VIRTIO_RNG_F_LEAK has been negotiated the device can report
> > +such leaks to the driver through a set of dedicated leak
> > +queues: \field{leakq1} and \field{leakq2}.
> > +
> > +Buffers added to the leak queues can have one of two forms:
> > +\begin{enumerate}
> > +\item A write-only buffer. It will be completely filled by random data by 
> > the device.
> > +\item A buffer consisting of read-only section followed by a
> > +write-only section, both of identical size. The
> > +device will copy data from the read-only section to the write-only
> > +section.
> > +\end{enumerate}
> > +
> > +The steps for operating the virtqueue are:
> > +
> > +\begin{enumerate}
> > +\item At each time, only one of \field{leakq1}, \field{leakq2} is active
> > +  (has buffers added/used).
> > +\item After initialization, \field{leakq1} is active.
> > +\item Driver adds multiple buffers to the active leak queue.
> > +\item The buffers are not used until an information leak is
> > +  detected, as long as that is the case driver can
> > +  add more buffers to the active queue.
> > +\item Upon detecting an information leak, device starts
> > +  using buffers in the active leak queue.
> > +\item Upon detecting that buffers have been used, driver
> > +  switches to another leak queue making it active
> > +  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
> > +  It then starts adding buffers to the new leak queue.\
> 
> I have been discussing with Alex and we think there's a potential race here,
> between the time the driver
> sees the used buffers in the active leak queue until the time it adds new
> buffers to the next leak queue.
> If a new entropy leak event arrives the VMM won't find any buffers in the
> queue.

I do not understand why this matters though. we know there was a leak,
why does it matter whether there was one or two leaks?

> In the last RFC implementing this 

Re: [virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2023-08-31 Thread Babis Chalios

Hi Michael,

On 21/11/22 17:30, Michael S. Tsirkin wrote:

Add virtqueues to support reporting entropy leaks (similar to virtio based 
vmgenid).

Signed-off-by: Michael S. Tsirkin 
---
  virtio-rng.tex | 61 +-
  1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/virtio-rng.tex b/virtio-rng.tex
index 1ec7164..4760dfa 100644
--- a/virtio-rng.tex
+++ b/virtio-rng.tex
@@ -9,10 +9,14 @@ \subsection{Device ID}\label{sec:Device Types / Entropy 
Device / Device ID}
  \subsection{Virtqueues}\label{sec:Device Types / Entropy Device / Virtqueues}
  \begin{description}
  \item[0] requestq
+\item[1] leakq1 (only if VIRTIO_RNG_F_LEAK is offered)
+\item[2] leakq2 (only if VIRTIO_RNG_F_LEAK is offered)
  \end{description}
  
  \subsection{Feature bits}\label{sec:Device Types / Entropy Device / Feature bits}

-  None currently defined
+\begin{description}
+\item[VIRTIO_RNG_F_LEAK (0)] Device can report and handle information leaks.
+\end{description}
  
  \subsection{Device configuration layout}\label{sec:Device Types / Entropy Device / Device configuration layout}

None currently defined.
@@ -21,6 +25,7 @@ \subsection{Device Initialization}\label{sec:Device Types / 
Entropy Device / Dev
  
  \begin{enumerate}

  \item The \field{requestq} virtqueue is initialized
+\item If VIRTIO_RNG_F_LEAK has been negotiated, \field{leakq1} and 
\field{leakq2} are initialized
  \end{enumerate}
  
  \subsection{Device Operation}\label{sec:Device Types / Entropy Device / Device Operation}

@@ -41,3 +46,57 @@ \subsection{Device Operation}\label{sec:Device Types / 
Entropy Device / Device O
  The device MUST place one or more random bytes into the buffer
  made available to it through \field{requestq}, but it
  MAY use less than the entire buffer length.
+
+\subsubsection{Reporting Information Leaks}{Device Types / Entropy Device / 
Device Operation / Reporting Information Leaks}
+
+The device might, after the fact, detect that some of the entropy
+it supplied to the driver has after the fact degraded in quality
+or leaked to the outside world.  One example is when the device
+is part of the virtual machine undergoing a restore from snapshot
+operation. Another example is when the information leaks from the
+host system through a side-channel.
+
+The driver would typically react by causing regeneration of any
+information that might have leaked and that has to be secret or
+unique.  It is understood that when an information leak has been
+detected it is likely not limited to the entropy received through
+the specific device. In particular, this is the case for
+snapshoting It is thus suggested that the system fully
+regenerate any unique/secret information in this scenario.
+
+If VIRTIO_RNG_F_LEAK has been negotiated the device can report
+such leaks to the driver through a set of dedicated leak
+queues: \field{leakq1} and \field{leakq2}.
+
+Buffers added to the leak queues can have one of two forms:
+\begin{enumerate}
+\item A write-only buffer. It will be completely filled by random data by the 
device.
+\item A buffer consisting of read-only section followed by a
+write-only section, both of identical size. The
+device will copy data from the read-only section to the write-only
+section.
+\end{enumerate}
+
+The steps for operating the virtqueue are:
+
+\begin{enumerate}
+\item At each time, only one of \field{leakq1}, \field{leakq2} is active
+  (has buffers added/used).
+\item After initialization, \field{leakq1} is active.
+\item Driver adds multiple buffers to the active leak queue.
+\item The buffers are not used until an information leak is
+  detected, as long as that is the case driver can
+  add more buffers to the active queue.
+\item Upon detecting an information leak, device starts
+  using buffers in the active leak queue.
+\item Upon detecting that buffers have been used, driver
+  switches to another leak queue making it active
+  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
+  It then starts adding buffers to the new leak queue.\


I have been discussing with Alex and we think there's a potential race 
here, between the time the driver
sees the used buffers in the active leak queue until the time it adds 
new buffers to the next leak queue.
If a new entropy leak event arrives the VMM won't find any buffers in 
the queue.


In the last RFC implementing this in Linux we sent to LKML [1] we avoid 
the issue by pre-populating both
queues, but that does not solve the problem if a third entropy leak 
event arrives. The probability of this

happening is indeed small, but we thought of a potential solution to this.

What if we modify the spec here to instruct the VMM to deny taking a 
snapshot if there are not any buffers
in the active leak queue? If we did this, we could even simplify the 
spec to just introduce a single entropy
leak queue, so we could avoid the complexity of switching between active 
leak queues in the 

[virtio-dev] [PATCH RFC 3/3] rng: leak detection support

2022-11-21 Thread Michael S. Tsirkin
Add virtqueues to support reporting entropy leaks (similar to virtio based 
vmgenid).

Signed-off-by: Michael S. Tsirkin 
---
 virtio-rng.tex | 61 +-
 1 file changed, 60 insertions(+), 1 deletion(-)

diff --git a/virtio-rng.tex b/virtio-rng.tex
index 1ec7164..4760dfa 100644
--- a/virtio-rng.tex
+++ b/virtio-rng.tex
@@ -9,10 +9,14 @@ \subsection{Device ID}\label{sec:Device Types / Entropy 
Device / Device ID}
 \subsection{Virtqueues}\label{sec:Device Types / Entropy Device / Virtqueues}
 \begin{description}
 \item[0] requestq
+\item[1] leakq1 (only if VIRTIO_RNG_F_LEAK is offered)
+\item[2] leakq2 (only if VIRTIO_RNG_F_LEAK is offered)
 \end{description}
 
 \subsection{Feature bits}\label{sec:Device Types / Entropy Device / Feature 
bits}
-  None currently defined
+\begin{description}
+\item[VIRTIO_RNG_F_LEAK (0)] Device can report and handle information leaks.
+\end{description}
 
 \subsection{Device configuration layout}\label{sec:Device Types / Entropy 
Device / Device configuration layout}
   None currently defined.
@@ -21,6 +25,7 @@ \subsection{Device Initialization}\label{sec:Device Types / 
Entropy Device / Dev
 
 \begin{enumerate}
 \item The \field{requestq} virtqueue is initialized
+\item If VIRTIO_RNG_F_LEAK has been negotiated, \field{leakq1} and 
\field{leakq2} are initialized
 \end{enumerate}
 
 \subsection{Device Operation}\label{sec:Device Types / Entropy Device / Device 
Operation}
@@ -41,3 +46,57 @@ \subsection{Device Operation}\label{sec:Device Types / 
Entropy Device / Device O
 The device MUST place one or more random bytes into the buffer
 made available to it through \field{requestq}, but it
 MAY use less than the entire buffer length.
+
+\subsubsection{Reporting Information Leaks}{Device Types / Entropy Device / 
Device Operation / Reporting Information Leaks}
+
+The device might, after the fact, detect that some of the entropy
+it supplied to the driver has after the fact degraded in quality
+or leaked to the outside world.  One example is when the device
+is part of the virtual machine undergoing a restore from snapshot
+operation. Another example is when the information leaks from the
+host system through a side-channel.
+
+The driver would typically react by causing regeneration of any
+information that might have leaked and that has to be secret or
+unique.  It is understood that when an information leak has been
+detected it is likely not limited to the entropy received through
+the specific device. In particular, this is the case for
+snapshoting It is thus suggested that the system fully
+regenerate any unique/secret information in this scenario.
+
+If VIRTIO_RNG_F_LEAK has been negotiated the device can report
+such leaks to the driver through a set of dedicated leak
+queues: \field{leakq1} and \field{leakq2}.
+
+Buffers added to the leak queues can have one of two forms:
+\begin{enumerate}
+\item A write-only buffer. It will be completely filled by random data by the 
device.
+\item A buffer consisting of read-only section followed by a
+write-only section, both of identical size. The
+device will copy data from the read-only section to the write-only
+section.
+\end{enumerate}
+
+The steps for operating the virtqueue are:
+
+\begin{enumerate}
+\item At each time, only one of \field{leakq1}, \field{leakq2} is active
+  (has buffers added/used).
+\item After initialization, \field{leakq1} is active.
+\item Driver adds multiple buffers to the active leak queue.
+\item The buffers are not used until an information leak is
+  detected, as long as that is the case driver can
+  add more buffers to the active queue.
+\item Upon detecting an information leak, device starts
+  using buffers in the active leak queue.
+\item Upon detecting that buffers have been used, driver
+  switches to another leak queue making it active
+  (e.g. from \field{leakq1} to \field{leakq2} or vice versa).
+  It then starts adding buffers to the new leak queue.
+\item Device will keep using buffers in the active leak queue
+  until it detects that both the current leak queue is empty and another
+  leak queue has buffers. At that point device switches to
+  another leak queue, making it active.
+\item After the switch, buffers from the new leak queue are not
+  used until an information leak is detected.
+\end{enumerate}
-- 
MST


-
To unsubscribe, e-mail: virtio-dev-unsubscr...@lists.oasis-open.org
For additional commands, e-mail: virtio-dev-h...@lists.oasis-open.org