mirror of
https://mirrors.bfsu.edu.cn/git/linux.git
synced 2024-11-27 22:24:11 +08:00
b3abe42e94
Wei Chen reported a NULL deref in sk_user_ns() [0][1], and Paolo diagnosed
the root cause: in unix_diag_get_exact(), the newly allocated skb does not
have sk. [2]
We must get the user_ns from the NETLINK_CB(in_skb).sk and pass it to
sk_diag_fill().
[0]:
BUG: kernel NULL pointer dereference, address: 0000000000000270
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
PGD 12bbce067 P4D 12bbce067 PUD 12bc40067 PMD 0
Oops: 0000 [#1] PREEMPT SMP
CPU: 0 PID: 27942 Comm: syz-executor.0 Not tainted 6.1.0-rc5-next-20221118 #2
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
rel-1.13.0-48-gd9c812dda519-prebuilt.qemu.org 04/01/2014
RIP: 0010:sk_user_ns include/net/sock.h:920 [inline]
RIP: 0010:sk_diag_dump_uid net/unix/diag.c:119 [inline]
RIP: 0010:sk_diag_fill+0x77d/0x890 net/unix/diag.c:170
Code: 89 ef e8 66 d4 2d fd c7 44 24 40 00 00 00 00 49 8d 7c 24 18 e8
54 d7 2d fd 49 8b 5c 24 18 48 8d bb 70 02 00 00 e8 43 d7 2d fd <48> 8b
9b 70 02 00 00 48 8d 7b 10 e8 33 d7 2d fd 48 8b 5b 10 48 8d
RSP: 0018:ffffc90000d67968 EFLAGS: 00010246
RAX: ffff88812badaa48 RBX: 0000000000000000 RCX: ffffffff840d481d
RDX: 0000000000000465 RSI: 0000000000000000 RDI: 0000000000000270
RBP: ffffc90000d679a8 R08: 0000000000000277 R09: 0000000000000000
R10: 0001ffffffffffff R11: 0001c90000d679a8 R12: ffff88812ac03800
R13: ffff88812c87c400 R14: ffff88812ae42210 R15: ffff888103026940
FS: 00007f08b4e6f700(0000) GS:ffff88813bc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000000000270 CR3: 000000012c58b000 CR4: 00000000003506f0
DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000
DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400
Call Trace:
<TASK>
unix_diag_get_exact net/unix/diag.c:285 [inline]
unix_diag_handler_dump+0x3f9/0x500 net/unix/diag.c:317
__sock_diag_cmd net/core/sock_diag.c:235 [inline]
sock_diag_rcv_msg+0x237/0x250 net/core/sock_diag.c:266
netlink_rcv_skb+0x13e/0x250 net/netlink/af_netlink.c:2564
sock_diag_rcv+0x24/0x40 net/core/sock_diag.c:277
netlink_unicast_kernel net/netlink/af_netlink.c:1330 [inline]
netlink_unicast+0x5e9/0x6b0 net/netlink/af_netlink.c:1356
netlink_sendmsg+0x739/0x860 net/netlink/af_netlink.c:1932
sock_sendmsg_nosec net/socket.c:714 [inline]
sock_sendmsg net/socket.c:734 [inline]
____sys_sendmsg+0x38f/0x500 net/socket.c:2476
___sys_sendmsg net/socket.c:2530 [inline]
__sys_sendmsg+0x197/0x230 net/socket.c:2559
__do_sys_sendmsg net/socket.c:2568 [inline]
__se_sys_sendmsg net/socket.c:2566 [inline]
__x64_sys_sendmsg+0x42/0x50 net/socket.c:2566
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x2b/0x70 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x4697f9
Code: f7 d8 64 89 02 b8 ff ff ff ff c3 66 0f 1f 44 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 bc ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f08b4e6ec48 EFLAGS: 00000246 ORIG_RAX: 000000000000002e
RAX: ffffffffffffffda RBX: 000000000077bf80 RCX: 00000000004697f9
RDX: 0000000000000000 RSI: 00000000200001c0 RDI: 0000000000000003
RBP: 00000000004d29e9 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 000000000077bf80
R13: 0000000000000000 R14: 000000000077bf80 R15: 00007ffdb36bc6c0
</TASK>
Modules linked in:
CR2: 0000000000000270
[1]: https://lore.kernel.org/netdev/CAO4mrfdvyjFpokhNsiwZiP-wpdSD0AStcJwfKcKQdAALQ9_2Qw@mail.gmail.com/
[2]: https://lore.kernel.org/netdev/e04315e7c90d9a75613f3993c2baf2d344eef7eb.camel@redhat.com/
Fixes: cae9910e73
("net: Add UNIX_DIAG_UID to Netlink UNIX socket diagnostics.")
Reported-by: syzbot <syzkaller@googlegroups.com>
Reported-by: Wei Chen <harperchen1110@gmail.com>
Diagnosed-by: Paolo Abeni <pabeni@redhat.com>
Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
343 lines
7.6 KiB
C
343 lines
7.6 KiB
C
// 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 a hash 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 - offsetof(struct sockaddr_un, sun_path),
|
|
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);
|
|
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 net *net = sock_net(skb->sk);
|
|
int num, s_num, slot, s_slot;
|
|
struct unix_diag_req *req;
|
|
|
|
req = nlmsg_data(cb->nlh);
|
|
|
|
s_slot = cb->args[0];
|
|
num = s_num = cb->args[1];
|
|
|
|
for (slot = s_slot; slot < UNIX_HASH_SIZE; s_num = 0, slot++) {
|
|
struct sock *sk;
|
|
|
|
num = 0;
|
|
spin_lock(&net->unx.table.locks[slot]);
|
|
sk_for_each(sk, &net->unx.table.buckets[slot]) {
|
|
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) {
|
|
spin_unlock(&net->unx.table.locks[slot]);
|
|
goto done;
|
|
}
|
|
next:
|
|
num++;
|
|
}
|
|
spin_unlock(&net->unx.table.locks[slot]);
|
|
}
|
|
done:
|
|
cb->args[0] = slot;
|
|
cb->args[1] = num;
|
|
|
|
return skb->len;
|
|
}
|
|
|
|
static struct sock *unix_lookup_by_ino(struct net *net, unsigned int ino)
|
|
{
|
|
struct sock *sk;
|
|
int i;
|
|
|
|
for (i = 0; i < UNIX_HASH_SIZE; i++) {
|
|
spin_lock(&net->unx.table.locks[i]);
|
|
sk_for_each(sk, &net->unx.table.buckets[i]) {
|
|
if (ino == sock_i_ino(sk)) {
|
|
sock_hold(sk);
|
|
spin_unlock(&net->unx.table.locks[i]);
|
|
return sk;
|
|
}
|
|
}
|
|
spin_unlock(&net->unx.table.locks[i]);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static int unix_diag_get_exact(struct sk_buff *in_skb,
|
|
const struct nlmsghdr *nlh,
|
|
struct unix_diag_req *req)
|
|
{
|
|
struct net *net = sock_net(in_skb->sk);
|
|
unsigned int extra_len;
|
|
struct sk_buff *rep;
|
|
struct sock *sk;
|
|
int err;
|
|
|
|
err = -EINVAL;
|
|
if (req->udiag_ino == 0)
|
|
goto out_nosk;
|
|
|
|
sk = unix_lookup_by_ino(net, req->udiag_ino);
|
|
err = -ENOENT;
|
|
if (sk == NULL)
|
|
goto out_nosk;
|
|
|
|
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 = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
|
|
|
|
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);
|
|
|
|
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(sock_net(skb->sk)->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 */);
|