8000 GitHub · Where software is built
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Detect some TOCTTOU bugs #946
Open
Open
@ainar-g

Description

@ainar-g

Time-of-check to time-of-use (aka “TOCTTOU”, “TOCTOU”, or “TOC/TOU”) is a class of race condition bugs that can be observed in almost any language. Some basic examples in Go would be:

// BAD:  Checks if the file is there before opening it instead of catching
// os.ErrNotExist with errors.Is.  File may be deleted between the call to
// os.Stat and the call to os.Open.
_, err = os.Stat(fileName)
if err == nil {
        f, err = os.Open(fileName)
        if err == nil 
65EF
{
                fatal()
        }
}

And:

// BAD:  lockedX could change between the first mu.Unlock() and the second
// mu.Lock().  Either lock the whole method or use the atomic.CompareAndSwap*
// family of functions.
var x int
mu.Lock()
x = lockedX
mu.Unlock()

if x == 0 {
        mu.Lock()
        lockedX = newX
        mu.Unlock()
}

Detecting such bugs in general is probably an impossible task (although there seems to have been some work put into it), but I think that these two patterns could be detected reliably at least within one function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positives but is helpful during code reviewneeds-decisionWe have to decide if this check is feasible and desirablenew-check

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0