Skip to contentSkip to conversation

firmware: cs_dsp: Replace strlcat() with seq_buf in KUnit tests

PATCH In review Andrey Zakharov 3 participants 3 messages Nov 24 Cirrus logic DSP firmware driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: cs_dsp: Replace strlcat() with seq_buf in KUnit tests

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 bin and wmfw tests fill a 512 byte buffer with a repeated string by calling strlcat() in a loop, and use the return value to detect that the buffer is full. Use seq_buf_init() and seq_buf_puts() and stop when seq_buf_has_overflowed() is true. kunit_kzalloc() returns a zeroed buffer, so seq_buf_init() is enough here.

Tested: built with gcc 16.2.1 and ran the cs_dsp KUnit suites under QEMU (x86_64, kunit.py, REGMAP_BUILD): 5829 of 5829 tests pass, the same as without the patch. The tests do not look at the contents of the buffer, so I also checked it with a temporary assertion after the
loop: strlen(infobuf) is 511 with the old strlcat() loop and with the new code, and the assertion fails if 510 is expected.

Not tested: other architectures and any real hardware.

---
 drivers/firmware/cirrus/test/cs_dsp_test_bin.c  | 7 +++++--
 drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)
drivers/firmware/cirrus/test/cs_dsp_test_bin.c +5 -2
index 63416838f865..2f44b7a6e637 100644
@@ -17,6 +17,7 @@
 #include <linux/math.h>
 #include <linux/random.h>
 #include <linux/regmap.h>
+#include <linux/seq_buf.h>
 
 #include "../cs_dsp.h"
 
@@ -2109,6 +2110,7 @@ static void bin_patch_name_and_info(struct kunit *test)
 	unsigned int reg_inc_per_word = cs_dsp_mock_reg_addr_inc_per_unpacked_word(priv);
 	u32 reg_val, payload_data;
 	char *infobuf;
+	struct seq_buf sb;
 	unsigned int alg_base_words, reg_addr;
 	struct firmware *fw;
 
@@ -2126,8 +2128,9 @@ static void bin_patch_name_and_info(struct kunit *test)
 	infobuf = kunit_kzalloc(test, 512, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, infobuf);
 
-	for (; strlcat(infobuf, "Waffle{Blah}\n", 512) < 512; )
-		;
+	seq_buf_init(&sb, infobuf, 512);
+	while (!seq_buf_has_overflowed(&sb))
+		seq_buf_puts(&sb, "Waffle{Blah}\n");
 
 	cs_dsp_mock_bin_add_info(priv->local->bin_builder, infobuf);
 
drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c +5 -2
index 1de70e81a868..464a0cc79415 100644
@@ -16,6 +16,7 @@
 #include <linux/firmware/cirrus/wmfw.h>
 #include <linux/random.h>
 #include <linux/regmap.h>
+#include <linux/seq_buf.h>
 #include <linux/string.h>
 #include <linux/vmalloc.h>
 
@@ -1722,6 +1723,7 @@ static void wmfw_load_with_info(struct kunit *test)
 	unsigned int reg_addr;
 	u8 *payload_data, *readback;
 	char *infobuf;
+	struct seq_buf sb;
 	const unsigned int payload_size_bytes = 48;
 	int ret;
 
@@ -1745,8 +1747,9 @@ static void wmfw_load_with_info(struct kunit *test)
 	infobuf = kunit_kzalloc(test, 512, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, infobuf);
 
-	for (; strlcat(infobuf, "Waffle{Blah}\n", 512) < 512;)
-		;
+	seq_buf_init(&sb, infobuf, 512);
+	while (!seq_buf_has_overflowed(&sb))
+		seq_buf_puts(&sb, "Waffle{Blah}\n");
 
 	cs_dsp_mock_wmfw_add_info(local->wmfw_builder, infobuf);
 	cs_dsp_mock_wmfw_add_info(local->wmfw_builder, "Another block of info");
Signature · 1 line
2.56.1
maintainer 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 bin and wmfw tests fill a 512 byte buffer with a repeated string by calling strlcat() in a loop, and use the return value to detect that the buffer is full. Use seq_buf_init() and seq_buf_puts() and stop when seq_buf_has_overflowed() is true. kunit_kzalloc() returns a zeroed buffer, so seq_buf_init() is enough here.

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

firmware: cs_dsp: Replace strlcat() with seq_buf in KUnit tests

Applied to

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

Thanks!

[1/1] firmware: cs_dsp: Replace strlcat() with seq_buf in KUnit tests

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

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