## Impact

seahorse crashes at exit in libpcsclite when another thread is still
inside an `SCard*()` call.  The fault is in pcsc-lite: the library
destructor frees `contextMapList` and destroys `contextMapList_lock`
without synchronising with in-flight API calls, and `list_destroy()`
leaves the sentinels pointing at freed memory.  This bucket is 125
crashes/week on Ubuntu 26.04.  Any PKCS#11 application that exits during
enumeration is affected, not only seahorse.

The minimal "neutralise the list instead of freeing it" fix is not
sufficient: it leaks the list on every dlopen/dlclose cycle, and it turns
the `SCardEstablishContext()`-at-exit path into a double free in
`SCardAddContext()`.  The attached patch fixes `list_destroy()`,
`list_insert_at()` and the lock initialisation, and keeps the dlclose
free.

## Test Plan

Prepared package: 2.4.1-1ubuntu0.1 for resolute (debdiff attached; it
becomes available in resolute-proposed once sponsored and accepted).  A
tenacious (27.04) debdiff follows in a separate comment.

1. Developer tests, no reader needed (harnesses are in the upstream
   issue and its gist):

   - `SCardGetStatusChange()` racing exit: unpatched 20/20 SIGSEGV,
     patched 0/20.
   - `SCardEstablishContext()` racing exit, pcscd running: the minimal
     neutralise patch 50/50 SIGABRT with `free(): double free detected
     in tcache 2`, this patch 0/50.
   - 5000 dlopen/dlclose cycles: the minimal patch leaks 112 bytes per
     cycle, this patch is flat.

   All three were run against `libpcsclite_real.so.1` extracted from the
   built `libpcsclite1_2.4.1-1ubuntu0.1_amd64.deb`.

2. User test, needs a smart-card reader and pcscd: start seahorse, open
   the PKCS#11/GnuPG keys view so module enumeration starts, and quit
   seahorse while enumeration is in flight.  Unpatched: seahorse dies
   with SIGSEGV in `list_seek`; patched: it exits cleanly.  Not run here
   (no reader hardware).

3. Regression: normal PKCS#11 use still works - seahorse with a reader
   enumerates and uses the card, and `SCardEstablishContext()`,
   `SCardReleaseContext()` and `SCardGetStatusChange()` behave as
   before.

## Where problems could occur

`list_destroy()` now clears the list fields after freeing.  `list_seek`,
`list_locate`, `list_findpos` and `list_get_at` already reject NULL
sentinels, and `list_init()` reinitialises the fields on reload, so a
destroyed list behaves like an empty one and a second `list_destroy()`
is harmless.

`list_insert_at()` now frees the inserted data only when `copy_data` is
set; when it is unset the data is caller-owned, so the old unconditional
free was wrong.  This is what caused the double free.

`contextMapList_lock` is statically initialised and no longer destroyed;
it is never re-created while the library is loaded, so the only change
is that the destructor no longer calls `pthread_mutex_destroy()` on a
mutex a racing thread may still use.

## Other Info

Upstream: https://github.com/LudovicRousseau/PCSC/issues/271
The patch is the proposed upstream fix; it is not in any upstream
release yet.  The same change applies to 2.5.4 for tenacious.


** Patch added: "resolute (26.04) debdiff: pcsc-lite 2.4.1-1 -> 
2.4.1-1ubuntu0.1"
   
https://bugs.launchpad.net/bugs/2190903/+attachment/6025311/+files/resolute-2.4.1-1ubuntu0.1.debdiff

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2190903

Title:
  pcsc-lite: use-after-free and double free when an SCard call races the
  destructor

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/pcsc-lite/+bug/2190903/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to