-
Notifications
You must be signed in to change notification settings - Fork 280
add vmmem process lookup functionality + windows API abstraction #2613
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
Open
rawahars
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
rawahars:LookupVMMEM
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+758
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| //go:build windows | ||
|
|
||
| package vmutils | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "iter" | ||
| "strings" | ||
| "unsafe" | ||
|
|
||
| "github.com/Microsoft/hcsshim/internal/log" | ||
| iwin "github.com/Microsoft/hcsshim/internal/windows" | ||
|
|
||
| "github.com/Microsoft/go-winio/pkg/guid" | ||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| const ( | ||
| // vmmemProcessName is the name of the Hyper-V memory management process. | ||
| vmmemProcessName = "vmmem" | ||
| // vmmemProcessNameExt is the name of the process with .exe extension. | ||
| vmmemProcessNameExt = "vmmem.exe" | ||
| // ntVirtualMachineDomain is the domain name for Hyper-V virtual machine security principals. | ||
| ntVirtualMachineDomain = "NT VIRTUAL MACHINE" | ||
| ) | ||
|
|
||
| // allProcessEntries returns an iterator over all process entries in a Toolhelp32 snapshot. | ||
| // If the snapshot cannot be created or a process entry cannot be read, the error is logged and | ||
| // iteration stops. | ||
| func allProcessEntries(ctx context.Context, win iwin.API) iter.Seq[*windows.ProcessEntry32] { | ||
| return func(yield func(*windows.ProcessEntry32) bool) { | ||
| snapshot, err := win.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) | ||
| if err != nil { | ||
| log.G(ctx).WithError(err).Error("failed to create process snapshot") | ||
| return | ||
| } | ||
| defer func(win iwin.API, h windows.Handle) { | ||
| _ = win.CloseHandle(h) | ||
| }(win, snapshot) | ||
|
|
||
| var pe32 windows.ProcessEntry32 | ||
| pe32.Size = uint32(unsafe.Sizeof(pe32)) | ||
|
|
||
| for err = win.Process32First(snapshot, &pe32); ; err = win.Process32Next(snapshot, &pe32) { | ||
| if err != nil { | ||
| log.G(ctx).WithError(err).Debug("finished iterating process entries") | ||
| return | ||
| } | ||
| if !yield(&pe32) { | ||
| return | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // LookupVMMEM locates the vmmem process for a VM given the VM ID. | ||
| // It enumerates processes using Toolhelp32 to filter by name, then validates | ||
| // the token using LookupAccount to match the "NT VIRTUAL MACHINE\<VM ID>" identity. | ||
| func LookupVMMEM(ctx context.Context, vmID guid.GUID, win iwin.API) (windows.Handle, error) { | ||
| vmIDStr := strings.ToUpper(vmID.String()) | ||
| log.G(ctx).WithField("vmID", vmIDStr).Debug("looking up vmmem via LookupAccount") | ||
|
|
||
| for pe32 := range allProcessEntries(ctx, win) { | ||
| exeName := windows.UTF16ToString(pe32.ExeFile[:]) | ||
|
|
||
| // 1. Only target processes named vmmem or vmmem.exe. | ||
| if !strings.EqualFold(exeName, vmmemProcessName) && !strings.EqualFold(exeName, vmmemProcessNameExt) { | ||
| continue | ||
| } | ||
|
|
||
| // 2. Open the process to inspect its security token. | ||
| pHandle, err := win.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, pe32.ProcessID) | ||
| if err != nil { | ||
| continue | ||
| } | ||
|
|
||
| var t windows.Token | ||
| if err := win.OpenProcessToken(pHandle, windows.TOKEN_QUERY, &t); err != nil { | ||
| _ = win.CloseHandle(pHandle) | ||
| continue | ||
| } | ||
|
|
||
| tUser, err := win.GetTokenUser(t) | ||
| if err != nil { | ||
| _ = win.CloseToken(t) | ||
| _ = win.CloseHandle(pHandle) | ||
| continue | ||
| } | ||
|
|
||
| // 3. Use the OS API to resolve the SID to account and domain strings. | ||
| account, domain, _, err := win.LookupAccount(tUser.User.Sid, "") | ||
| _ = win.CloseToken(t) | ||
| if err != nil { | ||
| _ = win.CloseHandle(pHandle) | ||
| continue | ||
| } | ||
|
|
||
| // 4. Compare against the expected Hyper-V UVM identity. | ||
| if strings.EqualFold(domain, ntVirtualMachineDomain) && strings.EqualFold(account, vmIDStr) { | ||
| log.G(ctx).WithField("pid", pe32.ProcessID).Debug("found vmmem match") | ||
| return pHandle, nil | ||
| } | ||
|
|
||
| _ = win.CloseHandle(pHandle) | ||
| } | ||
|
|
||
| return 0, errors.New("failed to find matching vmmem process") | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.