ALSA: seq: oss: copy SysEx payloads before handoff
PATCH 1/1 ALSA: seq: oss: copy SysEx payloads before handoff
The OSS sequencer MIDI output path feeds bytes into an snd_midi_event coder from snd_seq_oss_midi_putc(). When a SysEx message completes, the coder builds a variable-length sequencer event whose data.ext.ptr still points at the coder-owned buffer.
snd_seq_oss_midi_putc() returns after dropping the MIDI device use reference, but the caller still owns the freshly built event and may enqueue it afterwards. A concurrent port-removal notification can therefore run snd_seq_oss_midi_check_exit_port() and free the coder buffer before snd_seq_event_dup() copies the variable payload.
The dangerous interleaving is:
write path:
snd_seq_oss_midi_putc() completes a SysEx event whose data.ext.ptr points at mdev->coder storage. It then returns and drops the mdev use reference. insert_queue() later enqueues the variable-length event and snd_seq_event_dup() copies from event.data.ext.ptr.
teardown path:
a PORT_EXIT notification reaches snd_seq_oss_midi_check_exit_port(), removes the MIDI device, and frees mdev->coder before the queued event is duplicated.
Serialize access to the MIDI coder and copy completed SysEx payloads into caller-owned scratch storage before returning to the enqueue or direct-dispatch side. The copied payload remains valid until the sequencer core has synchronously duplicated or delivered the event, so teardown can no longer free the source buffer from under snd_seq_event_dup().
KASAN report as below:
KASAN slab-use-after-free in snd_seq_event_dup+0x40c/0x470
RIP: 0033:0x7f2cb66a6340 Read of size 6 Call trace: dump_stack_lvl+0x73/0xb0 (?:?) print_report+0xd1/0x650 (?:?) srso_alias_return_thunk+0x5/0xfbef5 (?:?) __virt_addr_valid+0x1a7/0x340 (?:?) kasan_complete_mode_report_info+0x64/0x200 (?:?) kasan_report+0xf7/0x130 (?:?) snd_seq_event_dup+0x40c/0x470 (?:?) kasan_check_range+0x10c/0x1c0 (?:?) __asan_memcpy+0x27/0x70 (?:?) snd_seq_event_dup+0x9/0x470 (?:?) snd_seq_client_enqueue_event+0x139/0x240 (?:?) _raw_spin_unlock_irqrestore+0x4b/0x60 (?:?) snd_seq_kernel_client_enqueue+0x102/0x120 (?:?) snd_seq_oss_write+0x416/0x4e0 (?:?) apparmor_file_permission+0x20/0x30 (?:?) odev_write+0x3b/0x60 (?:?) vfs_write+0x1ce/0x850 (?:?) lock_release+0xc8/0x2a0 (?:?) __kasan_check_write+0x18/0x20 (?:?) __mutex_unlock_slowpath+0x129/0x510 (?:?) ksys_write+0xe1/0x180 (?:?) mutex_unlock+0x16/0x20 (?:?) odev_ioctl+0x65/0xc0 (?:?) __x64_sys_write+0x46/0x60 (?:?) x64_sys_call+0x7d/0x20d0 (?:?) do_syscall_64+0xc1/0x360 (arch/x86/entry/syscall_64.c:87) entry_SYSCALL_64_after_hwframe+0x77/0x7f (?:?)
--- sound/core/seq/oss/seq_oss_event.c | 8 ++++--- sound/core/seq/oss/seq_oss_event.h | 4 +++- sound/core/seq/oss/seq_oss_ioctl.c | 6 +++-- sound/core/seq/oss/seq_oss_midi.c | 37 +++++++++++++++++++++++++++--- sound/core/seq/oss/seq_oss_midi.h | 3 ++- sound/core/seq/oss/seq_oss_rw.c | 4 +++- 6 files changed, 51 insertions(+), 11 deletions(-)
sound/core/seq/oss/seq_oss_event.c +5 -3
@@ -39,7 +39,9 @@ static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct s*/ int -snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev) +snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen) {switch (q->s.code) {case SEQ_EXTENDED :
@@ -69,7 +71,8 @@ snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct sndif (snd_seq_oss_midi_open (dp, q->s.dev,SNDRV_SEQ_OSS_FILE_WRITE ))break ;if (snd_seq_oss_midi_filemode (dp, q->s.dev) &SNDRV_SEQ_OSS_FILE_WRITE ) -return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, ev); +return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, + ev, buf, buflen);break ;case SEQ_ECHO :
@@ -442,4 +445,3 @@ snd_seq_oss_event_input(struct snd_seq_event *ev, int direct, void *private_data }return 0 ; } -
sound/core/seq/oss/seq_oss_event.h +3 -1
@@ -91,7 +91,9 @@ union evrec {#define ev_is_long (ev) ((ev)->s.code >= 128)#define ev_length (ev) ((ev)->s.code >= 128 ? LONG_EVENT_SIZE : SHORT_EVENT_SIZE) -int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev); +int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen);int snd_seq_oss_process_timer_event (struct seq_oss_timer *rec,union evrec *q);int snd_seq_oss_event_input (struct snd_seq_event *ev,int direct,void *private_data,int atomic,int hop);
sound/core/seq/oss/seq_oss_ioctl.c +4 -2
@@ -14,6 +14,7 @@#include "seq_oss_synth.h" #include "seq_oss_midi.h" #include "seq_oss_event.h" +#include <sound/seq_midi_event.h> static int snd_seq_oss_synth_info_user (struct seq_oss_devinfo *dp,void __user *arg) {
@@ -44,6 +45,7 @@ static int snd_seq_oss_midi_info_user(struct seq_oss_devinfo *dp, void __user *astatic int snd_seq_oss_oob_user (struct seq_oss_devinfo *dp,void __user *arg) {unsigned char ev[8 ]; +unsigned char buf[MAX_MIDI_EVENT_BUF ];struct snd_seq_event tmpev;if (copy_from_user (ev, arg,8 ))
@@ -51,7 +53,8 @@ static int snd_seq_oss_oob_user(struct seq_oss_devinfo *dp, void __user *arg)memset (&tmpev,0 ,sizeof (tmpev));snd_seq_oss_fill_addr (dp, &tmpev, dp->addr.client, dp->addr.port); tmpev.time.tick =0 ; -if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev)) { +if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev, + buf,sizeof (buf))) {snd_seq_oss_dispatch (dp, &tmpev,0 ,0 ); }return 0 ;
@@ -175,4 +178,3 @@ snd_seq_oss_ioctl(struct seq_oss_devinfo *dp, unsigned int cmd, unsigned long ca }return 0 ; } -
sound/core/seq/oss/seq_oss_midi.c +34 -3
@@ -38,6 +38,7 @@ struct seq_oss_midi {struct seq_oss_devinfo *devinfo;/* assigned OSSseq device */ snd_use_lock_t use_lock;struct mutex open_mutex; +struct mutex coder_mutex;/* protects coder buffer handoff */ }; DEFINE_FREE(seq_oss_midi, struct seq_oss_midi *, if (!IS_ERR_OR_NULL(_T)) snd_use_lock_free(&(_T)->use_lock))
@@ -57,6 +58,8 @@ static struct seq_oss_midi *get_mdev(int dev);static struct seq_oss_midi *get_mididev (struct seq_oss_devinfo *dp,int dev);static int send_synth_event (struct seq_oss_devinfo *dp,struct snd_seq_event *ev,int dev);static int send_midi_event (struct seq_oss_devinfo *dp,struct snd_seq_event *ev,struct seq_oss_midi *mdev); +static int copy_midi_sysex (struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen);/* * look up the existing ports
@@ -164,6 +167,7 @@ snd_seq_oss_midi_check_new_port(struct snd_seq_port_info *pinfo) mdev->opened =0 ;snd_use_lock_init (&mdev->use_lock);mutex_init (&mdev->open_mutex); +mutex_init (&mdev->coder_mutex);/* copy and truncate the name of synth device */ strscpy (mdev->name, pinfo->name,sizeof (mdev->name));
@@ -592,19 +596,46 @@ send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq* return 0 : enqueued * non-zero : invalid - ignored */ +static int copy_midi_sysex (struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen) +{ +unsigned int len = ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK ; + +if (!buf || len > buflen) +return -EINVAL ; + +memcpy (buf, ev->data.ext.ptr, len); + ev->data.ext.ptr = buf; + ev->data.ext.len = len; +return 0 ; +} +int -snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c,struct snd_seq_event *ev) +snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, +struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen) { struct seq_oss_midi *mdev __free(seq_oss_midi) =get_mididev (dp, dev); +int rc = -EINVAL ;if (!mdev)return -ENODEV ; + +mutex_lock (&mdev->coder_mutex);if (snd_midi_event_encode_byte (mdev->coder, c, ev)) { +if (snd_seq_ev_is_variable (ev)) { + rc =copy_midi_sysex (ev, buf, buflen); +if (rc <0 ) + goto unlock; + }snd_seq_oss_fill_addr (dp, ev, mdev->client, mdev->port); -return 0 ; + rc =0 ; } -return -EINVAL ; + +unlock: +mutex_unlock (&mdev->coder_mutex); +return rc; }/*
sound/core/seq/oss/seq_oss_midi.h +2 -1
@@ -26,7 +26,8 @@ void snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode);int snd_seq_oss_midi_close (struct seq_oss_devinfo *dp,int dev);void snd_seq_oss_midi_reset (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, -struct snd_seq_event *ev); +struct snd_seq_event *ev, +unsigned char *buf,unsigned int buflen);int snd_seq_oss_midi_input (struct snd_seq_event *ev,int direct,void *private);int snd_seq_oss_midi_filemode (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_make_info (struct seq_oss_devinfo *dp,int dev,struct midi_info *inf);
sound/core/seq/oss/seq_oss_rw.c +3 -1
@@ -11,6 +11,7 @@#include "seq_oss_readq.h" #include "seq_oss_writeq.h" #include "seq_oss_synth.h" +#include <sound/seq_midi_event.h> #include <sound/seq_oss_legacy.h> #include "seq_oss_event.h" #include "seq_oss_timer.h"
@@ -153,6 +154,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) {int rc =0 ;struct snd_seq_event event; +unsigned char buf[MAX_MIDI_EVENT_BUF ];/* if this is a timing event, process the current time */ if (snd_seq_oss_process_timer_event (dp->timer, rec))
@@ -164,7 +166,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) event.type =SNDRV_SEQ_EVENT_NOTEOFF ;snd_seq_oss_fill_addr (dp, &event, dp->addr.client, dp->addr.port); -if (snd_seq_oss_process_event (dp, rec, &event)) +if (snd_seq_oss_process_event (dp, rec, &event, buf,sizeof (buf)))return 0 ;/* invalid event - no need to insert queue */ event.time.tick =snd_seq_oss_timer_cur_tick (dp->timer);
Signature · 1 line
2.43.0
Quoted 67 lines · Zhang Cen
The OSS sequencer MIDI output path feeds bytes into an
snd_midi_eventcoder fromsnd_seq_oss_midi_putc(). When a SysEx message completes, the coder builds a variable-length sequencer event whose data.ext.ptr still points at the coder-owned buffer.
snd_seq_oss_midi_putc()returns after dropping the MIDI device use reference, but the caller still owns the freshly built event and may enqueue it afterwards. A concurrent port-removal notification can therefore runsnd_seq_oss_midi_check_exit_port()and free the coder buffer beforesnd_seq_event_dup()copies the variable payload.The dangerous interleaving is:
write path:
snd_seq_oss_midi_putc()completes a SysEx event whose data.ext.ptr points atmdev->coderstorage. It then returns and drops the mdev use reference.insert_queue()later enqueues the variable-length event andsnd_seq_event_dup()copies from event.data.ext.ptr.teardown path:
aPORT_EXITnotification reachessnd_seq_oss_midi_check_exit_port(), removes the MIDI device, and freesmdev->coderbefore the queued event is duplicated.Serialize access to the MIDI coder and copy completed SysEx payloads into caller-owned scratch storage before returning to the enqueue or direct-dispatch side. The copied payload remains valid until the sequencer core has synchronously duplicated or delivered the event, so teardown can no longer free the source buffer from under
snd_seq_event_dup().KASAN report as below:
KASAN slab-use-after-free in
snd_seq_event_dup+0x40c/0x470RIP: 0033:0x7f2cb66a6340 Read of size 6 Call trace: dump_stack_lvl+0x73/0xb0 (?:?) print_report+0xd1/0x650 (?:?) srso_alias_return_thunk+0x5/0xfbef5 (?:?) __virt_addr_valid+0x1a7/0x340 (?:?) kasan_complete_mode_report_info+0x64/0x200 (?:?) kasan_report+0xf7/0x130 (?:?) snd_seq_event_dup+0x40c/0x470 (?:?) kasan_check_range+0x10c/0x1c0 (?:?) __asan_memcpy+0x27/0x70 (?:?) snd_seq_event_dup+0x9/0x470 (?:?) snd_seq_client_enqueue_event+0x139/0x240 (?:?) _raw_spin_unlock_irqrestore+0x4b/0x60 (?:?) snd_seq_kernel_client_enqueue+0x102/0x120 (?:?) snd_seq_oss_write+0x416/0x4e0 (?:?) apparmor_file_permission+0x20/0x30 (?:?) odev_write+0x3b/0x60 (?:?) vfs_write+0x1ce/0x850 (?:?) lock_release+0xc8/0x2a0 (?:?) __kasan_check_write+0x18/0x20 (?:?) __mutex_unlock_slowpath+0x129/0x510 (?:?) ksys_write+0xe1/0x180 (?:?) mutex_unlock+0x16/0x20 (?:?) odev_ioctl+0x65/0xc0 (?:?) __x64_sys_write+0x46/0x60 (?:?) x64_sys_call+0x7d/0x20d0 (?:?) do_syscall_64+0xc1/0x360 (arch/x86/entry/syscall_64.c:87) entry_SYSCALL_64_after_hwframe+0x77/0x7f (?:?)
Quoted 2 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Zhang Cen <[email protected]>
Thanks for the report. I guess the analysis about UAF is right, but the fix looks too much. There is a refcounting of the midi device object, and we can just increase and decrease the refcount between snd_seq_oss_process_event() and snd_seq_oss_dispatch() & co.
Below is a totally untested fix patch. Could you check whether this works for your test case?
Takashi
-- 8< --
sound/core/seq/oss/seq_oss_event.c +4 -2
@@ -39,8 +39,10 @@ static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct s*/ int -snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev) +snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev,snd_use_lock_t **lockp) { + *lockp =NULL ;switch (q->s.code) {case SEQ_EXTENDED :return extended_event (dp, q, ev);
@@ -69,7 +71,7 @@ snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct sndif (snd_seq_oss_midi_open (dp, q->s.dev,SNDRV_SEQ_OSS_FILE_WRITE ))break ;if (snd_seq_oss_midi_filemode (dp, q->s.dev) &SNDRV_SEQ_OSS_FILE_WRITE ) -return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, ev); +return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, ev, lockp);break ;case SEQ_ECHO :
sound/core/seq/oss/seq_oss_event.h +2 -1
@@ -91,7 +91,8 @@ union evrec {#define ev_is_long (ev) ((ev)->s.code >= 128)#define ev_length (ev) ((ev)->s.code >= 128 ? LONG_EVENT_SIZE : SHORT_EVENT_SIZE) -int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev); +int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev,snd_use_lock_t **lockp);int snd_seq_oss_process_timer_event (struct seq_oss_timer *rec,union evrec *q);int snd_seq_oss_event_input (struct snd_seq_event *ev,int direct,void *private_data,int atomic,int hop);
sound/core/seq/oss/seq_oss_ioctl.c +4 -1
@@ -45,14 +45,17 @@ static int snd_seq_oss_oob_user(struct seq_oss_devinfo *dp, void __user *arg) {unsigned char ev[8 ];struct snd_seq_event tmpev; +snd_use_lock_t *lock =NULL ;if (copy_from_user (ev, arg,8 ))return -EFAULT ;memset (&tmpev,0 ,sizeof (tmpev));snd_seq_oss_fill_addr (dp, &tmpev, dp->addr.client, dp->addr.port); tmpev.time.tick =0 ; -if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev)) { +if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev, &lock)) {snd_seq_oss_dispatch (dp, &tmpev,0 ,0 ); +if (lock) +snd_use_lock_free (lock); }return 0 ; }
sound/core/seq/oss/seq_oss_midi.c +5 -1
@@ -593,7 +593,8 @@ send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq* non-zero : invalid - ignored */ int -snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c,struct snd_seq_event *ev) +snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, +struct snd_seq_event *ev,snd_use_lock_t **lockp) { struct seq_oss_midi *mdev __free(seq_oss_midi) =get_mididev (dp, dev);
@@ -602,6 +603,9 @@ snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, strureturn -ENODEV ;if (snd_midi_event_encode_byte (mdev->coder, c, ev)) {snd_seq_oss_fill_addr (dp, ev, mdev->client, mdev->port); +/* the caller must release this later */ + *lockp = &mdev->use_lock; +snd_use_lock_use (*lockp);return 0 ; }return -EINVAL ;
sound/core/seq/oss/seq_oss_midi.h +1 -1
@@ -26,7 +26,7 @@ void snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode);int snd_seq_oss_midi_close (struct seq_oss_devinfo *dp,int dev);void snd_seq_oss_midi_reset (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, -struct snd_seq_event *ev); +struct snd_seq_event *ev,snd_use_lock_t **lockp);int snd_seq_oss_midi_input (struct snd_seq_event *ev,int direct,void *private);int snd_seq_oss_midi_filemode (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_make_info (struct seq_oss_devinfo *dp,int dev,struct midi_info *inf);
sound/core/seq/oss/seq_oss_rw.c +4 -1
@@ -153,6 +153,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) {int rc =0 ;struct snd_seq_event event; +snd_use_lock_t *lock =NULL ;/* if this is a timing event, process the current time */ if (snd_seq_oss_process_timer_event (dp->timer, rec))
@@ -164,7 +165,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) event.type =SNDRV_SEQ_EVENT_NOTEOFF ;snd_seq_oss_fill_addr (dp, &event, dp->addr.client, dp->addr.port); -if (snd_seq_oss_process_event (dp, rec, &event)) +if (snd_seq_oss_process_event (dp, rec, &event, &lock))return 0 ;/* invalid event - no need to insert queue */ event.time.tick =snd_seq_oss_timer_cur_tick (dp->timer);
@@ -173,6 +174,8 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)else rc =snd_seq_kernel_client_enqueue (dp->cseq, &event, opt, !is_nonblock_mode (dp->file_mode)); +if (lock) +snd_use_lock_free (lock);return rc; }
Cen Zhang
Tested-byHi Takashi,
Thanks for the review and the patch.
I tested this approach with the same reproducer I used for the report, and it fixes the issue here.
Would you like me to send a v2 based on this approach, or would you prefer to take your patch directly with my Tested-by?
Thanks,
Zhang Cen
Quoted 204 lines · Takashi Iwai <[email protected]> 于2026年5月26日周二 21:36写道:
Quoted 67 lines · Zhang Cen
The OSS sequencer MIDI output path feeds bytes into an
snd_midi_eventcoder fromsnd_seq_oss_midi_putc(). When a SysEx message completes, the coder builds a variable-length sequencer event whose data.ext.ptr still points at the coder-owned buffer.
snd_seq_oss_midi_putc()returns after dropping the MIDI device use reference, but the caller still owns the freshly built event and may enqueue it afterwards. A concurrent port-removal notification can therefore runsnd_seq_oss_midi_check_exit_port()and free the coder buffer beforesnd_seq_event_dup()copies the variable payload.The dangerous interleaving is:
write path:
snd_seq_oss_midi_putc()completes a SysEx event whose data.ext.ptr points atmdev->coderstorage. It then returns and drops the mdev use reference.insert_queue()later enqueues the variable-length event andsnd_seq_event_dup()copies from event.data.ext.ptr.teardown path:
aPORT_EXITnotification reachessnd_seq_oss_midi_check_exit_port(), removes the MIDI device, and freesmdev->coderbefore the queued event is duplicated.Serialize access to the MIDI coder and copy completed SysEx payloads into caller-owned scratch storage before returning to the enqueue or direct-dispatch side. The copied payload remains valid until the sequencer core has synchronously duplicated or delivered the event, so teardown can no longer free the source buffer from under
snd_seq_event_dup().KASAN report as below:
KASAN slab-use-after-free in
snd_seq_event_dup+0x40c/0x470RIP: 0033:0x7f2cb66a6340 Read of size 6 Call trace: dump_stack_lvl+0x73/0xb0 (?:?) print_report+0xd1/0x650 (?:?) srso_alias_return_thunk+0x5/0xfbef5 (?:?) __virt_addr_valid+0x1a7/0x340 (?:?) kasan_complete_mode_report_info+0x64/0x200 (?:?) kasan_report+0xf7/0x130 (?:?) snd_seq_event_dup+0x40c/0x470 (?:?) kasan_check_range+0x10c/0x1c0 (?:?) __asan_memcpy+0x27/0x70 (?:?) snd_seq_event_dup+0x9/0x470 (?:?) snd_seq_client_enqueue_event+0x139/0x240 (?:?) _raw_spin_unlock_irqrestore+0x4b/0x60 (?:?) snd_seq_kernel_client_enqueue+0x102/0x120 (?:?) snd_seq_oss_write+0x416/0x4e0 (?:?) apparmor_file_permission+0x20/0x30 (?:?) odev_write+0x3b/0x60 (?:?) vfs_write+0x1ce/0x850 (?:?) lock_release+0xc8/0x2a0 (?:?) __kasan_check_write+0x18/0x20 (?:?) __mutex_unlock_slowpath+0x129/0x510 (?:?) ksys_write+0xe1/0x180 (?:?) mutex_unlock+0x16/0x20 (?:?) odev_ioctl+0x65/0xc0 (?:?) __x64_sys_write+0x46/0x60 (?:?) x64_sys_call+0x7d/0x20d0 (?:?) do_syscall_64+0xc1/0x360 (arch/x86/entry/syscall_64.c:87) entry_SYSCALL_64_after_hwframe+0x77/0x7f (?:?)Quoted 2 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Zhang Cen <[email protected]>Thanks for the report. I guess the analysis about UAF is right, but the fix looks too much. There is a refcounting of the midi device object, and we can just increase and decrease the refcount between
snd_seq_oss_process_event()andsnd_seq_oss_dispatch()& co.Below is a totally untested fix patch. Could you check whether this works for your test case?
Takashi
-- 8< --
sound/core/seq/oss/seq_oss_event.c +4
-2@@ -39,8 +39,10 @@ static int set_echo_event(struct seq_oss_devinfo *dp, union evrec *rec, struct s*/ int -snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev) +snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev,snd_use_lock_t **lockp) { + *lockp =NULL ;switch (q->s.code) {case SEQ_EXTENDED :return extended_event (dp, q, ev);@@ -69,7 +71,7 @@ snd_seq_oss_process_event(struct seq_oss_devinfo *dp, union evrec *q, struct sndif (snd_seq_oss_midi_open (dp, q->s.dev,SNDRV_SEQ_OSS_FILE_WRITE ))break ;if (snd_seq_oss_midi_filemode (dp, q->s.dev) &SNDRV_SEQ_OSS_FILE_WRITE ) -return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, ev); +return snd_seq_oss_midi_putc (dp, q->s.dev, q->s.parm1, ev, lockp);break ;case SEQ_ECHO :sound/core/seq/oss/seq_oss_event.h +2
-1@@ -91,7 +91,8 @@ union evrec {#define ev_is_long (ev) ((ev)->s.code >= 128)#define ev_length (ev) ((ev)->s.code >= 128 ? LONG_EVENT_SIZE : SHORT_EVENT_SIZE) -int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q,struct snd_seq_event *ev); +int snd_seq_oss_process_event (struct seq_oss_devinfo *dp,union evrec *q, +struct snd_seq_event *ev,snd_use_lock_t **lockp);int snd_seq_oss_process_timer_event (struct seq_oss_timer *rec,union evrec *q);int snd_seq_oss_event_input (struct snd_seq_event *ev,int direct,void *private_data,int atomic,int hop);sound/core/seq/oss/seq_oss_ioctl.c +4
-1@@ -45,14 +45,17 @@ static int snd_seq_oss_oob_user(struct seq_oss_devinfo *dp, void __user *arg) {unsigned char ev[8 ];struct snd_seq_event tmpev; +snd_use_lock_t *lock =NULL ;if (copy_from_user (ev, arg,8 ))return -EFAULT ;memset (&tmpev,0 ,sizeof (tmpev));snd_seq_oss_fill_addr (dp, &tmpev, dp->addr.client, dp->addr.port); tmpev.time.tick =0 ; -if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev)) { +if (!snd_seq_oss_process_event (dp, (union evrec *)ev, &tmpev, &lock)) {snd_seq_oss_dispatch (dp, &tmpev,0 ,0 ); +if (lock) +snd_use_lock_free (lock); }return 0 ; }sound/core/seq/oss/seq_oss_midi.c +5
-1@@ -593,7 +593,8 @@ send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq* non-zero : invalid - ignored */ int -snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c,struct snd_seq_event *ev) +snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, +struct snd_seq_event *ev,snd_use_lock_t **lockp) { struct seq_oss_midi *mdev __free(seq_oss_midi) =get_mididev (dp, dev);@@ -602,6 +603,9 @@ snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, strureturn -ENODEV ;if (snd_midi_event_encode_byte (mdev->coder, c, ev)) {snd_seq_oss_fill_addr (dp, ev, mdev->client, mdev->port); +/* the caller must release this later */ + *lockp = &mdev->use_lock; +snd_use_lock_use (*lockp);return 0 ; }return -EINVAL ;sound/core/seq/oss/seq_oss_midi.h +1
-1@@ -26,7 +26,7 @@ void snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode);int snd_seq_oss_midi_close (struct seq_oss_devinfo *dp,int dev);void snd_seq_oss_midi_reset (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_putc (struct seq_oss_devinfo *dp,int dev,unsigned char c, -struct snd_seq_event *ev); +struct snd_seq_event *ev,snd_use_lock_t **lockp);int snd_seq_oss_midi_input (struct snd_seq_event *ev,int direct,void *private);int snd_seq_oss_midi_filemode (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_midi_make_info (struct seq_oss_devinfo *dp,int dev,struct midi_info *inf);sound/core/seq/oss/seq_oss_rw.c +4
-1@@ -153,6 +153,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) {int rc =0 ;struct snd_seq_event event; +snd_use_lock_t *lock =NULL ;/* if this is a timing event, process the current time */ if (snd_seq_oss_process_timer_event (dp->timer, rec))@@ -164,7 +165,7 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt) event.type =SNDRV_SEQ_EVENT_NOTEOFF ;snd_seq_oss_fill_addr (dp, &event, dp->addr.client, dp->addr.port); -if (snd_seq_oss_process_event (dp, rec, &event)) +if (snd_seq_oss_process_event (dp, rec, &event, &lock))return 0 ;/* invalid event - no need to insert queue */ event.time.tick =snd_seq_oss_timer_cur_tick (dp->timer);@@ -173,6 +174,8 @@ insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)else rc =snd_seq_kernel_client_enqueue (dp->cseq, &event, opt, !is_nonblock_mode (dp->file_mode)); +if (lock) +snd_use_lock_free (lock);return rc; }
Quoted 5 lines · Cen Zhang
Hi Takashi,
Thanks for the review and the patch.
Quoted 7 lines
I tested this approach with the same reproducer I used for the report, and it fixes the issue here.
Would you like me to send a v2 based on this approach, or would you prefer to take your patch directly with my Tested-by?
Tested-by Zhang Cen <[email protected]>
Thanks for quick testing! I'm going to submit a proper patch, then.
Takashi