Skip to contentSkip to conversation

ASoC: wm_adsp_fw_find_test: Replace strlcat() with seq_buf

PATCH In review Andrey Zakharov 3 participants 3 messages Nov 7 – Nov 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: wm_adsp_fw_find_test: Replace strlcat() with seq_buf

strlcat() is being removed from the kernel. It runs an unbounded strlen() on the destination, and its size argument is not tied to the real buffer, see the issue below.

The test builds the searched_fw_files string by appending to a fixed size buffer in two places. Use seq_buf_init_append() and seq_buf_puts() instead. seq_buf_init_append() keeps the NUL-terminated content already in the buffer, and seq_buf_puts() copies as much as fits and keeps the buffer NUL-terminated if it overflows.

Tested: built with gcc 16.2.1 and ran the wm-adsp-fw-find KUnit suite under QEMU (x86_64, kunit.py, REGMAP_BUILD): 90 of 90 tests pass, the same as without the patch. As a negative control, changing the separator written by the firmware request stub makes the search order cases fail. The directory listing in the pick_file test is only used in failure messages and is not compared with anything.

Not tested: other architectures, the case where the 768 byte buffer overflows, and any real hardware.

---
 sound/soc/codecs/wm_adsp_fw_find_test.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
sound/soc/codecs/wm_adsp_fw_find_test.c +11 -6
index ae686dc4fa94..2a44b2670fa9 100644
@@ -8,6 +8,7 @@
 #include <kunit/device.h>
 #include <kunit/static_stub.h>
 #include <kunit/test.h>
+#include <linux/seq_buf.h>
 #include <linux/slab.h>
 #include "wm_adsp.h"
 
@@ -104,14 +105,15 @@ static void wm_adsp_fw_find_test_pick_file(struct kunit *test)
 	struct wm_adsp_fw_find_test *priv = test->priv;
 	const struct wm_adsp_fw_find_test_params *params = test->param_value;
 	struct wm_adsp *dsp = &priv->dsp;
+	struct seq_buf sb;
 	int i, ret;
 
 	/* Concatenate string of dir content for error messages */
+	seq_buf_init_append(&sb, priv->searched_fw_files,
+			    sizeof(priv->searched_fw_files));
 	for (i = 0; params->dir_files[i]; i++) {
-		strlcat(priv->searched_fw_files, params->dir_files[i],
-			sizeof(priv->searched_fw_files));
-		strlcat(priv->searched_fw_files, ";",
-			sizeof(priv->searched_fw_files));
+		seq_buf_puts(&sb, params->dir_files[i]);
+		seq_buf_puts(&sb, ";");
 	}
 
 	dsp->cs_dsp.name = params->dsp_name;
@@ -160,6 +162,7 @@ static int wm_adsp_fw_find_test_firmware_request_stub(const struct firmware **fi
 	struct kunit *test = kunit_get_current_test();
 	const struct wm_adsp_fw_find_test_params *params = test->param_value;
 	struct wm_adsp_fw_find_test *priv = test->priv;
+	struct seq_buf sb;
 
 	/*
 	 * Searches are accumulated as a single string of space-separated names.
@@ -168,10 +171,12 @@ static int wm_adsp_fw_find_test_firmware_request_stub(const struct firmware **fi
 	 * a simple KUNIT_EXPECT_STREQ(), which avoids the risk of bugs in a
 	 * more complex custom comparison.
 	 */
+	seq_buf_init_append(&sb, priv->searched_fw_files,
+			    sizeof(priv->searched_fw_files));
 	if (priv->searched_fw_files[0] != '\0')
-		strlcat(priv->searched_fw_files, " ", sizeof(priv->searched_fw_files));
+		seq_buf_puts(&sb, " ");
 
-	strlcat(priv->searched_fw_files, filename, sizeof(priv->searched_fw_files));
+	seq_buf_puts(&sb, filename);
 
 	/* Non-parameterized test? */
 	if (!params)
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Andrey Zakharov

strlcat() is being removed from the kernel. It runs an unbounded strlen() on the destination, and its size argument is not tied to the real buffer, see the issue below.

The test builds the searched_fw_files string by appending to a fixed size buffer in two places. Use seq_buf_init_append() and seq_buf_puts() instead. seq_buf_init_append() keeps the NUL-terminated content already in the buffer, and seq_buf_puts() copies as much as fits and keeps the buffer NUL-terminated if it overflows.

maintainer
#2 lore raw
In reply to Andrey Zakharov
Quoted 1 line · Andrey Zakharov

ASoC: wm_adsp_fw_find_test: Replace strlcat() with seq_buf

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5

Thanks!

[1/1] ASoC: wm_adsp_fw_find_test: Replace strlcat() with seq_buf

      https://git.kernel.org/broonie/sound/c/edd3a027969c

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark