sock_diag.c (7985B)
1/* License: GPL */ 2 3#include <linux/filter.h> 4#include <linux/mutex.h> 5#include <linux/socket.h> 6#include <linux/skbuff.h> 7#include <net/netlink.h> 8#include <net/net_namespace.h> 9#include <linux/module.h> 10#include <net/sock.h> 11#include <linux/kernel.h> 12#include <linux/tcp.h> 13#include <linux/workqueue.h> 14#include <linux/nospec.h> 15#include <linux/cookie.h> 16#include <linux/inet_diag.h> 17#include <linux/sock_diag.h> 18 19static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; 20static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); 21static DEFINE_MUTEX(sock_diag_table_mutex); 22static struct workqueue_struct *broadcast_wq; 23 24DEFINE_COOKIE(sock_cookie); 25 26u64 __sock_gen_cookie(struct sock *sk) 27{ 28 while (1) { 29 u64 res = atomic64_read(&sk->sk_cookie); 30 31 if (res) 32 return res; 33 res = gen_cookie_next(&sock_cookie); 34 atomic64_cmpxchg(&sk->sk_cookie, 0, res); 35 } 36} 37 38int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie) 39{ 40 u64 res; 41 42 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE) 43 return 0; 44 45 res = sock_gen_cookie(sk); 46 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1]) 47 return -ESTALE; 48 49 return 0; 50} 51EXPORT_SYMBOL_GPL(sock_diag_check_cookie); 52 53void sock_diag_save_cookie(struct sock *sk, __u32 *cookie) 54{ 55 u64 res = sock_gen_cookie(sk); 56 57 cookie[0] = (u32)res; 58 cookie[1] = (u32)(res >> 32); 59} 60EXPORT_SYMBOL_GPL(sock_diag_save_cookie); 61 62int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) 63{ 64 u32 mem[SK_MEMINFO_VARS]; 65 66 sk_get_meminfo(sk, mem); 67 68 return nla_put(skb, attrtype, sizeof(mem), &mem); 69} 70EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); 71 72int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, 73 struct sk_buff *skb, int attrtype) 74{ 75 struct sock_fprog_kern *fprog; 76 struct sk_filter *filter; 77 struct nlattr *attr; 78 unsigned int flen; 79 int err = 0; 80 81 if (!may_report_filterinfo) { 82 nla_reserve(skb, attrtype, 0); 83 return 0; 84 } 85 86 rcu_read_lock(); 87 filter = rcu_dereference(sk->sk_filter); 88 if (!filter) 89 goto out; 90 91 fprog = filter->prog->orig_prog; 92 if (!fprog) 93 goto out; 94 95 flen = bpf_classic_proglen(fprog); 96 97 attr = nla_reserve(skb, attrtype, flen); 98 if (attr == NULL) { 99 err = -EMSGSIZE; 100 goto out; 101 } 102 103 memcpy(nla_data(attr), fprog->filter, flen); 104out: 105 rcu_read_unlock(); 106 return err; 107} 108EXPORT_SYMBOL(sock_diag_put_filterinfo); 109 110struct broadcast_sk { 111 struct sock *sk; 112 struct work_struct work; 113}; 114 115static size_t sock_diag_nlmsg_size(void) 116{ 117 return NLMSG_ALIGN(sizeof(struct inet_diag_msg) 118 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */ 119 + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */ 120} 121 122static void sock_diag_broadcast_destroy_work(struct work_struct *work) 123{ 124 struct broadcast_sk *bsk = 125 container_of(work, struct broadcast_sk, work); 126 struct sock *sk = bsk->sk; 127 const struct sock_diag_handler *hndl; 128 struct sk_buff *skb; 129 const enum sknetlink_groups group = sock_diag_destroy_group(sk); 130 int err = -1; 131 132 WARN_ON(group == SKNLGRP_NONE); 133 134 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL); 135 if (!skb) 136 goto out; 137 138 mutex_lock(&sock_diag_table_mutex); 139 hndl = sock_diag_handlers[sk->sk_family]; 140 if (hndl && hndl->get_info) 141 err = hndl->get_info(skb, sk); 142 mutex_unlock(&sock_diag_table_mutex); 143 144 if (!err) 145 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group, 146 GFP_KERNEL); 147 else 148 kfree_skb(skb); 149out: 150 sk_destruct(sk); 151 kfree(bsk); 152} 153 154void sock_diag_broadcast_destroy(struct sock *sk) 155{ 156 /* Note, this function is often called from an interrupt context. */ 157 struct broadcast_sk *bsk = 158 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC); 159 if (!bsk) 160 return sk_destruct(sk); 161 bsk->sk = sk; 162 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work); 163 queue_work(broadcast_wq, &bsk->work); 164} 165 166void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) 167{ 168 mutex_lock(&sock_diag_table_mutex); 169 inet_rcv_compat = fn; 170 mutex_unlock(&sock_diag_table_mutex); 171} 172EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); 173 174void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) 175{ 176 mutex_lock(&sock_diag_table_mutex); 177 inet_rcv_compat = NULL; 178 mutex_unlock(&sock_diag_table_mutex); 179} 180EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); 181 182int sock_diag_register(const struct sock_diag_handler *hndl) 183{ 184 int err = 0; 185 186 if (hndl->family >= AF_MAX) 187 return -EINVAL; 188 189 mutex_lock(&sock_diag_table_mutex); 190 if (sock_diag_handlers[hndl->family]) 191 err = -EBUSY; 192 else 193 sock_diag_handlers[hndl->family] = hndl; 194 mutex_unlock(&sock_diag_table_mutex); 195 196 return err; 197} 198EXPORT_SYMBOL_GPL(sock_diag_register); 199 200void sock_diag_unregister(const struct sock_diag_handler *hnld) 201{ 202 int family = hnld->family; 203 204 if (family >= AF_MAX) 205 return; 206 207 mutex_lock(&sock_diag_table_mutex); 208 BUG_ON(sock_diag_handlers[family] != hnld); 209 sock_diag_handlers[family] = NULL; 210 mutex_unlock(&sock_diag_table_mutex); 211} 212EXPORT_SYMBOL_GPL(sock_diag_unregister); 213 214static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh) 215{ 216 int err; 217 struct sock_diag_req *req = nlmsg_data(nlh); 218 const struct sock_diag_handler *hndl; 219 220 if (nlmsg_len(nlh) < sizeof(*req)) 221 return -EINVAL; 222 223 if (req->sdiag_family >= AF_MAX) 224 return -EINVAL; 225 req->sdiag_family = array_index_nospec(req->sdiag_family, AF_MAX); 226 227 if (sock_diag_handlers[req->sdiag_family] == NULL) 228 sock_load_diag_module(req->sdiag_family, 0); 229 230 mutex_lock(&sock_diag_table_mutex); 231 hndl = sock_diag_handlers[req->sdiag_family]; 232 if (hndl == NULL) 233 err = -ENOENT; 234 else if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY) 235 err = hndl->dump(skb, nlh); 236 else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy) 237 err = hndl->destroy(skb, nlh); 238 else 239 err = -EOPNOTSUPP; 240 mutex_unlock(&sock_diag_table_mutex); 241 242 return err; 243} 244 245static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, 246 struct netlink_ext_ack *extack) 247{ 248 int ret; 249 250 switch (nlh->nlmsg_type) { 251 case TCPDIAG_GETSOCK: 252 case DCCPDIAG_GETSOCK: 253 if (inet_rcv_compat == NULL) 254 sock_load_diag_module(AF_INET, 0); 255 256 mutex_lock(&sock_diag_table_mutex); 257 if (inet_rcv_compat != NULL) 258 ret = inet_rcv_compat(skb, nlh); 259 else 260 ret = -EOPNOTSUPP; 261 mutex_unlock(&sock_diag_table_mutex); 262 263 return ret; 264 case SOCK_DIAG_BY_FAMILY: 265 case SOCK_DESTROY: 266 return __sock_diag_cmd(skb, nlh); 267 default: 268 return -EINVAL; 269 } 270} 271 272static DEFINE_MUTEX(sock_diag_mutex); 273 274static void sock_diag_rcv(struct sk_buff *skb) 275{ 276 mutex_lock(&sock_diag_mutex); 277 netlink_rcv_skb(skb, &sock_diag_rcv_msg); 278 mutex_unlock(&sock_diag_mutex); 279} 280 281static int sock_diag_bind(struct net *net, int group) 282{ 283 switch (group) { 284 case SKNLGRP_INET_TCP_DESTROY: 285 case SKNLGRP_INET_UDP_DESTROY: 286 if (!sock_diag_handlers[AF_INET]) 287 sock_load_diag_module(AF_INET, 0); 288 break; 289 case SKNLGRP_INET6_TCP_DESTROY: 290 case SKNLGRP_INET6_UDP_DESTROY: 291 if (!sock_diag_handlers[AF_INET6]) 292 sock_load_diag_module(AF_INET6, 0); 293 break; 294 } 295 return 0; 296} 297 298int sock_diag_destroy(struct sock *sk, int err) 299{ 300 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 301 return -EPERM; 302 303 if (!sk->sk_prot->diag_destroy) 304 return -EOPNOTSUPP; 305 306 return sk->sk_prot->diag_destroy(sk, err); 307} 308EXPORT_SYMBOL_GPL(sock_diag_destroy); 309 310static int __net_init diag_net_init(struct net *net) 311{ 312 struct netlink_kernel_cfg cfg = { 313 .groups = SKNLGRP_MAX, 314 .input = sock_diag_rcv, 315 .bind = sock_diag_bind, 316 .flags = NL_CFG_F_NONROOT_RECV, 317 }; 318 319 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); 320 return net->diag_nlsk == NULL ? -ENOMEM : 0; 321} 322 323static void __net_exit diag_net_exit(struct net *net) 324{ 325 netlink_kernel_release(net->diag_nlsk); 326 net->diag_nlsk = NULL; 327} 328 329static struct pernet_operations diag_net_ops = { 330 .init = diag_net_init, 331 .exit = diag_net_exit, 332}; 333 334static int __init sock_diag_init(void) 335{ 336 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0); 337 BUG_ON(!broadcast_wq); 338 return register_pernet_subsys(&diag_net_ops); 339} 340device_initcall(sock_diag_init);