Skip to content
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

Close our side of memfd #88

Merged
merged 1 commit into from
Nov 8, 2021
Merged

Close our side of memfd #88

merged 1 commit into from
Nov 8, 2021

Conversation

swsnr
Copy link
Collaborator

@swsnr swsnr commented Nov 5, 2021

Sending a file descriptor through a socket effectively duplicates the
file descriptor; it does not move the file descriptor out of the current
process.

Hence we need to close our side of the file descriptor explicitly to
avoid leaking file descriptors.

Sending a file descriptor through a socket effectively duplicates the
file descriptor; it does not move the file descriptor out of the current
process.

Hence we need to close our side of the file descriptor explicitly to
avoid leaking file descriptors.
@swsnr swsnr requested a review from lucab November 5, 2021 21:39
@swsnr swsnr self-assigned this Nov 5, 2021
@lucab
Copy link
Owner

lucab commented Nov 8, 2021

Ouch! Good find. Did you spot this on a running process? Does it need a minor bugfix release on its own?

@swsnr
Copy link
Collaborator Author

swsnr commented Nov 8, 2021

No I found this accidentally. It only occurs for very large messages. But still, I'd like to fix it 🙂

@lucab lucab merged commit 4a2685b into master Nov 8, 2021
@lucab lucab deleted the close-memfd branch November 8, 2021 12:37
@swsnr
Copy link
Collaborator Author

swsnr commented Nov 9, 2021

Thanks for merging. I don't think it's that urgent, so I'd make #87 next and then cut a new release?

dfreese added a commit to dfreese/libsystemd-rs that referenced this pull request Dec 23, 2022
This was added in lucab#88, however, drop is already implicitly called on all
objects when they go out of scope.  This, for memfd happens a couple
lines later, so the drop is redundant.  The primary problem that lucab#88
solved was changing `into_raw_fd()` to `as_raw_fd()`, which no longer
transferred ownership, requiring a manual close.

From the [std::os::fd::IntoRawFd](https://doc.rust-lang.org/stable/std/os/fd/trait.IntoRawFd.html) docs:
> This function is typically used to transfer ownership of the
underlying file descriptor to the caller. When used in this way, callers
are then the unique owners of the file descriptor and must close it once
it’s no longer needed.

It seems that the primary problem that close was not being called on
memfd when it was consumed into a RawFd.
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