[PATCH] ALSA: us122l: Prevent write upgrades for read mappings

Kazuki Hanai posted 1 patch 2 weeks, 3 days ago
sound/usb/usx2y/us122l.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
[PATCH] ALSA: us122l: Prevent write upgrades for read mappings
Posted by Kazuki Hanai 2 weeks, 3 days ago
The hwdep mmap callback rejects read-buffer mappings that are initially
writable, but leaves VM_MAYWRITE set on mappings created with PROT_READ.
A process that can open the hwdep node O_RDWR can later use mprotect() to
make the mapping writable.

The read allocation begins with struct usb_stream. Its read_size member is
used by the fault handler to decide which pages belong to the read buffer.
The read VMA intentionally remains expandable because pcm_usb_stream uses
mremap() after reading that size. Changing read_size first can therefore
map and access pages beyond the allocation. The same member is also
consumed by usb_stream_free(), where changing it can make
free_pages_exact() release pages outside the allocation.

Clear VM_MAYWRITE for read-buffer mappings after rejecting an initially
writable VMA. This keeps the separate output-buffer mapping writable while
preventing later permission upgrades.

Fixes: 030a07e44129 ("ALSA: Add USB US122L driver")
Cc: stable@vger.kernel.org
Signed-off-by: Kazuki Hanai <hnkz.64@gmail.com>
---
 sound/usb/usx2y/us122l.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/sound/usb/usx2y/us122l.c b/sound/usb/usx2y/us122l.c
index f00b53346abd..a5db0d044ef9 100644
--- a/sound/usb/usx2y/us122l.c
+++ b/sound/usb/usx2y/us122l.c
@@ -180,8 +180,11 @@ static int usb_stream_hwdep_mmap(struct snd_hwdep *hw,
 	guard(mutex)(&us122l->mutex);
 	s = us122l->sk.s;
 	read = offset < s->read_size;
-	if (read && area->vm_flags & VM_WRITE)
-		return -EPERM;
+	if (read) {
+		if (area->vm_flags & VM_WRITE)
+			return -EPERM;
+		vm_flags_clear(area, VM_MAYWRITE);
+	}
 	/* if userspace tries to mmap beyond end of our buffer, fail */
 	if (size > PAGE_ALIGN(read ? s->read_size : s->write_size)) {
 		dev_warn(hw->card->dev, "%s: size %lu > %u\n", __func__,
Re: [PATCH] ALSA: us122l: Prevent write upgrades for read mappings
Posted by Takashi Iwai 2 weeks, 3 days ago
On Tue, 08 Sep 2026 13:00:53 +0200,
Kazuki Hanai wrote:
> 
> The hwdep mmap callback rejects read-buffer mappings that are initially
> writable, but leaves VM_MAYWRITE set on mappings created with PROT_READ.
> A process that can open the hwdep node O_RDWR can later use mprotect() to
> make the mapping writable.
> 
> The read allocation begins with struct usb_stream. Its read_size member is
> used by the fault handler to decide which pages belong to the read buffer.
> The read VMA intentionally remains expandable because pcm_usb_stream uses
> mremap() after reading that size. Changing read_size first can therefore
> map and access pages beyond the allocation. The same member is also
> consumed by usb_stream_free(), where changing it can make
> free_pages_exact() release pages outside the allocation.
> 
> Clear VM_MAYWRITE for read-buffer mappings after rejecting an initially
> writable VMA. This keeps the separate output-buffer mapping writable while
> preventing later permission upgrades.
> 
> Fixes: 030a07e44129 ("ALSA: Add USB US122L driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Kazuki Hanai <hnkz.64@gmail.com>

Applied now.  Thanks.


Takashi