You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 22 Next »

Introduction

Whenever threads come into play, there are bounded to be shared memory or resources that each thread wants to access. But because of the random nature of execution of each thread, there will be corruption of data when multiple threads try to read and write into the same memory space. One possible way to fix the problem is using locking mechanism like a mutex. POSIX provides a mutex called pthread_mutex_t just for this purpose. See POS00-C Avoid race conditions with multiple threads for more information.

Deadlock can happen when multiple threads each holds a lock the other needs and are waiting for each other to release that resource. One way to fix the problem is to avoid circular wait by locking the mutex in a predefined order.

Noncompliant Code Example

Based on runtime environment and the scheduler on the operating system, the following code will have different behaviors. Let's assume function thread1 and thread2 are called consecutively as in pthread_create is called for thread2 right after pthread_create is called for thread1. If lucky, the code will run without any problems. In other times, the code will deadlock in which thread1 try to lock m1 while thread2 try to lock on m2 and the program will not progress.

#include <pthread.h>

void *thread1(void *ptr);
void *thread2(void *ptr);

pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *ptr) {

  pthread_mutex_lock(&m1);
  /* do some stuff that require locking mutex1 */

  pthread_mutex_lock(&m2);
  /* do some stuff that require locking mutex2 */

  pthread_mutex_unlock(&m2);
  pthread_mutex_unlock(&m1);

  return NULL;
}

void *thread2(void *ptr) {

  pthread_mutex_lock(&m2);
  /* do some stuff that require locking mutex2 */

  pthread_mutex_lock(&m1);
  /* do some stuff that require locking mutex1 */

  pthread_mutex_unlock(&m1);
  pthread_mutex_unlock(&m2);

  return NULL;
}

Compliant Solution

The solution to the deadlock problem is to lock in predefined order. In the following example, each thread will lock m1 first then m2. This way circular wait problem is avoided and when one thread requires a lock will guarantee it will require the next lock.

#include <pthread.h>

void *thread1(void *ptr);
void *thread2(void *ptr);

pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;

void *thread1(void *ptr) {

  pthread_mutex_lock(&m1);
  pthread_mutex_lock(&m2);

  /* do some stuff that require locking mutex1 */
  /* do some stuff that require locking mutex2 */

  pthread_mutex_unlock(&m2);
  pthread_mutex_unlock(&m1);

  return NULL;
}

void *thread2(void *ptr) {

  pthread_mutex_lock(&m1);
  pthread_mutex_lock(&m2);
  /* do some stuff that require locking mutex1 */
  /* do some stuff that require locking mutex2 */

  pthread_mutex_unlock(&m1);
  pthread_mutex_unlock(&m2);

  return NULL;
}

Risk Assessment

Deadlock causes multiple threads to not be able to progress and thus halt the executing program. This is a potential denial-of-service attack when the attacker can force deadlock situations. It's probable that deadlock will occur in multi-thread programs that manage multiple resources. Some automation for detecting deadlock can be implemented in which the detector can try different inputs and wait for a timeout. The fixes can be done automatically using some graph algorithm like Dijkstra, but most like be manual.

Recommendation

Severity

Likelihood

Remediation Cost

Level

Priority

POS43-C

low

probable

medium

L3

P4

References

pthread_mutex pthread_mutex tutorial
MITRE CWE:764 Multiple Locks of Critical Resources
Bryant 03 Chapter 13, Concurrent Programming

Other Languages

CON12-J. Avoid deadlock by requesting and releasing locks in the same order

  • No labels