Skip to contentSkip to conversation

xen_disk: cope with missing xenstore "params" node

PATCH In review Stefano Stabellini 2 participants 4 messages Feb 11, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen_disk: cope with missing xenstore "params" node

When disk is a cdrom and the drive is empty the "params" node in xenstore might be missing completely: cope with it instead of segfaulting.

hw/xen_disk.c +3 -2
index 134ac33..e553c4c 100644
@@ -577,12 +577,13 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, have_barriers, info = 0;
-    char *h;
+    char *h = NULL;
 
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
-        h = strchr(blkdev->params, ':');
+        if (blkdev->params != NULL)
+            h = strchr(blkdev->params, ':');
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;
 	    blkdev->filename  = h+1;
#1 lore raw

Am 11.02.2011 13:38, schrieb Stefano Stabellini:

Quoted 14 lines

When disk is a cdrom and the drive is empty the "params" node in xenstore might be missing completely: cope with it instead of segfaulting.

hw/xen_disk.c +0 -0
index 134ac33..e553c4c 100644
@@ -577,12 +577,13 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 586

Quoted 10 lines
hw/xen_disk.c +3 -2
     int index, qflags, have_barriers, info = 0;
-    char *h;
+    char *h = NULL;
 
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
-        h = strchr(blkdev->params, ':');
+        if (blkdev->params != NULL)
+            h = strchr(blkdev->params, ':');

The coding style requires braces here.

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 589

Quoted 3 lines
hw/xen_disk.c +0 -0
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;
 	    blkdev->filename  = h+1;

Let me add some more context:

    if (h != NULL) {
        blkdev->fileproto = blkdev->params;
        blkdev->filename  = h+1;
        *h = 0;
    } else {
        blkdev->fileproto = "<unset>";
        blkdev->filename  = blkdev->params;
    }

So in the NULL case we now have blkdev->filename = NULL. Doesn't this just move the crash a few lines downwards when bdrv_open() tries to use NULL as its filename?

Kevin

#2 lore raw
Quoted 19 lines · Kevin Wolf

Am 11.02.2011 13:38, schrieb Stefano Stabellini:

Quoted 18 lines

When disk is a cdrom and the drive is empty the "params" node in xenstore might be missing completely: cope with it instead of segfaulting.

hw/xen_disk.c +1 -1
index 134ac33..e553c4c 100644
@@ -577,12 +577,13 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, have_barriers, info = 0;
-    char *h;
+    char *h = NULL;
 
Quoted 9 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 586

Quoted 6 lines
hw/xen_disk.c +2 -1
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
-        h = strchr(blkdev->params, ':');
+        if (blkdev->params != NULL)
+            h = strchr(blkdev->params, ':');

The coding style requires braces here.

Good point, I'll do.

Quoted 15 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 589

Quoted 3 lines
hw/xen_disk.c +0 -0
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;
 	    blkdev->filename  = h+1;

Let me add some more context:

    if (h != NULL) {
        blkdev->fileproto = blkdev->params;
        blkdev->filename  = h+1;
        *h = 0;
    } else {
        blkdev->fileproto = "<unset>";
        blkdev->filename  = blkdev->params;
    }
Quoted 3 lines

So in the NULL case we now have blkdev->filename = NULL. Doesn't this just move the crash a few lines downwards when bdrv_open() tries to use NULL as its filename?

There is a check on blkdev->params being NULL few lines after so we just return.
Maybe an explicit return -1 like in the appended patch here would be better?

hw/xen_disk.c +3 -0
index 134ac33..fc0de14 100644
@@ -582,6 +582,9 @@ static int blk_init(struct XenDevice *xendev)
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
+        if (blkdev->params == NULL) {
+            return -1;
+        }
         h = strchr(blkdev->params, ':');
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;
#3 lore raw

Am 11.02.2011 13:59, schrieb Stefano Stabellini:

Quoted 47 lines · Kevin Wolf
Quoted 19 lines

Am 11.02.2011 13:38, schrieb Stefano Stabellini:

Quoted 18 lines

When disk is a cdrom and the drive is empty the "params" node in xenstore might be missing completely: cope with it instead of segfaulting.

hw/xen_disk.c +1 -1
index 134ac33..e553c4c 100644
@@ -577,12 +577,13 @@ static int blk_init(struct XenDevice *xendev)
 {
     struct XenBlkDev *blkdev = container_of(xendev, struct XenBlkDev, xendev);
     int index, qflags, have_barriers, info = 0;
-    char *h;
+    char *h = NULL;
 
Quoted 9 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev)

Quoted 6 lines
hw/xen_disk.c +2 -1
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
-        h = strchr(blkdev->params, ':');
+        if (blkdev->params != NULL)
+            h = strchr(blkdev->params, ':');

The coding style requires braces here.

Good point, I'll do.

Quoted 15 lines

hw/xen_disk.c static int blk_init(struct XenDevice *xendev)

Quoted 3 lines
hw/xen_disk.c +0 -0
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;
 	    blkdev->filename  = h+1;

Let me add some more context:

    if (h != NULL) {
        blkdev->fileproto = blkdev->params;
        blkdev->filename  = h+1;
        *h = 0;
    } else {
        blkdev->fileproto = "<unset>";
        blkdev->filename  = blkdev->params;
    }
Quoted 6 lines
Quoted 3 lines

So in the NULL case we now have blkdev->filename = NULL. Doesn't this just move the crash a few lines downwards when bdrv_open() tries to use NULL as its filename?

There is a check on blkdev->params being NULL few lines after so we just return.

Thanks, I missed that one.

Quoted 7 lines

Maybe an explicit return -1 like in the appended patch here would be better?

hw/xen_disk.c +0 -0
index 134ac33..fc0de14 100644

hw/xen_disk.c static int blk_init(struct XenDevice *xendev) · line 590

Quoted 9 lines
hw/xen_disk.c +3 -0
@@ -582,6 +582,9 @@ static int blk_init(struct XenDevice *xendev)
     /* read xenstore entries */
     if (blkdev->params == NULL) {
 	blkdev->params = xenstore_read_be_str(&blkdev->xendev, "params");
+        if (blkdev->params == NULL) {
+            return -1;
+        }
         h = strchr(blkdev->params, ':');
 	if (h != NULL) {
 	    blkdev->fileproto = blkdev->params;

Yes, I think this is more explicit, and therefore easier to read.

Kevin