Pthread Mutual Exclusion mutual exclusion (Mutexmutex) locks are used to avoid simultaneous usage of common resources. There are several Several types of mutex locks are defined by pthreads which include : NORMAL, ERRORCHECK, RECURSIVE, and DEFAULT.unmigrated-wiki-markup
POSIX defines describes PTHREAD_MUTEX_NORMAL locks to have the following undefined behavior \[[Open Group 4|https://www.securecoding.cert.org/confluence/display/seccode/AA.+C+References#AA.CReferences-OpenGroup04] \] locks as having the following undefined behavior [Open Group 2004]:
This type of mutex does not provide deadlock detection. A thread attempting to relock this mutex without first unlocking it shall deadlock. An error is not returned to the caller. Attempting to unlock a mutex locked by a different thread results in undefined behavior. Attempting to unlock an unlocked mutex results in undefined behavior.
...
The DEFAULT mutex type of pthreads is also generally mapped to mutex pthread is also generally mapped to PTHREAD_MUTEX_NORMAL which is seen across various implementations \[[SOL 10|http://docs.sun.com/app/docs/doc/816-5137/sync-28983?a=view] \]. Usage of NORMAL locks should be avoided and ERRORCHECK or RECURSIVE locks should be explicitly defined when using mutex locks but is known to vary from platform to platform [SOL 2010]. Consequently, NORMAL locks should not be used, and ERRORCHECK or RECURSIVE locks should be defined explicitly when mutex locks are used.
Noncompliant Code Example
The non compliant This noncompliant code example shows a simple mutex being created using PTHREAD_MUTEX_NORMAL. It should be noted that no return code is expected by the caller when NORMAL mutexes Note that the caller does not expect a return code when NORMAL mutex locks are used.
| Code Block | ||||
|---|---|---|---|---|
| ||||
pthread_mutexattr_t attr; pthread_mutex_t mutex; size_t const shared_var = 0; int main(void) { int result; intif rc((result =0; pthread_mutexattr_init(&attr)) != 0) { /* Handle Error */ } if rc((result = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_NORMAL);)) != 0) { /*Check Handle ReturnError Code*/ } if rc((result = pthread_mutex_init(&mutex, &attr);)) != 0) { /*Check Handle ReturnError Code*/ } if ((result = pthread_mutex_lock(&mutex); ) != 0) { /* Handle Error */ } /* Critical Region*/ if ((result = pthread_mutex_unlock(&mutex); )) != 0) { /* Handle Error */ } return 0; } |
Compliant Solution
The This compliant solution shows a pthread an ERRORCHECK mutex lock being created with type PTHREAD_MUTEX_ERRORCHECK where so that return codes will be available during locking and unlocking.:
| Code Block | ||||
|---|---|---|---|---|
| ||||
pthread_mutexattr_t attr; pthread_mutex_t mutex; size_t const shared_var = 0; int main(void) { int result; int rc =0; rcif ((result = pthread_mutexattr_init(&attr)) != 0) { /* Handle Error */ } if ((result = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_ERRORCHECK); )) != 0) { /*Check Handle ReturnError Code*/ } if rc((result = pthread_mutex_init(&mutex, &attr);)) != 0) { /*Check Handle ReturnError Code*/ } if rc((result = pthread_mutex_lock(&mutex); )) != 0) { /* Handle error ifError any*/ } /* Critical Region*/ if ((result rc = pthread_mutex_unlock(&mutex);)) != 0) { /* Handle error ifError any*/ } return 0; } |
Risk Assessment
Using NORMAL mutex locks can lead to deadlocks or abnormal program termination.
Recommendation | Severity | Likelihood |
|---|
Detectable | Repairable | Priority | Level |
|---|
POS04-C |
high
likely
medium
P12
L1
...
Low | Unlikely | Yes | Yes | P3 | L3 |
Automated Detection
Tool | Version | Checker | Description | ||||||
|---|---|---|---|---|---|---|---|---|---|
| PC-lint Plus |
| 586 | Fully supported |
Bibliography
...