Skip to content

Conversation

@Abdullah-Ebryx
Copy link
Contributor

@Abdullah-Ebryx Abdullah-Ebryx commented Oct 15, 2025

Added a new argument to chroot_realpath to support small buffers.
As discussed in the previous pull request, this change helps avoid technical debt and enforces a security boundary by design, rather than relying on the programmer to implement it correctly.

Summary by Sourcery

Harden chroot_realpath against buffer overflows by adding an explicit buffer size parameter to its signature, updating all callers and the test harness to supply buffer length, and using the size in snprintf.

Enhancements:

  • Add size_resolved_path parameter to chroot_realpath signature in criu.c, utils.c, and chroot_realpath.c
  • Use size_resolved_path in snprintf calls within chroot_realpath to cap output length
  • Update all chroot_realpath invocations to pass the buffer size argument

Tests:

  • Update test_chroot_realpath in the fuzzer to supply buffer size to the new chroot_realpath signature

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 15, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Introduces a size parameter to chroot_realpath and updates its implementation and all callers to use explicit buffer lengths for safer path resolution.

Class diagram for updated chroot_realpath function signature and usage

classDiagram
    class chroot_realpath {
        +char *chroot_realpath(const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path)
    }
    class libcrun_container_checkpoint_linux_criu {
        +calls chroot_realpath(..., buf, sizeof(buf))
    }
    class libcrun_container_restore_linux_criu {
        +calls chroot_realpath(..., buf, sizeof(buf))
    }
    class safe_openat_fallback {
        +calls chroot_realpath(..., buffer, sizeof(buffer))
    }
    libcrun_container_checkpoint_linux_criu --> chroot_realpath
    libcrun_container_restore_linux_criu --> chroot_realpath
    safe_openat_fallback --> chroot_realpath
Loading

File-Level Changes

Change Details Files
Extended chroot_realpath signature to accept buffer size
  • Added size_resolved_path parameter to function prototypes in criu.c and utils.c
  • Updated chroot_realpath definition to include size_resolved_path
  • Adjusted test declarations to match new signature
src/libcrun/criu.c
src/libcrun/utils.c
src/libcrun/chroot_realpath.c
tests/tests_libcrun_fuzzer.c
Updated all chroot_realpath calls to pass buffer lengths
  • Replaced calls that passed only buf with buf and sizeof(buf)
  • Ensured correct sizeof() usage in criu.c, utils.c, and tests
src/libcrun/criu.c
src/libcrun/utils.c
tests/tests_libcrun_fuzzer.c
Switched snprintf usage to use size_resolved_path
  • Replaced hardcoded PATH_MAX with size_resolved_path in snprintf calls
  • Maintained overflow check logic against size_resolved_path
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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `src/libcrun/chroot_realpath.c:138` </location>
<code_context>
 			/* If a component doesn't exist, then return what we could translate. */
 			if (errno == ENOENT) {
-				int ret = snprintf (resolved_path, PATH_MAX, "%s%s%s", got_path, path[0] == '/' || path[0] == '\0' ? "" : "/", path);
+				int ret = snprintf (resolved_path, size_resolved_path, "%s%s%s", got_path, path[0] == '/' || path[0] == '\0' ? "" : "/", path);
 				if (ret >= PATH_MAX) {
 					__set_errno(ENAMETOOLONG);
</code_context>

<issue_to_address>
**issue (bug_risk):** snprintf return value should be compared to size_resolved_path, not PATH_MAX.

Update the condition to 'if (ret >= size_resolved_path)' to ensure truncation is checked against the actual buffer size, avoiding incorrect results when size_resolved_path is less than PATH_MAX.
</issue_to_address>

### Comment 2
<location> `tests/tests_libcrun_fuzzer.c:126` </location>
<code_context>
   if (path == NULL)
     return 0;

-  chroot_realpath (".", path, resolved_path);
+  chroot_realpath (".", path, resolved_path,sizeof(resolved_path));
   (void) resolved_path;
   return 0;
</code_context>

<issue_to_address>
**suggestion (testing):** No explicit test for small buffer sizes or buffer overflow conditions.

Please add tests that pass small buffer sizes to chroot_realpath to verify it handles these cases safely and returns appropriate errors.
</issue_to_address>

### Comment 3
<location> `tests/tests_libcrun_fuzzer.c:114` </location>
<code_context>
 #  endif

 /* Defined in chroot_realpath.c  */
-char *chroot_realpath (const char *chroot, const char *path, char resolved_path[]);
+char *chroot_realpath (const char *chroot, const char *path, char resolved_path[],size_t size_resolved_path);

 static const char *console_socket = NULL;
</code_context>

<issue_to_address>
**suggestion (testing):** Fuzzer test does not assert or check the result of chroot_realpath.

Add assertions or checks for chroot_realpath's return value and errno, particularly with edge-case buffer sizes, to improve test coverage and catch regressions.
</issue_to_address>

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.

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.

1 participant