Skip to contentSkip to conversation

genetlink: correctly begin the iteration over policies

PATCH Applied Jakub Kicinski 3 participants 4 messages Nov 8, 2022 – Nov 9, 2022

Subsystems (2)
lore
Share & tools
lore

Jakub Kicinski

author maintainer
#0 lore raw

PATCH 1/1 genetlink: correctly begin the iteration over policies

The return value from genl_op_iter_init() only tells us if there are any policies but to begin the iteration (and therefore load the first entry) we need to call genl_op_iter_next(). Note that it's safe to call genl_op_iter_next() on a family with no ops, it will just return false.

This may lead to various crashes, a warning in netlink_policy_dump_get_policy_idx() when policy is not found or.. no problem at all if the kmalloc'ed memory happens to be zeroed.

Fixes b502b3185cd6 ("genetlink: use iterator in the op to policy map dumping")

---
CC: [email protected]

Why KASAN doesn't catch the use of uninit memory here is a mystery :S

---
 net/netlink/genetlink.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netlink/genetlink.c +3 -1
index 362a61179036..9b7dfc45dd67 100644
@@ -1437,7 +1437,9 @@ static int ctrl_dumppolicy_start(struct netlink_callback *cb)
 	ctx->op_iter = kmalloc(sizeof(*ctx->op_iter), GFP_KERNEL);
 	if (!ctx->op_iter)
 		return -ENOMEM;
-	ctx->dump_map = genl_op_iter_init(rt, ctx->op_iter);
+
+	genl_op_iter_init(rt, ctx->op_iter);
+	ctx->dump_map = genl_op_iter_next(ctx->op_iter);
 
 	for (genl_op_iter_init(rt, &i); genl_op_iter_next(&i); ) {
 		if (i.doit.policy) {
Signature · 1 line
2.38.1
author maintainer
#1 lore raw
Quoted 1 line · On Tue, 8 Nov 2022 12:41:28 -0800 Jakub Kicinski wrote:

Why KASAN doesn't catch the use of uninit memory here is a mystery :S

Ah, because it's not KMSAN.

#2 lore raw
Quoted 8 lines

-----Original Message-----
From: Jakub Kicinski <[email protected]>
Sent: Tuesday, November 8, 2022 12:44 PM
To: [email protected]
Cc: [email protected]; [email protected]; [email protected]; Keller, Jacob E <[email protected]>
Subject: Re: [PATCH net-next] genetlink: correctly begin the iteration over policies

Quoted 4 lines
Quoted 1 line · On Tue, 8 Nov 2022 12:41:28 -0800 Jakub Kicinski wrote:

Why KASAN doesn't catch the use of uninit memory here is a mystery :S

Ah, because it's not KMSAN.

Yea, because KASAN mainly focuses on use-after-free and allocated buffer overruns, while KMSAN focuses on uninitialized memory. It looks like KMSAN isn't yet upstream.

Thanks,
Jake

#3 lore raw
In reply to Jakub Kicinski
This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 6 lines · On Tue, 8 Nov 2022 12:41:28 -0800 you wrote:

The return value from genl_op_iter_init() only tells us if there are any policies but to begin the iteration (and therefore load the first entry) we need to call genl_op_iter_next(). Note that it's safe to call genl_op_iter_next() on a family with no ops, it will just return false.

Quoted 6 lines

This may lead to various crashes, a warning in netlink_policy_dump_get_policy_idx() when policy is not found or.. no problem at all if the kmalloc'ed memory happens to be zeroed.

[...]

Here is the summary with links:
- [net-next] genetlink: correctly begin the iteration over policies

    https://git.kernel.org/netdev/net-next/c/154ba79c9f16

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html