Skip to content

Conversation

@lifubang
Copy link
Member

No description provided.

defer dstFile.Close()
// "proc" and "sys" mounts need special handling (without resolving the
// destination) to avoid attacks.
m.dstFile = dstFile
Copy link
Member Author

Choose a reason for hiding this comment

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

The leak happened in here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah I see. I was confused because the line you linked here cannot be a leak, but the re-open is the issue. That's quite tricky, thanks for finding it...

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR moves the defer statement that closes m.dstFile from after the createOpenMountpoint call to the beginning of the mountToRootfs function. The intent is to ensure that m.dstFile is always closed regardless of which execution path is taken through the function.

  • Relocated the defer cleanup for m.dstFile to the function entry point for more comprehensive resource management

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cyphar cyphar added easy-to-review backport/1.2-todo A PR in main branch which needs to be backported to release-1.2 backport/1.3-todo A PR in main branch which needs to be backported to release-1.3 backport/1.4-todo A PR in main branch which needs to backported to release-1.4 labels Nov 18, 2025
@lifubang lifubang force-pushed the fix-fd-leak-in-mountToRootfs branch from c932c56 to b644a90 Compare November 19, 2025 01:48
@cyphar cyphar self-requested a review November 19, 2025 05:31
if err != nil {
return err
}
defer dstFile.Close()
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it's necessary to remove this -- having the defer is clearer and Go allows for calling Close on the same *os.File multiple times without issue (they are no-ops).

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, there’s no harm in keeping it. If removal isn’t necessary, I’ll revert to leaving it as is.

@lifubang lifubang force-pushed the fix-fd-leak-in-mountToRootfs branch from b644a90 to c932c56 Compare November 19, 2025 12:44
@cyphar
Copy link
Member

cyphar commented Nov 19, 2025

I've released v0.5.2 and v0.6.1 of filepath-securejoin which includes the leak fix you sent -- do you want to consolidate all of the leak fixes into one PR so it's a bit easier to review? (For 1.2.x and 1.3.x I would update go-selinux to v1.13.1 so that we can downgrade filepath-securejoin to v0.5.2 -- for 1.4.x I think we can just use the newer one.)

@lifubang
Copy link
Member Author

do you want to consolidate all of the leak fixes into one PR so it's a bit easier to review?

#5026

@cyphar
Copy link
Member

cyphar commented Nov 20, 2025

This has been cherry-picked into #5026, closing in favour of that.

@cyphar cyphar closed this Nov 20, 2025
@cyphar cyphar removed backport/1.2-todo A PR in main branch which needs to be backported to release-1.2 backport/1.3-todo A PR in main branch which needs to be backported to release-1.3 backport/1.4-todo A PR in main branch which needs to backported to release-1.4 labels Nov 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants