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

Bug: Wrong argument type to some functions (at least AdjustTokenPrivileges) in 0.40.0 #2034

Closed
poliorcetics opened this issue Sep 19, 2022 · 2 comments · Fixed by #2037
Closed
Labels
bug Something isn't working

Comments

@poliorcetics
Copy link
Contributor

Which crate is this about?

windows

Crate version

0.40.0

Summary

AdjustTokenPrivileges take the wrong argument type for its previousstate parameter

The official windows documentation for AdjustTokenPrivileges explicitely mentions this argument is a *mut TOKEN_PRIVILEGES.

When using a let mut previous_state = TOKEN_PRIVILEGES_DEFAULT, it's not possible to do something like &mut tp_previous as *mut _ as *mut [u8] as &mut [u8] because [u8] is a fat pointer, which cannot be obtained from a thin pointer as &mut TOKEN_PRIVILEGES

Toolchain version/configuration

No response

Reproducible example

No response

Crate manifest

No response

Expected behavior

No response

Actual behavior

No response

Additional comments

No response

@poliorcetics poliorcetics added the bug Something isn't working label Sep 19, 2022
@kennykerr
Copy link
Collaborator

Thanks for reporting! This results from a bug in the win32 metadata. The original header definition is as follows:

WINADVAPI
BOOL
WINAPI
AdjustTokenPrivileges(
    _In_ HANDLE TokenHandle,
    _In_ BOOL DisableAllPrivileges,
    _In_opt_ PTOKEN_PRIVILEGES NewState,
    _In_ DWORD BufferLength,
    _Out_writes_bytes_to_opt_(BufferLength,*ReturnLength) PTOKEN_PRIVILEGES PreviousState,
    _Out_opt_ PDWORD ReturnLength
    );

But the metadata ends up as follows:

public unsafe static extern BOOL AdjustTokenPrivileges(
   [In] HANDLE TokenHandle, 
   [In] BOOL DisableAllPrivileges, 
   [Optional][In] TOKEN_PRIVILEGES* NewState, 
   [In] uint BufferLength, 
   [Optional][Out][MemorySize(BytesParamIndex = 3)] TOKEN_PRIVILEGES* PreviousState, 
   [Optional][Out] uint* ReturnLength);

It's the faulty MemorySize attribute on the PreviousState parameter that's throwing off the Rust bindings.

I can't imagine how the Win32 metadata parser went from _Out_writes_bytes_to_opt_(BufferLength,*ReturnLength) to [MemorySize(BytesParamIndex = 3)]. 🤔

Will transfer to the win32 metadata repo for resolution.

@kennykerr kennykerr transferred this issue from microsoft/windows-rs Sep 19, 2022
@kennykerr
Copy link
Collaborator

Looking into this some more, the metadata is probably as accurate as it can be given that PreviousState is not an array but a variable-sized struct. I'll take care of this on the Rust side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants