Skip to content

Buffer Overflow Hardening - #1890

Closed
Abdullah-Ebryx wants to merge 0 commit into
containers:mainfrom
Abdullah-Ebryx:main
Closed

Buffer Overflow Hardening#1890
Abdullah-Ebryx wants to merge 0 commit into
containers:mainfrom
Abdullah-Ebryx:main

Conversation

@Abdullah-Ebryx

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

Copy link
Copy Markdown
Contributor

Hardening against buffer overflow (BOF) vulnerabilities requires more than just limiting the scanfn function with PATH_MAX. This approach doesn't account for scenarios where the buffer is under-allocated

Summary by Sourcery

Harden chroot_realpath against potential buffer overflows by extending its API to accept an explicit buffer size, updating its implementation to use that size for bounds checking, and adjusting all callers (including tests) to pass the buffer length.

Bug Fixes:

  • Prevent potential buffer overflows in chroot_realpath by replacing fixed PATH_MAX usage with explicit size-based snprintf checks.

Enhancements:

  • Update chroot_realpath signature in criu.c, utils.c, and tests to accept a size_resolved_path parameter and pass sizeof(buffer) in all calls.

Tests:

  • Modify chroot_realpath fuzz tests to use the new signature with buffer size argument.

@sourcery-ai

sourcery-ai Bot commented Oct 7, 2025

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

PR hardens against buffer overflows by extending chroot_realpath to accept a buffer size parameter, updating all its call sites to pass the actual buffer length, and replacing hardcoded PATH_MAX in snprintf calls with the dynamic size.

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[], unsigned long int 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

Flow diagram for buffer size propagation in chroot_realpath usage

flowchart TD
    A[Call site: libcrun_container_checkpoint_linux_criu] -->|pass buf, sizeof(buf)| B[chroot_realpath]
    C[Call site: libcrun_container_restore_linux_criu] -->|pass buf, sizeof(buf)| B
    D[Call site: safe_openat_fallback] -->|pass buffer, sizeof(buffer)| B
    B --> E[snprintf uses size_resolved_path]
    E --> F[Buffer overflow risk reduced]
Loading

File-Level Changes

Change Details Files
Extended chroot_realpath signature to include buffer size parameter
  • Modified function prototype in criu.c
  • Modified function declaration in utils.c
  • Updated declaration in tests_libcrun_fuzzer.c
  • Adjusted definition signature in chroot_realpath.c
src/libcrun/criu.c
src/libcrun/utils.c
src/libcrun/chroot_realpath.c
tests/tests_libcrun_fuzzer.c
Updated call sites to pass buffer size to chroot_realpath
  • Added sizeof(buf) in checkpoint and restore invocations
  • Included buffer size in safe_openat_fallback call
  • Passed sizeof(resolved_path) in fuzzer test
src/libcrun/criu.c
src/libcrun/utils.c
tests/tests_libcrun_fuzzer.c
Replaced hardcoded PATH_MAX in snprintf with dynamic size parameter
  • Changed snprintf to use size_resolved_path for formatting
  • Adjusted overflow check to compare 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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

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 found some issues that need to be addressed.

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

## Individual Comments

### Comment 1
<location> `src/libcrun/chroot_realpath.c:50` </location>
<code_context>
 #define MAX_READLINKS 32

-char *chroot_realpath(const char *chroot, const char *path, char resolved_path[])
+char *chroot_realpath(const char *chroot, const char *path, char resolved_path[],unsigned long int size_resolved_path )
 {
 	char copy_path[PATH_MAX];
</code_context>

<issue_to_address>
**suggestion:** Consider using size_t for buffer size parameter instead of unsigned long int.

Using size_t is standard for buffer sizes in C, helps prevent type mismatches, and clarifies the parameter's purpose.

Suggested implementation:

```c
char *chroot_realpath(const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path )

```

If `chroot_realpath` is declared in a header file (e.g., `chroot_realpath.h`), update the parameter type there as well:

<<<<<<< SEARCH
char *chroot_realpath(const char *chroot, const char *path, char resolved_path[], unsigned long int size_resolved_path );
=======
char *chroot_realpath(const char *chroot, const char *path, char resolved_path[], size_t size_resolved_path );
>>>>>>> REPLACE

Also, ensure that all calls to `chroot_realpath` throughout the codebase use a `size_t` value for the `size_resolved_path` argument.
</issue_to_address>

### Comment 2
<location> `src/libcrun/criu.c:643` </location>
<code_context>
             return crun_make_error (err, 0, "CRIU does not support `src-nofollow` for bind mounts");

-          dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf);
+          dest_in_root = chroot_realpath (status->rootfs, def->mounts[i]->destination, buf,sizeof(buf));
           if (UNLIKELY (dest_in_root == NULL))
             {
</code_context>

<issue_to_address>
**issue (bug_risk):** sizeof(buf) may not always be correct if buf is not a statically sized array.

If buf is a pointer or dynamically allocated, sizeof(buf) will not give the actual buffer size. Pass the correct buffer size explicitly to avoid 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[],unsigned long int size_resolved_path);

</code_context>

<issue_to_address>
**issue (bug_risk):** Incorrect function declaration in test file.

The parameter 'sizeof()' is invalid in the function declaration and will prevent compilation. Update the declaration to match the implementation: char *chroot_realpath(const char *chroot, const char *path, char resolved_path[], unsigned long int size_resolved_path);
</issue_to_address>

### Comment 4
<location> `tests/tests_libcrun_fuzzer.c:126-128` </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):** Test does not check for buffer overflow or error conditions.

Please add assertions to verify chroot_realpath's behavior with under-allocated buffers, long paths, and error returns, including NULL and errno values.

```suggestion
  // Test with a sufficiently large buffer
  int ret = chroot_realpath(".", path, resolved_path, sizeof(resolved_path));
  assert(ret != NULL || errno != 0);

  // Test with an under-allocated buffer
  char small_buf[4];
  errno = 0;
  ret = chroot_realpath(".", path, small_buf, sizeof(small_buf));
  assert(ret == NULL);
  assert(errno == ERANGE || errno != 0);

  // Test with a very long path
  char long_path[4096];
  memset(long_path, 'a', sizeof(long_path) - 1);
  long_path[sizeof(long_path) - 1] = '\0';
  errno = 0;
  ret = chroot_realpath(".", long_path, resolved_path, sizeof(resolved_path));
  assert(ret == NULL || ret != NULL);
  // If ret is NULL, errno should be set
  if (ret == NULL) {
    assert(errno != 0);
  }

  // Test with NULL path
  errno = 0;
  ret = chroot_realpath(".", NULL, resolved_path, sizeof(resolved_path));
  assert(ret == NULL);
  assert(errno != 0);

  // Test with NULL resolved_path buffer
  errno = 0;
  ret = chroot_realpath(".", path, NULL, sizeof(resolved_path));
  assert(ret == NULL);
  assert(errno != 0);

  return 0;
```
</issue_to_address>

### Comment 5
<location> `tests/tests_libcrun_fuzzer.c:127-111` </location>
<code_context>

-  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):** Test does not assert correctness of resolved_path.

Please add assertions verifying resolved_path contains the expected value for valid inputs and remains unchanged or NULL for invalid inputs.

Suggested implementation:

```c
  int ret = chroot_realpath (".", path, resolved_path, sizeof(resolved_path));
  if (ret == 0) {
    // For valid input, resolved_path should contain the expected real path.
    // Replace "/expected/real/path" with the actual expected value for your test case.
    assert(strcmp(resolved_path, "/expected/real/path") == 0 && "resolved_path does not match expected value");
  } else {
    // For invalid input, resolved_path should remain unchanged or be NULL.
    // If resolved_path is expected to be NULL, check for that.
    assert(resolved_path[0] == '\0' && "resolved_path should be empty for invalid input");
  }
  return 0;
}

```

- You will need to replace `"/expected/real/path"` with the actual expected resolved path for your test case.
- If your implementation of `chroot_realpath` sets `resolved_path` to NULL for invalid input, adjust the assertion accordingly (e.g., `assert(resolved_path == NULL)`).
- Make sure to include the necessary headers for `assert` and `strcmp` if not already present: `#include <assert.h>` and `#include <string.h>`.
</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.

Comment thread src/libcrun/chroot_realpath.c Outdated
Comment thread src/libcrun/criu.c Outdated
Comment thread tests/tests_libcrun_fuzzer.c
Comment thread tests/tests_libcrun_fuzzer.c
Comment thread src/libcrun/chroot_realpath.c Outdated
#define MAX_READLINKS 32

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 )

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can we just add a documentation hint that resolved_path must be at least PATH_MAX bytes? There is no point in supporting a smaller size

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Certainly, we could take that route. However, the changes required to support the new function arguments were minimal, and this approach helps avoid future technical debt by enforcing the required buffer length while still allowing dynamic buffer allocation for other use cases. There's also the consideration that relying solely on documentation assumes the caller will implement things correctly, whereas this change enforces correctness at the API level and prevents misuse.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yeah let's that that route

@kolyshkin

Copy link
Copy Markdown
Collaborator

@Abdullah-Ebryx can you please squash the commits?

@Abdullah-Ebryx Abdullah-Ebryx left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.

@packit-as-a-service

Copy link
Copy Markdown

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

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