Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: cleanup

Mutexes are often used for critical resources to prevent multiple threads from accessing them at the same time. Sometimes, when locking mutexes, deadlock will happen when multiple threads hold each other's lock and the program come consequently comes to a halt. There are four requirements for deadlock:

...

Each deadlock requires all four conditions. Therefore, to prevent deadlock one just need to avoid , prevent any one of the four . The advice of this guideline is to require conditions from being satisfied. This guideline recommends locking the mutexes in a predefined order to prevent circular wait.

...

Code Block
bgColor#ffcccc
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>

typedef struct {
  int balance;
  pthread_mutex_t balance_mutex; 
} bank_account;

typedef struct {
  bank_account *from;
  bank_account *to;
  int amount;
} deposit_thr_args;

/* return negative on error */
int create_bank_account(bank_account **ba, int initial_amount) {

  int ret;
  bank_account *nba = malloc(sizeof(bank_account));
  if (nba == NULL) {
    return -1;
  }

  nba->balance = initial_amount;
  ret = pthread_mutex_init(&nba->balance_mutex, NULL);
  if (ret)
    exit(ret);

  *ba = nba;

  return 0;
}


void *deposit(void *ptr) {
	
  deposit_thr_args *args = (deposit_thr_args *)ptr;

  pthread_mutex_lock(&(args->from->balance_mutex));

  /* not enough balance to transfer */
  if (args->from->balance < args->amount) {
    pthread_mutex_unlock(&(args->from->balance_mutex));
    return NULL;
  }

  pthread_mutex_lock(&(args->to->balance_mutex));
  args->from->balance -= args->amount;
  args->to->balance += args->amount;

  pthread_mutex_unlock(&(args->from->balance_mutex));
  pthread_mutex_unlock(&(args->to->balance_mutex));

  free(ptr);
  return NULL;
}

int main() {

  pthread_t thr1, thr2;
  int err;

  bank_account *ba1, *ba2;

  err = create_bank_account(&ba1, 1000);
  if (err < 0) 
    exit(err);

  err = create_bank_account(&ba2, 1000);
  if (err < 0) 
    exit(err);

  deposit_thr_args *arg1 = malloc(sizeof(deposit_thr_args));
  if (arg1 == NULL)
    exit(-1);

  deposit_thr_args *arg2 = malloc(sizeof(deposit_thr_args));
  if (arg2 == NULL)
    exit(-1);

  arg1->from = ba1;
  arg1->to = ba2;
  arg1->amount = 100;

  arg2->from = ba2;
  arg2->to = ba1;
  arg2->amount = 100;

  /* perform the deposit */
  err = pthread_create(&thr1, NULL, deposit, (void *)arg1);
  if (err)
    exit(err);

  err = pthread_create(&thr2, NULL, deposit, (void *)arg2);
  if (err)
    exit(err);

  pthread_exit(NULL);

  return 0;
}

Compliant Solution

The solution to the deadlock problem is to lock in predefined order in the deposit() function. In the following example, each thread will lock based on bank_account's id defined in the struct initialization. This way circular wait problem is avoided and when one thread requires a lock will guarantee it will require the next lock.

...

Deadlock causes multiple threads to not be able become unable to progress and thus halt halts the executing program. This is a potential denial-of-service attack when because the attacker can force deadlock situations. It 's probable that deadlock will is likely for deadlock to occur in multi-thread threaded 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 manuallyshared resources.

Recommendation

Severity

Likelihood

Remediation Cost

Level

Priority

POS43-C

low

probable

medium

L3

P3

Other Languages

This rule appears in the Java Secure Coding Standard as CON12-J. Avoid deadlock by requesting and releasing locks in the same order.

References

Wiki Markup
\[[pthread_mutex | https://computing.llnl.gov/tutorials/pthreads/#Mutexes]\]  pthread_mutex tutorial
\[[MITRE CWE:764 | http://cwe.mitre.org/data/definitions/764.html]\] Multiple Locks of Critical Resources
\[[Bryant 03|AA. References#Bryant 03]\] Chapter 13, Concurrent Programming