Skip to content

Commit b769f49

Browse files
Dan Rosenbergtiwai
authored andcommitted
sound/oss: remove offset from load_patch callbacks
Was: [PATCH] sound/oss/midi_synth: prevent underflow, use of uninitialized value, and signedness issue The offset passed to midi_synth_load_patch() can be essentially arbitrary. If it's greater than the header length, this will result in a copy_from_user(dst, src, negative_val). While this will just return -EFAULT on x86, on other architectures this may cause memory corruption. Additionally, the length field of the sysex_info structure may not be initialized prior to its use. Finally, a signed comparison may result in an unintentionally large loop. On suggestion by Takashi Iwai, version two removes the offset argument from the load_patch callbacks entirely, which also resolves similar issues in opl3. Compile tested only. v3 adjusts comments and hopefully gets copy offsets right. Signed-off-by: Dan Rosenberg <drosenberg@vsecurity.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent ce24f58 commit b769f49

5 files changed

Lines changed: 18 additions & 26 deletions

File tree

‎sound/oss/dev_table.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ struct synth_operations
271271
void (*reset) (int dev);
272272
void (*hw_control) (int dev, unsigned char *event);
273273
int (*load_patch) (int dev, int format, const char __user *addr,
274-
int offs, int count, int pmgr_flag);
274+
int count, int pmgr_flag);
275275
void (*aftertouch) (int dev, int voice, int pressure);
276276
void (*controller) (int dev, int voice, int ctrl_num, int value);
277277
void (*panning) (int dev, int voice, int value);

‎sound/oss/midi_synth.c‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ EXPORT_SYMBOL(midi_synth_hw_control);
476476

477477
int
478478
midi_synth_load_patch(int dev, int format, const char __user *addr,
479-
int offs, int count, int pmgr_flag)
479+
int count, int pmgr_flag)
480480
{
481481
int orig_dev = synth_devs[dev]->midi_dev;
482482

@@ -491,33 +491,29 @@ midi_synth_load_patch(int dev, int format, const char __user *addr,
491491
if (!prefix_cmd(orig_dev, 0xf0))
492492
return 0;
493493

494+
/* Invalid patch format */
494495
if (format != SYSEX_PATCH)
495-
{
496-
/* printk("MIDI Error: Invalid patch format (key) 0x%x\n", format);*/
497496
return -EINVAL;
498-
}
497+
498+
/* Patch header too short */
499499
if (count < hdr_size)
500-
{
501-
/* printk("MIDI Error: Patch header too short\n");*/
502500
return -EINVAL;
503-
}
501+
504502
count -= hdr_size;
505503

506504
/*
507-
* Copy the header from user space but ignore the first bytes which have
508-
* been transferred already.
505+
* Copy the header from user space
509506
*/
510507

511-
if(copy_from_user(&((char *) &sysex)[offs], &(addr)[offs], hdr_size - offs))
508+
if (copy_from_user(&sysex, addr, hdr_size))
512509
return -EFAULT;
513-
514-
if (count < sysex.len)
515-
{
516-
/* printk(KERN_WARNING "MIDI Warning: Sysex record too short (%d<%d)\n", count, (int) sysex.len);*/
510+
511+
/* Sysex record too short */
512+
if ((unsigned)count < (unsigned)sysex.len)
517513
sysex.len = count;
518-
}
519-
left = sysex.len;
520-
src_offs = 0;
514+
515+
left = sysex.len;
516+
src_offs = 0;
521517

522518
for (i = 0; i < left && !signal_pending(current); i++)
523519
{

‎sound/oss/midi_synth.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int midi_synth_open (int dev, int mode);
88
void midi_synth_close (int dev);
99
void midi_synth_hw_control (int dev, unsigned char *event);
1010
int midi_synth_load_patch (int dev, int format, const char __user * addr,
11-
int offs, int count, int pmgr_flag);
11+
int count, int pmgr_flag);
1212
void midi_synth_panning (int dev, int channel, int pressure);
1313
void midi_synth_aftertouch (int dev, int channel, int pressure);
1414
void midi_synth_controller (int dev, int channel, int ctrl_num, int value);

‎sound/oss/opl3.c‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ static void opl3_hw_control(int dev, unsigned char *event)
820820
}
821821

822822
static int opl3_load_patch(int dev, int format, const char __user *addr,
823-
int offs, int count, int pmgr_flag)
823+
int count, int pmgr_flag)
824824
{
825825
struct sbi_instrument ins;
826826

@@ -830,11 +830,7 @@ static int opl3_load_patch(int dev, int format, const char __user *addr,
830830
return -EINVAL;
831831
}
832832

833-
/*
834-
* What the fuck is going on here? We leave junk in the beginning
835-
* of ins and then check the field pretty close to that beginning?
836-
*/
837-
if(copy_from_user(&((char *) &ins)[offs], addr + offs, sizeof(ins) - offs))
833+
if (copy_from_user(&ins, addr, sizeof(ins)))
838834
return -EFAULT;
839835

840836
if (ins.channel < 0 || ins.channel >= SBFM_MAXINSTR)

‎sound/oss/sequencer.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ int sequencer_write(int dev, struct file *file, const char __user *buf, int coun
241241
return -ENXIO;
242242

243243
fmt = (*(short *) &event_rec[0]) & 0xffff;
244-
err = synth_devs[dev]->load_patch(dev, fmt, buf, p + 4, c, 0);
244+
err = synth_devs[dev]->load_patch(dev, fmt, buf + p, c, 0);
245245
if (err < 0)
246246
return err;
247247

0 commit comments

Comments
 (0)