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

Fix CompareObjectHandles check to run if syscall is available #5749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jameshiew
Copy link

Pull Request check list

  • Commit conforms to CONTRIBUTING.md?
  • Proper tests/regressions included?
  • Documentation updated?

Affected functionality
CompareObjectHandles peertracker check on Windows systems which support it

Description of change
Change if isCompareObjectHandlesFound() to if procCompareObjectHandlesErr != nil in compareObjectHandles so that the error is forwarded if it is present, otherwise the check can run (discussed with SPIRE maintainers)

Which issue this PR fixes
Fixes #5748

Check would previously be skipped if CompareObjectHandles syscall was found

Signed-off-by: James Hiew <james@hiew.net>
@@ -65,7 +65,7 @@ func isCompareObjectHandlesFound() bool {
// compareObjectHandles compares two object handles to determine if they
// refer to the same underlying kernel object
func compareObjectHandles(firstHandle, secondHandle windows.Handle) error {
if isCompareObjectHandlesFound() {
if procCompareObjectHandlesErr != nil {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't see an easy way to unit test compareObjectHandles without refactoring, I just included the one line change in this PR. To make it minimally testable could look something like in the direction of below - changing compareObjectHandles into a method on some type so that anything used by it e.g. syscall.SyscallN could be overridden on the struct used in testing.

// syscaller wraps anything used by `compareObjectHandles` calls
type syscaller struct {
	procCompareObjectHandlesAddr *uintptr // from procCompareObjectHandles.Addr()
	procCompareObjectHandlesErr  error
	syscallN func(trap uintptr, args ...uintptr) (uintptr, uintptr, syscall.Errno)
}

// windowsSyscaller is the only syscaller that should be used at runtime
var windowsSyscaller syscaller

// init sets up the real syscaller
func init() {
	kernelbase := windows.NewLazyDLL("kernelbase.dll")
	procCompareObjectHandles := kernelbase.NewProc("CompareObjectHandles")
	if err := procCompareObjectHandles.Find(); err != nil {
		windowsSyscaller = syscaller{
			procCompareObjectHandlesErr: err,
			syscallN:                    syscall.SyscallN,
		}
	} else {
		syscallAddr := procCompareObjectHandles.Addr()
		windowsSyscaller = syscaller{
			procCompareObjectHandlesAddr: &syscallAddr,
			syscallN:                     syscall.SyscallN,
		}
	}
}

func (s syscaller) isCompareObjectHandlesFound() bool {
	return s.procCompareObjectHandlesAddr == nil
}

// compareObjectHandles compares two object handles to determine if they
// refer to the same underlying kernel object
func (s syscaller) compareObjectHandles(firstHandle, secondHandle windows.Handle) error {
	if s.procCompareObjectHandlesErr != nil {
		return s.procCompareObjectHandlesErr
	}
	r1, _, e1 := s.syscallN(*s.procCompareObjectHandlesAddr, uintptr(firstHandle), uintptr(secondHandle))
	if r1 == 0 {
		return e1
	}
	return nil
}

@jameshiew jameshiew marked this pull request as ready for review December 27, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

peertracker: CompareObjectHandles check for Windows does not run
1 participant