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

winapi audit events returning garbage #119690

Closed
zooba opened this issue May 28, 2024 · 5 comments
Closed

winapi audit events returning garbage #119690

zooba opened this issue May 28, 2024 · 5 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes OS-windows

Comments

@zooba zooba added 3.11 only security fixes 3.10 only security fixes 3.9 only security fixes 3.8 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels May 28, 2024
@zooba zooba self-assigned this May 28, 2024
@zooba
Copy link
Member Author

zooba commented May 28, 2024

At a glance, it looks like _winapi is still using MBCS paths by default. We should switch it all to Unicode. (There may already be an issue for this.)

@eryksun
Copy link
Contributor

eryksun commented May 28, 2024

In 3.11+, the audit call looks correct in _winapi_CreateFile_impl(). The wrapper was updated to link with CreateFileW() and make file_name a wide-character string (LPCWSTR). The other audited values are of the DWORD type (i.e. unsigned long). Strictly they should be "k" values, not "I" values (i.e. unsigned int), but in practice "k" and "I" are equivalent on Windows. However, prior to 3.11 the audit call is incorrect.

At a glance, it looks like _winapi is still using MBCS paths by default. We should switch it all to Unicode. (There may already be an issue for this.)

I think it's just the CreateNamedPipe() and WaitNamedPipe() calls that need to be fixed.

  • _winapi_CreateNamedPipe_impl() mistakenly links with CreateNamedPipeA(), and, in that case, the audit call mistakenly assumes that name is a wide-character string.
  • _winapi_WaitNamedPipe_impl() mistakenly links with WaitNamedPipeA(). Apparently this call doesn't get audited.

Here's a couple more that aren't pressing issues:

  • new_overlapped() links with CreateEventA, as does _PySignal_Init() in "Modules/signalmodule.c". They should use CreateEventW() for consistency, but it's not an encoding or auditing problem.
  • _winapi_CreateJunction_impl() links with LookupPrivilegeValueA(). I don't think we should manually redefine SE_RESTORE_NAME as L"SeRestorePrivilege" just to support directly calling LookupPrivilegeValueW(). Instead, call LookupPrivilegeValueA(). More generally, there is no sufficient reason for this code to try to enable the restore privilege. It is not required to set a junction reparse point. I rewrote _winapi_CreateJunction_impl() more correctly in issue 97586.

@zooba
Copy link
Member Author

zooba commented May 29, 2024

In 3.11+, the audit call looks correct in _winapi_CreateFile_impl().

Hmm, I've definitely got garbage in logs from 3.11, but it might be an earlier version. Worth going back and fixing, or else I'll be going back and fixing in my own builds.

I think it's just the CreateNamedPipe() and WaitNamedPipe() calls that need to be fixed.

Thanks for confirming. I would've gone crazy trying to figure out the problem with CreateFile otherwise!

  • More generally, there is no sufficient reason for this code to try to enable the restore privilege. It is not required to set a junction reparse point.

Is it not? That privilege was the cause of one of the weirdest bugs I've ever had to diagnose (it was causing an unrelated stat test to succeed when run as admin after the privilege was set but not cleared, because it allowed the backup privilege to be used on a file ACLd for no access 🤦‍♂️)

  • I rewrote _winapi_CreateJunction_impl() more correctly ...

I was excited, but I think you rewrote it more correctly for Windows, not for us! As long as there's a specific API for us to use, I'd rather use it, even if technically all the structs used are supported. If a user is not supposed to be creating junctions, they'll get a relevant security error (and if an admin is auditing junction creation, they'll get those audit events).

Either way, no plans to touch anything outside of winapi in this issue.

@eryksun
Copy link
Contributor

eryksun commented May 29, 2024

I was excited, but I think you rewrote it more correctly for Windows, not for us! As long as there's a specific API for us to use, I'd rather use it, even if technically all the structs used are supported. If a user is not supposed to be creating junctions, they'll get a relevant security error (and if an admin is auditing junction creation, they'll get those audit events).

I don't know what you mean about a user not being allowed to be create junctions. There is no special privilege required to create a junction. You just need synchronize, read-attributes, and write-data access to set the reparse point. There is no API to create a junction per se. There's SetVolumeMountPointW(), but that sets a "\??\Volume{GUID}\" volume mountpoint and registers it with the system mountpoint manager. The latter requires Administrator access.

I rewrote the code for _winapi_CreateJunction_impl() to address multiple serious flaws in the existing code (only intended as is for internal testing) in order to bring it up to a level good enough that we could support adding a function in the os module to support creating junctions. Python's inability to create junctions is a notable gap compared to other scripting languages and shells.

@zooba
Copy link
Member Author

zooba commented May 29, 2024

Oh my bad, there isn't an API for creating a junction. I was probably thinking of the mount point API, yeah.

Python's inability to create junctions is a notable gap compared to other scripting languages and shells.

Perhaps, but I haven't heard any requests for it. The closest I've seen is people requesting posixpath.isjunction so they don't have to test for the function.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 29, 2024
…honGH-119717)

(cherry picked from commit 78d697b)

Co-authored-by: Steve Dower <steve.dower@python.org>
zooba added a commit to zooba/cpython that referenced this issue May 29, 2024
zooba added a commit to zooba/cpython that referenced this issue May 29, 2024
zooba added a commit that referenced this issue May 30, 2024
Also backports a minor improvement to test_audit.
zooba added a commit that referenced this issue May 31, 2024
(cherry picked from commit 78d697b)

Co-authored-by: Steve Dower <steve.dower@python.org>
ambv pushed a commit that referenced this issue Sep 4, 2024
…nd _winapi.CreateNamedPipe audit events (#119735)

gh-119690: Fixes buffer type confusion in _winapi.CreateFile and _winapi.CreateNamedPipe audit events
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 4, 2024
…File and _winapi.CreateNamedPipe audit events (pythonGH-119735)

(cherry picked from commit 2e861ac)

Co-authored-by: Steve Dower <steve.dower@python.org>
pythongh-119690: Fixes buffer type confusion in _winapi.CreateFile and _winapi.CreateNamedPipe audit events
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 4, 2024
…File and _winapi.CreateNamedPipe audit events (pythonGH-119735)

(cherry picked from commit 2e861ac)

Co-authored-by: Steve Dower <steve.dower@python.org>
pythongh-119690: Fixes buffer type confusion in _winapi.CreateFile and _winapi.CreateNamedPipe audit events
ambv pushed a commit that referenced this issue Sep 4, 2024
…ipe audit event (#119734)

gh-119690: Fixes buffer type confusion in _winapi.CreateNamedPipe audit event
ambv pushed a commit that referenced this issue Sep 4, 2024
…d _winapi.CreateNamedPipe audit events (GH-119735) (#123680)

(cherry picked from commit 2e861ac)

Co-authored-by: Steve Dower <steve.dower@python.org>
ambv pushed a commit that referenced this issue Sep 4, 2024
…d _winapi.CreateNamedPipe audit events (GH-119735) (#123679)

(cherry picked from commit 2e861ac)

Co-authored-by: Steve Dower <steve.dower@python.org>
@ambv ambv closed this as completed Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes OS-windows
Projects
None yet
Development

No branches or pull requests

3 participants