ALSA: Hardening for potential Spectre v1
cover ALSA: Hardening for potential Spectre v1
Hi,
this is a patchset to cover the potential issues wrt Spectre v1 that were spotted out recently by Smatch:
https://marc.info/?l=linux-kernel&m=152411496503418&w=2
Although all reports for sound/* seem like no serious issues, it's better to cover potential usages than a too late sorry.
The first patch is basically irrelevant for Spectre but a preliminary fix for the following fix. Most of the rest patches are straightforward, but the second one, the fix for OSS emulation, became a fair amount of code refactoring in the end.
An open question is whether we should put Cc-to-stable to these fixes. Maybe yes, although we've never proven that the problem would actually exist, the issue is still "hot", so people may want to see them more-or-less covered...
thanks,
Takashi
===
Takashi Iwai (8):
ALSA: seq: oss: Fix unbalanced use lock for synth MIDI device
ALSA: seq: oss: Hardening for potential Spectre v1
ALSA: control: Hardening for potential Spectre v1
ALSA: hda: Hardening for potential Spectre v1
ALSA: opl3: Hardening for potential Spectre v1
ALSA: asihpi: Hardening for potential Spectre v1
ALSA: hdspm: Hardening for potential Spectre v1
ALSA: rme9652: Hardening for potential Spectre v1
include/sound/control.h | 7 +++- sound/core/seq/oss/seq_oss_event.c | 15 ++++--- sound/core/seq/oss/seq_oss_midi.c | 2 + sound/core/seq/oss/seq_oss_synth.c | 85 ++++++++++++++++++++++---------------- sound/core/seq/oss/seq_oss_synth.h | 3 +- sound/drivers/opl3/opl3_synth.c | 7 +++- sound/pci/asihpi/hpimsginit.c | 13 ++++-- sound/pci/asihpi/hpioctl.c | 4 +- sound/pci/hda/hda_hwdep.c | 12 +++++- sound/pci/rme9652/hdspm.c | 24 ++++++----- sound/pci/rme9652/rme9652.c | 6 ++- 11 files changed, 113 insertions(+), 65 deletions(-)
Signature · 1 line
2.16.3
PATCH 1/8 ALSA: seq: oss: Fix unbalanced use lock for synth MIDI device
When get_synthdev() is called for a MIDI device, it returns the fixed midi_synth_dev without the use refcounting. OTOH, the caller is supposed to unreference unconditionally after the usage, so this would lead to unbalanced refcount.
This patch corrects the behavior and keep up the refcount balance also for the MIDI synth device.
--- sound/core/seq/oss/seq_oss_synth.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
sound/core/seq/oss/seq_oss_synth.c +8 -4
@@ -363,10 +363,14 @@ get_synthdev(struct seq_oss_devinfo *dp, int dev)return NULL ;if (! dp->synths[dev].opened)return NULL ; -if (dp->synths[dev].is_midi) -return &midi_synth_dev; -if ((rec =get_sdev (dev)) ==NULL ) -return NULL ; +if (dp->synths[dev].is_midi) { + rec = &midi_synth_dev; +snd_use_lock_use (&rec->use_lock); + }else { + rec =get_sdev (dev); +if (!rec) +return NULL ; + }if (! rec->opened) {snd_use_lock_free (&rec->use_lock);return NULL ;
Signature · 1 line
2.16.3
PATCH 2/8 ALSA: seq: oss: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, a few places may expand the array directly from the user-space value with speculation, where there are a significant amount of references to either info->ch[] or dp->synths[] array:
sound/core/seq/oss/seq_oss_event.c:315 note_on_event() warn: potential spectre issue 'info->ch' (local cap) sound/core/seq/oss/seq_oss_event.c:362 note_off_event() warn: potential spectre issue 'info->ch' (local cap) sound/core/seq/oss/seq_oss_synth.c:470 snd_seq_oss_synth_load_patch() warn: potential spectre issue 'dp->synths' (local cap) sound/core/seq/oss/seq_oss_event.c:293 note_on_event() warn: potential spectre issue 'dp->synths' sound/core/seq/oss/seq_oss_event.c:353 note_off_event() warn: potential spectre issue 'dp->synths' sound/core/seq/oss/seq_oss_synth.c:506 snd_seq_oss_synth_sysex() warn: potential spectre issue 'dp->synths' sound/core/seq/oss/seq_oss_synth.c:580 snd_seq_oss_synth_ioctl() warn: potential spectre issue 'dp->synths'
Although all these seem doing only the first load without further reference, we may want to stay in a safer side, so hardening with array_index_nospec() would still make sense.
We may put array_index_nospec() at each place, but here we take a different approach:
- For dp->synths[], change the helpers to retrieve seq_oss_synthinfo pointer directly instead of the array expansion at each place
- For info->ch[], harden in a normal way, as there are only a couple of places
As a result, the existing helper, snd_seq_oss_synth_is_valid() is replaced with snd_seq_oss_synth_info(). Also, we cover MIDI device where a similar array expansion is done, too, although it wasn't reported by Smatch.
--- sound/core/seq/oss/seq_oss_event.c | 15 +++++--- sound/core/seq/oss/seq_oss_midi.c | 2 + sound/core/seq/oss/seq_oss_synth.c | 75 +++++++++++++++++++++----------------- sound/core/seq/oss/seq_oss_synth.h | 3 +- 4 files changed, 55 insertions(+), 40 deletions(-)
sound/core/seq/oss/seq_oss_event.c +9 -6
@@ -26,6 +26,7 @@#include <sound/seq_oss_legacy.h> #include "seq_oss_readq.h" #include "seq_oss_writeq.h" +#include <linux/nospec.h> /*
@@ -287,10 +288,10 @@ note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, st {struct seq_oss_synthinfo *info; -if (!snd_seq_oss_synth_is_valid (dp, dev)) + info =snd_seq_oss_synth_info (dp, dev); +if (!info)return -ENXIO ; - info = &dp->synths[dev];switch (info->arg.event_passing) {case SNDRV_SEQ_OSS_PROCESS_EVENTS :if (! info->ch || ch <0 || ch >= info->nr_voices) {
@@ -298,6 +299,7 @@ note_on_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, streturn set_note_event (dp, dev,SNDRV_SEQ_EVENT_NOTEON , ch, note, vel, ev); } + ch =array_index_nospec (ch, info->nr_voices);if (note ==255 && info->ch[ch].note >=0 ) {/* volume control */ int type;
@@ -347,10 +349,10 @@ note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, s {struct seq_oss_synthinfo *info; -if (!snd_seq_oss_synth_is_valid (dp, dev)) + info =snd_seq_oss_synth_info (dp, dev); +if (!info)return -ENXIO ; - info = &dp->synths[dev];switch (info->arg.event_passing) {case SNDRV_SEQ_OSS_PROCESS_EVENTS :if (! info->ch || ch <0 || ch >= info->nr_voices) {
@@ -358,6 +360,7 @@ note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, sreturn set_note_event (dp, dev,SNDRV_SEQ_EVENT_NOTEON , ch, note, vel, ev); } + ch =array_index_nospec (ch, info->nr_voices);if (info->ch[ch].note >=0 ) { note = info->ch[ch].note; info->ch[ch].vel =0 ;
@@ -381,7 +384,7 @@ note_off_event(struct seq_oss_devinfo *dp, int dev, int ch, int note, int vel, sstatic int set_note_event (struct seq_oss_devinfo *dp,int dev,int type,int ch,int note,int vel,struct snd_seq_event *ev) { -if (!snd_seq_oss_synth_is_valid (dp, dev)) +if (!snd_seq_oss_synth_info (dp, dev))return -ENXIO ; ev->type = type;
@@ -399,7 +402,7 @@ set_note_event(struct seq_oss_devinfo *dp, int dev, int type, int ch, int note,static int set_control_event (struct seq_oss_devinfo *dp,int dev,int type,int ch,int param,int val,struct snd_seq_event *ev) { -if (!snd_seq_oss_synth_is_valid (dp, dev)) +if (!snd_seq_oss_synth_info (dp, dev))return -ENXIO ; ev->type = type;
sound/core/seq/oss/seq_oss_midi.c +2 -0
@@ -29,6 +29,7 @@#include "../seq_lock.h" #include <linux/init.h> #include <linux/slab.h> +#include <linux/nospec.h> /*
@@ -315,6 +316,7 @@ get_mididev(struct seq_oss_devinfo *dp, int dev) {if (dev <0 || dev >= dp->max_mididev)return NULL ; + dev =array_index_nospec (dev, dp->max_mididev);return get_mdev (dev); }
sound/core/seq/oss/seq_oss_synth.c +42 -33
171 lines not on this page · raw message
sound/core/seq/oss/seq_oss_synth.h +2 -1
@@ -37,7 +37,8 @@ void snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp);void snd_seq_oss_synth_reset (struct seq_oss_devinfo *dp,int dev);int snd_seq_oss_synth_load_patch (struct seq_oss_devinfo *dp,int dev,int fmt,const char __user *buf,int p,int c); -int snd_seq_oss_synth_is_valid (struct seq_oss_devinfo *dp,int dev); +struct seq_oss_synthinfo *snd_seq_oss_synth_info (struct seq_oss_devinfo *dp, +int dev);int snd_seq_oss_synth_sysex (struct seq_oss_devinfo *dp,int dev,unsigned char *buf,struct snd_seq_event *ev);int snd_seq_oss_synth_addr (struct seq_oss_devinfo *dp,int dev,struct snd_seq_event *ev);
Signature · 1 line
2.16.3
PATCH 3/8 ALSA: control: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, a few places in ALSA control core codes may expand the array directly from the user-space value with speculation:
sound/core/control.c:1003 snd_ctl_elem_lock() warn: potential spectre issue 'kctl->vd' sound/core/control.c:1031 snd_ctl_elem_unlock() warn: potential spectre issue 'kctl->vd' sound/core/control.c:844 snd_ctl_elem_info() warn: potential spectre issue 'kctl->vd' sound/core/control.c:891 snd_ctl_elem_read() warn: potential spectre issue 'kctl->vd' sound/core/control.c:939 snd_ctl_elem_write() warn: potential spectre issue 'kctl->vd'
Although all these seem doing only the first load without further reference, we may want to stay in a safer side, so hardening with array_index_nospec() would still make sense.
In this patch, we put array_index_nospec() to the common snd_ctl_get_ioff*() helpers instead of each caller. They are also referred from some drivers, too, and basically all usages are to calculate the array index from the user-space value.
--- include/sound/control.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
include/sound/control.h +5 -2
@@ -23,6 +23,7 @@*/ #include <linux/wait.h> +#include <linux/nospec.h> #include <sound/asound.h> #define snd_kcontrol_chip (kcontrol) ((kcontrol)->private_data)
@@ -148,12 +149,14 @@ int snd_ctl_get_preferred_subdevice(struct snd_card *card, int type);static inline unsigned int snd_ctl_get_ioffnum (struct snd_kcontrol *kctl,struct snd_ctl_elem_id *id) { -return id->numid - kctl->id.numid; +unsigned int ioff = id->numid - kctl->id.numid; +return array_index_nospec (ioff, kctl->count); }static inline unsigned int snd_ctl_get_ioffidx (struct snd_kcontrol *kctl,struct snd_ctl_elem_id *id) { -return id->index - kctl->id.index; +unsigned int ioff = id->index - kctl->id.index; +return array_index_nospec (ioff, kctl->count); }static inline unsigned int snd_ctl_get_ioff (struct snd_kcontrol *kctl,struct snd_ctl_elem_id *id)
Signature · 1 line
2.16.3
PATCH 4/8 ALSA: hda: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, one place in HD-audio hwdep ioctl codes
may expand the array directly from the user-space value with
speculation:
sound/pci/hda/hda_local.h:467 get_wcaps() warn: potential spectre issue 'codec->wcaps'
As get_wcaps() itself is a fairly frequently called inline function, and there is only one single call with a user-space value, it's better to open-code it locally with array_index_nospec() hardening.
--- sound/pci/hda/hda_hwdep.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
sound/pci/hda/hda_hwdep.c +11 -1
@@ -21,6 +21,7 @@#include <linux/init.h> #include <linux/slab.h> #include <linux/compat.h> +#include <linux/nospec.h> #include <sound/core.h> #include "hda_codec.h" #include "hda_local.h"
@@ -51,7 +52,16 @@ static int get_wcap_ioctl(struct hda_codec *codec,if (get_user (verb, &arg->verb))return -EFAULT ; - res =get_wcaps (codec, verb >>24 ); +/* open-code get_wcaps(verb>>24) with nospec */ + verb >>=24 ; +if (verb < codec->core.start_nid || + verb >= codec->core.start_nid + codec->core.num_nodes) { + res =0 ; + }else { + verb -= codec->core.start_nid; + verb =array_index_nospec (verb, codec->core.num_nodes); + res = codec->wcaps[verb]; + }if (put_user (res, &arg->res))return -EFAULT ;return 0 ;
Signature · 1 line
2.16.3
PATCH 5/8 ALSA: opl3: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, one place in OPL3 driver may expand the
array directly from the user-space value with speculation:
sound/drivers/opl3/opl3_synth.c:476 snd_opl3_set_voice() warn: potential spectre issue 'snd_opl3_regmap'
This patch puts array_index_nospec() for hardening against it.
--- sound/drivers/opl3/opl3_synth.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
sound/drivers/opl3/opl3_synth.c +5 -2
@@ -21,6 +21,7 @@#include <linux/slab.h> #include <linux/export.h> +#include <linux/nospec.h> #include <sound/opl3.h> #include <sound/asound_fm.h>
@@ -448,7 +449,7 @@ static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * v {unsigned short reg_side;unsigned char op_offset; -unsigned char voice_offset; +unsigned char voice_offset, voice_op;unsigned short opl3_reg;unsigned char reg_val;
@@ -473,7 +474,9 @@ static int snd_opl3_set_voice(struct snd_opl3 * opl3, struct snd_dm_fm_voice * v voice_offset = voice->voice -MAX_OPL2_VOICES ; }/* Get register offset of operator */ - op_offset = snd_opl3_regmap[voice_offset][voice->op]; + voice_offset =array_index_nospec (voice_offset,MAX_OPL2_VOICES ); + voice_op =array_index_nospec (voice->op,4 ); + op_offset = snd_opl3_regmap[voice_offset][voice_op]; reg_val =0x00 ;/* Set amplitude modulation (tremolo) effect */
Signature · 1 line
2.16.3
PATCH 6/8 ALSA: asihpi: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, a couple of places in ASIHPI driver may
expand the array directly from the user-space value with speculation:
sound/pci/asihpi/hpimsginit.c:70 hpi_init_response() warn: potential spectre issue 'res_size' (local cap) sound/pci/asihpi/hpioctl.c:189 asihpi_hpi_ioctl() warn: potential spectre issue 'adapters'
This patch puts array_index_nospec() for hardening against them.
--- sound/pci/asihpi/hpimsginit.c | 13 +++++++++---- sound/pci/asihpi/hpioctl.c | 4 +++- 2 files changed, 12 insertions(+), 5 deletions(-)
sound/pci/asihpi/hpimsginit.c +9 -4
@@ -23,6 +23,7 @@#include "hpi_internal.h" #include "hpimsginit.h" +#include <linux/nospec.h> /* The actual message size for each object type */ static u16 msg_size[HPI_OBJ_MAXINDEX +1 ] =HPI_MESSAGE_SIZE_BY_OBJECT ;
@@ -39,10 +40,12 @@ static void hpi_init_message(struct hpi_message *phm, u16 object, {u16 size; -if ((object >0 ) && (object <=HPI_OBJ_MAXINDEX )) +if ((object >0 ) && (object <=HPI_OBJ_MAXINDEX )) { + object =array_index_nospec (object,HPI_OBJ_MAXINDEX +1 ); size = msg_size[object]; -else + }else { size =sizeof (*phm); + }memset (phm,0 , size); phm->size = size;
@@ -66,10 +69,12 @@ void hpi_init_response(struct hpi_response *phr, u16 object, u16 function, {u16 size; -if ((object >0 ) && (object <=HPI_OBJ_MAXINDEX )) +if ((object >0 ) && (object <=HPI_OBJ_MAXINDEX )) { + object =array_index_nospec (object,HPI_OBJ_MAXINDEX +1 ); size = res_size[object]; -else + }else { size =sizeof (*phr); + }memset (phr,0 ,sizeof (*phr)); phr->size = size;
sound/pci/asihpi/hpioctl.c +3 -1
@@ -33,6 +33,7 @@#include <linux/stringify.h> #include <linux/module.h> #include <linux/vmalloc.h> +#include <linux/nospec.h> #ifdef MODULE_FIRMWARE MODULE_FIRMWARE ("asihpi/dsp5000.bin" );
@@ -186,7 +187,8 @@ long asihpi_hpi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)struct hpi_adapter *pa =NULL ;if (hm->h.adapter_index <ARRAY_SIZE (adapters)) - pa = &adapters[hm->h.adapter_index]; + pa = &adapters[array_index_nospec (hm->h.adapter_index, +ARRAY_SIZE (adapters))];if (!pa || !pa->adapter || !pa->adapter->type) { hpi_init_response(&hr->r0, hm->h.object,
Signature · 1 line
2.16.3
PATCH 7/8 ALSA: hdspm: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, a couple of places in HDSP MADI driver
may expand the array directly from the user-space value with
speculation:
sound/pci/rme9652/hdspm.c:5717 snd_hdspm_channel_info() warn: potential spectre issue 'hdspm->channel_map_out' (local cap) sound/pci/rme9652/hdspm.c:5734 snd_hdspm_channel_info() warn: potential spectre issue 'hdspm->channel_map_in' (local cap)
This patch puts array_index_nospec() for hardening against them.
--- sound/pci/rme9652/hdspm.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
sound/pci/rme9652/hdspm.c +14 -10
@@ -137,6 +137,7 @@#include <linux/pci.h> #include <linux/math64.h> #include <linux/io.h> +#include <linux/nospec.h> #include <sound/core.h> #include <sound/control.h>
@@ -5698,40 +5699,43 @@ static int snd_hdspm_channel_info(struct snd_pcm_substream *substream, struct snd_pcm_channel_info *info) {struct hdspm *hdspm =snd_pcm_substream_chip (substream); +unsigned int channel = info->channel;if (substream->stream ==SNDRV_PCM_STREAM_PLAYBACK ) { -if (snd_BUG_ON (info->channel >= hdspm->max_channels_out)) { +if (snd_BUG_ON (channel >= hdspm->max_channels_out)) {dev_info (hdspm->card->dev,"snd_hdspm_channel_info: output channel out of range (%d)\n" , - info->channel); + channel);return -EINVAL ; } -if (hdspm->channel_map_out[info->channel] <0 ) { + channel =array_index_nospec (channel, hdspm->max_channels_out); +if (hdspm->channel_map_out[channel] <0 ) {dev_info (hdspm->card->dev,"snd_hdspm_channel_info: output channel %d mapped out\n" , - info->channel); + channel);return -EINVAL ; } - info->offset = hdspm->channel_map_out[info->channel] * + info->offset = hdspm->channel_map_out[channel] *HDSPM_CHANNEL_BUFFER_BYTES ; }else { -if (snd_BUG_ON (info->channel >= hdspm->max_channels_in)) { +if (snd_BUG_ON (channel >= hdspm->max_channels_in)) {dev_info (hdspm->card->dev,"snd_hdspm_channel_info: input channel out of range (%d)\n" , - info->channel); + channel);return -EINVAL ; } -if (hdspm->channel_map_in[info->channel] <0 ) { + channel =array_index_nospec (channel, hdspm->max_channels_in); +if (hdspm->channel_map_in[channel] <0 ) {dev_info (hdspm->card->dev,"snd_hdspm_channel_info: input channel %d mapped out\n" , - info->channel); + channel);return -EINVAL ; } - info->offset = hdspm->channel_map_in[info->channel] * + info->offset = hdspm->channel_map_in[channel] *HDSPM_CHANNEL_BUFFER_BYTES ; }
Signature · 1 line
2.16.3
PATCH 8/8 ALSA: rme9652: Hardening for potential Spectre v1
In reply to Takashi IwaiAs the recent Smatch warned, one place in RME9652 driver may expand the
array directly from the user-space value with speculation:
sound/pci/rme9652/rme9652.c:2074 snd_rme9652_channel_info() warn: potential spectre issue 'rme9652->channel_map' (local cap)
This patch puts array_index_nospec() for hardening against it.
--- sound/pci/rme9652/rme9652.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
sound/pci/rme9652/rme9652.c +4 -2
@@ -26,6 +26,7 @@#include <linux/pci.h> #include <linux/module.h> #include <linux/io.h> +#include <linux/nospec.h> #include <sound/core.h> #include <sound/control.h>
@@ -2071,9 +2072,10 @@ static int snd_rme9652_channel_info(struct snd_pcm_substream *substream,if (snd_BUG_ON (info->channel >=RME9652_NCHANNELS ))return -EINVAL ; -if ((chn = rme9652->channel_map[info->channel]) <0 ) { + chn = rme9652->channel_map[array_index_nospec (info->channel, +RME9652_NCHANNELS )]; +if (chn <0 )return -EINVAL ; - } info->offset = chn *RME9652_CHANNEL_BUFFER_BYTES ; info->first =0 ;
Signature · 1 line
2.16.3