Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check null stack to prevent heap-buffer-overflow #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ yaml_document_add_scalar(yaml_document_t *document,

assert(document); /* Non-NULL document object is expected. */
assert(value); /* Non-NULL value is expected. */
if (STACK_NULL(&context, document->nodes)) goto error;

if (!tag) {
tag = (yaml_char_t *)YAML_DEFAULT_SCALAR_TAG;
Expand Down Expand Up @@ -1258,6 +1259,7 @@ yaml_document_add_sequence(yaml_document_t *document,
yaml_node_t node;

assert(document); /* Non-NULL document object is expected. */
if (STACK_NULL(&context, document->nodes)) goto error;
Copy link
Member

@perlpunk perlpunk Jun 12, 2024

Choose a reason for hiding this comment

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

I guess same the check would have to be added to yaml_document_add_scalar, yaml_document_add_mapping etc. to make this useful.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, there are other places needs stack checking. I saw your discussions and not sure this cve needs fix or not, just sending this patch to discuss. Will send a V2.


if (!tag) {
tag = (yaml_char_t *)YAML_DEFAULT_SEQUENCE_TAG;
Expand Down Expand Up @@ -1303,6 +1305,7 @@ yaml_document_add_mapping(yaml_document_t *document,
yaml_node_t node;

assert(document); /* Non-NULL document object is expected. */
if (STACK_NULL(&context, document->nodes)) goto error;

if (!tag) {
tag = (yaml_char_t *)YAML_DEFAULT_MAPPING_TAG;
Expand Down
5 changes: 5 additions & 0 deletions src/yaml_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ yaml_stack_extend(void **start, void **top, void **end);
YAML_DECLARE(int)
yaml_queue_extend(void **start, void **head, void **tail, void **end);

#define STACK_NULL(context,stack) \
(((stack).start != NULL) ? 0 : \
((context)->error = YAML_MEMORY_ERROR, \
1))

#define STACK_INIT(context,stack,type) \
(((stack).start = (type)yaml_malloc(INITIAL_STACK_SIZE*sizeof(*(stack).start))) ? \
((stack).top = (stack).start, \
Expand Down