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

proposal: os: add methods to safely convert between files and roots #70370

Open
Jorropo opened this issue Nov 15, 2024 · 2 comments
Open

proposal: os: add methods to safely convert between files and roots #70370

Jorropo opened this issue Nov 15, 2024 · 2 comments
Labels
Milestone

Comments

@Jorropo
Copy link
Member

Jorropo commented Nov 15, 2024

Proposal Details

I have code which opens both files and directories as files to perform various actions such as fetching and comparing generations (I need to upstream to linux once I write tests) or other metadata with SyscallConn.

Trying to use #67002 to make it less conservative with some TOCTOU edge cases prove challenging since *os.Root does not provide needed SyscallConn to fetch the generation of directories and more architecturally the current codepath is to open everything as a *os.File and then use statx to decide what to do next.


package os

// AsRoot open the File as a Root if it is a directory otherwise it errors.
// The Root is returned with a new lifetime such that each need to be closed independently.
func (*File) AsRoot() (*Root, error)

// AsFile opens the Root as a File.
// The File is returned with a new lifetime such that each need to be closed independently.
func (*Root) AsFile() (*File, error)
@gopherbot gopherbot added this to the Proposal milestone Nov 15, 2024
@Jorropo
Copy link
Member Author

Jorropo commented Nov 15, 2024

I discussed this with @neild already and:
I think it might be possible to convert *os.File*os.Root by using SyscallConn then calling os.OpenRoot with fmt.Sprintf("/proc/self/fd/%d") on Linux.
Not portable tho.

He pointed out *os.Root*os.File should be possible by calling (*os.Root).Open(root, ".").

@Jorropo
Copy link
Member Author

Jorropo commented Nov 15, 2024

I am not sure if lifetimes should be independent or not.
I guess from a point of implementing in the std this is the easiest, but as a consumer I would sometime want the opposite.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

2 participants