Skip to contentSkip to conversation

Minor Coverity xenstore fixes

PATCH In review Matthew Daley 4 participants 18 messages Nov 30, 2013 – Dec 19, 2013

lore
Share & tools
lore
#0 lore raw

Once these and Andrew's final xenstored have gone in, there should be no more xenstore CIDs.

 tools/xenstore/xs.c |   22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
#1 lore raw

PATCH 1/2 xenstore: sanity check incoming message body lengths

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

Coverity-ID: 1055449
Coverity-ID: 1056028

---
 tools/xenstore/xs.c |    6 ++++++
 1 file changed, 6 insertions(+)
tools/xenstore/xs.c +6 -0
index 261b841..184886f 100644
@@ -1145,6 +1145,12 @@ static int read_message(struct xs_handle *h, int nonblocking)
 		goto error_freemsg;
 	}
 
+	/* Sanity check message body length. */
+	if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
+		saved_errno = E2BIG;
+		goto error_freemsg;
+	}
+
 	/* Allocate and read the message body. */
 	body = msg->body = malloc(msg->hdr.len + 1);
 	if (body == NULL)
Signature · 1 line
1.7.10.4
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths

Quoted 6 lines · Matthew Daley

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

Coverity-ID: 1055449
Coverity-ID: 1056028

Quoted 21 lines
---
 tools/xenstore/xs.c |    6 ++++++
 1 file changed, 6 insertions(+)
tools/xenstore/xs.c +6 -0
index 261b841..184886f 100644
@@ -1145,6 +1145,12 @@ static int read_message(struct xs_handle *h, int nonblocking)
 		goto error_freemsg;
 	}
 
+	/* Sanity check message body length. */
+	if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
+		saved_errno = E2BIG;
+		goto error_freemsg;
+	}
+
 	/* Allocate and read the message body. */
 	body = msg->body = malloc(msg->hdr.len + 1);
 	if (body == NULL)
#3 lore raw

PATCH 1/2 xenstore: sanity check incoming message body lengths

In reply to Matthew Daley

Matthew Daley writes ("[PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 2 lines

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

...

tools/xenstore/xs.c static int read_message(struct xs_handle *h, int nonblocking) · line 1152

Quoted 5 lines
tools/xenstore/xs.c +5 -0
+	/* Sanity check message body length. */
+	if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
+		saved_errno = E2BIG;
+		goto error_freemsg;
+	}

If this situation should arise, your proposal would discard the headers of the bogus message and read the start of what would be the over-long payload as the next header.

Unfortunately, it looks like the existing code already does exactly this if it experiences a malloc failure !

It would be best to either kill the connection dead, or perhaps to skip the overlong data.

Ian.

#4 lore raw

Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths

Quoted 14 lines · Ian Jackson

Matthew Daley writes ("[PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 2 lines

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

...

tools/xenstore/xs.c static int read_message(struct xs_handle *h, int nonblocking) · line 1152

Quoted 5 lines
tools/xenstore/xs.c +5 -0
+     /* Sanity check message body length. */
+     if (msg->hdr.len > XENSTORE_PAYLOAD_MAX) {
+             saved_errno = E2BIG;
+             goto error_freemsg;
+     }

If this situation should arise, your proposal would discard the headers of the bogus message and read the start of what would be the over-long payload as the next header.

Quoted 5 lines

Unfortunately, it looks like the existing code already does exactly this if it experiences a malloc failure !

It would be best to either kill the connection dead, or perhaps to skip the overlong data.

The only callers of read_message I can see are read_reply, read_watch_internal and read_thread. read_reply's only caller is xs_talkv, which closes the connection on the failure being passed down. read_watch_internal doesn't, and neither do its callers. read_thread does close the connection.

So, with that said, where should the handling of the failure go? Would it be good to consolidate the handling in one spot, ie. directly in read_message? Since read_message is the root function for all these other functions, and since read_thread already does handle the failure and the other functions use that if implicitly if threaded background reading is enabled, it seems like a good idea to do so.

- Matthew

#5 lore raw

PATCH 1/2 xenstore: sanity check incoming message body lengths [and 2 more messages]

In reply to Matthew Daley

Matthew Daley writes ("[PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 2 lines

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

Andrew Cooper writes ("Re: [Xen-devel] [PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 1 line

Matthew Daley writes ("Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 4 lines · Ian Jackson
Quoted 3 lines

If this situation should arise, your proposal would discard the headers of the bogus message and read the start of what would be the over-long payload as the next header.

...

Quoted 9 lines
Quoted 2 lines

It would be best to either kill the connection dead, or perhaps to skip the overlong data.

The only callers of read_message I can see are read_reply, read_watch_internal and read_thread. read_reply's only caller is xs_talkv, which closes the connection on the failure being passed down. read_watch_internal doesn't, and neither do its callers. read_thread does close the connection.

Quoted 6 lines

So, with that said, where should the handling of the failure go? Would it be good to consolidate the handling in one spot, ie. directly in read_message? Since read_message is the root function for all these other functions, and since read_thread already does handle the failure and the other functions use that if implicitly if threaded background reading is enabled, it seems like a good idea to do so.

Yes.

Arguably, based on what you've said, this patch should be applied as-is, on the grounds of it being an improvement. Ian C, do you have an opinion ?

Ian.

#6 lore raw

Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths [and 2 more messages]

Quoted 29 lines · Ian Jackson

Matthew Daley writes ("[PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 2 lines

This is for the client-side receiving messages from xenstored, so there is no security impact, unlike XSA-72.

Andrew Cooper writes ("Re: [Xen-devel] [PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 1 line

Matthew Daley writes ("Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths"):

Quoted 4 lines · Ian Jackson
Quoted 3 lines

If this situation should arise, your proposal would discard the headers of the bogus message and read the start of what would be the over-long payload as the next header.

...

Quoted 9 lines
Quoted 2 lines

It would be best to either kill the connection dead, or perhaps to skip the overlong data.

The only callers of read_message I can see are read_reply, read_watch_internal and read_thread. read_reply's only caller is xs_talkv, which closes the connection on the failure being passed down. read_watch_internal doesn't, and neither do its callers. read_thread does close the connection.

Quoted 6 lines

So, with that said, where should the handling of the failure go? Would it be good to consolidate the handling in one spot, ie. directly in read_message? Since read_message is the root function for all these other functions, and since read_thread already does handle the failure and the other functions use that if implicitly if threaded background reading is enabled, it seems like a good idea to do so.

Quoted 5 lines

Yes.

Arguably, based on what you've said, this patch should be applied as-is, on the grounds of it being an improvement. Ian C, do you have an opinion ?

I don't see the harm in taking this, it is an improvement.

Ian.

Acked-by
#7 lore raw

Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths [and 2 more messages]

Ian Campbell writes ("Re: [PATCH 1/2] xenstore: sanity check incoming message body lengths [and 2 more messages]"):

Quoted 6 lines · Ian Jackson
Quoted 3 lines

Arguably, based on what you've said, this patch should be applied as-is, on the grounds of it being an improvement. Ian C, do you have an opinion ?

I don't see the harm in taking this, it is an improvement.

Thanks for the opinion.

Also queued for backport.

Ian.

#8 lore raw

PATCH 2/2 xenstore: check F_SETFL fcntl invocation in setnonblock

In reply to Matthew Daley

...and check the newly-added result of setnonblock itself where used.

Coverity-ID: 1055103

---
 tools/xenstore/xs.c |   16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
tools/xenstore/xs.c +7 -9
index 184886f..abac788 100644
@@ -146,20 +146,17 @@ struct xs_handle {
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
 
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	return fcntl(fd, F_SETFL, flags) != -1;
 }
 
 int xs_fileno(struct xs_handle *h)
@@ -369,8 +366,8 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 	if (!len)
 		return true;
 
-	if (nonblocking)
-		setnonblock(fd, 1);
+	if (nonblocking && !setnonblock(fd, 1))
+		return false;
 
 	while (len) {
 		int done;
@@ -390,8 +387,9 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 		len -= done;
 
 		if (nonblocking) {
-			setnonblock(fd, 0);
 			nonblocking = 0;
+			if (!setnonblock(fd, 0))
+				return false;
 		}
 	}
 
Signature · 1 line
1.7.10.4
#9 lore raw

PATCH v2 2/2 xenstore: check F_SETFL fcntl invocation in setnonblock

...and check the newly-added result of setnonblock itself where used.

Coverity-ID: 1055103

---
v2: return false -> goto out_false

 tools/xenstore/xs.c |   16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
tools/xenstore/xs.c +7 -9
index 184886f..504d524 100644
@@ -146,20 +146,17 @@ struct xs_handle {
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
 
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	return fcntl(fd, F_SETFL, flags) != -1;
 }
 
 int xs_fileno(struct xs_handle *h)
@@ -369,8 +366,8 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 	if (!len)
 		return true;
 
-	if (nonblocking)
-		setnonblock(fd, 1);
+	if (nonblocking && !setnonblock(fd, 1))
+		return false;
 
 	while (len) {
 		int done;
@@ -390,8 +387,9 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 		len -= done;
 
 		if (nonblocking) {
-			setnonblock(fd, 0);
 			nonblocking = 0;
+			if (!setnonblock(fd, 0))
+				goto out_false;
 		}
 	}
 
Signature · 1 line
1.7.10.4
Reviewed-by on v2 2/2
#10 lore raw

Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock

Quoted 4 lines · Matthew Daley

...and check the newly-added result of setnonblock itself where used.

Coverity-ID: 1055103

Quoted 56 lines

---
v2: return false -> goto out_false

 tools/xenstore/xs.c |   16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
tools/xenstore/xs.c +7 -9
index 184886f..504d524 100644
@@ -146,20 +146,17 @@ struct xs_handle {
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
 
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	return fcntl(fd, F_SETFL, flags) != -1;
 }
 
 int xs_fileno(struct xs_handle *h)
@@ -369,8 +366,8 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 	if (!len)
 		return true;
 
-	if (nonblocking)
-		setnonblock(fd, 1);
+	if (nonblocking && !setnonblock(fd, 1))
+		return false;
 
 	while (len) {
 		int done;
@@ -390,8 +387,9 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 		len -= done;
 
 		if (nonblocking) {
-			setnonblock(fd, 0);
 			nonblocking = 0;
+			if (!setnonblock(fd, 0))
+				goto out_false;
 		}
 	}
 
#11 lore raw

PATCH v2 2/2 xenstore: check F_SETFL fcntl invocation in setnonblock

In reply to Matthew Daley

Matthew Daley writes ("[PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock"):

Quoted 1 line

...and check the newly-added result of setnonblock itself where used.

...

Quoted 7 lines
tools/xenstore/xs.c +2 -3
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;

tools/xenstore/xs.c struct xs_handle { · line 159

Quoted 10 lines
tools/xenstore/xs.c +1 -3
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
 
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	return fcntl(fd, F_SETFL, flags) != -1;

fcntl F_SETFL returns 0 on success and -1 or error. But your setnonblock is supposed to return 1 on success and 0 on error.

Thanks,
Ian.

#12 lore raw

Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock

Quoted 16 lines · Ian Jackson

Matthew Daley writes ("[PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock"):

Quoted 1 line

...and check the newly-added result of setnonblock itself where used.

...

Quoted 13 lines
tools/xenstore/xs.c +2 -3
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
Quoted 7 lines

tools/xenstore/xs.c struct xs_handle { · line 159

Quoted 4 lines
tools/xenstore/xs.c +1 -3
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	return fcntl(fd, F_SETFL, flags) != -1;

fcntl F_SETFL returns 0 on success and -1 or error. But your setnonblock is supposed to return 1 on success and 0 on error.

The trailing "!= -1" should make the the case, shouldn't it?

Ian.

#13 lore raw

Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock

Ian Campbell writes ("Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock"):

Quoted 7 lines · Ian Jackson
Quoted 4 lines

tools/xenstore/xs.c struct xs_handle { · line 159

Quoted 1 line
tools/xenstore/xs.c +1 -0
+	return fcntl(fd, F_SETFL, flags) != -1;

fcntl F_SETFL returns 0 on success and -1 or error. But your setnonblock is supposed to return 1 on success and 0 on error.

The trailing "!= -1" should make the the case, shouldn't it?

Oh, I didn't spot that. How horrid. I think I need to reject this on style grounds...

Thanks,
Ian.

#14 lore raw

PATCH v2 2/2 xenstore: check F_SETFL fcntl invocation in setnonblock

...and check the newly-added result of setnonblock itself where used.

Coverity-ID: 1055103

---
v2: Use a constant value for the final setnonblock return, instead of a side-effecting expression

 tools/xenstore/xs.c |   19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tools/xenstore/xs.c +10 -9
index 184886f..a636498 100644
@@ -146,20 +146,20 @@ struct xs_handle {
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
-static void setnonblock(int fd, int nonblock) {
-	int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
 	int flags = fcntl(fd, F_GETFL);
 	if (flags == -1)
-		goto out;
+		return false;
 
 	if (nonblock)
 		flags |= O_NONBLOCK;
 	else
 		flags &= ~O_NONBLOCK;
 
-	fcntl(fd, F_SETFL, flags);
-out:
-	errno = esave;
+	if (fcntl(fd, F_SETFL, flags) == -1)
+		return false;
+
+	return true;
 }
 
 int xs_fileno(struct xs_handle *h)
@@ -369,8 +369,8 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 	if (!len)
 		return true;
 
-	if (nonblocking)
-		setnonblock(fd, 1);
+	if (nonblocking && !setnonblock(fd, 1))
+		return false;
 
 	while (len) {
 		int done;
@@ -390,8 +390,9 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
 		len -= done;
 
 		if (nonblocking) {
-			setnonblock(fd, 0);
 			nonblocking = 0;
+			if (!setnonblock(fd, 0))
+				goto out_false;
 		}
 	}
 
Signature · 1 line
1.7.10.4
#15 lore raw

Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock

Ping?

Quoted 67 lines · Matthew Daley

...and check the newly-added result of setnonblock itself where used.

Coverity-ID: 1055103

---
v2: Use a constant value for the final setnonblock return, instead of a side-effecting expression

 tools/xenstore/xs.c |   19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tools/xenstore/xs.c +10 -10
index 184886f..a636498 100644
@@ -146,20 +146,20 @@ struct xs_handle {
 
 static int read_message(struct xs_handle *h, int nonblocking);
 
-static void setnonblock(int fd, int nonblock) {
-       int esave = errno;
+static bool setnonblock(int fd, int nonblock) {
        int flags = fcntl(fd, F_GETFL);
        if (flags == -1)
-               goto out;
+               return false;
 
        if (nonblock)
                flags |= O_NONBLOCK;
        else
                flags &= ~O_NONBLOCK;
 
-       fcntl(fd, F_SETFL, flags);
-out:
-       errno = esave;
+       if (fcntl(fd, F_SETFL, flags) == -1)
+               return false;
+
+       return true;
 }
 
 int xs_fileno(struct xs_handle *h)
@@ -369,8 +369,8 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
        if (!len)
                return true;
 
-       if (nonblocking)
-               setnonblock(fd, 1);
+       if (nonblocking && !setnonblock(fd, 1))
+               return false;
 
        while (len) {
                int done;
@@ -390,8 +390,9 @@ static bool read_all(int fd, void *data, unsigned int len, int nonblocking)
                len -= done;
 
                if (nonblocking) {
-                       setnonblock(fd, 0);
                        nonblocking = 0;
+                       if (!setnonblock(fd, 0))
+                               goto out_false;
                }
        }
 
--

1.7.10.4

#16 lore raw

Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock

Matthew Daley writes ("Re: [PATCH 2/2 v2] xenstore: check F_SETFL fcntl invocation in setnonblock"):

Quoted 1 line

Ping?

Sorry about that, I have applied this patch (which is actually v3, not v2).

Regards,
Ian.

#17 lore raw
In reply to Matthew Daley
Quoted 2 lines · Matthew Daley

Once these and Andrew's final xenstored have gone in, there should be no more xenstore CIDs.

Correction: Once these and Andrew's final xenstored patch have gone in, there should be no more

Quoted 5 lines

xenstore CIDs in first-party code. There's still a few in the tdb/talloc code, which I'm not sure if we just want to fix, or if it'd be a good idea to look at refreshing our copies from upstream (Samba).

 tools/xenstore/xs.c |   22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)