1ea80b9eb4
[ Upstream commit 4d322dce82a1d44f8c83f0f54f95dd1b8dcf46c9 ] syzbot reported a lockdep splat [1]. Blamed commit hinted about the possible lockdep violation, and code used unix_state_lock_nested() in an attempt to silence lockdep. It is not sufficient, because unix_state_lock_nested() is already used from unix_state_double_lock(). We need to use a separate subclass. This patch adds a distinct enumeration to make things more explicit. Also use swap() in unix_state_double_lock() as a clean up. v2: add a missing inline keyword to unix_state_lock_nested() [1] WARNING: possible circular locking dependency detected 6.8.0-rc1-syzkaller-00356-g8a696a29c690 #0 Not tainted syz-executor.1/2542 is trying to acquire lock: ffff88808b5df9e8 (rlock-AF_UNIX){+.+.}-{2:2}, at: skb_queue_tail+0x36/0x120 net/core/skbuff.c:3863 but task is already holding lock: ffff88808b5dfe70 (&u->lock/1){+.+.}-{2:2}, at: unix_dgram_sendmsg+0xfc7/0x2200 net/unix/af_unix.c:2089 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (&u->lock/1){+.+.}-{2:2}: lock_acquire+0x1e3/0x530 kernel/locking/lockdep.c:5754 _raw_spin_lock_nested+0x31/0x40 kernel/locking/spinlock.c:378 sk_diag_dump_icons net/unix/diag.c:87 [inline] sk_diag_fill+0x6ea/0xfe0 net/unix/diag.c:157 sk_diag_dump net/unix/diag.c:196 [inline] unix_diag_dump+0x3e9/0x630 net/unix/diag.c:220 netlink_dump+0x5c1/0xcd0 net/netlink/af_netlink.c:2264 __netlink_dump_start+0x5d7/0x780 net/netlink/af_netlink.c:2370 netlink_dump_start include/linux/netlink.h:338 [inline] unix_diag_handler_dump+0x1c3/0x8f0 net/unix/diag.c:319 sock_diag_rcv_msg+0xe3/0x400 netlink_rcv_skb+0x1df/0x430 net/netlink/af_netlink.c:2543 sock_diag_rcv+0x2a/0x40 net/core/sock_diag.c:280 netlink_unicast_kernel net/netlink/af_netlink.c:1341 [inline] netlink_unicast+0x7e6/0x980 net/netlink/af_netlink.c:1367 netlink_sendmsg+0xa37/0xd70 net/netlink/af_netlink.c:1908 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg net/socket.c:745 [inline] sock_write_iter+0x39a/0x520 net/socket.c:1160 call_write_iter include/linux/fs.h:2085 [inline] new_sync_write fs/read_write.c:497 [inline] vfs_write+0xa74/0xca0 fs/read_write.c:590 ksys_write+0x1a0/0x2c0 fs/read_write.c:643 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf5/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b -> #0 (rlock-AF_UNIX){+.+.}-{2:2}: check_prev_add kernel/locking/lockdep.c:3134 [inline] check_prevs_add kernel/locking/lockdep.c:3253 [inline] validate_chain+0x1909/0x5ab0 kernel/locking/lockdep.c:3869 __lock_acquire+0x1345/0x1fd0 kernel/locking/lockdep.c:5137 lock_acquire+0x1e3/0x530 kernel/locking/lockdep.c:5754 __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline] _raw_spin_lock_irqsave+0xd5/0x120 kernel/locking/spinlock.c:162 skb_queue_tail+0x36/0x120 net/core/skbuff.c:3863 unix_dgram_sendmsg+0x15d9/0x2200 net/unix/af_unix.c:2112 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg net/socket.c:745 [inline] ____sys_sendmsg+0x592/0x890 net/socket.c:2584 ___sys_sendmsg net/socket.c:2638 [inline] __sys_sendmmsg+0x3b2/0x730 net/socket.c:2724 __do_sys_sendmmsg net/socket.c:2753 [inline] __se_sys_sendmmsg net/socket.c:2750 [inline] __x64_sys_sendmmsg+0xa0/0xb0 net/socket.c:2750 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf5/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&u->lock/1); lock(rlock-AF_UNIX); lock(&u->lock/1); lock(rlock-AF_UNIX); *** DEADLOCK *** 1 lock held by syz-executor.1/2542: #0: ffff88808b5dfe70 (&u->lock/1){+.+.}-{2:2}, at: unix_dgram_sendmsg+0xfc7/0x2200 net/unix/af_unix.c:2089 stack backtrace: CPU: 1 PID: 2542 Comm: syz-executor.1 Not tainted 6.8.0-rc1-syzkaller-00356-g8a696a29c690 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 11/17/2023 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0x1e7/0x2d0 lib/dump_stack.c:106 check_noncircular+0x366/0x490 kernel/locking/lockdep.c:2187 check_prev_add kernel/locking/lockdep.c:3134 [inline] check_prevs_add kernel/locking/lockdep.c:3253 [inline] validate_chain+0x1909/0x5ab0 kernel/locking/lockdep.c:3869 __lock_acquire+0x1345/0x1fd0 kernel/locking/lockdep.c:5137 lock_acquire+0x1e3/0x530 kernel/locking/lockdep.c:5754 __raw_spin_lock_irqsave include/linux/spinlock_api_smp.h:110 [inline] _raw_spin_lock_irqsave+0xd5/0x120 kernel/locking/spinlock.c:162 skb_queue_tail+0x36/0x120 net/core/skbuff.c:3863 unix_dgram_sendmsg+0x15d9/0x2200 net/unix/af_unix.c:2112 sock_sendmsg_nosec net/socket.c:730 [inline] __sock_sendmsg net/socket.c:745 [inline] ____sys_sendmsg+0x592/0x890 net/socket.c:2584 ___sys_sendmsg net/socket.c:2638 [inline] __sys_sendmmsg+0x3b2/0x730 net/socket.c:2724 __do_sys_sendmmsg net/socket.c:2753 [inline] __se_sys_sendmmsg net/socket.c:2750 [inline] __x64_sys_sendmmsg+0xa0/0xb0 net/socket.c:2750 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf5/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x63/0x6b RIP: 0033:0x7f26d887cda9 Code: 28 00 00 00 75 05 48 83 c4 28 c3 e8 e1 20 00 00 90 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 b0 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f26d95a60c8 EFLAGS: 00000246 ORIG_RAX: 0000000000000133 RAX: ffffffffffffffda RBX: 00007f26d89abf80 RCX: 00007f26d887cda9 RDX: 000000000000003e RSI: 00000000200bd000 RDI: 0000000000000004 RBP: 00007f26d88c947a R08: 0000000000000000 R09: 0000000000000000 R10: 00000000000008c0 R11: 0000000000000246 R12: 0000000000000000 R13: 000000000000000b R14: 00007f26d89abf80 R15: 00007ffcfe081a68 Fixes: 2aac7a2cb0d9 ("unix_diag: Pending connections IDs NLA") Reported-by: syzbot <syzkaller@googlegroups.com> Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20240130184235.1620738-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
348 lines
7.7 KiB
C
Executable file
348 lines
7.7 KiB
C
Executable file
// SPDX-License-Identifier: GPL-2.0-only
|
|
#include <linux/types.h>
|
|
#include <linux/spinlock.h>
|
|
#include <linux/sock_diag.h>
|
|
#include <linux/unix_diag.h>
|
|
#include <linux/skbuff.h>
|
|
#include <linux/module.h>
|
|
#include <linux/uidgid.h>
|
|
#include <net/netlink.h>
|
|
#include <net/af_unix.h>
|
|
#include <net/tcp_states.h>
|
|
#include <net/sock.h>
|
|
|
|
static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
/* might or might not have unix_table_lock */
|
|
struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
|
|
|
|
if (!addr)
|
|
return 0;
|
|
|
|
return nla_put(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short),
|
|
addr->name->sun_path);
|
|
}
|
|
|
|
static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
struct dentry *dentry = unix_sk(sk)->path.dentry;
|
|
|
|
if (dentry) {
|
|
struct unix_diag_vfs uv = {
|
|
.udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
|
|
.udiag_vfs_dev = dentry->d_sb->s_dev,
|
|
};
|
|
|
|
return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int sk_diag_dump_peer(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
struct sock *peer;
|
|
int ino;
|
|
|
|
peer = unix_peer_get(sk);
|
|
if (peer) {
|
|
unix_state_lock(peer);
|
|
ino = sock_i_ino(peer);
|
|
unix_state_unlock(peer);
|
|
sock_put(peer);
|
|
|
|
return nla_put_u32(nlskb, UNIX_DIAG_PEER, ino);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
struct sk_buff *skb;
|
|
struct nlattr *attr;
|
|
u32 *buf;
|
|
int i;
|
|
|
|
if (sk->sk_state == TCP_LISTEN) {
|
|
spin_lock(&sk->sk_receive_queue.lock);
|
|
|
|
attr = nla_reserve(nlskb, UNIX_DIAG_ICONS,
|
|
sk->sk_receive_queue.qlen * sizeof(u32));
|
|
if (!attr)
|
|
goto errout;
|
|
|
|
buf = nla_data(attr);
|
|
i = 0;
|
|
skb_queue_walk(&sk->sk_receive_queue, skb) {
|
|
struct sock *req, *peer;
|
|
|
|
req = skb->sk;
|
|
/*
|
|
* The state lock is outer for the same sk's
|
|
* queue lock. With the other's queue locked it's
|
|
* OK to lock the state.
|
|
*/
|
|
unix_state_lock_nested(req, U_LOCK_DIAG);
|
|
peer = unix_sk(req)->peer;
|
|
buf[i++] = (peer ? sock_i_ino(peer) : 0);
|
|
unix_state_unlock(req);
|
|
}
|
|
spin_unlock(&sk->sk_receive_queue.lock);
|
|
}
|
|
|
|
return 0;
|
|
|
|
errout:
|
|
spin_unlock(&sk->sk_receive_queue.lock);
|
|
return -EMSGSIZE;
|
|
}
|
|
|
|
static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
|
|
{
|
|
struct unix_diag_rqlen rql;
|
|
|
|
if (sk->sk_state == TCP_LISTEN) {
|
|
rql.udiag_rqueue = sk->sk_receive_queue.qlen;
|
|
rql.udiag_wqueue = sk->sk_max_ack_backlog;
|
|
} else {
|
|
rql.udiag_rqueue = (u32) unix_inq_len(sk);
|
|
rql.udiag_wqueue = (u32) unix_outq_len(sk);
|
|
}
|
|
|
|
return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
|
|
}
|
|
|
|
static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb,
|
|
struct user_namespace *user_ns)
|
|
{
|
|
uid_t uid = from_kuid_munged(user_ns, sock_i_uid(sk));
|
|
return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
|
|
}
|
|
|
|
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
|
|
struct user_namespace *user_ns,
|
|
u32 portid, u32 seq, u32 flags, int sk_ino)
|
|
{
|
|
struct nlmsghdr *nlh;
|
|
struct unix_diag_msg *rep;
|
|
|
|
nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
|
|
flags);
|
|
if (!nlh)
|
|
return -EMSGSIZE;
|
|
|
|
rep = nlmsg_data(nlh);
|
|
rep->udiag_family = AF_UNIX;
|
|
rep->udiag_type = sk->sk_type;
|
|
rep->udiag_state = sk->sk_state;
|
|
rep->pad = 0;
|
|
rep->udiag_ino = sk_ino;
|
|
sock_diag_save_cookie(sk, rep->udiag_cookie);
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_NAME) &&
|
|
sk_diag_dump_name(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_VFS) &&
|
|
sk_diag_dump_vfs(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_PEER) &&
|
|
sk_diag_dump_peer(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
|
|
sk_diag_dump_icons(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_RQLEN) &&
|
|
sk_diag_show_rqlen(sk, skb))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_MEMINFO) &&
|
|
sock_diag_put_meminfo(sk, skb, UNIX_DIAG_MEMINFO))
|
|
goto out_nlmsg_trim;
|
|
|
|
if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
|
|
goto out_nlmsg_trim;
|
|
|
|
if ((req->udiag_show & UDIAG_SHOW_UID) &&
|
|
sk_diag_dump_uid(sk, skb, user_ns))
|
|
goto out_nlmsg_trim;
|
|
|
|
nlmsg_end(skb, nlh);
|
|
return 0;
|
|
|
|
out_nlmsg_trim:
|
|
nlmsg_cancel(skb, nlh);
|
|
return -EMSGSIZE;
|
|
}
|
|
|
|
static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
|
|
struct user_namespace *user_ns,
|
|
u32 portid, u32 seq, u32 flags)
|
|
{
|
|
int sk_ino;
|
|
|
|
unix_state_lock(sk);
|
|
sk_ino = sock_i_ino(sk);
|
|
unix_state_unlock(sk);
|
|
|
|
if (!sk_ino)
|
|
return 0;
|
|
|
|
return sk_diag_fill(sk, skb, req, user_ns, portid, seq, flags, sk_ino);
|
|
}
|
|
|
|
static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
|
|
{
|
|
struct unix_diag_req *req;
|
|
int num, s_num, slot, s_slot;
|
|
struct net *net = sock_net(skb->sk);
|
|
|
|
req = nlmsg_data(cb->nlh);
|
|
|
|
s_slot = cb->args[0];
|
|
num = s_num = cb->args[1];
|
|
|
|
spin_lock(&unix_table_lock);
|
|
for (slot = s_slot;
|
|
slot < ARRAY_SIZE(unix_socket_table);
|
|
s_num = 0, slot++) {
|
|
struct sock *sk;
|
|
|
|
num = 0;
|
|
sk_for_each(sk, &unix_socket_table[slot]) {
|
|
if (!net_eq(sock_net(sk), net))
|
|
continue;
|
|
if (num < s_num)
|
|
goto next;
|
|
if (!(req->udiag_states & (1 << sk->sk_state)))
|
|
goto next;
|
|
if (sk_diag_dump(sk, skb, req, sk_user_ns(skb->sk),
|
|
NETLINK_CB(cb->skb).portid,
|
|
cb->nlh->nlmsg_seq,
|
|
NLM_F_MULTI) < 0)
|
|
goto done;
|
|
next:
|
|
num++;
|
|
}
|
|
}
|
|
done:
|
|
spin_unlock(&unix_table_lock);
|
|
cb->args[0] = slot;
|
|
cb->args[1] = num;
|
|
|
|
return skb->len;
|
|
}
|
|
|
|
static struct sock *unix_lookup_by_ino(unsigned int ino)
|
|
{
|
|
int i;
|
|
struct sock *sk;
|
|
|
|
spin_lock(&unix_table_lock);
|
|
for (i = 0; i < ARRAY_SIZE(unix_socket_table); i++) {
|
|
sk_for_each(sk, &unix_socket_table[i])
|
|
if (ino == sock_i_ino(sk)) {
|
|
sock_hold(sk);
|
|
spin_unlock(&unix_table_lock);
|
|
|
|
return sk;
|
|
}
|
|
}
|
|
|
|
spin_unlock(&unix_table_lock);
|
|
return NULL;
|
|
}
|
|
|
|
static int unix_diag_get_exact(struct sk_buff *in_skb,
|
|
const struct nlmsghdr *nlh,
|
|
struct unix_diag_req *req)
|
|
{
|
|
int err = -EINVAL;
|
|
struct sock *sk;
|
|
struct sk_buff *rep;
|
|
unsigned int extra_len;
|
|
struct net *net = sock_net(in_skb->sk);
|
|
|
|
if (req->udiag_ino == 0)
|
|
goto out_nosk;
|
|
|
|
sk = unix_lookup_by_ino(req->udiag_ino);
|
|
err = -ENOENT;
|
|
if (sk == NULL)
|
|
goto out_nosk;
|
|
if (!net_eq(sock_net(sk), net))
|
|
goto out;
|
|
|
|
err = sock_diag_check_cookie(sk, req->udiag_cookie);
|
|
if (err)
|
|
goto out;
|
|
|
|
extra_len = 256;
|
|
again:
|
|
err = -ENOMEM;
|
|
rep = nlmsg_new(sizeof(struct unix_diag_msg) + extra_len, GFP_KERNEL);
|
|
if (!rep)
|
|
goto out;
|
|
|
|
err = sk_diag_fill(sk, rep, req, sk_user_ns(NETLINK_CB(in_skb).sk),
|
|
NETLINK_CB(in_skb).portid,
|
|
nlh->nlmsg_seq, 0, req->udiag_ino);
|
|
if (err < 0) {
|
|
nlmsg_free(rep);
|
|
extra_len += 256;
|
|
if (extra_len >= PAGE_SIZE)
|
|
goto out;
|
|
|
|
goto again;
|
|
}
|
|
err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
|
|
MSG_DONTWAIT);
|
|
if (err > 0)
|
|
err = 0;
|
|
out:
|
|
if (sk)
|
|
sock_put(sk);
|
|
out_nosk:
|
|
return err;
|
|
}
|
|
|
|
static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
|
|
{
|
|
int hdrlen = sizeof(struct unix_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 = unix_diag_dump,
|
|
};
|
|
return netlink_dump_start(net->diag_nlsk, skb, h, &c);
|
|
} else
|
|
return unix_diag_get_exact(skb, h, nlmsg_data(h));
|
|
}
|
|
|
|
static const struct sock_diag_handler unix_diag_handler = {
|
|
.family = AF_UNIX,
|
|
.dump = unix_diag_handler_dump,
|
|
};
|
|
|
|
static int __init unix_diag_init(void)
|
|
{
|
|
return sock_diag_register(&unix_diag_handler);
|
|
}
|
|
|
|
static void __exit unix_diag_exit(void)
|
|
{
|
|
sock_diag_unregister(&unix_diag_handler);
|
|
}
|
|
|
|
module_init(unix_diag_init);
|
|
module_exit(unix_diag_exit);
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 1 /* AF_LOCAL */);
|