]> git.proxmox.com Git - mirror_qemu.git/blob - qobject/json-streamer.c
json: Treat unwanted interpolation as lexical error
[mirror_qemu.git] / qobject / json-streamer.c
1 /*
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
14 #include "qemu/osdep.h"
15 #include "qemu-common.h"
16 #include "qapi/qmp/json-lexer.h"
17 #include "qapi/qmp/json-parser.h"
18 #include "qapi/qmp/json-streamer.h"
19
20 #define MAX_TOKEN_SIZE (64ULL << 20)
21 #define MAX_TOKEN_COUNT (2ULL << 20)
22 #define MAX_NESTING (1ULL << 10)
23
24 static void json_message_free_token(void *token, void *opaque)
25 {
26 g_free(token);
27 }
28
29 static void json_message_free_tokens(JSONMessageParser *parser)
30 {
31 if (parser->tokens) {
32 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
33 g_queue_free(parser->tokens);
34 parser->tokens = NULL;
35 }
36 }
37
38 void json_message_process_token(JSONLexer *lexer, GString *input,
39 JSONTokenType type, int x, int y)
40 {
41 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
42 QObject *json = NULL;
43 Error *err = NULL;
44 JSONToken *token;
45
46 switch (type) {
47 case JSON_LCURLY:
48 parser->brace_count++;
49 break;
50 case JSON_RCURLY:
51 parser->brace_count--;
52 break;
53 case JSON_LSQUARE:
54 parser->bracket_count++;
55 break;
56 case JSON_RSQUARE:
57 parser->bracket_count--;
58 break;
59 case JSON_ERROR:
60 goto out_emit;
61 default:
62 break;
63 }
64
65 token = g_malloc(sizeof(JSONToken) + input->len + 1);
66 token->type = type;
67 memcpy(token->str, input->str, input->len);
68 token->str[input->len] = 0;
69 token->x = x;
70 token->y = y;
71
72 parser->token_size += input->len;
73
74 g_queue_push_tail(parser->tokens, token);
75
76 if (parser->brace_count < 0 ||
77 parser->bracket_count < 0 ||
78 (parser->brace_count == 0 &&
79 parser->bracket_count == 0)) {
80 json = json_parser_parse(parser->tokens, parser->ap, &err);
81 parser->tokens = NULL;
82 goto out_emit;
83 }
84
85 if (parser->token_size > MAX_TOKEN_SIZE ||
86 g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT ||
87 parser->bracket_count + parser->brace_count > MAX_NESTING) {
88 /* Security consideration, we limit total memory allocated per object
89 * and the maximum recursion depth that a message can force.
90 */
91 goto out_emit;
92 }
93
94 return;
95
96 out_emit:
97 parser->brace_count = 0;
98 parser->bracket_count = 0;
99 json_message_free_tokens(parser);
100 parser->tokens = g_queue_new();
101 parser->token_size = 0;
102 parser->emit(parser->opaque, json, err);
103 }
104
105 void json_message_parser_init(JSONMessageParser *parser,
106 void (*emit)(void *opaque, QObject *json,
107 Error *err),
108 void *opaque, va_list *ap)
109 {
110 parser->emit = emit;
111 parser->opaque = opaque;
112 parser->ap = ap;
113 parser->brace_count = 0;
114 parser->bracket_count = 0;
115 parser->tokens = g_queue_new();
116 parser->token_size = 0;
117
118 json_lexer_init(&parser->lexer, !!ap);
119 }
120
121 void json_message_parser_feed(JSONMessageParser *parser,
122 const char *buffer, size_t size)
123 {
124 json_lexer_feed(&parser->lexer, buffer, size);
125 }
126
127 void json_message_parser_flush(JSONMessageParser *parser)
128 {
129 json_lexer_flush(&parser->lexer);
130 }
131
132 void json_message_parser_destroy(JSONMessageParser *parser)
133 {
134 json_lexer_destroy(&parser->lexer);
135 json_message_free_tokens(parser);
136 }