linux: fix copy_from_fd ownership#2005
Conversation
Summary of ChangesHello @eriksjolund, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a file descriptor ownership problem within the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
(I haven't tested that it actually works). |
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a file descriptor ownership bug, preventing a double-close on copy_from_fd. The changes in handle_tmpcopyup and process_single_mount properly manage the file descriptor's lifecycle, ensuring it's closed exactly once. The use of cleanup_close combined with setting the descriptor to -1 after its ownership is transferred is a robust pattern that correctly handles error paths. The added comments clarifying which function is responsible for closing the file descriptors are a welcome addition for code clarity and maintainability. The fix appears solid and well-implemented.
aefbb33 to
f625f77
Compare
|
Ephemeral COPR build failed. @containers/packit-build please check. |
1 similar comment
|
Ephemeral COPR build failed. @containers/packit-build please check. |
|
TMT tests failed. @containers/packit-build please check. |
Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
f625f77 to
e38674b
Compare
|
For improved readability, I could also diff --git a/src/libcrun/linux.c b/src/libcrun/linux.c
index 7438b8c8..46b5c901 100644
--- a/src/libcrun/linux.c
+++ b/src/libcrun/linux.c
@@ -2124,16 +2124,16 @@ static int
handle_tmpcopyup (libcrun_container_t *container, const char *rootfs, const char *target,
int copy_from_fd, libcrun_error_t *err)
{
- int destfd, ret;
+ int ret;
cleanup_close int tmpfd = copy_from_fd;
- destfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, target,
+ cleanup_close int destfd = safe_openat (get_private_data (container)->rootfsfd, rootfs, target,
O_CLOEXEC | O_DIRECTORY, 0, err);
if (UNLIKELY (destfd < 0))
return crun_error_wrap (err, "open `%s` to write for tmpcopyup", target);
// copy_recursive_fd_to_fd closes tmpfd and destfd
ret = copy_recursive_fd_to_fd (tmpfd, destfd, target, target, err);
- tmpfd = -1;
+ tmpfd = destfd = -1;
return ret;
}but this makes the code less efficient. |
Fix some issues regarding file descriptor ownership.
Initial investigation in #2002
(I found these when reading the source code)