Nmap Development mailing list archives

Re: Locking a mutex by name?


From: Ron <ron () skullsecurity net>
Date: Fri, 14 Nov 2008 18:04:14 -0600

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.

Ron

-- 
Ron Bowes
http://www.skullsecurity.org/

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


Current thread: