Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

netlink: fix false positive warning in extack during dumps

Commit under fixes extended extack reporting to dumps.
It works under normal conditions, because extack errors are
usually reported during ->start() or the first ->dump(),
it's quite rare that the dump starts okay but fails later.
If the dump does fail later, however, the input skb will
already have the initiating message pulled, so checking
if bad attr falls within skb->data will fail.

Switch the check to using nlh, which is always valid.

syzbot found a way to hit that scenario by filling up
the receive queue. In this case we initiate a dump
but don't call ->dump() until there is read space for
an skb.

WARNING: CPU: 1 PID: 5845 at net/netlink/af_netlink.c:2210 netlink_ack_tlv_fill+0x1a8/0x560 net/netlink/af_netlink.c:2209
RIP: 0010:netlink_ack_tlv_fill+0x1a8/0x560 net/netlink/af_netlink.c:2209
Call Trace:
<TASK>
netlink_dump_done+0x513/0x970 net/netlink/af_netlink.c:2250
netlink_dump+0x91f/0xe10 net/netlink/af_netlink.c:2351
netlink_recvmsg+0x6bb/0x11d0 net/netlink/af_netlink.c:1983
sock_recvmsg_nosec net/socket.c:1051 [inline]
sock_recvmsg+0x22f/0x280 net/socket.c:1073
__sys_recvfrom+0x246/0x3d0 net/socket.c:2267
__do_sys_recvfrom net/socket.c:2285 [inline]
__se_sys_recvfrom net/socket.c:2281 [inline]
__x64_sys_recvfrom+0xde/0x100 net/socket.c:2281
do_syscall_x64 arch/x86/entry/common.c:52 [inline]
do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7ff37dd17a79

Reported-by: syzbot+d4373fa8042c06cefa84@syzkaller.appspotmail.com
Fixes: 8af4f60472fc ("netlink: support all extack types in dumps")
Reviewed-by: Jacob Keller <jacob.e.keller@intel.com>
Link: https://patch.msgid.link/20241119224432.1713040-1-kuba@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+11 -10
+11 -10
net/netlink/af_netlink.c
··· 2181 2181 return tlvlen; 2182 2182 } 2183 2183 2184 + static bool nlmsg_check_in_payload(const struct nlmsghdr *nlh, const void *addr) 2185 + { 2186 + return !WARN_ON(addr < nlmsg_data(nlh) || 2187 + addr - (const void *) nlh >= nlh->nlmsg_len); 2188 + } 2189 + 2184 2190 static void 2185 - netlink_ack_tlv_fill(struct sk_buff *in_skb, struct sk_buff *skb, 2186 - const struct nlmsghdr *nlh, int err, 2191 + netlink_ack_tlv_fill(struct sk_buff *skb, const struct nlmsghdr *nlh, int err, 2187 2192 const struct netlink_ext_ack *extack) 2188 2193 { 2189 2194 if (extack->_msg) ··· 2200 2195 if (!err) 2201 2196 return; 2202 2197 2203 - if (extack->bad_attr && 2204 - !WARN_ON((u8 *)extack->bad_attr < in_skb->data || 2205 - (u8 *)extack->bad_attr >= in_skb->data + in_skb->len)) 2198 + if (extack->bad_attr && nlmsg_check_in_payload(nlh, extack->bad_attr)) 2206 2199 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_OFFS, 2207 2200 (u8 *)extack->bad_attr - (const u8 *)nlh)); 2208 2201 if (extack->policy) ··· 2209 2206 if (extack->miss_type) 2210 2207 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_TYPE, 2211 2208 extack->miss_type)); 2212 - if (extack->miss_nest && 2213 - !WARN_ON((u8 *)extack->miss_nest < in_skb->data || 2214 - (u8 *)extack->miss_nest > in_skb->data + in_skb->len)) 2209 + if (extack->miss_nest && nlmsg_check_in_payload(nlh, extack->miss_nest)) 2215 2210 WARN_ON(nla_put_u32(skb, NLMSGERR_ATTR_MISS_NEST, 2216 2211 (u8 *)extack->miss_nest - (const u8 *)nlh)); 2217 2212 } ··· 2238 2237 if (extack_len) { 2239 2238 nlh->nlmsg_flags |= NLM_F_ACK_TLVS; 2240 2239 if (skb_tailroom(skb) >= extack_len) { 2241 - netlink_ack_tlv_fill(cb->skb, skb, cb->nlh, 2240 + netlink_ack_tlv_fill(skb, cb->nlh, 2242 2241 nlk->dump_done_errno, extack); 2243 2242 nlmsg_end(skb, nlh); 2244 2243 } ··· 2497 2496 } 2498 2497 2499 2498 if (tlvlen) 2500 - netlink_ack_tlv_fill(in_skb, skb, nlh, err, extack); 2499 + netlink_ack_tlv_fill(skb, nlh, err, extack); 2501 2500 2502 2501 nlmsg_end(skb, rep); 2503 2502