Skip to content

Fix buffer-overflow in sha256 string construction - #377

Merged
liulanzheng merged 1 commit into
containerd:mainfrom
runloopai:adam/fix-buffer-overflow-sha256
Aug 18, 2025
Merged

Fix buffer-overflow in sha256 string construction#377
liulanzheng merged 1 commit into
containerd:mainfrom
runloopai:adam/fix-buffer-overflow-sha256

Conversation

@adam-rl

@adam-rl adam-rl commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

Need to leave room for the last null-byte.

What this PR does / why we need it: Fixes a buffer overflow assertion in Release mode

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

Need to leave room for the last null-byte.
@northtyphoon

Copy link
Copy Markdown
Contributor

@adam-rl can you elebrate a little bit what the issue is? how did you run into the buffer overflow error?

@juliusl

juliusl commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

I don't think that constructor needs a null terminator? Since the length is passed explicitly?

@adam-rl

adam-rl commented Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

sprintf will write a null terminating byte to the end of the string. This means that 3 bytes are written for each sprintf call: the 2 hex digits and a null-byte. The last byte of the sha256 will write past the end of the stack-allocated buffer unless accounted for with an extra byte.

If there was a variable declared after the buffer in the stack, it would be partially overwritten with a zero.

If you build on ubuntu in Release mode, -D_FORTIFY_SOURCE is enabled which causes the call to sprintf to abort.

@adam-rl

adam-rl commented Aug 15, 2025

Copy link
Copy Markdown
Contributor Author

@juliusl the issue isn't in copying the buffer to a std::string, that part is fine. The issue is in the final sprintf call of the loop.

@liulanzheng

Copy link
Copy Markdown
Member

sprintf will write a null terminating byte to the end of the string. This means that 3 bytes are written for each sprintf call: the 2 hex digits and a null-byte. The last byte of the sha256 will write past the end of the stack-allocated buffer unless accounted for with an extra byte.

If there was a variable declared after the buffer in the stack, it would be partially overwritten with a zero.

If you build on ubuntu in Release mode, -D_FORTIFY_SOURCE is enabled which causes the call to sprintf to abort.

Thanks, sprintf indeed needs an extra character.

@liulanzheng
liulanzheng merged commit 6e1bd7b into containerd:main Aug 18, 2025
2 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.

4 participants