Skip to content

linux: fix copy_from_fd ownership#2005

Merged
giuseppe merged 1 commit intocontainers:mainfrom
eriksjolund:fix-fd-ownership
Feb 9, 2026
Merged

linux: fix copy_from_fd ownership#2005
giuseppe merged 1 commit intocontainers:mainfrom
eriksjolund:fix-fd-ownership

Conversation

@eriksjolund
Copy link
Contributor

@eriksjolund eriksjolund commented Feb 9, 2026

Fix some issues regarding file descriptor ownership.

Initial investigation in #2002
(I found these when reading the source code)

@gemini-code-assist
Copy link

Summary of Changes

Hello @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 crun project, specifically related to temporary copy-up operations. It refactors the handling of file descriptors passed between process_single_mount, handle_tmpcopyup, and copy_recursive_fd_to_fd to prevent potential double-closing or leaks. The changes clarify which function is responsible for closing specific file descriptors, improving the robustness and correctness of file handling.

Highlights

  • File Descriptor Ownership: The handle_tmpcopyup function now correctly manages the copy_from_fd by initializing tmpfd with cleanup_close, ensuring the file descriptor is automatically closed when tmpfd goes out of scope.
  • Redundant Close Calls: Explicit close(destfd) and close(tmpfd) calls were removed from handle_tmpcopyup, as the copy_recursive_fd_to_fd function is now responsible for closing these file descriptors.
  • Caller Responsibility: The process_single_mount function now explicitly sets copy_from_fd to -1 after calling handle_tmpcopyup, indicating that handle_tmpcopyup (and subsequently copy_recursive_fd_to_fd) has taken ownership and closed the descriptor.

🧠 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
  • src/libcrun/linux.c
    • The handle_tmpcopyup function was modified to use cleanup_close for tmpfd and remove manual close() calls, delegating closing responsibility to copy_recursive_fd_to_fd.
    • In process_single_mount, copy_from_fd is now explicitly set to -1 after being passed to handle_tmpcopyup to reflect its closure.
  • src/libcrun/utils.c
    • A comment was added to copy_recursive_fd_to_fd to explicitly state that it closes the srcfd and destfd file descriptors it operates on.
Activity
  • No human activity (comments, reviews, etc.) has been recorded on this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@eriksjolund
Copy link
Contributor Author

(I haven't tested that it actually works).

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

@packit-as-a-service
Copy link

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

1 similar comment
@packit-as-a-service
Copy link

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

@packit-as-a-service
Copy link

TMT tests failed. @containers/packit-build please check.

Signed-off-by: Erik Sjölund <erik.sjolund@gmail.com>
@eriksjolund
Copy link
Contributor Author

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.
For me either way is fine.

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

LGTM

@giuseppe giuseppe merged commit 7f5ff11 into containers:main Feb 9, 2026
42 of 47 checks passed
@eriksjolund eriksjolund deleted the fix-fd-ownership branch February 12, 2026 09:43
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