mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-11 12:28:41 +08:00
43a6b3b4c7
[ Upstream commit25a9c8a443
] syzbot reported a warning in __local_bh_enable_ip(). [0] Commit8d61f926d4
("netlink: fix potential deadlock in netlink_set_err()") converted read_lock(&nl_table_lock) to read_lock_irqsave() in __netlink_diag_dump() to prevent a deadlock. However, __netlink_diag_dump() calls sock_i_ino() that uses read_lock_bh() and read_unlock_bh(). If CONFIG_TRACE_IRQFLAGS=y, read_unlock_bh() finally enables IRQ even though it should stay disabled until the following read_unlock_irqrestore(). Using read_lock() in sock_i_ino() would trigger a lockdep splat in another place that was fixed in commitf064af1e50
("net: fix a lockdep splat"), so let's add __sock_i_ino() that would be safe to use under BH disabled. [0]: WARNING: CPU: 0 PID: 5012 at kernel/softirq.c:376 __local_bh_enable_ip+0xbe/0x130 kernel/softirq.c:376 Modules linked in: CPU: 0 PID: 5012 Comm: syz-executor487 Not tainted 6.4.0-rc7-syzkaller-00202-g6f68fc395f49 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/27/2023 RIP: 0010:__local_bh_enable_ip+0xbe/0x130 kernel/softirq.c:376 Code: 45 bf 01 00 00 00 e8 91 5b 0a 00 e8 3c 15 3d 00 fb 65 8b 05 ec e9 b5 7e 85 c0 74 58 5b 5d c3 65 8b 05 b2 b6 b4 7e 85 c0 75 a2 <0f> 0b eb 9e e8 89 15 3d 00 eb 9f 48 89 ef e8 6f 49 18 00 eb a8 0f RSP: 0018:ffffc90003a1f3d0 EFLAGS: 00010046 RAX: 0000000000000000 RBX: 0000000000000201 RCX: 1ffffffff1cf5996 RDX: 0000000000000000 RSI: 0000000000000201 RDI: ffffffff8805c6f3 RBP: ffffffff8805c6f3 R08: 0000000000000001 R09: ffff8880152b03a3 R10: ffffed1002a56074 R11: 0000000000000005 R12: 00000000000073e4 R13: dffffc0000000000 R14: 0000000000000002 R15: 0000000000000000 FS: 0000555556726300(0000) GS:ffff8880b9800000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 000000000045ad50 CR3: 000000007c646000 CR4: 00000000003506f0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: <TASK> sock_i_ino+0x83/0xa0 net/core/sock.c:2559 __netlink_diag_dump+0x45c/0x790 net/netlink/diag.c:171 netlink_diag_dump+0xd6/0x230 net/netlink/diag.c:207 netlink_dump+0x570/0xc50 net/netlink/af_netlink.c:2269 __netlink_dump_start+0x64b/0x910 net/netlink/af_netlink.c:2374 netlink_dump_start include/linux/netlink.h:329 [inline] netlink_diag_handler_dump+0x1ae/0x250 net/netlink/diag.c:238 __sock_diag_cmd net/core/sock_diag.c:238 [inline] sock_diag_rcv_msg+0x31e/0x440 net/core/sock_diag.c:269 netlink_rcv_skb+0x165/0x440 net/netlink/af_netlink.c:2547 sock_diag_rcv+0x2a/0x40 net/core/sock_diag.c:280 netlink_unicast_kernel net/netlink/af_netlink.c:1339 [inline] netlink_unicast+0x547/0x7f0 net/netlink/af_netlink.c:1365 netlink_sendmsg+0x925/0xe30 net/netlink/af_netlink.c:1914 sock_sendmsg_nosec net/socket.c:724 [inline] sock_sendmsg+0xde/0x190 net/socket.c:747 ____sys_sendmsg+0x71c/0x900 net/socket.c:2503 ___sys_sendmsg+0x110/0x1b0 net/socket.c:2557 __sys_sendmsg+0xf7/0x1c0 net/socket.c:2586 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd RIP: 0033:0x7f5303aaabb9 Code: 28 c3 e8 2a 14 00 00 66 2e 0f 1f 84 00 00 00 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 c0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffc7506e548 EFLAGS: 00000246 ORIG_RAX: 000000000000002e RAX: ffffffffffffffda RBX: 0000000000000000 RCX: 00007f5303aaabb9 RDX: 0000000000000000 RSI: 0000000020000180 RDI: 0000000000000003 RBP: 00007f5303a6ed60 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 00007f5303a6edf0 R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000 </TASK> Fixes:8d61f926d4
("netlink: fix potential deadlock in netlink_set_err()") Reported-by: syzbot+5da61cf6a9bc1902d422@syzkaller.appspotmail.com Link: https://syzkaller.appspot.com/bug?extid=5da61cf6a9bc1902d422 Suggested-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Reviewed-by: Eric Dumazet <edumazet@google.com> Link: https://lore.kernel.org/r/20230626164313.52528-1-kuniyu@amazon.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
261 lines
5.4 KiB
C
261 lines
5.4 KiB
C
#include <linux/module.h>
|
|
|
|
#include <net/sock.h>
|
|
#include <linux/netlink.h>
|
|
#include <linux/sock_diag.h>
|
|
#include <linux/netlink_diag.h>
|
|
#include <linux/rhashtable.h>
|
|
|
|
#include "af_netlink.h"
|
|
|
|
static int sk_diag_dump_groups(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
if (nlk->groups == NULL)
|
|
return 0;
|
|
|
|
return nla_put(nlskb, NETLINK_DIAG_GROUPS, NLGRPSZ(nlk->ngroups),
|
|
nlk->groups);
|
|
}
|
|
|
|
static int sk_diag_put_flags(struct sock *sk, struct sk_buff *skb)
|
|
{
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
u32 flags = 0;
|
|
|
|
if (nlk->cb_running)
|
|
flags |= NDIAG_FLAG_CB_RUNNING;
|
|
if (nlk->flags & NETLINK_F_RECV_PKTINFO)
|
|
flags |= NDIAG_FLAG_PKTINFO;
|
|
if (nlk->flags & NETLINK_F_BROADCAST_SEND_ERROR)
|
|
flags |= NDIAG_FLAG_BROADCAST_ERROR;
|
|
if (nlk->flags & NETLINK_F_RECV_NO_ENOBUFS)
|
|
flags |= NDIAG_FLAG_NO_ENOBUFS;
|
|
if (nlk->flags & NETLINK_F_LISTEN_ALL_NSID)
|
|
flags |= NDIAG_FLAG_LISTEN_ALL_NSID;
|
|
if (nlk->flags & NETLINK_F_CAP_ACK)
|
|
flags |= NDIAG_FLAG_CAP_ACK;
|
|
|
|
return nla_put_u32(skb, NETLINK_DIAG_FLAGS, flags);
|
|
}
|
|
|
|
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
|
|
struct netlink_diag_req *req,
|
|
u32 portid, u32 seq, u32 flags, int sk_ino)
|
|
{
|
|
struct nlmsghdr *nlh;
|
|
struct netlink_diag_msg *rep;
|
|
struct netlink_sock *nlk = nlk_sk(sk);
|
|
|
|
nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
|
|
flags);
|
|
if (!nlh)
|
|
return -EMSGSIZE;
|
|
|
|
rep = nlmsg_data(nlh);
|
|
rep->ndiag_family = AF_NETLINK;
|
|
rep->ndiag_type = sk->sk_type;
|
|
rep->ndiag_protocol = sk->sk_protocol;
|
|
rep->ndiag_state = sk->sk_state;
|
|
|
|
rep->ndiag_ino = sk_ino;
|
|
rep->ndiag_portid = nlk->portid;
|
|
rep->ndiag_dst_portid = nlk->dst_portid;
|
|
rep->ndiag_dst_group = nlk->dst_group;
|
|
sock_diag_save_cookie(sk, rep->ndiag_cookie);
|
|
|
|
if ((req->ndiag_show & NDIAG_SHOW_GROUPS) &&
|
|
sk_diag_dump_groups(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->ndiag_show & NDIAG_SHOW_MEMINFO) &&
|
|
sock_diag_put_meminfo(sk, skb, NETLINK_DIAG_MEMINFO))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->ndiag_show & NDIAG_SHOW_FLAGS) &&
|
|
sk_diag_put_flags(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
nlmsg_end(skb, nlh);
|
|
return 0;
|
|
|
|
out_nlmsg_trim:
|
|
nlmsg_cancel(skb, nlh);
|
|
return -EMSGSIZE;
|
|
}
|
|
|
|
static int __netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
|
|
int protocol, int s_num)
|
|
{
|
|
struct rhashtable_iter *hti = (void *)cb->args[2];
|
|
struct netlink_table *tbl = &nl_table[protocol];
|
|
struct net *net = sock_net(skb->sk);
|
|
struct netlink_diag_req *req;
|
|
struct netlink_sock *nlsk;
|
|
unsigned long flags;
|
|
struct sock *sk;
|
|
int num = 2;
|
|
int ret = 0;
|
|
|
|
req = nlmsg_data(cb->nlh);
|
|
|
|
if (s_num > 1)
|
|
goto mc_list;
|
|
|
|
num--;
|
|
|
|
if (!hti) {
|
|
hti = kmalloc(sizeof(*hti), GFP_KERNEL);
|
|
if (!hti)
|
|
return -ENOMEM;
|
|
|
|
cb->args[2] = (long)hti;
|
|
}
|
|
|
|
if (!s_num)
|
|
rhashtable_walk_enter(&tbl->hash, hti);
|
|
|
|
rhashtable_walk_start(hti);
|
|
|
|
while ((nlsk = rhashtable_walk_next(hti))) {
|
|
if (IS_ERR(nlsk)) {
|
|
ret = PTR_ERR(nlsk);
|
|
if (ret == -EAGAIN) {
|
|
ret = 0;
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
|
|
sk = (struct sock *)nlsk;
|
|
|
|
if (!net_eq(sock_net(sk), net))
|
|
continue;
|
|
|
|
if (sk_diag_fill(sk, skb, req,
|
|
NETLINK_CB(cb->skb).portid,
|
|
cb->nlh->nlmsg_seq,
|
|
NLM_F_MULTI,
|
|
sock_i_ino(sk)) < 0) {
|
|
ret = 1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
rhashtable_walk_stop(hti);
|
|
|
|
if (ret)
|
|
goto done;
|
|
|
|
rhashtable_walk_exit(hti);
|
|
num++;
|
|
|
|
mc_list:
|
|
read_lock_irqsave(&nl_table_lock, flags);
|
|
sk_for_each_bound(sk, &tbl->mc_list) {
|
|
if (sk_hashed(sk))
|
|
continue;
|
|
if (!net_eq(sock_net(sk), net))
|
|
continue;
|
|
if (num < s_num) {
|
|
num++;
|
|
continue;
|
|
}
|
|
|
|
if (sk_diag_fill(sk, skb, req,
|
|
NETLINK_CB(cb->skb).portid,
|
|
cb->nlh->nlmsg_seq,
|
|
NLM_F_MULTI,
|
|
__sock_i_ino(sk)) < 0) {
|
|
ret = 1;
|
|
break;
|
|
}
|
|
num++;
|
|
}
|
|
read_unlock_irqrestore(&nl_table_lock, flags);
|
|
|
|
done:
|
|
cb->args[0] = num;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int netlink_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
|
|
{
|
|
struct netlink_diag_req *req;
|
|
int s_num = cb->args[0];
|
|
int err = 0;
|
|
|
|
req = nlmsg_data(cb->nlh);
|
|
|
|
if (req->sdiag_protocol == NDIAG_PROTO_ALL) {
|
|
int i;
|
|
|
|
for (i = cb->args[1]; i < MAX_LINKS; i++) {
|
|
err = __netlink_diag_dump(skb, cb, i, s_num);
|
|
if (err)
|
|
break;
|
|
s_num = 0;
|
|
}
|
|
cb->args[1] = i;
|
|
} else {
|
|
if (req->sdiag_protocol >= MAX_LINKS)
|
|
return -ENOENT;
|
|
|
|
err = __netlink_diag_dump(skb, cb, req->sdiag_protocol, s_num);
|
|
}
|
|
|
|
return err < 0 ? err : skb->len;
|
|
}
|
|
|
|
static int netlink_diag_dump_done(struct netlink_callback *cb)
|
|
{
|
|
struct rhashtable_iter *hti = (void *)cb->args[2];
|
|
|
|
if (cb->args[0] == 1)
|
|
rhashtable_walk_exit(hti);
|
|
|
|
kfree(hti);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int netlink_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
|
|
{
|
|
int hdrlen = sizeof(struct netlink_diag_req);
|
|
struct net *net = sock_net(skb->sk);
|
|
|
|
if (nlmsg_len(h) < hdrlen)
|
|
return -EINVAL;
|
|
|
|
if (h->nlmsg_flags & NLM_F_DUMP) {
|
|
struct netlink_dump_control c = {
|
|
.dump = netlink_diag_dump,
|
|
.done = netlink_diag_dump_done,
|
|
};
|
|
return netlink_dump_start(net->diag_nlsk, skb, h, &c);
|
|
} else
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
static const struct sock_diag_handler netlink_diag_handler = {
|
|
.family = AF_NETLINK,
|
|
.dump = netlink_diag_handler_dump,
|
|
};
|
|
|
|
static int __init netlink_diag_init(void)
|
|
{
|
|
return sock_diag_register(&netlink_diag_handler);
|
|
}
|
|
|
|
static void __exit netlink_diag_exit(void)
|
|
{
|
|
sock_diag_unregister(&netlink_diag_handler);
|
|
}
|
|
|
|
module_init(netlink_diag_init);
|
|
module_exit(netlink_diag_exit);
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 16 /* AF_NETLINK */);
|