]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
ALSA: ump: Copy FB name string more safely
authorTakashi Iwai <tiwai@suse.de>
Fri, 10 Jan 2025 15:59:37 +0000 (16:59 +0100)
committerTakashi Iwai <tiwai@suse.de>
Sun, 12 Jan 2025 12:12:20 +0000 (13:12 +0100)
The UMP group names are referred as the corresponding sequencer port
names, hence they should be proper ASCII strings.  OTOH, the UMP group
names are composed from the UMP FB strings that are received from the
device; i.e. a device may give some bogus letters and we can't trust
them fully.

To assure that the group names consist of the proper ASCII strings,
replace the normal string copy and append operations with special ones
that strip the non-printable letters.

Signed-off-by: Takashi Iwai <tiwai@suse.de>
Link: https://patch.msgid.link/20250110155943.31578-5-tiwai@suse.de
sound/core/ump.c

index d6cd11be875045ac901e3d7089efb2a3a267fafb..ce2e180ca5571813a31d5531181a5ecb41135803 100644 (file)
@@ -53,6 +53,34 @@ static inline void update_legacy_names(struct snd_ump_endpoint *ump)
 }
 #endif
 
+/* copy a string safely with stripping non-printable letters */
+static void safe_copy_string(void *dst, size_t max_dst_size,
+                            const void *src, size_t max_src_size)
+{
+       const unsigned char *s = src;
+       unsigned char *d = dst;
+
+       if (!max_dst_size--)
+               return;
+       for (s = src; max_dst_size && *s && max_src_size--; s++) {
+               if (!isascii(*s) || !isprint(*s))
+                       continue;
+               *d++ = *s;
+               max_dst_size--;
+       }
+       *d = 0;
+}
+
+/* append a string safely with stripping non-printable letters */
+static void safe_append_string(void *dst, size_t max_dst_size,
+                              const void *src, size_t max_src_size)
+{
+       unsigned char *d = dst;
+       size_t len = strlen(d);
+
+       safe_copy_string(d + len, max_dst_size - len, src, max_src_size);
+}
+
 static const struct snd_rawmidi_global_ops snd_ump_rawmidi_ops = {
        .dev_register = snd_ump_dev_register,
        .dev_unregister = snd_ump_dev_unregister,
@@ -565,16 +593,10 @@ void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump)
                        }
                        if (!*fb->info.name)
                                continue;
-                       if (!*group->name) {
-                               /* store the first matching name */
-                               strscpy(group->name, fb->info.name,
-                                       sizeof(group->name));
-                       } else {
-                               /* when overlapping, concat names */
+                       if (*group->name)
                                strlcat(group->name, ", ", sizeof(group->name));
-                               strlcat(group->name, fb->info.name,
-                                       sizeof(group->name));
-                       }
+                       safe_append_string(group->name, sizeof(group->name),
+                                          fb->info.name, sizeof(fb->info.name));
                }
        }
 }