From nobody Wed Oct 14 16:03:54 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4j4bbt4BPgz6vmQQ for ; Wed, 14 Oct 2026 16:03:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4j4bbt2R92z4ZbS for ; Wed, 14 Oct 2026 16:03:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791993834; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yUYwjkP3BJ9mCMwBOOqHaflvbxxbEf3tmzw1/Dlimfk=; b=eN8ebpQHZKVsPyJeSUukEeAWCnPHHcc1ALJr6/OG83JvJAAWm9u1Xnx5SB8nGg3Acccnse +gLP88B0oQ5YifZX4Ba8ECQ7Cp/66Ur/n+eGa15psXdtUTWUIgCSvOLvtEeyiLgJ/hyZ5K QWpCbX6I/ZqYexJiMXH1H7h43LrDV550pPHNsubl9bhASphyV32Lk1tOvtoBBccJwoKxSb O/HIEvokqqjdWvuIJ3W2R/vDl1b9mGYC4BivJT9IWHquaUBHgrpNRU7S0BShsnuJ0L7APM 6UEfqXlNFlcewW5d9ffR4QlM9p9XZn4tPQq8yilzuEPo7M7/xa00WzB0Dmh+vA== ARC-Seal: i=1; a=rsa-sha256; d=freebsd.org; s=dkim; cv=none; t=1791993834; b=iMTIDVKrNFbap+9sbWxo8i2m8GJXdNlIh2QjWVnZcOCPLFRoCWwDw1xel/Oxh/yCy7GDRf 97RLivQh+EFpFycKaus8PQpqSb0Z8BC1UAEBB7e8m/UJHQfyqad+Y21z+4Sztb9uC9OBAe AxD7S3kvz++krqgje5GvrBV3hhAms0B2tE5GrpLiWdkwi5Pur1G05ZZ0L+xkYCTigdFI+J s4LOx8ZZ0D69tZxbnalLolBqQa7XykZNUG+vZHklz5xHYD5fTsw6iTgGprNMQRVXOQuX9A pii7lRsDUS2/1qaqqXbJkejyr0ufjMfRpAAZj8Ja2rPR1oypsYaXu5islOXimw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1791993834; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yUYwjkP3BJ9mCMwBOOqHaflvbxxbEf3tmzw1/Dlimfk=; b=NnybxewGpBpu/dvdwNjWgqFeuzUwh75peuzV4vURMRSE4IfIZqzvBR9o3KnSArzOfJd4K3 ug8GH4uAogumcO+ucOnJQw9KyrtCKqe52Lu0KxI61rie4H7HScZ7X/1jREFl9SQ5JA5U4L nkyCcwWUSkxEc8nLSJVoAl586IsP6yR9G4M0qu4fjC5dEbj8dxQByto7M1V2R+6O94G6U7 +1Tv1YWvvpr6DEkvpeuX4YJV0L4tQ7nyYKmiyij8g9QoNLmHsxSK0zCVpqTgtCHMe7S8ru nKhzk/PyfPBv1K3fcV8F/XfkJZOedd5YaLhyMXMT4ZoEiKv/fou8hXLlQ89AOA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4j4bbt1WQTz18Qw for ; Wed, 14 Oct 2026 16:03:54 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 30025 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Wed, 14 Oct 2026 16:03:54 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mark Johnston Subject: git: ea6ecd1e3762 - releng/14.4 - sound: Revalidate after reacquiring locks when resizing buffers List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: Commits to the stable branches of the FreeBSD src repository List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/releng/14.4 X-Git-Reftype: branch X-Git-Commit: ea6ecd1e3762574a4c6d76ca9f12f7f914547a03 Auto-Submitted: auto-generated Date: Wed, 14 Oct 2026 16:03:54 +0000 Message-Id: <6acfa7ea.30025.7e8bccb0@gitrepo.freebsd.org> The branch releng/14.4 has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=ea6ecd1e3762574a4c6d76ca9f12f7f914547a03 commit ea6ecd1e3762574a4c6d76ca9f12f7f914547a03 Author: Mark Johnston AuthorDate: 2026-10-13 15:54:07 +0000 Commit: Mark Johnston CommitDate: 2026-10-13 16:54:40 +0000 sound: Revalidate after reacquiring locks when resizing buffers sndbuf_resize() and sndbuf_remalloc() drop the channel lock in order to allocate the new buffer(s) with M_WAITOK. Before doing that they verify that the buffer is not mapped, but this could change while the lock is dropped, as there is no higher-level synchronization which prevents buffer resize (e.g., via the SNDCTL_DSP_SETBLKSIZE ioctl) from happening concurrently with mmap. Thus, these checks are insufficient. Modify sndbuf_resize() and sndbuf_remalloc() to revalidate after reacquiring the lock. Extend them to check for a couple of channel flags, mirroring the check in chn_resizebuf(). Modify chn_resizebuf() to revalidate state after reacquiring the channel lock. We could also allocate new buffers with M_NOWAIT and so avoid dropping the lock. Then, I think we could go even further and invoke CHANNEL_SETBLOCKSIZE and CHANNEL_SETFRAGMENTS with the channel lock held. But really it would be better to ensure that dsp operations are serialized; there is no reason for mmap and chn_resizebuf() to be able to run in parallel, we should not be relying on the channel lock for this. So, let's keep a more minimal approach here. Add a simple regression test. Approved by: so Security: FreeBSD-SA-26:71.sound Security: CVE-2026-101308 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D60554 --- sys/dev/sound/pcm/buffer.c | 45 +++++++++++++++++++++++++++-------- sys/dev/sound/pcm/channel.c | 11 +++++++-- tests/sys/sound/mmap.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 12 deletions(-) diff --git a/sys/dev/sound/pcm/buffer.c b/sys/dev/sound/pcm/buffer.c index ddcf14fbc1c2..53eea0113126 100644 --- a/sys/dev/sound/pcm/buffer.c +++ b/sys/dev/sound/pcm/buffer.c @@ -84,12 +84,31 @@ sndbuf_rele(struct snd_dbuf *b) } } + bus_addr_t sndbuf_getbufaddr(struct snd_dbuf *buf) { return (buf->buf_addr); } +static bool +sndbuf_busy(struct snd_dbuf *b) +{ + CHN_LOCKASSERT(b->channel); + + return (refcount_load(&b->refcount) > 1 || + (b->flags & SNDBUF_F_DETACHED) != 0 || + /* + * XXX-MJ CHN_F_MMAP is not reliable, but for software buffers the + * refcount accurately reflects whether the buffer is mapped. + * Ensure that there is no in-flight DMA transfer by checking + * CHN_F_TRIGGERED. This should really be checked at a higher + * layer, but this is tricky since chn_resizebuf() and + * sndbuf_resize() drop and reacquire the channel lock. + */ + (b->channel->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0); +} + static void sndbuf_setmap(void *arg, bus_dma_segment_t *segs, int nseg, int error) { @@ -207,8 +226,7 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) if (bufsize > b->allocsize || bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) { - if (refcount_load(&b->refcount) > 1 || - (b->flags & SNDBUF_F_DETACHED) != 0) { + if (sndbuf_busy(b)) { CHN_UNLOCK(b->channel); return (EBUSY); } @@ -216,6 +234,11 @@ sndbuf_resize(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) CHN_UNLOCK(b->channel); tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); CHN_LOCK(b->channel); + if (sndbuf_busy(b)) { + CHN_UNLOCK(b->channel); + free(tmpbuf, M_DEVBUF); + return (EBUSY); + } if (snd_verbose > 3) printf("%s(): b=%p %p -> %p [%d -> %d : %d]\n", __func__, b, b->tmpbuf, tmpbuf, @@ -253,8 +276,7 @@ sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) if (bufsize > b->allocsize || bufsize < (b->allocsize >> SNDBUF_CACHE_SHIFT)) { - if (refcount_load(&b->refcount) > 1 || - (b->flags & SNDBUF_F_DETACHED) != 0) + if (sndbuf_busy(b)) return (EBUSY); allocsize = round_page(bufsize); CHN_UNLOCK(b->channel); @@ -262,14 +284,17 @@ sndbuf_remalloc(struct snd_dbuf *b, unsigned int blkcnt, unsigned int blksz) tmpbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); shadbuf = malloc(allocsize, M_DEVBUF, M_WAITOK); CHN_LOCK(b->channel); - if (b->buf != NULL) - free(b->buf, M_DEVBUF); + if (sndbuf_busy(b)) { + free(buf, M_DEVBUF); + free(tmpbuf, M_DEVBUF); + free(shadbuf, M_DEVBUF); + return (EBUSY); + } + free(b->buf, M_DEVBUF); b->buf = buf; - if (b->tmpbuf != NULL) - free(b->tmpbuf, M_DEVBUF); + free(b->tmpbuf, M_DEVBUF); b->tmpbuf = tmpbuf; - if (b->shadbuf != NULL) - free(b->shadbuf, M_DEVBUF); + free(b->shadbuf, M_DEVBUF); b->shadbuf = shadbuf; if (snd_verbose > 3) printf("%s(): b=%p %d -> %d [%d]\n", diff --git a/sys/dev/sound/pcm/channel.c b/sys/dev/sound/pcm/channel.c index 5264bad15516..e803c8fd3407 100644 --- a/sys/dev/sound/pcm/channel.c +++ b/sys/dev/sound/pcm/channel.c @@ -1873,8 +1873,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, CHN_LOCKASSERT(c); - if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) || - !(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC)) + if (!(c->direction == PCMDIR_PLAY || c->direction == PCMDIR_REC)) + return EINVAL; + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) return EINVAL; if (latency == -1) { @@ -1931,6 +1932,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, chn_notify(c->parentchannel, CHN_N_BLOCKSIZE); CHN_UNLOCK(c->parentchannel); CHN_LOCK(c); + /* Revalidate. */ + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) + return EINVAL; if (c->direction == PCMDIR_PLAY) { limit = (pb != NULL) ? sndbuf_xbytes(sndbuf_getsize(pb), pb, bs) : 0; @@ -1988,6 +1992,9 @@ chn_resizebuf(struct pcm_channel *c, int latency, sndbuf_setblksz(b, nblksz); } CHN_LOCK(c); + /* Revalidate. */ + if ((c->flags & (CHN_F_MMAP | CHN_F_TRIGGERED)) != 0) + return EINVAL; if (!CHN_EMPTY(c, children)) { nsblksz = round_blksz( diff --git a/tests/sys/sound/mmap.c b/tests/sys/sound/mmap.c index f775c0f8da8a..800a523debf9 100644 --- a/tests/sys/sound/mmap.c +++ b/tests/sys/sound/mmap.c @@ -102,10 +102,68 @@ ATF_TC_BODY(mmap_buffer_lifetime, tc) ATF_REQUIRE(munmap(buf, len) == 0); } +/* + * Verify that an attempt to resize a channel's buffer while it is mmapped + * fails. + */ +ATF_TC(mmap_resize_fails); +ATF_TC_HEAD(mmap_resize_fails, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Resizing a mmapped channel buffer fails"); + atf_tc_set_md_var(tc, "require.kmods", "snd_dummy"); +} +ATF_TC_BODY(mmap_resize_fails, tc) +{ + audio_buf_info abi; + uint8_t *buf; + size_t len; + int fd, arg, ret; + + fd = open("/dev/dsp.dummy", O_RDWR); + ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open")); + + arg = (2 << 16) | 14; /* 2 * 16KB */ + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == 0, + FMT_ERR("SNDCTL_DSP_SETFRAGMENT")); + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_GETOSPACE, &abi) == 0, + FMT_ERR("SNDCTL_DSP_GETOSPACE")); + + len = abi.fragstotal * abi.fragsize; + ATF_REQUIRE_MSG(len >= PAGE_SIZE, "buffer too small: %zu", len); + + buf = mmap(NULL, len, PROT_WRITE, MAP_SHARED, fd, 0); + ATF_REQUIRE_MSG(buf != MAP_FAILED, FMT_ERR("mmap")); + + /* Try to resize the buffer; this should fail. */ + arg = (4 << 16) | 12; /* 4 * 4KB */ + ret = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg); + ATF_REQUIRE_MSG(ret != 0, "resize of mmapped buffer should fail"); + ATF_REQUIRE_MSG(errno == EINVAL, + "expected EINVAL, got %s", strerror(errno)); + + ATF_REQUIRE(munmap(buf, len) == 0); + + /* + * CHN_F_MMAP is only cleared when the fd is closed, so resizing still + * fails after munmap. This is probably a bug, but we can at least + * verify that the resize succeeds after the fd is closed and reopened. + */ + ATF_REQUIRE(close(fd) == 0); + fd = open("/dev/dsp.dummy", O_RDWR); + ATF_REQUIRE_MSG(fd >= 0, FMT_ERR("open")); + arg = (4 << 16) | 12; /* 4 * 4KB */ + ATF_REQUIRE_MSG(ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) == 0, + FMT_ERR("SNDCTL_DSP_SETFRAGMENT after reopen")); + + ATF_REQUIRE(close(fd) == 0); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, mmap_offset_overflow); ATF_TP_ADD_TC(tp, mmap_buffer_lifetime); + ATF_TP_ADD_TC(tp, mmap_resize_fails); return (atf_no_error()); }