Difference between revisions of "Race condition within a thread"

From OWASP
Jump to: navigation, search
 
(Examples)
Line 45: Line 45:
 
In C/C++:
 
In C/C++:
  
 +
<pre>
 
int foo = 0;
 
int foo = 0;
 
     int storenum(int num)
 
     int storenum(int num)
Line 54: Line 55:
 
             return foo;   
 
             return foo;   
 
     }
 
     }
 +
</pre>
 +
 
In Java:
 
In Java:
  
public classRace {
+
<pre>public classRace {
 
   static int foo = 0;
 
   static int foo = 0;
  
Line 70: Line 73:
 
   }
 
   }
 
}
 
}
 +
</pre>
 +
 
==Related problems ==
 
==Related problems ==
  

Revision as of 10:09, 16 April 2006



Overview

If two threads of execution use a resource simultaneously, there exists the possibility that resources may be used while invalid, in turn making the state of execution undefined.

Consequences

  • Integrity: The main problem is that - if a lock is overcome - data could be altered in a bad state.

Exposure period

  • Design: Use a language which provides facilities to easily use threads safely.

Platform

  • Languages: Any language with threads
  • Operating platforms: All

Required resources

Any

Severity

High

Likelihood of exploit

Medium

Avoidance and mitigation

Discussion

  • Design: Use locking functionality. This is the recommended solution. Implement some form of locking mechanism around code which alters or reads persistent data in a multi-threaded environment.
  • Design: Create resource-locking sanity checks. If no inherent locking mechanisms exist, use flags and signals to enforce your own blocking scheme when resources are being used by other threads of execution.

Examples

In C/C++:

int foo = 0;
    int storenum(int num)
    {
        static int counter = 0;
        counter++;
        if (num > foo) 
            foo = num;
            return foo;   
    }

In Java:

public classRace {
  static int foo = 0;

  public static void main() {
    new Threader().start();
    foo = 1;
  }

  public static class Threader extends Thread {
    public void run() { 
      System.out.println(foo);
    }
  }
}

Related problems

Not available.

Categories