Skip to content

Add stack overflow crash protection, error out instead - #348

Open
lfittl wants to merge 3 commits into
18-latestfrom
protect-against-stack-overflow
Open

Add stack overflow crash protection, error out instead#348
lfittl wants to merge 3 commits into
18-latestfrom
protect-against-stack-overflow

Conversation

@lfittl

@lfittl lfittl commented Jun 21, 2026

Copy link
Copy Markdown
Member

This makes libpg_query return the standard "stack depth limit exceeded" error that Postgres also shows for overly deep queries, instead of crashing when stack depth gets exceeded. This error is triggered by appropriately placed calls to the Postgres check_stack_depth() function, and goes together with initializing the stack base and depth in pg_query_init.

The maximum stack depth is handled similar to Postgres max_stack_depth variable, defaulting to 100 kB, and auto-sized up to 2MB. Because we may be called in threads with varying stack sizes, and from different host program locations, the allowed stack depth is recalculated for each call to libpg_query.

Per multiple reports from OSS-Fuzz.

Follow-ups

  • protobuf_c_message_unpack can run into similar issues, but since its a vendored library its not exactly sure how we should deal with it (possibly have a set of patches on top of it, and automate the vendoring?)
  • The PG_TRY/PG_CATCH sections in the parser are repetitive, especially in how they populate the result - can we unify them, or add a function?

lfittl added 2 commits June 21, 2026 12:31
This makes libpg_query return the standard "stack depth limit exceeded"
error that Postgres also shows for overly deep queries, instead of
crashing when stack depth gets exceeded. This error is triggered by
appropriately placed calls to the Postgres check_stack_depth() function,
and goes together with initializing the stack base and depth in
pg_query_init.

The maximum stack depth is handled similar to Postgres max_stack_depth
variable, defaulting to 100 kB, and auto-sized up to 2MB. Because we may
be called in threads with varying stack sizes, and from different host
program locations, the allowed stack depth is recalculated for each
call to libpg_query.

Per multiple reports from OSS-Fuzz.
@seanlinsley

Copy link
Copy Markdown
Member

Looks like there's a memory leak

@lfittl

lfittl commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

Looks like there's a memory leak

Yep, that was in Protobuf C++ specifically. Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants