Skip to content

CDRIVER-5634 account for embedded NULL when copying salt #1700

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

Merged
merged 2 commits into from
Aug 14, 2024

Conversation

kevinAlbs
Copy link
Collaborator

Followup to #1684. Fixes test failures on Windows with SCRAM auth. Example failure:

Begin /scram/cache_invalidation, seed 1723487056
    { "status": "fail", "test_file": "/scram/cache_invalidation", "seed": "1723487056", "start": 965.718000, "end": 974.359000, "elapsed": 8.641000  },
FAIL:C:\data\mci\26ca9ede4b9bb4808634a41a0bd207c0\mongoc\src\libmongoc\tests\test-mongoc-scram.c:457  _try_auth_from_uri()
  res
  Authentication failed.

Verified with this patch build.

Running /scram/cache_invalidation (which auths many times) on a spawn host showed salt may contain NULL bytes. This resulted in truncation when salt is copied with bson_strndup. This PR changes the copy to use bson_malloc + memcpy.

I expect embedded NULLs are not permitted in passwords, but copying the password was updated similarly for consistency.

@kevinAlbs kevinAlbs marked this pull request as ready for review August 12, 2024 19:30
Comment on lines 179 to 182
char *password_copy = bson_malloc (sizeof (char) * password_len);
memcpy (password_copy, password, password_len);
uint8_t *salt_copy = bson_malloc (sizeof (uint8_t) * salt_len);
memcpy (salt_copy, salt, salt_len);
Copy link
Contributor

@eramongodb eramongodb Aug 12, 2024

Choose a reason for hiding this comment

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

Suggested change
char *password_copy = bson_malloc (sizeof (char) * password_len);
memcpy (password_copy, password, password_len);
uint8_t *salt_copy = bson_malloc (sizeof (uint8_t) * salt_len);
memcpy (salt_copy, salt, salt_len);
unsigned char *password_copy = bson_malloc (password_len);
memcpy (password_copy, password, password_len);
unsigned char *salt_copy = bson_malloc (salt_len);
memcpy (salt_copy, salt, salt_len);

Suggest taking the opportunity to "cast" to unsigned char during the memcpy operations and remove the (unsigned char*) casts below.

sizeof (char) is guaranteed to be 1. We do not support (or I believe plan to support) any target where sizeof (uint8_t) != 1.

@kevinAlbs kevinAlbs merged commit 4676c8c into mongodb:master Aug 14, 2024
42 of 48 checks passed
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