Skip to content

Fix descriptor mutex errno reporting and synchronize probe workers (#2481) - #2482

Open
Mohit20057 wants to merge 1 commit into
OpenSCAP:mainfrom
Mohit20057:fix-tmpdir-s390x-mutex
Open

Mohit20057 wants to merge 1 commit into
OpenSCAP:mainfrom
Mohit20057:fix-tmpdir-s390x-mutex

Conversation

@Mohit20057

Copy link
Copy Markdown
Contributor

Summary

Fixes the intermittent test failure in tests/common/test_tmpdir.sh on s390x and systems under specific thread timing, and corrects the misleading DESC_WUNLOCK failed to unlock a mutex: Success error log.

Fixes: #2481


Root Causes

  1. Misleading Mutex Error Logging (seap-descriptor.h):
    POSIX thread functions (pthread_mutex_lock, pthread_mutex_unlock, and pthread_mutex_trylock) return an error integer directly upon failure and do not assign to errno. The helper functions in seap-descriptor.h did not set errno = ret; when those calls failed. Callers like SEAP_packet_send() called strerror(errno) where errno was 0, emitting the contradictory message:

    E: oscap: DESC_WUNLOCK failed to unlock a mutex: Success
    
  2. Probe Worker Teardown Race Condition (probe_main.c / worker.c):
    When an OVAL probe evaluation completed its final object response, probe_common_main_cleanup() freed the probe's SEAP context and descriptor tables while a background worker thread executing SEAP_packet_send() was still in the middle of sending or about to unlock the descriptor mutex. This use-after-free led to memory corruption and unexpected termination of oscap (exiting with code 0 instead of propagating code 2 in test_tmpdir.sh).


Changes

  • src/OVAL/probes/SEAP/seap-descriptor.h:
    • Set errno = ret; on failure in SEAP_desc_lock(), SEAP_desc_unlock(), and SEAP_desc_trylock().
  • src/OVAL/probes/probe/probe.h:
    • Added workers_mutex, workers_cond, and workers_running count to probe_t.
  • src/OVAL/probes/probe/worker.h:
    • Declared probe_worker_start(), probe_worker_end(), and probe_worker_wait().
  • src/OVAL/probes/probe/worker.c:
    • Implemented worker counting and condition wait functions; notified condition variable when workers_running == 0.
    • Added probe_worker_end(probe) to worker thread exit points in probe_worker_runfn().
  • src/OVAL/probes/probe/input_handler.c:
    • Incremented running worker count via probe_worker_start() prior to thread creation with rollback on pthread_create() failure.
  • src/OVAL/probes/probe/probe_main.c:
    • Initialized and destroyed worker sync primitives (workers_mutex and workers_cond).
    • Added probe_worker_wait(probe) to probe_common_main_cleanup() to ensure all workers exit before destroying SEAP contexts and caches.

Testing Done

  • Clean build with -DCMAKE_COMPILE_WARNING_AS_ERROR=True (0 compiler warnings, 0 errors).
  • Executed ctest --verbose -R common/test_tmpdir.sh with 100% pass across all 6 test scenarios:
    • TMPDIR: remediation: PASSED
    • TMPDIR with trailing slash: remediation: PASSED
    • TMPDIR: oval: PASSED
    • TMPDIR with trailing slash: oval: PASSED
    • TMPDIR: datastream: PASSED
    • TMPDIR with trailing slash: datastream: PASSED
  • Verified conformance with Linux kernel / OpenSCAP code style (LGPL 2.1).

In seap-descriptor.h, SEAP_desc_lock, SEAP_desc_unlock, and
SEAP_desc_trylock invoked pthread_mutex functions without setting
errno on failure. Since pthread_mutex_* functions return the error
code directly instead of setting errno, callers like SEAP_packet_send
logged misleading messages such as:
    E: oscap: DESC_WUNLOCK failed to unlock a mutex: Success
Fix this by explicitly setting errno = ret on failure in all SEAP
descriptor mutex helpers.

Furthermore, synchronize probe worker threads during probe shutdown.
When a probe completes its last evaluation, probe_common_main_cleanup
freed the SEAP context and descriptors while a worker thread in
SEAP_packet_send was still about to unlock the descriptor mutex,
causing intermittent use-after-free failures and non-deterministic
failures in test_tmpdir.sh (especially on s390x).
Wait for all active worker threads to terminate before releasing
probe resources.

Fixes: OpenSCAP#2481
@sonarqubecloud

sonarqubecloud Bot commented Nov 7, 2026

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test_tmpdir.sh fails randomly on s390x architecture

1 participant