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

Add fs.ResolvePath to resolve symbolic links #275

Merged
merged 4 commits into from
Apr 14, 2023

Conversation

helsaawy
Copy link
Contributor

@helsaawy helsaawy commented Feb 6, 2023

filepath.EvalSymlinks does not work well on Windows, and can loop indefinitely in certain situations and error out.

Add ResolvePath, which uses the Win32 GetFinalPathNameByHandle to resolve the final path of a file by first opening it.
Add "internal/stringbuffer".WString struct to maintain a pool of []uint16 buffers for use with interacting with the Win32APIs
Add "internal/fs" pkg with Win32 CreateFile, GetFinalPathNameByHandle, and associated flags and access masks

Core implementation is based off of: containerd/containerd#5411

Signed-off-by: Hamza El-Saawy hamzaelsaawy@microsoft.com

@helsaawy helsaawy requested a review from a team as a code owner February 6, 2023 19:52
`filepath.EvalSymlinks` does not work well on Windows, and can enter
infinite loops in certain situations and error out.
Use Win32 API GetFinalPathNameByHandle to handle path resolution.

Implementation based off on: containerd/containerd#5411

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
@kevpar
Copy link
Member

kevpar commented Feb 6, 2023

@helsaawy
Copy link
Contributor Author

helsaawy commented Feb 6, 2023

Can we carry over the tests from https://github.com/containerd/containerd/blob/main/pkg/os/os_windows_test.go?

I wanted to avoid that since that would hcsshim as a dependency for this package, which could cause weird cyclic import issues.
The alternative would be add computestorage bindings here, but I would rather save that for a separate PR

@helsaawy helsaawy mentioned this pull request Feb 7, 2023
@helsaawy
Copy link
Contributor Author

helsaawy commented Feb 7, 2023

Can we carry over the tests from https://github.com/containerd/containerd/blob/main/pkg/os/os_windows_test.go?

I wanted to avoid that since that would hcsshim as a dependency for this package, which could cause weird cyclic import issues. The alternative would be add computestorage bindings here, but I would rather save that for a separate PR

@kevpar
I added tests and needed APIs to this PR: #276

pkg/fs/resolve.go Outdated Show resolved Hide resolved
internal/fs/security.go Outdated Show resolved Hide resolved
internal/fs/security.go Outdated Show resolved Hide resolved
internal/fs/fs.go Outdated Show resolved Hide resolved
// Specific Object Access
// from ntioapi.h

FILE_READ_DATA AccessMask = (0x0001) // file & pipe
Copy link
Member

Choose a reason for hiding this comment

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

Same with constants, if it's in golang.org/x/sys/windows, let's use those instead.

Copy link
Contributor Author

@helsaawy helsaawy Feb 13, 2023

Choose a reason for hiding this comment

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

As in, create values that just reference windows.* for their definition?

Copy link
Member

Choose a reason for hiding this comment

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

Do we need to define a FILE_READ_DATA const at all? I would think we can just use the const from the other package.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the sys/windows constants are not typed, so we lose the ability to type-check and catch bugs that way
and the general constants were all copied in from the corresponding headers, since it was the same amount of work to copy in the ones we needed vs all of them

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, the general philosophy has been to carry as little as we can here, and rely on golang.org/x/sys/windows for the bulk of Windows support. I'd like to not get in the habit of re-creating functionality that lives in windows as well.

Can we get away without using typed constants here? Alternately, can we just add the minimum set of typed constants here that we need? And then we could try to get windows to type theirs (though that might be tough, as it is a breaking change).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I doubt they would, since the majority of that code seems auto-generated.
I just copied-pasted the constants here from the relevant header files, so it was trivial to add of them, but ill whittle the number down to just what we use here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do away with the types all together, but I am hesitant to do so, since the function signatures tend to be pretty crowded (eg, CreateFile(*uint16, uint32, uint32, *windows.SecurityAttributes, uint32, uint32, wiindows.Handle)) and having types prevents mixing up parameters

Copy link
Member

@kevpar kevpar Apr 13, 2023

Choose a reason for hiding this comment

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

In the interest of not blocking the PR I think this is okay as is. I am interested in longer term how we can push things upstream to stdlib and x/sys/windows rather than accumulating more bits specific to this repo, so that would be a good thing to think about.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Update logic to try querying for normalized path initially, then use
opened path if access is denied.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
Copy link
Member

@kevpar kevpar left a comment

Choose a reason for hiding this comment

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

The resolve logic looks fine to me. It would be good to have someone else specifically look at the string buffer parts since I didn't focus on that.

Copy link
Contributor

@msscotb msscotb left a comment

Choose a reason for hiding this comment

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

lgtm

@helsaawy helsaawy merged commit b884eb7 into microsoft:main Apr 14, 2023
@helsaawy helsaawy deleted the fs-resolve branch April 14, 2023 16:58
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.

None yet

3 participants