[PATCH 6.1 05/15] genetlink: add test case for family with invalid ops

Yana Bashlykova posted 15 patches 2 weeks, 5 days ago
[PATCH 6.1 05/15] genetlink: add test case for family with invalid ops
Posted by Yana Bashlykova 2 weeks, 5 days ago
Add test case that verifies error handling when registering
a genetlink family with invalid operations where both doit
and dumpit callbacks are NULL.

The test registers incorrect_ops_genl_family which contains a command with:
- .doit = NULL
- .dumpit = NULL
and expects the registration to fail with -EINVAL.

This validates proper validation of genetlink operations during family
registration.

Signed-off-by: Yana Bashlykova <yana2bsh@gmail.com>
---
 .../net-pf-16-proto-16-family-PARALLEL_GENL.c | 41 +++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c b/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
index 1db5d15a6f2c..245f3b0f4fbb 100644
--- a/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
+++ b/drivers/net/genetlink/net-pf-16-proto-16-family-PARALLEL_GENL.c
@@ -1089,6 +1089,33 @@ static struct genl_family incorrect_genl_family = {
 	.policy = my_genl_policy,
 };
 
+enum {
+	INCORRECT_OP_WITH_NULL,
+};
+
+// Generic Netlink operations with incorrect ops
+static const struct genl_ops incorrect_ops_with_null[] = {
+	{
+		.cmd = INCORRECT_OP_WITH_NULL,
+		.flags = 0,
+		.policy = my_genl_policy, // random policy
+		.doit = NULL, // doit and dumpit are NULL --> kernel will send -EINVAL
+		.dumpit = NULL,
+	},
+};
+
+// genl_family struct with incorrect ops
+static struct genl_family incorrect_ops_genl_family = {
+	.hdrsize = 0,
+	.name = "INCORRECT",
+	.version = 1,
+	.maxattr = 1,
+	.netnsok = true,
+	.ops = incorrect_ops_with_null, // ops contain NULL
+	.n_ops = ARRAY_SIZE(incorrect_ops_with_null),
+	.policy = my_genl_policy, // random policy
+};
+
 static int __init init_netlink(void)
 {
 	int rc;
@@ -1116,6 +1143,16 @@ static int __init init_netlink(void)
 	return rc;
 }
 
+static int __init incorrect_ops_netlink(void)
+{
+	int ret;
+
+	ret = genl_register_family(&incorrect_ops_genl_family);
+	if (ret != -EINVAL)
+		return ret;
+	return 0;
+}
+
 static int __init incorrect_init_netlink(void)
 {
 	int rc;
@@ -1241,6 +1278,10 @@ static int __init module_netlink_init(void)
 	if (ret)
 		goto err_sysfs;
 
+	ret = incorrect_ops_netlink();
+	if (ret)
+		goto err_sysfs;
+
 	ret = init_netlink();
 	if (ret)
 		goto err_sysfs;
-- 
2.34.1