git: 315d50898cc2 - main - sound/midi: Create the device node with make_dev_s()

From: Christos Margiolis <christos_at_FreeBSD.org>
Date: Thu, 12 Nov 2026 16:38:20 UTC
The branch main has been updated by christos:

URL: https://cgit.FreeBSD.org/src/commit/?id=315d50898cc2a561020059251a42068b3f983e78

commit 315d50898cc2a561020059251a42068b3f983e78
Author:     Christos Margiolis <christos@FreeBSD.org>
AuthorDate: 2026-11-12 10:49:46 +0000
Commit:     Christos Margiolis <christos@FreeBSD.org>
CommitDate: 2026-11-12 16:35:49 +0000

    sound/midi: Create the device node with make_dev_s()
    
    make_dev(9) publishes the node before si_drv1 is assigned, so an open(2)
    racing with midi_init() could dereference si_drv1 before it's set.
    Triggering a UAF is not currently an issue because all cdevsw methods
    check for NULL, but the fix makes it more robust in any case.
    
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
---
 sys/dev/sound/midi/midi.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/sys/dev/sound/midi/midi.c b/sys/dev/sound/midi/midi.c
index 790fdc05feac..03f5907491ca 100644
--- a/sys/dev/sound/midi/midi.c
+++ b/sys/dev/sound/midi/midi.c
@@ -146,10 +146,12 @@ midi_destroy(struct snd_midi *m)
 struct snd_midi *
 midi_init(kobj_class_t cls, void *cookie)
 {
+	struct make_dev_args args;
 	struct snd_midi *m;
 	size_t inqsize, outqsize;
 	uint8_t *ibuf = NULL;
 	uint8_t *obuf = NULL;
+	int err;
 
 	m = malloc(sizeof(*m), M_MIDI, M_WAITOK | M_ZERO);
 	kobj_init((kobj_t)m, cls);
@@ -184,9 +186,20 @@ midi_init(kobj_class_t cls, void *cookie)
 		return (NULL);
 	}
 
-	m->dev = make_dev(&midi_cdevsw, m->unit, UID_ROOT, GID_AUDIO, 0660,
-	    "midi%d.%d", m->unit, m->channel);
-	m->dev->si_drv1 = m;
+	make_dev_args_init(&args);
+	args.mda_devsw = &midi_cdevsw;
+	args.mda_unit = m->unit;
+	args.mda_uid = UID_ROOT;
+	args.mda_gid = GID_AUDIO;
+	args.mda_mode = 0660;
+	args.mda_si_drv1 = m;
+	err = make_dev_s(&args, &m->dev, "midi%d.%d", m->unit, m->channel);
+	if (err != 0) {
+		printf("%s: failed to create midi%d.%d: error %d\n", __func__,
+		    m->unit, m->channel, err);
+		midi_destroy(m);
+		return (NULL);
+	}
 
 	return (m);
 }