git: 18f80d6d4634 - releng/14.1 - sound: Check user-supplied size passed to SNDSTIOC_ADD_USER_DEVS*

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Wed, 22 May 2024 13:23:19 UTC
The branch releng/14.1 has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=18f80d6d463495703f9eb2ccfd92bae93c7889a3

commit 18f80d6d463495703f9eb2ccfd92bae93c7889a3
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2024-05-20 14:18:28 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2024-05-22 13:22:26 +0000

    sound: Check user-supplied size passed to SNDSTIOC_ADD_USER_DEVS*
    
    SNDSTIOC_ADD_USER_DEVS* expects a user-supplied sndstioc_nv_arg->nbytes,
    however we currently do not check whether this size is actually valid,
    which results in a panic when SNDSTIOC_ADD_USER_DEVS* is called with an
    invalid size. sndstat_add_user_devs() calls
    sndstat_unpack_user_nvlbuf(), which then calls malloc() with that size.
    
    PR:             266142
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 day
    Reviewed by:    brooks
    Differential Revision:  https://reviews.freebsd.org/D45236
    
    (cherry picked from commit 074d337ad618f9cc2a1d5ab18b484928e57bd72b)
    (cherry picked from commit 5830a00c2c5485ec17900558e4f29c459c6a1f3e)
    
    Approved by:    re (cperciva)
---
 sys/dev/sound/pcm/sndstat.c | 5 +++++
 sys/sys/sndstat.h           | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/sys/dev/sound/pcm/sndstat.c b/sys/dev/sound/pcm/sndstat.c
index ef006a580d40..eb5c68c7c519 100644
--- a/sys/dev/sound/pcm/sndstat.c
+++ b/sys/dev/sound/pcm/sndstat.c
@@ -865,6 +865,11 @@ sndstat_add_user_devs(struct sndstat_file *pf, caddr_t data)
 		goto done;
 	}
 
+	if (arg->nbytes > SNDST_UNVLBUF_MAX) {
+		err = ENOMEM;
+		goto done;
+	}
+
 	err = sndstat_unpack_user_nvlbuf(arg->buf, arg->nbytes, &nvl);
 	if (err != 0)
 		goto done;
diff --git a/sys/sys/sndstat.h b/sys/sys/sndstat.h
index f0e4d352242f..8a49042b0453 100644
--- a/sys/sys/sndstat.h
+++ b/sys/sys/sndstat.h
@@ -74,6 +74,11 @@ struct sndstioc_nv_arg {
 #define SNDST_DSPS_SOUND4_PVCHAN	"pvchan"
 #define SNDST_DSPS_SOUND4_RVCHAN	"rvchan"
 
+/*
+ * Maximum user-specified nvlist buffer size
+ */
+#define SNDST_UNVLBUF_MAX		65535
+
 #define SNDSTIOC_REFRESH_DEVS \
 	_IO('D', 100)
 #define SNDSTIOC_GET_DEVS \