Skip to contentSkip to conversation

Fix segfault in qemu_rbd_parse_filename

PATCH Superseded Connor Kuehl 2 participants 7 messages Apr 9, 2021 – Apr 22, 2021 Block layer

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix segfault in qemu_rbd_parse_filename

Connor Kuehl (2):
iotests/231: Update expected deprecation message block/rbd: Add an escape-aware strchr helper

 block/rbd.c                | 20 ++++++++++++++++++--
 tests/qemu-iotests/231     |  4 ++++
 tests/qemu-iotests/231.out |  7 ++++---
 3 files changed, 26 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/2 iotests/231: Update expected deprecation message

The deprecation message in the expected output has technically been wrong since the wrong version of a patch was applied to it. Because of this, the test fails. Correct the expected output so that it passes.

---
 tests/qemu-iotests/231.out | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tests/qemu-iotests/231.out +1 -3
index 579ba11c16..747dd221bb 100644
@@ -1,9 +1,7 @@
 QA output created by 231
-qemu-img: RBD options encoded in the filename as keyvalue pairs is deprecated.  Future versions may cease to parse these options in the future.
+qemu-img: warning: RBD options encoded in the filename as keyvalue pairs is deprecated
 unable to get monitor info from DNS SRV with service name: ceph-mon
-no monitors specified to connect to.
 qemu-img: Could not open 'json:{'file.driver':'rbd','file.filename':'rbd:rbd/bogus:conf=BOGUS_CONF'}': error connecting: No such file or directory
 unable to get monitor info from DNS SRV with service name: ceph-mon
-no monitors specified to connect to.
 qemu-img: Could not open 'json:{'file.driver':'rbd','file.pool':'rbd','file.image':'bogus','file.conf':'BOGUS_CONF'}': error connecting: No such file or directory
 *** done
Signature · 1 line
2.30.2
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] iotests/231: Update expected deprecation message

Quoted 9 lines · Connor Kuehl

The deprecation message in the expected output has technically been wrong since the wrong version of a patch was applied to it. Because of this, the test fails. Correct the expected output so that it passes.

---
tests/qemu-iotests/231.out | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 20 lines
tests/qemu-iotests/231.out +0 -0
index 579ba11c16..747dd221bb 100644
@@ -1,9 +1,7 @@

QA output created by 231

tests/qemu-iotests/231.out +1 -1
-qemu-img: RBD options encoded in the filename as keyvalue pairs is deprecated.  Future versions may cease to parse these options in the future.
+qemu-img: warning: RBD options encoded in the filename as keyvalue pairs is deprecated

unable to get monitor info from DNS SRV with service name: ceph-mon
-no monitors specified to connect to.
qemu-img: Could not open 'json:{'file.driver':'rbd','file.filename':'rbd:rbd/bogus:conf=BOGUS_CONF'}': error connecting: No such file or directory unable to get monitor info from DNS SRV with service name: ceph-mon
-no monitors specified to connect to.
qemu-img: Could not open 'json:{'file.driver':'rbd','file.pool':'rbd','file.image':'bogus','file.conf':'BOGUS_CONF'}': error connecting: No such file or directory *** done

Signature · 1 line
2.30.2
#3 lore raw

PATCH v3 2/2 block/rbd: Add an escape-aware strchr helper

In reply to Connor Kuehl

Sometimes the parser needs to further split a token it has collected from the token input stream. Right now, it does a cursory check to see if the relevant characters appear in the token to determine if it should break it down further.

However, qemu_rbd_next_tok() will escape characters as it removes tokens from the token stream and plain strchr() won't. This can make the initial strchr() check slightly misleading since it implies qemu_rbd_next_tok() will find the token and split on it, except the reality is that qemu_rbd_next_tok() will pass over it if it is escaped.

Use a custom strchr to avoid mixing escaped and unescaped string operations.

---
v2 -> v3:
* Update qemu_rbd_strchr to only skip if there's a delimiter AND the next character is not the NUL terminator

 block/rbd.c                | 20 ++++++++++++++++++--
 tests/qemu-iotests/231     |  4 ++++
 tests/qemu-iotests/231.out |  3 +++
 3 files changed, 25 insertions(+), 2 deletions(-)
block/rbd.c +18 -2
index 9071a00e3f..291e3f09e1 100644
@@ -134,6 +134,22 @@ static char *qemu_rbd_next_tok(char *src, char delim, char **p)
     return src;
 }
 
+static char *qemu_rbd_strchr(char *src, char delim)
+{
+    char *p;
+
+    for (p = src; *p; ++p) {
+        if (*p == delim) {
+            return p;
+        }
+        if (*p == '\\' && p[1] != '\0') {
+            ++p;
+        }
+    }
+
+    return NULL;
+}
+
 static void qemu_rbd_unescape(char *src)
 {
     char *p;
@@ -171,7 +187,7 @@ static void qemu_rbd_parse_filename(const char *filename, QDict *options,
     qemu_rbd_unescape(found_str);
     qdict_put_str(options, "pool", found_str);
 
-    if (strchr(p, '@')) {
+    if (qemu_rbd_strchr(p, '@')) {
         image_name = qemu_rbd_next_tok(p, '@', &p);
 
         found_str = qemu_rbd_next_tok(p, ':', &p);
@@ -181,7 +197,7 @@ static void qemu_rbd_parse_filename(const char *filename, QDict *options,
         image_name = qemu_rbd_next_tok(p, ':', &p);
     }
     /* Check for namespace in the image_name */
-    if (strchr(image_name, '/')) {
+    if (qemu_rbd_strchr(image_name, '/')) {
         found_str = qemu_rbd_next_tok(image_name, '/', &image_name);
         qemu_rbd_unescape(found_str);
         qdict_put_str(options, "namespace", found_str);
tests/qemu-iotests/231 +4 -0
index 0f66d0ca36..8e6c6447c1 100755
@@ -55,6 +55,10 @@ _filter_conf()
 $QEMU_IMG info "json:{'file.driver':'rbd','file.filename':'rbd:rbd/bogus:conf=${BOGUS_CONF}'}" 2>&1 | _filter_conf
 $QEMU_IMG info "json:{'file.driver':'rbd','file.pool':'rbd','file.image':'bogus','file.conf':'${BOGUS_CONF}'}" 2>&1 | _filter_conf
 
+# Regression test: the qemu-img invocation is expected to fail, but it should
+# not seg fault the parser.
+$QEMU_IMG create "rbd:rbd/aa\/bb:conf=${BOGUS_CONF}" 1M 2>&1 | _filter_conf
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/231.out +3 -0
index 747dd221bb..a785a6e859 100644
@@ -4,4 +4,7 @@ unable to get monitor info from DNS SRV with service name: ceph-mon
 qemu-img: Could not open 'json:{'file.driver':'rbd','file.filename':'rbd:rbd/bogus:conf=BOGUS_CONF'}': error connecting: No such file or directory
 unable to get monitor info from DNS SRV with service name: ceph-mon
 qemu-img: Could not open 'json:{'file.driver':'rbd','file.pool':'rbd','file.image':'bogus','file.conf':'BOGUS_CONF'}': error connecting: No such file or directory
+Formatting 'rbd:rbd/aa\/bb:conf=BOGUS_CONF', fmt=raw size=1048576
+unable to get monitor info from DNS SRV with service name: ceph-mon
+qemu-img: rbd:rbd/aa\/bb:conf=BOGUS_CONF: error connecting: No such file or directory
 *** done
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH v3 2/2] block/rbd: Add an escape-aware strchr helper

Quoted 41 lines · Connor Kuehl

Sometimes the parser needs to further split a token it has collected from the token input stream. Right now, it does a cursory check to see if the relevant characters appear in the token to determine if it should break it down further.

However, qemu_rbd_next_tok() will escape characters as it removes tokens from the token stream and plain strchr() won't. This can make the initial strchr() check slightly misleading since it implies qemu_rbd_next_tok() will find the token and split on it, except the reality is that qemu_rbd_next_tok() will pass over it if it is escaped.

Use a custom strchr to avoid mixing escaped and unescaped string operations.

---
v2 -> v3:
* Update qemu_rbd_strchr to only skip if there's a delimiter AND the next character is not the NUL terminator

block/rbd.c                | 20 ++++++++++++++++++--
tests/qemu-iotests/231     |  4 ++++
tests/qemu-iotests/231.out |  3 +++
3 files changed, 25 insertions(+), 2 deletions(-)
block/rbd.c +6 -0
index 9071a00e3f..291e3f09e1 100644
@@ -134,6 +134,22 @@ static char *qemu_rbd_next_tok(char *src, char delim, char **p)
    return src;
 }
 
+static char *qemu_rbd_strchr(char *src, char delim)
+{
+    char *p;
+
+    for (p = src; *p; ++p) {
+        if (*p == delim) {

block/rbd.c static char *qemu_rbd_next_tok(char *src, char delim, char **p) · line 152

Quoted 10 lines
block/rbd.c +10 -0
+            return p;
+        }
+        if (*p == '\\' && p[1] != '\0') {
+            ++p;
+        }
+    }
+
+    return NULL;
+}
+

IIUC this is similar to the code used in qemu_rbd_next_tok(). To avoid code duplication can we use this new function inside it?

I mean something like this (not tested):

block/rbd.c +2 -9
index f098a89c7b..eb6a839362 100644
@@ -119,15 +119,8 @@ static char *qemu_rbd_next_tok(char *src, char delim, char **p)
 
     *p = NULL;
 
-    for (end = src; *end; ++end) {
-        if (*end == delim) {
-            break;
-        }
-        if (*end == '\\' && end[1] != '\0') {
-            end++;
-        }
-    }
-    if (*end == delim) {
+    end = qemu_rbd_strchr(src, delim);
+    if (end && *end == delim) {
         *p = end + 1;
         *end = '\0';
     }

The rest LGTM!

Thanks for fixing this issue,
Stefano

#5 lore raw

Re: [PATCH v3 2/2] block/rbd: Add an escape-aware strchr helper

Quoted 11 lines · Stefano Garzarella
Quoted 11 lines
(quoted diff) +11 -0
+static char *qemu_rbd_strchr(char *src, char delim)
+{
+    char *p;
+
+    for (p = src; *p; ++p) {
+        if (*p == delim) {
+            return p;
+        }
+        if (*p == '\\' && p[1] != '\0') {
+            ++p;
+        }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    }
+
+    return NULL;
+}
+

IIUC this is similar to the code used in qemu_rbd_next_tok(). To avoid code duplication can we use this new function inside it?

Hi Stefano! Good catch. I think you're right. I've incorporated your suggestion into my next revision. The only thing I changed was the if-statement from:

	if (end && *end == delim) {

to:

	if (end) {

Since qemu_rbd_strchr() will only ever return a non-NULL pointer if it was able to find 'delim'.

Connor

Quoted 32 lines

I mean something like this (not tested):

block/rbd.c +2 -9
index f098a89c7b..eb6a839362 100644
@@ -119,15 +119,8 @@ static char *qemu_rbd_next_tok(char *src, char delim, char **p)
 
     *p = NULL;
 
-    for (end = src; *end; ++end) {
-        if (*end == delim) {
-            break;
-        }
-        if (*end == '\\' && end[1] != '\0') {
-            end++;
-        }
-    }
-    if (*end == delim) {
+    end = qemu_rbd_strchr(src, delim);
+    if (end && *end == delim) {
         *p = end + 1;
         *end = '\0';
     }

The rest LGTM!

Thanks for fixing this issue,
Stefano

#6 lore raw

Re: [PATCH v3 2/2] block/rbd: Add an escape-aware strchr helper

Hi Connor,

Quoted 25 lines · Connor Kuehl
Quoted 11 lines · Stefano Garzarella
Quoted 11 lines
(quoted diff) +11 -0
+static char *qemu_rbd_strchr(char *src, char delim)
+{
+    char *p;
+
+    for (p = src; *p; ++p) {
+        if (*p == delim) {
+            return p;
+        }
+        if (*p == '\\' && p[1] != '\0') {
+            ++p;
+        }
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    }
+
+    return NULL;
+}
+

IIUC this is similar to the code used in qemu_rbd_next_tok(). To avoid code duplication can we use this new function inside it?

Hi Stefano! Good catch. I think you're right. I've incorporated your suggestion into my next revision. The only thing I changed was the if-statement from:

Quoted 8 lines
	if (end && *end == delim) {

to:

	if (end) {

Since qemu_rbd_strchr() will only ever return a non-NULL pointer if it was able to find 'delim'.

Yeah, definitely better :-)

Thanks,
Stefano