Nmap Development mailing list archives

Re: Locking a mutex by name?


From: "Patrick Donnelly" <batrick.donnelly () gmail com>
Date: Fri, 14 Nov 2008 17:27:46 -0700

On Fri, Nov 14, 2008 at 5:04 PM, Ron <ron () skullsecurity net> wrote:
Patrick Donnelly wrote:
It sounds like you are unlocking the mutex from a thread that does not
have the lock.
It sounds that way too, but I'm positive it's the same thread. When I
changed to storing it in the registry, it started working fine.

Could you post either your code or a minimal example demonstrating this problem?
I'm trying to recreate it minimally, but everything seems to work on a
small scale. I really can't say why, so something funny must be happening.

Here's the output of the program when it isn't working (grepping for
'mutex':
--
SCRIPT ENGINE DEBUG: SMB: Attempting to lock mutex [start(1)]
SCRIPT ENGINE DEBUG: SMB: Creating new mutex
SCRIPT ENGINE DEBUG: SMB: Using mutex named 'SMB-BASEWIN2K'
SCRIPT ENGINE DEBUG: SMB: Mutex lock obtained [start(1)]
SCRIPT ENGINE DEBUG: SMB: Attempting to release mutex [stop()]
SCRIPT ENGINE DEBUG: SMB: Creating new mutex
SCRIPT ENGINE DEBUG: SMB: Using mutex named 'SMB-BASEWIN2K'
SCRIPT ENGINE: ./nselib/smb.lua:223: Do not have a lock on this mutex
--

Which is generated by the following code (keeping in mind that the
registry bits are commented out):
--
local function get_mutex(smbstate)
   local mutex_name = "SMB-"
   local mutex

   -- Decide whether to use the name or the ip address as the unique
identifier
   if(smbstate['name'] ~= nil) then
       mutex_name = mutex_name .. smbstate['name']
   else
       mutex_name = mutext_name .. smbstate['ip']
   end

   -- Create the mutex table in the registry, if it doesn't exist
--  if(nmap.registry.mutex == nil) then
--      nmap.registry.mutex = {}
--  end

   -- Attempt to read the mutex from the registry
--  mutex = nmap.registry.mutex[mutex_name]

   -- If it isn't found, create one
   if(mutex == nil) then
       stdnse.print_debug(3, "SMB: Creating new mutex")
       mutex = nmap.mutex(mutex_name)
--      nmap.registry.mutex[mutex_name] = mutex
   end

   stdnse.print_debug(3, "SMB: Using mutex named '%s'", mutex_name)

   return mutex
end

local function lock_mutex(smbstate, func)
   local mutex

   stdnse.print_debug(3, "SMB: Attempting to lock mutex [%s]", func)
   mutex = get_mutex(smbstate)
   mutex "lock"
   stdnse.print_debug(3, "SMB: Mutex lock obtained [%s]", func)
end

local function unlock_mutex(smbstate, func)
   local mutex

   stdnse.print_debug(3, "SMB: Attempting to release mutex [%s]", func)
   mutex = get_mutex(smbstate)
   mutex "done"
   stdnse.print_debug(3, "SMB: Mutex released [%s]", func)
end
--

You can trace the path pretty easily by looking at the debug output.

The odd thing is that, if I uncomment the registry lines, it works fine:
--
SCRIPT ENGINE DEBUG: SMB: Attempting to lock mutex [start(1)]
SCRIPT ENGINE DEBUG: SMB: Creating new mutex
SCRIPT ENGINE DEBUG: SMB: Using mutex named 'SMB-BASEWIN2K'
SCRIPT ENGINE DEBUG: SMB: Mutex lock obtained [start(1)]
SCRIPT ENGINE DEBUG: SMB: Attempting to release mutex [stop()]
SCRIPT ENGINE DEBUG: SMB: Using mutex named 'SMB-BASEWIN2K'
SCRIPT ENGINE DEBUG: SMB: Mutex released [stop()]
--

Because I can't recreate it on a small scale, I'd be convinced I was
doing something wrong if using the registry didn't fix it...

I'll take another shot at creating a small example later, but I'm not
confident I'll be able to.

The problem you are experiencing is the mutex is being garbage
collected during the time between you lock it and release it. The
mutexes are stored internally with weak references to allow them to be
collected when no longer used. The registry is holding onto that
reference for you. It is normally expected you use a local reference
to the mutex. I would recommend you not store the reference in the
registry and instead keep it local or in the function's (that is, your
script) environment.

Cheers,

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant

_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org


Current thread: