Skip to content

Commit e3f035e

Browse files
committed
ALSA: ump: Copy FB name string more safely
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 <[email protected]> Link: https://patch.msgid.link/[email protected]
1 parent 7bb49d2 commit e3f035e

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

sound/core/ump.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ static inline void update_legacy_names(struct snd_ump_endpoint *ump)
5353
}
5454
#endif
5555

56+
/* copy a string safely with stripping non-printable letters */
57+
static void safe_copy_string(void *dst, size_t max_dst_size,
58+
const void *src, size_t max_src_size)
59+
{
60+
const unsigned char *s = src;
61+
unsigned char *d = dst;
62+
63+
if (!max_dst_size--)
64+
return;
65+
for (s = src; max_dst_size && *s && max_src_size--; s++) {
66+
if (!isascii(*s) || !isprint(*s))
67+
continue;
68+
*d++ = *s;
69+
max_dst_size--;
70+
}
71+
*d = 0;
72+
}
73+
74+
/* append a string safely with stripping non-printable letters */
75+
static void safe_append_string(void *dst, size_t max_dst_size,
76+
const void *src, size_t max_src_size)
77+
{
78+
unsigned char *d = dst;
79+
size_t len = strlen(d);
80+
81+
safe_copy_string(d + len, max_dst_size - len, src, max_src_size);
82+
}
83+
5684
static const struct snd_rawmidi_global_ops snd_ump_rawmidi_ops = {
5785
.dev_register = snd_ump_dev_register,
5886
.dev_unregister = snd_ump_dev_unregister,
@@ -565,16 +593,10 @@ void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump)
565593
}
566594
if (!*fb->info.name)
567595
continue;
568-
if (!*group->name) {
569-
/* store the first matching name */
570-
strscpy(group->name, fb->info.name,
571-
sizeof(group->name));
572-
} else {
573-
/* when overlapping, concat names */
596+
if (*group->name)
574597
strlcat(group->name, ", ", sizeof(group->name));
575-
strlcat(group->name, fb->info.name,
576-
sizeof(group->name));
577-
}
598+
safe_append_string(group->name, sizeof(group->name),
599+
fb->info.name, sizeof(fb->info.name));
578600
}
579601
}
580602
}

0 commit comments

Comments
 (0)