Skip to content

Conversation

osamakader
Copy link

@osamakader osamakader commented Sep 26, 2025

Replace all instances of sprintf with snprintf to prevent buffer overflows. This follows the security hardening mentioned in NEWS.md where sprintf was replaced with safer alternatives.

Changes made:

  • tests/init.c: Replace sprintf with snprintf for proc paths
  • tests/tests_libcrun_fuzzer.c: Replace sprintf with snprintf for ID generation
  • src/libcrun/chroot_realpath.c: Replace sprintf with snprintf for path resolution

All changes use appropriate buffer size limits to prevent overflow.

Summary by Sourcery

Harden string formatting across the project by replacing unsafe sprintf calls with bounded snprintf usages and explicit buffer size limits to prevent buffer overflows in both test code and core functionality.

Enhancements:

  • Replace sprintf with snprintf for proc paths in tests/init.c
  • Replace sprintf with snprintf for fuzzer ID generation in tests/tests_libcrun_fuzzer.c
  • Replace sprintf with snprintf for path resolution in src/libcrun/chroot_realpath.c

Copy link

sourcery-ai bot commented Sep 26, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replace all unsafe sprintf calls with snprintf using fixed buffer limits (sizeof buffers or PATH_MAX) across test initialization, fuzzer ID generation, and core chroot_realpath logic to mitigate potential buffer overflow vulnerabilities.

File-Level Changes

Change Details Files
Bound path formatting in tests/init.c by replacing sprintf with snprintf to prevent overflows
  • Replace sprintf for proc_path uid_map formatting
  • Replace sprintf for proc_path gid_map formatting
  • Replace sprintf for proc_path user namespace formatting
  • Replace sprintf for cgroup directory path creation
  • Replace sprintf for cgroup procs file path formatting
tests/init.c
Use snprintf for fuzzer ID generation in tests/tests_libcrun_fuzzer.c
  • Replace sprintf with snprintf for id buffer formatting
tests/tests_libcrun_fuzzer.c
Harden chroot_realpath logic with snprintf to limit resolved path length
  • Replace sprintf with snprintf using PATH_MAX in error-handling output
src/libcrun/chroot_realpath.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • In chroot_realpath.c, consider replacing the hard-coded PATH_MAX with sizeof(resolved_path) or an explicit bufsize parameter to ensure snprintf’s size matches the actual buffer length.
  • The change to the HF_ITER extern declaration in tests_libcrun_fuzzer.c looks like an unrelated formatting tweak—please revert or separate it into its own PR to keep the snprintf changes focused.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In chroot_realpath.c, consider replacing the hard-coded PATH_MAX with sizeof(resolved_path) or an explicit bufsize parameter to ensure snprintf’s size matches the actual buffer length.
- The change to the HF_ITER extern declaration in tests_libcrun_fuzzer.c looks like an unrelated formatting tweak—please revert or separate it into its own PR to keep the snprintf changes focused.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

Ephemeral COPR build failed. @containers/packit-build please check.

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

The first commit says "tests:" in the subject line but modifies src/libcrun/chroot_realpath.c.

In general, we are not interested in hardening tests security.

}
#ifdef FUZZER
extern void HF_ITER (uint8_t **buf, size_t *len);
extern void HF_ITER (uint8_t * *buf, size_t * len);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this for?

tests/init.c Outdated
{
/* change one page each 0.1 seconds */
nanosleep ((const struct timespec[]) { { 0, 100000000L } }, NULL);
nanosleep ((const struct timespec[]){ { 0, 100000000L } }, NULL);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is this for?

Copy link
Collaborator

@kolyshkin kolyshkin left a comment

Choose a reason for hiding this comment

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

OK, this is really hard to review since you are modifying the single line in chroot_realpath.c in three subsequent commits. Marking this as draft for now; once ready for review, please click to "ready for review".

@kolyshkin kolyshkin marked this pull request as draft September 27, 2025 00:46
@osamakader osamakader force-pushed the sprintf-to-snprintf branch 2 times, most recently from b302be8 to 11a40b1 Compare September 27, 2025 11:43
@osamakader osamakader marked this pull request as ready for review September 27, 2025 11:44
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • In chroot_realpath, verify snprintf’s return value to detect and handle any potential buffer truncation instead of assuming full writes.
  • Prefer using sizeof(resolved_path) (the actual buffer length) instead of hardcoded PATH_MAX to keep snprintf size in sync with the buffer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In chroot_realpath, verify snprintf’s return value to detect and handle any potential buffer truncation instead of assuming full writes.
- Prefer using sizeof(resolved_path) (the actual buffer length) instead of hardcoded PATH_MAX to keep snprintf size in sync with the buffer.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@osamakader
Copy link
Author

@kolyshkin I forgot to rebase the commits, now should be ok. Please re-review.
Thanks.

@osamakader osamakader changed the title tests: Replace sprintf with snprintf for security chroot_realpath and tests: Replace sprintf with snprintf for security Sep 27, 2025
Replace sprintf with snprintf to prevent buffer overflows in
chroot_realpath.c. This follows the security hardening mentioned
in NEWS.md where sprintf was replaced with safer alternatives.

The function uses PATH_MAX as the buffer size since sizeof on
array function parameters returns the pointer size, not the
actual buffer size.

Signed-off-by: Osama Abdelkader <[email protected]>
Replace sprintf with snprintf in test files to prevent buffer
overflows. This follows the security hardening mentioned in
NEWS.md where sprintf was replaced with safer alternatives.

Changes made:
- tests/init.c: Replace sprintf with snprintf for proc paths
- tests/tests_libcrun_fuzzer.c: Replace sprintf with snprintf for ID generation

Signed-off-by: Osama Abdelkader <[email protected]>
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