Skip to contentSkip to conversation

Coverity fixes for python lowlevel libraries

PATCH Superseded Andrew Cooper 3 participants 21 messages Nov 27, 2014 – Dec 16, 2014 Arm/actions semi architecture

lore
Share & tools
lore
#0 lore raw

cover Coverity fixes for python lowlevel libraries

These were initially flagged by the XenServer coverity scanning. It turns out that they were being found by the upstream scan, but had been grouped with Xend and listed as ignored, hiding them from the output. I have adjusted the scan filters to not ignore the lowlevel libraries, and identified the appropriate scan CIDs in the patches

While Xend is certainly dead and gone, XenServer at the very least still has consumers of these python libraries.

Konrad: I am requesting a release ack for this. All 5 issues are bugs with the handling of error cases, rather than with the basic functionality provided. With these changes, Coverity is of the opinion that the python libraries are perfect (0 issues), and I feel this is a worthy position to be in for 4.5

Andrew Cooper (3):
python/xc: Fix multiple issues in pyflask_context_to_sid() python/xc: Fix multiple issues in pyxc_readconsolering() python/xs: Correct the indirection of the NULL xshandle() check

 tools/python/xen/lowlevel/xc/xc.c |   34 +++++++++-------------------------
 tools/python/xen/lowlevel/xs/xs.c |    2 +-
 2 files changed, 10 insertions(+), 26 deletions(-)
#1 lore raw

PATCH 1/3 python/xc: Fix multiple issues in pyflask_context_to_sid()

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

---
 tools/python/xen/lowlevel/xc/xc.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +3 -18
index d95d459..c70b388 100644
@@ -2126,8 +2126,6 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
 {
     xc_interface *xc_handle;
     char *ctx;
-    char *buf;
-    uint32_t len;
     uint32_t sid;
     int ret;
 
@@ -2137,28 +2135,15 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
                                       &ctx) )
         return NULL;
 
-    len = strlen(ctx);
-
-    buf = malloc(len);
-    if (!buf) {
-        errno = -ENOMEM;
-        PyErr_SetFromErrno(xc_error_obj);
-    }
-    
-    memcpy(buf, ctx, len);
-    
     xc_handle = xc_interface_open(0,0,0);
     if (!xc_handle) {
-        free(buf);
         return PyErr_SetFromErrno(xc_error_obj);
     }
-    
-    ret = xc_flask_context_to_sid(xc_handle, buf, len, &sid);
-        
+
+    ret = xc_flask_context_to_sid(xc_handle, ctx, strlen(ctx), &sid);
+
     xc_interface_close(xc_handle);
 
-    free(buf);
-    
     if ( ret != 0 ) {
         errno = -ret;
         return PyErr_SetFromErrno(xc_error_obj);
Signature · 1 line
1.7.10.4
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Quoted 49 lines
---
 tools/python/xen/lowlevel/xc/xc.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +3 -18
index d95d459..c70b388 100644
@@ -2126,8 +2126,6 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
 {
     xc_interface *xc_handle;
     char *ctx;
-    char *buf;
-    uint32_t len;
     uint32_t sid;
     int ret;
 
@@ -2137,28 +2135,15 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
                                       &ctx) )
         return NULL;
 
-    len = strlen(ctx);
-
-    buf = malloc(len);
-    if (!buf) {
-        errno = -ENOMEM;
-        PyErr_SetFromErrno(xc_error_obj);
-    }
-    
-    memcpy(buf, ctx, len);
-    
     xc_handle = xc_interface_open(0,0,0);
     if (!xc_handle) {
-        free(buf);
         return PyErr_SetFromErrno(xc_error_obj);
     }
-    
-    ret = xc_flask_context_to_sid(xc_handle, buf, len, &sid);
-        
+
+    ret = xc_flask_context_to_sid(xc_handle, ctx, strlen(ctx), &sid);
+
     xc_interface_close(xc_handle);
 
-    free(buf);
-    
     if ( ret != 0 ) {
         errno = -ret;
         return PyErr_SetFromErrno(xc_error_obj);
#3 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 15 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 4 lines

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Hmm - I appear to have missed a detail in the description. One of the CIDs is to do with putting a string in a new buffer without a NUL terminator, and passing it as a char* into xc_flask_context_to_sid; the issue being that anyone expecting things like strlen() to work will be in for a nasty shock.

One solution to this was strdup(), but as the duplication was unnecessary anyway, it was easier just to drop it all.

~Andrew

Quoted 51 lines
Quoted 50 lines
---
 tools/python/xen/lowlevel/xc/xc.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +3 -18
index d95d459..c70b388 100644
@@ -2126,8 +2126,6 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
 {
     xc_interface *xc_handle;
     char *ctx;
-    char *buf;
-    uint32_t len;
     uint32_t sid;
     int ret;
 
@@ -2137,28 +2135,15 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
                                       &ctx) )
         return NULL;
 
-    len = strlen(ctx);
-
-    buf = malloc(len);
-    if (!buf) {
-        errno = -ENOMEM;
-        PyErr_SetFromErrno(xc_error_obj);
-    }
-    
-    memcpy(buf, ctx, len);
-    
     xc_handle = xc_interface_open(0,0,0);
     if (!xc_handle) {
-        free(buf);
         return PyErr_SetFromErrno(xc_error_obj);
     }
-    
-    ret = xc_flask_context_to_sid(xc_handle, buf, len, &sid);
-        
+
+    ret = xc_flask_context_to_sid(xc_handle, ctx, strlen(ctx), &sid);
+
     xc_interface_close(xc_handle);
 
-    free(buf);
-    
     if ( ret != 0 ) {
         errno = -ret;
         return PyErr_SetFromErrno(xc_error_obj);
#4 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 8 lines
Quoted 2 lines

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Hmm - I appear to have missed a detail in the description. One of the CIDs is to do with putting a string in a new buffer without a NUL terminator, and passing it as a char* into xc_flask_context_to_sid; the issue being that anyone expecting things like strlen() to work will be in for a nasty shock.

ISTR a discussion of this interface in Julien's device-tree passthrough thing a while back and some sort of conclusion that the hypervisor was supposed to use the len field and not rely on the NULL.

I'm not sure that necessarily invalidates what you are saying, since even given that throwing a NULL on the end would be friendly to libxc consumers if nothing else.

Ian.

#5 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

In reply to Ian Campbell
Quoted 16 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 4 lines

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Konrad, given
http://article.gmane.org/gmane.comp.emulators.xen.devel/224881 does this have a release ack?

Ian.

#6 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 18 lines · Ian Campbell
Quoted 17 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 8 lines
Quoted 2 lines

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Konrad, given
http://article.gmane.org/gmane.comp.emulators.xen.devel/224881 does this have a release ack?

Ian.

I can resubmit with a clearer description if that would help clarity, but the code is correct for the fixes (not fantastically well) described.

~Andrew

#7 lore raw

Re: [PATCH for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 25 lines · Andrew Cooper
Quoted 24 lines · Ian Campbell
Quoted 15 lines · Ian Campbell
Quoted 8 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Furthermore, the context string is simply an input parameter to the hypercall, and is not mutated anywhere along the way. The error handling elsewhere in the function can be simplified by not duplicating it to start with.

Quoted 6 lines

Coverity-IDs: 1055305 1055721
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 4 lines

This would have been far more obviously correct for 4.5 if you had stuck to fixing the issue in the first paragraph.

Konrad, given
http://article.gmane.org/gmane.comp.emulators.xen.devel/224881 does this have a release ack?

Quoted 5 lines
Quoted 2 lines

Ian.

I can resubmit with a clearer description if that would help clarity, but the code is correct for the fixes (not fantastically well) described.

Please do - that is all I was waiting for. Thank you.

Quoted 2 lines

~Andrew

#8 lore raw

PATCH v2 1/3 python/xc: Fix multiple issues in pyflask_context_to_sid()

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Coverity also complains about passing a non-NUL terminated string to xc_flask_context_to_sid(). xc_flask_context_to_sid() doesn't actually take a NUL terminated string, but it does take a char* which, in context, used to be a string, which is why Coverity complains.

One solution would be to use strdup(ctx) which is simpler than a strlen()/malloc()/memcpy() combo, which would result in a NUL-terminated string being used with xc_flask_context_to_sid().

However, ctx is strictly an input to the hypercall and is not mutated along the way. Both these issues can be fixed, and the error logic simplified, by not duplicating ctx in the first place.

Coverity-IDs: 1055305 1055721

CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Xen Coverity Team <[email protected]>

---
v2: Expand the commit message. No code change

---
 tools/python/xen/lowlevel/xc/xc.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +3 -18
index f83e33d..2aa0dc7 100644
@@ -2125,8 +2125,6 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
 {
     xc_interface *xc_handle;
     char *ctx;
-    char *buf;
-    uint32_t len;
     uint32_t sid;
     int ret;
 
@@ -2136,28 +2134,15 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
                                       &ctx) )
         return NULL;
 
-    len = strlen(ctx);
-
-    buf = malloc(len);
-    if (!buf) {
-        errno = -ENOMEM;
-        PyErr_SetFromErrno(xc_error_obj);
-    }
-    
-    memcpy(buf, ctx, len);
-    
     xc_handle = xc_interface_open(0,0,0);
     if (!xc_handle) {
-        free(buf);
         return PyErr_SetFromErrno(xc_error_obj);
     }
-    
-    ret = xc_flask_context_to_sid(xc_handle, buf, len, &sid);
-        
+
+    ret = xc_flask_context_to_sid(xc_handle, ctx, strlen(ctx), &sid);
+
     xc_interface_close(xc_handle);
 
-    free(buf);
-    
     if ( ret != 0 ) {
         errno = -ret;
         return PyErr_SetFromErrno(xc_error_obj);
Signature · 1 line
1.7.10.4
#9 lore raw

Re: [PATCH v2 for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 18 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Coverity also complains about passing a non-NUL terminated string to xc_flask_context_to_sid(). xc_flask_context_to_sid() doesn't actually take a NUL terminated string, but it does take a char* which, in context, used to be a string, which is why Coverity complains.

One solution would be to use strdup(ctx) which is simpler than a strlen()/malloc()/memcpy() combo, which would result in a NUL-terminated string being used with xc_flask_context_to_sid().

However, ctx is strictly an input to the hypercall and is not mutated along the way. Both these issues can be fixed, and the error logic simplified, by not duplicating ctx in the first place.

Quoted 8 lines

Coverity-IDs: 1055305 1055721

CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Xen Coverity Team <[email protected]>

---
v2: Expand the commit message. No code change

Thank you.

Quoted 52 lines
---
 tools/python/xen/lowlevel/xc/xc.c |   21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +3 -18
index f83e33d..2aa0dc7 100644
@@ -2125,8 +2125,6 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
 {
     xc_interface *xc_handle;
     char *ctx;
-    char *buf;
-    uint32_t len;
     uint32_t sid;
     int ret;
 
@@ -2136,28 +2134,15 @@ static PyObject *pyflask_context_to_sid(PyObject *self, PyObject *args,
                                       &ctx) )
         return NULL;
 
-    len = strlen(ctx);
-
-    buf = malloc(len);
-    if (!buf) {
-        errno = -ENOMEM;
-        PyErr_SetFromErrno(xc_error_obj);
-    }
-    
-    memcpy(buf, ctx, len);
-    
     xc_handle = xc_interface_open(0,0,0);
     if (!xc_handle) {
-        free(buf);
         return PyErr_SetFromErrno(xc_error_obj);
     }
-    
-    ret = xc_flask_context_to_sid(xc_handle, buf, len, &sid);
-        
+
+    ret = xc_flask_context_to_sid(xc_handle, ctx, strlen(ctx), &sid);
+
     xc_interface_close(xc_handle);
 
-    free(buf);
-    
     if ( ret != 0 ) {
         errno = -ret;
         return PyErr_SetFromErrno(xc_error_obj);
Signature · 1 line
1.7.10.4
Applied
#10 lore raw

Re: [PATCH v2 for-4.5 1/3] python/xc: Fix multiple issues in pyflask_context_to_sid()

Quoted 25 lines · Konrad Rzeszutek Wilk
Quoted 24 lines · Andrew Cooper

The error handling from a failed memory allocation should return PyErr_SetFromErrno(xc_error_obj); rather than simply calling it and continuing to the memcpy() below, with the dest pointer being NULL.

Coverity also complains about passing a non-NUL terminated string to xc_flask_context_to_sid(). xc_flask_context_to_sid() doesn't actually take a NUL terminated string, but it does take a char* which, in context, used to be a string, which is why Coverity complains.

One solution would be to use strdup(ctx) which is simpler than a strlen()/malloc()/memcpy() combo, which would result in a NUL-terminated string being used with xc_flask_context_to_sid().

However, ctx is strictly an input to the hypercall and is not mutated along the way. Both these issues can be fixed, and the error logic simplified, by not duplicating ctx in the first place.

Coverity-IDs: 1055305 1055721

CC Ian Jackson <[email protected]>
CC Wei Liu <[email protected]>
CC Xen Coverity Team <[email protected]>
Quoted 7 lines
Quoted 2 lines

---
v2: Expand the commit message. No code change

Thank you.

Applied.

#11 lore raw

PATCH 2/3 python/xc: Fix multiple issues in pyxc_readconsolering()

In reply to Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

---
 tools/python/xen/lowlevel/xc/xc.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +6 -7
index c70b388..2aa0dc7 100644
@@ -1089,7 +1089,7 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 {
     unsigned int clear = 0, index = 0, incremental = 0;
     unsigned int count = 16384 + 1, size = count;
-    char        *str = malloc(size), *ptr;
+    char        *str, *ptr;
     PyObject    *obj;
     int          ret;
 
@@ -1097,15 +1097,17 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 
     if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwd_list,
                                       &clear, &index, &incremental) ||
-         !str )
+         !(str = malloc(size)) )
         return NULL;
 
     ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                              incremental, &index);
-    if ( ret < 0 )
+    if ( ret < 0 ) {
+        free(str);
         return pyxc_error_to_exception(self->xc_handle);
+    }
 
-    while ( !incremental && count == size )
+    while ( !incremental && count == size && ret >= 0 )
     {
         size += count - 1;
         if ( size < count )
@@ -1119,9 +1121,6 @@ static PyObject *pyxc_readconsolering(XcObject *self,
         count = size - count;
         ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                                  1, &index);
-        if ( ret < 0 )
-            break;
-
         count += str - ptr;
         str = ptr;
     }
Signature · 1 line
1.7.10.4
Acked-by on v1 2/3
#12 lore raw

Re: [PATCH for-4.5 2/3] python/xc: Fix multiple issues in pyxc_readconsolering()

Quoted 7 lines · Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Quoted 6 lines

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 48 lines
---
 tools/python/xen/lowlevel/xc/xc.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +6 -7
index c70b388..2aa0dc7 100644
@@ -1089,7 +1089,7 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 {
     unsigned int clear = 0, index = 0, incremental = 0;
     unsigned int count = 16384 + 1, size = count;
-    char        *str = malloc(size), *ptr;
+    char        *str, *ptr;
     PyObject    *obj;
     int          ret;
 
@@ -1097,15 +1097,17 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 
     if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwd_list,
                                       &clear, &index, &incremental) ||
-         !str )
+         !(str = malloc(size)) )
         return NULL;
 
     ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                              incremental, &index);
-    if ( ret < 0 )
+    if ( ret < 0 ) {
+        free(str);
         return pyxc_error_to_exception(self->xc_handle);
+    }
 
-    while ( !incremental && count == size )
+    while ( !incremental && count == size && ret >= 0 )
     {
         size += count - 1;
         if ( size < count )
@@ -1119,9 +1121,6 @@ static PyObject *pyxc_readconsolering(XcObject *self,
         count = size - count;
         ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                                  1, &index);
-        if ( ret < 0 )
-            break;
-
         count += str - ptr;
         str = ptr;
     }
#13 lore raw

Re: [PATCH for-4.5 2/3] python/xc: Fix multiple issues in pyxc_readconsolering()

Quoted 8 lines · Ian Campbell
Quoted 7 lines · Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Quoted 8 lines
Quoted 6 lines

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 56 lines
Quoted 40 lines
---
 tools/python/xen/lowlevel/xc/xc.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
tools/python/xen/lowlevel/xc/xc.c +6 -4
index c70b388..2aa0dc7 100644
@@ -1089,7 +1089,7 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 {
     unsigned int clear = 0, index = 0, incremental = 0;
     unsigned int count = 16384 + 1, size = count;
-    char        *str = malloc(size), *ptr;
+    char        *str, *ptr;
     PyObject    *obj;
     int          ret;
 
@@ -1097,15 +1097,17 @@ static PyObject *pyxc_readconsolering(XcObject *self,
 
     if ( !PyArg_ParseTupleAndKeywords(args, kwds, "|iii", kwd_list,
                                       &clear, &index, &incremental) ||
-         !str )
+         !(str = malloc(size)) )
         return NULL;
 
     ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                              incremental, &index);
-    if ( ret < 0 )
+    if ( ret < 0 ) {
+        free(str);
         return pyxc_error_to_exception(self->xc_handle);
+    }
 
-    while ( !incremental && count == size )
+    while ( !incremental && count == size && ret >= 0 )
     {
         size += count - 1;
         if ( size < count )

tools/python/xen/lowlevel/xc/xc.c static PyObject *pyxc_readconsolering(XcObject *self, · line 1126

Quoted 9 lines
tools/python/xen/lowlevel/xc/xc.c +0 -3
@@ -1119,9 +1121,6 @@ static PyObject *pyxc_readconsolering(XcObject *self,
         count = size - count;
         ret = xc_readconsolering(self->xc_handle, str, &count, clear,
                                  1, &index);
-        if ( ret < 0 )
-            break;
-
         count += str - ptr;
         str = ptr;
     }

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

#14 lore raw

Re: [PATCH for-4.5 2/3] python/xc: Fix multiple issues in pyxc_readconsolering()

Quoted 16 lines · Konrad Rzeszutek Wilk
Quoted 15 lines · Ian Campbell
Quoted 13 lines · Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 3 lines
Quoted 1 line

Did you intend to also ack patch #1? (or have I missed a mail?)

#15 lore raw

Re: [PATCH for-4.5 2/3] python/xc: Fix multiple issues in pyxc_readconsolering()

Quoted 17 lines · Ian Campbell
Quoted 16 lines · Konrad Rzeszutek Wilk
Quoted 15 lines · Ian Campbell
Quoted 13 lines · Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Did you intend to also ack patch #1? (or have I missed a mail?)

No. You two still were discussing it so I figured I will wait until a repost.

Quoted 3 lines
#16 lore raw

Re: [PATCH for-4.5 2/3] python/xc: Fix multiple issues in pyxc_readconsolering()

Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 17 lines · Ian Campbell
Quoted 16 lines · Konrad Rzeszutek Wilk
Quoted 15 lines · Ian Campbell
Quoted 13 lines · Andrew Cooper

Don't leak a 16k allocation if PyArg_ParseTupleAndKeywords() or the first xc_readconsolering() fail. It is trivial to run throught the processes memory by repeatedly passing junk parameters to this function.

In the case that the call to xc_readconsolering() in the while loop fails, reinstate str before breaking out, and passing a spurious pointer to free().

Coverity-IDs: 1054984 1055906
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Did you intend to also ack patch #1? (or have I missed a mail?)

No. You two still were discussing it so I figured I will wait until a repost.

I've applied these two.

FWIW despite the discussion I think the first could go in too, and probably doesn't need a resend.

#17 lore raw

PATCH 3/3 python/xs: Correct the indirection of the NULL xshandle() check

In reply to Andrew Cooper

The code now now matches its comment, and will actually catch the case of a bad xs handle.

Coverity-ID: 1055948
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

---
 tools/python/xen/lowlevel/xs/xs.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/python/xen/lowlevel/xs/xs.c +1 -1
index 84e1711..ec364bb 100644
@@ -816,7 +816,7 @@ static int parse_transaction_path(XsHandle *self, PyObject *args,
 
     *xh = xshandle(self);
 
-    if (!xh)
+    if (!*xh)
         return 0;
 
     if (!PyArg_ParseTuple(args, "ss", &thstr, path))
Signature · 1 line
1.7.10.4
Acked-by on v1 3/3
#18 lore raw

Re: [PATCH for-4.5 3/3] python/xs: Correct the indirection of the NULL xshandle() check

Quoted 9 lines · Andrew Cooper

The code now now matches its comment, and will actually catch the case of a bad xs handle.

Coverity-ID: 1055948
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 17 lines
---
 tools/python/xen/lowlevel/xs/xs.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/python/xen/lowlevel/xs/xs.c +1 -1
index 84e1711..ec364bb 100644
@@ -816,7 +816,7 @@ static int parse_transaction_path(XsHandle *self, PyObject *args,
 
     *xh = xshandle(self);
 
-    if (!xh)
+    if (!*xh)
         return 0;
 
     if (!PyArg_ParseTuple(args, "ss", &thstr, path))
#19 lore raw

Re: [PATCH for-4.5 3/3] python/xs: Correct the indirection of the NULL xshandle() check

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper

The code now now matches its comment, and will actually catch the case of a bad xs handle.

Coverity-ID: 1055948
CC: Ian Campbell <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Wei Liu <[email protected]>
CC: Xen Coverity Team <[email protected]>

Quoted 20 lines
---
 tools/python/xen/lowlevel/xs/xs.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/python/xen/lowlevel/xs/xs.c +1 -1
index 84e1711..ec364bb 100644
@@ -816,7 +816,7 @@ static int parse_transaction_path(XsHandle *self, PyObject *args,
 
     *xh = xshandle(self);
 
-    if (!xh)
+    if (!*xh)
         return 0;
 
     if (!PyArg_ParseTuple(args, "ss", &thstr, path))
Signature · 1 line
1.7.10.4
#20 lore raw
In reply to Andrew Cooper
Quoted 2 lines · Andrew Cooper

While Xend is certainly dead and gone, XenServer at the very least still has consumers of these python libraries.

AIUI XS mainly uses the xs.c stuff, with the xc.c stuff being mainly debug utilities.

This is important because while the xs.c is simple and obviously correct the xc.c patches are a little more involved.

Quoted 16 lines

Konrad: I am requesting a release ack for this. All 5 issues are bugs with the handling of error cases, rather than with the basic functionality provided. With these changes, Coverity is of the opinion that the python libraries are perfect (0 issues), and I feel this is a worthy position to be in for 4.5

Andrew Cooper (3):
python/xc: Fix multiple issues in pyflask_context_to_sid() python/xc: Fix multiple issues in pyxc_readconsolering() python/xs: Correct the indirection of the NULL xshandle() check

 tools/python/xen/lowlevel/xc/xc.c |   34 +++++++++-------------------------
 tools/python/xen/lowlevel/xs/xs.c |    2 +-
 2 files changed, 10 insertions(+), 26 deletions(-)