ALSA: control: Add verification for kctl accesses
PATCH 1/1 ALSA: control: Add verification for kctl accesses
The current implementation of ALSA control API fully relies on the callbacks of each driver, and there is no verification of the values passed via API. This patch is an attempt to improve the situation slightly by adding the validation code for the values stored via info and get callbacks.
The patch adds a new kconfig, CONFIG_SND_CTL_VALIDATION. It depends on CONFIG_SND_DEBUG and off as default since the validation would require a slight overhead including the additional call of info callback at each get callback invocation.
When this config is enabled, the values stored by each info callback invocation are verified, namely:
- Whether the info type is valid
- Whether the number of enum items is non-zero
- Whether the given info count is within the allowed boundary
Similarly, the values stored at each get callback are verified as well:
- Whether the values are within the given range
- Whether the values are aligned with the given step
- Whether any further changes are seen in the data array over the given info count
The last point helps identifying a possibly invalid data type access, typically a case where the info callback declares the type being SNDRV_CTL_ELEM_TYPE_ENUMERATED while the get/put callbacks store the values in value.integer.value[] array.
When a validation fails, the ALSA core logs an error message including the device and the control ID, and the API call also returns an error. So, with the new validation turned on, the driver behavior difference may be visible on user-space, too -- it's intentional, though, so that we can catch an error more clearly.
The patch also introduces a new ctl access type, SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK. A driver may pass this flag with other access bits to indicate that the ctl element won't be verified. It's useful when a driver code is specially written to access the data greater than info->count size by some reason. For example, this flag is actually set now in HD-audio HDMI codec driver which needs to clear the data array in the case of the disconnected monitor.
Also, the PCM channel-map helper code is slightly modified to avoid the false-positive hit by this validation code, too.
---
This is a resend of the previous RFC patch for merging for 5.6.
include/sound/control.h | 10 ++ sound/core/Kconfig | 9 ++ sound/core/control.c | 283 +++++++++++++++++++++++++++++++++++++++------ sound/core/pcm_lib.c | 2 +- sound/pci/hda/patch_hdmi.c | 3 +- 5 files changed, 268 insertions(+), 39 deletions(-)
include/sound/control.h +10 -0
@@ -22,6 +22,16 @@ typedef int (snd_kcontrol_tlv_rw_t)(struct snd_kcontrol *kcontrol,unsigned int size, unsigned int __user *tlv); +/* internal flag for skipping validations */ +#ifdef CONFIG_SND_CTL_VALIDATION +#define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK (1 << 27) +#define snd_ctl_skip_validation (info) \ + ((info)->access & SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK) +#else +#define SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK 0 +#define snd_ctl_skip_validation (info) true +#endif +enum {SNDRV_CTL_TLV_OP_READ =0 ,SNDRV_CTL_TLV_OP_WRITE =1 ,
sound/core/Kconfig +9 -0
@@ -178,6 +178,15 @@ config SND_PCM_XRUN_DEBUG sound clicking when system is loaded, it may help to determine the process or driver which causes the scheduling gaps. +config SND_CTL_VALIDATION + bool "Perform sanity-checks for each control element access" + depends on SND_DEBUG + help + Say Y to enable the additional validation of each control element + access, including sanity-checks like whether the values returned + from the driver are in the proper ranges or the check of the invalid + access at out-of-array areas. + config SND_VMASTER bool
sound/core/control.c +246 -37
343 lines not on this page · raw message
sound/core/pcm_lib.c +1 -1
@@ -2341,7 +2341,7 @@ static int pcm_chmap_ctl_get(struct snd_kcontrol *kcontrol,if (!substream)return -ENODEV ;memset (ucontrol->value.integer.value,0 , -sizeof (ucontrol->value.integer.value)); +sizeof (long ) * info->max_channels);if (!substream->runtime)return 0 ;/* no channels set */ for (map = info->chmap; map->channels; map++) {
sound/pci/hda/patch_hdmi.c +2 -1
@@ -372,7 +372,8 @@ static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol, }static const struct snd_kcontrol_new eld_bytes_ctl = { - .access =SNDRV_CTL_ELEM_ACCESS_READ |SNDRV_CTL_ELEM_ACCESS_VOLATILE , + .access =SNDRV_CTL_ELEM_ACCESS_READ |SNDRV_CTL_ELEM_ACCESS_VOLATILE | +SNDRV_CTL_ELEM_ACCESS_SKIP_CHECK , .iface =SNDRV_CTL_ELEM_IFACE_PCM , .name ="ELD" , .info = hdmi_eld_ctl_info,
Signature · 6 lines
2.16.4 _______________________________________________ Alsa-devel mailing list [email protected] https://mailman.alsa-project.org/mailman/listinfo/alsa-devel