Commit c86cf0b1 authored by Adam Edwards's avatar Adam Edwards
Browse files

Merge pull request #41 from opscode/adamed/mixlib-25

MIXLIB-25: Only close handles that are set to a valid handle value
parents 0ba04824 d2e7d72e
......@@ -122,8 +122,8 @@ module Mixlib
end
ensure
CloseHandle(process.thread_handle)
CloseHandle(process.process_handle)
CloseHandle(process.thread_handle) if process.thread_handle
CloseHandle(process.process_handle) if process.process_handle
end
ensure
......
......@@ -288,19 +288,23 @@ module Process
token = token.read_ulong
bool = CreateProcessAsUserW(
token, # User token handle
app, # App name
cmd, # Command line
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles
hash['creation_flags'], # Creation Flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
procinfo # Process Info
)
begin
bool = CreateProcessAsUserW(
token, # User token handle
app, # App name
cmd, # Command line
process_security, # Process attributes
thread_security, # Thread attributes
inherit, # Inherit handles
hash['creation_flags'], # Creation Flags
env, # Environment
cwd, # Working directory
startinfo, # Startup Info
procinfo # Process Info
)
ensure
CloseHandle(token)
end
unless bool
raise SystemCallError.new("CreateProcessAsUserW (You must hold the 'Replace a process level token' permission)", FFI.errno)
......@@ -346,9 +350,14 @@ module Process
# Automatically close the process and thread handles in the
# PROCESS_INFORMATION struct unless explicitly told not to.
if hash['close_handles']
CloseHandle(procinfo[:hProcess])
CloseHandle(procinfo[:hThread])
CloseHandle(token)
CloseHandle(procinfo[:hProcess]) if procinfo[:hProcess]
CloseHandle(procinfo[:hThread]) if procinfo[:hThread]
# Set fields to nil so callers don't attempt to close the handle
# which can result in the wrong handle being closed or an
# exception in some circumstances
procinfo[:hProcess] = nil
procinfo[:hThread] = nil
end
ProcessInfo.new(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment