Skip to content

Conversation

@tpowell-progress
Copy link
Contributor

@tpowell-progress tpowell-progress commented Apr 10, 2023

Description

Windows runs of Chef get intermittent ArgumentErrors for "Invalid Memory Object". This is due to an attempt to compare a legitimate Windows HANDLE retrieved from GetCurrentProcess with an FFI::Pointer. The "Invalid Memory Object" error itself comes from trying to compare a memory object (FFI::Pointer with the return value from GetCurrentProcess(), a HANDLE from Windows)

Windows data types

These errors do not result in breakage because of their happening in a finalizer, but

Error example

C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-18.2.9-x64-mingw-ucrt/lib/chef/win32/handle.rb:45:in `==': Invalid Memory object (ArgumentError)
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-18.2.9-x64-mingw-ucrt/lib/chef/win32/handle.rb:45:in `close_handle'
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-18.2.9-x64-mingw-ucrt/lib/chef/win32/handle.rb:37:in `block in close_handle_finalizer'
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/ffi-win32-extensions-1.0.4/lib/ffi/win32/extensions.rb:60:in `read_utf16string'
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-powershell-18.0.1/lib/chef-powershell/powershell.rb:120:in `exec'
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-powershell-18.0.1/lib/chef-powershell/powershell.rb:45:in `initialize'
	from C:/opscode/chef/embedded/lib/ruby/gems/3.1.0/gems/chef-powershell-18.0.1/lib/chef-powershell/powershell_exec.rb:116:in `new'

Analysis

Chef::ReservedNames::Win32::Security.open_process_token passes a unsigned long and Chef::ReservedNames::Win32::Process.get_current_process the actual HANDLE from GetCurrentProcess()... these finalize properly, because they are compared as numerical values in handle == GetCurrentProcess().

Chef::ReservedNames::Win32::Security.logon_user passes the result of FFI::Buffer#read_pointer to Handle.new instead, which results in an FFI::Pointer being stored as the handle.

Prior to Ruby 3.1/Chef 18/UCRT, this mismatch was at least not as noisy as it is in the current versions of everything.

Solution

Use FFI::Buffer#read_ulong to prevent the 8-byte value from being interpreted as a Ruby memory object so that Ruby will assume it's comparing a primitive 8-byte value to the return value of GetCurrentProcess(). While HANDLE is a void * in Windows, Ruby has no type information or heap access to know what to do with the memory.

Related Issue

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (non-breaking change that does not add functionality or fix an issue)

Checklist:

  • I have read the CONTRIBUTING document.
  • I have run the pre-merge tests locally and they pass.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • All commits have been signed-off for the Developer Certificate of Origin.

@tpowell-progress tpowell-progress requested review from a team as code owners April 10, 2023 12:46
@tpowell-progress tpowell-progress force-pushed the tp/invalid_memory_object branch 2 times, most recently from 28322e3 to 5bcf82f Compare April 10, 2023 12:52
Signed-off-by: Thomas Powell <powell@progress.com>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Signed-off-by: Thomas Powell <powell@progress.com>
Signed-off-by: Thomas Powell <powell@progress.com>
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@sonarqube-for-infrastructure-prod

Copy link
Collaborator

@jaymzh jaymzh left a comment

Choose a reason for hiding this comment

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

💯

@tpowell-progress tpowell-progress merged commit 0bfee95 into main Apr 14, 2023
@tpowell-progress tpowell-progress deleted the tp/invalid_memory_object branch April 14, 2023 19:42
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.

4 participants