diag.c 7.5 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
P
Pavel Emelyanov 已提交
2 3 4 5 6
#include <linux/types.h>
#include <linux/spinlock.h>
#include <linux/sock_diag.h>
#include <linux/unix_diag.h>
#include <linux/skbuff.h>
7
#include <linux/module.h>
8
#include <linux/uidgid.h>
P
Pavel Emelyanov 已提交
9 10 11
#include <net/netlink.h>
#include <net/af_unix.h>
#include <net/tcp_states.h>
12
#include <net/sock.h>
P
Pavel Emelyanov 已提交
13

P
Pavel Emelyanov 已提交
14 15
static int sk_diag_dump_name(struct sock *sk, struct sk_buff *nlskb)
{
16 17
	/* might or might not have unix_table_lock */
	struct unix_address *addr = smp_load_acquire(&unix_sk(sk)->addr);
P
Pavel Emelyanov 已提交
18

19 20
	if (!addr)
		return 0;
P
Pavel Emelyanov 已提交
21

22 23
	return nla_put(nlskb, UNIX_DIAG_NAME, addr->len - sizeof(short),
		       addr->name->sun_path);
P
Pavel Emelyanov 已提交
24 25
}

P
Pavel Emelyanov 已提交
26 27
static int sk_diag_dump_vfs(struct sock *sk, struct sk_buff *nlskb)
{
A
Al Viro 已提交
28
	struct dentry *dentry = unix_sk(sk)->path.dentry;
P
Pavel Emelyanov 已提交
29 30

	if (dentry) {
31
		struct unix_diag_vfs uv = {
32
			.udiag_vfs_ino = d_backing_inode(dentry)->i_ino,
33 34 35 36
			.udiag_vfs_dev = dentry->d_sb->s_dev,
		};

		return nla_put(nlskb, UNIX_DIAG_VFS, sizeof(uv), &uv);
P
Pavel Emelyanov 已提交
37 38 39 40 41
	}

	return 0;
}

P
Pavel Emelyanov 已提交
42 43 44 45 46 47 48 49 50 51 52 53
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);

54
		return nla_put_u32(nlskb, UNIX_DIAG_PEER, ino);
P
Pavel Emelyanov 已提交
55 56 57 58 59
	}

	return 0;
}

60 61 62
static int sk_diag_dump_icons(struct sock *sk, struct sk_buff *nlskb)
{
	struct sk_buff *skb;
63
	struct nlattr *attr;
64 65 66 67 68
	u32 *buf;
	int i;

	if (sk->sk_state == TCP_LISTEN) {
		spin_lock(&sk->sk_receive_queue.lock);
69 70 71 72 73 74 75

		attr = nla_reserve(nlskb, UNIX_DIAG_ICONS,
				   sk->sk_receive_queue.qlen * sizeof(u32));
		if (!attr)
			goto errout;

		buf = nla_data(attr);
76 77 78 79 80 81 82 83 84 85 86 87
		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;
88
			buf[i++] = (peer ? sock_i_ino(peer) : 0);
89 90 91 92 93 94 95
			unix_state_unlock(req);
		}
		spin_unlock(&sk->sk_receive_queue.lock);
	}

	return 0;

96
errout:
97 98 99 100
	spin_unlock(&sk->sk_receive_queue.lock);
	return -EMSGSIZE;
}

101 102
static int sk_diag_show_rqlen(struct sock *sk, struct sk_buff *nlskb)
{
103
	struct unix_diag_rqlen rql;
104 105

	if (sk->sk_state == TCP_LISTEN) {
106 107
		rql.udiag_rqueue = sk->sk_receive_queue.qlen;
		rql.udiag_wqueue = sk->sk_max_ack_backlog;
108
	} else {
109 110
		rql.udiag_rqueue = (u32) unix_inq_len(sk);
		rql.udiag_wqueue = (u32) unix_outq_len(sk);
111 112
	}

113
	return nla_put(nlskb, UNIX_DIAG_RQLEN, sizeof(rql), &rql);
114 115
}

116 117 118 119 120 121
static int sk_diag_dump_uid(struct sock *sk, struct sk_buff *nlskb)
{
	uid_t uid = from_kuid_munged(sk_user_ns(nlskb->sk), sock_i_uid(sk));
	return nla_put(nlskb, UNIX_DIAG_UID, sizeof(uid_t), &uid);
}

122
static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
123
		u32 portid, u32 seq, u32 flags, int sk_ino)
124 125 126 127
{
	struct nlmsghdr *nlh;
	struct unix_diag_msg *rep;

128
	nlh = nlmsg_put(skb, portid, seq, SOCK_DIAG_BY_FAMILY, sizeof(*rep),
129
			flags);
130
	if (!nlh)
131
		return -EMSGSIZE;
132

133
	rep = nlmsg_data(nlh);
134 135 136
	rep->udiag_family = AF_UNIX;
	rep->udiag_type = sk->sk_type;
	rep->udiag_state = sk->sk_state;
M
Mathias Krause 已提交
137
	rep->pad = 0;
138 139 140
	rep->udiag_ino = sk_ino;
	sock_diag_save_cookie(sk, rep->udiag_cookie);

P
Pavel Emelyanov 已提交
141
	if ((req->udiag_show & UDIAG_SHOW_NAME) &&
142
	    sk_diag_dump_name(sk, skb))
143
		goto out_nlmsg_trim;
P
Pavel Emelyanov 已提交
144

P
Pavel Emelyanov 已提交
145
	if ((req->udiag_show & UDIAG_SHOW_VFS) &&
146
	    sk_diag_dump_vfs(sk, skb))
147
		goto out_nlmsg_trim;
P
Pavel Emelyanov 已提交
148

P
Pavel Emelyanov 已提交
149
	if ((req->udiag_show & UDIAG_SHOW_PEER) &&
150
	    sk_diag_dump_peer(sk, skb))
151
		goto out_nlmsg_trim;
P
Pavel Emelyanov 已提交
152

153
	if ((req->udiag_show & UDIAG_SHOW_ICONS) &&
154
	    sk_diag_dump_icons(sk, skb))
155
		goto out_nlmsg_trim;
156

157
	if ((req->udiag_show & UDIAG_SHOW_RQLEN) &&
158
	    sk_diag_show_rqlen(sk, skb))
159
		goto out_nlmsg_trim;
160 161 162

	if ((req->udiag_show & UDIAG_SHOW_MEMINFO) &&
	    sock_diag_put_meminfo(sk, skb, UNIX_DIAG_MEMINFO))
163
		goto out_nlmsg_trim;
164

165 166 167
	if (nla_put_u8(skb, UNIX_DIAG_SHUTDOWN, sk->sk_shutdown))
		goto out_nlmsg_trim;

168 169 170 171
	if ((req->udiag_show & UDIAG_SHOW_UID) &&
	    sk_diag_dump_uid(sk, skb))
		goto out_nlmsg_trim;

172 173
	nlmsg_end(skb, nlh);
	return 0;
174

175
out_nlmsg_trim:
176
	nlmsg_cancel(skb, nlh);
177 178 179 180
	return -EMSGSIZE;
}

static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, struct unix_diag_req *req,
181
		u32 portid, u32 seq, u32 flags)
182 183 184 185 186 187 188 189 190 191
{
	int sk_ino;

	unix_state_lock(sk);
	sk_ino = sock_i_ino(sk);
	unix_state_unlock(sk);

	if (!sk_ino)
		return 0;

192
	return sk_diag_fill(sk, skb, req, portid, seq, flags, sk_ino);
193 194
}

P
Pavel Emelyanov 已提交
195 196
static int unix_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
{
197 198
	struct unix_diag_req *req;
	int num, s_num, slot, s_slot;
A
Andrey Vagin 已提交
199
	struct net *net = sock_net(skb->sk);
200

201
	req = nlmsg_data(cb->nlh);
202 203 204 205 206

	s_slot = cb->args[0];
	num = s_num = cb->args[1];

	spin_lock(&unix_table_lock);
E
Eric Dumazet 已提交
207 208 209
	for (slot = s_slot;
	     slot < ARRAY_SIZE(unix_socket_table);
	     s_num = 0, slot++) {
210 211 212
		struct sock *sk;

		num = 0;
213
		sk_for_each(sk, &unix_socket_table[slot]) {
A
Andrey Vagin 已提交
214 215
			if (!net_eq(sock_net(sk), net))
				continue;
216 217 218 219 220
			if (num < s_num)
				goto next;
			if (!(req->udiag_states & (1 << sk->sk_state)))
				goto next;
			if (sk_diag_dump(sk, skb, req,
221
					 NETLINK_CB(cb->skb).portid,
222 223
					 cb->nlh->nlmsg_seq,
					 NLM_F_MULTI) < 0)
224 225 226 227 228 229 230 231 232 233 234
				goto done;
next:
			num++;
		}
	}
done:
	spin_unlock(&unix_table_lock);
	cb->args[0] = slot;
	cb->args[1] = num;

	return skb->len;
P
Pavel Emelyanov 已提交
235 236
}

237
static struct sock *unix_lookup_by_ino(unsigned int ino)
238 239 240 241 242
{
	int i;
	struct sock *sk;

	spin_lock(&unix_table_lock);
E
Eric Dumazet 已提交
243
	for (i = 0; i < ARRAY_SIZE(unix_socket_table); i++) {
244
		sk_for_each(sk, &unix_socket_table[i])
245 246 247 248 249 250 251 252 253 254 255 256
			if (ino == sock_i_ino(sk)) {
				sock_hold(sk);
				spin_unlock(&unix_table_lock);

				return sk;
			}
	}

	spin_unlock(&unix_table_lock);
	return NULL;
}

P
Pavel Emelyanov 已提交
257 258 259 260
static int unix_diag_get_exact(struct sk_buff *in_skb,
			       const struct nlmsghdr *nlh,
			       struct unix_diag_req *req)
{
261 262 263 264
	int err = -EINVAL;
	struct sock *sk;
	struct sk_buff *rep;
	unsigned int extra_len;
A
Andrey Vagin 已提交
265
	struct net *net = sock_net(in_skb->sk);
266 267 268 269 270 271 272 273

	if (req->udiag_ino == 0)
		goto out_nosk;

	sk = unix_lookup_by_ino(req->udiag_ino);
	err = -ENOENT;
	if (sk == NULL)
		goto out_nosk;
274 275
	if (!net_eq(sock_net(sk), net))
		goto out;
276 277 278 279 280 281 282 283

	err = sock_diag_check_cookie(sk, req->udiag_cookie);
	if (err)
		goto out;

	extra_len = 256;
again:
	err = -ENOMEM;
284
	rep = nlmsg_new(sizeof(struct unix_diag_msg) + extra_len, GFP_KERNEL);
285 286 287
	if (!rep)
		goto out;

288
	err = sk_diag_fill(sk, rep, req, NETLINK_CB(in_skb).portid,
289 290
			   nlh->nlmsg_seq, 0, req->udiag_ino);
	if (err < 0) {
291
		nlmsg_free(rep);
292 293 294 295 296 297
		extra_len += 256;
		if (extra_len >= PAGE_SIZE)
			goto out;

		goto again;
	}
298
	err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
299 300 301 302 303 304 305 306
			      MSG_DONTWAIT);
	if (err > 0)
		err = 0;
out:
	if (sk)
		sock_put(sk);
out_nosk:
	return err;
P
Pavel Emelyanov 已提交
307 308 309 310 311
}

static int unix_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
{
	int hdrlen = sizeof(struct unix_diag_req);
A
Andrey Vagin 已提交
312
	struct net *net = sock_net(skb->sk);
P
Pavel Emelyanov 已提交
313 314 315 316

	if (nlmsg_len(h) < hdrlen)
		return -EINVAL;

317 318 319 320
	if (h->nlmsg_flags & NLM_F_DUMP) {
		struct netlink_dump_control c = {
			.dump = unix_diag_dump,
		};
A
Andrey Vagin 已提交
321
		return netlink_dump_start(net->diag_nlsk, skb, h, &c);
322
	} else
323
		return unix_diag_get_exact(skb, h, nlmsg_data(h));
P
Pavel Emelyanov 已提交
324 325
}

326
static const struct sock_diag_handler unix_diag_handler = {
P
Pavel Emelyanov 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	.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 */);