Minor Coverity xenstore fixes
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(-)
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
@@ -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
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: 1056028Signed-off-by Matthew Daley <[email protected]>
Quoted 21 lines
--- tools/xenstore/xs.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/xenstore/xs.c +6
-0@@ -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 )
PATCH 1/2 xenstore: sanity check incoming message body lengths
In reply to Matthew DaleyMatthew 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.
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
PATCH 1/2 xenstore: sanity check incoming message body lengths [and 2 more messages]
In reply to Matthew DaleyMatthew 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
Reviewed-by Andrew Cooper <[email protected]>
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_messageI can see areread_reply,read_watch_internalandread_thread. read_reply's only caller isxs_talkv, which closes the connection on the failure being passed down.read_watch_internaldoesn't, and neither do its callers.read_threaddoes 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? Sinceread_messageis the root function for all these other functions, and sinceread_threadalready 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.
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
Reviewed-by Andrew Cooper <[email protected]>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_messageI can see areread_reply,read_watch_internalandread_thread. read_reply's only caller isxs_talkv, which closes the connection on the failure being passed down.read_watch_internaldoesn't, and neither do its callers.read_threaddoes 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? Sinceread_messageis the root function for all these other functions, and sinceread_threadalready 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.
Ian Jackson
Acked-byRe: [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.
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
@@ -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
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
@@ -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
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
Signed-off-by Matthew Daley <[email protected]>
Quoted 56 lines
---
v2: return false -> gotoout_falsetools/xenstore/xs.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)tools/xenstore/xs.c +7
-9@@ -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; } }
PATCH v2 2/2 xenstore: check F_SETFL fcntl invocation in setnonblock
In reply to Matthew DaleyMatthew 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
-3if (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.
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_SETFLfcntl 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_SETFLreturns 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.
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_SETFLreturns 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.
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
@@ -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
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
Signed-off-by Matthew Daley <[email protected]>---
v2: Use a constant value for the final setnonblock return, instead of a side-effecting expressiontools/xenstore/xs.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)tools/xenstore/xs.c +10
-10@@ -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
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.
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(-)