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

Use NtCreateFile on Windows #226

Closed
sunfishcode opened this issue Jan 24, 2022 · 5 comments · Fixed by #293
Closed

Use NtCreateFile on Windows #226

sunfishcode opened this issue Jan 24, 2022 · 5 comments · Fixed by #293
Labels
enhancement New feature or request

Comments

@sunfishcode
Copy link
Member

sunfishcode commented Jan 24, 2022

As @kubkon observed, NtOpenFile provides a more efficient and likely more reliable way to implement openat-like behavior on Windows. As an example, Rust is now using it this way in its remove_dir_all implementation.

Microsoft officially documents NtOpenFile as an Internal API which may change between releases or even service packs of Windows, however this particular function, NtOpenFile, has apparently been stable for a long time, and since Rust itself is now depending on it, it would seem to be sufficiently stable for cap-std to use as well.

@sunfishcode sunfishcode added the enhancement New feature or request label Jan 24, 2022
@sunfishcode sunfishcode changed the title Use NtOpenFile on Windows Use NtCreateFile on Windows Aug 24, 2022
@sunfishcode
Copy link
Member Author

Use NtCreateFile instead of NtOpenFile; see rust-lang/rust#93206.

@ChrisDenton
Copy link

My current thinking on this has changed since that PR. At the time using any Nt* function was controversial so I was eager to be cautious above all else, especially given it's the standard library. Since then I've had more discussion with folks at Microsoft and I now feel much more relaxed about use of Nt* functions where it can be justified. So long as they're well documented and have been around for awhile, they are not going to break.

Btw, the Calling Internal APIs page, while scary, is somewhat outdated. There now exists an ntdll.lib which is distributed with the Windows SDK and allows linking with many ntdll functions without using GetProcAddress. If using a gnu (rather than msvc) toolchain it can not necessarily be assumed that the SDK is installed but if the function is part of the windows and windows-sys crates then this will be in the import libraries distributed by those crates (and these are also official Microsoft projects).

@sunfishcode
Copy link
Member Author

Oh wow, I wasn't previously aware that windows-sys has bindings for NtCreateFile and NtOpenFile.

@sunfishcode
Copy link
Member Author

Also, cap-std needs the ability to create files, so it appears we do need NtCreateFile.

One thing we'll need to figure out is how to map from security QoS flags to NtCreateFile. There's a SecurityQualityOfService inside the ObjectAttributes parameter, though it doesn't look like a trivial mapping.

@ChrisDenton
Copy link

It shouldn't be too difficult. SECURITY_QUALITY_OF_SERVICE is basically just an enum and two bools. Very roughly it goes something like:

ImpersonationLevel = (qos_flags >> 16) & 0b11;
ContextTrackingMode = (qos_flags & SECURITY_CONTEXT_TRACKING) != 0
EffectiveOnly = (qos_flags & SECURITY_EFFECTIVE_ONLY) != 0

sunfishcode added a commit that referenced this issue Feb 3, 2023
Windows' `NtCreateFile` has an ability to take a directory and a
relative path, so use that to implement `open_unchecked` instead of
using path concatenation.

We still use concatenation for other functions, but this is the first
step to rewriting those to avoid it.

Fixes #226.
sunfishcode added a commit that referenced this issue Feb 3, 2023
Windows' `NtCreateFile` has an ability to take a directory and a
relative path, so use that to implement `open_unchecked` instead of
using path concatenation.

We still use concatenation for other functions, but this is the first
step to rewriting those to avoid it.

Fixes #226.
sunfishcode added a commit that referenced this issue Feb 3, 2023
Windows' `NtCreateFile` has an ability to take a directory and a
relative path, so use that to implement `open_unchecked` instead of
using path concatenation.

We still use concatenation for other functions, but this is the first
step to rewriting those to avoid it.

Fixes #226.
sunfishcode added a commit that referenced this issue Feb 10, 2023
Windows' `NtCreateFile` has an ability to take a directory and a
relative path, so use that to implement `open_unchecked` instead of
using path concatenation.

We still use concatenation for other functions, but this is the first
step to rewriting those to avoid it.

Fixes #226.
sunfishcode added a commit that referenced this issue Mar 16, 2023
Windows' `NtCreateFile` has an ability to take a directory and a
relative path, so use that to implement `open_unchecked` instead of
using path concatenation.

We still use concatenation for other functions, but this is the first
step to rewriting those to avoid it.

Fixes #226.
Kryptos-FR added a commit to fragcolor-xyz/shards that referenced this issue Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants