sahvx655-wq opened a new pull request, #3762:
URL: https://github.com/apache/brpc/pull/3762

   ### What problem does this PR solve?
   
   Issue Number: N/A
   
   Problem Summary:
   
   `ObjectIterator::operator++()` and `ArrayIterator::operator++()` in 
`src/mcpack2pb/parser.cpp` skip a deleted field (`type & FIELD_NON_DELETED_MASK 
== 0`) by calling `operator++()` again, marked `// tailr`. Nothing makes that a 
tail call. `head` is address-taken through `cut_packed_pod()`, and clang keeps 
a real `call` to `operator++` in the `-O2` output. Deleted fields are walked by 
byte length, not by `item_count`, and they all sit at the same depth, so 
neither the `item_count` check in `ObjectIterator::init()` nor `MAX_DEPTH` 
limits them. The root cause is one stack frame per deleted field, with no limit 
other than the body size.
   
   The bytes come straight off the wire. `ubrpc2pb_protocol.cpp:56` builds an 
`ObjectIterator` over the request body, and the nshead_mcpack adaptor reaches 
the same iterators through the generated `parse_*` code. Both run before any 
service code or authentication. A body made of `0x80 0x00 0x00` repeated (a 
short-head field with no name, no value and the deleted type 0x80) is enough. I 
extracted the real `parser.cpp`/`parser-inl.h` iterators into a standalone 
harness and built it with clang at `-O0` and `-O2`. 5000 deleted fields parse 
fine. 20000 (about 60KB, far below `max_body_size`) overflow a 1MB stack and 
the process dies with a stack overflow, just as on a NORMAL bthread stack. The 
risk if unfixed is that any client that can reach a ubrpc or nshead_mcpack port 
can kill the whole server with one small request. This is the same CWE-674 
class that the existing `MAX_DEPTH` check was added for. The nesting guard just 
does not cover siblings.
   
   ### What is changed and the side effects?
   
   Changed:
   
   - The six `return operator++();  // tailr` sites now `goto next_field;`, a 
label placed before the end-of-object check. The skip becomes a loop, the same 
pattern `HPacker::Decode` uses with `decode_next`. The consumed-bytes check at 
the top of `operator++()` is still done once per call, as before. After a skip 
it was always a no-op, because the skip pops exactly `full_size - sizeof(head)` 
bytes.
   - Added `Mcpack2pbParserTest.ManyDeletedFieldsDoNotOverflowStack`. It parses 
an object with 100000 deleted fields, followed by a `children` array that 
itself holds 100000 deleted items, on a 1MB stack using the existing 
`ParseRecursivePayloadWith1MBStack` helper. This covers both iterators. 
Mirrored in the standalone harness, the test crashes (stack overflow) before 
the change and passes after it. The skip still lands on `children`.
   
   Side effects:
   - Performance effects: none, and slightly less stack per deleted field.
   
   - Breaking backward compatibility: no. Parsing results are identical. Only 
the way deleted fields are skipped changes.
   
   ---
   ### Check List:
   - Please make sure your changes are compilable.
   - When providing us with a new feature, it is best to add related tests.
   - Please follow [Contributor Covenant Code of 
Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
   
   Tested so far: the clang `-O0`/`-O2` harness on Windows, before and after 
the change. GCC was not tried, and the full Linux build and 
`brpc_mcpack2pb_unittest` have not run here yet, so CI is the first full build.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to