]> Git Repo - qemu.git/blob - qobject/json-streamer.c
json: Pass lexical errors and limit violations to callback
[qemu.git] / qobject / json-streamer.c
1 /*
2  * JSON streaming support
3  *
4  * Copyright IBM, Corp. 2009
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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/error.h"
17 #include "qapi/qmp/json-lexer.h"
18 #include "qapi/qmp/json-parser.h"
19 #include "qapi/qmp/json-streamer.h"
20
21 #define MAX_TOKEN_SIZE (64ULL << 20)
22 #define MAX_TOKEN_COUNT (2ULL << 20)
23 #define MAX_NESTING (1ULL << 10)
24
25 static void json_message_free_token(void *token, void *opaque)
26 {
27     g_free(token);
28 }
29
30 static void json_message_free_tokens(JSONMessageParser *parser)
31 {
32     if (parser->tokens) {
33         g_queue_foreach(parser->tokens, json_message_free_token, NULL);
34         g_queue_free(parser->tokens);
35         parser->tokens = NULL;
36     }
37 }
38
39 void json_message_process_token(JSONLexer *lexer, GString *input,
40                                 JSONTokenType type, int x, int y)
41 {
42     JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
43     QObject *json = NULL;
44     Error *err = NULL;
45     JSONToken *token;
46
47     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;
60     case JSON_ERROR:
61         error_setg(&err, "JSON parse error, stray '%s'", input->str);
62         goto out_emit;
63     default:
64         break;
65     }
66
67     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;
73
74     parser->token_size += input->len;
75
76     g_queue_push_tail(parser->tokens, token);
77
78     if (parser->brace_count < 0 ||
79         parser->bracket_count < 0 ||
80         (parser->brace_count == 0 &&
81          parser->bracket_count == 0)) {
82         json = json_parser_parse(parser->tokens, parser->ap, &err);
83         parser->tokens = NULL;
84         goto out_emit;
85     }
86
87     /*
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");
101         goto out_emit;
102     }
103
104     return;
105
106 out_emit:
107     parser->brace_count = 0;
108     parser->bracket_count = 0;
109     json_message_free_tokens(parser);
110     parser->tokens = g_queue_new();
111     parser->token_size = 0;
112     parser->emit(parser->opaque, json, err);
113 }
114
115 void json_message_parser_init(JSONMessageParser *parser,
116                               void (*emit)(void *opaque, QObject *json,
117                                            Error *err),
118                               void *opaque, va_list *ap)
119 {
120     parser->emit = emit;
121     parser->opaque = opaque;
122     parser->ap = ap;
123     parser->brace_count = 0;
124     parser->bracket_count = 0;
125     parser->tokens = g_queue_new();
126     parser->token_size = 0;
127
128     json_lexer_init(&parser->lexer, !!ap);
129 }
130
131 void json_message_parser_feed(JSONMessageParser *parser,
132                              const char *buffer, size_t size)
133 {
134     json_lexer_feed(&parser->lexer, buffer, size);
135 }
136
137 void json_message_parser_flush(JSONMessageParser *parser)
138 {
139     json_lexer_flush(&parser->lexer);
140 }
141
142 void json_message_parser_destroy(JSONMessageParser *parser)
143 {
144     json_lexer_destroy(&parser->lexer);
145     json_message_free_tokens(parser);
146 }
This page took 0.060597 seconds and 4 git commands to generate.