Skip to contentSkip to conversation

netconsole: selftest for userdata overflow

PATCH Superseded Breno Leitao 2 participants 7 messages Jan 3, 2025 – Jan 8, 2025

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Breno Leitao

author maintainer
#0 lore raw

v2 cover netconsole: selftest for userdata overflow

Implement comprehensive testing for netconsole userdata entry handling, demonstrating correct behavior when creating maximum entries and preventing unauthorized overflow.

Refactor existing test infrastructure to support modular, reusable helper functions that validate strict entry limit enforcement.

Also, add a warning if update_userdata() sees more than MAX_USERDATA_ITEMS entries. This shouldn't happen and it is a bug that shouldn't be silently ignored.

---
Changes in v2:
- Add the new script (netcons_overflow.sh) in tools/testing/selftests/drivers/net/Makefile as suggested by Simon
Horman
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Breno Leitao (4):
netconsole: Warn if MAX_USERDATA_ITEMS limit is exceeded
netconsole: selftest: Split the helpers from the selftest
netconsole: selftest: Delete all userdata keys
netconsole: selftest: verify userdata entry limit

 MAINTAINERS                                        |   3 +-
 drivers/net/netconsole.c                           |   2 +-
 tools/testing/selftests/drivers/net/Makefile       |   1 +
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    | 225 +++++++++++++++++++++
 .../testing/selftests/drivers/net/netcons_basic.sh | 218 +-------------------
 .../selftests/drivers/net/netcons_overflow.sh      |  67 ++++++
 6 files changed, 297 insertions(+), 219 deletions(-)

---
base-commit: 94c16fd4df9089931f674fb9aaec41ea20b0fd7a
change-id: 20241204-netcons_overflow_test-eaf735d1f743

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
author maintainer
#1 lore raw

PATCH v2 1/4 netconsole: Warn if MAX_USERDATA_ITEMS limit is exceeded

netconsole configfs helpers doesn't allow the creation of more than MAX_USERDATA_ITEMS items.

Add a warning when netconsole userdata update function attempts sees more than MAX_USERDATA_ITEMS entries.

Replace silent ignore mechanism with WARN_ON_ONCE() to highlight potential misuse during development and debugging.

---
 drivers/net/netconsole.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/netconsole.c +1 -1
index f422a2f666ef2276cf5b10e1dbc6badbc8ef0038..86ab4a42769a49eebe5dd6f01dafafc6c86ec54f 100644
@@ -730,7 +730,7 @@ static void update_userdata(struct netconsole_target *nt)
 		struct userdatum *udm_item;
 		struct config_item *item;
 
-		if (child_count >= MAX_USERDATA_ITEMS)
+		if (WARN_ON_ONCE(child_count >= MAX_USERDATA_ITEMS))
 			break;
 		child_count++;
 
Signature · 1 line
2.43.5
author maintainer
#2 lore raw

PATCH v2 2/4 netconsole: selftest: Split the helpers from the selftest

In reply to Breno Leitao

Split helper functions from the netconsole basic test into a separate library file to enable reuse across different netconsole tests. This change only moves the existing helper functions to lib/sh/lib_netcons.sh while preserving the same test functionality.

The helpers provide common functions for:
- Setting up network namespaces and interfaces
- Managing netconsole dynamic targets
- Setting user data
- Handling test dependencies
- Cleanup operations

Do not make any change in the code, other than the mechanical separation.

---
 MAINTAINERS                                        |   1 +
 .../selftests/drivers/net/lib/sh/lib_netcons.sh    | 225 +++++++++++++++++++++
 .../testing/selftests/drivers/net/netcons_basic.sh | 218 +-------------------
 3 files changed, 227 insertions(+), 217 deletions(-)
MAINTAINERS +1 -0
index 1579124ef426b8f2e066462e90e6c8e4d068f01e..d7af1c38dbab0c1ac253ced11a07ee7422c75c0a 100644
@@ -16174,6 +16174,7 @@ M:	Breno Leitao <[email protected]>
 S:	Maintained
 F:	Documentation/networking/netconsole.rst
 F:	drivers/net/netconsole.c
+F:	tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
 F:	tools/testing/selftests/drivers/net/netcons_basic.sh
 
 NETDEVSIM
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh new file +225 -0

225 lines not on this page · raw message

tools/testing/selftests/drivers/net/netcons_basic.sh +1 -217

225 lines not on this page · raw message

Signature · 1 line
2.43.5
maintainer
#3 lore raw

Re: [PATCH net-next v2 2/4] netconsole: selftest: Split the helpers from the selftest

Quoted 12 lines · On Fri, 03 Jan 2025 04:57:50 -0800 Breno Leitao wrote:

Split helper functions from the netconsole basic test into a separate library file to enable reuse across different netconsole tests. This change only moves the existing helper functions to lib/sh/lib_netcons.sh while preserving the same test functionality.

The helpers provide common functions for:
- Setting up network namespaces and interfaces
- Managing netconsole dynamic targets
- Setting user data
- Handling test dependencies
- Cleanup operations

Quoted 2 lines

Do not make any change in the code, other than the mechanical separation.

You have to update the Makefile, we don't wildcard all files under drivers/net/lib, the lib support in ksft is not great :(

$ make -C tools/testing/selftests/ \
	TARGETS="drivers/net drivers/net/hw" \
	install INSTALL_PATH=/tmp/ksft-net-drv
[... noise ...]
$ find /tmp/ksft-net-drv/ -iname '*netcons*'
/tmp/ksft-net-drv/drivers/net/netcons_basic.sh
/tmp/ksft-net-drv/drivers/net/netcons_overflow.sh

.. your new lib file won't get installed.

Signature · 1 line
pw-bot: cr
author maintainer
#4 lore raw

Re: [PATCH net-next v2 2/4] netconsole: selftest: Split the helpers from the selftest

Quoted 27 lines · Jakub Kicinski
Quoted 12 lines · On Fri, 03 Jan 2025 04:57:50 -0800 Breno Leitao wrote:

Split helper functions from the netconsole basic test into a separate library file to enable reuse across different netconsole tests. This change only moves the existing helper functions to lib/sh/lib_netcons.sh while preserving the same test functionality.

The helpers provide common functions for:
- Setting up network namespaces and interfaces
- Managing netconsole dynamic targets
- Setting user data
- Handling test dependencies
- Cleanup operations

Quoted 2 lines

Do not make any change in the code, other than the mechanical separation.

You have to update the Makefile, we don't wildcard all files under drivers/net/lib, the lib support in ksft is not great :(

$ make -C tools/testing/selftests/ \
	TARGETS="drivers/net drivers/net/hw" \
	install INSTALL_PATH=/tmp/ksft-net-drv
[... noise ...]
$ find /tmp/ksft-net-drv/ -iname '*netcons*'
/tmp/ksft-net-drv/drivers/net/netcons_basic.sh
/tmp/ksft-net-drv/drivers/net/netcons_overflow.sh
Quoted 1 line

.. your new lib file won't get installed.

Thanks for the detailed explanation. I found that the new lib was not installed.

I will be adding the following to the upcoming revision.

/tools/testing/selftests/drivers/net/Makefile
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0

TEST_INCLUDES := $(wildcard lib/py/*.py) \

+                $(wildcard lib/sh/*.sh) \
		 ../../net/net_helper.sh \
		 ../../net/lib.sh \
author maintainer
#5 lore raw

PATCH v2 3/4 netconsole: selftest: Delete all userdata keys

In reply to Breno Leitao

Modify the cleanup function to remove all userdata keys created during the test, instead of just deleting a single predefined key. This ensures a more thorough cleanup of temporary resources.

Move the KEY_PATH variable definition inside the set_user_data function to reduce global variables and improve encapsulation. The KEY_PATH variable is now dynamically created when setting user data.

This change has no effect on the current test, while improving an upcoming test that would create several userdata entries.

---
 tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh +3 -3
index fdd45a3468f17449eeb66d9a808b7a3b2107e47c..3acaba41ac7b21aa2fd8457ed640a5ac8a41bc12 100644
@@ -23,7 +23,6 @@ TARGET=$(mktemp -u netcons_XXXXX)
 DEFAULT_PRINTK_VALUES=$(cat /proc/sys/kernel/printk)
 NETCONS_CONFIGFS="/sys/kernel/config/netconsole"
 NETCONS_PATH="${NETCONS_CONFIGFS}"/"${TARGET}"
-KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
 # NAMESPACE will be populated by setup_ns with a random value
 NAMESPACE=""
 
@@ -116,8 +115,8 @@ function cleanup() {
 
 	# delete netconsole dynamic reconfiguration
 	echo 0 > "${NETCONS_PATH}"/enabled
-	# Remove key
-	rmdir "${KEY_PATH}"
+	# Remove all the keys that got created during the selftest
+	find "${NETCONS_PATH}/userdata/" -mindepth 1 -type d -delete
 	# Remove the configfs entry
 	rmdir "${NETCONS_PATH}"
 
@@ -139,6 +138,7 @@ function set_user_data() {
 		exit "${ksft_skip}"
 	fi
 
+	KEY_PATH="${NETCONS_PATH}/userdata/${USERDATA_KEY}"
 	mkdir -p "${KEY_PATH}"
 	VALUE_PATH="${KEY_PATH}""/value"
 	echo "${USERDATA_VALUE}" > "${VALUE_PATH}"
Signature · 1 line
2.43.5
author maintainer
#6 lore raw

PATCH v2 4/4 netconsole: selftest: verify userdata entry limit

In reply to Breno Leitao

Add a new selftest for netconsole that tests the userdata entry limit functionality. The test performs two key verifications:

1. Create MAX_USERDATA_ITEMS (16) userdata entries successfully
2. Confirm that attempting to create an additional userdata entry fails

The selftest script uses the netcons library and checks the behavior by attempting to create entries beyond the maximum allowed limit.

---
 MAINTAINERS                                        |  2 +-
 tools/testing/selftests/drivers/net/Makefile       |  1 +
 .../selftests/drivers/net/netcons_overflow.sh      | 67 ++++++++++++++++++++++
 3 files changed, 69 insertions(+), 1 deletion(-)
MAINTAINERS +1 -1
index d7af1c38dbab0c1ac253ced11a07ee7422c75c0a..d14bfb5ab0122f0b6651e72add3b178dcf742f18 100644
@@ -16175,7 +16175,7 @@ S:	Maintained
 F:	Documentation/networking/netconsole.rst
 F:	drivers/net/netconsole.c
 F:	tools/testing/selftests/drivers/net/lib/sh/lib_netcons.sh
-F:	tools/testing/selftests/drivers/net/netcons_basic.sh
+F:	tools/testing/selftests/drivers/net/netcons\*
 
 NETDEVSIM
 M:	Jakub Kicinski <[email protected]>
tools/testing/selftests/drivers/net/Makefile +1 -0
index 0fec8f9801ad4d94d99ab6d0acaea6e2e7b2894d..bcd630eb072eb9cf22bb11a647de751795de2408 100644
@@ -6,6 +6,7 @@ TEST_INCLUDES := $(wildcard lib/py/*.py) \
 
 TEST_PROGS := \
 	netcons_basic.sh \
+	netcons_overflow.sh \
 	ping.py \
 	queues.py \
 	stats.py \
tools/testing/selftests/drivers/net/netcons_overflow.sh new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
2.43.5