diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-02-08 16:40:49 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-08 16:40:49 +0100 |
| commit | 9cb39a6083c34f2266ec4f2235deda7bdab60b06 (patch) | |
| tree | 7bbf7f516e7c9f3318828c4856e9cf5c7729ce88 /library/std/src/sys | |
| parent | a11e2b1f755e392e7c776b98e76aa86a30b7b22e (diff) | |
| parent | ac02fcc4d8b5cc0f1d727b30f8f2142dc9bacc8e (diff) | |
| download | rust-9cb39a6083c34f2266ec4f2235deda7bdab60b06.tar.gz rust-9cb39a6083c34f2266ec4f2235deda7bdab60b06.zip | |
Rollup merge of #93206 - ChrisDenton:ntopenfile, r=nagisa
Use `NtCreateFile` instead of `NtOpenFile` to open a file Generally the internal `Nt*` functions should be avoided but when we do need to use one we should stick to the most commonly used for the job. To that end, this PR replaces `NtOpenFile` with `NtCreateFile`. NOTE: The initial version of this comment hypothesised that this may help with some recent false positives from malware scanners. This hypothesis proved wrong. Sorry for the distraction.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/windows/c.rs | 12 | ||||
| -rw-r--r-- | library/std/src/sys/windows/fs.rs | 11 |
2 files changed, 17 insertions, 6 deletions
diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs index 09d3661e4fd..dfcd6124454 100644 --- a/library/std/src/sys/windows/c.rs +++ b/library/std/src/sys/windows/c.rs @@ -88,6 +88,7 @@ pub const FILE_SHARE_DELETE: DWORD = 0x4; pub const FILE_SHARE_READ: DWORD = 0x1; pub const FILE_SHARE_WRITE: DWORD = 0x2; +pub const FILE_OPEN: ULONG = 0x00000001; pub const FILE_OPEN_REPARSE_POINT: ULONG = 0x200000; pub const OBJ_DONT_REPARSE: ULONG = 0x1000; @@ -1228,15 +1229,20 @@ compat_fn! { compat_fn! { "ntdll": - pub fn NtOpenFile( + pub fn NtCreateFile( FileHandle: *mut HANDLE, DesiredAccess: ACCESS_MASK, ObjectAttributes: *const OBJECT_ATTRIBUTES, IoStatusBlock: *mut IO_STATUS_BLOCK, + AllocationSize: *mut i64, + FileAttributes: ULONG, ShareAccess: ULONG, - OpenOptions: ULONG + CreateDisposition: ULONG, + CreateOptions: ULONG, + EaBuffer: *mut c_void, + EaLength: ULONG ) -> NTSTATUS { - panic!("`NtOpenFile` not available"); + panic!("`NtCreateFile` not available"); } pub fn RtlNtStatusToDosError( Status: NTSTATUS diff --git a/library/std/src/sys/windows/fs.rs b/library/std/src/sys/windows/fs.rs index fed655af87e..cb83ee2469a 100644 --- a/library/std/src/sys/windows/fs.rs +++ b/library/std/src/sys/windows/fs.rs @@ -712,11 +712,11 @@ impl<'a> Iterator for DirBuffIter<'a> { /// Open a link relative to the parent directory, ensure no symlinks are followed. fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result<File> { - // This is implemented using the lower level `NtOpenFile` function as + // This is implemented using the lower level `NtCreateFile` function as // unfortunately opening a file relative to a parent is not supported by // win32 functions. It is however a fundamental feature of the NT kernel. // - // See https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntopenfile + // See https://docs.microsoft.com/en-us/windows/win32/api/winternl/nf-winternl-ntcreatefile unsafe { let mut handle = ptr::null_mut(); let mut io_status = c::IO_STATUS_BLOCK::default(); @@ -732,14 +732,19 @@ fn open_link_no_reparse(parent: &File, name: &[u16], access: u32) -> io::Result< Attributes: ATTRIBUTES.load(Ordering::Relaxed), ..c::OBJECT_ATTRIBUTES::default() }; - let status = c::NtOpenFile( + let status = c::NtCreateFile( &mut handle, access, &object, &mut io_status, + crate::ptr::null_mut(), + 0, c::FILE_SHARE_DELETE | c::FILE_SHARE_READ | c::FILE_SHARE_WRITE, + c::FILE_OPEN, // If `name` is a symlink then open the link rather than the target. c::FILE_OPEN_REPARSE_POINT, + crate::ptr::null_mut(), + 0, ); // Convert an NTSTATUS to the more familiar Win32 error codes (aka "DosError") if c::nt_success(status) { |
