G'day,

Thanks, David, for the reply on the earlier ones. Here is one more, from fuzzing the config parser, and I think it's the most useful of the lot.

1. A function named like a keyword corrupts the keyword

In function_def, the finished body is stored in $2, the symbol as the lexer returned it, rather than in this_function. They differ when the name is a keyword, so for

function recursive() { }

the body goes into the global keyword symbol "recursive", which lives for the whole life of BIRD:

- "recursive" then lexes as a garbage token ("syntax error, unexpected ENUM_TOKEN") in that config and every later one, e.g. a static route's "recursive" after a reconfigure, until BIRD restarts;
- once that config is freed, each use of the keyword reads the freed f_line: a heap use-after-free (this is what the fuzzer found);
- the function itself keeps only its empty prototype, so calling it runs no code.

2. Parser state left over from a failed parse

The filter grammar keeps static state between rules, and a parse that fails part way (cf_error()) leaves it set for the next config or CLI command:

- this_function is never cleared. A later "return" outside any function, e.g. in a static route's block, then passes the "Can't return from a non-function" check and sets the return type of a function from an earlier, possibly freed, config. After a config failing in a function header ("function f(int a, ) { }"), the next config with such a "return" crashed BIRD with a NULL dereference.
- f_method_scope_pos is raised by a method call that fails to parse (a method on the wrong type) and not lowered. After 32 of them, every later method call fails with "Too many nested method calls" until restart. CLI commands like "eval" do this too, which a restricted CLI user can also send.
- this_for_scope can be left set the same way.

The patch resets them in f_parse_init(), at the start of config_parse() and cli_parse(), and clears this_function at the end of its definition.

Based on master (4bee51c8):
https://github.com/randomizedcoder/bird/tree/fix-this-function

Tip: de8af555fefe00b9c5886d2d146d522f51af0b1c

8422e3c6 Test: Add router ID parser tests
80e617dd Filter: Reset the parser state on each parse
de8af555 Filter: Store a function's body in the symbol it defines

The new filter/config_test.c parses configs without committing them. The first commit adds router ID tests (IPv4 addresses and numbers with their boundaries, expressions, constants, IPv6 and invalid forms). Each fix then adds its own cases: parser state carried between configs and CLI commands, and keyword-named functions followed by that keyword in IPv4 and IPv6 static routes.

A few router ID rows pin behaviour that may surprise: 0 and 0.0.0.0 are taken, the same as no router ID; a negative expression is taken as 32 bits; and an IPv4-mapped IPv6 constant is taken as its IPv4 address, while the same address written out is a syntax error.

All test executables pass with ASan/UBSan, except filter_test and trie_test, which fail the same way on master and are fixed by the trie series. config_test also passes on big-endian s390x under qemu-user.

LLM tools assisted with the analysis and patches.

--
Regards,
Dave Seddon