Skip to content

lib-oauth2: jwt - Avoid use-after-realloc of jwt_node front pointer#289

Open
rootvector2 wants to merge 1 commit into
dovecot:mainfrom
rootvector2:lib-oauth2-jwt-nodes-realloc
Open

lib-oauth2: jwt - Avoid use-after-realloc of jwt_node front pointer#289
rootvector2 wants to merge 1 commit into
dovecot:mainfrom
rootvector2:lib-oauth2-jwt-nodes-realloc

Conversation

@rootvector2
Copy link
Copy Markdown
Contributor

oauth2_jwt_copy_fields() captures subroot from array_front(&nodes)
then calls array_append_space(&nodes) inside the inner loop. When the
append cannot extend the buffer in place it relocates it, so subroot
aliases the previous slot and later subroot->prefix / subroot->array
reads operate on the abandoned location. Copy the front node by value
before the append.

Comment thread src/lib-oauth2/oauth2-jwt.c Outdated
/* Take a local copy: array_append_space() below grows the
nodes buffer, which may move it via realloc and leave any
pointer into the old slot dangling. */
const struct jwt_node subroot = *array_front(&nodes);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This *array_front() won't compile with non-gcc/clang which don't have enough type checks. Also, I think this could be changed to a one line patch by just adding subroot = array_front(&nodes); after the root = array_append_space(&nodes);, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Switched to refreshing subroot after array_append_space() so it survives a possible realloc, instead of the local-copy approach.

oauth2_jwt_copy_fields() captures subroot from array_front(&nodes)
then calls array_append_space(&nodes) inside the inner loop. When the
append cannot extend the buffer in place it relocates it, so subroot
aliases the previous slot and later subroot->prefix / subroot->array
reads operate on the abandoned location. Refresh subroot after the
append.
@rootvector2 rootvector2 force-pushed the lib-oauth2-jwt-nodes-realloc branch from 564981f to 390e926 Compare May 28, 2026 06:34

if (!json_node_is_singular(jnode)) {
root = array_append_space(&nodes);
subroot = array_front(&nodes);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small comment before this would be good to prevent someone accidentally optimizing away the assignment.

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