Skip to content

Fix use validator on stack after returning. #91

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

buriedpot
Copy link
Contributor

@buriedpot buriedpot commented Mar 3, 2025

Describe the bug

pg_documentdb/src/metadata/collection.c:1905, the function ParseAndGetValidatorSpec returns a variable validator. It points to a memory area allocated on stack.

//...
	EnsureTopLevelFieldType(validatorName, iter, BSON_TYPE_DOCUMENT);
	const bson_value_t *validator = bson_iter_value(iter);

	/* Large and overly complex validation rules can impact database performance, especially during write operations. */
	if (validator->value.v_doc.data_len > (uint32_t) MaxSchemaValidatorSize)
//...

pg_documentdb/src/commands/create_collection_view.c:382, the function ParseCreateSpec assign the pointer to spec->validator, which will be used after the function returning.

else if (strcmp(key, "validator") == 0)
{
	spec->validator = ParseAndGetValidatorSpec(&createIter, "create.validator",
										   hasSchemaValidationSpec);
}

Environment

  • Linux
  • PostgreSQL 16
  • Architecture

Reproduction Steps

compilation: gcc -fstack-protector-strong -O0
run test case: schema_validation_insert, schema_validation

postgres=# SELECT documentdb_api.create_collection_view('schema_validation', '{ "create": "col", "validator": {"$jsonSchema": {"bsonType": "object", "properties": {"a": {"bsonType": "int"}}}}, "validationLevel": "strict", "validationAction": "error"}');
NOTICE:  creating collection
ERROR:  expected a document to create a bson object

Solution

Use palloc0. It's worth mentioning that the validator->value.v_doc.data is allocated on heap, so assigning the value of validator to spec->validator is enough and we don't need to do deep copying.

We can see that the ParseAndGetValidatorSpec function returns a pointer
pointing to a memory area on stack, and then the return value of this function
is assigned to spec->validator, which will be used after the function
ParseCreateSpec returning.
@buriedpot buriedpot requested a review from a team as a code owner March 3, 2025 11:22
@@ -378,8 +378,10 @@ ParseCreateSpec(Datum databaseDatum, pgbson *createSpec, bool *hasSchemaValidati
}
else if (strcmp(key, "validator") == 0)
{
spec->validator = ParseAndGetValidatorSpec(&createIter, "create.validator",
hasSchemaValidationSpec);
bson_value_t *validator = palloc0(sizeof(bson_value_t));
Copy link
Member

Choose a reason for hiding this comment

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

Could we avoid the palloc0 changing ParseAndGetValidatorSpec to take a pointer and assign the value to such pointer?

Then we could call the function like:
ParseAndGetValidatorSpec(&createIter, "create.validator", hasSchemaValidationSpec, &spec->validator)

Copy link
Contributor Author

@buriedpot buriedpot Mar 7, 2025

Choose a reason for hiding this comment

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

Thank you for your review 😊

Do you mean that we should still allocate validator on stack or we should allocate validator on heap in function ParseAndGetValidatorSpec instead of ParseCreateSpec?

As you can see at create_collection_view.c:154, after function ParseCreateSpec returning, spec->validator will be still used, so I think that using palloc is necessary. I can put the palloc into function ParseAndGetValidatorSpec.

Copy link
Contributor Author

@buriedpot buriedpot Mar 7, 2025

Choose a reason for hiding this comment

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

Or you want to use palloc instead of palloc0?

I think using palloc is indeed more reasonable.

Copy link
Member

Choose a reason for hiding this comment

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

What I mean is that spec in create_collection_view is palloc'd already and not in the stack. So we could just change the signature of the ParseAndGetValidatorSpec function to accept a bson_value_t * instead of returning bson_value_t.

Then it will populate the bson_value_t * passed in with the parsed validator spec.

Then we call is like this:

ParseAndGetValidatorSpec(&createIter, "create.validator", hasSchemaValidationSpec, &spec->validator)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your advice, I will try to make some modifications.

Copy link
Contributor Author

@buriedpot buriedpot Mar 8, 2025

Choose a reason for hiding this comment

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

Thank you for your review.
The validator field in spec is a pointer, so palloc is inevitable.

I can put palloc in function ParseAndGetValidatorSpec instead of ParseCreateSpec.

The core problem is that the function bson_iter_value will return a pointer which points to a memory area on stack.

Copy link
Contributor

Choose a reason for hiding this comment

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

bson_value_t is a struct right? - so if we have spec's validator be a bson_value_t (instead of a const bson_value_t *) then when calling ParseAndGetValidatorSpec as called above, we can have something like

specValidator = *bson_iter_value(&iterator)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I will make some modifications. Thank you for reviewing.

Copy link
Member

Choose a reason for hiding this comment

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

Hi, @buriedpot -- where you able to do the adjustment suggested here?

@safern
Copy link
Member

safern commented May 9, 2025

@buriedpot friendly ping, will you be able to finish this? Thank you in advance.

@buriedpot
Copy link
Contributor Author

buriedpot commented May 9, 2025 via email

@safern
Copy link
Member

safern commented May 13, 2025

@buriedpot thanks for your reply. We will try to finish your work then.

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.

3 participants