Skip to contentSkip to conversation

json-streamer: fix double-free on exiting during a parse

PATCH In review Paolo Bonzini 5 participants 8 messages Jul 4, 2016 – Jul 6, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 json-streamer: fix double-free on exiting during a parse

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

---
 qobject/json-streamer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
qobject/json-streamer.c +6 -2
index 7164390..c51c202 100644
@@ -39,6 +39,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input,
 {
     JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
     JSONToken *token;
+    GQueue *tokens;
 
     switch (type) {
     case JSON_LCURLY:
@@ -96,9 +97,12 @@ out_emit:
     /* send current list of tokens to parser and reset tokenizer */
     parser->brace_count = 0;
     parser->bracket_count = 0;
-    /* parser->emit takes ownership of parser->tokens.  */
-    parser->emit(parser, parser->tokens);
+    /* parser->emit takes ownership of parser->tokens.  Remove our own
+     * reference to parser->tokens before handing it out to parser->emit.
+     */
+    tokens = parser->tokens;
     parser->tokens = g_queue_new();
+    parser->emit(parser, tokens);
     parser->token_size = 0;
 }
 
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 8 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Two meta questions:

Is there a reproducer and/or test case coverage?

Does qemu-stable need this?

Fam

Quoted 35 lines
---
 qobject/json-streamer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
qobject/json-streamer.c +6 -2
index 7164390..c51c202 100644
@@ -39,6 +39,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input,
 {
     JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
     JSONToken *token;
+    GQueue *tokens;
 
     switch (type) {
     case JSON_LCURLY:
@@ -96,9 +97,12 @@ out_emit:
     /* send current list of tokens to parser and reset tokenizer */
     parser->brace_count = 0;
     parser->bracket_count = 0;
-    /* parser->emit takes ownership of parser->tokens.  */
-    parser->emit(parser, parser->tokens);
+    /* parser->emit takes ownership of parser->tokens.  Remove our own
+     * reference to parser->tokens before handing it out to parser->emit.
+     */
+    tokens = parser->tokens;
     parser->tokens = g_queue_new();
+    parser->emit(parser, tokens);
     parser->token_size = 0;
 }
 
Signature · 1 line
1.8.3.1
#2 lore raw
Quoted 7 lines · Fam Zheng
Quoted 6 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Quoted 6 lines
Quoted 2 lines

Two meta questions:

Is there a reproducer and/or test case coverage?

tests/qemu-iotests/071

Quoted 3 lines

Does qemu-stable need this?

http://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg00465.html

Thanks
-Xie

Quoted 41 lines

Fam

Quoted 32 lines
---
  qobject/json-streamer.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
qobject/json-streamer.c +6 -3
index 7164390..c51c202 100644
@@ -39,6 +39,7 @@ static void json_message_process_token(JSONLexer *lexer, GString *input,
  {
      JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
      JSONToken *token;
+    GQueue *tokens;
 
      switch (type) {
      case JSON_LCURLY:
@@ -96,9 +97,12 @@ out_emit:
      /* send current list of tokens to parser and reset tokenizer */
      parser->brace_count = 0;
      parser->bracket_count = 0;
-    /* parser->emit takes ownership of parser->tokens.  */
-    parser->emit(parser, parser->tokens);
+    /* parser->emit takes ownership of parser->tokens.  Remove our own
+     * reference to parser->tokens before handing it out to parser->emit.
+     */
+    tokens = parser->tokens;
      parser->tokens = g_queue_new();
+    parser->emit(parser, tokens);
      parser->token_size = 0;
  }
 
--
Quoted 3 lines

1.8.3.1

.

#3 lore raw
Quoted 15 lines · Changlong Xie
Quoted 7 lines · Fam Zheng
Quoted 6 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Quoted 6 lines
Quoted 2 lines

Two meta questions:

Is there a reproducer and/or test case coverage?

Quoted 7 lines

tests/qemu-iotests/071

Quoted 3 lines

Does qemu-stable need this?

http://lists.nongnu.org/archive/html/qemu-devel/2016-07/msg00465.html

Get it! Thanks!

Fam

Reviewed-by on v1 1/1
#4 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

---
 qobject/json-streamer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/1
#5 lore raw
In reply to Paolo Bonzini
Quoted 8 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Want me to do the pull request?

#6 lore raw
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Quoted 6 lines
Quoted 2 lines

Want me to do the pull request?

I'm doing one tomorrow, so your choice.

Paolo

#7 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Markus Armbruster
Quoted 6 lines · Paolo Bonzini

Now that json-streamer tries not to leak tokens on incomplete parse, the tokens can be freed twice if QEMU destroys the json-streamer object during the parser->emit call. To fix this, create the new empty GQueue earlier, so that it is already in place when the old one is passed to parser->emit.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Want me to do the pull request?

I'm doing one tomorrow, so your choice.

Please include it in your pull request then.