Skip to contentSkip to conversation

xenfs: race condition on xenstore watch

PATCH Jonathan Davies 3 participants 7 messages May 15, 2013 – Jun 3, 2013 Xen hypervisor interface

lore
Share & tools
lore
#0 lore raw

Dear xen-devel,

There's a race condition in xenfs (xenstore driver) that causes userspace utility xenstore-watch to crash.

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Here's what is happening:

The userspace process xenstore-watch writes to /proc/xen/xenbus with msg_type==XS_WATCH. This is handled by xenbus_write_watch which calls register_xenbus_watch with watch_fired as a callback *before* acquiring the reply_mutex and sending the synthesised "OK" reply.

This gives a fast xenstore the opportunity to cause the watch_fired to run (and briefly grab the reply_mutex for itself) before the fake "OK" message is sent.

Below, I've included a putative patch for pre-3.3 xenfs that fixes this problem. (It looks like the patch would also apply cleanly to 3.3-onwards xenbus_dev_frontend.c, but I haven't tried.) Any comments about whether this is a reasonable approach?

A cursory glance at drivers/xen/xenbus/xenbus_dev.c suggests that it suffers from the same problem. Although I haven't haven't tested this, I'd expect that it requires a very similar solution.

Jonathan

Take the (non-reentrant) reply_mutex before calling register_xenbus_watch to prevent the watch_fired callback from writing anything until the "OK" has been sent.

drivers/xen/xenfs/xenbus.c +3 -2
diff -r 5ab1b4af1faf drivers/xen/xenfs/xenbus.c
@@ -359,6 +359,8 @@ static int xenbus_write_watch(unsigned m
 	}
 	token++;
 
+	mutex_lock(&u->reply_mutex);
+
 	if (msg_type == XS_WATCH) {
 		watch = alloc_watch_adapter(path, token);
 		if (watch == NULL) {
@@ -401,12 +403,11 @@ static int xenbus_write_watch(unsigned m
 			"OK"
 		};
 
-		mutex_lock(&u->reply_mutex);
 		rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
-		mutex_unlock(&u->reply_mutex);
 	}
 
 out:
+	mutex_unlock(&u->reply_mutex);
 	return rc;
 }
#1 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line · Jonathan Davies

The userspace process xenstore-watch writes to /proc/xen/xenbus with msg_type==XS_WATCH. This is handled by xenbus_write_watch which calls register_xenbus_watch with watch_fired as a callback *before* acquiring the reply_mutex and sending the synthesised "OK" reply.

Quoted 8 lines

This gives a fast xenstore the opportunity to cause the watch_fired to run (and briefly grab the reply_mutex for itself) before the fake "OK" message is sent.

Below, I've included a putative patch for pre-3.3 xenfs that fixes this problem. (It looks like the patch would also apply cleanly to 3.3-onwards xenbus_dev_frontend.c, but I haven't tried.) Any comments about whether this is a reasonable approach?

Yes, this looks reasonable at a first glance, pending confirmation by someone involved in the original xenbus/xenstore design that the expectation to see the watch firing only after the OK response is a valid one.

There's an implementation problem though:

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m · line 363

Quoted 8 lines
drivers/xen/xenfs/xenbus.c +2 -0
@@ -359,6 +359,8 @@ static int xenbus_write_watch(unsigned m
  	}
  	token++;
 
+	mutex_lock(&u->reply_mutex);
+

Right above the initial patch context there is another "goto out", so ...

Quoted 4 lines
drivers/xen/xenfs/xenbus.c +0 -0
  	if (msg_type == XS_WATCH) {
  		watch = alloc_watch_adapter(path, token);
  		if (watch == NULL) {

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m · line 410

Quoted 10 lines
drivers/xen/xenfs/xenbus.c +1 -2
@@ -401,12 +403,11 @@ static int xenbus_write_watch(unsigned m
  			"OK"
  		};
 
-		mutex_lock(&u->reply_mutex);
  		rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
-		mutex_unlock(&u->reply_mutex);
  	}
 
  out:
+	mutex_unlock(&u->reply_mutex);

... this is not valid.

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m

Quoted 3 lines
drivers/xen/xenfs/xenbus.c +0 -0
  	return rc;
  }
 

Thus the unlock needs to become conditional here (either via tracking the state in a local variable, or via adding a second label, or via replacing the first goto with a straight return).

I'd also recommend to shrink the protected region to the minimum possible (i.e. acquire the mutex right before the call to register_xenbus_watch(), and at the end of the "else" body). Since you then would end up with only a single error path needing to drop the lock, dropping it in that error path rather than moving it past the "out" label might be preferable.

Jan

#2 lore raw
In reply to Jonathan Davies
Quoted 18 lines · Jonathan Davies

Dear xen-devel,

There's a race condition in xenfs (xenstore driver) that causes userspace utility xenstore-watch to crash.

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Here's what is happening:

The userspace process xenstore-watch writes to /proc/xen/xenbus with msg_type==XS_WATCH. This is handled by xenbus_write_watch which calls register_xenbus_watch with watch_fired as a callback *before* acquiring the reply_mutex and sending the synthesised "OK" reply.

Quoted 8 lines

This gives a fast xenstore the opportunity to cause the watch_fired to run (and briefly grab the reply_mutex for itself) before the fake "OK" message is sent.

Below, I've included a putative patch for pre-3.3 xenfs that fixes this problem. (It looks like the patch would also apply cleanly to 3.3-onwards xenbus_dev_frontend.c, but I haven't tried.) Any comments about whether this is a reasonable approach?

It can't apply cleanly as the file moved :-(

Quoted 11 lines

A cursory glance at drivers/xen/xenbus/xenbus_dev.c suggests that it suffers from the same problem. Although I haven't haven't tested this, I'd expect that it requires a very similar solution.

Jonathan

Take the (non-reentrant) reply_mutex before calling register_xenbus_watch to prevent the watch_fired callback from writing anything until the "OK" has been sent.

Should that perhaps be done inside the msg_type == XS_WATCH code (with a bool that would determine whether an reply_mutex has been taken?)

As in, is there no need to take this mutex if msg_type != XS_WATCH?

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m · line 363

Quoted 12 lines

diff -r 5ab1b4af1faf drivers/xen/xenfs/xenbus.c

drivers/xen/xenfs/xenbus.c +2 -0
@@ -359,6 +359,8 @@ static int xenbus_write_watch(unsigned m
 	}
 	token++;
 
+	mutex_lock(&u->reply_mutex);
+

Have you tested this with the kernel compiled with DEBUG_MUTEX and DEBUG_PROVE_LOCKING to make sure there are no mutex/spinlock issues?

Quoted 23 lines
drivers/xen/xenfs/xenbus.c +1 -2
 	if (msg_type == XS_WATCH) {
 		watch = alloc_watch_adapter(path, token);
 		if (watch == NULL) {
@@ -401,12 +403,11 @@ static int xenbus_write_watch(unsigned m
 			"OK"
 		};
 
-		mutex_lock(&u->reply_mutex);
 		rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
-		mutex_unlock(&u->reply_mutex);
 	}
 
 out:
+	mutex_unlock(&u->reply_mutex);
 	return rc;
 }
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Konrad Rzeszutek Wilk
Quoted 5 lines · Jonathan Davies

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Now that I look at this another time, I wonder how this behavior can be guaranteed even with your patch: xenbus_write_watch() sends the reply by calling queue_reply() followed by wake_up(), so the reply getting delivered and the watch firing appear to be inherently asynchronous anyway, and your patch just reduces the race window. Am I overlooking something here?

Quoted 8 lines
Quoted 3 lines

Take the (non-reentrant) reply_mutex before calling register_xenbus_watch to prevent the watch_fired callback from writing anything until the "OK" has been sent.

Should that perhaps be done inside the msg_type == XS_WATCH code (with a bool that would determine whether an reply_mutex has been taken?)

As in, is there no need to take this mutex if msg_type != XS_WATCH?

As said in an earlier reply, I also think that it would be preferable to shrink the locked region as much as possible. But there clearly is a need to also acquire the mutex for msg_type != XS_WATCH, just not before the loop in the else branch.

Jan

#6 lore raw
In reply to Konrad Rzeszutek Wilk
Quoted 23 lines · Konrad Rzeszutek Wilk
Quoted 22 lines · Jonathan Davies

Dear xen-devel,

There's a race condition in xenfs (xenstore driver) that causes userspace utility xenstore-watch to crash.

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Here's what is happening:

The userspace process xenstore-watch writes to /proc/xen/xenbus with msg_type==XS_WATCH. This is handled by xenbus_write_watch which calls register_xenbus_watch with watch_fired as a callback *before* acquiring the reply_mutex and sending the synthesised "OK" reply.

This gives a fast xenstore the opportunity to cause the watch_fired to run (and briefly grab the reply_mutex for itself) before the fake "OK" message is sent.

Quoted 6 lines
Quoted 4 lines

Below, I've included a putative patch for pre-3.3 xenfs that fixes this problem. (It looks like the patch would also apply cleanly to 3.3-onwards xenbus_dev_frontend.c, but I haven't tried.) Any comments about whether this is a reasonable approach?

It can't apply cleanly as the file moved :-(

Well, it applies cleanly if you rename the affected file to drivers/xen/xenbus/xenbus_dev_frontend.c and can tolerate a 15-line offset... :-)

Quoted 7 lines
Quoted 7 lines

A cursory glance at drivers/xen/xenbus/xenbus_dev.c suggests that it suffers from the same problem. Although I haven't haven't tested this, I'd expect that it requires a very similar solution.

Jonathan

Quoted 8 lines
Quoted 3 lines

Take the (non-reentrant) reply_mutex before calling register_xenbus_watch to prevent the watch_fired callback from writing anything until the "OK" has been sent.

Should that perhaps be done inside the msg_type == XS_WATCH code (with a bool that would determine whether an reply_mutex has been taken?)

As in, is there no need to take this mutex if msg_type != XS_WATCH?

When msg_type != XS_WATCH, we only need to take the mutex briefly when sending the "OK". There's no issue with XS_UNWATCH because there's never anything further to be written. v2 of the patch won't hold the lock while doing the work for XS_UNWATCH.

Quoted 9 lines
Quoted 9 lines

diff -r 5ab1b4af1faf drivers/xen/xenfs/xenbus.c

drivers/xen/xenfs/xenbus.c +0 -0
@@ -359,6 +359,8 @@ static int xenbus_write_watch(unsigned m
  	}
  	token++;
 
Quoted 5 lines

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m · line 363

Quoted 2 lines
drivers/xen/xenfs/xenbus.c +2 -0
+	mutex_lock(&u->reply_mutex);
+

Have you tested this with the kernel compiled with DEBUG_MUTEX and DEBUG_PROVE_LOCKING to make sure there are no mutex/spinlock issues?

No. I will give that a try.

Quoted 12 lines
Quoted 12 lines
drivers/xen/xenfs/xenbus.c +0 -2
  	if (msg_type == XS_WATCH) {
  		watch = alloc_watch_adapter(path, token);
  		if (watch == NULL) {
@@ -401,12 +403,11 @@ static int xenbus_write_watch(unsigned m
  			"OK"
  		};
 
-		mutex_lock(&u->reply_mutex);
  		rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
-		mutex_unlock(&u->reply_mutex);
  	}
 
Quoted 4 lines

drivers/xen/xenfs/xenbus.c static int xenbus_write_watch(unsigned m · line 412

Quoted 4 lines
drivers/xen/xenfs/xenbus.c +1 -0
  out:
+	mutex_unlock(&u->reply_mutex);
  	return rc;
  }

Thanks for the feedback,
Jonathan

#4 lore raw
In reply to Jan Beulich
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Konrad Rzeszutek Wilk
Quoted 5 lines · Jonathan Davies

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Quoted 6 lines

Now that I look at this another time, I wonder how this behavior can be guaranteed even with your patch: xenbus_write_watch() sends the reply by calling queue_reply() followed by wake_up(), so the reply getting delivered and the watch firing appear to be inherently asynchronous anyway, and your patch just reduces the race window. Am I overlooking something here?

There's no call to wake_up() in xenbus_write_watch(), as far as I can see...

The key thing is to guarantee that xenbus_write_watch()'s call to queue_reply() (for the "OK" message) executes before the watch_fired() callback -- registered by the call to register_xenbus_watch() -- could be called-back. Given that watch_fired() also takes the reply_mutex before its own calls to queue_reply() and wake_up(), this behaviour can be guaranteed by taking the reply_mutex before registering the callback.

Quoted 11 lines
Quoted 8 lines
Quoted 3 lines

Take the (non-reentrant) reply_mutex before calling register_xenbus_watch to prevent the watch_fired callback from writing anything until the "OK" has been sent.

Should that perhaps be done inside the msg_type == XS_WATCH code (with a bool that would determine whether an reply_mutex has been taken?)

As in, is there no need to take this mutex if msg_type != XS_WATCH?

As said in an earlier reply, I also think that it would be preferable to shrink the locked region as much as possible.

v2 of the patch will have a much tighter locked region. I'll post that once I've tested it.

Thanks,
Jonathan

#5 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · Jonathan Davies
Quoted 8 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · Konrad Rzeszutek Wilk
Quoted 5 lines · Jonathan Davies

Normally, the userspace process gets an "OK" from xenfs and then the watch fires immediately after. Occasionally, this happens the other way
around: the watch fires before the driver sends "OK", which confuses the xenstore-watch client. It seems to me that the client is within its rights to expect the "OK" first.

Quoted 8 lines
Quoted 6 lines

Now that I look at this another time, I wonder how this behavior can be guaranteed even with your patch: xenbus_write_watch() sends the reply by calling queue_reply() followed by wake_up(), so the reply getting delivered and the watch firing appear to be inherently asynchronous anyway, and your patch just reduces the race window. Am I overlooking something here?

There's no call to wake_up() in xenbus_write_watch(), as far as I can see...

I'm not sure where you're looking, but both xenfs/xenbus.c and the more recent xenbus/xenbus_dev_frontend.c have this

		mutex_lock(&u->reply_mutex);
		rc = queue_reply(&u->read_buffers, &reply, sizeof(reply));
		wake_up(&u->read_waitq);
		mutex_unlock(&u->reply_mutex);

near the end of xenbus_write_watch().

Quoted 6 lines

The key thing is to guarantee that xenbus_write_watch()'s call to queue_reply() (for the "OK" message) executes before the watch_fired() callback -- registered by the call to register_xenbus_watch() -- could be called-back. Given that watch_fired() also takes the reply_mutex before its own calls to queue_reply() and wake_up(), this behaviour can be guaranteed by taking the reply_mutex before registering the callback.

Okay, I agree after taking another closer look - the very different variable naming obfuscate this a little. And inn fact the mutex appears to get acquired too early in watch_fired - it could be confined to just the list_splice_tail() (and maybe the wake_up(), if the really requires external synchronization) invocation.

Jan