Skip to content

Race condition in OS_BSP_Unlock_Impl can restore another thread's cancel state #1618

Backlog

Description

@steven1518

Describe the bug

OS_BSP_Unlock_Impl() can restore the cancel state of a different thread.

OS_BSP_Lock_Impl() disables cancellation while a thread holds the BSP lock. It saves the previous cancel state in one global variable, AccessCancelState. OS_BSP_Unlock_Impl() restores this state. But it reads AccessCancelState after it releases the mutex:

status = pthread_mutex_unlock(&OS_BSP_GenericLinuxGlobal.AccessMutex);
...
pthread_setcancelstate(OS_BSP_GenericLinuxGlobal.AccessCancelState, NULL); /* read after unlock */

After the mutex is free, a different thread can call OS_BSP_Lock_Impl() and write its own state to AccessCancelState. The first thread then restores the wrong state.

To Reproduce

sequenceDiagram
    participant A as Thread A
    participant S as Shared saved state
    participant B as Thread B

    Note over A: Initially: cancellation ENABLED
    Note over B: Initially: cancellation DISABLED

    A->>A: Lock mutex
    A->>S: Save ENABLED
    A->>A: Disable cancellation and do work
    A->>A: Unlock mutex

    Note over A,B: A possible thread switch happens here

    B->>B: Lock mutex
    B->>S: Save DISABLED — overwrites A's value

    S-->>A: Read DISABLED
    Note over A: BUG: restores DISABLED<br/>Expected: ENABLED
Loading
  1. Thread A has cancellation enabled. It calls OS_BSP_Lock_Impl(). It locks the mutex, saves ENABLED in AccessCancelState and disables cancellation.
  2. Thread A calls OS_BSP_Unlock_Impl(). It releases the mutex.
  3. Thread B has cancellation disabled. It calls OS_BSP_Lock_Impl() before thread A reads AccessCancelState. It saves DISABLED in AccessCancelState. This replaces the value of thread A.
  4. Thread A reads AccessCancelState and restores DISABLED. The correct state for thread A is ENABLED.

The program below shows the problem. It uses a copy of the two functions. Two threads with different cancel states lock and unlock in a loop. Each thread counts the number of times that it gets the wrong state.

Reproducer (build with gcc -O2 repro.c -lpthread)
/* Same logic as OS_BSP_Lock_Impl / OS_BSP_Unlock_Impl in src/bsp/generic-linux/src/bsp_start.c */
#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t AccessMutex = PTHREAD_MUTEX_INITIALIZER;
static int             AccessCancelState;
static volatile int    done;

static void Lock(void)
{
    pthread_mutex_lock(&AccessMutex);
    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &AccessCancelState);
}

static void Unlock(void)
{
    pthread_mutex_unlock(&AccessMutex);
    pthread_setcancelstate(AccessCancelState, NULL); /* read after unlock */
}

/* Each thread keeps its own cancel state and checks what Unlock() restored */
static void *worker(void *arg)
{
    int mine = *(int *)arg, restored, wrong = 0, n = 0;

    pthread_setcancelstate(mine, NULL);
    while (!done && n < 1000000)
    {
        Lock();
        Unlock();
        pthread_setcancelstate(mine, &restored); /* read back, then reset */
        wrong += (restored != mine);
        ++n;
    }
    done = 1;
    printf("thread with %s: wrong state restored %d / %d times\n",
           mine == PTHREAD_CANCEL_ENABLE ? "ENABLE " : "DISABLE", wrong, n);
    return NULL;
}

int main(void)
{
    int       s[2] = {PTHREAD_CANCEL_ENABLE, PTHREAD_CANCEL_DISABLE};
    pthread_t t[2];

    for (int i = 0; i < 2; ++i) pthread_create(&t[i], NULL, worker, &s[i]);
    for (int i = 0; i < 2; ++i) pthread_join(t[i], NULL);
    return 0;
}

Sample output (Ubuntu 22.04, glibc 2.35):

thread with ENABLE : wrong state restored 5583 / 952696 times
thread with DISABLE: wrong state restored 5038 / 1000000 times

Expected behavior
Each thread restores its own previous cancel state. To do this, copy AccessCancelState to a local variable before the call to pthread_mutex_unlock().

Code snips

void OS_BSP_Unlock_Impl(void)
{
int status;
status = pthread_mutex_unlock(&OS_BSP_GenericLinuxGlobal.AccessMutex);
if (status < 0)
{
BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status));
}
else
{
/* Restore previous cancellability state */
pthread_setcancelstate(OS_BSP_GenericLinuxGlobal.AccessCancelState, NULL);
}
}

The generic-qnx and generic-vxworks-rtp BSPs have the same code:

  • void OS_BSP_Unlock_Impl(void)
    {
    int status;
    status = pthread_mutex_unlock(&OS_BSP_GenericQnxGlobal.AccessMutex);
    if (status < 0)
    {
    BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status));
    }
    else
    {
    /* Restore previous cancellability state */
    pthread_setcancelstate(OS_BSP_GenericQnxGlobal.AccessCancelState, NULL);
    }
    }
  • void OS_BSP_Unlock_Impl(void)
    {
    int status;
    status = pthread_mutex_unlock(&OS_BSP_GenericVxWorksRtpGlobal.AccessMutex);
    if (status < 0)
    {
    BSP_DEBUG("pthread_mutex_unlock: %s\n", strerror(status));
    }
    else
    {
    /* Restore previous cancellability state */
    pthread_setcancelstate(OS_BSP_GenericVxWorksRtpGlobal.AccessCancelState, NULL);
    }
    }

System observed on:
Ubuntu 22.04, OSAL dev (2a96a4f)

Reporter Info
Jiahao Shi, Purdue University

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions