blob: e372ecc895eb737e08faba38298f783f0bac7894 [file] [log] [blame]
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -06001/*
2 * JSON streaming support
3 *
4 * Copyright IBM, Corp. 2009
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
Peter Maydellf2ad72b2016-01-29 17:50:01 +000014#include "qemu/osdep.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060015#include "qemu-common.h"
Markus Armbruster84a56f32018-08-23 18:40:06 +020016#include "qapi/error.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010017#include "qapi/qmp/json-lexer.h"
Markus Armbruster62815d82018-08-23 18:40:01 +020018#include "qapi/qmp/json-parser.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/json-streamer.h"
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060020
Anthony Liguori29c75dd2011-06-01 12:14:53 -050021#define MAX_TOKEN_SIZE (64ULL << 20)
Markus Armbrusterdf649832015-11-25 22:23:33 +010022#define MAX_TOKEN_COUNT (2ULL << 20)
Anthony Liguori29c75dd2011-06-01 12:14:53 -050023#define MAX_NESTING (1ULL << 10)
24
Eric Blakeba4dba52016-05-18 15:46:52 -060025static void json_message_free_token(void *token, void *opaque)
26{
27 g_free(token);
28}
29
Paolo Bonzini95385fe2015-11-25 22:23:31 +010030static void json_message_free_tokens(JSONMessageParser *parser)
31{
32 if (parser->tokens) {
Eric Blakeba4dba52016-05-18 15:46:52 -060033 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
Paolo Bonzini95385fe2015-11-25 22:23:31 +010034 g_queue_free(parser->tokens);
35 parser->tokens = NULL;
36 }
37}
38
Markus Armbruster037f2442018-08-23 18:40:00 +020039void json_message_process_token(JSONLexer *lexer, GString *input,
40 JSONTokenType type, int x, int y)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060041{
42 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
Markus Armbrusterff281a22018-08-23 18:40:02 +020043 QObject *json = NULL;
Markus Armbruster62815d82018-08-23 18:40:01 +020044 Error *err = NULL;
Paolo Bonzini9bada892015-11-25 22:23:32 +010045 JSONToken *token;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060046
Markus Armbrusterc5461662015-11-25 22:23:26 +010047 switch (type) {
48 case JSON_LCURLY:
49 parser->brace_count++;
50 break;
51 case JSON_RCURLY:
52 parser->brace_count--;
53 break;
54 case JSON_LSQUARE:
55 parser->bracket_count++;
56 break;
57 case JSON_RSQUARE:
58 parser->bracket_count--;
59 break;
Markus Armbruster269e57a2018-08-23 18:40:03 +020060 case JSON_ERROR:
Markus Armbruster84a56f32018-08-23 18:40:06 +020061 error_setg(&err, "JSON parse error, stray '%s'", input->str);
Markus Armbruster269e57a2018-08-23 18:40:03 +020062 goto out_emit;
Markus Armbrusterc5461662015-11-25 22:23:26 +010063 default:
64 break;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060065 }
66
Paolo Bonzini9bada892015-11-25 22:23:32 +010067 token = g_malloc(sizeof(JSONToken) + input->len + 1);
68 token->type = type;
69 memcpy(token->str, input->str, input->len);
70 token->str[input->len] = 0;
71 token->x = x;
72 token->y = y;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060073
Paolo Bonzinid2ca7c02015-11-25 22:23:29 +010074 parser->token_size += input->len;
Anthony Liguori29c75dd2011-06-01 12:14:53 -050075
Paolo Bonzini9bada892015-11-25 22:23:32 +010076 g_queue_push_tail(parser->tokens, token);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -060077
Markus Armbrusterff281a22018-08-23 18:40:02 +020078 if (parser->brace_count < 0 ||
Anthony Liguori55f83012011-06-01 12:14:51 -050079 parser->bracket_count < 0 ||
80 (parser->brace_count == 0 &&
81 parser->bracket_count == 0)) {
Markus Armbrusterff281a22018-08-23 18:40:02 +020082 json = json_parser_parse(parser->tokens, parser->ap, &err);
83 parser->tokens = NULL;
Michael Roth5e2dafe2011-06-01 12:14:59 -050084 goto out_emit;
Markus Armbrusterff281a22018-08-23 18:40:02 +020085 }
86
Markus Armbruster84a56f32018-08-23 18:40:06 +020087 /*
88 * Security consideration, we limit total memory allocated per object
89 * and the maximum recursion depth that a message can force.
90 */
91 if (parser->token_size > MAX_TOKEN_SIZE) {
92 error_setg(&err, "JSON token size limit exceeded");
93 goto out_emit;
94 }
95 if (g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT) {
96 error_setg(&err, "JSON token count limit exceeded");
97 goto out_emit;
98 }
99 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
100 error_setg(&err, "JSON nesting depth limit exceeded");
Markus Armbrusterff281a22018-08-23 18:40:02 +0200101 goto out_emit;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600102 }
Michael Roth5e2dafe2011-06-01 12:14:59 -0500103
104 return;
105
Michael Roth5e2dafe2011-06-01 12:14:59 -0500106out_emit:
Michael Roth5e2dafe2011-06-01 12:14:59 -0500107 parser->brace_count = 0;
108 parser->bracket_count = 0;
Markus Armbrusterff281a22018-08-23 18:40:02 +0200109 json_message_free_tokens(parser);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100110 parser->tokens = g_queue_new();
Michael Roth5e2dafe2011-06-01 12:14:59 -0500111 parser->token_size = 0;
Markus Armbruster62815d82018-08-23 18:40:01 +0200112 parser->emit(parser->opaque, json, err);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600113}
114
115void json_message_parser_init(JSONMessageParser *parser,
Markus Armbruster62815d82018-08-23 18:40:01 +0200116 void (*emit)(void *opaque, QObject *json,
117 Error *err),
118 void *opaque, va_list *ap)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600119{
Markus Armbruster62815d82018-08-23 18:40:01 +0200120 parser->emit = emit;
121 parser->opaque = opaque;
122 parser->ap = ap;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600123 parser->brace_count = 0;
124 parser->bracket_count = 0;
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100125 parser->tokens = g_queue_new();
Anthony Liguori29c75dd2011-06-01 12:14:53 -0500126 parser->token_size = 0;
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600127
Markus Armbruster2cbd15a2018-08-23 18:40:05 +0200128 json_lexer_init(&parser->lexer, !!ap);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600129}
130
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200131void json_message_parser_feed(JSONMessageParser *parser,
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600132 const char *buffer, size_t size)
133{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200134 json_lexer_feed(&parser->lexer, buffer, size);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600135}
136
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200137void json_message_parser_flush(JSONMessageParser *parser)
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600138{
Marc-André Lureau7c1e1d52018-08-23 18:39:58 +0200139 json_lexer_flush(&parser->lexer);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600140}
141
142void json_message_parser_destroy(JSONMessageParser *parser)
143{
144 json_lexer_destroy(&parser->lexer);
Paolo Bonzini95385fe2015-11-25 22:23:31 +0100145 json_message_free_tokens(parser);
Anthony Liguorid7ff3ac2009-11-11 10:38:59 -0600146}