-
Notifications
You must be signed in to change notification settings - Fork 377
BufferOverflow Hardening #1896
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
base: main
Are you sure you want to change the base?
BufferOverflow Hardening #1896
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideIntroduces 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 usageclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this 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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
please squash and remove all the unrelated changes |
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:
Tests: