netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes
@ 2024-08-29  7:43 Matt Johnston
  2024-08-29  7:43 ` [PATCH net v2 1/2] net: mctp-serial: Add kunit test for next_chunk_len() Matt Johnston
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Matt Johnston @ 2024-08-29  7:43 UTC (permalink / raw)
  To: Jeremy Kerr
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet,
	netdev

The mctp-serial code to add escape characters was incorrect due to an
off-by-one error. This series adds a test for the chunking which splits
by escape characters, and fixes the bug.

v2: Fix kunit param const pointer

Matt Johnston (2):
  net: mctp-serial: Add kunit test for next_chunk_len()
  net: mctp-serial: Fix missing escapes on transmit

 drivers/net/mctp/Kconfig       |   5 ++
 drivers/net/mctp/mctp-serial.c | 113 ++++++++++++++++++++++++++++++++-
 2 files changed, 116 insertions(+), 2 deletions(-)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH net v2 1/2] net: mctp-serial: Add kunit test for next_chunk_len()
  2024-08-29  7:43 [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes Matt Johnston
@ 2024-08-29  7:43 ` Matt Johnston
  2024-08-29  7:43 ` [PATCH net v2 2/2] net: mctp-serial: Fix missing escapes on transmit Matt Johnston
  2024-09-01 17:20 ` [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Matt Johnston @ 2024-08-29  7:43 UTC (permalink / raw)
  To: Jeremy Kerr
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet,
	netdev

Test various edge cases of inputs that contain characters
that need escaping.

This adds a new kunit suite for mctp-serial.

Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
---

v2: Fix kunit param const pointer

 drivers/net/mctp/Kconfig       |   5 ++
 drivers/net/mctp/mctp-serial.c | 109 +++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+)

diff --git a/drivers/net/mctp/Kconfig b/drivers/net/mctp/Kconfig
index ce9d2d2ccf3b..15860d6ac39f 100644
--- a/drivers/net/mctp/Kconfig
+++ b/drivers/net/mctp/Kconfig
@@ -21,6 +21,11 @@ config MCTP_SERIAL
 	  Say y here if you need to connect to MCTP endpoints over serial. To
 	  compile as a module, use m; the module will be called mctp-serial.
 
+config MCTP_SERIAL_TEST
+        bool "MCTP serial tests" if !KUNIT_ALL_TESTS
+        depends on MCTP_SERIAL=y && KUNIT=y
+        default KUNIT_ALL_TESTS
+
 config MCTP_TRANSPORT_I2C
 	tristate "MCTP SMBus/I2C transport"
 	# i2c-mux is optional, but we must build as a module if i2c-mux is a module
diff --git a/drivers/net/mctp/mctp-serial.c b/drivers/net/mctp/mctp-serial.c
index 5bf6fdff701c..7a40d07ff77b 100644
--- a/drivers/net/mctp/mctp-serial.c
+++ b/drivers/net/mctp/mctp-serial.c
@@ -521,3 +521,112 @@ module_exit(mctp_serial_exit);
 MODULE_LICENSE("GPL v2");
 MODULE_AUTHOR("Jeremy Kerr <jk@codeconstruct.com.au>");
 MODULE_DESCRIPTION("MCTP Serial transport");
+
+#if IS_ENABLED(CONFIG_MCTP_SERIAL_TEST)
+#include <kunit/test.h>
+
+#define MAX_CHUNKS 6
+struct test_chunk_tx {
+	u8 input_len;
+	u8 input[MCTP_SERIAL_MTU];
+	u8 chunks[MAX_CHUNKS];
+};
+
+static void test_next_chunk_len(struct kunit *test)
+{
+	struct mctp_serial devx;
+	struct mctp_serial *dev = &devx;
+	int next;
+
+	const struct test_chunk_tx *params = test->param_value;
+
+	memset(dev, 0x0, sizeof(*dev));
+	memcpy(dev->txbuf, params->input, params->input_len);
+	dev->txlen = params->input_len;
+
+	for (size_t i = 0; i < MAX_CHUNKS; i++) {
+		next = next_chunk_len(dev);
+		dev->txpos += next;
+		KUNIT_EXPECT_EQ(test, next, params->chunks[i]);
+
+		if (next == 0) {
+			KUNIT_EXPECT_EQ(test, dev->txpos, dev->txlen);
+			return;
+		}
+	}
+
+	KUNIT_FAIL_AND_ABORT(test, "Ran out of chunks");
+}
+
+static struct test_chunk_tx chunk_tx_tests[] = {
+	{
+		.input_len = 5,
+		.input = { 0x00, 0x11, 0x22, 0x7e, 0x80 },
+		.chunks = { 3, 1, 1, 0},
+	},
+	{
+		.input_len = 5,
+		.input = { 0x00, 0x11, 0x22, 0x7e, 0x7d },
+		.chunks = { 3, 1, 1, 0},
+	},
+	{
+		.input_len = 3,
+		.input = { 0x7e, 0x11, 0x22, },
+		.chunks = { 1, 2, 0},
+	},
+	{
+		.input_len = 3,
+		.input = { 0x7e, 0x7e, 0x7d, },
+		.chunks = { 1, 1, 1, 0},
+	},
+	{
+		.input_len = 4,
+		.input = { 0x7e, 0x7e, 0x00, 0x7d, },
+		.chunks = { 1, 1, 1, 1, 0},
+	},
+	{
+		.input_len = 6,
+		.input = { 0x7e, 0x7e, 0x00, 0x7d, 0x10, 0x10},
+		.chunks = { 1, 1, 1, 1, 2, 0},
+	},
+	{
+		.input_len = 1,
+		.input = { 0x7e },
+		.chunks = { 1, 0 },
+	},
+	{
+		.input_len = 1,
+		.input = { 0x80 },
+		.chunks = { 1, 0 },
+	},
+	{
+		.input_len = 3,
+		.input = { 0x80, 0x80, 0x00 },
+		.chunks = { 3, 0 },
+	},
+	{
+		.input_len = 7,
+		.input = { 0x01, 0x00, 0x08, 0xc8, 0x00, 0x80, 0x02 },
+		.chunks = { 7, 0 },
+	},
+	{
+		.input_len = 7,
+		.input = { 0x01, 0x00, 0x08, 0xc8, 0x7e, 0x80, 0x02 },
+		.chunks = { 4, 1, 2, 0 },
+	},
+};
+
+KUNIT_ARRAY_PARAM(chunk_tx, chunk_tx_tests, NULL);
+
+static struct kunit_case mctp_serial_test_cases[] = {
+	KUNIT_CASE_PARAM(test_next_chunk_len, chunk_tx_gen_params),
+};
+
+static struct kunit_suite mctp_serial_test_suite = {
+	.name = "mctp_serial",
+	.test_cases = mctp_serial_test_cases,
+};
+
+kunit_test_suite(mctp_serial_test_suite);
+
+#endif /* CONFIG_MCTP_SERIAL_TEST */

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH net v2 2/2] net: mctp-serial: Fix missing escapes on transmit
  2024-08-29  7:43 [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes Matt Johnston
  2024-08-29  7:43 ` [PATCH net v2 1/2] net: mctp-serial: Add kunit test for next_chunk_len() Matt Johnston
@ 2024-08-29  7:43 ` Matt Johnston
  2024-09-01 17:20 ` [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: Matt Johnston @ 2024-08-29  7:43 UTC (permalink / raw)
  To: Jeremy Kerr
  Cc: David S. Miller, Jakub Kicinski, Paolo Abeni, Eric Dumazet,
	netdev, stable

0x7d and 0x7e bytes are meant to be escaped in the data portion of
frames, but this didn't occur since next_chunk_len() had an off-by-one
error. That also resulted in the final byte of a payload being written
as a separate tty write op.

The chunk prior to an escaped byte would be one byte short, and the
next call would never test the txpos+1 case, which is where the escaped
byte was located. That meant it never hit the escaping case in
mctp_serial_tx_work().

Example Input: 01 00 08 c8 7e 80 02

Previous incorrect chunks from next_chunk_len():

01 00 08
c8 7e 80
02

With this fix:

01 00 08 c8
7e
80 02

Cc: stable@vger.kernel.org
Fixes: a0c2ccd9b5ad ("mctp: Add MCTP-over-serial transport binding")
Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
---
 drivers/net/mctp/mctp-serial.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/mctp/mctp-serial.c b/drivers/net/mctp/mctp-serial.c
index 7a40d07ff77b..f39bbe255497 100644
--- a/drivers/net/mctp/mctp-serial.c
+++ b/drivers/net/mctp/mctp-serial.c
@@ -91,8 +91,8 @@ static int next_chunk_len(struct mctp_serial *dev)
 	 * will be those non-escaped bytes, and does not include the escaped
 	 * byte.
 	 */
-	for (i = 1; i + dev->txpos + 1 < dev->txlen; i++) {
-		if (needs_escape(dev->txbuf[dev->txpos + i + 1]))
+	for (i = 1; i + dev->txpos < dev->txlen; i++) {
+		if (needs_escape(dev->txbuf[dev->txpos + i]))
 			break;
 	}
 

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes
  2024-08-29  7:43 [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes Matt Johnston
  2024-08-29  7:43 ` [PATCH net v2 1/2] net: mctp-serial: Add kunit test for next_chunk_len() Matt Johnston
  2024-08-29  7:43 ` [PATCH net v2 2/2] net: mctp-serial: Fix missing escapes on transmit Matt Johnston
@ 2024-09-01 17:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 4+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-09-01 17:20 UTC (permalink / raw)
  To: Matt Johnston; +Cc: jk, davem, kuba, pabeni, edumazet, netdev

Hello:

This series was applied to netdev/net.git (main)
by David S. Miller <davem@davemloft.net>:

On Thu, 29 Aug 2024 15:43:44 +0800 you wrote:
> The mctp-serial code to add escape characters was incorrect due to an
> off-by-one error. This series adds a test for the chunking which splits
> by escape characters, and fixes the bug.
> 
> v2: Fix kunit param const pointer
> 
> Matt Johnston (2):
>   net: mctp-serial: Add kunit test for next_chunk_len()
>   net: mctp-serial: Fix missing escapes on transmit
> 
> [...]

Here is the summary with links:
  - [net,v2,1/2] net: mctp-serial: Add kunit test for next_chunk_len()
    https://git.kernel.org/netdev/net/c/4fa9c5181cfe
  - [net,v2,2/2] net: mctp-serial: Fix missing escapes on transmit
    https://git.kernel.org/netdev/net/c/f962e8361adf

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-09-01 17:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-29  7:43 [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes Matt Johnston
2024-08-29  7:43 ` [PATCH net v2 1/2] net: mctp-serial: Add kunit test for next_chunk_len() Matt Johnston
2024-08-29  7:43 ` [PATCH net v2 2/2] net: mctp-serial: Fix missing escapes on transmit Matt Johnston
2024-09-01 17:20 ` [PATCH net v2 0/2] net: mctp-serial: Fix for missing tx escapes patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).