]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ALSA: asihpi: detect truncated control names
authorPengpeng Hou <pengpeng@iscas.ac.cn>
Sat, 28 Mar 2026 10:28:08 +0000 (18:28 +0800)
committerTakashi Iwai <tiwai@suse.de>
Sat, 28 Mar 2026 13:13:04 +0000 (14:13 +0100)
asihpi_ctl_init() builds mixer control names in the fixed 44-byte
hpi_ctl->name buffer with sprintf().

This is not only a defensive cleanup. The current in-tree name tables and
format strings can already exceed 44 bytes. For example,

  "Bitstream 0 Internal 0 Monitor Playback Volume"

is 46 characters before the trailing NUL, so the current sprintf() call
writes past the end of hpi_ctl->name.

The generated control name is used as the ALSA control element key, so
blindly truncating it is not sufficient. Switch the formatting to
snprintf() and emit an error if truncation happens, showing the
truncated name while still keeping the write bounded to hpi_ctl->name.

Signed-off-by: Pengpeng Hou <pengpeng@iscas.ac.cn>
Link: https://patch.msgid.link/20260328102808.33969-1-pengpeng@iscas.ac.cn
Signed-off-by: Takashi Iwai <tiwai@suse.de>
sound/pci/asihpi/asihpi.c

index 3a64d05628030e850e7cc04f24bfa83a221b0c21..b1c7ed7f1604e8aa9d88ce1aa585d4d1fc30ba8d 100644 (file)
@@ -1362,6 +1362,7 @@ static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
                                struct hpi_control *hpi_ctl,
                                char *name)
 {
+       int len;
        char *dir;
        memset(snd_control, 0, sizeof(*snd_control));
        snd_control->name = hpi_ctl->name;
@@ -1384,23 +1385,30 @@ static void asihpi_ctl_init(struct snd_kcontrol_new *snd_control,
                dir = "Playback "; /* PCM Playback source, or  output node */
 
        if (hpi_ctl->src_node_type && hpi_ctl->dst_node_type)
-               sprintf(hpi_ctl->name, "%s %d %s %d %s%s",
-                       asihpi_src_names[hpi_ctl->src_node_type],
-                       hpi_ctl->src_node_index,
-                       asihpi_dst_names[hpi_ctl->dst_node_type],
-                       hpi_ctl->dst_node_index,
-                       dir, name);
+               len = snprintf(hpi_ctl->name, sizeof(hpi_ctl->name),
+                              "%s %d %s %d %s%s",
+                              asihpi_src_names[hpi_ctl->src_node_type],
+                              hpi_ctl->src_node_index,
+                              asihpi_dst_names[hpi_ctl->dst_node_type],
+                              hpi_ctl->dst_node_index,
+                              dir, name);
        else if (hpi_ctl->dst_node_type) {
-               sprintf(hpi_ctl->name, "%s %d %s%s",
-               asihpi_dst_names[hpi_ctl->dst_node_type],
-               hpi_ctl->dst_node_index,
-               dir, name);
+               len = snprintf(hpi_ctl->name, sizeof(hpi_ctl->name),
+                              "%s %d %s%s",
+                              asihpi_dst_names[hpi_ctl->dst_node_type],
+                              hpi_ctl->dst_node_index,
+                              dir, name);
        } else {
-               sprintf(hpi_ctl->name, "%s %d %s%s",
-               asihpi_src_names[hpi_ctl->src_node_type],
-               hpi_ctl->src_node_index,
-               dir, name);
+               len = snprintf(hpi_ctl->name, sizeof(hpi_ctl->name),
+                              "%s %d %s%s",
+                              asihpi_src_names[hpi_ctl->src_node_type],
+                              hpi_ctl->src_node_index,
+                              dir, name);
        }
+
+       if (len >= sizeof(hpi_ctl->name))
+               pr_err("asihpi: truncated control name: %s\n",
+                      hpi_ctl->name);
 }
 
 /*------------------------------------------------------------