All of lore.kernel.org
 help / color / mirror / Atom feed
* ALSA PCM: 3 vulnerabilities in OSS mmap path (mmap_count bypass + PAGE_ALIGN gap + error path UAF)
@ 2026-12-02 10:26 Prénom? Ahmed
  2026-12-02 11:08 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: Prénom? Ahmed @ 2026-12-02 10:26 UTC (permalink / raw)
  To: security, tiwai, perex; +Cc: alsa-devel

Hey folks,

I'm Ahmed Abdelmoemen (bohmiiidd) — I've been poking around the ALSA PCM
subsystem and found three bugs in the OSS compatibility layer that chain
together into a local privilege escalation path. I figured I should report
them properly rather than sit on them.

Bugs 1 and 2 are confirmed in QEMU on kernel 7.3.7 (latest stable, x86-64).
Bug 3 is a clear code-level vuln whose trigger needs specific hardware
(explained below).

This repository is currently private because it contains root-level exploit
code and five exploitation chains.

Let me know if you need any additional details. I can add the Linux kernel
team as contributors or share the repository access details directly in
this email.

The PoC code and QEMU test harness are available here:
https://github.com/bohmiiidd/linux-UAF.git
---

## Bug 1: is_oss_stream mmap_count bypass

**Where:** `sound/core/pcm_native.c:815`
**Type:** Logic error — safety check skipped for OSS streams
**Severity:** High
**Introduced by:** commit 92ee3c60ec9f ("ALSA: pcm: Fix potential AB/BA
lock")
**Affected:** CONFIG_SND_PCM_OSS=y, kernel 5.17+

So here's the thing — in `snd_pcm_hw_params()`, the mmap_count guard that's
supposed to prevent hw_params from running while userspace holds a mapping
is
deliberately skipped for OSS streams:

```c
// sound/core/pcm_native.c:815
if (!is_oss_stream(substream) &&
    atomic_read(&substream->mmap_count))
    err = -EBADFD;
```

I get why it was done (the AB/BA lock fix needed it for backward compat),
but
the result is that hw_params can run — including buffer reallocation and
memset — while userspace holds a live VM_PFNMAP mapping. Since
remap_pfn_range() doesn't do page reference counting, the kernel has no way
to revoke the mapping when the buffer gets freed or cleared.

**How I proved it in QEMU:** Open `/dev/dsp` → SETFRAGMENT (small buffer) →
read (triggers allocation) → mmap PROT_READ on capture substream → mprotect
PROT_WRITE → write 0x41 markers to the whole page → change sample rate →
read
again (triggers hw_params re-parameterization).

Result: hw_params runs `memset(dma_area, 0, PAGE_ALIGN(dma_bytes))`, and all
4096 marker bytes get zeroed through the live mapping. The kernel is
modifying
memory that userspace still has mapped — not great!

---

## Bug 2: PAGE_ALIGN mmap size gap

**Where:** `sound/core/pcm_native.c:4050`
**Type:** Integer rounding — maps more memory than was logically allocated
**Severity:** Medium
**Affected:** All kernels with ALSA PCM mmap support

The mmap size validation rounds UP with PAGE_ALIGN:

```c
// sound/core/pcm_native.c:4050
dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
if ((size_t)size > dma_bytes)
    return -EINVAL;
```

But `snd_pcm_lib_malloc_pages()` stores the exact requested size:

```c
// sound/core/pcm_memory.c:438
if (runtime->dma_buffer_p->bytes >= size)
    return 0;  // dmab->bytes = exact size, NOT page-aligned
```

With SETFRAGMENT producing a 512-byte buffer:
- `dmab->bytes = 512` (what was actually allocated)
- `PAGE_ALIGN(512) = 4096` (what mmap allows)
- Userspace gets to mmap 4096 bytes from a 512-byte logical buffer

This creates a fun size mismatch where `mmap_bytes (4096) > dmab->bytes
(512)`.
The realloc check at pcm_memory.c:438 (`dmab->bytes >= size`) would trigger
a
reallocation if a buffer request exceeded 512 — but OSS layer constraints
(SETFRAGMENT one-shot + maxfrags lock) currently prevent that path.

**QEMU proof:** `mmap(4096, PROT_READ)` succeeds on a 512-byte buffer. Full
4096-byte read/write confirmed via mprotect + memset.

---

## Bug 3: Error path frees buffer without checking mmap_count

**Where:** `sound/core/pcm_native.c:917`
**Type:** Use-after-free — DMA buffer freed while userspace mapping alive
**Severity:** Critical
**Affected:** CONFIG_SND_PCM_OSS=y + managed buffer driver, kernel 5.17+

This is the dangerous one. When a driver's hw_params callback fails AFTER
`snd_pcm_lib_malloc_pages()` has already allocated the buffer, the error
cleanup frees it with no mmap_count check:

```c
// sound/core/pcm_native.c:908-919
_error:
    if (err) {
        snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
        if (substream->ops->hw_free != NULL)
            substream->ops->hw_free(substream);
        if (substream->managed_buffer_alloc)
            snd_pcm_lib_free_pages(substream);  // <-- no mmap_count check!
    }
```

Because Bug 1 lets OSS streams skip the mmap_count guard at line 815, this
error path is fully reachable with an active mapping. The freed page goes
back
to the PCP free list while the VM_PFNMAP mmap stays alive (no refcount = no
revocation). Classic use-after-free.

**How exploitation would work:**
1. Open `/dev/dsp`, allocate small DMA buffer via SETFRAGMENT
2. mmap 4096 bytes via the PAGE_ALIGN gap (Bug 2) — VM_PFNMAP, no refcount
3. Trigger a hw_params failure → error path frees buffer → page returns to
PCP
4. Fork spray with VMAP_STACK=n → cred_jar allocates from PCP order-0
5. Scan mmap'd page for struct cred (uid=65534) → overwrite uid/caps → root

**What hardware you need:** The driver's hw_params callback has to fail
AFTER
malloc_pages succeeds. Known triggers:
- **Intel ICH4+ AC97:** `snd_ac97_pcm_open()` returns -EBUSY on AC97 slot
  conflict when two capture streams fight for the same slots
(ac97_pcm.c:580)
- **Intel HDA:** `azx_pcm_hw_params()` returns -EBUSY when DSP is locked
  (hda_controller.c:115-117)
- Any managed-buffer driver whose hw_params can fail post-allocation

**Why QEMU didn't trigger it:** QEMU's AC97 is non-ICH4 with no ADC2, so the
driver's hw_params always succeeds — error path never fires. Bugs 1 and 2
are
confirmed; Bug 3 needs real hardware or a test module.

---

## Also worth looking at: realloc path (pcm_memory.c:442)

Same class of bug — `snd_pcm_lib_malloc_pages()` frees the old buffer during
reallocation without checking mmap_count:

```c
// sound/core/pcm_memory.c:442
snd_pcm_lib_free_pages(substream);  // no mmap_count check
```

Currently mitigated from OSS: SETFRAGMENT is one-shot (pcm_oss.c:1982) and
maxfrags locks the period count, so buffer size can't change between calls.
But the missing check is the same pattern as Bug 3 — might bite through
a different path someday.

---

## Full audit of snd_pcm_lib_free_pages() callers

| Call site | Location | mmap_count guard | Status |
|---|---|---|---|
| hw_params error path | pcm_native.c:918 | **NONE** | **VULNERABLE** (Bug
3) |
| do_hw_free via hw_free | pcm_native.c:952 via :981 | Yes (:971) | Safe |
| do_hw_free via release | pcm_native.c:952 via :2788 | VMA holds file ref
| Safe |
| malloc_pages realloc | pcm_memory.c:442 | **NONE** | Mitigated by OSS
layer |

---

## Suggested Fixes

I've got four patches in mind — I'd recommend applying all of them together,
but here's the breakdown:

**Fix A — Guard the error path (minimal fix for Bug 3):**
```diff
     _error:
         if (err) {
             snd_pcm_set_state(substream, SNDRV_PCM_STATE_OPEN);
             if (substream->ops->hw_free != NULL)
                 substream->ops->hw_free(substream);
-            if (substream->managed_buffer_alloc)
+            if (substream->managed_buffer_alloc &&
+                !atomic_read(&substream->mmap_count))
                 snd_pcm_lib_free_pages(substream);
         }
```

**Fix B — Remove the is_oss_stream bypass (fixes Bug 1):**
```diff
-        if (check_mmap && !is_oss_stream(substream) &&
-            atomic_read(&substream->mmap_count))
+        if (check_mmap && atomic_read(&substream->mmap_count))
             return -EBADFD;
```
Heads up: this might break OSS apps that re-parameterize while mmap'd. Worth
testing with PulseAudio's OSS compat and any legacy OSS software you know
of.

**Fix C — Defense-in-depth in snd_pcm_lib_free_pages() (catches
everything):**
```diff
 int snd_pcm_lib_free_pages(struct snd_pcm_substream *substream)
 {
+    if (WARN_ON(atomic_read(&substream->mmap_count)))
+        return -EBUSY;
     ...
 }
```

**Fix D — Tighten the mmap size check (fixes Bug 2):**
```diff
-    dma_bytes = PAGE_ALIGN(runtime->dma_bytes);
+    dma_bytes = runtime->dma_bytes;
     if ((size_t)size > dma_bytes)
         return -EINVAL;
```
This could break apps that expect to mmap a full page from a sub-page
buffer.
A safer variant would be `min(PAGE_ALIGN(dma_bytes), dmab->bytes)` but that
needs dmab accessible in the mmap path.

---

## How this differs from existing CVEs

Just to be clear — I checked these and our bugs are distinct:

- **CVE-2022-49291** (commit 92ee3c60ec9f): This is the commit that
  *introduced* the is_oss_stream bypass we're exploiting. It was fixing an
  AB/BA lock ordering issue. We're reporting that the fix created a new
hole.

- **CVE-2026-98116** (commit 9b110a9dcecc): Fixes a race in
  snd_pcm_mmap_data() buffer_accessing serialization. Our vector is a logic
  bug, not a race — completely different mechanism.

- **CVE-2026-139792** (commit 9017bc3d13da): Blocks O_APPEND for OSS
streams.
  Doesn't touch the mmap_count bypass or error path. Our vector is single
  process, no O_APPEND.

---

## Timeline

- 2022-03: Commit 92ee3c60ec9f introduces is_oss_stream bypass
- 2026-11-30: Bugs 1-3 discovered, PoC chain proven in QEMU
- 2026-12-01: PAGE_ALIGN gap (Bug 2) discovered and confirmed
- 2026-12-02: Verified on latest stable 7.3.7, QEMU test confirmed
- 2026-12-02: This report

## Affected Configurations

- CONFIG_SND_PCM_OSS=y (enabled on most distros for OSS compat)
- DMA-coherent sound driver with managed buffer allocation
- /dev/dsp accessible to unprivileged user (varies by distro udev rules)
- Kernel 5.17+ (commit 92ee3c60ec9f introduced the bypass)

---

I'd appreciate a 90-day embargo before public disclosure. Happy to provide
full PoC source, QEMU test harness, and exploitation logs — whatever helps
you reproduce and fix this.

Let me know if you have questions or need anything else!

Cheers,
Ahmed Abdelmoemen (bohmiiidd)
ahmedabdelmoumen05@gmail.com
https://github.com/bohmiiidd/linux-UAF (private)

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: ALSA PCM: 3 vulnerabilities in OSS mmap path (mmap_count bypass + PAGE_ALIGN gap + error path UAF)
  2026-12-02 10:26 ALSA PCM: 3 vulnerabilities in OSS mmap path (mmap_count bypass + PAGE_ALIGN gap + error path UAF) Prénom? Ahmed
@ 2026-12-02 11:08 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-12-02 11:08 UTC (permalink / raw)
  To: Prénom? Ahmed; +Cc: security, tiwai, perex, alsa-devel

On Wed, Dec 02, 2026 at 10:26:45AM +0000, Prénom? Ahmed wrote:
> Hey folks,
> 
> I'm Ahmed Abdelmoemen (bohmiiidd) — I've been poking around the ALSA PCM
> subsystem and found three bugs in the OSS compatibility layer that chain
> together into a local privilege escalation path. I figured I should report
> them properly rather than sit on them.
> 
> Bugs 1 and 2 are confirmed in QEMU on kernel 7.3.7 (latest stable, x86-64).
> Bug 3 is a clear code-level vuln whose trigger needs specific hardware
> (explained below).
> 
> This repository is currently private because it contains root-level exploit
> code and five exploitation chains.
> 
> Let me know if you need any additional details. I can add the Linux kernel
> team as contributors or share the repository access details directly in
> this email.

You sent this to a public list, so it's obviously a public issue now, no
need to get security@kernel.org involved, just send patches for these
issues now please.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-02 11:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 10:26 ALSA PCM: 3 vulnerabilities in OSS mmap path (mmap_count bypass + PAGE_ALIGN gap + error path UAF) Prénom? Ahmed
2026-12-02 11:08 ` Greg KH

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.