sock_diag.c 8.0 KB

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