json: fix error protagation when parsing binop lhs/rhs
Florian Westphal
maintainerPATCH 1/1 json: fix error protagation when parsing binop lhs/rhs
Malformed input returns NULL when decoding left/right side of binop. This causes a NULL dereference in expr_evaluate_binop; left/right must point to a valid expression.
Fix this in the parser, else would have to sprinkle NULL checks all over the evaluation code.
After fix, loading the bogon yields:
internal:0:0-0: Error: Malformed object (too many properties): '{}'. internal:0:0-0: Error: could not decode binop rhs, '<<'. internal:0:0-0: Error: Invalid mangle statement value internal:0:0-0: Error: Parsing expr array at index 1 failed. internal:0:0-0: Error: Parsing command array at index 3 failed.
--- src/parser_json.c | 14 ++++ .../nft-j-f/binop_rhs_decode_error_crash | 76 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash
src/parser_json.c +14 -0
@@ -1210,11 +1210,25 @@ static struct expr *json_parse_binop_expr(struct json_ctx *ctx,if (json_array_size (root) >2 ) { left =json_parse_primary_expr (ctx,json_array_get (root,0 )); +if (!left) { +json_error (ctx,"Failed to parse LHS of binop expression." ); +return NULL ; + } right =json_parse_primary_expr (ctx,json_array_get (root,1 )); +if (!right) { +json_error (ctx,"Failed to parse RHS of binop expression." ); +expr_free (left); +return NULL ; + } left =binop_expr_alloc (int_loc, thisop, left, right);for (i =2 ; i <json_array_size (root); i++) { jright =json_array_get (root, i); right =json_parse_primary_expr (ctx, jright); +if (!right) { +json_error (ctx,"Failed to parse RHS of binop expression." ); +expr_free (left); +return NULL ; + } left =binop_expr_alloc (int_loc, thisop, left, right); }return left;
tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash new file +76 -0
76 lines not on this page · raw message
Signature · 1 line
2.48.1
Quoted 14 lines · Florian Westphal
Malformed input returns NULL when decoding left/right side of binop. This causes a NULL dereference in
expr_evaluate_binop; left/right must point to a valid expression.Fix this in the parser, else would have to sprinkle NULL checks all over the evaluation code.
After fix, loading the bogon yields:
internal:0:0-0: Error: Malformed object (too many properties): '{}'. internal:0:0-0: Error: could not decode binop rhs, '<<'. internal:0:0-0: Error: Invalid mangle statement value internal:0:0-0: Error: Parsing expr array at index 1 failed. internal:0:0-0: Error: Parsing command array at index 3 failed.
Quoted 2 lines
Fixes 0ac39384fd9e ("json: Accept more than two operands in binary expressions")Signed-off-by Florian Westphal <[email protected]>
one nitpick:
Quoted 5 lines
tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash new file +0
-0
[...]
tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash line 66
Quoted 13 lines
tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash new file +13
-0+ "value": { + "<<": [ + { + "|": [ + { + "meta": { + "key": "mark" + } + }, + 16 + ] + }, + { },
Something strange here in this indent.
Quoted 10 lines
tests/shell/testcases/bogons/nft-j-f/binop_rhs_decode_error_crash new file +10
-0+ 8 + ] + } + } + } + ] + } + } + ] +}