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 a hardcoded constant instead of calling OpenProcessToken. #90144

Closed
wants to merge 1 commit into from
Closed

Use a hardcoded constant instead of calling OpenProcessToken. #90144

wants to merge 1 commit into from

Conversation

smmalis37
Copy link
Contributor

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making api calls to get the same information.

This also (I think) removes the need for rust binaries to directly link against advapi32. Let me know if some changes are needed in those build-related bits, that I'm much less sure on.

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 21, 2021
@joshtriplett
Copy link
Member

cc @ChrisDenton

Is this a reasonable assumption to make?

@smmalis37
Copy link
Contributor Author

Also worth noting that Go already does this: https://github.com/golang/sys/blob/master/windows/security_windows.go#L668

@joshtriplett
Copy link
Member

@smmalis37 That's helpful context, thank you.

@ChrisDenton
Copy link
Member

Yeah, it seems fine as it's hardcoded in the official Win32 API. Though it appears it was introduced in Windows 8 so my only concern is if the value works on Windows 7,

@smmalis37
Copy link
Contributor Author

Unfortunately I don't know the answer to that. Will CI test Windows 7? Or does someone have a machine/vm to test on?

@ChrisDenton
Copy link
Member

ChrisDenton commented Oct 21, 2021

Sadly it doesn't work on Windows 7. Test program:

token.rs
fn main() {
    unsafe {
        let mut buffer = vec![0_u16; 260];
        let mut len: u32 = buffer.len() as _;
        let result = GetUserProfileDirectoryW(-4, buffer.as_mut_ptr(), &mut len);
        if result != 0 {
            println!("{}", String::from_utf16_lossy(&buffer[..len as _]));
        } else {
            println!("{}", std::io::Error::last_os_error());
        }
    }
}
extern "system" {
    fn GetUserProfileDirectoryW(hToken: isize, lpProfileDir: *mut u16, lpcchSize: *mut u32) -> i32;
}

Windows 7 output:

The handle is invalid. (os error 6)

I guess this could be worked around by testing for that error and then lazily loading the necessary functions. Obviously the drawback would be that it makes things more complicated.

Another option is to wait to see if the min target api version RFC is accepted. This (along with build-std) would allow conditional compilation based on the target OS version. Or wait until Windows 7 support is removed, but that is not likely to happen for a long time yet.

@smmalis37
Copy link
Contributor Author

Dang, oh well.

@smmalis37 smmalis37 closed this Oct 21, 2021
@smmalis37
Copy link
Contributor Author

Looks like go restricts this to Windows 8 already: https://github.com/golang/sys/blob/master/windows/syscall_windows_test.go#L119

@smmalis37
Copy link
Contributor Author

It seems there is another option here, calling NtQueryInformationProcess with the ProcessAccessToken info class should return the same token, and allow us to still drop the advapi dependency. However this info class is undocumented, despite existing unchanged since pre-XP. I don't know if that's a dealbreaker or worthwhile or not?

@ChrisDenton
Copy link
Member

ChrisDenton commented Oct 21, 2021

Hm, it's undocumented but if it's only used as a fallback then it could be worth it.

On the other hand, GetUserProfileDirectoryW is only used by env::home_dir which is now deprecated. So the import should not appear in most Rust programs and can be avoided by using a third party implementation if it's needed.

@joshtriplett
Copy link
Member

@smmalis37 Would you send a PR adding a comment to the current usage, noting that when we drop support for Windows 7, we can switch to a hardcoded value here? I'd be happy to merge that PR.

@smmalis37
Copy link
Contributor Author

@joshtriplett Can do, gimme a few minutes.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 22, 2021
Add comment documenting why we can't use a simpler solution

See rust-lang#90144 for context.

r? `@joshtriplett`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 22, 2021
Add comment documenting why we can't use a simpler solution

See rust-lang#90144 for context.

r? ``@joshtriplett``
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 23, 2021
Add comment documenting why we can't use a simpler solution

See rust-lang#90144 for context.

r? ```@joshtriplett```
@smmalis37 smmalis37 deleted the wintokens branch December 16, 2021 17:52
smmalis37 added a commit to smmalis37/rust that referenced this pull request Dec 17, 2023
Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
smmalis37 added a commit to smmalis37/rust that referenced this pull request Dec 19, 2023
Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
smmalis37 added a commit to smmalis37/rust that referenced this pull request Feb 16, 2024
Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
Nadrieril added a commit to Nadrieril/rust that referenced this pull request Feb 17, 2024
Use a hardcoded constant instead of calling OpenProcessToken.

Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
Nadrieril added a commit to Nadrieril/rust that referenced this pull request Feb 17, 2024
Use a hardcoded constant instead of calling OpenProcessToken.

Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
Rollup merge of rust-lang#119032 - smmalis37:patch-1, r=ChrisDenton

Use a hardcoded constant instead of calling OpenProcessToken.

Now that Win 7 support is dropped, we can resurrect rust-lang#90144.

GetCurrentProcessToken is defined in processthreadsapi.h as:

FORCEINLINE
HANDLE
GetCurrentProcessToken (
    VOID
    )
{
    return (HANDLE)(LONG_PTR) -4;
}

Since it's very unlikely that this constant will ever change, let's just use it instead of making calls to get the same information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants