Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: linux-sound@vger.kernel.org
Cc: Takashi Iwai <tiwai@suse.de>
Subject: [PATCH 4/8] ALSA: ump: Copy FB name string more safely
Date: Fri, 10 Jan 2025 16:59:37 +0100	[thread overview]
Message-ID: <20250110155943.31578-5-tiwai@suse.de> (raw)
In-Reply-To: <20250110155943.31578-1-tiwai@suse.de>

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>
---
 sound/core/ump.c | 40 +++++++++++++++++++++++++++++++---------
 1 file changed, 31 insertions(+), 9 deletions(-)

diff --git a/sound/core/ump.c b/sound/core/ump.c
index d6cd11be8750..ce2e180ca557 100644
--- a/sound/core/ump.c
+++ b/sound/core/ump.c
@@ -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));
 		}
 	}
 }
-- 
2.43.0


  parent reply	other threads:[~2025-01-10 15:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-10 15:59 [PATCH 0/8] ALSA: Small extensions for UMP Takashi Iwai
2025-01-10 15:59 ` [PATCH 1/8] ALSA: rawmidi: Expose the tied device number in info ioctl Takashi Iwai
2025-01-14 10:25   ` Jaroslav Kysela
2025-01-14 10:37     ` Takashi Iwai
2025-01-10 15:59 ` [PATCH 2/8] ALSA: rawmidi: Show substream activity " Takashi Iwai
2025-01-10 15:59 ` [PATCH 3/8] ALSA: rawmidi: Bump protocol version to 2.0.5 Takashi Iwai
2025-01-10 15:59 ` Takashi Iwai [this message]
2025-01-10 15:59 ` [PATCH 5/8] ALSA: ump: Copy safe string name to rawmidi Takashi Iwai
2025-01-10 15:59 ` [PATCH 6/8] ALSA: ump: Update rawmidi name per EP name update Takashi Iwai
2025-01-10 15:59 ` [PATCH 7/8] ALSA: seq: Allow system notification in atomic Takashi Iwai
2025-01-10 15:59 ` [PATCH 8/8] ALSA: seq: Notify UMP EP and FB changes Takashi Iwai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250110155943.31578-5-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=linux-sound@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox