Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

If a lock is being held and an operation that can block is performed, any other thread that needs to acquire that lock may also block. This condition can degrade system performance or cause a deadlock to occur.  Blocking calls include, but are not limited to, network, file, and console I/O.

Using a blocking operation while holding a lock may be unavoidable for a portable solution. For instance, file access could be protected via a lock to prevent multiple threads from mutating the contents of the file. Or, a thread may be required to block while holding one or more locks and waiting to acquire another lock. In these cases, attempt to hold the lock for the least time required. Additionally, if acquiring multiple locks, the order of locking must avoid deadlock, as specified in CON35-C. Avoid deadlock by locking in a predefined order.

Noncompliant Code Example

This noncompliant code example demonstrates an occurrence of a blocking call that waits to receive data on a socket example calls fopen() while a mutex is locked. The recv() call blocks until data arrives on the socket. While it calls to fopen() and fclose() are blocking and may block for an extended period of time if the file resides on a network drive. While the call is blocked, other threads that are waiting for the lock are also blocked.Although this example is specific to network I/O, the recv() call could be replaced with any blocking call, and the same behavior would occur.

Code Block
bgColor#ffcccc
langc
#include <stdio.h>
#include <threads.h>
 
mtx_t mutex;

voidint thread_foo(void *ptr) {
  uint32_t num;
  int result;
  intFILE sock*fp;

  /* sock is a connected TCP socket */
  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }
 
  iffp ((result = recvfopen(sock, (void *)&num, sizeof(uint32_t), 0)) < 0"SomeNetworkFile", "r");
  if (fp != NULL) {
    /* Handle ErrorWork with the file */
  }

  /* ... */
fclose(fp);
  }
 
  if ((result = mtx_unlock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  return 0;
}

int main(void) {
  thrd_t thread;
  int result;

  if ((result = mtx_init(&mutex, mtx_plain)) != thrd_success) {
    /* Handle Errorerror */
  }

  if (thrd_create(&thread,(void *)& thread_foo, NULL) != thrd_success) {
    /* Handle Errorerror */
  }

  /* ... */

  if (thrd_join(thread, NULL);

  if ((result = mtx_destroy(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  mtx_destroy(&mutex);

  return 0;
}

Compliant Solution (Block while Not Locked)

This compliant solution performs the recv() call file operations when the lock has not been acquired. The blocking behavior consequently affects only the thread that called the blocking function.

Code Block
bgColor#ccccff
langc
void thread_foo(void *ptr) {
  uint32#include <stdio.h>
#include <threads.h>
 
mtx_t nummutex;
  int result;
  int sock;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), 0)) < 0)  
int thread_foo(void *ptr) {
  int result;
 /* HandleFILE Error */
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Error */
  }

  /* ... */
fp = fopen("SomeNetworkFile", "r");
 
  if ((result = pthread_mutex_unlock(&mutex))fp != 0NULL) {
    /* Handle Error */
  }
}

Compliant Solution (Use a Nonblocking Call)

This compliant solution performs the recv() call with the parameter o_nonblock, which causes the call to fail if no messages are available on the socket.

Code Block
bgColor#ccccff
langc
void thread_foo(void *ptr) {
  uint32_t num;
  int result;

  /* sock is a connected TCP socket */

  if ((result = recv(sock, (void *)&num, sizeof(uint32_t), O_NONBLOCK)) < 0) {
    /* Handle Error */
  Work with the file */
    fclose(fp);
  }

  if ((result = mtx_lock(&mutex)) != thrd_success) {
    /* Handle Errorerror */
  }

  /* ... */

  if ((result = mtxpthread_mutex_unlock(&mutex)) != thrd_success0) {
    /* Handle Errorerror */
  }

  return 0;
}

...

CON36-EX1: A thread may block while holding one or more locks and waiting to acquire another lock. When acquiring multiple locks, the order of locking must avoid deadlock, as specified in CON35-C. Avoid deadlock by locking in predefined order.

Risk Assessment

Blocking or lengthy operations performed within synchronized regions could result in a deadlocked or an unresponsive system.

Rule

Recommendation

Severity

Likelihood

Detectable

Remediation Cost

Repairable

Priority

Level

CON36

CON05-C

low

Low

Probable

probable

No

high

No

P2

L3

Related Guidelines

Automated Detection

ToolVersionCheckerDescription
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V

CONCURRENCY.STARVE.BLOCKING

Blocking in critical section
Klocwork
Include Page
Klocwork_V
Klocwork_V
CONC.SLEEP
Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V
CERT_C-CON05-a
Do not use blocking functions while holding a lock
Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. CON05-CChecks for blocking operation while holding lock (Rec. partially covered)
Security Reviewer - Static Reviewer

6.02

C12Fully Implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Key here (explains table format and definitions)

...

Taxonomy

Taxonomy item

Relationship

...

...

Sources

...

Prior to 2018-01-12: CERT: Unspecified Relationship
MITRE CWECWE-557Prior to 2018-01-12:
MITRE CWECWE-662Prior to 2018-01-12:


...

Image Modified Image Modified Image Modified